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,735 @@
1
+ class TC_RMtx4 < Minitest::Test
2
+
3
+ def setup
4
+ @tolerance = RMath3D::TOLERANCE
5
+ @mZero = RMtx4.new.setZero
6
+ @mIdentity = RMtx4.new.setIdentity
7
+ end
8
+
9
+ def teardown
10
+ end
11
+
12
+ def test_RMtx_initialize
13
+ m0 = RMtx4.new
14
+ for r in 0...4 do
15
+ for c in 0...4 do
16
+ assert_equal( 0.0, m0.getElement(r,c) )
17
+ end
18
+ end
19
+
20
+ m1 = RMtx4.new( 0, 1, 2, 3,
21
+ 4, 5, 6, 7,
22
+ 8, 9,10,11,
23
+ 12,13,14,15 )
24
+ assert_equal( 0, m1.getElement(0,0) )
25
+ assert_equal( 1, m1.getElement(0,1) )
26
+ assert_equal( 2, m1.getElement(0,2) )
27
+ assert_equal( 3, m1.getElement(0,3) )
28
+ assert_equal( 4, m1.getElement(1,0) )
29
+ assert_equal( 5, m1.getElement(1,1) )
30
+ assert_equal( 6, m1.getElement(1,2) )
31
+ assert_equal( 7, m1.getElement(1,3) )
32
+ assert_equal( 8, m1.getElement(2,0) )
33
+ assert_equal( 9, m1.getElement(2,1) )
34
+ assert_equal(10, m1.getElement(2,2) )
35
+ assert_equal(11, m1.getElement(2,3) )
36
+ assert_equal(12, m1.getElement(3,0) )
37
+ assert_equal(13, m1.getElement(3,1) )
38
+ assert_equal(14, m1.getElement(3,2) )
39
+ assert_equal(15, m1.getElement(3,3) )
40
+
41
+ m2 = RMtx4.new( m1 )
42
+ for r in 0...4 do
43
+ for c in 0...4 do
44
+ assert_equal( 4*r+c, m2.getElement(r,c) )
45
+ end
46
+ end
47
+ end
48
+
49
+ def test_to_s
50
+ assert_respond_to( @mZero, :to_s )
51
+ end
52
+
53
+ def test_coerce
54
+ assert_respond_to( @mZero, :coerce )
55
+ end
56
+
57
+ def test_setElements
58
+ @mZero.setElements( 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 )
59
+ for r in 0...4 do
60
+ for c in 0...4 do
61
+ assert_equal( 4*r+c, @mZero.getElement(r,c) )
62
+ end
63
+ end
64
+ end
65
+
66
+ def test_setElement
67
+ for r in 0...4 do
68
+ for c in 0...4 do
69
+ @mZero.setElement( r, c, 4*r+c )
70
+ end
71
+ end
72
+ for r in 0...4 do
73
+ for c in 0...4 do
74
+ assert_equal( 4*r+c, @mZero.getElement(r,c) )
75
+ end
76
+ end
77
+
78
+ for r in 0...4 do
79
+ for c in 0...4 do
80
+ @mZero[ r, c ] = 4*c+r
81
+ end
82
+ end
83
+ for r in 0...4 do
84
+ for c in 0...4 do
85
+ assert_equal( 4*c+r, @mZero[r,c] )
86
+ end
87
+ end
88
+ end
89
+
90
+ def test_getElement
91
+ assert_respond_to( @mIdentity, :getElement )
92
+ for r in 0...4 do
93
+ for c in 0...4 do
94
+ e = @mIdentity.getElement( r, c )
95
+ if ( r == c )
96
+ assert_equal( 1.0, e )
97
+ else
98
+ assert_equal( 0.0, e )
99
+ end
100
+ end
101
+ end
102
+
103
+ for r in 0...4 do
104
+ for c in 0...4 do
105
+ e = @mIdentity[r, c]
106
+ if ( r == c )
107
+ assert_equal( 1.0, e )
108
+ else
109
+ assert_equal( 0.0, e )
110
+ end
111
+ end
112
+ end
113
+
114
+ mtx = RMtx4.new(1,2,3,4,
115
+ 5,6,7,8,
116
+ 9,10,11,12,
117
+ 13,14,15,16)
118
+ assert_equal( mtx.e00, 1 )
119
+ assert_equal( mtx.e01, 2 )
120
+ assert_equal( mtx.e02, 3 )
121
+ assert_equal( mtx.e03, 4 )
122
+ assert_equal( mtx.e10, 5 )
123
+ assert_equal( mtx.e11, 6 )
124
+ assert_equal( mtx.e12, 7 )
125
+ assert_equal( mtx.e13, 8 )
126
+ assert_equal( mtx.e20, 9 )
127
+ assert_equal( mtx.e21,10 )
128
+ assert_equal( mtx.e22,11 )
129
+ assert_equal( mtx.e23,12 )
130
+ assert_equal( mtx.e30,13 )
131
+ assert_equal( mtx.e31,14 )
132
+ assert_equal( mtx.e32,15 )
133
+ assert_equal( mtx.e33,16 )
134
+ end
135
+
136
+ def test_getRowColumn
137
+ mtx = RMtx4.new(1,2,3,4,
138
+ 5,6,7,8,
139
+ 9,10,11,12,
140
+ 13,14,15,16)
141
+
142
+ v = mtx.getRow(0)
143
+ assert_equal( v.x, 1 )
144
+ assert_equal( v.y, 2 )
145
+ assert_equal( v.z, 3 )
146
+ assert_equal( v.w, 4 )
147
+
148
+ v = mtx.getRow(1)
149
+ assert_equal( v.x, 5 )
150
+ assert_equal( v.y, 6 )
151
+ assert_equal( v.z, 7 )
152
+ assert_equal( v.w, 8 )
153
+
154
+ v = mtx.getRow(2)
155
+ assert_equal( v.x, 9 )
156
+ assert_equal( v.y, 10)
157
+ assert_equal( v.z, 11)
158
+ assert_equal( v.w, 12)
159
+
160
+ v = mtx.getRow(3)
161
+ assert_equal( v.x, 13)
162
+ assert_equal( v.y, 14)
163
+ assert_equal( v.z, 15)
164
+ assert_equal( v.w, 16)
165
+
166
+
167
+ v = mtx.getColumn(0)
168
+ assert_equal( v.x, 1 )
169
+ assert_equal( v.y, 5 )
170
+ assert_equal( v.z, 9 )
171
+ assert_equal( v.w, 13)
172
+
173
+ v = mtx.getColumn(1)
174
+ assert_equal( v.x, 2 )
175
+ assert_equal( v.y, 6 )
176
+ assert_equal( v.z, 10)
177
+ assert_equal( v.w, 14)
178
+
179
+ v = mtx.getColumn(2)
180
+ assert_equal( v.x, 3 )
181
+ assert_equal( v.y, 7 )
182
+ assert_equal( v.z, 11)
183
+ assert_equal( v.w, 15)
184
+
185
+ v = mtx.getColumn(3)
186
+ assert_equal( v.x, 4 )
187
+ assert_equal( v.y, 8 )
188
+ assert_equal( v.z, 12)
189
+ assert_equal( v.w, 16)
190
+ end
191
+
192
+ def test_setRowColumn
193
+ mtx = RMtx4.new
194
+
195
+ vr = [RVec4.new(1,2,3,4),RVec4.new(5,6,7,8),RVec4.new(9,10,11,12),RVec4.new(13,14,15,16)]
196
+ mtx.setRow(vr[0],0)
197
+ mtx.setRow(vr[1],1)
198
+ mtx.setRow(vr[2],2)
199
+ mtx.setRow(vr[3],3)
200
+ assert_equal( mtx.e00, 1 )
201
+ assert_equal( mtx.e01, 2 )
202
+ assert_equal( mtx.e02, 3 )
203
+ assert_equal( mtx.e03, 4 )
204
+ assert_equal( mtx.e10, 5 )
205
+ assert_equal( mtx.e11, 6 )
206
+ assert_equal( mtx.e12, 7 )
207
+ assert_equal( mtx.e13, 8 )
208
+ assert_equal( mtx.e20, 9 )
209
+ assert_equal( mtx.e21, 10)
210
+ assert_equal( mtx.e22, 11)
211
+ assert_equal( mtx.e23, 12)
212
+ assert_equal( mtx.e30, 13)
213
+ assert_equal( mtx.e31, 14)
214
+ assert_equal( mtx.e32, 15)
215
+ assert_equal( mtx.e33, 16)
216
+
217
+ vc = [RVec4.new(1,2,3,4),RVec4.new(5,6,7,8),RVec4.new(9,10,11,12),RVec4.new(13,14,15,16)]
218
+ mtx.setColumn(vc[0],0)
219
+ mtx.setColumn(vc[1],1)
220
+ mtx.setColumn(vc[2],2)
221
+ mtx.setColumn(vc[3],3)
222
+ assert_equal( mtx.e00, 1 )
223
+ assert_equal( mtx.e01, 5 )
224
+ assert_equal( mtx.e02, 9 )
225
+ assert_equal( mtx.e03, 13)
226
+ assert_equal( mtx.e10, 2 )
227
+ assert_equal( mtx.e11, 6 )
228
+ assert_equal( mtx.e12, 10)
229
+ assert_equal( mtx.e13, 14)
230
+ assert_equal( mtx.e20, 3 )
231
+ assert_equal( mtx.e21, 7 )
232
+ assert_equal( mtx.e22, 11)
233
+ assert_equal( mtx.e23, 15)
234
+ assert_equal( mtx.e30, 4 )
235
+ assert_equal( mtx.e31, 8 )
236
+ assert_equal( mtx.e32, 12)
237
+ assert_equal( mtx.e33, 16)
238
+ end
239
+
240
+ def test_getUpper3x3
241
+ m4 = RMtx4.new( 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 )
242
+ m3 = m4.getUpper3x3
243
+ assert_equal( m3.e00, 0 )
244
+ assert_equal( m3.e01, 1 )
245
+ assert_equal( m3.e02, 2 )
246
+ assert_equal( m3.e10, 4 )
247
+ assert_equal( m3.e11, 5 )
248
+ assert_equal( m3.e12, 6 )
249
+ assert_equal( m3.e20, 8 )
250
+ assert_equal( m3.e21, 9 )
251
+ assert_equal( m3.e22, 10)
252
+ end
253
+
254
+ def test_setUpper3x3
255
+ m4 = RMtx4.new
256
+ m3 = RMtx3.new( 1, 2, 3, 4, 5, 6, 7, 8, 9 )
257
+ m4.setUpper3x3( m3 )
258
+ assert_equal( m4.e00, 1 )
259
+ assert_equal( m4.e01, 2 )
260
+ assert_equal( m4.e02, 3 )
261
+ assert_equal( m4.e10, 4 )
262
+ assert_equal( m4.e11, 5 )
263
+ assert_equal( m4.e12, 6 )
264
+ assert_equal( m4.e20, 7 )
265
+ assert_equal( m4.e21, 8 )
266
+ assert_equal( m4.e22, 9 )
267
+ end
268
+
269
+ def test_setZero
270
+ m = RMtx4.new( 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 )
271
+ m.setZero
272
+ for r in 0...4 do
273
+ for c in 0...4 do
274
+ assert_equal( 0.0, m.getElement( r, c ) )
275
+ end
276
+ end
277
+ end
278
+
279
+ def test_setIdentity
280
+ m = RMtx4.new( 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 )
281
+ m.setIdentity
282
+ for r in 0...4 do
283
+ for c in 0...4 do
284
+ e = @mIdentity.getElement( r, c )
285
+ if ( r == c )
286
+ assert_equal( 1.0, e )
287
+ else
288
+ assert_equal( 0.0, e )
289
+ end
290
+ end
291
+ end
292
+ end
293
+
294
+ def test_getDeterminant
295
+ m0 = RMtx4.new( -2, 2, -3, 2,
296
+ -1, 1, 3, -2,
297
+ 2, 0, -1, 1,
298
+ 1, 0, -2, 3 )
299
+ assert_in_delta( 27.0, m0.getDeterminant, @tolerance )
300
+ end
301
+
302
+ def test_transpose
303
+ m0 = RMtx4.new( -2, 2, -3, 2,
304
+ -1, 1, 3, -2,
305
+ 2, 0, -1, 1,
306
+ 1, 0, -2, 3 )
307
+
308
+ # RMtx4#getTransposed
309
+ m1 = m0.getTransposed
310
+ for r in 0...4 do
311
+ for c in 0...4 do
312
+ assert_equal( m0.getElement(c,r), m1.getElement(r,c) )
313
+ end
314
+ end
315
+
316
+ # RMtx4#transpose!
317
+ m0.transpose!
318
+ for r in 0...4 do
319
+ for c in 0...4 do
320
+ assert_equal( m0.getElement(r,c), m1.getElement(r,c) )
321
+ end
322
+ end
323
+ end
324
+
325
+ def test_inverse
326
+ m0 = RMtx4.new( -2, 2, -3, 2,
327
+ -1, 1, 3, -2,
328
+ 2, 0, -1, 1,
329
+ 1, 0, -2, 3 )
330
+
331
+ m0inv = RMtx4.new( -1, 2, 15, -3,
332
+ 8, 11, 15, -3,
333
+ -5, 10, -6, 12,
334
+ -3, 6, -9, 18 )
335
+ m0inv *= 1.0/27.0
336
+
337
+ # RMtx4#getInverse
338
+ m1 = m0.getInverse
339
+ for r in 0...4 do
340
+ for c in 0...4 do
341
+ assert_in_delta( m0inv.getElement(r,c), m1.getElement(r,c), @tolerance )
342
+ end
343
+ end
344
+
345
+ # RMtx3#invert!
346
+ m0.invert!
347
+ for r in 0...4 do
348
+ for c in 0...4 do
349
+ assert_in_delta( m0inv.getElement(r,c), m0.getElement(r,c), @tolerance )
350
+ end
351
+ end
352
+ end
353
+
354
+ def test_translation
355
+ m0 = RMtx4.new( 1.0, 0.0, 0.0, 10.0,
356
+ 0.0, 1.0, 0.0, 10.0,
357
+ 0.0, 0.0, 1.0, 10.0,
358
+ 0.0, 0.0, 0.0, 1.0 )
359
+ m1 = RMtx4.new.translation( 10.0, 10.0, 10.0 )
360
+ for r in 0...4 do
361
+ for c in 0...4 do
362
+ assert_in_delta( m0.getElement(r,c), m1.getElement(r,c), @tolerance )
363
+ end
364
+ end
365
+ end
366
+
367
+ def test_rotationX
368
+ m0 = RMtx4.new( 1.0, 0.0, 0.0, 0.0,
369
+ 0.0, Math::sqrt(2)/2, -Math::sqrt(2)/2, 0.0,
370
+ 0.0, Math::sqrt(2)/2, Math::sqrt(2)/2, 0.0,
371
+ 0.0, 0.0, 0.0, 1.0 )
372
+ m1 = RMtx4.new.rotationX( Math::PI/4.0 )
373
+
374
+ for r in 0...4 do
375
+ for c in 0...4 do
376
+ assert_in_delta( m0.getElement(r,c), m1.getElement(r,c), @tolerance )
377
+ end
378
+ end
379
+
380
+
381
+ m2 = RMtx4.new( 1.0, 0.0, 0.0, 0.0,
382
+ 0.0, 0.5, -Math::sqrt(3)/2, 0.0,
383
+ 0.0, Math::sqrt(3)/2, 0.5, 0.0,
384
+ 0.0, 0.0, 0.0, 1.0 )
385
+ m3 = RMtx4.new.rotationX( Math::PI/3.0 )
386
+
387
+ for r in 0...4 do
388
+ for c in 0...4 do
389
+ assert_in_delta( m2.getElement(r,c), m3.getElement(r,c), @tolerance )
390
+ end
391
+ end
392
+ end
393
+
394
+ def test_rotationY
395
+ m0 = RMtx4.new( Math::sqrt(2)/2, 0.0, Math::sqrt(2)/2, 0.0,
396
+ 0.0, 1.0, 0.0, 0.0,
397
+ -Math::sqrt(2)/2, 0.0, Math::sqrt(2)/2, 0.0,
398
+ 0.0, 0.0, 0.0, 1.0 )
399
+ m1 = RMtx4.new.rotationY( Math::PI/4.0 )
400
+
401
+ for r in 0...4 do
402
+ for c in 0...4 do
403
+ assert_in_delta( m0.getElement(r,c), m1.getElement(r,c), @tolerance )
404
+ end
405
+ end
406
+
407
+ m2 = RMtx4.new( 0.5, 0.0, Math::sqrt(3)/2, 0.0,
408
+ 0.0, 1.0, 0.0, 0.0,
409
+ -Math::sqrt(3)/2, 0.0, 0.5, 0.0,
410
+ 0.0, 0.0, 0.0, 1.0 )
411
+ m3 = RMtx4.new.rotationY( Math::PI/3.0 )
412
+
413
+ for r in 0...4 do
414
+ for c in 0...4 do
415
+ assert_in_delta( m2.getElement(r,c), m3.getElement(r,c), @tolerance )
416
+ end
417
+ end
418
+ end
419
+
420
+ def test_rotationZ
421
+ m0 = RMtx4.new( Math::sqrt(2)/2, -Math::sqrt(2)/2, 0.0, 0.0,
422
+ Math::sqrt(2)/2, Math::sqrt(2)/2, 0.0, 0.0,
423
+ 0.0, 0.0, 1.0, 0.0,
424
+ 0.0, 0.0, 0.0, 1.0 )
425
+ m1 = RMtx4.new.rotationZ( Math::PI/4.0 )
426
+
427
+ for r in 0...4 do
428
+ for c in 0...4 do
429
+ assert_in_delta( m0.getElement(r,c), m1.getElement(r,c), @tolerance )
430
+ end
431
+ end
432
+
433
+ m2 = RMtx4.new( 0.5, -Math::sqrt(3)/2, 0.0, 0.0,
434
+ Math::sqrt(3)/2, 0.5, 0.0, 0.0,
435
+ 0.0, 0.0, 1.0, 0.0,
436
+ 0.0, 0.0, 0.0, 1.0 )
437
+ m3 = RMtx4.new.rotationZ( Math::PI/3.0 )
438
+
439
+ for r in 0...4 do
440
+ for c in 0...4 do
441
+ assert_in_delta( m2.getElement(r,c), m3.getElement(r,c), @tolerance )
442
+ end
443
+ end
444
+ end
445
+
446
+ def test_rotationAxis
447
+ m0 = RMtx4.new( 0.5, -Math::sqrt(3)/2, 0.0, 0.0,
448
+ Math::sqrt(3)/2, 0.5, 0.0, 0.0,
449
+ 0.0, 0.0, 1.0, 0.0,
450
+ 0.0, 0.0, 0.0, 1.0 )
451
+ m1 = RMtx4.new.rotationAxis( RVec3.new(0,0,1), Math::PI/3.0 )
452
+
453
+ for r in 0...4 do
454
+ for c in 0...4 do
455
+ assert_in_delta( m0.getElement(r,c), m1.getElement(r,c), @tolerance )
456
+ end
457
+ end
458
+ end
459
+
460
+ def test_rotationQuaternion
461
+ q = RQuat.new.rotationAxis( RVec3.new(0,0,1), Math::PI/3.0 )
462
+ m0 = RMtx4.new( 0.5, -Math::sqrt(3)/2, 0.0, 0.0,
463
+ Math::sqrt(3)/2, 0.5, 0.0, 0.0,
464
+ 0.0, 0.0, 1.0, 0.0,
465
+ 0.0, 0.0, 0.0, 1.0 )
466
+ m1 = RMtx4.new.rotationQuaternion( q )
467
+
468
+ for r in 0...4 do
469
+ for c in 0...4 do
470
+ assert_in_delta( m0.getElement(r,c), m1.getElement(r,c), @tolerance )
471
+ end
472
+ end
473
+ end
474
+
475
+ def test_scaling
476
+ m0 = RMtx4.new( 10.0, 0.0, 0.0, 0.0,
477
+ 0.0, 20.0, 0.0, 0.0,
478
+ 0.0, 0.0, 30.0, 0.0,
479
+ 0.0, 0.0, 0.0, 1.0 )
480
+ m1 = RMtx4.new.scaling( 10.0, 20.0, 30.0 )
481
+ for r in 0...4 do
482
+ for c in 0...4 do
483
+ assert_in_delta( m0.getElement(r,c), m1.getElement(r,c), @tolerance )
484
+ end
485
+ end
486
+ end
487
+
488
+ def test_lookAtRH
489
+ pEye = RVec3.new( 10, 10, 10 )
490
+ vDir = ( pEye - RVec3.new(0,0,0) ).normalize! # staring at (0,0,0)
491
+ vUp = RVec3.new( 0, 1, 0 )
492
+ vRight = RVec3.cross( vUp, vDir ).normalize!
493
+ vUp = RVec3.cross( vDir, vRight ).normalize!
494
+
495
+ m0 = RMtx4.new( vRight.x, vRight.y, vRight.z, -RVec3.dot(pEye,vRight),
496
+ vUp.x, vUp.y, vUp.z, -RVec3.dot(pEye,vUp),
497
+ vDir.x, vDir.y, vDir.z, -RVec3.dot(pEye,vDir),
498
+ 0.0, 0.0, 0.0, 1.0 )
499
+
500
+ m1 = RMtx4.new.lookAtRH( RVec3.new(10,10,10), # posistion
501
+ RVec3.new(0,0,0), # at
502
+ RVec3.new(0,1,0) ) # up
503
+
504
+ for r in 0...4 do
505
+ for c in 0...4 do
506
+ assert_in_delta( m0.getElement(r,c), m1.getElement(r,c), @tolerance )
507
+ end
508
+ end
509
+ end
510
+
511
+ # http://pyopengl.sourceforge.net/documentation/manual/glFrustum.3G.html
512
+ # http://pyopengl.sourceforge.net/documentation/manual/gluPerspective.3G.html
513
+ def test_perspectiveRH
514
+ left = -640.0
515
+ right = 640.0
516
+ bottom = -360.0
517
+ top = 360.0
518
+ z_n = 1.0
519
+ z_f = 1000.0
520
+ width = right - left
521
+ height = top - bottom
522
+ aspect = width/height
523
+
524
+ # RMtx4#perspectiveRH
525
+ m0 = RMtx4.new( 2*z_n/width, 0.0, 0.0, 0.0,
526
+ 0.0, 2*z_n/height, 0.0, 0.0,
527
+ 0.0, 0.0, -(z_f+z_n)/(z_f-z_n), -2.0*z_f*z_n / (z_f-z_n),
528
+ 0.0, 0.0, -1.0, 0.0 )
529
+ m1 = RMtx4.new.perspectiveRH( width, height, z_n, z_f )
530
+
531
+ for r in 0...4 do
532
+ for c in 0...4 do
533
+ assert_in_delta( m0.getElement(r,c), m1.getElement(r,c), @tolerance )
534
+ end
535
+ end
536
+
537
+ # RMtx4#perspectiveFovRH
538
+
539
+ # [NOTE] tan(fovy/2) == (height/2)/z_n
540
+ fovy = 2.0 * Math::atan( (height/2.0) / z_n )
541
+ f = 1.0/Math::tan( fovy/2.0 )
542
+
543
+ m2 = RMtx4.new( f/aspect, 0.0, 0.0, 0.0,
544
+ 0.0, f, 0.0, 0.0,
545
+ 0.0, 0.0, (z_f+z_n)/(z_n-z_f), 2*z_f*z_n/(z_n-z_f),
546
+ 0.0, 0.0, -1.0, 0.0 )
547
+ m3 = RMtx4.new.perspectiveFovRH( fovy, aspect, z_n, z_f );
548
+
549
+ for r in 0...4 do
550
+ for c in 0...4 do
551
+ assert_in_delta( m2.getElement(r,c), m2.getElement(r,c), @tolerance )
552
+ end
553
+ end
554
+
555
+ # RMtx4#perspectiveOffCenterRH
556
+
557
+ a = (right+left)/(right-left)
558
+ b = (top+bottom)/(top-bottom)
559
+ c = -(z_f+z_n)/(z_f-z_n)
560
+ d = -2.0*z_f*z_n/(z_f-z_n)
561
+ m4 = RMtx4.new( 2*z_n/(right-left), 0.0, a, 0.0,
562
+ 0.0, 2*z_n/(top-bottom), b, 0.0,
563
+ 0.0, 0.0, c, d,
564
+ 0.0, 0.0, -1.0, 0.0 )
565
+
566
+ m5 = RMtx4.new.perspectiveOffCenterRH( left, right, bottom, top, z_n, z_f )
567
+
568
+ for r in 0...4 do
569
+ for c in 0...4 do
570
+ assert_in_delta( m4.getElement(r,c), m5.getElement(r,c), @tolerance )
571
+ end
572
+ end
573
+ end
574
+
575
+ # http://pyopengl.sourceforge.net/documentation/manual/glOrtho.3G.xml
576
+ def test_orthoRH
577
+ left = -640.0
578
+ right = 640.0
579
+ bottom = -360.0
580
+ top = 360.0
581
+ z_n = 1.0
582
+ z_f = 1000.0
583
+ width = right - left
584
+ height = top - bottom
585
+
586
+ # RMtx4#orthoRH
587
+ tx = (right+left)/width
588
+ ty = (top+bottom)/height
589
+ tz = (z_f+z_n)/(z_f-z_n)
590
+ m0 = RMtx4.new( 2.0/width, 0.0, 0.0, tx,
591
+ 0.0, 2.0/height, 0.0, ty,
592
+ 0.0, 0.0, -2.0/(z_f-z_n), tz,
593
+ 0.0, 0.0, 0.0, 1.0 )
594
+ m1 = RMtx4.new.orthoRH( width, height, z_n, z_f )
595
+
596
+ for r in 0...4 do
597
+ for c in 0...4 do
598
+ assert_in_delta( m0.getElement(r,c), m1.getElement(r,c), @tolerance )
599
+ end
600
+ end
601
+
602
+ # RMtx4#orthoOffCenterRH
603
+ tx = (right+left)/(right-left)
604
+ ty = (top+bottom)/(top-bottom)
605
+ tz = (z_f+z_n)/(z_f-z_n)
606
+ m2 = RMtx4.new( 2.0/(right-left), 0.0, 0.0, tx,
607
+ 0.0, 2.0/(top-bottom), 0.0, ty,
608
+ 0.0, 0.0, -2.0/(z_f-z_n), tz,
609
+ 0.0, 0.0, 0.0, 1.0 )
610
+ m3 = RMtx4.new.orthoOffCenterRH( left, right, bottom, top, z_n, z_f )
611
+
612
+ for r in 0...4 do
613
+ for c in 0...4 do
614
+ assert_in_delta( m2.getElement(r,c), m3.getElement(r,c), @tolerance )
615
+ end
616
+ end
617
+ end
618
+
619
+ def test_unary_operators
620
+ # RMtx4#+@
621
+ m0 = RMtx4.new( 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 )
622
+ m1 = RMtx4.new( 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 )
623
+ m2 = +m0
624
+
625
+ assert_same( m0, m2 )
626
+ assert( m1 == m2 )
627
+
628
+ for r in 0...4 do
629
+ for c in 0...4 do
630
+ assert_in_delta( 4*r+c, m2.getElement(r,c), @tolerance )
631
+ end
632
+ end
633
+
634
+ # RMtx4#-@
635
+ m2 = -m0
636
+ for r in 0...4 do
637
+ for c in 0...4 do
638
+ assert_in_delta( m0.getElement(r,c), -m2.getElement(r,c), @tolerance )
639
+ end
640
+ end
641
+ end
642
+
643
+ def test_binary_plus
644
+ m0 = RMtx4.new( 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10,11,12,13,14,15 )
645
+ m1 = RMtx4.new(16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31 )
646
+ m2 = RMtx4.new(16,18,20,22,24,26,28,30,32,34,36,38,40,42,44,46 )
647
+
648
+ # RMtx4#+
649
+ m3 = m0 + m1
650
+ for r in 0...4 do
651
+ for c in 0...4 do
652
+ assert_in_delta( m2.getElement(r,c), m3.getElement(r,c), @tolerance )
653
+ end
654
+ end
655
+
656
+ # RMtx4#add!
657
+ m0.add!( m1 )
658
+ for r in 0...4 do
659
+ for c in 0...4 do
660
+ assert_in_delta( m2.getElement(r,c), m0.getElement(r,c), @tolerance )
661
+ end
662
+ end
663
+ end
664
+
665
+ def test_binary_minus
666
+ m0 = RMtx4.new( 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10,11,12,13,14,15 )
667
+ m1 = RMtx4.new(16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31 )
668
+ m2 = RMtx4.new(-16,-16,-16,-16,-16,-16,-16,-16,-16,-16,-16,-16,-16,-16,-16,-16)
669
+
670
+ # RMtx4#-
671
+ m3 = m0 - m1
672
+ for r in 0...4 do
673
+ for c in 0...4 do
674
+ assert_in_delta( m2.getElement(r,c), m3.getElement(r,c), @tolerance )
675
+ end
676
+ end
677
+
678
+ # RMtx4#sub!
679
+ m0.sub!( m1 )
680
+ for r in 0...4 do
681
+ for c in 0...4 do
682
+ assert_in_delta( m2.getElement(r,c), m0.getElement(r,c), @tolerance )
683
+ end
684
+ end
685
+ end
686
+
687
+ def test_binary_mult
688
+ m0 = RMtx4.new( 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10,11,12,13,14,15 )
689
+ m1 = RMtx4.new(16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31 )
690
+ m0x1 = RMtx4.new( 152, 158, 164, 170, 504, 526, 548, 570, 856, 894, 932, 970, 1208, 1262, 1316, 1370 )
691
+ m1x0 = RMtx4.new( 440, 510, 580, 650, 536, 622, 708, 794, 632, 734, 836, 938, 728, 846, 964, 1082 )
692
+
693
+ # RMtx4#*
694
+ m2 = m0 * m1
695
+ for r in 0...4 do
696
+ for c in 0...4 do
697
+ assert_in_delta( m0x1.getElement(r,c), m2.getElement(r,c), @tolerance )
698
+ end
699
+ end
700
+
701
+ m2 = m1 * m0
702
+ for r in 0...4 do
703
+ for c in 0...4 do
704
+ assert_in_delta( m1x0.getElement(r,c), m2.getElement(r,c), @tolerance )
705
+ end
706
+ end
707
+
708
+ # RMtx4#mul!
709
+ m2 = RMtx4.new( m0 )
710
+ m2.mul!( m1 )
711
+ for r in 0...4 do
712
+ for c in 0...4 do
713
+ assert_in_delta( m0x1.getElement(r,c), m2.getElement(r,c), @tolerance )
714
+ end
715
+ end
716
+
717
+ m2 = RMtx4.new( m1 )
718
+ m2.mul!( m0 )
719
+ for r in 0...4 do
720
+ for c in 0...4 do
721
+ assert_in_delta( m1x0.getElement(r,c), m2.getElement(r,c), @tolerance )
722
+ end
723
+ end
724
+ end
725
+
726
+ def test_equality_operators
727
+ m0 = RMtx4.new( 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10,11,12,13,14,15 )
728
+ m1 = RMtx4.new( 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10,11,12,13,14,15 )
729
+ m2 = RMtx4.new(16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31 )
730
+
731
+ assert( m0 == m1 )
732
+ assert( m0 != m2 )
733
+ end
734
+
735
+ end