rmath3d_plain 1.0.2 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/test/test.rb CHANGED
@@ -11,8 +11,10 @@ include RMath3D
11
11
  require 'minitest/autorun'
12
12
 
13
13
  # Test cases
14
+ require_relative 'test_RVec2.rb'
14
15
  require_relative 'test_RVec3.rb'
15
16
  require_relative 'test_RVec4.rb'
16
17
  require_relative 'test_RQuat.rb'
18
+ require_relative 'test_RMtx2.rb'
17
19
  require_relative 'test_RMtx3.rb'
18
20
  require_relative 'test_RMtx4.rb'
@@ -0,0 +1,363 @@
1
+ class TC_RMtx2 < Minitest::Test
2
+
3
+ def setup
4
+ @tolerance = RMath3D::TOLERANCE
5
+ @mZero = RMtx2.new.setZero
6
+ @mIdentity = RMtx2.new.setIdentity
7
+ end
8
+
9
+ def teardown
10
+ end
11
+
12
+ def test_RMtx_initialize
13
+ m0 = RMtx2.new
14
+ for r in 0...2 do
15
+ for c in 0...2 do
16
+ assert_equal( 0.0, m0.getElement(r,c) )
17
+ end
18
+ end
19
+
20
+ m1 = RMtx2.new( 0, 1,
21
+ 2, 3 )
22
+ assert_equal( 0, m1.getElement(0,0) )
23
+ assert_equal( 1, m1.getElement(0,1) )
24
+ assert_equal( 2, m1.getElement(1,0) )
25
+ assert_equal( 3, m1.getElement(1,1) )
26
+
27
+ m2 = RMtx2.new( m1 )
28
+ for r in 0...2 do
29
+ for c in 0...2 do
30
+ assert_equal( 2*r+c, m2.getElement(r,c) )
31
+ end
32
+ end
33
+ end
34
+
35
+ def test_to_s
36
+ assert_respond_to( @mZero, :to_s )
37
+ end
38
+
39
+ def test_coerce
40
+ assert_respond_to( @mZero, :coerce )
41
+ end
42
+
43
+ def test_setElements
44
+ @mZero.setElements( 0, 1, 2, 3 )
45
+ for r in 0...2 do
46
+ for c in 0...2 do
47
+ assert_equal( 2*r+c, @mZero.getElement(r,c) )
48
+ end
49
+ end
50
+ end
51
+
52
+ def test_setElement
53
+ for r in 0...2 do
54
+ for c in 0...2 do
55
+ @mZero.setElement( r, c, 2*r+c )
56
+ end
57
+ end
58
+ for r in 0...2 do
59
+ for c in 0...2 do
60
+ assert_equal( 2*r+c, @mZero.getElement(r,c) )
61
+ end
62
+ end
63
+
64
+ for r in 0...2 do
65
+ for c in 0...2 do
66
+ @mZero[r, c] = 2*c+r
67
+ end
68
+ end
69
+ for r in 0...2 do
70
+ for c in 0...2 do
71
+ assert_equal( 2*c+r, @mZero[r,c] )
72
+ end
73
+ end
74
+ end
75
+
76
+ def test_getElement
77
+ assert_respond_to( @mIdentity, :getElement )
78
+ for r in 0...2 do
79
+ for c in 0...2 do
80
+ e = @mIdentity.getElement( r, c )
81
+ if ( r == c )
82
+ assert_equal( 1.0, e )
83
+ else
84
+ assert_equal( 0.0, e )
85
+ end
86
+ end
87
+ end
88
+
89
+ for r in 0...2 do
90
+ for c in 0...2 do
91
+ e = @mIdentity[ r, c ]
92
+ if ( r == c )
93
+ assert_equal( 1.0, e )
94
+ else
95
+ assert_equal( 0.0, e )
96
+ end
97
+ end
98
+ end
99
+
100
+ mtx = RMtx2.new(1,2,
101
+ 3,4)
102
+ assert_equal( mtx.e00, 1 )
103
+ assert_equal( mtx.e01, 2 )
104
+ assert_equal( mtx.e10, 3 )
105
+ assert_equal( mtx.e11, 4 )
106
+ end
107
+
108
+ def test_getRowColumn
109
+ mtx = RMtx2.new(1,2,
110
+ 3,4)
111
+
112
+ v = mtx.getRow(0)
113
+ assert_equal( v.x, 1 )
114
+ assert_equal( v.y, 2 )
115
+
116
+ v = mtx.getRow(1)
117
+ assert_equal( v.x, 3 )
118
+ assert_equal( v.y, 4 )
119
+
120
+ v = mtx.getColumn(0)
121
+ assert_equal( v.x, 1 )
122
+ assert_equal( v.y, 3 )
123
+
124
+ v = mtx.getColumn(1)
125
+ assert_equal( v.x, 2 )
126
+ assert_equal( v.y, 4 )
127
+ end
128
+
129
+ def test_setRowColumn
130
+ mtx = RMtx2.new
131
+
132
+ vr = [RVec2.new(1,2),RVec2.new(3,4)]
133
+ mtx.setRow(vr[0],0)
134
+ mtx.setRow(vr[1],1)
135
+ assert_equal( mtx.e00, 1 )
136
+ assert_equal( mtx.e01, 2 )
137
+ assert_equal( mtx.e10, 3 )
138
+ assert_equal( mtx.e11, 4 )
139
+
140
+ vc = [RVec2.new(1,2),RVec2.new(3,4)]
141
+ mtx.setColumn(vc[0],0)
142
+ mtx.setColumn(vc[1],1)
143
+ assert_equal( mtx.e00, 1 )
144
+ assert_equal( mtx.e01, 3 )
145
+ assert_equal( mtx.e10, 2 )
146
+ assert_equal( mtx.e11, 4 )
147
+ end
148
+
149
+ def test_setZero
150
+ m = RMtx2.new( 1, 2, 3, 4 )
151
+ m.setZero
152
+ for r in 0...2 do
153
+ for c in 0...2 do
154
+ assert_equal( 0.0, m.getElement( r, c ) )
155
+ end
156
+ end
157
+ end
158
+
159
+ def test_setIdentity
160
+ m = RMtx2.new( 1, 2, 3, 4 )
161
+ m.setIdentity
162
+ for r in 0...2 do
163
+ for c in 0...2 do
164
+ e = @mIdentity.getElement( r, c )
165
+ if ( r == c )
166
+ assert_equal( 1.0, e )
167
+ else
168
+ assert_equal( 0.0, e )
169
+ end
170
+ end
171
+ end
172
+ end
173
+
174
+ def test_getDeterminant
175
+ m1 = RMtx2.new( 2, -3,
176
+ 1, 3 )
177
+ assert_equal( 9.0, m1.getDeterminant )
178
+ end
179
+
180
+ def test_transpose
181
+ m0 = RMtx2.new( -2, -3,
182
+ -1, 3 )
183
+ # RMtx2#getTransposed
184
+ m1 = m0.getTransposed
185
+ for r in 0...2 do
186
+ for c in 0...2 do
187
+ assert_equal( m0.getElement(c,r), m1.getElement(r,c) )
188
+ end
189
+ end
190
+
191
+ # RMtx2#transpose!
192
+ m0.transpose!
193
+ for r in 0...2 do
194
+ for c in 0...2 do
195
+ assert_equal( m0.getElement(r,c), m1.getElement(r,c) )
196
+ end
197
+ end
198
+ end
199
+
200
+ def test_inverse
201
+ # RMtx2#getInverse
202
+ m0 = RMtx2.new( 1, 2,
203
+ 3, 4 )
204
+
205
+ m0inv = RMtx2.new(-2.0, 1.0,
206
+ 1.5, -0.5 )
207
+ m1 = m0.getInverse
208
+ for r in 0...2 do
209
+ for c in 0...2 do
210
+ assert_in_delta( m0inv.getElement(r,c), m1.getElement(r,c), @tolerance )
211
+ end
212
+ end
213
+
214
+ # RMtx2#invert!
215
+ m0.invert!
216
+
217
+ for r in 0...2 do
218
+ for c in 0...2 do
219
+ assert_in_delta( m0inv.getElement(r,c), m0.getElement(r,c), @tolerance )
220
+ end
221
+ end
222
+ end
223
+
224
+ def test_rotation
225
+ m0 = RMtx2.new( Math::sqrt(2)/2, -Math::sqrt(2)/2,
226
+ Math::sqrt(2)/2, Math::sqrt(2)/2 )
227
+ m1 = RMtx2.new.rotation( Math::PI/4.0 )
228
+
229
+ for r in 0...2 do
230
+ for c in 0...2 do
231
+ assert_in_delta( m0.getElement(r,c), m1.getElement(r,c), @tolerance )
232
+ end
233
+ end
234
+ end
235
+
236
+ def test_scaling
237
+ m0 = RMtx2.new( 10.0, 0.0,
238
+ 0.0, 20.0 )
239
+ m1 = RMtx2.new.scaling( 10.0, 20.0 )
240
+ for r in 0...2 do
241
+ for c in 0...2 do
242
+ assert_in_delta( m0.getElement(r,c), m1.getElement(r,c), @tolerance )
243
+ end
244
+ end
245
+ end
246
+
247
+ def test_unary_operators
248
+ # RMtx2#+@
249
+ m0 = RMtx2.new( 0, 1, 2, 3 )
250
+ m1 = RMtx2.new( 0, 1, 2, 3 )
251
+ m2 = +m0
252
+
253
+ assert_same( m0, m2 )
254
+ assert( m1 == m2 )
255
+
256
+ for r in 0...2 do
257
+ for c in 0...2 do
258
+ assert_in_delta( 2*r+c, m2.getElement(r,c), @tolerance )
259
+ end
260
+ end
261
+
262
+ # RMtx2#-@
263
+ m2 = -m0
264
+ for r in 0...2 do
265
+ for c in 0...2 do
266
+ assert_in_delta( m0.getElement(r,c), -m2.getElement(r,c), @tolerance )
267
+ end
268
+ end
269
+ end
270
+
271
+ def test_binary_plus
272
+ m0 = RMtx2.new( 0, 1, 2, 3 )
273
+ m1 = RMtx2.new( 9,10,11,12 )
274
+ m2 = RMtx2.new( 9,11,13,15 )
275
+
276
+ # RMtx2#+
277
+ m3 = m0 + m1
278
+ for r in 0...2 do
279
+ for c in 0...2 do
280
+ assert_in_delta( m2.getElement(r,c), m3.getElement(r,c), @tolerance )
281
+ end
282
+ end
283
+
284
+ # RMtx2#add!
285
+ m0.add!( m1 )
286
+ for r in 0...2 do
287
+ for c in 0...2 do
288
+ assert_in_delta( m2.getElement(r,c), m0.getElement(r,c), @tolerance )
289
+ end
290
+ end
291
+ end
292
+
293
+ def test_binary_minus
294
+ m0 = RMtx2.new( 0, 1, 2, 3 )
295
+ m1 = RMtx2.new( 9,10,11,12 )
296
+ m2 = RMtx2.new(-9,-9,-9,-9 )
297
+
298
+ # RMtx2#-
299
+ m3 = m0 - m1
300
+ for r in 0...2 do
301
+ for c in 0...2 do
302
+ assert_in_delta( m2.getElement(r,c), m3.getElement(r,c), @tolerance )
303
+ end
304
+ end
305
+
306
+ # RMtx2#sub!
307
+ m0.sub!( m1 )
308
+ for r in 0...2 do
309
+ for c in 0...2 do
310
+ assert_in_delta( m2.getElement(r,c), m0.getElement(r,c), @tolerance )
311
+ end
312
+ end
313
+ end
314
+
315
+ def test_binary_mult
316
+ m0 = RMtx2.new( 0, 1, 2, 3 )
317
+ m1 = RMtx2.new( 9,10,11,12 )
318
+ m0x1 = RMtx2.new( 11, 12, 51, 56)
319
+ m1x0 = RMtx2.new( 20, 39, 24, 47)
320
+
321
+ # RMtx2#*
322
+ m2 = m0 * m1
323
+ for r in 0...2 do
324
+ for c in 0...2 do
325
+ assert_in_delta( m0x1.getElement(r,c), m2.getElement(r,c), @tolerance )
326
+ end
327
+ end
328
+
329
+ m2 = m1 * m0
330
+ for r in 0...2 do
331
+ for c in 0...2 do
332
+ assert_in_delta( m1x0.getElement(r,c), m2.getElement(r,c), @tolerance )
333
+ end
334
+ end
335
+
336
+ # RMtx2#mul!
337
+ m2 = RMtx2.new( m0 )
338
+ m2.mul!( m1 )
339
+ for r in 0...2 do
340
+ for c in 0...2 do
341
+ assert_in_delta( m0x1.getElement(r,c), m2.getElement(r,c), @tolerance )
342
+ end
343
+ end
344
+
345
+ m2 = RMtx2.new( m1 )
346
+ m2.mul!( m0 )
347
+ for r in 0...2 do
348
+ for c in 0...2 do
349
+ assert_in_delta( m1x0.getElement(r,c), m2.getElement(r,c), @tolerance )
350
+ end
351
+ end
352
+ end
353
+
354
+ def test_equality_operators
355
+ m0 = RMtx2.new( 0, 1, 2, 3 )
356
+ m1 = RMtx2.new( 0, 1, 2, 3 )
357
+ m2 = RMtx2.new( 9,10,11,12 )
358
+
359
+ assert( m0 == m1 )
360
+ assert( m0 != m2 )
361
+ end
362
+
363
+ end
@@ -0,0 +1,187 @@
1
+ require 'minitest/autorun'
2
+ class TC_RVec2 < Minitest::Test
3
+ def setup
4
+ @tolerance = RMath3D::TOLERANCE
5
+ @zero = RVec2.new( 0, 0 )
6
+ @ax = RVec2.new( 1, 0 )
7
+ @ay = RVec2.new( 0, 1 )
8
+ end
9
+
10
+ def teardown
11
+ end
12
+
13
+ def test_initialize
14
+ v0 = RVec2.new
15
+ assert_in_delta( 0, v0.x, @tolerance )
16
+ assert_in_delta( 0, v0.y, @tolerance )
17
+
18
+ v1 = RVec2.new( 1, 2 )
19
+ assert_in_delta( 1, v1.x, @tolerance )
20
+ assert_in_delta( 2, v1.y, @tolerance )
21
+
22
+ v2 = RVec2.new( v1 )
23
+ assert_in_delta( 1, v2.x, @tolerance )
24
+ assert_in_delta( 2, v2.y, @tolerance )
25
+ end
26
+
27
+ def test_to_s
28
+ assert_respond_to( @zero, :to_s )
29
+ end
30
+
31
+ def test_coerce
32
+ assert_respond_to( @zero, :coerce )
33
+ end
34
+
35
+ def test_setElements
36
+ v = RVec2.new
37
+ v.setElements( 1, 2 )
38
+ assert_in_delta( 1, v.x, @tolerance )
39
+ assert_in_delta( 2, v.y, @tolerance )
40
+ end
41
+
42
+ def test_setElement
43
+ v = RVec2.new
44
+
45
+ # x=
46
+ v.x = 1
47
+ assert_in_delta( 1, v.x, @tolerance )
48
+ v[0] = 2
49
+ assert_in_delta( 2, v.x, @tolerance )
50
+
51
+ # y=
52
+ v.y = 1
53
+ assert_in_delta( 1, v.y, @tolerance )
54
+ v[1] = 2
55
+ assert_in_delta( 2, v.y, @tolerance )
56
+ end
57
+
58
+ def test_getElement
59
+ assert_in_delta( 1, @ax[0], @tolerance )
60
+ assert_in_delta( 1, @ay[1], @tolerance )
61
+
62
+ assert_in_delta( 1, @ax.x, @tolerance )
63
+ assert_in_delta( 1, @ay.y, @tolerance )
64
+ end
65
+
66
+ def test_getLength
67
+ len_sq = 1.0*1.0 + 2.0*2.0 # == 5.0
68
+ len = Math.sqrt( len_sq ) # == 2.23606797749979
69
+
70
+ v = RVec2.new( 1, 2 )
71
+ assert_in_delta( len_sq, v.getLengthSq, @tolerance )
72
+ assert_in_delta( len , v.getLength , @tolerance )
73
+ end
74
+
75
+ def test_normalize
76
+ len_sq = 1.0*1.0 + 2.0*2.0 # == 5.0
77
+ len = Math.sqrt( len_sq ) # == 2.23606797749979
78
+ x = 1.0 / len
79
+ y = 2.0 / len
80
+ v = RVec2.new( 1, 2 )
81
+ # getNormalized
82
+ v0 = v.getNormalized
83
+ assert_in_delta( x, v0.x, @tolerance )
84
+ assert_in_delta( y, v0.y, @tolerance )
85
+
86
+ # normalize!
87
+ v.normalize!
88
+ assert_in_delta( x, v.x, @tolerance )
89
+ assert_in_delta( y, v.y, @tolerance )
90
+ end
91
+
92
+ def test_unary_operators
93
+ v = RVec2.new( 1, 2 )
94
+
95
+ # RVec2#+@
96
+ vp = +v
97
+ assert_in_delta( 1, vp.x, @tolerance )
98
+ assert_in_delta( 2, vp.y, @tolerance )
99
+
100
+ # RVec2#-@
101
+ vm = -v
102
+ assert_in_delta( -1, vm.x, @tolerance )
103
+ assert_in_delta( -2, vm.y, @tolerance )
104
+ end
105
+
106
+ def test_plus_operations
107
+ v0 = RVec2.new( 1, 1 )
108
+ v1 = RVec2.new( 2, 2 )
109
+
110
+ # RVec2#+
111
+ vr = v0 + v1
112
+ assert_in_delta( 3, vr.x, @tolerance )
113
+ assert_in_delta( 3, vr.y, @tolerance )
114
+
115
+ # RVec2#add!
116
+ v0.add!( v1 )
117
+ assert_in_delta( 3, v0.x, @tolerance )
118
+ assert_in_delta( 3, v0.y, @tolerance )
119
+
120
+ assert_raises( TypeError ) { v0 + 1.0 }
121
+ assert_raises( TypeError ) { 1.0 + v0 }
122
+ end
123
+
124
+ def test_minus_operations
125
+ v0 = RVec2.new( 1, 1 )
126
+ v1 = RVec2.new( 2, 2 )
127
+
128
+ # RVec2#-
129
+ vr = v0 - v1
130
+ assert_in_delta( -1, vr.x, @tolerance )
131
+ assert_in_delta( -1, vr.y, @tolerance )
132
+
133
+ # RVec2#sub!
134
+ v0.sub!( v1 )
135
+ assert_in_delta( -1, v0.x, @tolerance )
136
+ assert_in_delta( -1, v0.y, @tolerance )
137
+
138
+ assert_raises( TypeError ) { v0 - 1.0 }
139
+ assert_raises( TypeError ) { 1.0 - v0 }
140
+ end
141
+
142
+ def test_mult_operations
143
+ v0 = RVec2.new( 1, 1 )
144
+
145
+ vr = v0 * 2.0
146
+ assert_in_delta( 2.0, vr.x, @tolerance )
147
+ assert_in_delta( 2.0, vr.y, @tolerance )
148
+
149
+ vr = 2.0 * v0
150
+ assert_in_delta( 2.0, vr.x, @tolerance )
151
+ assert_in_delta( 2.0, vr.y, @tolerance )
152
+
153
+ v0.mul!( 2.0 )
154
+ assert_in_delta( 2.0, v0.x, @tolerance )
155
+ assert_in_delta( 2.0, v0.y, @tolerance )
156
+
157
+ assert_raises( TypeError ) { v0 * @zero }
158
+ end
159
+
160
+ def test_equality_operations
161
+ v = RVec2.new
162
+ assert( v == @zero )
163
+ assert( v != @ax )
164
+ end
165
+
166
+ def test_dot
167
+ v0 = RVec2.new( 1, 1 )
168
+ v1 = RVec2.new( 2, 2 )
169
+ assert_in_delta( 4.0, RVec2.dot(v0, v1), @tolerance )
170
+ assert_in_delta( 0.0, RVec2.dot(@ax, @ay), @tolerance )
171
+ end
172
+
173
+ def test_cross
174
+ c = RVec2.cross( @ax, @ay )
175
+ assert_in_delta( c, 1.0, @tolerance )
176
+ end
177
+
178
+ def test_transform
179
+ m = RMtx2.new.rotation( Math::PI/4.0 )
180
+ va = RVec2.new( Math.sqrt(2)/2, Math.sqrt(2)/2 )
181
+
182
+ vr = @ax.transform( m )
183
+ assert_kind_of( RVec2, vr )
184
+ assert_in_delta( va.x, vr.x, @tolerance )
185
+ assert_in_delta( va.y, vr.y, @tolerance )
186
+ end
187
+ end