gmath3D 0.2.5 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,306 +1,306 @@
1
- $LOAD_PATH.unshift(File.dirname(__FILE__))
2
- require 'helper'
3
-
4
- include GMath3D
5
-
6
- MiniTest::Unit.autorun
7
-
8
- class FiniteLineTestCase < MiniTest::Unit::TestCase
9
- def test_initialize
10
- start_point_tmp = Vector3.new(1.0, 0.0, 2.0)
11
- end_point_tmp = Vector3.new(1.0, -3.5, 1.0)
12
- line = FiniteLine.new(start_point_tmp, end_point_tmp)
13
-
14
- assert_equal(start_point_tmp ,line.start_point)
15
- assert_equal(end_point_tmp ,line.end_point )
16
-
17
- lineDefault = FiniteLine.new()
18
- assert_equal(Vector3.new(0,0,0), lineDefault.start_point)
19
- assert_equal(Vector3.new(1,0,0), lineDefault.end_point )
20
- end
21
-
22
- def test_to_s
23
- line = FiniteLine.new(Vector3.new(1,0,2), Vector3.new(1,-3.5,2))
24
- assert_equal("FiniteLine[from[1, 0, 2], to[1, -3.5, 2]]", line.to_s)
25
- end
26
-
27
- def test_rotation
28
- line = FiniteLine.new(Vector3.new(1,0,0), Vector3.new(2,0,0))
29
- angle = -45.0 * Math::PI / 180.0
30
- mat = Matrix.from_axis( Vector3.new(0,0,1), angle )
31
- rotated_line = line.rotate(mat)
32
- assert_equal(Vector3.new(Math.sqrt(2)/2.0,Math.sqrt(2)/2.0,0), rotated_line.start_point)
33
- assert_equal(Vector3.new(Math.sqrt(2),Math.sqrt(2),0), rotated_line.end_point)
34
- end
35
-
36
- def test_equals
37
- line = FiniteLine.new()
38
- shallow_copied = line
39
- assert(line == shallow_copied)
40
- assert(line.equal?(shallow_copied))
41
- assert(line != nil)
42
- assert(line != "string")
43
-
44
- assert_equal(FiniteLine.new(Vector3.new(1,2,3), Vector3.new(2,3,4)),
45
- FiniteLine.new(Vector3.new(1.0,2.0,3.0), Vector3.new(2.0,3.0,4.0)))
46
-
47
- assert(FiniteLine.new(Vector3.new(1,2,3), Vector3.new(2,3,4)) == FiniteLine.new(Vector3.new(1.0,2.0,3.0), Vector3.new(2.0,3.0,4.0)))
48
- assert(FiniteLine.new(Vector3.new(1,2,3), Vector3.new(2,3,3)) != FiniteLine.new(Vector3.new(1.0,2.0,3.0), Vector3.new(2.0,3.0,4.0)))
49
- assert(FiniteLine.new(Vector3.new(1,2,3), Vector3.new(2,3,4)) != FiniteLine.new(Vector3.new(2,3,4), Vector3.new(1,2,3)))
50
- end
51
-
52
- def test_clone
53
- line = FiniteLine.new()
54
- shallow_copied = line
55
- shallow_copied.start_point.x = -1
56
- assert(line == shallow_copied)
57
- assert(line.equal?(shallow_copied))
58
- assert_equal(-1, shallow_copied.start_point.x)
59
-
60
- cloned = line.clone
61
- assert(line == cloned)
62
- assert(!line.equal?(cloned))
63
- cloned.start_point.x = -2
64
- assert_equal(-2, cloned.start_point.x)
65
-
66
- assert_equal(-1, line.start_point.x) # original never changed in editing cloned one.
67
- end
68
-
69
- def test_box
70
- start_point_tmp = Vector3.new(1.0, 0.0, 2.0)
71
- end_point_tmp = Vector3.new(1.0, -3.5, 5.0)
72
- line = FiniteLine.new(start_point_tmp, end_point_tmp)
73
- assert_equal(Vector3.new(1, -3.5, 2), line.box.min_point)
74
- assert_equal(Vector3.new(1, 0, 5), line.box.max_point)
75
-
76
- line = FiniteLine.new()
77
- assert_equal(Vector3.new(), line.box.min_point)
78
- assert_equal(Vector3.new(1,0,0), line.box.max_point)
79
- end
80
-
81
- def test_direction
82
- start_point_tmp = Vector3.new(1.0, 0.0, 2.0)
83
- end_point_tmp = Vector3.new(1.0, -3.5, 1.0)
84
- line = FiniteLine.new(start_point_tmp, end_point_tmp)
85
- assert_in_delta( 0.0, line.direction.x, line.tolerance)
86
- assert_in_delta(-3.5, line.direction.y, line.tolerance)
87
- assert_in_delta(-1.0, line.direction.z, line.tolerance)
88
- end
89
-
90
- def test_param
91
- start_point_tmp = Vector3.new(1.0, 1.0, 2.0)
92
- end_point_tmp = Vector3.new(2.0, 3.0, -4.0)
93
- line = FiniteLine.new(start_point_tmp, end_point_tmp)
94
- assert_equal( Vector3.new(1.0, 1.0, 2.0), line.point(0.0))
95
- assert_equal( Vector3.new(1.5, 2.0, -1.0), line.point(0.5))
96
- assert_equal( Vector3.new(2.0, 3.0, -4.0), line.point(1))
97
-
98
- # if param is smaller than 0 or bigger than 1 then return nil
99
- assert_equal( nil, line.point(-1.0))
100
- assert_equal( nil, line.point(1.2))
101
- end
102
-
103
- def test_length
104
- start_point_tmp = Vector3.new(0.0, 0.0, 2.0)
105
- end_point_tmp = Vector3.new(2.0, 2.0, 2.0)
106
- line = FiniteLine.new(start_point_tmp, end_point_tmp)
107
-
108
- assert_in_delta(Math::sqrt(8), line.length, line.tolerance)
109
- end
110
-
111
- def test_distance_to_point
112
- start_point_tmp = Vector3.new(0.0, 0.0, 2.0)
113
- end_point_tmp = Vector3.new(2.0, 2.0, 2.0)
114
- line = FiniteLine.new(start_point_tmp, end_point_tmp)
115
-
116
- targetPoint = Vector3.new(1.0, 1.0, -4.0)
117
- distance, pointOnLine, parameter = line.distance(targetPoint)
118
- assert_in_delta(6.0, distance, targetPoint.tolerance)
119
- assert_equal( Vector3.new(1.0, 1.0, 2.0), pointOnLine)
120
- assert_in_delta(0.5, parameter, targetPoint.tolerance)
121
-
122
- targetPoint2 = Vector3.new(3.0, 3.0, 2.0)
123
- distance, pointOnLine, parameter = line.distance(targetPoint2)
124
- assert_in_delta(Math::sqrt(2), distance, targetPoint.tolerance)
125
- assert_equal( end_point_tmp, pointOnLine)
126
- assert_in_delta(1.0, parameter, targetPoint.tolerance)
127
-
128
- targetPoint3 = Vector3.new(0.0, -1.0, 3.0)
129
- distance, pointOnLine, parameter = line.distance(targetPoint3)
130
- assert_in_delta(Math::sqrt(2), distance, targetPoint.tolerance)
131
- assert_equal( start_point_tmp, pointOnLine)
132
- assert_in_delta(0.0, parameter, targetPoint.tolerance)
133
-
134
- target_point4 = Vector3.new( 2,3,3)
135
- line = FiniteLine.new( Vector3.new(1,2,3), Vector3.new(1,4,3))
136
- distance, point_on_line, parameter = line.distance( target_point4 )
137
- assert_in_delta( 1, distance, line.tolerance )
138
- assert_equal( Vector3.new(1,3,3), point_on_line )
139
- assert_in_delta( 0.5, parameter, line.tolerance )
140
- end
141
-
142
- def test_distance_to_line
143
- start_point_tmp = Vector3.new(0.0, 0.0, 2.0)
144
- end_point_tmp = Vector3.new(2.0, 2.0, 2.0)
145
- finite_line = FiniteLine.new(start_point_tmp, end_point_tmp)
146
-
147
- #intersect case
148
- infinite_line = Line.new(Vector3.new(0.0, 2.0, 3.0), Vector3.new(2.0, -2.0, -2.0))
149
- distance, point1, point2, parameter1, parameter2 = finite_line.distance(infinite_line)
150
- assert_in_delta(0.0, distance, infinite_line.tolerance)
151
- assert_equal( Vector3.new(1,1,2), point1)
152
- assert_equal( Vector3.new(1,1,2), point2)
153
- assert_in_delta(0.5, parameter1, infinite_line.tolerance)
154
- assert_in_delta(0.5, parameter2, infinite_line.tolerance)
155
-
156
- #not intersect case
157
- infinite_line = Line.new(Vector3.new(0.0, 2.0, 4.0), Vector3.new(2.0, -2.0, 0.0))
158
- distance, point1, point2, parameter1, parameter2 = finite_line.distance(infinite_line)
159
- assert_in_delta(2.0, distance, infinite_line.tolerance)
160
- assert_equal( Vector3.new(1,1,2), point1)
161
- assert_equal( Vector3.new(1,1,4), point2)
162
- assert_in_delta(0.5, parameter1, infinite_line.tolerance)
163
- assert_in_delta(0.5, parameter2, infinite_line.tolerance)
164
-
165
- #not intersect case2
166
- infinite_line = Line.new(Vector3.new(-2.0, 2.0, 4.0), Vector3.new(0.0, -2.0, 0.0))
167
- distance, point1, point2, parameter1, parameter2 = finite_line.distance(infinite_line)
168
- assert_in_delta(Math::sqrt(8), distance, infinite_line.tolerance)
169
- assert_equal( Vector3.new(0,0,2), point1)
170
- assert_equal( Vector3.new(-2,0,4), point2)
171
- assert_in_delta(0.0, parameter1, infinite_line.tolerance)
172
- assert_in_delta(1.0, parameter2, infinite_line.tolerance)
173
-
174
- #not intersect case3
175
- infinite_line = Line.new(Vector3.new(12.0, 12.0, 2.0), Vector3.new(0, 5.0, 0.0))
176
- distance, point1, point2, parameter1, parameter2 = finite_line.distance(infinite_line)
177
- assert_in_delta(10, distance, infinite_line.tolerance)
178
- assert_equal( Vector3.new(2,2,2), point1)
179
- assert_equal( Vector3.new(12,2,2), point2)
180
- assert_in_delta(1.0, parameter1, infinite_line.tolerance)
181
- assert_in_delta(-2.0, parameter2, infinite_line.tolerance)
182
-
183
- #parallel case
184
- infinite_line = Line.new(Vector3.new(0, -2.0, 6.0), Vector3.new(-1.0, -1.0, 0.0))
185
- distance, point1, point2, parameter1, parameter2 = finite_line.distance(infinite_line)
186
- assert_in_delta(Math::sqrt(18), distance, infinite_line.tolerance)
187
- assert_equal( nil, point1)
188
- assert_equal( nil, point2)
189
- assert_equal( nil, parameter1)
190
- assert_equal( nil, parameter2)
191
-
192
- #including case
193
- infinite_line = Line.new(Vector3.new(-2, -2.0, 2.0), Vector3.new(1.0, 1.0, 0.0))
194
- distance, point1, point2, parameter1, parameter2 = finite_line.distance(infinite_line)
195
- assert_in_delta(0, distance, infinite_line.tolerance)
196
- assert_equal( nil, point1)
197
- assert_equal( nil, point2)
198
- assert_equal( nil, parameter1)
199
- assert_equal( nil, parameter2)
200
- end
201
-
202
- def test_distance_to_finite_line
203
- finite_line = FiniteLine.new(Vector3.new(0.0, 0.0, 2.0), Vector3.new(2.0,2.0,2.0))
204
-
205
- #intersect case
206
- target_finite_line = FiniteLine.new(Vector3.new(2.0, 0.0, 2.0), Vector3.new(0.0, 2.0,2.0))
207
- distance, point1, point2, parameter1, parameter2 = finite_line.distance(target_finite_line)
208
- assert_in_delta(0.0, distance, target_finite_line.tolerance)
209
- assert_equal( Vector3.new(1,1,2), point1)
210
- assert_equal( Vector3.new(1,1,2), point2)
211
- assert_in_delta(0.5, parameter1, target_finite_line.tolerance)
212
- assert_in_delta(0.5, parameter2, target_finite_line.tolerance)
213
-
214
- #not intersect case1
215
- target_finite_line = FiniteLine.new(Vector3.new(2.0, 0.0, 4.0), Vector3.new(0.0, 2.0, 4.0))
216
- distance, point1, point2, parameter1, parameter2 = finite_line.distance(target_finite_line)
217
- assert_in_delta(2.0, distance, target_finite_line.tolerance)
218
- assert_equal( Vector3.new(1,1,2), point1)
219
- assert_equal( Vector3.new(1,1,4), point2)
220
- assert_in_delta(0.5, parameter1, target_finite_line.tolerance)
221
- assert_in_delta(0.5, parameter2, target_finite_line.tolerance)
222
-
223
- #not intersect case2
224
- target_finite_line = FiniteLine.new(Vector3.new(3.0, 2.0, 2.0), Vector3.new(5.0, 0.0, 2.0))
225
- distance, point1, point2, parameter1, parameter2 = finite_line.distance(target_finite_line)
226
- assert_in_delta(1.0, distance, target_finite_line.tolerance)
227
- assert_equal( Vector3.new(2,2,2), point1)
228
- assert_equal( Vector3.new(3,2,2), point2)
229
- assert_in_delta(1.0, parameter1, target_finite_line.tolerance)
230
- assert_in_delta(0.0, parameter2, target_finite_line.tolerance)
231
-
232
- #not intersect case3
233
- target_finite_line = FiniteLine.new(Vector3.new(-3.0, 5.0, 5.0), Vector3.new(0.0, 2.0, 2.0))
234
- distance, point1, point2, parameter1, parameter2 = finite_line.distance(target_finite_line)
235
- assert_in_delta(Math::sqrt(2), distance, target_finite_line.tolerance)
236
- assert_equal( Vector3.new(1,1,2), point1)
237
- assert_equal( Vector3.new(0,2,2), point2)
238
- assert_in_delta(0.5, parameter1, target_finite_line.tolerance)
239
- assert_in_delta(1.0, parameter2, target_finite_line.tolerance)
240
-
241
- #not intersect case4
242
- target_finite_line = FiniteLine.new(Vector3.new(-2.0, 5.0, 0), Vector3.new(-2.0, 2.0, 0))
243
- distance, point1, point2, parameter1, parameter2 = finite_line.distance(target_finite_line)
244
- assert_in_delta(Math::sqrt(12), distance, target_finite_line.tolerance)
245
- assert_equal( Vector3.new(0,0,2), point1)
246
- assert_equal( Vector3.new(-2,2,0), point2)
247
- assert_in_delta(0, parameter1, target_finite_line.tolerance)
248
- assert_in_delta(1, parameter2, target_finite_line.tolerance)
249
-
250
- #parallel case1
251
- target_finite_line = FiniteLine.new(Vector3.new(4,4,0),Vector3.new(5,5,0))
252
- distance, point1, point2, parameter1, parameter2 = finite_line.distance(target_finite_line)
253
- assert_in_delta(Math::sqrt(12), distance, target_finite_line.tolerance)
254
- assert_equal( Vector3.new(2,2,2), point1)
255
- assert_equal( Vector3.new(4,4,0), point2)
256
- assert_in_delta(1.0, parameter1, target_finite_line.tolerance)
257
- assert_in_delta(0.0, parameter2, target_finite_line.tolerance)
258
-
259
- #parallel case2
260
- target_finite_line = FiniteLine.new(Vector3.new(2,2,0),Vector3.new(5,5,0))
261
- distance, point1, point2, parameter1, parameter2 = finite_line.distance(target_finite_line)
262
- assert_in_delta(2, distance, target_finite_line.tolerance)
263
- assert_equal( Vector3.new(2,2,2), point1)
264
- assert_equal( Vector3.new(2,2,0), point2)
265
- assert_in_delta(1.0, parameter1, target_finite_line.tolerance)
266
- assert_in_delta(0.0, parameter2, target_finite_line.tolerance)
267
-
268
- #parallel case3
269
- target_finite_line = FiniteLine.new(Vector3.new(1,1,1),Vector3.new(5,5,1))
270
- distance, point1, point2, parameter1, parameter2 = finite_line.distance(target_finite_line)
271
- assert_in_delta(1, distance, target_finite_line.tolerance)
272
- assert_equal( nil, point1)
273
- assert_equal( nil, point2)
274
- assert_equal( nil, parameter1)
275
- assert_equal( nil, parameter2)
276
-
277
- #including case1
278
- target_finite_line = FiniteLine.new(Vector3.new(2,2,2),Vector3.new(5,5,2))
279
- distance, point1, point2, parameter1, parameter2 = finite_line.distance(target_finite_line)
280
- assert_in_delta(0, distance, target_finite_line.tolerance)
281
- assert_equal( Vector3.new(2,2,2), point1)
282
- assert_equal( Vector3.new(2,2,2), point2)
283
- assert_in_delta(1.0, parameter1, target_finite_line.tolerance)
284
- assert_in_delta(0.0, parameter2, target_finite_line.tolerance)
285
-
286
- #including case2
287
- target_finite_line = FiniteLine.new(Vector3.new(1,1,2),Vector3.new(5,5,2))
288
- distance, point1, point2, parameter1, parameter2 = finite_line.distance(target_finite_line)
289
- assert_in_delta(0, distance, target_finite_line.tolerance)
290
- assert_equal( nil, point1)
291
- assert_equal( nil, point2)
292
- assert_equal( nil, parameter1)
293
- assert_equal( nil, parameter2)
294
- end
295
-
296
- def test_distance_to_invalid_value
297
- finite_line = FiniteLine.new(Vector3.new(0,1,2), Vector3.new(2,3,4))
298
- assert_raises ArgumentError do
299
- finite_line.distance(4)
300
- end
301
- assert_raises ArgumentError do
302
- finite_line.distance(nil)
303
- end
304
- end
305
-
306
- end
1
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
2
+ require 'helper'
3
+
4
+ include GMath3D
5
+
6
+ MiniTest::Unit.autorun
7
+
8
+ class FiniteLineTestCase < MiniTest::Unit::TestCase
9
+ def test_initialize
10
+ start_point_tmp = Vector3.new(1.0, 0.0, 2.0)
11
+ end_point_tmp = Vector3.new(1.0, -3.5, 1.0)
12
+ line = FiniteLine.new(start_point_tmp, end_point_tmp)
13
+
14
+ assert_equal(start_point_tmp ,line.start_point)
15
+ assert_equal(end_point_tmp ,line.end_point )
16
+
17
+ lineDefault = FiniteLine.new()
18
+ assert_equal(Vector3.new(0,0,0), lineDefault.start_point)
19
+ assert_equal(Vector3.new(1,0,0), lineDefault.end_point )
20
+ end
21
+
22
+ def test_to_s
23
+ line = FiniteLine.new(Vector3.new(1,0,2), Vector3.new(1,-3.5,2))
24
+ assert_equal("FiniteLine[from[1, 0, 2], to[1, -3.5, 2]]", line.to_s)
25
+ end
26
+
27
+ def test_rotation
28
+ line = FiniteLine.new(Vector3.new(1,0,0), Vector3.new(2,0,0))
29
+ angle = -45.0 * Math::PI / 180.0
30
+ mat = Matrix.from_axis( Vector3.new(0,0,1), angle )
31
+ rotated_line = line.rotate(mat)
32
+ assert_equal(Vector3.new(Math.sqrt(2)/2.0,Math.sqrt(2)/2.0,0), rotated_line.start_point)
33
+ assert_equal(Vector3.new(Math.sqrt(2),Math.sqrt(2),0), rotated_line.end_point)
34
+ end
35
+
36
+ def test_equals
37
+ line = FiniteLine.new()
38
+ shallow_copied = line
39
+ assert(line == shallow_copied)
40
+ assert(line.equal?(shallow_copied))
41
+ assert(line != nil)
42
+ assert(line != "string")
43
+
44
+ assert_equal(FiniteLine.new(Vector3.new(1,2,3), Vector3.new(2,3,4)),
45
+ FiniteLine.new(Vector3.new(1.0,2.0,3.0), Vector3.new(2.0,3.0,4.0)))
46
+
47
+ assert(FiniteLine.new(Vector3.new(1,2,3), Vector3.new(2,3,4)) == FiniteLine.new(Vector3.new(1.0,2.0,3.0), Vector3.new(2.0,3.0,4.0)))
48
+ assert(FiniteLine.new(Vector3.new(1,2,3), Vector3.new(2,3,3)) != FiniteLine.new(Vector3.new(1.0,2.0,3.0), Vector3.new(2.0,3.0,4.0)))
49
+ assert(FiniteLine.new(Vector3.new(1,2,3), Vector3.new(2,3,4)) != FiniteLine.new(Vector3.new(2,3,4), Vector3.new(1,2,3)))
50
+ end
51
+
52
+ def test_clone
53
+ line = FiniteLine.new()
54
+ shallow_copied = line
55
+ shallow_copied.start_point.x = -1
56
+ assert(line == shallow_copied)
57
+ assert(line.equal?(shallow_copied))
58
+ assert_equal(-1, shallow_copied.start_point.x)
59
+
60
+ cloned = line.clone
61
+ assert(line == cloned)
62
+ assert(!line.equal?(cloned))
63
+ cloned.start_point.x = -2
64
+ assert_equal(-2, cloned.start_point.x)
65
+
66
+ assert_equal(-1, line.start_point.x) # original never changed in editing cloned one.
67
+ end
68
+
69
+ def test_box
70
+ start_point_tmp = Vector3.new(1.0, 0.0, 2.0)
71
+ end_point_tmp = Vector3.new(1.0, -3.5, 5.0)
72
+ line = FiniteLine.new(start_point_tmp, end_point_tmp)
73
+ assert_equal(Vector3.new(1, -3.5, 2), line.box.min_point)
74
+ assert_equal(Vector3.new(1, 0, 5), line.box.max_point)
75
+
76
+ line = FiniteLine.new()
77
+ assert_equal(Vector3.new(), line.box.min_point)
78
+ assert_equal(Vector3.new(1,0,0), line.box.max_point)
79
+ end
80
+
81
+ def test_direction
82
+ start_point_tmp = Vector3.new(1.0, 0.0, 2.0)
83
+ end_point_tmp = Vector3.new(1.0, -3.5, 1.0)
84
+ line = FiniteLine.new(start_point_tmp, end_point_tmp)
85
+ assert_in_delta( 0.0, line.direction.x, line.tolerance)
86
+ assert_in_delta(-3.5, line.direction.y, line.tolerance)
87
+ assert_in_delta(-1.0, line.direction.z, line.tolerance)
88
+ end
89
+
90
+ def test_param
91
+ start_point_tmp = Vector3.new(1.0, 1.0, 2.0)
92
+ end_point_tmp = Vector3.new(2.0, 3.0, -4.0)
93
+ line = FiniteLine.new(start_point_tmp, end_point_tmp)
94
+ assert_equal( Vector3.new(1.0, 1.0, 2.0), line.point(0.0))
95
+ assert_equal( Vector3.new(1.5, 2.0, -1.0), line.point(0.5))
96
+ assert_equal( Vector3.new(2.0, 3.0, -4.0), line.point(1))
97
+
98
+ # if param is smaller than 0 or bigger than 1 then return nil
99
+ assert_equal( nil, line.point(-1.0))
100
+ assert_equal( nil, line.point(1.2))
101
+ end
102
+
103
+ def test_length
104
+ start_point_tmp = Vector3.new(0.0, 0.0, 2.0)
105
+ end_point_tmp = Vector3.new(2.0, 2.0, 2.0)
106
+ line = FiniteLine.new(start_point_tmp, end_point_tmp)
107
+
108
+ assert_in_delta(Math::sqrt(8), line.length, line.tolerance)
109
+ end
110
+
111
+ def test_distance_to_point
112
+ start_point_tmp = Vector3.new(0.0, 0.0, 2.0)
113
+ end_point_tmp = Vector3.new(2.0, 2.0, 2.0)
114
+ line = FiniteLine.new(start_point_tmp, end_point_tmp)
115
+
116
+ targetPoint = Vector3.new(1.0, 1.0, -4.0)
117
+ distance, pointOnLine, parameter = line.distance(targetPoint)
118
+ assert_in_delta(6.0, distance, targetPoint.tolerance)
119
+ assert_equal( Vector3.new(1.0, 1.0, 2.0), pointOnLine)
120
+ assert_in_delta(0.5, parameter, targetPoint.tolerance)
121
+
122
+ targetPoint2 = Vector3.new(3.0, 3.0, 2.0)
123
+ distance, pointOnLine, parameter = line.distance(targetPoint2)
124
+ assert_in_delta(Math::sqrt(2), distance, targetPoint.tolerance)
125
+ assert_equal( end_point_tmp, pointOnLine)
126
+ assert_in_delta(1.0, parameter, targetPoint.tolerance)
127
+
128
+ targetPoint3 = Vector3.new(0.0, -1.0, 3.0)
129
+ distance, pointOnLine, parameter = line.distance(targetPoint3)
130
+ assert_in_delta(Math::sqrt(2), distance, targetPoint.tolerance)
131
+ assert_equal( start_point_tmp, pointOnLine)
132
+ assert_in_delta(0.0, parameter, targetPoint.tolerance)
133
+
134
+ target_point4 = Vector3.new( 2,3,3)
135
+ line = FiniteLine.new( Vector3.new(1,2,3), Vector3.new(1,4,3))
136
+ distance, point_on_line, parameter = line.distance( target_point4 )
137
+ assert_in_delta( 1, distance, line.tolerance )
138
+ assert_equal( Vector3.new(1,3,3), point_on_line )
139
+ assert_in_delta( 0.5, parameter, line.tolerance )
140
+ end
141
+
142
+ def test_distance_to_line
143
+ start_point_tmp = Vector3.new(0.0, 0.0, 2.0)
144
+ end_point_tmp = Vector3.new(2.0, 2.0, 2.0)
145
+ finite_line = FiniteLine.new(start_point_tmp, end_point_tmp)
146
+
147
+ #intersect case
148
+ infinite_line = Line.new(Vector3.new(0.0, 2.0, 3.0), Vector3.new(2.0, -2.0, -2.0))
149
+ distance, point1, point2, parameter1, parameter2 = finite_line.distance(infinite_line)
150
+ assert_in_delta(0.0, distance, infinite_line.tolerance)
151
+ assert_equal( Vector3.new(1,1,2), point1)
152
+ assert_equal( Vector3.new(1,1,2), point2)
153
+ assert_in_delta(0.5, parameter1, infinite_line.tolerance)
154
+ assert_in_delta(0.5, parameter2, infinite_line.tolerance)
155
+
156
+ #not intersect case
157
+ infinite_line = Line.new(Vector3.new(0.0, 2.0, 4.0), Vector3.new(2.0, -2.0, 0.0))
158
+ distance, point1, point2, parameter1, parameter2 = finite_line.distance(infinite_line)
159
+ assert_in_delta(2.0, distance, infinite_line.tolerance)
160
+ assert_equal( Vector3.new(1,1,2), point1)
161
+ assert_equal( Vector3.new(1,1,4), point2)
162
+ assert_in_delta(0.5, parameter1, infinite_line.tolerance)
163
+ assert_in_delta(0.5, parameter2, infinite_line.tolerance)
164
+
165
+ #not intersect case2
166
+ infinite_line = Line.new(Vector3.new(-2.0, 2.0, 4.0), Vector3.new(0.0, -2.0, 0.0))
167
+ distance, point1, point2, parameter1, parameter2 = finite_line.distance(infinite_line)
168
+ assert_in_delta(Math::sqrt(8), distance, infinite_line.tolerance)
169
+ assert_equal( Vector3.new(0,0,2), point1)
170
+ assert_equal( Vector3.new(-2,0,4), point2)
171
+ assert_in_delta(0.0, parameter1, infinite_line.tolerance)
172
+ assert_in_delta(1.0, parameter2, infinite_line.tolerance)
173
+
174
+ #not intersect case3
175
+ infinite_line = Line.new(Vector3.new(12.0, 12.0, 2.0), Vector3.new(0, 5.0, 0.0))
176
+ distance, point1, point2, parameter1, parameter2 = finite_line.distance(infinite_line)
177
+ assert_in_delta(10, distance, infinite_line.tolerance)
178
+ assert_equal( Vector3.new(2,2,2), point1)
179
+ assert_equal( Vector3.new(12,2,2), point2)
180
+ assert_in_delta(1.0, parameter1, infinite_line.tolerance)
181
+ assert_in_delta(-2.0, parameter2, infinite_line.tolerance)
182
+
183
+ #parallel case
184
+ infinite_line = Line.new(Vector3.new(0, -2.0, 6.0), Vector3.new(-1.0, -1.0, 0.0))
185
+ distance, point1, point2, parameter1, parameter2 = finite_line.distance(infinite_line)
186
+ assert_in_delta(Math::sqrt(18), distance, infinite_line.tolerance)
187
+ assert_equal( nil, point1)
188
+ assert_equal( nil, point2)
189
+ assert_equal( nil, parameter1)
190
+ assert_equal( nil, parameter2)
191
+
192
+ #including case
193
+ infinite_line = Line.new(Vector3.new(-2, -2.0, 2.0), Vector3.new(1.0, 1.0, 0.0))
194
+ distance, point1, point2, parameter1, parameter2 = finite_line.distance(infinite_line)
195
+ assert_in_delta(0, distance, infinite_line.tolerance)
196
+ assert_equal( nil, point1)
197
+ assert_equal( nil, point2)
198
+ assert_equal( nil, parameter1)
199
+ assert_equal( nil, parameter2)
200
+ end
201
+
202
+ def test_distance_to_finite_line
203
+ finite_line = FiniteLine.new(Vector3.new(0.0, 0.0, 2.0), Vector3.new(2.0,2.0,2.0))
204
+
205
+ #intersect case
206
+ target_finite_line = FiniteLine.new(Vector3.new(2.0, 0.0, 2.0), Vector3.new(0.0, 2.0,2.0))
207
+ distance, point1, point2, parameter1, parameter2 = finite_line.distance(target_finite_line)
208
+ assert_in_delta(0.0, distance, target_finite_line.tolerance)
209
+ assert_equal( Vector3.new(1,1,2), point1)
210
+ assert_equal( Vector3.new(1,1,2), point2)
211
+ assert_in_delta(0.5, parameter1, target_finite_line.tolerance)
212
+ assert_in_delta(0.5, parameter2, target_finite_line.tolerance)
213
+
214
+ #not intersect case1
215
+ target_finite_line = FiniteLine.new(Vector3.new(2.0, 0.0, 4.0), Vector3.new(0.0, 2.0, 4.0))
216
+ distance, point1, point2, parameter1, parameter2 = finite_line.distance(target_finite_line)
217
+ assert_in_delta(2.0, distance, target_finite_line.tolerance)
218
+ assert_equal( Vector3.new(1,1,2), point1)
219
+ assert_equal( Vector3.new(1,1,4), point2)
220
+ assert_in_delta(0.5, parameter1, target_finite_line.tolerance)
221
+ assert_in_delta(0.5, parameter2, target_finite_line.tolerance)
222
+
223
+ #not intersect case2
224
+ target_finite_line = FiniteLine.new(Vector3.new(3.0, 2.0, 2.0), Vector3.new(5.0, 0.0, 2.0))
225
+ distance, point1, point2, parameter1, parameter2 = finite_line.distance(target_finite_line)
226
+ assert_in_delta(1.0, distance, target_finite_line.tolerance)
227
+ assert_equal( Vector3.new(2,2,2), point1)
228
+ assert_equal( Vector3.new(3,2,2), point2)
229
+ assert_in_delta(1.0, parameter1, target_finite_line.tolerance)
230
+ assert_in_delta(0.0, parameter2, target_finite_line.tolerance)
231
+
232
+ #not intersect case3
233
+ target_finite_line = FiniteLine.new(Vector3.new(-3.0, 5.0, 5.0), Vector3.new(0.0, 2.0, 2.0))
234
+ distance, point1, point2, parameter1, parameter2 = finite_line.distance(target_finite_line)
235
+ assert_in_delta(Math::sqrt(2), distance, target_finite_line.tolerance)
236
+ assert_equal( Vector3.new(1,1,2), point1)
237
+ assert_equal( Vector3.new(0,2,2), point2)
238
+ assert_in_delta(0.5, parameter1, target_finite_line.tolerance)
239
+ assert_in_delta(1.0, parameter2, target_finite_line.tolerance)
240
+
241
+ #not intersect case4
242
+ target_finite_line = FiniteLine.new(Vector3.new(-2.0, 5.0, 0), Vector3.new(-2.0, 2.0, 0))
243
+ distance, point1, point2, parameter1, parameter2 = finite_line.distance(target_finite_line)
244
+ assert_in_delta(Math::sqrt(12), distance, target_finite_line.tolerance)
245
+ assert_equal( Vector3.new(0,0,2), point1)
246
+ assert_equal( Vector3.new(-2,2,0), point2)
247
+ assert_in_delta(0, parameter1, target_finite_line.tolerance)
248
+ assert_in_delta(1, parameter2, target_finite_line.tolerance)
249
+
250
+ #parallel case1
251
+ target_finite_line = FiniteLine.new(Vector3.new(4,4,0),Vector3.new(5,5,0))
252
+ distance, point1, point2, parameter1, parameter2 = finite_line.distance(target_finite_line)
253
+ assert_in_delta(Math::sqrt(12), distance, target_finite_line.tolerance)
254
+ assert_equal( Vector3.new(2,2,2), point1)
255
+ assert_equal( Vector3.new(4,4,0), point2)
256
+ assert_in_delta(1.0, parameter1, target_finite_line.tolerance)
257
+ assert_in_delta(0.0, parameter2, target_finite_line.tolerance)
258
+
259
+ #parallel case2
260
+ target_finite_line = FiniteLine.new(Vector3.new(2,2,0),Vector3.new(5,5,0))
261
+ distance, point1, point2, parameter1, parameter2 = finite_line.distance(target_finite_line)
262
+ assert_in_delta(2, distance, target_finite_line.tolerance)
263
+ assert_equal( Vector3.new(2,2,2), point1)
264
+ assert_equal( Vector3.new(2,2,0), point2)
265
+ assert_in_delta(1.0, parameter1, target_finite_line.tolerance)
266
+ assert_in_delta(0.0, parameter2, target_finite_line.tolerance)
267
+
268
+ #parallel case3
269
+ target_finite_line = FiniteLine.new(Vector3.new(1,1,1),Vector3.new(5,5,1))
270
+ distance, point1, point2, parameter1, parameter2 = finite_line.distance(target_finite_line)
271
+ assert_in_delta(1, distance, target_finite_line.tolerance)
272
+ assert_equal( nil, point1)
273
+ assert_equal( nil, point2)
274
+ assert_equal( nil, parameter1)
275
+ assert_equal( nil, parameter2)
276
+
277
+ #including case1
278
+ target_finite_line = FiniteLine.new(Vector3.new(2,2,2),Vector3.new(5,5,2))
279
+ distance, point1, point2, parameter1, parameter2 = finite_line.distance(target_finite_line)
280
+ assert_in_delta(0, distance, target_finite_line.tolerance)
281
+ assert_equal( Vector3.new(2,2,2), point1)
282
+ assert_equal( Vector3.new(2,2,2), point2)
283
+ assert_in_delta(1.0, parameter1, target_finite_line.tolerance)
284
+ assert_in_delta(0.0, parameter2, target_finite_line.tolerance)
285
+
286
+ #including case2
287
+ target_finite_line = FiniteLine.new(Vector3.new(1,1,2),Vector3.new(5,5,2))
288
+ distance, point1, point2, parameter1, parameter2 = finite_line.distance(target_finite_line)
289
+ assert_in_delta(0, distance, target_finite_line.tolerance)
290
+ assert_equal( nil, point1)
291
+ assert_equal( nil, point2)
292
+ assert_equal( nil, parameter1)
293
+ assert_equal( nil, parameter2)
294
+ end
295
+
296
+ def test_distance_to_invalid_value
297
+ finite_line = FiniteLine.new(Vector3.new(0,1,2), Vector3.new(2,3,4))
298
+ assert_raises ArgumentError do
299
+ finite_line.distance(4)
300
+ end
301
+ assert_raises ArgumentError do
302
+ finite_line.distance(nil)
303
+ end
304
+ end
305
+
306
+ end