Class: Geom::Matrix3D

Inherits:
Matrix
  • Object
show all
Defined in:
lib/geom/matrix3d.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.identityObject



3
4
5
6
7
8
9
10
# File 'lib/geom/matrix3d.rb', line 3

def self.identity
  Matrix3D[
    [1,0,0,0],
    [0,1,0,0],
    [0,0,1,0],
    [0,0,0,1]
  ]
end

.multiply_vector_3x3(m, v) ⇒ Object



103
104
105
106
107
108
109
110
111
112
# File 'lib/geom/matrix3d.rb', line 103

def self.multiply_vector_3x3(m,v)
  ma = m.to_a
  vx = v.x
  vy = v.y
  vz = v.z

  v.x = vx * ma[0][0] + vy * ma[0][1] + vz * ma[0][2]
  v.y = vx * ma[1][0] + vy * ma[1][1] + vz * ma[1][2]
  v.z = vx * ma[2][0] + vy * ma[2][1] + vz * ma[2][2]
end

.reflection(plane) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/geom/matrix3d.rb', line 90

def self.reflection(plane)
  a = plane.normal.x
  b = plane.normal.y
  c = plane.normal.z

  self[
    [1-(2*a*a) , 0-(2*a*b) , 0-(2*a*c) , 0],
    [0-(2*a*b) , 1-(2*b*b) , 0-(2*b*c) , 0],
    [0-(2*a*c) , 0-(2*b*c) , 1-(2*c*c) , 0],
    [0         , 0         , 0         , 1]
  ]
end

.rotation(x, y, z, rad) ⇒ Object



12
13
14
15
16
17
18
19
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
# File 'lib/geom/matrix3d.rb', line 12

def self.rotation(x,y,z,rad)
  n_cos = Math.cos(rad)
  n_sin = Math.sin(rad)
  s_cos = 1 - n_cos

  sxy = x * y * s_cos
  syz = y * z * s_cos
  sxz = x * z * s_cos

  sz  = n_sin * z
  sy  = n_sin * y
  sx  = n_sin * x

  n11  = n_cos + x * x * s_cos
  n12  = -sz + sxy
  n13  = sy + sxz
  n14  = 0

  n21  = sz + sxy
  n22  = n_cos + y * y * s_cos
  n23  = -sx + syz
  n24  = 0

  n31  = -sy + sxz
  n32  = sx + syz
  n33  = n_cos + z * z * s_cos
  n34  = 0

  self[
    [n11,n12,n13,n14],
    [n21,n22,n23,n24],
    [n31,n32,n33,n34],
    [0,0,0,1]
  ]
end

.rotationX(rad) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
# File 'lib/geom/matrix3d.rb', line 48

def self.rotationX(rad)
  c = Math.cos(rad)
  s = Math.sin(rad)

  self[
    [1,0, 0,0],
    [0,c,-s,0],
    [0,s, c,0],
    [0,0, 0,1],
  ]
end

.rotationZ(rad) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
# File 'lib/geom/matrix3d.rb', line 60

def self.rotationZ(rad)
  c  = Math.cos(rad)
  s  = Math.sin(rad)

  self[
    [c,-s,0,0],
    [s, c,0,0],
    [0, 0,1,0],
    [0, 0,0,1]
  ]
end

.scale(x, y, z) ⇒ Object



81
82
83
84
85
86
87
88
# File 'lib/geom/matrix3d.rb', line 81

def self.scale(x,y,z)
  self[
    [x,0,0,0],
    [0,y,0,0],
    [0,0,z,0],
    [0,0,0,1]
  ]
end

.translation(x, y, z) ⇒ Object



72
73
74
75
76
77
78
79
# File 'lib/geom/matrix3d.rb', line 72

def self.translation(x,y,z)
  self[
    [1,0,0,x],
    [0,1,0,y],
    [0,0,1,z],
    [0,0,0,1]
  ]
end

Instance Method Details

#*(other) ⇒ Object



114
115
116
# File 'lib/geom/matrix3d.rb', line 114

def *(other)
  multiply(other)
end

#multiply(other) ⇒ Object



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/geom/matrix3d.rb', line 118

def multiply(other)
  # matrix multiplication is m[r][c] = (row[r]).(col[c])
  s = to_a
  o = other.to_a
  rm00 = s[0][0] * o[0][0] + s[0][1] * o[1][0] + s[0][2] * o[2][0]
  rm01 = s[0][0] * o[0][1] + s[0][1] * o[1][1] + s[0][2] * o[2][1]
  rm02 = s[0][0] * o[0][2] + s[0][1] * o[1][2] + s[0][2] * o[2][2]
  rm03 = s[0][0] * o[0][3] + s[0][1] * o[1][3] + s[0][2] * o[2][3] + s[0][3]

  rm10 = s[1][0] * o[0][0] + s[1][1] * o[1][0] + s[1][2] * o[2][0]
  rm11 = s[1][0] * o[0][1] + s[1][1] * o[1][1] + s[1][2] * o[2][1]
  rm12 = s[1][0] * o[0][2] + s[1][1] * o[1][2] + s[1][2] * o[2][2]
  rm13 = s[1][0] * o[0][3] + s[1][1] * o[1][3] + s[1][2] * o[2][3] + s[1][3]

  rm20 = s[2][0] * o[0][0] + s[2][1] * o[1][0] + s[2][2] * o[2][0]
  rm21 = s[2][0] * o[0][1] + s[2][1] * o[1][1] + s[2][2] * o[2][1]
  rm22 = s[2][0] * o[0][2] + s[2][1] * o[1][2] + s[2][2] * o[2][2]
  rm23 = s[2][0] * o[0][3] + s[2][1] * o[1][3] + s[2][2] * o[2][3] + s[2][3]

  Matrix3D[
    [rm00, rm01, rm02, rm03],
    [rm10, rm11, rm12, rm13],
    [rm20, rm21, rm22, rm23],
    [   0,    0,    0,    1]
  ]
end