Class: BulldogPhysics::Matrix3

Inherits:
Object
  • Object
show all
Defined in:
lib/matrix3.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Matrix3

Returns a new instance of Matrix3.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/matrix3.rb', line 9

def initialize(*args)
  @data = Array.new

  if(args.size == 0)
    @data = [ 0, 0, 0, \
					    0, 0, 0, \
					    0, 0, 0]
	elsif( args[0].is_a? Float)			
    @data = [ args[0], args[1], args[2],\
							  args[3], args[4], args[5],\
								args[6], args[7], args[8]]
  elsif( args[0].is_a? Vector3)
      setComponents(args[0], args[1], args[2])
  end
end

Instance Attribute Details

#dataObject

Returns the value of attribute data.



6
7
8
# File 'lib/matrix3.rb', line 6

def data
  @data
end

Class Method Details

.linearInterpolate(a, b, prop) ⇒ Object



228
229
230
231
232
233
234
# File 'lib/matrix3.rb', line 228

def self.linearInterpolate(a, b, prop)
		result = Matrix3.new
		for i in 0..9
			result.data[i] = a.data * (1-prop) + b.data[i] * prop
		end
		return result
end

Instance Method Details

#*(vector) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/matrix3.rb', line 27

def *(vector)
	
	if( @data.size < 8)
		raise Exception.new("@data not big enough for vector math")
	end

	if( vector.is_a? Matrix3)
		o = vector.dup
		return Matrix3.new(@data[0]*o.data[0] + @data[1]*o.data[3] + @data[2]*o.data[6], @data[0]*o.data[1] + @data[1]*o.data[4] + @data[2]*o.data[7], @data[0]*o.data[2] + @data[1]*o.data[5] + @data[2]*o.data[8], \
		                   @data[3]*o.data[0] + @data[4]*o.data[3] + @data[5]*o.data[6], @data[3]*o.data[1] + @data[4]*o.data[4] + @data[5]*o.data[7], @data[3]*o.data[2] + @data[4]*o.data[5] + @data[5]*o.data[8], \
		                   @data[6]*o.data[0] + @data[7]*o.data[3] + @data[8]*o.data[6], @data[6]*o.data[1] + @data[7]*o.data[4] + @data[8]*o.data[7], @data[6]*o.data[2] + @data[7]*o.data[5] + @data[8]*o.data[8] )
	elsif vector.is_a? Float
		scalar = vector
		return Matrix3.new(@data[0] * scalar, @data[1] * scalar, @data[2] * scalar, \
						   @data[3] * scalar, @data[4] * scalar, @data[5] * scalar, \
						   @data[6] * scalar, @data[7] * scalar, @data[8] * scalar)
	else
		
	  return Vector3.new( (vector.x * @data[0]) + (vector.y * @data[1]) + (vector.z * @data[2]) , \
									vector.x * @data[3] + vector.y * @data[4] + vector.z * @data[5] , \
									vector.x * @data[6] + vector.y * @data[7] + vector.z * @data[8])
	end

end

#+(o) ⇒ Object



52
53
54
55
56
57
58
# File 'lib/matrix3.rb', line 52

def +(o)
	Matrix3.new( 
		@data[0] + o.data[0], @data[1] + o.data[1], @data[2] + o.data[2], \
		@data[3] + o.data[3], @data[4] + o.data[4], @data[5] + o.data[5], \
		@data[6] + o.data[6], @data[7] + o.data[7], @data[8] + o.data[8] \
	  )
end

#getAxisVector(i) ⇒ Object



113
114
115
# File 'lib/matrix3.rb', line 113

def getAxisVector(i)
	Vector3.new( @data[i], @data[i+3], @data[i+6])
end

#inverseObject



148
149
150
151
152
# File 'lib/matrix3.rb', line 148

def inverse()
	result = Matrix3.new
	result.setInverse(self)
	return result
end

#invertObject



154
155
156
# File 'lib/matrix3.rb', line 154

def invert()
	setInverse(self)
end

#multiplyByMatrix(o) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/matrix3.rb', line 60

def multiplyByMatrix(o)
	o = o.dup
	
	t1 = @data[0]*o.data[0] + @data[1]*o.data[3] + @data[2]*o.data[6]; 
	t2 = @data[0]*o.data[1] + @data[1]*o.data[4] + @data[2]*o.data[7]; 
	t3 = @data[0]*o.data[2] + @data[1]*o.data[5] + @data[2]*o.data[8]; 
	@data[0] = t1;
	@data[1] = t2; 
	@data[2] = t3;
	t1 = @data[3]*o.data[0] + @data[4]*o.data[3] + @data[5]*o.data[6]; 
	t2 = @data[3]*o.data[1] + @data[4]*o.data[4] + @data[5]*o.data[7]; 
	t3 = @data[3]*o.data[2] + @data[4]*o.data[5] + @data[5]*o.data[8]; 
	@data[3] = t1;
	@data[4] = t2; 
	@data[5] = t3;
	t1 = @data[6]*o.data[0] + @data[7]*o.data[3] + @data[8]*o.data[6]; 
	t2 = @data[6]*o.data[1] + @data[7]*o.data[4] + @data[8]*o.data[7];
	t3 = @data[6]*o.data[2] + @data[7]*o.data[5] + @data[8]*o.data[8]; 
	@data[6] = t1;
	@data[7] = t2;
	@data[8] = t3;
end

#multiplyByScalar(scalar) ⇒ Object



83
84
85
86
87
# File 'lib/matrix3.rb', line 83

def multiplyByScalar(scalar)
	@data[0] *= scalar; @data[1] *= scalar; @data[2] *= scalar;
	@data[3] *= scalar; @data[4] *= scalar; @data[5] *= scalar;
	@data[6] *= scalar; @data[7] *= scalar; @data[8] *= scalar;
end

#setBlockInertiaTensor(vHalfSizes, mass) ⇒ Object



199
200
201
202
203
204
# File 'lib/matrix3.rb', line 199

def setBlockInertiaTensor(vHalfSizes, mass)
	squares = vHalfSizes.componentProduct(vHalfSizes)
	setInertiaTensorCoeffs(0.3 * mass * (squares.y + squares.z),
												 0.3 * mass * (squares.x + squares.z),
												 0.3 * mass * (squares.x + squares.y))
end

#setComponents(vCompOne, vCompTwo, vCompThree) ⇒ Object



216
217
218
219
220
221
222
223
224
225
226
# File 'lib/matrix3.rb', line 216

def setComponents(vCompOne, vCompTwo, vCompThree)
	@data[0] = vCompOne.x
	@data[1] = vCompTwo.x
	@data[2] = vCompThree.x
	@data[3] = vCompOne.y
	@data[4] = vCompTwo.y
	@data[5] = vCompThree.y
	@data[6] = vCompOne.z
	@data[7] = vCompTwo.z
	@data[8] = vCompThree.z
end

#setDiagonal(a, b, c) ⇒ Object



185
186
187
# File 'lib/matrix3.rb', line 185

def setDiagonal(a, b, c)
	setInertiaTensorCoeffs(a, b, c)
end

#setInertiaTensorCoeffs(ix, iy, iz, ixy = 0, ixz = 0, iyz = 0) ⇒ Object



189
190
191
192
193
194
195
196
# File 'lib/matrix3.rb', line 189

def setInertiaTensorCoeffs(ix, iy, iz, ixy = 0, ixz = 0,iyz = 0)
	@data[0] = ix;
	@data[1] = @data[3] = -ixy;
	@data[2] = @data[6] = -ixz;
	@data[4] = iy;
	@data[5] = @data[7] = -iyz;
	@data[8] = iz;
end

#setInverse(m) ⇒ 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
144
145
146
# File 'lib/matrix3.rb', line 118

def setInverse(m)
	t4 = m.data[0]*m.data[4];
	t6 = m.data[0]*m.data[5];
	t8 = m.data[1]*m.data[3];
	t10 = m.data[2]*m.data[3];
	t12 = m.data[1]*m.data[6];
	t14 = m.data[2]*m.data[6];
	
	# Calculate the determinant
	t16 = (t4*m.data[8] - t6*m.data[7] - t8*m.data[8]+
							t10*m.data[7] + t12*m.data[5] - t14*m.data[4]);
	
	# Make sure the determinant is non-zero.
	if (t16 == 0.0) 
		return
	end
	
	t17 = 1/t16;
	
	@data[0] = (m.data[4]*m.data[8]-m.data[5]*m.data[7])*t17;
	@data[1] = -(m.data[1]*m.data[8]-m.data[2]*m.data[7])*t17;
	@data[2] = (m.data[1]*m.data[5]-m.data[2]*m.data[4])*t17;
	@data[3] = -(m.data[3]*m.data[8]-m.data[5]*m.data[6])*t17;
	@data[4] = (m.data[0]*m.data[8]-t14)*t17;
	@data[5] = -(t6-t10)*t17;
	@data[6] = (m.data[3]*m.data[7]-m.data[4]*m.data[6])*t17;
	@data[7] = -(m.data[0]*m.data[7]-t12)*t17;
	@data[8] = (t4-t8)*t17;
end

#setOrientation(q) ⇒ Object



101
102
103
104
105
106
107
108
109
110
111
# File 'lib/matrix3.rb', line 101

def setOrientation(q)
	@data[0] = 1 - (2*q.j*q.j + 2*q.k*q.k);
	@data[1] = 2*q.i*q.j + 2*q.k*q.r;
	@data[2] = 2*q.i*q.k - 2*q.j*q.r;
	@data[3] = 2*q.i*q.j - 2*q.k*q.r;
	@data[4] = 1 - (2*q.i*q.i  + 2*q.k*q.k);
	@data[5] = 2*q.j*q.k + 2*q.i*q.r;
	@data[6] = 2*q.i*q.k + 2*q.j*q.r;
	@data[7] = 2*q.j*q.k - 2*q.i*q.r;
	@data[8] = 1 - (2*q.i*q.i  + 2*q.j*q.j);
end

#setSkewSymmetric(vector) ⇒ Object



206
207
208
209
210
211
212
213
214
# File 'lib/matrix3.rb', line 206

def setSkewSymmetric(vector)
	@data[0] = @data[4] = @data[8] = 0;
	@data[1] = -vector.z;
	@data[2] = vector.y;
	@data[3] = vector.z;
	@data[5] = -vector.x;
	@data[6] = -vector.y;
	@data[7] = vector.x;
end

#setTranspose(m) ⇒ Object



158
159
160
161
162
163
164
165
166
167
168
# File 'lib/matrix3.rb', line 158

def setTranspose(m)
	@data[0] = m.data[0];
	@data[1] = m.data[3];
	@data[2] = m.data[6];
	@data[3] = m.data[1];
	@data[4] = m.data[4];
	@data[5] = m.data[7];
	@data[6] = m.data[2];
	@data[7] = m.data[5];
	@data[8] = m.data[8];
end

#to_sObject



236
237
238
# File 'lib/matrix3.rb', line 236

def to_s
	"[ #{@data[0]} #{@data[1]} #{@data[2]}\n \ #{@data[3]} #{@data[4]} #{@data[5]}\n \ #{@data[6]} #{@data[7]} #{@data[8]} ]"
end

#transform(vector) ⇒ Object



89
90
91
# File 'lib/matrix3.rb', line 89

def transform(vector)
	self * vector
end

#transform_transpose(vector) ⇒ Object



171
172
173
174
175
176
177
# File 'lib/matrix3.rb', line 171

def transform_transpose(vector)
			return Vector3.new(
        vector.x * @data[0] + vector.y * @data[3] + vector.z * @data[6],
        vector.x * @data[1] + vector.y * @data[4] + vector.z * @data[7],
        vector.x * @data[2] + vector.y * @data[5] + vector.z * @data[8]
    );
end

#transformTranspose(vector) ⇒ Object



93
94
95
96
97
98
99
# File 'lib/matrix3.rb', line 93

def transformTranspose(vector)
	Vector3.new(
			vector.x * @data[0] + vector.y * @data[3] + vector.z * @data[6],
			vector.x * @data[1] + vector.y * @data[4] + vector.z * @data[7],
			vector.x * @data[2] + vector.y * @data[5] + vector.z * @data[8]
	)
end

#transposeObject



179
180
181
182
183
# File 'lib/matrix3.rb', line 179

def transpose()
	result = Matrix3.new
	result.setTranspose(self)
	return result
end