gmath3D 0.2.4 → 0.2.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/test/test_vector3.rb CHANGED
@@ -1,439 +1,439 @@
1
- $LOAD_PATH.unshift(File.dirname(__FILE__))
2
- require 'helper'
3
-
4
- include GMath3D
5
-
6
- MiniTest::Unit.autorun
7
-
8
- class Vector3TestCase < MiniTest::Unit::TestCase
9
- def setup
10
- @vector_init_zero = Vector3.new()
11
- @vector = Vector3.new(1, 2.0, 3.0)
12
- end
13
-
14
- def test_initalize
15
- assert_equal(0, @vector_init_zero.x)
16
- assert_equal(0, @vector_init_zero.y)
17
- assert_equal(0, @vector_init_zero.z)
18
-
19
- assert_equal(1.0, @vector.x)
20
- assert_equal(2.0, @vector.y)
21
- assert_equal(3.0, @vector.z)
22
-
23
- assert_equal(Geom.default_tolerance, @vector.tolerance)
24
-
25
- assert_raises ArgumentError do
26
- invalidResult = Vector3.new( "hoge" )
27
- end
28
- end
29
-
30
- def test_to_s
31
- assert_equal("Vector3[1, 2.0, 3.0]", @vector.to_s)
32
- end
33
-
34
- def test_to_element_s
35
- assert_equal("[1, 2.0, 3.0]", @vector.to_element_s)
36
- end
37
-
38
- def test_assign_value
39
- assert_equal(1.0, @vector.x)
40
- assert_equal(2.0, @vector.y)
41
- assert_equal(3.0, @vector.z)
42
-
43
- @vector.x = 2.0
44
- @vector.y *= 2.0
45
- @vector.z -= 3.0
46
-
47
- assert_equal(2.0, @vector.x)
48
- assert_equal(4.0, @vector.y)
49
- assert_equal(0.0, @vector.z)
50
- end
51
-
52
- def test_assign_invalid_value
53
- #TODO unimplemented!
54
- @vector.x = "this is a pen"
55
- end
56
-
57
- def test_clone
58
- vec1 = Vector3.new(1,2,3)
59
- vec1_shallow_copy = vec1
60
- vec1_shallow_copy.x = 4
61
- assert_equal( 4, vec1.x )
62
- assert_equal( 2, vec1.y )
63
- assert_equal( 3, vec1.z )
64
-
65
- vec1_deep_copy = vec1.clone
66
- vec1_deep_copy.x = 6
67
- assert_equal( 4, vec1.x )
68
- assert_equal( 6, vec1_deep_copy.x )
69
-
70
- vec_ary = [Vector3.new(1,2,3), Vector3.new(4,5,6)]
71
- vec_ary_shallow = vec_ary
72
- vec_ary_shallow[0].x = 4
73
- vec_ary_shallow[1] = Vector3.new(7,8,9)
74
- vec_ary_shallow.push(Vector3.new(10,11,12))
75
- assert_equal(4, vec_ary_shallow[0].x)
76
- assert_equal(Vector3.new(7,8,9), vec_ary_shallow[1])
77
- assert_equal(3, vec_ary_shallow.size)
78
- assert_equal(4, vec_ary[0].x)
79
- assert_equal(Vector3.new(7,8,9), vec_ary[1])
80
- assert_equal(3, vec_ary.size)
81
-
82
- vec_ary = [Vector3.new(1,2,3), Vector3.new(4,5,6)]
83
- vec_ary_deep = vec_ary.clone
84
- vec_ary_deep[0].x = 4
85
- vec_ary_deep[1] = Vector3.new(7,8,9)
86
- vec_ary_deep.push(Vector3.new(10,11,12))
87
- assert_equal(4, vec_ary_deep[0].x)
88
- assert_equal(Vector3.new(7,8,9), vec_ary_deep[1])
89
- assert_equal(3, vec_ary_deep.size)
90
-
91
- # Array.clone does not call element.clone
92
- # assert_equal(1, vec_ary[0].x)
93
- assert_equal(4, vec_ary[0].x)
94
- assert_equal(Vector3.new(4,5,6), vec_ary[1])
95
- assert_equal(2, vec_ary.size)
96
- end
97
-
98
- def test_equals
99
- assert(!(@vector_init_zero == @vector))
100
- assert(@vector_init_zero != @vector)
101
-
102
- assert(@vector == @vector)
103
- assert(@vector.eql?(@vector))
104
-
105
- vector = Vector3.new(1,2,3)
106
- assert(@vector == vector)
107
- assert(@vector.eql?(vector))
108
-
109
- # Floating error check
110
- floatingError = Geom.default_tolerance*0.1
111
- vector = Vector3.new(1.0 - floatingError, 2.0 + floatingError, 3.0)
112
- assert(@vector == vector)
113
-
114
- floatingError2 = Geom.default_tolerance*10.0
115
- vector = Vector3.new(1.0 - floatingError2, 2.0 + floatingError2, 3.0)
116
- assert(@vector != vector)
117
-
118
- assert_equal(Vector3.new(1,2,3), Vector3.new(1.0,2.0,3.0))
119
-
120
- #invlid value comparison
121
- assert(@vector != "string")
122
- assert(@vector != -4)
123
- end
124
-
125
- def test_equ_hash
126
- # in case use Vector3 as Key of hash...
127
- # Vector3#eql? and Vector3#hash should be implement
128
- hash = Hash.new()
129
- vec = Vector3.new(1,2,3)
130
- hash[vec] = 1
131
- assert_equal(1, hash.keys.count)
132
- assert_equal(1, hash[vec])
133
-
134
- hash[vec] = 2
135
- assert_equal(1, hash.keys.count)
136
- assert_equal(2, hash[vec])
137
-
138
- hash[Vector3.new(3,2,1)] = 3
139
- assert_equal(2, hash.keys.count)
140
- assert_equal(2, hash[vec])
141
-
142
- hash[@vector] = 3
143
- assert_equal(2, hash.keys.count)
144
- assert_equal(3, hash[vec])
145
- end
146
-
147
- def test_box
148
- box1 = @vector.box
149
- box2 = Vector3.new().box
150
- assert_equal(Vector3.new(1,2,3), box1.min_point)
151
- assert_equal(Vector3.new(1,2,3), box1.max_point)
152
- assert_equal(Vector3.new(), box2.min_point)
153
- assert_equal(Vector3.new(), box2.max_point)
154
- end
155
-
156
- def test_add
157
- vector = Vector3.new(4,5,6)
158
- addedVector = vector + @vector
159
-
160
- assert_equal(5.0, addedVector.x)
161
- assert_equal(7.0, addedVector.y)
162
- assert_equal(9.0, addedVector.z)
163
-
164
- addedVector = @vector + vector
165
-
166
- assert_equal(5.0, addedVector.x)
167
- assert_equal(7.0, addedVector.y)
168
- assert_equal(9.0, addedVector.z)
169
- end
170
- def test_add_invalid_value
171
- assert_raises ArgumentError do
172
- invalidResult = @vector + 5
173
- end
174
- assert_raises ArgumentError do
175
- invalidResult = @vector + nil
176
- end
177
- end
178
-
179
- def test_subtract
180
- vector = Vector3.new(4,5,6)
181
- subtractedVector = vector - @vector
182
-
183
- assert_equal(3.0, subtractedVector.x)
184
- assert_equal(3.0, subtractedVector.y)
185
- assert_equal(3.0, subtractedVector.z)
186
-
187
- subtractedVector = @vector - vector
188
-
189
- assert_equal(-3.0, subtractedVector.x)
190
- assert_equal(-3.0, subtractedVector.y)
191
- assert_equal(-3.0, subtractedVector.z)
192
- end
193
- def test_subtract_invalid_value
194
- assert_raises ArgumentError do
195
- invalidResult = @vector - 5
196
- end
197
- assert_raises ArgumentError do
198
- invalidResult = @vector - nil
199
- end
200
- end
201
-
202
- def test_multiply
203
- multipliedVector = @vector * 5
204
-
205
- assert_equal( 5.0, multipliedVector.x)
206
- assert_equal(10.0, multipliedVector.y)
207
- assert_equal(15.0, multipliedVector.z)
208
-
209
- multipliedVector = @vector * -3.0
210
-
211
- assert_equal(-3.0, multipliedVector.x)
212
- assert_equal(-6.0, multipliedVector.y)
213
- assert_equal(-9.0, multipliedVector.z)
214
- end
215
- def test_multiply_invalid_value
216
- assert_raises ArgumentError do
217
- invalidResult = @vector * @vector
218
- end
219
- assert_raises ArgumentError do
220
- invalidResult = @vector * nil
221
- end
222
- end
223
-
224
- def test_divide
225
- dividedVector = @vector / 5
226
-
227
- assert_equal(1.0/5.0, dividedVector.x)
228
- assert_equal(2.0/5.0, dividedVector.y)
229
- assert_equal(3.0/5.0, dividedVector.z)
230
-
231
- dividedVector = @vector / -3.0
232
-
233
- assert_equal(-1.0/3.0, dividedVector.x)
234
- assert_equal(-2.0/3.0, dividedVector.y)
235
- assert_equal(-3.0/3.0, dividedVector.z)
236
- end
237
- def test_divide_invalid_value
238
- assert_raises ArgumentError do
239
- invalidResult = @vector / @vector
240
- end
241
- assert_raises ArgumentError do
242
- invalidResult = @vector / nil
243
- end
244
- end
245
-
246
- def test_dot
247
- vector = Vector3.new(2.0, 4.0, 6.0)
248
- innerProduct = @vector.dot(vector)
249
- assert_in_delta( 1.0*2.0 + 2.0*4.0 + 3.0*6.0, innerProduct, @vector.tolerance)
250
-
251
- innerProduct = @vector.dot(@vector_init_zero)
252
- assert_in_delta( 0.0, innerProduct, @vector.tolerance)
253
- end
254
- def test_dot_invalid_value
255
- assert_raises ArgumentError do
256
- invalidResult = @vector.dot(5.0)
257
- end
258
- assert_raises ArgumentError do
259
- invalidResult = @vector.dot(nil)
260
- end
261
- end
262
-
263
- def test_cross
264
- outerProduct = Vector3.new(1.0,0.0,0.0).cross(Vector3.new(0.0,1.0,0.0))
265
- assert(outerProduct == Vector3.new(0.0,0.0,1.0))
266
-
267
- vector = Vector3.new(1.0, 1.0, 4.0)
268
- outerProduct = @vector.cross(vector)
269
- assert_in_delta(2.0*4.0 - 3.0*1.0, outerProduct.x, @vector.tolerance)
270
- assert_in_delta(3.0*1.0 - 1.0*4.0, outerProduct.y, @vector.tolerance)
271
- assert_in_delta(1.0*1.0 - 2.0*1.0, outerProduct.z, @vector.tolerance)
272
-
273
- outerProduct = @vector.cross(@vector_init_zero)
274
- assert_in_delta( 0.0, outerProduct.x, @vector.tolerance)
275
- assert_in_delta( 0.0, outerProduct.y, @vector.tolerance)
276
- assert_in_delta( 0.0, outerProduct.z, @vector.tolerance)
277
- end
278
- def test_cross_invalid_value
279
- assert_raises ArgumentError do
280
- invalidResult = @vector.cross(5.0)
281
- end
282
- assert_raises ArgumentError do
283
- invalidResult = @vector.cross(nil)
284
- end
285
- end
286
-
287
- def test_length
288
- assert_in_delta(0.0, @vector_init_zero.length, @vector_init_zero.tolerance)
289
- assert_in_delta( Math::sqrt(1.0*1.0+2.0*2.0+3.0*3.0), @vector.length, @vector.tolerance)
290
- vector = Vector3.new(3.0, 4.0, 0.0)
291
- assert_in_delta( 5.0, vector.length, @vector.tolerance)
292
- end
293
-
294
- def test_distance
295
- point1 = Vector3.new(1.0, 3.0, -5.0)
296
- point2 = Vector3.new(3.0, -5.5, 2.2)
297
- vector1to2 = point2 - point1
298
- assert_in_delta( vector1to2.length, point2.distance(point1), vector1to2.tolerance)
299
- assert_in_delta( vector1to2.length, point1.distance(point2), vector1to2.tolerance)
300
- assert_in_delta( 0.0, point1.distance(point1), vector1to2.tolerance)
301
- end
302
- def test_distance_invalid_value
303
- assert_raises ArgumentError do
304
- invalidResult = @vector.distance(5.0)
305
- end
306
- assert_raises ArgumentError do
307
- invalidResult = @vector.distance(nil)
308
- end
309
- end
310
-
311
- def test_angle
312
- vector1 = Vector3.new( 1.0 , 0.0, 0.0 )
313
- vector2 = Vector3.new( 1.0 , 1.0, 0.0 )
314
- vector3 = Vector3.new( -1.0 , 1.0, 0.0 )
315
- vector4 = Vector3.new( -1.0 , 0.0, 0.0 )
316
- vector5 = Vector3.new( -1.0 , -1.0, 0.0 )
317
- vector6 = Vector3.new( 1.0 , -1.0, 0.0 )
318
-
319
- # result should be between 0 and PI.
320
- assert_in_delta(0.0,vector1.angle(vector1), vector1.tolerance) ;
321
- assert_in_delta(Math::PI/4.0,vector1.angle(vector2), vector1.tolerance) ;
322
- assert_in_delta(Math::PI*3.0/4.0, vector1.angle(vector3), vector1.tolerance) ;
323
- assert_in_delta(Math::PI, vector1.angle(vector4), vector1.tolerance) ;
324
- assert_in_delta(Math::PI*3.0/4.0, vector1.angle(vector5), vector1.tolerance) ;
325
- assert_in_delta(Math::PI/4.0, vector1.angle(vector6), vector1.tolerance) ;
326
- end
327
- def test_angle_invalid_value
328
- assert_raises ArgumentError do
329
- invalidResult = @vector.angle(5.0)
330
- end
331
- assert_raises ArgumentError do
332
- invalidResult = @vector.angle(nil)
333
- end
334
- end
335
-
336
- def test_normalize
337
- normalized = @vector.normalize()
338
- assert_in_delta(1.0, normalized.length, normalized.tolerance) ;
339
- assert(normalized.same_direction?(@vector))
340
- end
341
-
342
- def test_parallel
343
- vectorZero = Vector3.new( 0.0, 0.0, 0.0) ;
344
- vector1 = Vector3.new( 1.0, 2.0, 3.0 ) ;
345
- vector2 = Vector3.new( 1.0, 1.0, 4.0 ) ;
346
- vector3 = Vector3.new( 2.0, 4.0, 6.0 ) ;
347
- vector4 = Vector3.new( -1.0, -2.0, -3.0 ) ;
348
- vector5 = Vector3.new( -3.0, -6.0, -9.0 ) ;
349
-
350
- assert(!vector1.parallel?(vectorZero)) ;
351
- assert(!vectorZero.parallel?(vector2)) ;
352
- assert(!vector1.parallel?(vector2)) ;
353
-
354
- assert(vector1.parallel?(vector3)) ;
355
- assert(vector1.parallel?(vector4)) ;
356
- assert(vector1.parallel?(vector5)) ;
357
- end
358
-
359
- def test_same_direction
360
- vectorZero = Vector3.new( 0.0, 0.0, 0.0) ;
361
- vector1 = Vector3.new( 1.0, 2.0, 3.0 ) ;
362
- vector2 = Vector3.new( 1.0, 1.0, 4.0 ) ;
363
- vector3 = Vector3.new( 2.0, 4.0, 6.0 ) ;
364
- vector4 = Vector3.new( -1.0, -2.0, -3.0 ) ;
365
- vector5 = Vector3.new( -3.0, -6.0, -9.0 ) ;
366
-
367
- assert(!vector1.same_direction?(vectorZero)) ;
368
- assert(!vectorZero.same_direction?(vector2)) ;
369
- assert(!vector1.same_direction?(vector2)) ;
370
-
371
- assert(vector1.same_direction?(vector3)) ;
372
- assert(!vector1.same_direction?(vector4)) ;
373
- assert(!vector1.same_direction?(vector5)) ;
374
- end
375
-
376
- def test_project
377
- vector1 = Vector3.new(1.0, 1.0, 0.0)
378
- vector2 = Vector3.new(0.0, 1.0, 0.0)
379
-
380
- projectedVector, parameter = vector2.project_to(vector1)
381
- assert(Vector3.new(0.5,0.5,0.0) == projectedVector)
382
- assert_in_delta(0.5, parameter, projectedVector.tolerance)
383
-
384
- projectedVector, parameter = vector2.project_to(Vector3.new(0.0, 0.0, 0.0))
385
- assert(Vector3.new() == projectedVector)
386
- assert_in_delta(0.0, parameter, projectedVector.tolerance)
387
-
388
- vector3 = Vector3.new(5.0, -1.0, 4.0)
389
- projectedVector, parmeter = vector3.project_to(vector1)
390
- assert(projectedVector.same_direction?(vector1))
391
- end
392
-
393
- def test_to_column_vector
394
- matrix = @vector.to_column_vector
395
- assert_equal( 3, matrix.row_size )
396
- assert_equal( 1, matrix.column_size )
397
- assert_equal( 1, matrix[0,0])
398
- assert_equal( 2, matrix[1,0])
399
- assert_equal( 3, matrix[2,0])
400
-
401
- matrix = @vector_init_zero.to_column_vector
402
- assert_equal( 3, matrix.row_size )
403
- assert_equal( 1, matrix.column_size )
404
- assert_equal( 0, matrix[0,0])
405
- assert_equal( 0, matrix[1,0])
406
- assert_equal( 0, matrix[2,0])
407
- end
408
-
409
- def test_arbitrary_orthogonal
410
- vec1 = Vector3.new( 1.0, 2.0, 3.0 )
411
- vec2 = Vector3.new( 1.0, 1.0, 4.0 )
412
- vec3 = Vector3.new( 2.0, 4.0, 6.0 )
413
- vec4 = Vector3.new( -1.0, -2.0, -3.0 )
414
- vecx = Vector3.new( 1.0, 0.0, 0.0 )
415
- vecy = Vector3.new( 0.0, -3.0, 0.0 )
416
- vecz = Vector3.new( 0.0, 0.0, 5.0 )
417
- veczero = Vector3.new( 0.0, 0.0, 0.0 )
418
-
419
- vec1_orth = vec1.arbitrary_orthogonal
420
- vec2_orth = vec2.arbitrary_orthogonal
421
- vec3_orth = vec3.arbitrary_orthogonal
422
- vec4_orth = vec4.arbitrary_orthogonal
423
- vecx_orth = vecx.arbitrary_orthogonal
424
- vecy_orth = vecy.arbitrary_orthogonal
425
- vecz_orth = vecz.arbitrary_orthogonal
426
- veczero_orth = veczero.arbitrary_orthogonal
427
-
428
- assert_in_delta(0.5*Math::PI, vec1.angle(vec1_orth), vec1.tolerance)
429
- assert_in_delta(0.5*Math::PI, vec2.angle(vec2_orth), vec2.tolerance)
430
- assert_in_delta(0.5*Math::PI, vec3.angle(vec3_orth), vec3.tolerance)
431
- assert_in_delta(0.5*Math::PI, vec4.angle(vec4_orth), vec4.tolerance)
432
- assert_in_delta(0.5*Math::PI, vecx.angle(vecx_orth), vecx.tolerance)
433
- assert_in_delta(0.5*Math::PI, vecy.angle(vecy_orth), vecy.tolerance)
434
- assert_in_delta(0.5*Math::PI, vecz.angle(vecz_orth), vecz.tolerance)
435
- assert_in_delta(0.0, veczero_orth.x, veczero.tolerance)
436
- assert_in_delta(0.0, veczero_orth.y, veczero.tolerance)
437
- assert_in_delta(0.0, veczero_orth.z, veczero.tolerance)
438
- end
439
- end
1
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
2
+ require 'helper'
3
+
4
+ include GMath3D
5
+
6
+ MiniTest::Unit.autorun
7
+
8
+ class Vector3TestCase < MiniTest::Unit::TestCase
9
+ def setup
10
+ @vector_init_zero = Vector3.new()
11
+ @vector = Vector3.new(1, 2.0, 3.0)
12
+ end
13
+
14
+ def test_initalize
15
+ assert_equal(0, @vector_init_zero.x)
16
+ assert_equal(0, @vector_init_zero.y)
17
+ assert_equal(0, @vector_init_zero.z)
18
+
19
+ assert_equal(1.0, @vector.x)
20
+ assert_equal(2.0, @vector.y)
21
+ assert_equal(3.0, @vector.z)
22
+
23
+ assert_equal(Geom.default_tolerance, @vector.tolerance)
24
+
25
+ assert_raises ArgumentError do
26
+ invalidResult = Vector3.new( "hoge" )
27
+ end
28
+ end
29
+
30
+ def test_to_s
31
+ assert_equal("Vector3[1, 2.0, 3.0]", @vector.to_s)
32
+ end
33
+
34
+ def test_to_element_s
35
+ assert_equal("[1, 2.0, 3.0]", @vector.to_element_s)
36
+ end
37
+
38
+ def test_assign_value
39
+ assert_equal(1.0, @vector.x)
40
+ assert_equal(2.0, @vector.y)
41
+ assert_equal(3.0, @vector.z)
42
+
43
+ @vector.x = 2.0
44
+ @vector.y *= 2.0
45
+ @vector.z -= 3.0
46
+
47
+ assert_equal(2.0, @vector.x)
48
+ assert_equal(4.0, @vector.y)
49
+ assert_equal(0.0, @vector.z)
50
+ end
51
+
52
+ def test_assign_invalid_value
53
+ #TODO unimplemented!
54
+ @vector.x = "this is a pen"
55
+ end
56
+
57
+ def test_clone
58
+ vec1 = Vector3.new(1,2,3)
59
+ vec1_shallow_copy = vec1
60
+ vec1_shallow_copy.x = 4
61
+ assert_equal( 4, vec1.x )
62
+ assert_equal( 2, vec1.y )
63
+ assert_equal( 3, vec1.z )
64
+
65
+ vec1_deep_copy = vec1.clone
66
+ vec1_deep_copy.x = 6
67
+ assert_equal( 4, vec1.x )
68
+ assert_equal( 6, vec1_deep_copy.x )
69
+
70
+ vec_ary = [Vector3.new(1,2,3), Vector3.new(4,5,6)]
71
+ vec_ary_shallow = vec_ary
72
+ vec_ary_shallow[0].x = 4
73
+ vec_ary_shallow[1] = Vector3.new(7,8,9)
74
+ vec_ary_shallow.push(Vector3.new(10,11,12))
75
+ assert_equal(4, vec_ary_shallow[0].x)
76
+ assert_equal(Vector3.new(7,8,9), vec_ary_shallow[1])
77
+ assert_equal(3, vec_ary_shallow.size)
78
+ assert_equal(4, vec_ary[0].x)
79
+ assert_equal(Vector3.new(7,8,9), vec_ary[1])
80
+ assert_equal(3, vec_ary.size)
81
+
82
+ vec_ary = [Vector3.new(1,2,3), Vector3.new(4,5,6)]
83
+ vec_ary_deep = vec_ary.clone
84
+ vec_ary_deep[0].x = 4
85
+ vec_ary_deep[1] = Vector3.new(7,8,9)
86
+ vec_ary_deep.push(Vector3.new(10,11,12))
87
+ assert_equal(4, vec_ary_deep[0].x)
88
+ assert_equal(Vector3.new(7,8,9), vec_ary_deep[1])
89
+ assert_equal(3, vec_ary_deep.size)
90
+
91
+ # Array.clone does not call element.clone
92
+ # assert_equal(1, vec_ary[0].x)
93
+ assert_equal(4, vec_ary[0].x)
94
+ assert_equal(Vector3.new(4,5,6), vec_ary[1])
95
+ assert_equal(2, vec_ary.size)
96
+ end
97
+
98
+ def test_equals
99
+ assert(!(@vector_init_zero == @vector))
100
+ assert(@vector_init_zero != @vector)
101
+
102
+ assert(@vector == @vector)
103
+ assert(@vector.eql?(@vector))
104
+
105
+ vector = Vector3.new(1,2,3)
106
+ assert(@vector == vector)
107
+ assert(@vector.eql?(vector))
108
+
109
+ # Floating error check
110
+ floatingError = Geom.default_tolerance*0.1
111
+ vector = Vector3.new(1.0 - floatingError, 2.0 + floatingError, 3.0)
112
+ assert(@vector == vector)
113
+
114
+ floatingError2 = Geom.default_tolerance*10.0
115
+ vector = Vector3.new(1.0 - floatingError2, 2.0 + floatingError2, 3.0)
116
+ assert(@vector != vector)
117
+
118
+ assert_equal(Vector3.new(1,2,3), Vector3.new(1.0,2.0,3.0))
119
+
120
+ #invlid value comparison
121
+ assert(@vector != "string")
122
+ assert(@vector != -4)
123
+ end
124
+
125
+ def test_equ_hash
126
+ # in case use Vector3 as Key of hash...
127
+ # Vector3#eql? and Vector3#hash should be implement
128
+ hash = Hash.new()
129
+ vec = Vector3.new(1,2,3)
130
+ hash[vec] = 1
131
+ assert_equal(1, hash.keys.count)
132
+ assert_equal(1, hash[vec])
133
+
134
+ hash[vec] = 2
135
+ assert_equal(1, hash.keys.count)
136
+ assert_equal(2, hash[vec])
137
+
138
+ hash[Vector3.new(3,2,1)] = 3
139
+ assert_equal(2, hash.keys.count)
140
+ assert_equal(2, hash[vec])
141
+
142
+ hash[@vector] = 3
143
+ assert_equal(2, hash.keys.count)
144
+ assert_equal(3, hash[vec])
145
+ end
146
+
147
+ def test_box
148
+ box1 = @vector.box
149
+ box2 = Vector3.new().box
150
+ assert_equal(Vector3.new(1,2,3), box1.min_point)
151
+ assert_equal(Vector3.new(1,2,3), box1.max_point)
152
+ assert_equal(Vector3.new(), box2.min_point)
153
+ assert_equal(Vector3.new(), box2.max_point)
154
+ end
155
+
156
+ def test_add
157
+ vector = Vector3.new(4,5,6)
158
+ addedVector = vector + @vector
159
+
160
+ assert_equal(5.0, addedVector.x)
161
+ assert_equal(7.0, addedVector.y)
162
+ assert_equal(9.0, addedVector.z)
163
+
164
+ addedVector = @vector + vector
165
+
166
+ assert_equal(5.0, addedVector.x)
167
+ assert_equal(7.0, addedVector.y)
168
+ assert_equal(9.0, addedVector.z)
169
+ end
170
+ def test_add_invalid_value
171
+ assert_raises ArgumentError do
172
+ invalidResult = @vector + 5
173
+ end
174
+ assert_raises ArgumentError do
175
+ invalidResult = @vector + nil
176
+ end
177
+ end
178
+
179
+ def test_subtract
180
+ vector = Vector3.new(4,5,6)
181
+ subtractedVector = vector - @vector
182
+
183
+ assert_equal(3.0, subtractedVector.x)
184
+ assert_equal(3.0, subtractedVector.y)
185
+ assert_equal(3.0, subtractedVector.z)
186
+
187
+ subtractedVector = @vector - vector
188
+
189
+ assert_equal(-3.0, subtractedVector.x)
190
+ assert_equal(-3.0, subtractedVector.y)
191
+ assert_equal(-3.0, subtractedVector.z)
192
+ end
193
+ def test_subtract_invalid_value
194
+ assert_raises ArgumentError do
195
+ invalidResult = @vector - 5
196
+ end
197
+ assert_raises ArgumentError do
198
+ invalidResult = @vector - nil
199
+ end
200
+ end
201
+
202
+ def test_multiply
203
+ multipliedVector = @vector * 5
204
+
205
+ assert_equal( 5.0, multipliedVector.x)
206
+ assert_equal(10.0, multipliedVector.y)
207
+ assert_equal(15.0, multipliedVector.z)
208
+
209
+ multipliedVector = @vector * -3.0
210
+
211
+ assert_equal(-3.0, multipliedVector.x)
212
+ assert_equal(-6.0, multipliedVector.y)
213
+ assert_equal(-9.0, multipliedVector.z)
214
+ end
215
+ def test_multiply_invalid_value
216
+ assert_raises ArgumentError do
217
+ invalidResult = @vector * @vector
218
+ end
219
+ assert_raises ArgumentError do
220
+ invalidResult = @vector * nil
221
+ end
222
+ end
223
+
224
+ def test_divide
225
+ dividedVector = @vector / 5
226
+
227
+ assert_equal(1.0/5.0, dividedVector.x)
228
+ assert_equal(2.0/5.0, dividedVector.y)
229
+ assert_equal(3.0/5.0, dividedVector.z)
230
+
231
+ dividedVector = @vector / -3.0
232
+
233
+ assert_equal(-1.0/3.0, dividedVector.x)
234
+ assert_equal(-2.0/3.0, dividedVector.y)
235
+ assert_equal(-3.0/3.0, dividedVector.z)
236
+ end
237
+ def test_divide_invalid_value
238
+ assert_raises ArgumentError do
239
+ invalidResult = @vector / @vector
240
+ end
241
+ assert_raises ArgumentError do
242
+ invalidResult = @vector / nil
243
+ end
244
+ end
245
+
246
+ def test_dot
247
+ vector = Vector3.new(2.0, 4.0, 6.0)
248
+ innerProduct = @vector.dot(vector)
249
+ assert_in_delta( 1.0*2.0 + 2.0*4.0 + 3.0*6.0, innerProduct, @vector.tolerance)
250
+
251
+ innerProduct = @vector.dot(@vector_init_zero)
252
+ assert_in_delta( 0.0, innerProduct, @vector.tolerance)
253
+ end
254
+ def test_dot_invalid_value
255
+ assert_raises ArgumentError do
256
+ invalidResult = @vector.dot(5.0)
257
+ end
258
+ assert_raises ArgumentError do
259
+ invalidResult = @vector.dot(nil)
260
+ end
261
+ end
262
+
263
+ def test_cross
264
+ outerProduct = Vector3.new(1.0,0.0,0.0).cross(Vector3.new(0.0,1.0,0.0))
265
+ assert(outerProduct == Vector3.new(0.0,0.0,1.0))
266
+
267
+ vector = Vector3.new(1.0, 1.0, 4.0)
268
+ outerProduct = @vector.cross(vector)
269
+ assert_in_delta(2.0*4.0 - 3.0*1.0, outerProduct.x, @vector.tolerance)
270
+ assert_in_delta(3.0*1.0 - 1.0*4.0, outerProduct.y, @vector.tolerance)
271
+ assert_in_delta(1.0*1.0 - 2.0*1.0, outerProduct.z, @vector.tolerance)
272
+
273
+ outerProduct = @vector.cross(@vector_init_zero)
274
+ assert_in_delta( 0.0, outerProduct.x, @vector.tolerance)
275
+ assert_in_delta( 0.0, outerProduct.y, @vector.tolerance)
276
+ assert_in_delta( 0.0, outerProduct.z, @vector.tolerance)
277
+ end
278
+ def test_cross_invalid_value
279
+ assert_raises ArgumentError do
280
+ invalidResult = @vector.cross(5.0)
281
+ end
282
+ assert_raises ArgumentError do
283
+ invalidResult = @vector.cross(nil)
284
+ end
285
+ end
286
+
287
+ def test_length
288
+ assert_in_delta(0.0, @vector_init_zero.length, @vector_init_zero.tolerance)
289
+ assert_in_delta( Math::sqrt(1.0*1.0+2.0*2.0+3.0*3.0), @vector.length, @vector.tolerance)
290
+ vector = Vector3.new(3.0, 4.0, 0.0)
291
+ assert_in_delta( 5.0, vector.length, @vector.tolerance)
292
+ end
293
+
294
+ def test_distance
295
+ point1 = Vector3.new(1.0, 3.0, -5.0)
296
+ point2 = Vector3.new(3.0, -5.5, 2.2)
297
+ vector1to2 = point2 - point1
298
+ assert_in_delta( vector1to2.length, point2.distance(point1), vector1to2.tolerance)
299
+ assert_in_delta( vector1to2.length, point1.distance(point2), vector1to2.tolerance)
300
+ assert_in_delta( 0.0, point1.distance(point1), vector1to2.tolerance)
301
+ end
302
+ def test_distance_invalid_value
303
+ assert_raises ArgumentError do
304
+ invalidResult = @vector.distance(5.0)
305
+ end
306
+ assert_raises ArgumentError do
307
+ invalidResult = @vector.distance(nil)
308
+ end
309
+ end
310
+
311
+ def test_angle
312
+ vector1 = Vector3.new( 1.0 , 0.0, 0.0 )
313
+ vector2 = Vector3.new( 1.0 , 1.0, 0.0 )
314
+ vector3 = Vector3.new( -1.0 , 1.0, 0.0 )
315
+ vector4 = Vector3.new( -1.0 , 0.0, 0.0 )
316
+ vector5 = Vector3.new( -1.0 , -1.0, 0.0 )
317
+ vector6 = Vector3.new( 1.0 , -1.0, 0.0 )
318
+
319
+ # result should be between 0 and PI.
320
+ assert_in_delta(0.0,vector1.angle(vector1), vector1.tolerance) ;
321
+ assert_in_delta(Math::PI/4.0,vector1.angle(vector2), vector1.tolerance) ;
322
+ assert_in_delta(Math::PI*3.0/4.0, vector1.angle(vector3), vector1.tolerance) ;
323
+ assert_in_delta(Math::PI, vector1.angle(vector4), vector1.tolerance) ;
324
+ assert_in_delta(Math::PI*3.0/4.0, vector1.angle(vector5), vector1.tolerance) ;
325
+ assert_in_delta(Math::PI/4.0, vector1.angle(vector6), vector1.tolerance) ;
326
+ end
327
+ def test_angle_invalid_value
328
+ assert_raises ArgumentError do
329
+ invalidResult = @vector.angle(5.0)
330
+ end
331
+ assert_raises ArgumentError do
332
+ invalidResult = @vector.angle(nil)
333
+ end
334
+ end
335
+
336
+ def test_normalize
337
+ normalized = @vector.normalize()
338
+ assert_in_delta(1.0, normalized.length, normalized.tolerance) ;
339
+ assert(normalized.same_direction?(@vector))
340
+ end
341
+
342
+ def test_parallel
343
+ vectorZero = Vector3.new( 0.0, 0.0, 0.0) ;
344
+ vector1 = Vector3.new( 1.0, 2.0, 3.0 ) ;
345
+ vector2 = Vector3.new( 1.0, 1.0, 4.0 ) ;
346
+ vector3 = Vector3.new( 2.0, 4.0, 6.0 ) ;
347
+ vector4 = Vector3.new( -1.0, -2.0, -3.0 ) ;
348
+ vector5 = Vector3.new( -3.0, -6.0, -9.0 ) ;
349
+
350
+ assert(!vector1.parallel?(vectorZero)) ;
351
+ assert(!vectorZero.parallel?(vector2)) ;
352
+ assert(!vector1.parallel?(vector2)) ;
353
+
354
+ assert(vector1.parallel?(vector3)) ;
355
+ assert(vector1.parallel?(vector4)) ;
356
+ assert(vector1.parallel?(vector5)) ;
357
+ end
358
+
359
+ def test_same_direction
360
+ vectorZero = Vector3.new( 0.0, 0.0, 0.0) ;
361
+ vector1 = Vector3.new( 1.0, 2.0, 3.0 ) ;
362
+ vector2 = Vector3.new( 1.0, 1.0, 4.0 ) ;
363
+ vector3 = Vector3.new( 2.0, 4.0, 6.0 ) ;
364
+ vector4 = Vector3.new( -1.0, -2.0, -3.0 ) ;
365
+ vector5 = Vector3.new( -3.0, -6.0, -9.0 ) ;
366
+
367
+ assert(!vector1.same_direction?(vectorZero)) ;
368
+ assert(!vectorZero.same_direction?(vector2)) ;
369
+ assert(!vector1.same_direction?(vector2)) ;
370
+
371
+ assert(vector1.same_direction?(vector3)) ;
372
+ assert(!vector1.same_direction?(vector4)) ;
373
+ assert(!vector1.same_direction?(vector5)) ;
374
+ end
375
+
376
+ def test_project
377
+ vector1 = Vector3.new(1.0, 1.0, 0.0)
378
+ vector2 = Vector3.new(0.0, 1.0, 0.0)
379
+
380
+ projectedVector, parameter = vector2.project_to(vector1)
381
+ assert(Vector3.new(0.5,0.5,0.0) == projectedVector)
382
+ assert_in_delta(0.5, parameter, projectedVector.tolerance)
383
+
384
+ projectedVector, parameter = vector2.project_to(Vector3.new(0.0, 0.0, 0.0))
385
+ assert(Vector3.new() == projectedVector)
386
+ assert_in_delta(0.0, parameter, projectedVector.tolerance)
387
+
388
+ vector3 = Vector3.new(5.0, -1.0, 4.0)
389
+ projectedVector, parmeter = vector3.project_to(vector1)
390
+ assert(projectedVector.same_direction?(vector1))
391
+ end
392
+
393
+ def test_to_column_vector
394
+ matrix = @vector.to_column_vector
395
+ assert_equal( 3, matrix.row_size )
396
+ assert_equal( 1, matrix.column_size )
397
+ assert_equal( 1, matrix[0,0])
398
+ assert_equal( 2, matrix[1,0])
399
+ assert_equal( 3, matrix[2,0])
400
+
401
+ matrix = @vector_init_zero.to_column_vector
402
+ assert_equal( 3, matrix.row_size )
403
+ assert_equal( 1, matrix.column_size )
404
+ assert_equal( 0, matrix[0,0])
405
+ assert_equal( 0, matrix[1,0])
406
+ assert_equal( 0, matrix[2,0])
407
+ end
408
+
409
+ def test_arbitrary_orthogonal
410
+ vec1 = Vector3.new( 1.0, 2.0, 3.0 )
411
+ vec2 = Vector3.new( 1.0, 1.0, 4.0 )
412
+ vec3 = Vector3.new( 2.0, 4.0, 6.0 )
413
+ vec4 = Vector3.new( -1.0, -2.0, -3.0 )
414
+ vecx = Vector3.new( 1.0, 0.0, 0.0 )
415
+ vecy = Vector3.new( 0.0, -3.0, 0.0 )
416
+ vecz = Vector3.new( 0.0, 0.0, 5.0 )
417
+ veczero = Vector3.new( 0.0, 0.0, 0.0 )
418
+
419
+ vec1_orth = vec1.arbitrary_orthogonal
420
+ vec2_orth = vec2.arbitrary_orthogonal
421
+ vec3_orth = vec3.arbitrary_orthogonal
422
+ vec4_orth = vec4.arbitrary_orthogonal
423
+ vecx_orth = vecx.arbitrary_orthogonal
424
+ vecy_orth = vecy.arbitrary_orthogonal
425
+ vecz_orth = vecz.arbitrary_orthogonal
426
+ veczero_orth = veczero.arbitrary_orthogonal
427
+
428
+ assert_in_delta(0.5*Math::PI, vec1.angle(vec1_orth), vec1.tolerance)
429
+ assert_in_delta(0.5*Math::PI, vec2.angle(vec2_orth), vec2.tolerance)
430
+ assert_in_delta(0.5*Math::PI, vec3.angle(vec3_orth), vec3.tolerance)
431
+ assert_in_delta(0.5*Math::PI, vec4.angle(vec4_orth), vec4.tolerance)
432
+ assert_in_delta(0.5*Math::PI, vecx.angle(vecx_orth), vecx.tolerance)
433
+ assert_in_delta(0.5*Math::PI, vecy.angle(vecy_orth), vecy.tolerance)
434
+ assert_in_delta(0.5*Math::PI, vecz.angle(vecz_orth), vecz.tolerance)
435
+ assert_in_delta(0.0, veczero_orth.x, veczero.tolerance)
436
+ assert_in_delta(0.0, veczero_orth.y, veczero.tolerance)
437
+ assert_in_delta(0.0, veczero_orth.z, veczero.tolerance)
438
+ end
439
+ end