Class: Mittsu::Euler
- Inherits:
-
Object
- Object
- Mittsu::Euler
- Defined in:
- lib/mittsu/math/euler.rb
Constant Summary collapse
- RotationOrders =
[ 'XYZ', 'YZX', 'ZXY', 'XZY', 'YXZ', 'ZYX' ]
- DefaultOrder =
'XYZ'
Instance Attribute Summary collapse
-
#order ⇒ Object
Returns the value of attribute order.
-
#x ⇒ Object
Returns the value of attribute x.
-
#y ⇒ Object
Returns the value of attribute y.
-
#z ⇒ Object
Returns the value of attribute z.
Instance Method Summary collapse
- #==(euler) ⇒ Object
- #clone ⇒ Object
- #copy(euler) ⇒ Object
- #from_array(array) ⇒ Object
-
#initialize(x = 0.0, y = 0.0, z = 0.0, order = DefaultOrder) ⇒ Euler
constructor
A new instance of Euler.
- #on_change(&callback) ⇒ Object
- #on_change_callback ⇒ Object
- #reorder(new_order) ⇒ Object
- #set(x, y, z, order = nil) ⇒ Object
- #set_from_quaternion(q, order, update = true) ⇒ Object
- #set_from_rotation_matrix(m, order, update = true) ⇒ Object
- #set_from_vector3(v, order) ⇒ Object
- #to_a(array = [], offset = 0) ⇒ Object
- #to_vector3(optional_result = nil) ⇒ Object
Constructor Details
#initialize(x = 0.0, y = 0.0, z = 0.0, order = DefaultOrder) ⇒ Euler
Returns a new instance of Euler.
8 9 10 11 |
# File 'lib/mittsu/math/euler.rb', line 8 def initialize(x = 0.0, y = 0.0, z = 0.0, order = DefaultOrder) @x, @y, @z, @order = x.to_f, y.to_f, z.to_f, order @on_change_callback = nil end |
Instance Attribute Details
#order ⇒ Object
Returns the value of attribute order.
6 7 8 |
# File 'lib/mittsu/math/euler.rb', line 6 def order @order end |
#x ⇒ Object
Returns the value of attribute x.
6 7 8 |
# File 'lib/mittsu/math/euler.rb', line 6 def x @x end |
#y ⇒ Object
Returns the value of attribute y.
6 7 8 |
# File 'lib/mittsu/math/euler.rb', line 6 def y @y end |
#z ⇒ Object
Returns the value of attribute z.
6 7 8 |
# File 'lib/mittsu/math/euler.rb', line 6 def z @z end |
Instance Method Details
#==(euler) ⇒ Object
137 138 139 |
# File 'lib/mittsu/math/euler.rb', line 137 def ==(euler) (euler.x == @x) && (euler.y == @y) && (euler.z == @z) && (euler.order == @order) end |
#clone ⇒ Object
176 177 178 |
# File 'lib/mittsu/math/euler.rb', line 176 def clone Mittsu::Euler.new(@x, @y, @z, @order) end |
#copy(euler) ⇒ Object
42 43 44 45 46 47 48 49 |
# File 'lib/mittsu/math/euler.rb', line 42 def copy(euler) @x = euler.x @y = euler.y @z = euler.z @order = euler.order self.on_change_callback self end |
#from_array(array) ⇒ Object
141 142 143 144 145 146 147 148 |
# File 'lib/mittsu/math/euler.rb', line 141 def from_array(array) @x = array[0] @y = array[1] @z = array[2] @order = array[3] unless array[3].nil? self.on_change_callback self end |
#on_change(&callback) ⇒ Object
166 167 168 169 |
# File 'lib/mittsu/math/euler.rb', line 166 def on_change(&callback) @on_change_callback = callback self end |
#on_change_callback ⇒ Object
171 172 173 174 |
# File 'lib/mittsu/math/euler.rb', line 171 def on_change_callback return unless @on_change_callback @on_change_callback.call end |
#reorder(new_order) ⇒ Object
131 132 133 134 135 |
# File 'lib/mittsu/math/euler.rb', line 131 def reorder(new_order) # WARNING: this discards revolution information -bhouston q = Mittsu::Quaternion.new.set_from_euler(self) self.set_from_quaternion(q, new_order) end |
#set(x, y, z, order = nil) ⇒ Object
13 14 15 16 17 18 19 20 |
# File 'lib/mittsu/math/euler.rb', line 13 def set(x, y, z, order = nil) @x = x.to_f @y = y.to_f @z = z.to_f @order = order || @order self.on_change_callback self end |
#set_from_quaternion(q, order, update = true) ⇒ Object
120 121 122 123 124 125 |
# File 'lib/mittsu/math/euler.rb', line 120 def set_from_quaternion(q, order, update = true) matrix = Mittsu::Matrix4.new matrix.make_rotation_from_quaternion(q) self.set_from_rotation_matrix(matrix, order, update) self end |
#set_from_rotation_matrix(m, order, update = true) ⇒ Object
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 |
# File 'lib/mittsu/math/euler.rb', line 51 def set_from_rotation_matrix(m, order, update = true) # assumes the upper 3x3 of m is a pure rotation matrix (i.e, unscaled) te = m.elements m11 = te[0]; m12 = te[4]; m13 = te[8] m21 = te[1]; m22 = te[5]; m23 = te[9] m31 = te[2]; m32 = te[6]; m33 = te[10] order = order || @order if order == 'XYZ' @y = ::Math.asin(Math.clamp(m13, -1.0, 1.0)) if m13.abs < 0.99999 @x = ::Math.atan2(- m23, m33) @z = ::Math.atan2(- m12, m11) else @x = ::Math.atan2(m32, m22) @z = 0.0 end elsif order == 'YXZ' @x = ::Math.asin(- Math.clamp(m23, -1.0, 1.0)) if m23.abs < 0.99999 @y = ::Math.atan2(m13, m33) @z = ::Math.atan2(m21, m22) else @y = ::Math.atan2(- m31, m11) @z = 0.0 end elsif order == 'ZXY' @x = ::Math.asin(Math.clamp(m32, -1.0, 1.0)) if m32.abs < 0.99999 @y = ::Math.atan2(- m31, m33) @z = ::Math.atan2(- m12, m22) else @y = 0.0 @z = ::Math.atan2(m21, m11) end elsif order == 'ZYX' @y = ::Math.asin(- Math.clamp(m31, -1.0, 1.0)) if m31.abs < 0.99999 @x = ::Math.atan2(m32, m33) @z = ::Math.atan2(m21, m11) else @x = 0.0 @z = ::Math.atan2(- m12, m22) end elsif order == 'YZX' @z = ::Math.asin(Math.clamp(m21, -1.0, 1.0)) if m21.abs < 0.99999 @x = ::Math.atan2(- m23, m22) @y = ::Math.atan2(- m31, m11) else @x = 0.0 @y = ::Math.atan2(m13, m33) end elsif order == 'XZY' @z = ::Math.asin(- Math.clamp(m12, -1.0, 1.0)) if m12.abs < 0.99999 @x = ::Math.atan2(m32, m22) @y = ::Math.atan2(m13, m11) else @x = ::Math.atan2(- m23, m33) @y = 0.0 end else puts("WARNING: Mittsu::Euler#set_from_rotation_matrix given unsupported order: #{order}") end @order = order self.on_change_callback if update self end |
#set_from_vector3(v, order) ⇒ Object
127 128 129 |
# File 'lib/mittsu/math/euler.rb', line 127 def set_from_vector3(v, order) self.set(v.x, v.y, v.z, order || @order) end |
#to_a(array = [], offset = 0) ⇒ Object
150 151 152 153 154 155 156 |
# File 'lib/mittsu/math/euler.rb', line 150 def to_a(array = [], offset = 0) array[offset] = @x array[offset + 1] = @y array[offset + 2] = @z array[offset + 3] = @order array end |