Class: Three::Euler

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/three/math/euler.rb

Constant Summary collapse

DEFAULT_ORDER =
"XYZ"
ORDERS =
%w[XYZ YXZ ZXY ZYX YZX XZY].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(x = 0, y = 0, z = 0, order = DEFAULT_ORDER) ⇒ Euler

Returns a new instance of Euler.



15
16
17
18
19
20
21
22
23
# File 'lib/three/math/euler.rb', line 15

def initialize(x = 0, y = 0, z = 0, order = DEFAULT_ORDER)
  validate_order!(order)

  @x = x
  @y = y
  @z = z
  @order = order
  @on_change_callback = proc {}
end

Instance Attribute Details

#orderObject

Returns the value of attribute order.



13
14
15
# File 'lib/three/math/euler.rb', line 13

def order
  @order
end

#xObject

Returns the value of attribute x.



13
14
15
# File 'lib/three/math/euler.rb', line 13

def x
  @x
end

#yObject

Returns the value of attribute y.



13
14
15
# File 'lib/three/math/euler.rb', line 13

def y
  @y
end

#zObject

Returns the value of attribute z.



13
14
15
# File 'lib/three/math/euler.rb', line 13

def z
  @z
end

Instance Method Details

#==(other) ⇒ Object



153
154
155
# File 'lib/three/math/euler.rb', line 153

def ==(other)
  other.is_a?(self.class) && equals?(other)
end

#cloneObject



57
58
59
# File 'lib/three/math/euler.rb', line 57

def clone
  self.class.new(@x, @y, @z, @order)
end

#copy(euler, update: true) ⇒ Object



61
62
63
# File 'lib/three/math/euler.rb', line 61

def copy(euler, update: true)
  set(euler.x, euler.y, euler.z, euler.order, update: update)
end

#deconstructObject



177
178
179
# File 'lib/three/math/euler.rb', line 177

def deconstruct
  to_a
end

#each {|@x| ... } ⇒ Object

Yields:

  • (@x)


164
165
166
167
168
169
170
171
# File 'lib/three/math/euler.rb', line 164

def each
  return enum_for(:each) unless block_given?

  yield @x
  yield @y
  yield @z
  yield @order
end

#equals?(euler, epsilon: 0.0) ⇒ Boolean

Returns:

  • (Boolean)


146
147
148
149
150
151
# File 'lib/three/math/euler.rb', line 146

def equals?(euler, epsilon: 0.0)
  @order == euler.order &&
    (@x - euler.x).abs <= epsilon &&
    (@y - euler.y).abs <= epsilon &&
    (@z - euler.z).abs <= epsilon
end

#inspectObject



181
182
183
# File 'lib/three/math/euler.rb', line 181

def inspect
  "#<#{self.class} x=#{@x.inspect} y=#{@y.inspect} z=#{@z.inspect} order=#{@order.inspect}>"
end

#on_change(&callback) ⇒ Object Also known as: _on_change



157
158
159
160
# File 'lib/three/math/euler.rb', line 157

def on_change(&callback)
  @on_change_callback = callback || proc {}
  self
end

#set(x, y, z, order = @order, update: true) ⇒ Object



46
47
48
49
50
51
52
53
54
55
# File 'lib/three/math/euler.rb', line 46

def set(x, y, z, order = @order, update: true)
  validate_order!(order)

  @x = x
  @y = y
  @z = z
  @order = order
  changed! if update
  self
end

#set_from_quaternion(quaternion, order = @order, update: true) ⇒ Object



141
142
143
144
# File 'lib/three/math/euler.rb', line 141

def set_from_quaternion(quaternion, order = @order, update: true)
  matrix = Matrix4.new.make_rotation_from_quaternion(quaternion)
  set_from_rotation_matrix(matrix, order, update: update)
end

#set_from_rotation_matrix(matrix, order = @order, update: true) ⇒ Object



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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/three/math/euler.rb', line 65

def set_from_rotation_matrix(matrix, order = @order, update: true)
  validate_order!(order)

  elements = matrix.elements
  m11 = elements[0]
  m12 = elements[4]
  m13 = elements[8]
  m21 = elements[1]
  m22 = elements[5]
  m23 = elements[9]
  m31 = elements[2]
  m32 = elements[6]
  m33 = elements[10]

  case order
  when "XYZ"
    @y = Math.asin(MathUtils.clamp(m13, -1, 1))
    if m13.abs < 0.9999999
      @x = Math.atan2(-m23, m33)
      @z = Math.atan2(-m12, m11)
    else
      @x = Math.atan2(m32, m22)
      @z = 0
    end
  when "YXZ"
    @x = Math.asin(-MathUtils.clamp(m23, -1, 1))
    if m23.abs < 0.9999999
      @y = Math.atan2(m13, m33)
      @z = Math.atan2(m21, m22)
    else
      @y = Math.atan2(-m31, m11)
      @z = 0
    end
  when "ZXY"
    @x = Math.asin(MathUtils.clamp(m32, -1, 1))
    if m32.abs < 0.9999999
      @y = Math.atan2(-m31, m33)
      @z = Math.atan2(-m12, m22)
    else
      @y = 0
      @z = Math.atan2(m21, m11)
    end
  when "ZYX"
    @y = Math.asin(-MathUtils.clamp(m31, -1, 1))
    if m31.abs < 0.9999999
      @x = Math.atan2(m32, m33)
      @z = Math.atan2(m21, m11)
    else
      @x = 0
      @z = Math.atan2(-m12, m22)
    end
  when "YZX"
    @z = Math.asin(MathUtils.clamp(m21, -1, 1))
    if m21.abs < 0.9999999
      @x = Math.atan2(-m23, m22)
      @y = Math.atan2(-m31, m11)
    else
      @x = 0
      @y = Math.atan2(m13, m33)
    end
  when "XZY"
    @z = Math.asin(-MathUtils.clamp(m12, -1, 1))
    if m12.abs < 0.9999999
      @x = Math.atan2(m32, m22)
      @y = Math.atan2(m13, m11)
    else
      @x = Math.atan2(-m23, m33)
      @y = 0
    end
  end

  @order = order
  changed! if update
  self
end

#to_aObject



173
174
175
# File 'lib/three/math/euler.rb', line 173

def to_a
  [@x, @y, @z, @order]
end