Class: Eigen::Quaternion

Inherits:
Object
  • Object
show all
Defined in:
lib/eigen/quaternion.rb,
ext/eigen/eigen.cpp

Overview

A floating-point valued quaternion

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

._load(coordinates) ⇒ Object

:nodoc:



110
111
112
# File 'lib/eigen/quaternion.rb', line 110

def self._load(coordinates) # :nodoc:
    new(*Marshal.load(coordinates))
end

.from_angle_axis(*args) ⇒ Object

Creates a quaternion from an angle and axis description



23
24
25
26
27
# File 'lib/eigen/quaternion.rb', line 23

def self.from_angle_axis(*args)
        q = new(1, 0, 0, 0)
    q.from_angle_axis(*args)
    q
end

.from_euler(*args) ⇒ Object

Creates a quaternion from a set of euler angles.

See Quaternion#from_euler for details



63
64
65
66
67
# File 'lib/eigen/quaternion.rb', line 63

def self.from_euler(*args)
    q = new(1, 0, 0, 0)
    q.from_euler(*args)
    q
end

.from_matrix(m) ⇒ Object

Creates a quaternion from a rotation matrix



70
71
72
73
74
# File 'lib/eigen/quaternion.rb', line 70

def self.from_matrix(m)
    q = new(1, 0, 0, 0)
    q.from_matrix(m)
    q
end

.from_yaw(yaw) ⇒ Object

The inverse of #yaw



93
94
95
# File 'lib/eigen/quaternion.rb', line 93

def self.from_yaw(yaw)
    from_euler(Eigen::Vector3.new(yaw, 0, 0), 2, 1, 0)
end

.IdentityObject

Returns the identity unit quaternion (identity rotation)



12
13
14
# File 'lib/eigen/quaternion.rb', line 12

def self.Identity
        Quaternion.new(1, 0, 0, 0)
end

.UnitObject

DEPRECATED: please use identity instead. Returns the unit quaternion (identity rotation)



17
18
19
20
# File 'lib/eigen/quaternion.rb', line 17

def self.Unit
	    warn "[DEPRECATED] Quaternion.unit, please use Quaternion.identity."
	    self.Identity
end

Instance Method Details

#*(obj) ⇒ Object

Concatenates with another quaternion or transforms a vector



98
99
100
101
102
103
104
# File 'lib/eigen/quaternion.rb', line 98

def *(obj)
    if obj.kind_of?(Quaternion)
        concatenate(obj)
    else
        transform(obj)
    end
end

#==(q) ⇒ Object

Tests for equality

Since Quaternion stores the coordinates as floating-point values, this is a bad test. Use

(v - other_v).norm < threshold

instead



126
127
128
129
# File 'lib/eigen/quaternion.rb', line 126

def ==(q)
    q.kind_of?(self.class) &&
        __equal__(q)
end

#_dump(level) ⇒ Object

:nodoc:



106
107
108
# File 'lib/eigen/quaternion.rb', line 106

def _dump(level) # :nodoc:
    Marshal.dump(to_a)
end

#approx?(q, threshold = dummy_precision) ⇒ Boolean

Verifies that two quaternions are within threshold of each other, elementwise

Parameters:

Returns:

  • (Boolean)

#concatenate(q) ⇒ Quaternion

Quaternion multiplication

Parameters:

Returns:

#dupObject



4
5
6
# File 'lib/eigen/quaternion.rb', line 4

def dup
    Quaternion.new(w, x, y, z)
end

#from_angle_axis(angle_axis) ⇒ void

This method returns an undefined value.

Initializes from an angle-axis representation

Parameters:

  • an (AngleAxis)

    angle-axis representation of a rotation

#from_euler(v) ⇒ void

This method returns an undefined value.

Initializes from euler angles

Parameters:

  • v (Vector3)

    a 3-vector where .x is roll (rotation around X), .y is pitch and .z yaw

#from_matrix(matrix) ⇒ void

This method returns an undefined value.

Initializes from a rotation matrix

Parameters:

#imObject



139
140
141
# File 'lib/eigen/quaternion.rb', line 139

def im
    [x,y,z]
end

#im=(value) ⇒ Object



143
144
145
# File 'lib/eigen/quaternion.rb', line 143

def im=(value)
    self.x, self.y, self.z = *value
end

#inverseQuaternion

The quaternion inverse

Returns:

#matrixMatrixX

The rotation matrix equivalent to this unit quaternion

Returns:

#normNumeric

The norm

Returns:

  • (Numeric)

#normalizeQuaternion

Returns the self normalized

Returns:

#normalize!void

This method returns an undefined value.

Normalizes self

#pitchObject



84
85
86
# File 'lib/eigen/quaternion.rb', line 84

def pitch
    to_euler[1]
end

#reObject



131
132
133
# File 'lib/eigen/quaternion.rb', line 131

def re
    w
end

#re=(value) ⇒ Object



135
136
137
# File 'lib/eigen/quaternion.rb', line 135

def re=(value)
    self.w = value
end

#rollObject



88
89
90
# File 'lib/eigen/quaternion.rb', line 88

def roll
    to_euler[2]
end

#to_aObject

Returns the quaternion as [w, x, y, z]



9
# File 'lib/eigen/quaternion.rb', line 9

def to_a; [w, x, y, z] end

#to_angle_axis(eps = 1e-12) ⇒ (Float,Vector3)

Returns an angle,axis representation equivalent to this quaternion

If the angle turns out to be PI, there are two solutions and the one with positive Z component is chosen.

Parameters:

  • eps (Float) (defaults to: 1e-12)

    if the angle turns out to be closer to zero than eps, the rotation axis is undefined and chosen to be the Z axis.

Returns:

  • ((Float,Vector3))

    the angle and axis. The angle is in [0, PI]



38
39
40
41
42
43
44
45
46
47
48
# File 'lib/eigen/quaternion.rb', line 38

def to_angle_axis(eps = 1e-12)
    w, x, y, z = to_a
    norm  = Math.sqrt(x*x+y*y+z*z);
    if norm < eps
        return 0, Eigen::Vector3.new(0,0,1);
    end

    angle = 2.0 * Math.atan2(norm, w);
    axis  = Eigen::Vector3.new(x, y, z) / norm
    return angle, axis
end

#to_eulerVector3

Converts this quaternion into Euler-Bryant angles

Returns:

  • (Vector3)

    a 3-vector where .x is roll (rotation around X), .y is pitch and .z yaw

#to_qtQt::Quaternion

one

Returns:

  • (Qt::Quaternion)

    the Qt quaternion that is identical to this



248
249
250
# File 'lib/eigen/quaternion.rb', line 248

def to_qt
    Qt::Quaternion.new(w, x, y, z)
end

#to_sObject

:nodoc:



114
115
116
# File 'lib/eigen/quaternion.rb', line 114

def to_s # :nodoc:
    "Quaternion(#{w}, (#{x}, #{y}, #{z}))"
end

#to_scaled_axis(eps = 1e-12) ⇒ Vector3

Returns a scaled axis representation that is equivalent to this quaternion

Parameters:

Returns:



55
56
57
58
# File 'lib/eigen/quaternion.rb', line 55

def to_scaled_axis(eps = 1e-12)
    angle, axis = to_angle_axis(eps)
    return axis * angle
end

#transform(v) ⇒ Vector3

Transform a vector-3 by this quaternion

Parameters:

Returns:

#wNumeric

The real part of the quaternion

Returns:

  • (Numeric)

#w=(value) ⇒ Numeric

Sets the real part of the quaternion

Parameters:

  • value (Numeric)

Returns:

  • (Numeric)

#xNumeric

The first element of the imaginary part of the quaternion

Returns:

  • (Numeric)

#x=(value) ⇒ Numeric

Sets the first element of the imaginary part of the quaternion

Parameters:

  • value (Numeric)

Returns:

  • (Numeric)

#yNumeric

The second element of the imaginary part of the quaternion

Returns:

  • (Numeric)

#y=(value) ⇒ Numeric

Sets the second element of the imaginary part of the quaternion

Parameters:

  • value (Numeric)

Returns:

  • (Numeric)

#yawObject

Extracts the yaw angle from this quaternion

It decomposes the quaternion in euler angles using to_euler and returns the first element. See #to_euler for details.



80
81
82
# File 'lib/eigen/quaternion.rb', line 80

def yaw
    to_euler[0]
end

#zNumeric

The third element of the imaginary part of the quaternion

Returns:

  • (Numeric)

#z=(value) ⇒ Numeric

Sets the third element of the imaginary part of the quaternion

Parameters:

  • value (Numeric)

Returns:

  • (Numeric)