rmath3d_plain 1.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,381 @@
1
+ require 'minitest/autorun'
2
+ class TC_RQuat < Minitest::Test
3
+ def setup
4
+ @tolerance = RMath3D::TOLERANCE
5
+ @zero = RQuat.new( 0, 0, 0, 0 )
6
+ end
7
+
8
+ def teardown
9
+ end
10
+
11
+ def test_initialize
12
+ q0 = RQuat.new
13
+ assert_in_delta( 0, q0.x, @tolerance )
14
+ assert_in_delta( 0, q0.y, @tolerance )
15
+ assert_in_delta( 0, q0.z, @tolerance )
16
+ assert_in_delta( 0, q0.w, @tolerance )
17
+
18
+ q1 = RQuat.new( 1, 2, 3, 4 )
19
+ assert_in_delta( 1, q1.x, @tolerance )
20
+ assert_in_delta( 2, q1.y, @tolerance )
21
+ assert_in_delta( 3, q1.z, @tolerance )
22
+ assert_in_delta( 4, q1.w, @tolerance )
23
+
24
+ q2 = RQuat.new( q1 )
25
+ assert_in_delta( 1, q2.x, @tolerance )
26
+ assert_in_delta( 2, q2.y, @tolerance )
27
+ assert_in_delta( 3, q2.z, @tolerance )
28
+ assert_in_delta( 4, q2.w, @tolerance )
29
+ end
30
+
31
+ def test_to_s
32
+ assert_respond_to( @zero, :to_s )
33
+ end
34
+
35
+ def test_coerce
36
+ assert_respond_to( @zero, :coerce )
37
+ end
38
+
39
+ def test_setElements
40
+ q = RQuat.new
41
+ q.setElements( 1, 2, 3, 4 )
42
+ assert_in_delta( 1, q.x, @tolerance )
43
+ assert_in_delta( 2, q.y, @tolerance )
44
+ assert_in_delta( 3, q.z, @tolerance )
45
+ assert_in_delta( 4, q.w, @tolerance )
46
+ end
47
+
48
+ def test_setElement
49
+ q = RQuat.new
50
+
51
+ # x=
52
+ q.x = 1
53
+ assert_in_delta( 1, q.x, @tolerance )
54
+ q[0] = 2
55
+ assert_in_delta( 2, q.x, @tolerance )
56
+
57
+ # y=
58
+ q.y = 1
59
+ assert_in_delta( 1, q.y, @tolerance )
60
+ q[1] = 2
61
+ assert_in_delta( 2, q.y, @tolerance )
62
+
63
+ # z=
64
+ q.z = 1
65
+ assert_in_delta( 1, q.z, @tolerance )
66
+ q[2] = 2
67
+ assert_in_delta( 2, q.z, @tolerance )
68
+
69
+ # w=
70
+ q.w = 1
71
+ assert_in_delta( 1, q.w, @tolerance )
72
+ q[3] = 2
73
+ assert_in_delta( 2, q.w, @tolerance )
74
+
75
+ # xyz=
76
+ v3 = RVec3.new( 4, 5, 6 )
77
+ q.xyz = v3
78
+ assert_in_delta( 4, q.x, @tolerance )
79
+ assert_in_delta( 5, q.y, @tolerance )
80
+ assert_in_delta( 6, q.z, @tolerance )
81
+
82
+ v3 = RVec3.new( 7, 8, 9 )
83
+ q.xyz = v3
84
+ assert_in_delta( 7, q.x, @tolerance )
85
+ assert_in_delta( 8, q.y, @tolerance )
86
+ assert_in_delta( 9, q.z, @tolerance )
87
+ end
88
+
89
+ def test_getElement
90
+ q = RQuat.new(1,2,3,4)
91
+ assert_in_delta( 1, q[0], @tolerance )
92
+ assert_in_delta( 2, q[1], @tolerance )
93
+ assert_in_delta( 3, q[2], @tolerance )
94
+ assert_in_delta( 4, q[3], @tolerance )
95
+
96
+ assert_in_delta( 1, q.x, @tolerance )
97
+ assert_in_delta( 2, q.y, @tolerance )
98
+ assert_in_delta( 3, q.z, @tolerance )
99
+ assert_in_delta( 4, q.w, @tolerance )
100
+
101
+ # RQuat#getXYZ, RQuat#xyz
102
+ v3 = q.xyz
103
+ assert_kind_of( RVec3, v3 )
104
+ assert_in_delta( 1, v3.x, @tolerance )
105
+ assert_in_delta( 2, v3.y, @tolerance )
106
+ assert_in_delta( 3, v3.z, @tolerance )
107
+
108
+ v3 = q.xyz
109
+ assert_kind_of( RVec3, v3 )
110
+ assert_in_delta( 1, v3.x, @tolerance )
111
+ assert_in_delta( 2, v3.y, @tolerance )
112
+ assert_in_delta( 3, v3.z, @tolerance )
113
+ end
114
+
115
+ def test_getLength
116
+ len_sq = 1.0*1.0 + 2.0*2.0 + 3.0*3.0 + 4.0*4.0
117
+ len = Math.sqrt( len_sq )
118
+
119
+ q = RQuat.new( 1, 2, 3, 4 )
120
+ assert_in_delta( len_sq, q.getLengthSq, @tolerance )
121
+ assert_in_delta( len , q.getLength , @tolerance )
122
+ end
123
+
124
+ def test_setIdentity
125
+ q = RQuat.new.setIdentity
126
+ assert_in_delta( 0, q.x, @tolerance )
127
+ assert_in_delta( 0, q.y, @tolerance )
128
+ assert_in_delta( 0, q.z, @tolerance )
129
+ assert_in_delta( 1, q.w, @tolerance )
130
+ end
131
+
132
+ def test_conjugate
133
+ q0 = RQuat.new( 1, 2, 3, 4 )
134
+
135
+ q1 = q0.getConjugated
136
+ assert_in_delta( -1, q1.x, @tolerance )
137
+ assert_in_delta( -2, q1.y, @tolerance )
138
+ assert_in_delta( -3, q1.z, @tolerance )
139
+ assert_in_delta( 4, q1.w, @tolerance )
140
+
141
+ q0.conjugate!
142
+ assert_in_delta( -1, q0.x, @tolerance )
143
+ assert_in_delta( -2, q0.y, @tolerance )
144
+ assert_in_delta( -3, q0.z, @tolerance )
145
+ assert_in_delta( 4, q0.w, @tolerance )
146
+ end
147
+
148
+ def test_invert
149
+ qa = RQuat.new( 1,0,0, 3 )
150
+ qb = RQuat.new( 5,1,-2, 0 )
151
+ qc = RQuat.new.rotationAxis( RVec3.new(0,0,1), Math::PI/4.0 )
152
+
153
+ qai = qa.getInverse
154
+ qi = qa * qai
155
+
156
+ assert_in_delta( 0, qi.x, @tolerance )
157
+ assert_in_delta( 0, qi.y, @tolerance )
158
+ assert_in_delta( 0, qi.z, @tolerance )
159
+ assert_in_delta( 1, qi.w, @tolerance )
160
+
161
+ qbi = qb.getInverse
162
+ qi = qbi * qb
163
+ assert_in_delta( 0, qi.x, @tolerance )
164
+ assert_in_delta( 0, qi.y, @tolerance )
165
+ assert_in_delta( 0, qi.z, @tolerance )
166
+ assert_in_delta( 1, qi.w, @tolerance )
167
+
168
+ qci = RQuat.new( qc )
169
+ qci.invert!
170
+
171
+ qr = qci * qc
172
+ assert_in_delta( 0, qr.x, @tolerance )
173
+ assert_in_delta( 0, qr.y, @tolerance )
174
+ assert_in_delta( 0, qr.z, @tolerance )
175
+ assert_in_delta( 1, qr.w, @tolerance )
176
+ end
177
+
178
+ def test_normalize
179
+ len_sq = 1.0*1.0 + 2.0*2.0 + 3.0*3.0 + 4.0*4.0
180
+ len = Math.sqrt( len_sq )
181
+ x = 1.0 / len
182
+ y = 2.0 / len
183
+ z = 3.0 / len
184
+ w = 4.0 / len
185
+ q = RQuat.new( 1, 2, 3, 4 )
186
+ # getNormalized
187
+ q0 = q.getNormalized
188
+ assert_in_delta( x, q0.x, @tolerance )
189
+ assert_in_delta( y, q0.y, @tolerance )
190
+ assert_in_delta( z, q0.z, @tolerance )
191
+ assert_in_delta( w, q0.w, @tolerance )
192
+
193
+ # normalize!
194
+ q.normalize!
195
+ assert_in_delta( x, q.x, @tolerance )
196
+ assert_in_delta( y, q.y, @tolerance )
197
+ assert_in_delta( z, q.z, @tolerance )
198
+ assert_in_delta( w, q.w, @tolerance )
199
+ end
200
+
201
+ def test_unary_operators
202
+ q = RQuat.new( 1, 2, 3, 4 )
203
+
204
+ # RQuat#+@
205
+ qp = +q
206
+ assert_in_delta( 1, qp.x, @tolerance )
207
+ assert_in_delta( 2, qp.y, @tolerance )
208
+ assert_in_delta( 3, qp.z, @tolerance )
209
+ assert_in_delta( 4, qp.w, @tolerance )
210
+
211
+ # RQuat#-@
212
+ qm = -q
213
+ assert_in_delta( -1, qm.x, @tolerance )
214
+ assert_in_delta( -2, qm.y, @tolerance )
215
+ assert_in_delta( -3, qm.z, @tolerance )
216
+ assert_in_delta( -4, qm.w, @tolerance )
217
+ end
218
+
219
+ def test_plus_operations
220
+ q0 = RQuat.new( 1, 1, 1, 1 )
221
+ q1 = RQuat.new( 2, 2, 2, 2 )
222
+
223
+ # RQuat#+
224
+ qr = q0 + q1
225
+ assert_in_delta( 3, qr.x, @tolerance )
226
+ assert_in_delta( 3, qr.y, @tolerance )
227
+ assert_in_delta( 3, qr.z, @tolerance )
228
+ assert_in_delta( 3, qr.w, @tolerance )
229
+
230
+ # RQuat#add!
231
+ q0.add!( q1 )
232
+ assert_in_delta( 3, q0.x, @tolerance )
233
+ assert_in_delta( 3, q0.y, @tolerance )
234
+ assert_in_delta( 3, q0.z, @tolerance )
235
+ assert_in_delta( 3, q0.w, @tolerance )
236
+
237
+ assert_raises( TypeError ) { q0 + 1.0 }
238
+ assert_raises( TypeError ) { 1.0 + q0 }
239
+ end
240
+
241
+ def test_minus_operations
242
+ q0 = RQuat.new( 1, 1, 1, 1 )
243
+ q1 = RQuat.new( 2, 2, 2, 2 )
244
+
245
+ # RQuat#-
246
+ qr = q0 - q1
247
+ assert_in_delta( -1, qr.x, @tolerance )
248
+ assert_in_delta( -1, qr.y, @tolerance )
249
+ assert_in_delta( -1, qr.z, @tolerance )
250
+ assert_in_delta( -1, qr.w, @tolerance )
251
+
252
+ # RQuat#sub!
253
+ q0.sub!( q1 )
254
+ assert_in_delta( -1, q0.x, @tolerance )
255
+ assert_in_delta( -1, q0.y, @tolerance )
256
+ assert_in_delta( -1, q0.z, @tolerance )
257
+ assert_in_delta( -1, q0.w, @tolerance )
258
+
259
+ assert_raises( TypeError ) { q0 - 1.0 }
260
+ assert_raises( TypeError ) { 1.0 - q0 }
261
+ end
262
+
263
+ def test_mult_operations
264
+ q0 = RQuat.new( 1, 1, 1, 1 )
265
+
266
+ qr = q0 * 2.0
267
+ assert_in_delta( 2.0, qr.x, @tolerance )
268
+ assert_in_delta( 2.0, qr.y, @tolerance )
269
+ assert_in_delta( 2.0, qr.z, @tolerance )
270
+ assert_in_delta( 2.0, qr.w, @tolerance )
271
+
272
+ qr = 2.0 * q0
273
+ assert_in_delta( 2.0, qr.x, @tolerance )
274
+ assert_in_delta( 2.0, qr.y, @tolerance )
275
+ assert_in_delta( 2.0, qr.z, @tolerance )
276
+ assert_in_delta( 2.0, qr.w, @tolerance )
277
+
278
+ q0.mul!( 2.0 )
279
+ assert_in_delta( 2.0, q0.x, @tolerance )
280
+ assert_in_delta( 2.0, q0.y, @tolerance )
281
+ assert_in_delta( 2.0, q0.z, @tolerance )
282
+ assert_in_delta( 2.0, q0.w, @tolerance )
283
+
284
+ # http://en.wikipedia.org/wiki/Quaternions
285
+ qa = RQuat.new( 1,0,0, 3 )
286
+ qb = RQuat.new( 5,1,-2, 0 )
287
+ qab = RQuat.new( 15,5,-5, -5 )
288
+ qba = RQuat.new( 15,1,-7, -5 )
289
+
290
+ qr = qa * qb
291
+ assert_in_delta( qab.x, qr.x, @tolerance )
292
+ assert_in_delta( qab.y, qr.y, @tolerance )
293
+ assert_in_delta( qab.z, qr.z, @tolerance )
294
+ assert_in_delta( qab.w, qr.w, @tolerance )
295
+
296
+ qr = qb * qa
297
+ assert_in_delta( qba.x, qr.x, @tolerance )
298
+ assert_in_delta( qba.y, qr.y, @tolerance )
299
+ assert_in_delta( qba.z, qr.z, @tolerance )
300
+ assert_in_delta( qba.w, qr.w, @tolerance )
301
+
302
+ qa.mul!( qb )
303
+ assert_in_delta( qab.x, qa.x, @tolerance )
304
+ assert_in_delta( qab.y, qa.y, @tolerance )
305
+ assert_in_delta( qab.z, qa.z, @tolerance )
306
+ assert_in_delta( qab.w, qa.w, @tolerance )
307
+ end
308
+
309
+ def test_equality_operations
310
+ v = RQuat.new
311
+ assert( v == @zero )
312
+
313
+ v0 = RQuat.new(1,2,3,4)
314
+ assert( v != v0 )
315
+ end
316
+
317
+ def test_rotationMatrix
318
+ rad = Math::PI/3
319
+ m0 = RMtx4.new.rotationAxis( RVec3.new(0,0,1), rad )
320
+ q0 = RQuat.new( 0, 0, Math::sin(rad/2), Math::cos(rad/2) )
321
+ q1 = RQuat.new.rotationMatrix( m0 )
322
+ assert_in_delta( q0.x, q1.x, @tolerance )
323
+ assert_in_delta( q0.y, q1.y, @tolerance )
324
+ assert_in_delta( q0.z, q1.z, @tolerance )
325
+ assert_in_delta( q0.w, q1.w, @tolerance )
326
+ end
327
+
328
+ def test_rotationAxis
329
+ rad = Math::PI/3
330
+ q0 = RQuat.new( 0, 0, Math::sin(rad/2), Math::cos(rad/2) )
331
+ q1 = RQuat.new.rotationAxis( RVec3.new(0,0,1), rad )
332
+ assert_in_delta( q0.x, q1.x, @tolerance )
333
+ assert_in_delta( q0.y, q1.y, @tolerance )
334
+ assert_in_delta( q0.z, q1.z, @tolerance )
335
+ assert_in_delta( q0.w, q1.w, @tolerance )
336
+ end
337
+
338
+ def test_toAxisAngle
339
+ rad = Math::PI/3
340
+ q0 = RQuat.new( 0, 0, Math::sin(rad/2), Math::cos(rad/2) )
341
+ axis, angle = q0.toAxisAngle
342
+
343
+ assert_kind_of( RVec3, axis )
344
+ assert_in_delta( 0, axis.x, @tolerance )
345
+ assert_in_delta( 0, axis.y, @tolerance )
346
+ assert_in_delta( 1, axis.z, @tolerance )
347
+ assert_in_delta( rad, angle, @tolerance )
348
+ end
349
+
350
+ def test_dot
351
+ q0 = RQuat.new( 1, 1, 1, 1 )
352
+ q1 = RQuat.new( 2, 2, 2, 2 )
353
+
354
+ assert_in_delta( 8.0, RQuat.dot(q0, q1), @tolerance )
355
+ end
356
+
357
+ def test_slerp
358
+ qs = RQuat.new.rotationAxis( RVec3.new(0,0,1), 1.0*Math::PI/6 )
359
+ qe = RQuat.new.rotationAxis( RVec3.new(0,0,1), 5.0*Math::PI/6 )
360
+
361
+ q = RQuat.slerp( qs, qe, 0.0 )
362
+ assert_in_delta( qs.x, q.x, @tolerance )
363
+ assert_in_delta( qs.y, q.y, @tolerance )
364
+ assert_in_delta( qs.z, q.z, @tolerance )
365
+ assert_in_delta( qs.w, q.w, @tolerance )
366
+
367
+ q = RQuat.slerp( qs, qe, 0.5 )
368
+ qr = RQuat.new( 0,0,Math::sin((Math::PI/2)/2), Math::cos((Math::PI/2)/2) )
369
+ assert_in_delta( qr.x, q.x, @tolerance )
370
+ assert_in_delta( qr.y, q.y, @tolerance )
371
+ assert_in_delta( qr.z, q.z, @tolerance )
372
+ assert_in_delta( qr.w, q.w, @tolerance )
373
+
374
+ q = RQuat.slerp( qs, qe, 1.0 )
375
+ assert_in_delta( qe.x, q.x, @tolerance )
376
+ assert_in_delta( qe.y, q.y, @tolerance )
377
+ assert_in_delta( qe.z, q.z, @tolerance )
378
+ assert_in_delta( qe.w, q.w, @tolerance )
379
+ end
380
+
381
+ end
@@ -0,0 +1,308 @@
1
+ require 'minitest/autorun'
2
+ class TC_RVec3 < Minitest::Test
3
+ def setup
4
+ @tolerance = RMath3D::TOLERANCE
5
+ @zero = RVec3.new( 0, 0, 0 )
6
+ @ax = RVec3.new( 1, 0, 0 )
7
+ @ay = RVec3.new( 0, 1, 0 )
8
+ @az = RVec3.new( 0, 0, 1 )
9
+ end
10
+
11
+ def teardown
12
+ end
13
+
14
+ def test_initialize
15
+ v0 = RVec3.new
16
+ assert_in_delta( 0, v0.x, @tolerance )
17
+ assert_in_delta( 0, v0.y, @tolerance )
18
+ assert_in_delta( 0, v0.z, @tolerance )
19
+
20
+ v1 = RVec3.new( 1, 2, 3 )
21
+ assert_in_delta( 1, v1.x, @tolerance )
22
+ assert_in_delta( 2, v1.y, @tolerance )
23
+ assert_in_delta( 3, v1.z, @tolerance )
24
+
25
+ v2 = RVec3.new( v1 )
26
+ assert_in_delta( 1, v2.x, @tolerance )
27
+ assert_in_delta( 2, v2.y, @tolerance )
28
+ assert_in_delta( 3, v2.z, @tolerance )
29
+ end
30
+
31
+ def test_to_s
32
+ assert_respond_to( @zero, :to_s )
33
+ end
34
+
35
+ def test_coerce
36
+ assert_respond_to( @zero, :coerce )
37
+ end
38
+
39
+ def test_setElements
40
+ v = RVec3.new
41
+ v.setElements( 1, 2, 3 )
42
+ assert_in_delta( 1, v.x, @tolerance )
43
+ assert_in_delta( 2, v.y, @tolerance )
44
+ assert_in_delta( 3, v.z, @tolerance )
45
+ end
46
+
47
+ def test_setElement
48
+ v = RVec3.new
49
+
50
+ # x=
51
+ v.x = 1
52
+ assert_in_delta( 1, v.x, @tolerance )
53
+ v[0] = 2
54
+ assert_in_delta( 2, v.x, @tolerance )
55
+
56
+ # y=
57
+ v.y = 1
58
+ assert_in_delta( 1, v.y, @tolerance )
59
+ v[1] = 2
60
+ assert_in_delta( 2, v.y, @tolerance )
61
+
62
+ # z=
63
+ v.z = 1
64
+ assert_in_delta( 1, v.z, @tolerance )
65
+ v[2] = 2
66
+ assert_in_delta( 2, v.z, @tolerance )
67
+ end
68
+
69
+ def test_getElement
70
+ assert_in_delta( 1, @ax[0], @tolerance )
71
+ assert_in_delta( 1, @ay[1], @tolerance )
72
+ assert_in_delta( 1, @az[2], @tolerance )
73
+
74
+ assert_in_delta( 1, @ax.x, @tolerance )
75
+ assert_in_delta( 1, @ay.y, @tolerance )
76
+ assert_in_delta( 1, @az.z, @tolerance )
77
+ end
78
+
79
+ def test_getLength
80
+ len_sq = 1.0*1.0 + 2.0*2.0 + 3.0*3.0 # == 14.0
81
+ len = Math.sqrt( len_sq ) # == 3.3166247903554
82
+
83
+ v = RVec3.new( 1, 2, 3 )
84
+ assert_in_delta( len_sq, v.getLengthSq, @tolerance )
85
+ assert_in_delta( len , v.getLength , @tolerance )
86
+ end
87
+
88
+ def test_normalize
89
+ len_sq = 1.0*1.0 + 2.0*2.0 + 3.0*3.0 # == 14.0
90
+ len = Math.sqrt( len_sq ) # == 3.3166247903554
91
+ x = 1.0 / len
92
+ y = 2.0 / len
93
+ z = 3.0 / len
94
+ v = RVec3.new( 1, 2, 3 )
95
+ # getNormalized
96
+ v0 = v.getNormalized
97
+ assert_in_delta( x, v0.x, @tolerance )
98
+ assert_in_delta( y, v0.y, @tolerance )
99
+ assert_in_delta( z, v0.z, @tolerance )
100
+
101
+ # normalize!
102
+ v.normalize!
103
+ assert_in_delta( x, v.x, @tolerance )
104
+ assert_in_delta( y, v.y, @tolerance )
105
+ assert_in_delta( z, v.z, @tolerance )
106
+ end
107
+
108
+ def test_unary_operators
109
+ v = RVec3.new( 1, 2, 3 )
110
+
111
+ # RVec3#+@
112
+ vp = +v
113
+ assert_in_delta( 1, vp.x, @tolerance )
114
+ assert_in_delta( 2, vp.y, @tolerance )
115
+ assert_in_delta( 3, vp.z, @tolerance )
116
+
117
+ # RVec3#-@
118
+ vm = -v
119
+ assert_in_delta( -1, vm.x, @tolerance )
120
+ assert_in_delta( -2, vm.y, @tolerance )
121
+ assert_in_delta( -3, vm.z, @tolerance )
122
+ end
123
+
124
+ def test_plus_operations
125
+ v0 = RVec3.new( 1, 1, 1 )
126
+ v1 = RVec3.new( 2, 2, 2 )
127
+
128
+ # RVec3#+
129
+ vr = v0 + v1
130
+ assert_in_delta( 3, vr.x, @tolerance )
131
+ assert_in_delta( 3, vr.y, @tolerance )
132
+ assert_in_delta( 3, vr.z, @tolerance )
133
+
134
+ # RVec3#add!
135
+ v0.add!( v1 )
136
+ assert_in_delta( 3, v0.x, @tolerance )
137
+ assert_in_delta( 3, v0.y, @tolerance )
138
+ assert_in_delta( 3, v0.z, @tolerance )
139
+
140
+ assert_raises( TypeError ) { v0 + 1.0 }
141
+ assert_raises( TypeError ) { 1.0 + v0 }
142
+ end
143
+
144
+ def test_minus_operations
145
+ v0 = RVec3.new( 1, 1, 1 )
146
+ v1 = RVec3.new( 2, 2, 2 )
147
+
148
+ # RVec3#-
149
+ vr = v0 - v1
150
+ assert_in_delta( -1, vr.x, @tolerance )
151
+ assert_in_delta( -1, vr.y, @tolerance )
152
+ assert_in_delta( -1, vr.z, @tolerance )
153
+
154
+ # RVec3#sub!
155
+ v0.sub!( v1 )
156
+ assert_in_delta( -1, v0.x, @tolerance )
157
+ assert_in_delta( -1, v0.y, @tolerance )
158
+ assert_in_delta( -1, v0.z, @tolerance )
159
+
160
+ assert_raises( TypeError ) { v0 - 1.0 }
161
+ assert_raises( TypeError ) { 1.0 - v0 }
162
+ end
163
+
164
+ def test_mult_operations
165
+ v0 = RVec3.new( 1, 1, 1 )
166
+
167
+ vr = v0 * 2.0
168
+ assert_in_delta( 2.0, vr.x, @tolerance )
169
+ assert_in_delta( 2.0, vr.y, @tolerance )
170
+ assert_in_delta( 2.0, vr.z, @tolerance )
171
+
172
+ vr = 2.0 * v0
173
+ assert_in_delta( 2.0, vr.x, @tolerance )
174
+ assert_in_delta( 2.0, vr.y, @tolerance )
175
+ assert_in_delta( 2.0, vr.z, @tolerance )
176
+
177
+ v0.mul!( 2.0 )
178
+ assert_in_delta( 2.0, v0.x, @tolerance )
179
+ assert_in_delta( 2.0, v0.y, @tolerance )
180
+ assert_in_delta( 2.0, v0.z, @tolerance )
181
+
182
+ assert_raises( TypeError ) { v0 * @zero }
183
+ end
184
+
185
+ def test_equality_operations
186
+ v = RVec3.new
187
+ assert( v == @zero )
188
+ assert( v != @ax )
189
+ end
190
+
191
+ def test_dot
192
+ v0 = RVec3.new( 1, 1, 1 )
193
+ v1 = RVec3.new( 2, 2, 2 )
194
+ assert_in_delta( 6.0, RVec3.dot(v0, v1), @tolerance )
195
+ assert_in_delta( 0.0, RVec3.dot(@ax, @ay), @tolerance )
196
+ # assert_raises( TypeError ) { RVec3.dot(@ax, 1.0) }
197
+ # assert_raises( TypeError ) { RVec3.dot(1.0, @ax) }
198
+ end
199
+
200
+ def test_cross
201
+ vr = RVec3.cross( @ax, @ay )
202
+ assert_in_delta( @az.x, vr.x, @tolerance )
203
+ assert_in_delta( @az.y, vr.y, @tolerance )
204
+ assert_in_delta( @az.z, vr.z, @tolerance )
205
+ end
206
+
207
+ def test_transform
208
+ m = RMtx4.new.rotationX( Math::PI/4.0 )
209
+ va = RVec3.new( 0.0, -Math.sqrt(2)/2, Math.sqrt(2)/2 )
210
+
211
+ vr = @az.transform( m )
212
+ assert_kind_of( RVec4, vr )
213
+ assert_in_delta( va.x, vr.x, @tolerance )
214
+ assert_in_delta( va.y, vr.y, @tolerance )
215
+ assert_in_delta( va.z, vr.z, @tolerance )
216
+
217
+ m = RMtx4.new.translation( 1.0, 1.0, 1.0 )
218
+ va = RVec3.new( 1.0, 1.0, 2.0 )
219
+
220
+ vr = @az.transform( m )
221
+ assert_kind_of( RVec4, vr )
222
+ assert_in_delta( va.x, vr.x, @tolerance )
223
+ assert_in_delta( va.y, vr.y, @tolerance )
224
+ assert_in_delta( va.z, vr.z, @tolerance )
225
+ end
226
+
227
+ def test_transformCoord
228
+ m = RMtx4.new.rotationX( Math::PI/4.0 )
229
+ va = RVec3.new( 0.0, -Math.sqrt(2)/2, Math.sqrt(2)/2 )
230
+
231
+ vr = @az.transformCoord( m )
232
+ assert_kind_of( RVec3, vr )
233
+ assert_in_delta( va.x, vr.x, @tolerance )
234
+ assert_in_delta( va.y, vr.y, @tolerance )
235
+ assert_in_delta( va.z, vr.z, @tolerance )
236
+
237
+ vr = RVec3.new( @az )
238
+ vr.transformCoord!( m )
239
+ assert_kind_of( RVec3, vr )
240
+ assert_in_delta( va.x, vr.x, @tolerance )
241
+ assert_in_delta( va.y, vr.y, @tolerance )
242
+ assert_in_delta( va.z, vr.z, @tolerance )
243
+
244
+ m = RMtx4.new.translation( 1.0, 1.0, 1.0 )
245
+ va = RVec3.new( 1.0, 1.0, 2.0 )
246
+
247
+ vr = @az.transformCoord( m )
248
+ assert_kind_of( RVec3, vr )
249
+ assert_in_delta( va.x, vr.x, @tolerance )
250
+ assert_in_delta( va.y, vr.y, @tolerance )
251
+ assert_in_delta( va.z, vr.z, @tolerance )
252
+ end
253
+
254
+ def test_transformNormal
255
+ m = RMtx4.new.rotationX( Math::PI/4.0 )
256
+ va = RVec3.new( 0.0, -Math.sqrt(2)/2, Math.sqrt(2)/2 )
257
+
258
+ vr = @az.transformNormal( m )
259
+ assert_kind_of( RVec3, vr )
260
+ assert_in_delta( va.x, vr.x, @tolerance )
261
+ assert_in_delta( va.y, vr.y, @tolerance )
262
+ assert_in_delta( va.z, vr.z, @tolerance )
263
+
264
+ vr = RVec3.new( @az )
265
+ vr.transformNormal!( m )
266
+ assert_kind_of( RVec3, vr )
267
+ assert_in_delta( va.x, vr.x, @tolerance )
268
+ assert_in_delta( va.y, vr.y, @tolerance )
269
+ assert_in_delta( va.z, vr.z, @tolerance )
270
+
271
+ m = RMtx4.new.translation( 1.0, 1.0, 1.0 )
272
+ va = RVec3.new( 1.0, 1.0, 2.0 )
273
+
274
+ vr = @az.transformNormal( m )
275
+ assert_kind_of( RVec3, vr )
276
+ assert_in_delta( @az.x, vr.x, @tolerance )
277
+ assert_in_delta( @az.y, vr.y, @tolerance )
278
+ assert_in_delta( @az.z, vr.z, @tolerance )
279
+ end
280
+
281
+ def test_transformRS
282
+ m = RMtx3.new.rotationX( Math::PI/4.0 )
283
+ va = RVec3.new( 0.0, -Math.sqrt(2)/2, Math.sqrt(2)/2 )
284
+
285
+ vr = @az.transformRS( m )
286
+ assert_kind_of( RVec3, vr )
287
+ assert_in_delta( va.x, vr.x, @tolerance )
288
+ assert_in_delta( va.y, vr.y, @tolerance )
289
+ assert_in_delta( va.z, vr.z, @tolerance )
290
+
291
+ vr = RVec3.new( @az )
292
+ vr.transformRS!( m )
293
+ assert_kind_of( RVec3, vr )
294
+ assert_in_delta( va.x, vr.x, @tolerance )
295
+ assert_in_delta( va.y, vr.y, @tolerance )
296
+ assert_in_delta( va.z, vr.z, @tolerance )
297
+
298
+ m = RMtx3.new.scaling( 2.0, 2.0, 2.0 )
299
+
300
+ vr = @az.transformRS( m )
301
+ va = RVec3.new( 0, 0, 2.0 )
302
+
303
+ assert_kind_of( RVec3, vr )
304
+ assert_in_delta( va.x, vr.x, @tolerance )
305
+ assert_in_delta( va.y, vr.y, @tolerance )
306
+ assert_in_delta( va.z, vr.z, @tolerance )
307
+ end
308
+ end