Class: Mittsu::Euler

Inherits:
Object
  • Object
show all
Defined in:
lib/mittsu/math/euler.rb

Constant Summary collapse

RotationOrders =
[ 'XYZ', 'YZX', 'ZXY', 'XZY', 'YXZ', 'ZYX' ]
DefaultOrder =
'XYZ'

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#orderObject

Returns the value of attribute order.



6
7
8
# File 'lib/mittsu/math/euler.rb', line 6

def order
  @order
end

#xObject

Returns the value of attribute x.



6
7
8
# File 'lib/mittsu/math/euler.rb', line 6

def x
  @x
end

#yObject

Returns the value of attribute y.



6
7
8
# File 'lib/mittsu/math/euler.rb', line 6

def y
  @y
end

#zObject

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

#cloneObject



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_callbackObject



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

#to_vector3(optional_result = nil) ⇒ Object



158
159
160
161
162
163
164
# File 'lib/mittsu/math/euler.rb', line 158

def to_vector3(optional_result = nil)
  if optional_result
    return optional_result.set(@x, @y, @z)
  else
    return Mittsu::Vector3.new(@x, @y, @z)
  end
end