gmath3D 0.2.5 → 1.0.0

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