Class: Assimp::Matrix3x3

Inherits:
FFI::Struct
  • Object
show all
Extended by:
StructAccessors
Defined in:
lib/assimp/matrix3x3.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from StructAccessors

extended, has_ref?, struct_array_attr_accessor, struct_array_attr_checker, struct_array_attr_reader, struct_array_attr_writer, struct_attr_accessor, struct_attr_reader, struct_attr_writer, struct_ref_array_attr_accessor, struct_ref_array_attr_reader, struct_ref_array_attr_writer

Class Method Details

.identityObject



12
13
14
15
16
# File 'lib/assimp/matrix3x3.rb', line 12

def self.identity
  m = Matrix3x3::new
  Assimp::aiIdentityMatrix3(m)
  m
end

.rotation(a, axis) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/assimp/matrix3x3.rb', line 18

def self.rotation(a, axis)
  # https://en.wikipedia.org/wiki/Rotation_matrix#Axis_and_angle
  c = Math::cos(a)
  s = Math::sin(a)
  t = 1 - c
  x = axis.x
  y = axis.y
  z = axis.z

  out = Matrix3x3
  out.a1 = t*x*x + c
  out.a2 = t*x*y - s*z
  out.a3 = t*x*z + s*y
  out.b1 = t*x*y + s*z
  out.b2 = t*y*y + c
  out.b3 = t*y*z - s*x
  out.c1 = t*x*z - s*y
  out.c2 = t*y*z + s*x
  out.c3 = t*z*z + c
  out
end

Instance Method Details

#*(other) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/assimp/matrix3x3.rb', line 63

def *(other)
  if other.kind_of?(Matrix3x3)
    m = self.dup
    Assimp::aiMultiplyMatrix3(m, other)
    m
  elsif other.kind_of?(Vector3D)
    v = other.dup
    Assimp::aiTransformVecByMatrix3(v, self)
    v
  else
    raise "Unsupported operand: #{other.inspect}!"
  end
end

#determinantObject



77
78
79
# File 'lib/assimp/matrix3x3.rb', line 77

def determinant
  a1*b2*c3 - a1*b3*c2 + a2*b3*c1 - a2*b1*c3 + a3*b1*c2 - a3*b2*c1
end

#inverseObject



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/assimp/matrix3x3.rb', line 81

def inverse
  det = determinant
  raise "Not inversible!" if det == 0.0
  m = Matrix3x3::new
  invdet = 1.0/det
  m.a1 = invdet  * (b2 * c3 - b3 * c2)
  m.a2 = -invdet * (a2 * c3 - a3 * c2)
  m.a3 = invdet  * (a2 * b3 - a3 * b2)
  m.b1 = -invdet * (b1 * c3 - b3 * c1)
  m.b2 = invdet  * (a1 * c3 - a3 * c1)
  m.b3 = -invdet * (a1 * b3 - a3 * b1)
  m.c1 = invdet  * (b1 * c2 - b2 * c1)
  m.c2 = -invdet * (a1 * c2 - a2 * c1)
  m.c3 = invdet  * (a1 * b2 - a2 * b1)
  m
end

#quaternionObject



48
49
50
51
# File 'lib/assimp/matrix3x3.rb', line 48

def quaternion
  q = Quaternion::new
  Assimp::aiCreateQuaternionFromMatrix(q, self)
end

#to_sObject



40
41
42
43
44
45
46
# File 'lib/assimp/matrix3x3.rb', line 40

def to_s
  <<EOF
< <#{a1}, #{a2}, #{a3}>,
  <#{b1}, #{b2}, #{b3}>,
  <#{c1}, #{c2}, #{c3}> >
EOF
end

#transposeObject



58
59
60
61
# File 'lib/assimp/matrix3x3.rb', line 58

def transpose
  t = self.dup
  t.transpose!
end

#transpose!Object



53
54
55
56
# File 'lib/assimp/matrix3x3.rb', line 53

def transpose!
  Assimp::aiTransposeMatrix3(self)
  self
end