class Mittsu::Euler

Constants

DefaultOrder
RotationOrders

Attributes

order[R]
x[R]
y[R]
z[R]

Public Class Methods

new(x = 0.0, y = 0.0, z = 0.0, order = DefaultOrder) click to toggle source
# 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

Public Instance Methods

==(euler) click to toggle source
# File lib/mittsu/math/euler.rb, line 137
def ==(euler)
  (euler.x == @x) && (euler.y == @y) && (euler.z == @z) && (euler.order == @order)
end
clone() click to toggle source
# File lib/mittsu/math/euler.rb, line 176
def clone
  Mittsu::Euler.new(@x, @y, @z, @order)
end
copy(euler) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# File lib/mittsu/math/euler.rb, line 166
def on_change(&callback)
  @on_change_callback = callback
  self
end
on_change_callback() click to toggle source
# File lib/mittsu/math/euler.rb, line 171
def on_change_callback
  return unless @on_change_callback
  @on_change_callback.call
end
order=(order) click to toggle source
# File lib/mittsu/math/euler.rb, line 37
def order=(order)
  @order = order
  self.on_change_callback
end
reorder(new_order) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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
x=(x) click to toggle source
# File lib/mittsu/math/euler.rb, line 22
def x=(x)
  @x = x.to_f
  self.on_change_callback
end
y=(y) click to toggle source
# File lib/mittsu/math/euler.rb, line 27
def y=(y)
  @y = y.to_f
  self.on_change_callback
end
z=(z) click to toggle source
# File lib/mittsu/math/euler.rb, line 32
def z=(z)
  @z = z.to_f
  self.on_change_callback
end