Class: Assimp::Matrix4x4

Inherits:
FFI::Struct
  • Object
show all
Extended by:
StructAccessors
Defined in:
lib/assimp/matrix4x4.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



14
15
16
17
18
# File 'lib/assimp/matrix4x4.rb', line 14

def self.identity
  m = Matrix4x4::new
  Assimp::aiIdentityMatrix4(m)
  m
end

.rotation(a, axis) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/assimp/matrix4x4.rb', line 20

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 = Matrix4x4
  out.a1 = t*x*x + c
  out.a2 = t*x*y - s*z
  out.a3 = t*x*z + s*y
  out.a4 = 0.0
  out.b1 = t*x*y + s*z
  out.b2 = t*y*y + c
  out.b3 = t*y*z - s*x
  out.b4 = 0.0
  out.c1 = t*x*z - s*y
  out.c2 = t*y*z + s*x
  out.c3 = t*z*z + c
  out.c4 = 0.0
  out.d1 = 0.0
  out.d2 = 0.0
  out.d3 = 0.0
  out.d4 = 1.0
  out
end

.scaling(vec) ⇒ Object



57
58
59
60
61
62
63
# File 'lib/assimp/matrix4x4.rb', line 57

def self.scaling(vec)
   out = identity
   out.a1 = v.x
   out.b2 = v.y
   out.c3 = v.z
   out
end

.translation(vec) ⇒ Object



49
50
51
52
53
54
55
# File 'lib/assimp/matrix4x4.rb', line 49

def self.translation(vec)
  out = identity
  out.a4 = vec.x
  out.b4 = vec.y
  out.c4 = vec.z
  out
end

Instance Method Details

#*(other) ⇒ Object



97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/assimp/matrix4x4.rb', line 97

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

#decomposeObject



79
80
81
82
83
84
85
# File 'lib/assimp/matrix4x4.rb', line 79

def decompose
  scaling = Vector3D::new
  rotation = Quaternion::new
  position = Vector3D::new
  Assimp::aiDecomposeMatrix(self, scaling, rotation, position)
  [scaling, rotation, position]
end

#determinantObject



111
112
113
114
115
116
117
118
# File 'lib/assimp/matrix4x4.rb', line 111

def determinant
  a1*b2*c3*d4 - a1*b2*c4*d3 + a1*b3*c4*d2 - a1*b3*c2*d4 +
  a1*b4*c2*d3 - a1*b4*c3*d2 - a2*b3*c4*d1 + a2*b3*c1*d4 -
  a2*b4*c1*d3 + a2*b4*c3*d1 - a2*b1*c3*d4 + a2*b1*c4*d3 +
  a3*b4*c1*d2 - a3*b4*c2*d1 + a3*b1*c2*d4 - a3*b1*c4*d2 +
  a3*b2*c4*d1 - a3*b2*c1*d4 - a4*b1*c2*d3 + a4*b1*c3*d2 -
  a4*b2*c3*d1 + a4*b2*c1*d3 - a4*b3*c1*d2 + a4*b3*c2*d1
end

#identity!Object



65
66
67
68
# File 'lib/assimp/matrix4x4.rb', line 65

def identity!
  Assimp::aiIdentityMatrix4(self)
  self
end

#inverseObject



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# File 'lib/assimp/matrix4x4.rb', line 120

def inverse
  det = determinant
  raise "Not inversible!" if det == 0.0
  m = Matrix4x4::new
  invdet = 1.0/det
  m.a1 = invdet  * (b2 * (c3 * d4 - c4 * d3) +
                    b3 * (c4 * d2 - c2 * d4) +
                    b4 * (c2 * d3 - c3 * d2))
  m.a2 = -invdet * (a2 * (c3 * d4 - c4 * d3) +
                    a3 * (c4 * d2 - c2 * d4) +
                    a4 * (c2 * d3 - c3 * d2))
  m.a3 = invdet  * (a2 * (b3 * d4 - b4 * d3) +
                    a3 * (b4 * d2 - b2 * d4) +
                    a4 * (b2 * d3 - b3 * d2))
  m.a4 = -invdet * (a2 * (b3 * c4 - b4 * c3) +
                    a3 * (b4 * c2 - b2 * c4) +
                    a4 * (b2 * c3 - b3 * c2))
  m.b1 = -invdet * (b1 * (c3 * d4 - c4 * d3) +
                    b3 * (c4 * d1 - c1 * d4) +
                    b4 * (c1 * d3 - c3 * d1))
  m.b2 = invdet  * (a1 * (c3 * d4 - c4 * d3) +
                    a3 * (c4 * d1 - c1 * d4) +
                    a4 * (c1 * d3 - c3 * d1))
  m.b3 = -invdet * (a1 * (b3 * d4 - b4 * d3) +
                    a3 * (b4 * d1 - b1 * d4) +
                    a4 * (b1 * d3 - b3 * d1))
  m.b4 = invdet  * (a1 * (b3 * c4 - b4 * c3) +
                    a3 * (b4 * c1 - b1 * c4) +
                    a4 * (b1 * c3 - b3 * c1))
  m.c1 = invdet  * (b1 * (c2 * d4 - c4 * d2) +
                    b2 * (c4 * d1 - c1 * d4) +
                    b4 * (c1 * d2 - c2 * d1))
  m.c2 = -invdet * (a1 * (c2 * d4 - c4 * d2) +
                    a2 * (c4 * d1 - c1 * d4) +
                    a4 * (c1 * d2 - c2 * d1))
  m.c3 = invdet  * (a1 * (b2 * d4 - b4 * d2) +
                    a2 * (b4 * d1 - b1 * d4) +
                    a4 * (b1 * d2 - b2 * d1))
  m.c4 = -invdet * (a1 * (b2 * c4 - b4 * c2) +
                    a2 * (b4 * c1 - b1 * c4) +
                    a4 * (b1 * c2 - b2 * c1))
  m.d1 = -invdet * (b1 * (c2 * d3 - c3 * d2) +
                    b2 * (c3 * d1 - c1 * d3) +
                    b3 * (c1 * d2 - c2 * d1))
  m.d2 = invdet  * (a1 * (c2 * d3 - c3 * d2) +
                    a2 * (c3 * d1 - c1 * d3) +
                    a3 * (c1 * d2 - c2 * d1))
  m.d3 = -invdet * (a1 * (b2 * d3 - b3 * d2) +
                    a2 * (b3 * d1 - b1 * d3) +
                    a3 * (b1 * d2 - b2 * d1))
  m.d4 = invdet  * (a1 * (b2 * c3 - b3 * c2) +
                    a2 * (b3 * c1 - b1 * c3) +
                    a3 * (b1 * c2 - b2 * c1))
  m
end

#to_sObject



70
71
72
73
74
75
76
77
# File 'lib/assimp/matrix4x4.rb', line 70

def to_s
  <<EOF
< <#{a1}, #{a2}, #{a3}, #{a4}>,
  <#{b1}, #{b2}, #{b3}, #{b4}>,
  <#{c1}, #{c2}, #{c3}, #{c4}>,
  <#{d1}, #{d2}, #{d3}, #{d4}> >
EOF
end

#transposeObject



92
93
94
95
# File 'lib/assimp/matrix4x4.rb', line 92

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

#transpose!Object



87
88
89
90
# File 'lib/assimp/matrix4x4.rb', line 87

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