gmath3D 0.2.4 → 0.2.5

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,318 +1,318 @@
1
- $LOAD_PATH.unshift(File.dirname(__FILE__))
2
- require 'helper'
3
-
4
- include GMath3D
5
-
6
- MiniTest::Unit.autorun
7
-
8
- class TriangleTestCase < MiniTest::Unit::TestCase
9
- def setup
10
- @triangle = Triangle.new( Vector3.new(1,2,2), Vector3.new(1,4,2), Vector3.new(-1,3,0))
11
- @triangle_default = Triangle.new()
12
- end
13
-
14
- def test_initialize
15
- assert_equal(Vector3.new(1,2,2), @triangle.vertices[0])
16
- assert_equal(Vector3.new(1,4,2), @triangle.vertices[1])
17
- assert_equal(Vector3.new(-1,3,0), @triangle.vertices[2])
18
-
19
- assert_equal(Vector3.new(0,0,0), @triangle_default.vertices[0])
20
- assert_equal(Vector3.new(1,0,0), @triangle_default.vertices[1])
21
- assert_equal(Vector3.new(0,1,0), @triangle_default.vertices[2])
22
- end
23
-
24
- def test_equals
25
- assert(@triangle != nil)
26
- assert(@triangle != "string")
27
-
28
- shallow_copied = @triangle
29
- assert(@triangle.equal?(shallow_copied))
30
- assert(@triangle == shallow_copied)
31
-
32
- assert_equal(@triangle, Triangle.new( Vector3.new(1.0,2.0,2.0), Vector3.new(1.0,4.0,2.0), Vector3.new(-1.0,3.0,0.0)))
33
- assert(@triangle == Triangle.new( Vector3.new(1.0,2.0,2.0), Vector3.new(1.0,4.0,2.0), Vector3.new(-1.0,3.0,0.0)))
34
- assert(@triangle != Triangle.new( Vector3.new(1.0,2.0,2.0), Vector3.new(1.0,4.0,2.0), Vector3.new(-1.0,3.0,0.5)))
35
- end
36
-
37
- def test_clone
38
- shallow_copied = @triangle
39
- shallow_copied.vertices[2].x = 1
40
- assert(@triangle == shallow_copied)
41
- assert(@triangle.equal?(shallow_copied))
42
- assert_equal(1, @triangle.vertices[2].x)
43
- assert_equal(1, shallow_copied.vertices[2].x)
44
-
45
- cloned =@triangle.clone
46
- assert(@triangle == cloned)
47
- assert(!@triangle.equal?(cloned))
48
-
49
- cloned.vertices[2].x = -10
50
- assert_equal(-10, cloned.vertices[2].x)
51
- assert_equal(1, @triangle.vertices[2].x) # original never changed in editing cloned one.
52
- end
53
-
54
- def test_to_s
55
- assert_equal("Triangle[[1, 2, 2], [1, 4, 2], [-1, 3, 0]]", @triangle.to_s)
56
- end
57
-
58
- def test_box
59
- box1 = @triangle.box
60
- box2 = @triangle_default.box
61
- assert_equal(Vector3.new(-1,2,0), box1.min_point)
62
- assert_equal(Vector3.new(1,4,2), box1.max_point)
63
- assert_equal(Vector3.new(0,0,0) , box2.min_point)
64
- assert_equal(Vector3.new(1,1,0) , box2.max_point)
65
- end
66
-
67
- def test_point
68
- assert_equal(@triangle.vertices[0], @triangle.point( Array.new([1,0,0])))
69
- assert_equal(@triangle.vertices[1], @triangle.point( Array.new([0,1,0])))
70
- assert_equal(@triangle.vertices[2], @triangle.point( Array.new([0,0,1])))
71
-
72
- #on edge
73
- assert_equal(Vector3.new(1,3,2), @triangle.point( Array.new([0.5,0.5,0])))
74
- assert_equal(Vector3.new(0,3.5,1), @triangle.point( Array.new([0,0.5,0.5])))
75
- assert_equal(Vector3.new(0,2.5,1), @triangle.point( Array.new([0.5,0,0.5])))
76
-
77
- #inside
78
- assert_equal(Vector3.new(0, 3, 1), @triangle.point( Array.new([0.25,0.25,0.5])))
79
- assert_equal(@triangle.center, @triangle.point( Array.new([1.0/3.0,1.0/3.0,1.0/3.0])))
80
- end
81
-
82
- def test_edges
83
- edges = @triangle.edges
84
- assert_equal(@triangle.vertices[0], edges[0].start_point)
85
- assert_equal(@triangle.vertices[1], edges[0].end_point)
86
- assert_equal(@triangle.vertices[1], edges[1].start_point)
87
- assert_equal(@triangle.vertices[2], edges[1].end_point)
88
- assert_equal(@triangle.vertices[2], edges[2].start_point)
89
- assert_equal(@triangle.vertices[0], edges[2].end_point)
90
- end
91
-
92
- def test_area
93
- assert_in_delta(Math::sqrt(8), @triangle.area, @triangle.tolerance)
94
- assert_in_delta(0.5, @triangle_default.area, @triangle_default.tolerance)
95
- end
96
-
97
- def test_normal
98
- assert_equal( Vector3.new(-1,0,1).normalize(), @triangle.normal )
99
- assert_equal( Vector3.new(0,0,1) ,@triangle_default.normal )
100
- end
101
-
102
- def test_reverse
103
- current_normal = @triangle.normal()
104
- reversed_normal = @triangle.reverse().normal()
105
- assert( current_normal.parallel?(reversed_normal) )
106
- assert( !current_normal.same_direction?(reversed_normal) )
107
- end
108
-
109
- def test_center
110
- center = @triangle.center
111
- assert_in_delta( 0.333333333333333, center.x, @triangle.tolerance)
112
- assert_in_delta( 3.0, center.y, @triangle.tolerance)
113
- assert_in_delta( 1.333333333333333, center.z, @triangle.tolerance)
114
-
115
- barycentric_coordinate = @triangle.barycentric_coordinate(center)
116
- assert_in_delta( 1.0/3.0, barycentric_coordinate[0], @triangle.tolerance)
117
- assert_in_delta( 1.0/3.0, barycentric_coordinate[1], @triangle.tolerance)
118
- assert_in_delta( 1.0/3.0, barycentric_coordinate[2], @triangle.tolerance)
119
- end
120
-
121
- def test_barycentric_coordinate
122
- # on vertex
123
- coord = @triangle.barycentric_coordinate( @triangle.vertices[1])
124
- assert_equal( 3, coord.size )
125
- assert_in_delta( 0, coord[0], @triangle.tolerance)
126
- assert_in_delta( 1, coord[1], @triangle.tolerance)
127
- assert_in_delta( 0, coord[2], @triangle.tolerance)
128
-
129
- # on edge
130
- coord = @triangle.barycentric_coordinate( Vector3.new(1,3,2) )
131
- assert_in_delta( 0.5, coord[0], @triangle.tolerance)
132
- assert_in_delta( 0.5, coord[1], @triangle.tolerance)
133
- assert_in_delta( 0, coord[2], @triangle.tolerance)
134
-
135
- # inside case
136
- coord = @triangle.barycentric_coordinate( Vector3.new(0,3,1) )
137
- assert_in_delta( 1.0, coord[0] + coord[1] + coord[2], @triangle.tolerance)
138
- assert_in_delta( 0.25, coord[0], @triangle.tolerance)
139
- assert_in_delta( 0.25, coord[1], @triangle.tolerance)
140
- assert_in_delta( 0.5, coord[2], @triangle.tolerance)
141
-
142
- # outside case
143
- coord = @triangle_default.barycentric_coordinate( Vector3.new(2,0,0) )
144
- assert_in_delta( -1.0, coord[0], @triangle.tolerance)
145
- assert_in_delta( 2.0, coord[1], @triangle.tolerance)
146
- assert_in_delta( 0.0, coord[2], @triangle.tolerance)
147
-
148
- # outside case
149
- coord = @triangle.barycentric_coordinate( Vector3.new(3,3,4) )
150
- assert_in_delta( 1.0, coord[0], @triangle.tolerance)
151
- assert_in_delta( 1.0, coord[1], @triangle.tolerance)
152
- assert_in_delta( -1.0, coord[2], @triangle.tolerance)
153
-
154
- #invalid argument
155
- assert_raises ArgumentError do
156
- coord = @triangle.barycentric_coordinate( 3 )
157
- end
158
- assert_raises ArgumentError do
159
- coord = @triangle.barycentric_coordinate( nil )
160
- end
161
- end
162
-
163
- def test_contains
164
- check_point = @triangle.center + @triangle.normal*3.0
165
- assert( !@triangle.contains?( check_point) )
166
- assert( @triangle.contains?( @triangle.center) )
167
- assert( @triangle.contains?( Vector3.new( 1,3,2)))
168
- assert( @triangle.contains?( Vector3.new(-1,3,0)))
169
- assert( @triangle_default.contains?( Vector3.new( 0.5, 0.5, 0.0 ) ))
170
- assert( !@triangle_default.contains?( Vector3.new( -1.0, 2.0, 0.0 )))
171
- assert( !@triangle_default.contains?( Vector3.new( 1.0, 1.0, 0.0 )))
172
- end
173
-
174
- def test_distance_to_point
175
- # on inside
176
- check_point = @triangle.center + @triangle.normal*3.0
177
- distance, point_on_triangle = @triangle.distance(check_point)
178
- assert_in_delta(3.0, distance, @triangle.tolerance)
179
- assert_equal(@triangle.center, point_on_triangle)
180
-
181
- # on vertex
182
- distance, point_on_triangle = @triangle_default.distance(Vector3.new(-1,-1,0.5))
183
- assert_in_delta(Math::sqrt(2.25), distance, @triangle_default.tolerance)
184
- assert_equal(@triangle_default.vertices[0], point_on_triangle)
185
-
186
- # on edge
187
- distance, point_on_triangle = @triangle.distance( Vector3.new(2,3,2))
188
- assert_in_delta(1, distance, @triangle_default.tolerance)
189
- assert_equal(Vector3.new(1,3,2), point_on_triangle)
190
-
191
- # contains to inside
192
- distance, point_on_triangle = @triangle_default.distance(@triangle_default.center)
193
- assert_in_delta(0, distance, @triangle_default.tolerance)
194
- assert_equal(@triangle_default.center, point_on_triangle)
195
- end
196
-
197
- def test_distance_to_line
198
- # on inside
199
- line = Line.new(Vector3.new(0,3,-1), Vector3.new(0,0,4))
200
- distance, point_on_triangle, point_on_line, param_on_line = @triangle.distance(line)
201
- assert_equal(0, distance)
202
- assert_equal(Vector3.new(0,3,1), point_on_triangle)
203
- assert_equal(Vector3.new(0,3,1), point_on_line)
204
- assert_in_delta(0.5, param_on_line, line.tolerance)
205
-
206
- # on edge
207
- line = Line.new(Vector3.new(1,3,-1), Vector3.new(0,0,4))
208
- distance, point_on_triangle, point_on_line, param_on_line = @triangle.distance(line)
209
- assert_equal(0, distance)
210
- assert_equal(Vector3.new(1,3,2), point_on_triangle)
211
- assert_equal(Vector3.new(1,3,2), point_on_line)
212
- assert_in_delta(0.75, param_on_line, line.tolerance)
213
-
214
- # on vertex
215
- line = Line.new(Vector3.new(-1,3,0), Vector3.new(1,1,2))
216
- distance, point_on_triangle, point_on_line, param_on_line = @triangle.distance(line)
217
- assert_equal(0, distance)
218
- assert_equal(Vector3.new(-1,3,0), point_on_triangle)
219
- assert_equal(Vector3.new(-1,3,0), point_on_line)
220
- assert_in_delta(0.0, param_on_line, line.tolerance)
221
-
222
- # closest point is out of triangle1
223
- line = Line.new(Vector3.new(2,3,0), Vector3.new(0,0,4))
224
- distance, point_on_triangle, point_on_line, param_on_line = @triangle.distance(line)
225
- assert_equal(1, distance)
226
- assert_equal(Vector3.new(1,3,2), point_on_triangle)
227
- assert_equal(Vector3.new(2,3,2), point_on_line)
228
- assert_in_delta(0.5, param_on_line, line.tolerance)
229
-
230
- # closest point is out of triangle2
231
- line = Line.new(Vector3.new(-2,3,-1), Vector3.new(0,0,1))
232
- distance, point_on_triangle, point_on_line, param_on_line = @triangle.distance(line)
233
- assert_equal(1, distance)
234
- assert_equal(Vector3.new(-1,3,0), point_on_triangle)
235
- assert_equal(Vector3.new(-2,3,0), point_on_line)
236
- assert_in_delta(1, param_on_line, line.tolerance)
237
-
238
- # parallel case
239
- line = Line.new(Vector3.new(1,0,4), Vector3.new(0,6,0))
240
- distance, point_on_triangle, point_on_line, param_on_line = @triangle.distance(line)
241
- assert_equal(2, distance)
242
- assert_equal(nil, point_on_triangle)
243
- assert_equal(nil, point_on_line)
244
- assert_equal(nil, param_on_line)
245
- end
246
-
247
- def test_distance_to_plane
248
- # intersect case
249
- plane = Plane.new(Vector3.new(0,0,1), Vector3.new(1,0,0))
250
- distance, intersect_line, point_on_triangle, point_on_plane = @triangle.distance(plane)
251
- assert_equal( 0, distance )
252
- point1 = Vector3.new(0,2.5,1)
253
- point2 = Vector3.new(0,3.5,1)
254
- assert( FiniteLine.new(point1, point2) == intersect_line || FiniteLine.new(point2, point1) == intersect_line)
255
- assert_equal( nil, point_on_triangle )
256
- assert_equal( nil, point_on_plane )
257
-
258
- # contains edge
259
- plane = Plane.new(Vector3.new(1,0,1), Vector3.new(-1,0,0))
260
- distance, intersect_line, point_on_triangle, point_on_plane = @triangle.distance(plane)
261
- assert_equal( 0, distance )
262
- point1 = Vector3.new(1,2,2)
263
- point2 = Vector3.new(1,4,2)
264
- assert( FiniteLine.new(point1, point2) == intersect_line || FiniteLine.new(point2, point1) == intersect_line)
265
- assert_equal( nil, point_on_triangle )
266
- assert_equal( nil, point_on_plane )
267
-
268
- # contains vertex
269
- plane = Plane.new(Vector3.new(-1,0,1), Vector3.new(1,0,0))
270
- distance, intersect_line, point_on_triangle, point_on_plane = @triangle.distance(plane)
271
- assert_equal( 0, distance )
272
- assert_equal( nil, intersect_line)
273
- assert_equal( Vector3.new(-1,3,0), point_on_triangle )
274
- assert_equal( Vector3.new(-1,3,0), point_on_plane )
275
-
276
- # closeing point is outside of triangle
277
- plane = Plane.new(Vector3.new(-2,0,0), Vector3.new(-1,0,0))
278
- distance, intersect_line, point_on_triangle, point_on_plane = @triangle.distance(plane)
279
- assert_equal( 1, distance )
280
- assert_equal( nil, intersect_line)
281
- assert_equal( Vector3.new(-1,3,0), point_on_triangle )
282
- assert_equal( Vector3.new(-2,3,0), point_on_plane )
283
-
284
- # parallel to plane
285
- plane = Plane.new(Vector3.new(0,0,4), Vector3.new(0,0,1))
286
- distance, intersect_line, point_on_triangle, point_on_plane = @triangle_default.distance(plane)
287
- assert_equal( 4, distance )
288
- assert_equal( nil, intersect_line)
289
- assert_equal( nil, point_on_triangle )
290
- assert_equal( nil, point_on_plane )
291
-
292
- # parallel to edge
293
- plane = Plane.new(Vector3.new(3.5,0,3), Vector3.new(-1,0,0))
294
- distance, closest_line, point_on_triangle, point_on_plane = @triangle.distance(plane)
295
- assert_equal( 2.5, distance )
296
- point1 = Vector3.new(1,2,2)
297
- point2 = Vector3.new(1,4,2)
298
- assert( FiniteLine.new(point1, point2) == closest_line || FiniteLine.new(point2, point1) == closest_line)
299
- assert_equal( nil, point_on_triangle )
300
- assert_equal( nil, point_on_plane )
301
- end
302
-
303
- def test_angle
304
- triangle1 = Triangle.new( Vector3.new(0,0,1), Vector3.new(Math.sqrt(3.0),0,1), Vector3.new(0,1,1))
305
- triangle2 = Triangle.new( Vector3.new(1,0,0), Vector3.new(0,0,1), Vector3.new(1,1,1))
306
-
307
- assert_equal(nil, triangle1.angle(-1))
308
- assert_equal(nil, triangle1.angle(3))
309
-
310
- assert_in_delta(90.0*Math::PI/180.0, triangle1.angle(0), triangle1.tolerance)
311
- assert_in_delta(30.0*Math::PI/180.0, triangle1.angle(1), triangle1.tolerance)
312
- assert_in_delta(60.0*Math::PI/180.0, triangle1.angle(2), triangle1.tolerance)
313
-
314
- assert_in_delta(60.0*Math::PI/180.0, triangle2.angle(0), triangle1.tolerance)
315
- assert_in_delta(60.0*Math::PI/180.0, triangle2.angle(1), triangle1.tolerance)
316
- assert_in_delta(60.0*Math::PI/180.0, triangle2.angle(2), triangle1.tolerance)
317
- end
318
- end
1
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
2
+ require 'helper'
3
+
4
+ include GMath3D
5
+
6
+ MiniTest::Unit.autorun
7
+
8
+ class TriangleTestCase < MiniTest::Unit::TestCase
9
+ def setup
10
+ @triangle = Triangle.new( Vector3.new(1,2,2), Vector3.new(1,4,2), Vector3.new(-1,3,0))
11
+ @triangle_default = Triangle.new()
12
+ end
13
+
14
+ def test_initialize
15
+ assert_equal(Vector3.new(1,2,2), @triangle.vertices[0])
16
+ assert_equal(Vector3.new(1,4,2), @triangle.vertices[1])
17
+ assert_equal(Vector3.new(-1,3,0), @triangle.vertices[2])
18
+
19
+ assert_equal(Vector3.new(0,0,0), @triangle_default.vertices[0])
20
+ assert_equal(Vector3.new(1,0,0), @triangle_default.vertices[1])
21
+ assert_equal(Vector3.new(0,1,0), @triangle_default.vertices[2])
22
+ end
23
+
24
+ def test_equals
25
+ assert(@triangle != nil)
26
+ assert(@triangle != "string")
27
+
28
+ shallow_copied = @triangle
29
+ assert(@triangle.equal?(shallow_copied))
30
+ assert(@triangle == shallow_copied)
31
+
32
+ assert_equal(@triangle, Triangle.new( Vector3.new(1.0,2.0,2.0), Vector3.new(1.0,4.0,2.0), Vector3.new(-1.0,3.0,0.0)))
33
+ assert(@triangle == Triangle.new( Vector3.new(1.0,2.0,2.0), Vector3.new(1.0,4.0,2.0), Vector3.new(-1.0,3.0,0.0)))
34
+ assert(@triangle != Triangle.new( Vector3.new(1.0,2.0,2.0), Vector3.new(1.0,4.0,2.0), Vector3.new(-1.0,3.0,0.5)))
35
+ end
36
+
37
+ def test_clone
38
+ shallow_copied = @triangle
39
+ shallow_copied.vertices[2].x = 1
40
+ assert(@triangle == shallow_copied)
41
+ assert(@triangle.equal?(shallow_copied))
42
+ assert_equal(1, @triangle.vertices[2].x)
43
+ assert_equal(1, shallow_copied.vertices[2].x)
44
+
45
+ cloned =@triangle.clone
46
+ assert(@triangle == cloned)
47
+ assert(!@triangle.equal?(cloned))
48
+
49
+ cloned.vertices[2].x = -10
50
+ assert_equal(-10, cloned.vertices[2].x)
51
+ assert_equal(1, @triangle.vertices[2].x) # original never changed in editing cloned one.
52
+ end
53
+
54
+ def test_to_s
55
+ assert_equal("Triangle[[1, 2, 2], [1, 4, 2], [-1, 3, 0]]", @triangle.to_s)
56
+ end
57
+
58
+ def test_box
59
+ box1 = @triangle.box
60
+ box2 = @triangle_default.box
61
+ assert_equal(Vector3.new(-1,2,0), box1.min_point)
62
+ assert_equal(Vector3.new(1,4,2), box1.max_point)
63
+ assert_equal(Vector3.new(0,0,0) , box2.min_point)
64
+ assert_equal(Vector3.new(1,1,0) , box2.max_point)
65
+ end
66
+
67
+ def test_point
68
+ assert_equal(@triangle.vertices[0], @triangle.point( Array.new([1,0,0])))
69
+ assert_equal(@triangle.vertices[1], @triangle.point( Array.new([0,1,0])))
70
+ assert_equal(@triangle.vertices[2], @triangle.point( Array.new([0,0,1])))
71
+
72
+ #on edge
73
+ assert_equal(Vector3.new(1,3,2), @triangle.point( Array.new([0.5,0.5,0])))
74
+ assert_equal(Vector3.new(0,3.5,1), @triangle.point( Array.new([0,0.5,0.5])))
75
+ assert_equal(Vector3.new(0,2.5,1), @triangle.point( Array.new([0.5,0,0.5])))
76
+
77
+ #inside
78
+ assert_equal(Vector3.new(0, 3, 1), @triangle.point( Array.new([0.25,0.25,0.5])))
79
+ assert_equal(@triangle.center, @triangle.point( Array.new([1.0/3.0,1.0/3.0,1.0/3.0])))
80
+ end
81
+
82
+ def test_edges
83
+ edges = @triangle.edges
84
+ assert_equal(@triangle.vertices[0], edges[0].start_point)
85
+ assert_equal(@triangle.vertices[1], edges[0].end_point)
86
+ assert_equal(@triangle.vertices[1], edges[1].start_point)
87
+ assert_equal(@triangle.vertices[2], edges[1].end_point)
88
+ assert_equal(@triangle.vertices[2], edges[2].start_point)
89
+ assert_equal(@triangle.vertices[0], edges[2].end_point)
90
+ end
91
+
92
+ def test_area
93
+ assert_in_delta(Math::sqrt(8), @triangle.area, @triangle.tolerance)
94
+ assert_in_delta(0.5, @triangle_default.area, @triangle_default.tolerance)
95
+ end
96
+
97
+ def test_normal
98
+ assert_equal( Vector3.new(-1,0,1).normalize(), @triangle.normal )
99
+ assert_equal( Vector3.new(0,0,1) ,@triangle_default.normal )
100
+ end
101
+
102
+ def test_reverse
103
+ current_normal = @triangle.normal()
104
+ reversed_normal = @triangle.reverse().normal()
105
+ assert( current_normal.parallel?(reversed_normal) )
106
+ assert( !current_normal.same_direction?(reversed_normal) )
107
+ end
108
+
109
+ def test_center
110
+ center = @triangle.center
111
+ assert_in_delta( 0.333333333333333, center.x, @triangle.tolerance)
112
+ assert_in_delta( 3.0, center.y, @triangle.tolerance)
113
+ assert_in_delta( 1.333333333333333, center.z, @triangle.tolerance)
114
+
115
+ barycentric_coordinate = @triangle.barycentric_coordinate(center)
116
+ assert_in_delta( 1.0/3.0, barycentric_coordinate[0], @triangle.tolerance)
117
+ assert_in_delta( 1.0/3.0, barycentric_coordinate[1], @triangle.tolerance)
118
+ assert_in_delta( 1.0/3.0, barycentric_coordinate[2], @triangle.tolerance)
119
+ end
120
+
121
+ def test_barycentric_coordinate
122
+ # on vertex
123
+ coord = @triangle.barycentric_coordinate( @triangle.vertices[1])
124
+ assert_equal( 3, coord.size )
125
+ assert_in_delta( 0, coord[0], @triangle.tolerance)
126
+ assert_in_delta( 1, coord[1], @triangle.tolerance)
127
+ assert_in_delta( 0, coord[2], @triangle.tolerance)
128
+
129
+ # on edge
130
+ coord = @triangle.barycentric_coordinate( Vector3.new(1,3,2) )
131
+ assert_in_delta( 0.5, coord[0], @triangle.tolerance)
132
+ assert_in_delta( 0.5, coord[1], @triangle.tolerance)
133
+ assert_in_delta( 0, coord[2], @triangle.tolerance)
134
+
135
+ # inside case
136
+ coord = @triangle.barycentric_coordinate( Vector3.new(0,3,1) )
137
+ assert_in_delta( 1.0, coord[0] + coord[1] + coord[2], @triangle.tolerance)
138
+ assert_in_delta( 0.25, coord[0], @triangle.tolerance)
139
+ assert_in_delta( 0.25, coord[1], @triangle.tolerance)
140
+ assert_in_delta( 0.5, coord[2], @triangle.tolerance)
141
+
142
+ # outside case
143
+ coord = @triangle_default.barycentric_coordinate( Vector3.new(2,0,0) )
144
+ assert_in_delta( -1.0, coord[0], @triangle.tolerance)
145
+ assert_in_delta( 2.0, coord[1], @triangle.tolerance)
146
+ assert_in_delta( 0.0, coord[2], @triangle.tolerance)
147
+
148
+ # outside case
149
+ coord = @triangle.barycentric_coordinate( Vector3.new(3,3,4) )
150
+ assert_in_delta( 1.0, coord[0], @triangle.tolerance)
151
+ assert_in_delta( 1.0, coord[1], @triangle.tolerance)
152
+ assert_in_delta( -1.0, coord[2], @triangle.tolerance)
153
+
154
+ #invalid argument
155
+ assert_raises ArgumentError do
156
+ coord = @triangle.barycentric_coordinate( 3 )
157
+ end
158
+ assert_raises ArgumentError do
159
+ coord = @triangle.barycentric_coordinate( nil )
160
+ end
161
+ end
162
+
163
+ def test_contains
164
+ check_point = @triangle.center + @triangle.normal*3.0
165
+ assert( !@triangle.contains?( check_point) )
166
+ assert( @triangle.contains?( @triangle.center) )
167
+ assert( @triangle.contains?( Vector3.new( 1,3,2)))
168
+ assert( @triangle.contains?( Vector3.new(-1,3,0)))
169
+ assert( @triangle_default.contains?( Vector3.new( 0.5, 0.5, 0.0 ) ))
170
+ assert( !@triangle_default.contains?( Vector3.new( -1.0, 2.0, 0.0 )))
171
+ assert( !@triangle_default.contains?( Vector3.new( 1.0, 1.0, 0.0 )))
172
+ end
173
+
174
+ def test_distance_to_point
175
+ # on inside
176
+ check_point = @triangle.center + @triangle.normal*3.0
177
+ distance, point_on_triangle = @triangle.distance(check_point)
178
+ assert_in_delta(3.0, distance, @triangle.tolerance)
179
+ assert_equal(@triangle.center, point_on_triangle)
180
+
181
+ # on vertex
182
+ distance, point_on_triangle = @triangle_default.distance(Vector3.new(-1,-1,0.5))
183
+ assert_in_delta(Math::sqrt(2.25), distance, @triangle_default.tolerance)
184
+ assert_equal(@triangle_default.vertices[0], point_on_triangle)
185
+
186
+ # on edge
187
+ distance, point_on_triangle = @triangle.distance( Vector3.new(2,3,2))
188
+ assert_in_delta(1, distance, @triangle_default.tolerance)
189
+ assert_equal(Vector3.new(1,3,2), point_on_triangle)
190
+
191
+ # contains to inside
192
+ distance, point_on_triangle = @triangle_default.distance(@triangle_default.center)
193
+ assert_in_delta(0, distance, @triangle_default.tolerance)
194
+ assert_equal(@triangle_default.center, point_on_triangle)
195
+ end
196
+
197
+ def test_distance_to_line
198
+ # on inside
199
+ line = Line.new(Vector3.new(0,3,-1), Vector3.new(0,0,4))
200
+ distance, point_on_triangle, point_on_line, param_on_line = @triangle.distance(line)
201
+ assert_equal(0, distance)
202
+ assert_equal(Vector3.new(0,3,1), point_on_triangle)
203
+ assert_equal(Vector3.new(0,3,1), point_on_line)
204
+ assert_in_delta(0.5, param_on_line, line.tolerance)
205
+
206
+ # on edge
207
+ line = Line.new(Vector3.new(1,3,-1), Vector3.new(0,0,4))
208
+ distance, point_on_triangle, point_on_line, param_on_line = @triangle.distance(line)
209
+ assert_equal(0, distance)
210
+ assert_equal(Vector3.new(1,3,2), point_on_triangle)
211
+ assert_equal(Vector3.new(1,3,2), point_on_line)
212
+ assert_in_delta(0.75, param_on_line, line.tolerance)
213
+
214
+ # on vertex
215
+ line = Line.new(Vector3.new(-1,3,0), Vector3.new(1,1,2))
216
+ distance, point_on_triangle, point_on_line, param_on_line = @triangle.distance(line)
217
+ assert_equal(0, distance)
218
+ assert_equal(Vector3.new(-1,3,0), point_on_triangle)
219
+ assert_equal(Vector3.new(-1,3,0), point_on_line)
220
+ assert_in_delta(0.0, param_on_line, line.tolerance)
221
+
222
+ # closest point is out of triangle1
223
+ line = Line.new(Vector3.new(2,3,0), Vector3.new(0,0,4))
224
+ distance, point_on_triangle, point_on_line, param_on_line = @triangle.distance(line)
225
+ assert_equal(1, distance)
226
+ assert_equal(Vector3.new(1,3,2), point_on_triangle)
227
+ assert_equal(Vector3.new(2,3,2), point_on_line)
228
+ assert_in_delta(0.5, param_on_line, line.tolerance)
229
+
230
+ # closest point is out of triangle2
231
+ line = Line.new(Vector3.new(-2,3,-1), Vector3.new(0,0,1))
232
+ distance, point_on_triangle, point_on_line, param_on_line = @triangle.distance(line)
233
+ assert_equal(1, distance)
234
+ assert_equal(Vector3.new(-1,3,0), point_on_triangle)
235
+ assert_equal(Vector3.new(-2,3,0), point_on_line)
236
+ assert_in_delta(1, param_on_line, line.tolerance)
237
+
238
+ # parallel case
239
+ line = Line.new(Vector3.new(1,0,4), Vector3.new(0,6,0))
240
+ distance, point_on_triangle, point_on_line, param_on_line = @triangle.distance(line)
241
+ assert_equal(2, distance)
242
+ assert_equal(nil, point_on_triangle)
243
+ assert_equal(nil, point_on_line)
244
+ assert_equal(nil, param_on_line)
245
+ end
246
+
247
+ def test_distance_to_plane
248
+ # intersect case
249
+ plane = Plane.new(Vector3.new(0,0,1), Vector3.new(1,0,0))
250
+ distance, intersect_line, point_on_triangle, point_on_plane = @triangle.distance(plane)
251
+ assert_equal( 0, distance )
252
+ point1 = Vector3.new(0,2.5,1)
253
+ point2 = Vector3.new(0,3.5,1)
254
+ assert( FiniteLine.new(point1, point2) == intersect_line || FiniteLine.new(point2, point1) == intersect_line)
255
+ assert_equal( nil, point_on_triangle )
256
+ assert_equal( nil, point_on_plane )
257
+
258
+ # contains edge
259
+ plane = Plane.new(Vector3.new(1,0,1), Vector3.new(-1,0,0))
260
+ distance, intersect_line, point_on_triangle, point_on_plane = @triangle.distance(plane)
261
+ assert_equal( 0, distance )
262
+ point1 = Vector3.new(1,2,2)
263
+ point2 = Vector3.new(1,4,2)
264
+ assert( FiniteLine.new(point1, point2) == intersect_line || FiniteLine.new(point2, point1) == intersect_line)
265
+ assert_equal( nil, point_on_triangle )
266
+ assert_equal( nil, point_on_plane )
267
+
268
+ # contains vertex
269
+ plane = Plane.new(Vector3.new(-1,0,1), Vector3.new(1,0,0))
270
+ distance, intersect_line, point_on_triangle, point_on_plane = @triangle.distance(plane)
271
+ assert_equal( 0, distance )
272
+ assert_equal( nil, intersect_line)
273
+ assert_equal( Vector3.new(-1,3,0), point_on_triangle )
274
+ assert_equal( Vector3.new(-1,3,0), point_on_plane )
275
+
276
+ # closeing point is outside of triangle
277
+ plane = Plane.new(Vector3.new(-2,0,0), Vector3.new(-1,0,0))
278
+ distance, intersect_line, point_on_triangle, point_on_plane = @triangle.distance(plane)
279
+ assert_equal( 1, distance )
280
+ assert_equal( nil, intersect_line)
281
+ assert_equal( Vector3.new(-1,3,0), point_on_triangle )
282
+ assert_equal( Vector3.new(-2,3,0), point_on_plane )
283
+
284
+ # parallel to plane
285
+ plane = Plane.new(Vector3.new(0,0,4), Vector3.new(0,0,1))
286
+ distance, intersect_line, point_on_triangle, point_on_plane = @triangle_default.distance(plane)
287
+ assert_equal( 4, distance )
288
+ assert_equal( nil, intersect_line)
289
+ assert_equal( nil, point_on_triangle )
290
+ assert_equal( nil, point_on_plane )
291
+
292
+ # parallel to edge
293
+ plane = Plane.new(Vector3.new(3.5,0,3), Vector3.new(-1,0,0))
294
+ distance, closest_line, point_on_triangle, point_on_plane = @triangle.distance(plane)
295
+ assert_equal( 2.5, distance )
296
+ point1 = Vector3.new(1,2,2)
297
+ point2 = Vector3.new(1,4,2)
298
+ assert( FiniteLine.new(point1, point2) == closest_line || FiniteLine.new(point2, point1) == closest_line)
299
+ assert_equal( nil, point_on_triangle )
300
+ assert_equal( nil, point_on_plane )
301
+ end
302
+
303
+ def test_angle
304
+ triangle1 = Triangle.new( Vector3.new(0,0,1), Vector3.new(Math.sqrt(3.0),0,1), Vector3.new(0,1,1))
305
+ triangle2 = Triangle.new( Vector3.new(1,0,0), Vector3.new(0,0,1), Vector3.new(1,1,1))
306
+
307
+ assert_equal(nil, triangle1.angle(-1))
308
+ assert_equal(nil, triangle1.angle(3))
309
+
310
+ assert_in_delta(90.0*Math::PI/180.0, triangle1.angle(0), triangle1.tolerance)
311
+ assert_in_delta(30.0*Math::PI/180.0, triangle1.angle(1), triangle1.tolerance)
312
+ assert_in_delta(60.0*Math::PI/180.0, triangle1.angle(2), triangle1.tolerance)
313
+
314
+ assert_in_delta(60.0*Math::PI/180.0, triangle2.angle(0), triangle1.tolerance)
315
+ assert_in_delta(60.0*Math::PI/180.0, triangle2.angle(1), triangle1.tolerance)
316
+ assert_in_delta(60.0*Math::PI/180.0, triangle2.angle(2), triangle1.tolerance)
317
+ end
318
+ end