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/.document +5 -5
- data/Gemfile +5 -5
- data/LICENSE.txt +20 -20
- data/README.rdoc +19 -19
- data/Rakefile +45 -45
- data/VERSION +1 -1
- data/gmath3D.gemspec +79 -79
- data/lib/box.rb +146 -145
- data/lib/ellipse.rb +11 -11
- data/lib/ext.rb +82 -82
- data/lib/finite_line.rb +244 -244
- data/lib/geom.rb +20 -20
- data/lib/gmath3D.rb +22 -22
- data/lib/line.rb +122 -122
- data/lib/plane.rb +131 -131
- data/lib/polyline.rb +73 -73
- data/lib/quat.rb +170 -170
- data/lib/rectangle.rb +155 -155
- data/lib/tri_mesh.rb +258 -258
- data/lib/triangle.rb +281 -281
- data/lib/util.rb +36 -36
- data/lib/vector3.rb +231 -227
- data/test/helper.rb +15 -15
- data/test/test_box.rb +172 -167
- data/test/test_ellipse.rb +55 -55
- data/test/test_finite_line.rb +306 -306
- data/test/test_geom.rb +17 -17
- data/test/test_line.rb +146 -146
- data/test/test_matrix_util.rb +84 -84
- data/test/test_plane.rb +200 -200
- data/test/test_polyline.rb +93 -93
- data/test/test_quat.rb +144 -144
- data/test/test_rectangle.rb +184 -184
- data/test/test_tri_mesh.rb +186 -186
- data/test/test_triangle.rb +318 -318
- data/test/test_util.rb +88 -88
- data/test/test_vector3.rb +453 -439
- metadata +8 -8
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
|
39
|
-
assert_equal(
|
40
|
-
assert_equal(
|
41
|
-
assert_equal(
|
42
|
-
|
43
|
-
@
|
44
|
-
|
45
|
-
@vector.
|
46
|
-
|
47
|
-
assert_equal(
|
48
|
-
assert_equal(
|
49
|
-
assert_equal(
|
50
|
-
end
|
51
|
-
|
52
|
-
def
|
53
|
-
|
54
|
-
@vector.
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
assert_equal(
|
62
|
-
assert_equal(
|
63
|
-
assert_equal(
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
assert_equal(4,
|
76
|
-
assert_equal(
|
77
|
-
assert_equal(3,
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
assert_equal(
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
assert_equal(
|
94
|
-
assert_equal(
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
assert(@vector
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
assert(@vector
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
assert_equal(1,
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
hash
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
assert_equal(
|
151
|
-
|
152
|
-
|
153
|
-
assert_equal(
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
assert_equal(
|
167
|
-
assert_equal(
|
168
|
-
|
169
|
-
|
170
|
-
def
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
def
|
194
|
-
|
195
|
-
|
196
|
-
|
197
|
-
|
198
|
-
|
199
|
-
|
200
|
-
|
201
|
-
|
202
|
-
|
203
|
-
|
204
|
-
|
205
|
-
assert_equal(
|
206
|
-
|
207
|
-
|
208
|
-
|
209
|
-
|
210
|
-
|
211
|
-
|
212
|
-
|
213
|
-
|
214
|
-
end
|
215
|
-
|
216
|
-
|
217
|
-
|
218
|
-
|
219
|
-
|
220
|
-
|
221
|
-
|
222
|
-
|
223
|
-
|
224
|
-
|
225
|
-
|
226
|
-
|
227
|
-
assert_equal(
|
228
|
-
|
229
|
-
|
230
|
-
|
231
|
-
|
232
|
-
|
233
|
-
|
234
|
-
|
235
|
-
|
236
|
-
end
|
237
|
-
|
238
|
-
|
239
|
-
|
240
|
-
|
241
|
-
|
242
|
-
|
243
|
-
|
244
|
-
|
245
|
-
|
246
|
-
|
247
|
-
|
248
|
-
|
249
|
-
|
250
|
-
|
251
|
-
|
252
|
-
|
253
|
-
|
254
|
-
|
255
|
-
assert_raises ArgumentError do
|
256
|
-
invalidResult = @vector
|
257
|
-
end
|
258
|
-
|
259
|
-
|
260
|
-
|
261
|
-
|
262
|
-
|
263
|
-
|
264
|
-
|
265
|
-
|
266
|
-
|
267
|
-
|
268
|
-
|
269
|
-
|
270
|
-
|
271
|
-
|
272
|
-
|
273
|
-
|
274
|
-
|
275
|
-
|
276
|
-
|
277
|
-
|
278
|
-
|
279
|
-
|
280
|
-
|
281
|
-
|
282
|
-
|
283
|
-
|
284
|
-
|
285
|
-
|
286
|
-
|
287
|
-
|
288
|
-
assert_in_delta(0.0,
|
289
|
-
assert_in_delta(
|
290
|
-
|
291
|
-
|
292
|
-
|
293
|
-
|
294
|
-
|
295
|
-
|
296
|
-
|
297
|
-
|
298
|
-
|
299
|
-
|
300
|
-
|
301
|
-
|
302
|
-
|
303
|
-
|
304
|
-
|
305
|
-
|
306
|
-
|
307
|
-
|
308
|
-
|
309
|
-
|
310
|
-
|
311
|
-
|
312
|
-
|
313
|
-
|
314
|
-
|
315
|
-
|
316
|
-
|
317
|
-
|
318
|
-
|
319
|
-
|
320
|
-
|
321
|
-
|
322
|
-
|
323
|
-
|
324
|
-
|
325
|
-
|
326
|
-
|
327
|
-
|
328
|
-
|
329
|
-
|
330
|
-
|
331
|
-
|
332
|
-
|
333
|
-
|
334
|
-
|
335
|
-
|
336
|
-
|
337
|
-
|
338
|
-
assert_in_delta(
|
339
|
-
|
340
|
-
end
|
341
|
-
|
342
|
-
|
343
|
-
|
344
|
-
|
345
|
-
|
346
|
-
|
347
|
-
|
348
|
-
|
349
|
-
|
350
|
-
|
351
|
-
|
352
|
-
|
353
|
-
|
354
|
-
|
355
|
-
|
356
|
-
|
357
|
-
|
358
|
-
|
359
|
-
|
360
|
-
|
361
|
-
|
362
|
-
|
363
|
-
|
364
|
-
|
365
|
-
|
366
|
-
|
367
|
-
|
368
|
-
assert(
|
369
|
-
assert(
|
370
|
-
|
371
|
-
|
372
|
-
|
373
|
-
|
374
|
-
|
375
|
-
|
376
|
-
|
377
|
-
|
378
|
-
|
379
|
-
|
380
|
-
|
381
|
-
assert(
|
382
|
-
|
383
|
-
|
384
|
-
|
385
|
-
assert(
|
386
|
-
|
387
|
-
|
388
|
-
|
389
|
-
|
390
|
-
|
391
|
-
|
392
|
-
|
393
|
-
|
394
|
-
|
395
|
-
|
396
|
-
|
397
|
-
|
398
|
-
|
399
|
-
|
400
|
-
|
401
|
-
|
402
|
-
|
403
|
-
|
404
|
-
|
405
|
-
|
406
|
-
|
407
|
-
|
408
|
-
|
409
|
-
|
410
|
-
|
411
|
-
|
412
|
-
|
413
|
-
|
414
|
-
|
415
|
-
|
416
|
-
|
417
|
-
|
418
|
-
|
419
|
-
|
420
|
-
|
421
|
-
|
422
|
-
|
423
|
-
|
424
|
-
|
425
|
-
|
426
|
-
|
427
|
-
|
428
|
-
|
429
|
-
|
430
|
-
|
431
|
-
|
432
|
-
|
433
|
-
|
434
|
-
|
435
|
-
|
436
|
-
|
437
|
-
|
438
|
-
|
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_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
|