mageo 0.0.0 → 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -6,253 +6,252 @@ require "mageo/triangle.rb"
6
6
  require "mageo/vector3d.rb"
7
7
 
8
8
  class Triangle
9
- public :internal_axes
9
+ public :internal_axes
10
10
  end
11
11
 
12
12
  class TC_Triangle < Test::Unit::TestCase
13
- $tolerance = 10.0 ** (-10)
14
-
15
- VEC_O = Vector3D[0.0, 0.0, 0.0]
16
- VEC_X = Vector3D[1.0, 0.0, 0.0]
17
- VEC_Y = Vector3D[0.0, 1.0, 0.0]
18
- VEC_Z = Vector3D[0.0, 0.0, 1.0]
19
-
20
- def setup
21
- @t00 = Triangle.new([VEC_O, VEC_X, VEC_Y])
22
- @t01 = Triangle.new([VEC_X, VEC_Y, VEC_Z])
23
- @t02 = Triangle.new([[10.0,10.0,10.0], [20.0,10.0,10.0], [10.0,20.0,10.0]])
24
- @t03 = Triangle.new([[10.0,20.0,30.0], [ 0.0,20.0,30.0], [10.0, 0.0,30.0]])
25
- end
26
-
27
- def test_initialize
28
- assert_raise( ArgumentError ){ Triangle.new }
29
- assert_raise( ArgumentError ){ Triangle.new() }
30
- assert_raise( Triangle::InitializeError ){ Triangle.new( nil ) }
31
- assert_raise( Triangle::InitializeError ){ Triangle.new( [] ) }
32
- assert_raise( Triangle::InitializeError ){ Triangle.new( [ 0, 1 ] ) }
33
- assert_raise( Triangle::InitializeError ){ Triangle.new( [ 0, 1, 2 ] ) }
34
- assert_raise( Triangle::InitializeError ){ Triangle.new( [ 0, 1, 2, 3 ] ) }
35
- assert_raise( Triangle::InitializeError ){ Triangle.new( [ [ 0, 0, 0 ], [ 1, 1, 1 ], [ 2, 3 ] ] ) } #3次元座標になっていないものがある。
36
-
37
- assert_raise( Triangle::LinearException ){ Triangle.new( [ [ 0, 0, 0 ], [ 0, 0, 0 ], [ 2, 2, 2 ] ] ) } #同一の点を含む。
38
- assert_raise( Triangle::LinearException ){ Triangle.new( [ [ 0, 0, 0 ], [ 1, 1, 1 ], [ 2, 2, 2 ] ] ) } #直線上に並ぶ
39
-
40
- assert_equal( Triangle, Triangle.new( [ Vector3D[ 0, 0, 0 ], Vector3D[ 1, 0, 0 ], Vector3D[ 0, 1, 0 ] ] ).class )
41
- end
42
-
43
- def test_vertices
44
- assert_equal( Array, @t00.vertices.class )
45
- assert_equal( 3, @t00.vertices.size )
46
- assert_equal( Vector3D[0, 0, 0], @t00.vertices[0] )
47
- assert_equal( Vector3D[1, 0, 0], @t00.vertices[1] )
48
- assert_equal( Vector3D[0, 1, 0], @t00.vertices[2] )
49
- end
50
-
51
- def test_same_side?
52
- assert_raise(Triangle::TypeError){ @t00.same_side?( [ 2, 3, 7 ], [ -2, -3, 2 ])}
53
-
54
- assert_equal( true , @t00.same_side?( Vector3D[ 2, 3, 7 ], Vector3D[ -2, -3, 2 ] ) )
55
- assert_equal( true , @t00.same_side?( Vector3D[ 2, 3, -7 ], Vector3D[ -2, -3, -2 ] ) )
56
- assert_equal( false, @t00.same_side?( Vector3D[ 2, 3, 7 ], Vector3D[ -2, -3, -2 ] ) )
57
- assert_equal( false, @t00.same_side?( Vector3D[ 2, 3, -7 ], Vector3D[ -2, -3, 2 ] ) )
58
- assert_equal( false, @t00.same_side?( Vector3D[ 1, 2, 0 ], Vector3D[ 10, 10, 10 ] ) ) #pos0 が面上の点
59
- assert_equal( false, @t00.same_side?( Vector3D[10, 10,10 ], Vector3D[ 1, 2, 0 ] ) ) #pos1 が面上の点
60
- assert_equal( false, @t00.same_side?( Vector3D[10, 10, 0 ], Vector3D[ 1, 2, 0 ] ) ) #両方 が面上の点
61
-
62
- assert_equal( true , @t01.same_side?( Vector3D[ 10, 10, 10 ], Vector3D[ 20, 30, 40 ] ) )
63
- assert_equal( true , @t01.same_side?( Vector3D[ 0, 0, 0 ], Vector3D[ -10, -10, -10 ] ) )
64
- assert_equal( false, @t01.same_side?( Vector3D[ 10, 10, 10 ], Vector3D[ -10, -10, -10 ] ) )
65
- assert_equal( false, @t01.same_side?( Vector3D[ 0, 0, 0 ], Vector3D[ 20, 30, 40 ] ) )
66
- assert_equal( false, @t01.same_side?( Vector3D[ 0.5, 0.5, 0 ], Vector3D[ 10, 10, 10 ] ) ) #pos0 が面上の点
67
- assert_equal( false, @t01.same_side?( Vector3D[ 10, 10, 10 ], Vector3D[ 0.5, 0.5, 0 ] ) ) #pos1 が面上の点
68
- assert_equal( false, @t01.same_side?( Vector3D[ 0.5, 0.5, 0 ], Vector3D[ 0, 0.5, 0.5 ] ) ) #両方 が面上の点
69
- end
70
-
71
- def test_include?
72
- assert_raise(Triangle::TypeError){ @t00.include?([11.0, 11.0, 10.0], $tolerance)}
73
-
74
- # on face
75
- assert_equal(true, @t02.include?(Vector3D[11.0, 11.0, 10.0], $tolerance))
76
-
77
- # on vertices
78
- assert_equal(true, @t02.include?(Vector3D[10.0, 10.0, 10.0], $tolerance))
79
- assert_equal(true, @t02.include?(Vector3D[20.0, 10.0, 10.0], $tolerance))
80
- assert_equal(true, @t02.include?(Vector3D[10.0, 20.0, 10.0], $tolerance))
81
-
82
- # on edge
83
- assert_equal(true, @t02.include?(Vector3D[15.0, 10.0, 10.0], $tolerance))
84
- assert_equal(true, @t02.include?(Vector3D[10.0, 15.0, 10.0], $tolerance))
85
- assert_equal(true, @t02.include?(Vector3D[15.0, 15.0, 10.0], $tolerance))
86
-
87
- # out
88
- assert_equal(false, @t02.include?(Vector3D[ 30.0, 10.0, 10.0], $tolerance))
89
- assert_equal(false, @t02.include?(Vector3D[ 10.0, 30.0, 10.0], $tolerance))
90
- assert_equal(false, @t02.include?(Vector3D[-10.0, 10.0, 10.0], $tolerance))
91
- assert_equal(false, @t02.include?(Vector3D[ 10.0,-10.0, 10.0], $tolerance))
92
- assert_equal(false, @t02.include?(Vector3D[ 10.0, 10.0, 0.0], $tolerance))
93
-
94
- assert_equal(false, @t03.include?(Vector3D[ 3.0, 6.0, 30.0], $tolerance))
95
-
96
- # 計算誤差
97
- assert_equal(false, @t01.include?(Vector3D[ 0.3, 0.3, 0.3], $tolerance))
98
- assert_equal(true, @t01.include?(Vector3D[ 0.3, 0.3, 0.3], 1.0))
99
- end
100
-
101
- def test_intersection
102
- # 平行
103
- pos0 = Vector3D[0.0, 0.0, 2.0]
104
- pos1 = Vector3D[2.0, 0.0, 2.0]
105
- seg01 = Segment.new(pos0, pos1)
106
- assert_raise(Triangle::NoIntersectionError){ @t00.intersection(seg01, $tolerance) }
107
-
108
- # 面に含まれる
109
- pos0 = Vector3D[0.0, 0.0, 0.0]
110
- pos1 = Vector3D[2.0, 0.0, 0.0]
111
- seg01 = Segment.new(pos0, pos1)
112
- assert_raise(Triangle::NoIntersectionError){ @t00.intersection(seg01, $tolerance) }
113
-
114
- # 平行ではないが、三角形の外を通過。
115
- pos2 = Vector3D[0.0,10.0, 0.0]
116
- pos3 = Vector3D[0.0,10.0, 2.0]
117
- seg01 = Segment.new(pos0, pos1)
118
- assert_raise(Triangle::NoIntersectionError){ @t00.intersection(seg01, $tolerance) }
119
-
120
- # 三角形を通る
121
- pos2 = Vector3D[0.5, 0.5,-1.0]
122
- pos3 = Vector3D[0.5, 0.5, 2.0]
123
- seg01 = Segment.new(pos2, pos3)
124
- assert_equal(Vector3D[0.5, 0.5, 0.0], @t00.intersection(seg01, $tolerance))
125
- #
126
- pos2 = Vector3D[0.5, 0.5, 0.0]
127
- pos3 = Vector3D[0.5, 0.5, 1.0]
128
- seg01 = Segment.new(pos2, pos3)
129
- assert_equal(Vector3D[0.5, 0.5, 0.0], @t00.intersection(seg01, $tolerance))
130
- #
131
- pos2 = Vector3D[ 1.5, 1.5, 1.0]
132
- pos3 = Vector3D[-0.5,-0.5,-1.0]
133
- seg01 = Segment.new(pos2, pos3)
134
- assert_equal(Vector3D[0.5, 0.5, 0.0], @t00.intersection(seg01, $tolerance))
135
- #
136
- pos2 = Vector3D[ 0.00, 0.00, 0.00]
137
- pos3 = Vector3D[ 1.00, 1.00, 1.00]
138
- seg01 = Segment.new(pos2, pos3)
139
- t = @t01.intersection(seg01, $tolerance)
140
- assert_in_delta(1.0/3.0, t[0], $tolerance)
141
- assert_in_delta(1.0/3.0, t[1], $tolerance)
142
- assert_in_delta(1.0/3.0, t[2], $tolerance)
143
- #
144
- pos2 = Vector3D[ 0.25, 0.25, 0.00]
145
- pos3 = Vector3D[ 0.25, 0.25, 1.00]
146
- seg01 = Segment.new(pos2, pos3)
147
- assert_equal(Vector3D[0.25, 0.25, 0.50], @t01.intersection(seg01, $tolerance))
148
- end
149
-
150
- #def test_intersect?
151
- # TODO
152
- #end
153
-
154
- def test_parallel_segment?
155
- # 平行
156
- pos0 = Vector3D[0.0, 0.0, 2.0]
157
- pos1 = Vector3D[2.0, 0.0, 2.0]
158
- seg01 = Segment.new(pos0, pos1)
159
- assert_equal(true , @t00.parallel_segment?(seg01))
160
-
161
- # 面に含まれる
162
- pos0 = Vector3D[0.0, 0.0, 0.0]
163
- pos1 = Vector3D[2.0, 0.0, 0.0]
164
- seg01 = Segment.new(pos0, pos1)
165
- assert_equal(false, @t00.parallel_segment?(seg01))
166
-
167
- # 平行ではない。
168
- pos2 = Vector3D[0.0,10.0, 0.0]
169
- pos3 = Vector3D[0.0,10.0, 2.0]
170
- seg01 = Segment.new(pos0, pos1)
171
- assert_equal(false, @t00.parallel_segment?(seg01))
172
- end
173
-
174
- def test_normal_vector
175
- t = Vector3D[0.0, 0.0, 1.0]
176
- assert_equal(t, @t00.normal_vector)
177
-
178
- t = Vector3D[1.0, 1.0, 1.0]
179
- t = t * (1.0/t.r)
180
- assert_equal(t, @t01.normal_vector)
181
-
182
- t = Vector3D[0.0, 0.0, 1.0]
183
- assert_equal(t, @t02.normal_vector)
184
- end
185
-
186
- def test_equivalent?
187
- assert_raise(Triangle::TypeError){
188
- @t00.equivalent?([[ 0.0, 0.0, 0.0], [ 1.0, 0.0, 0.0], [ 0.0, 1.0, 0.0]])
189
- }
190
-
191
- t = Triangle.new([[ 0.0, 0.0, 0.0], [ 1.0, 0.0, 0.0], [ 0.0, 1.0, 0.0]])
192
- assert_equal(true , @t00.eql?(t))
193
-
194
- t = Triangle.new([[ 1.0, 0.0, 0.0], [ 0.0, 0.0, 0.0], [ 0.0, 1.0, 0.0]])
195
- assert_equal(true , @t00.eql?(t))
196
-
197
- t = Triangle.new([[ 1.0, 0.0, 0.0], [ 0.0, 1.0, 0.0], [ 0.0, 0.0, 0.0]])
198
- assert_equal(true , @t00.eql?(t))
199
-
200
- t = Triangle.new([[ 0.0, 0.0, 1.0], [ 1.0, 0.0, 0.0], [ 0.0, 1.0, 0.0]])
201
- assert_equal(false, @t00.eql?(t))
202
-
203
- # tolerance を設定の上 0.0
204
- t = Triangle.new([[ 0.0, 0.0, 1.0], [ 1.0, 0.0, 0.0], [ 0.0, 1.0, 0.0]])
205
- assert_equal(false, @t00.eql?(t, 0.0))
206
-
207
- # tolerance を 1.0 に設定
208
- t = Triangle.new([[ 0.0, 0.0, 1.0], [ 1.0, 0.0, 0.0], [ 0.0, 1.0, 0.0]])
209
- assert_equal(true, @t00.eql?(t, 1.0))
210
- end
211
-
212
- def test_equal2
213
- t = Triangle.new([[ 0.0, 0.0, 0.0], [ 1.0, 0.0, 0.0], [ 0.0, 1.0, 0.0]])
214
- assert_equal(true , @t00 == t)
215
-
216
- t = Triangle.new([[ 1.0, 0.0, 0.0], [ 0.0, 0.0, 0.0], [ 0.0, 1.0, 0.0]])
217
- assert_equal(false, @t00 == t)
218
-
219
- t = Triangle.new([[ 1.0, 0.0, 0.0], [ 0.0, 1.0, 0.0], [ 0.0, 0.0, 0.0]])
220
- assert_equal(false, @t00 == t)
221
-
222
- t = Triangle.new([[ 0.0, 0.0, 1.0], [ 1.0, 0.0, 0.0], [ 0.0, 1.0, 0.0]])
223
- assert_equal(false, @t00 == t)
224
- end
225
-
226
- def test_internal_axes
227
- @t01 = Triangle.new([[ 1.0, 0.0, 0.0], [ 0.0, 1.0, 0.0], [ 0.0, 0.0, 1.0]])
228
- @t02 = Triangle.new([[10.0,10.0,10.0], [20.0,10.0,10.0], [10.0,20.0,10.0]])
229
-
230
- t = Axes.new([[ 1.0, 0.0, 0.0], [ 0.0, 1.0, 0.0], [ 0.0, 0.0, 1.0]])
231
- assert_equal(t, @t00.internal_axes)
232
-
233
- t = @t01.internal_axes
234
- assert_in_delta(-1.0, t[0][0] , $tolerance)
235
- assert_in_delta( 1.0, t[0][1] , $tolerance)
236
- assert_in_delta( 0.0, t[0][2] , $tolerance)
237
- assert_in_delta(-1.0, t[1][0] , $tolerance)
238
- assert_in_delta( 0.0, t[1][1] , $tolerance)
239
- assert_in_delta( 1.0, t[1][2] , $tolerance)
240
- assert_in_delta(1.0/Math.sqrt(3.0), t[2][0], $tolerance)
241
- assert_in_delta(1.0/Math.sqrt(3.0), t[2][1], $tolerance)
242
- assert_in_delta(1.0/Math.sqrt(3.0), t[2][2], $tolerance)
243
-
244
- t = Axes.new([[ 10.0, 0.0, 0.0], [ 0.0, 10.0, 0.0], [ 0.0, 0.0, 1.0]])
245
- assert_equal(t, @t02.internal_axes)
246
- end
247
-
248
- def test_edges
249
- t = @t00.edges
250
- assert_equal(3, t.size)
251
-
252
- assert_equal(Segment.new(VEC_O, VEC_X), t[0])
253
- assert_equal(Segment.new(VEC_X, VEC_Y), t[1])
254
- assert_equal(Segment.new(VEC_Y, VEC_O), t[2])
255
- end
256
-
13
+ $tolerance = 10.0 ** (-10)
14
+
15
+ VEC_O = Vector3D[0.0, 0.0, 0.0]
16
+ VEC_X = Vector3D[1.0, 0.0, 0.0]
17
+ VEC_Y = Vector3D[0.0, 1.0, 0.0]
18
+ VEC_Z = Vector3D[0.0, 0.0, 1.0]
19
+
20
+ def setup
21
+ @t00 = Triangle.new([VEC_O, VEC_X, VEC_Y])
22
+ @t01 = Triangle.new([VEC_X, VEC_Y, VEC_Z])
23
+ @t02 = Triangle.new([[10.0,10.0,10.0], [20.0,10.0,10.0], [10.0,20.0,10.0]])
24
+ @t03 = Triangle.new([[10.0,20.0,30.0], [ 0.0,20.0,30.0], [10.0, 0.0,30.0]])
25
+ end
26
+
27
+ def test_initialize
28
+ assert_raise( ArgumentError ){ Triangle.new }
29
+ assert_raise( ArgumentError ){ Triangle.new() }
30
+ assert_raise( Triangle::InitializeError ){ Triangle.new( nil ) }
31
+ assert_raise( Triangle::InitializeError ){ Triangle.new( [] ) }
32
+ assert_raise( Triangle::InitializeError ){ Triangle.new( [ 0, 1 ] ) }
33
+ assert_raise( Triangle::InitializeError ){ Triangle.new( [ 0, 1, 2 ] ) }
34
+ assert_raise( Triangle::InitializeError ){ Triangle.new( [ 0, 1, 2, 3 ] ) }
35
+ assert_raise( Triangle::InitializeError ){ Triangle.new( [ [ 0, 0, 0 ], [ 1, 1, 1 ], [ 2, 3 ] ] ) } #3次元座標になっていないものがある。
36
+
37
+ assert_raise( Triangle::LinearException ){ Triangle.new( [ [ 0, 0, 0 ], [ 0, 0, 0 ], [ 2, 2, 2 ] ] ) } #同一の点を含む。
38
+ assert_raise( Triangle::LinearException ){ Triangle.new( [ [ 0, 0, 0 ], [ 1, 1, 1 ], [ 2, 2, 2 ] ] ) } #直線上に並ぶ
39
+
40
+ assert_equal( Triangle, Triangle.new( [ Vector3D[ 0, 0, 0 ], Vector3D[ 1, 0, 0 ], Vector3D[ 0, 1, 0 ] ] ).class )
41
+ end
42
+
43
+ def test_vertices
44
+ assert_equal( Array, @t00.vertices.class )
45
+ assert_equal( 3, @t00.vertices.size )
46
+ assert_equal( Vector3D[0, 0, 0], @t00.vertices[0] )
47
+ assert_equal( Vector3D[1, 0, 0], @t00.vertices[1] )
48
+ assert_equal( Vector3D[0, 1, 0], @t00.vertices[2] )
49
+ end
50
+
51
+ def test_same_side?
52
+ assert_raise(Triangle::TypeError){ @t00.same_side?( [ 2, 3, 7 ], [ -2, -3, 2 ])}
53
+
54
+ assert_equal( true , @t00.same_side?( Vector3D[ 2, 3, 7 ], Vector3D[ -2, -3, 2 ] ) )
55
+ assert_equal( true , @t00.same_side?( Vector3D[ 2, 3, -7 ], Vector3D[ -2, -3, -2 ] ) )
56
+ assert_equal( false, @t00.same_side?( Vector3D[ 2, 3, 7 ], Vector3D[ -2, -3, -2 ] ) )
57
+ assert_equal( false, @t00.same_side?( Vector3D[ 2, 3, -7 ], Vector3D[ -2, -3, 2 ] ) )
58
+ assert_equal( false, @t00.same_side?( Vector3D[ 1, 2, 0 ], Vector3D[ 10, 10, 10 ] ) ) #pos0 が面上の点
59
+ assert_equal( false, @t00.same_side?( Vector3D[10, 10,10 ], Vector3D[ 1, 2, 0 ] ) ) #pos1 が面上の点
60
+ assert_equal( false, @t00.same_side?( Vector3D[10, 10, 0 ], Vector3D[ 1, 2, 0 ] ) ) #両方 が面上の点
61
+
62
+ assert_equal( true , @t01.same_side?( Vector3D[ 10, 10, 10 ], Vector3D[ 20, 30, 40 ] ) )
63
+ assert_equal( true , @t01.same_side?( Vector3D[ 0, 0, 0 ], Vector3D[ -10, -10, -10 ] ) )
64
+ assert_equal( false, @t01.same_side?( Vector3D[ 10, 10, 10 ], Vector3D[ -10, -10, -10 ] ) )
65
+ assert_equal( false, @t01.same_side?( Vector3D[ 0, 0, 0 ], Vector3D[ 20, 30, 40 ] ) )
66
+ assert_equal( false, @t01.same_side?( Vector3D[ 0.5, 0.5, 0 ], Vector3D[ 10, 10, 10 ] ) ) #pos0 が面上の点
67
+ assert_equal( false, @t01.same_side?( Vector3D[ 10, 10, 10 ], Vector3D[ 0.5, 0.5, 0 ] ) ) #pos1 が面上の点
68
+ assert_equal( false, @t01.same_side?( Vector3D[ 0.5, 0.5, 0 ], Vector3D[ 0, 0.5, 0.5 ] ) ) #両方 が面上の点
69
+ end
70
+
71
+ def test_include?
72
+ assert_raise(Triangle::TypeError){ @t00.include?([11.0, 11.0, 10.0], $tolerance)}
73
+
74
+ # on face
75
+ assert_equal(true, @t02.include?(Vector3D[11.0, 11.0, 10.0], $tolerance))
76
+
77
+ # on vertices
78
+ assert_equal(true, @t02.include?(Vector3D[10.0, 10.0, 10.0], $tolerance))
79
+ assert_equal(true, @t02.include?(Vector3D[20.0, 10.0, 10.0], $tolerance))
80
+ assert_equal(true, @t02.include?(Vector3D[10.0, 20.0, 10.0], $tolerance))
81
+
82
+ # on edge
83
+ assert_equal(true, @t02.include?(Vector3D[15.0, 10.0, 10.0], $tolerance))
84
+ assert_equal(true, @t02.include?(Vector3D[10.0, 15.0, 10.0], $tolerance))
85
+ assert_equal(true, @t02.include?(Vector3D[15.0, 15.0, 10.0], $tolerance))
86
+
87
+ # out
88
+ assert_equal(false, @t02.include?(Vector3D[ 30.0, 10.0, 10.0], $tolerance))
89
+ assert_equal(false, @t02.include?(Vector3D[ 10.0, 30.0, 10.0], $tolerance))
90
+ assert_equal(false, @t02.include?(Vector3D[-10.0, 10.0, 10.0], $tolerance))
91
+ assert_equal(false, @t02.include?(Vector3D[ 10.0,-10.0, 10.0], $tolerance))
92
+ assert_equal(false, @t02.include?(Vector3D[ 10.0, 10.0, 0.0], $tolerance))
93
+
94
+ assert_equal(false, @t03.include?(Vector3D[ 3.0, 6.0, 30.0], $tolerance))
95
+
96
+ # 計算誤差
97
+ assert_equal(false, @t01.include?(Vector3D[ 0.3, 0.3, 0.3], $tolerance))
98
+ assert_equal(true, @t01.include?(Vector3D[ 0.3, 0.3, 0.3], 1.0))
99
+ end
100
+
101
+ def test_intersection
102
+ # 平行
103
+ pos0 = Vector3D[0.0, 0.0, 2.0]
104
+ pos1 = Vector3D[2.0, 0.0, 2.0]
105
+ seg01 = Segment.new(pos0, pos1)
106
+ assert_raise(Triangle::NoIntersectionError){ @t00.intersection(seg01, $tolerance) }
107
+
108
+ # 面に含まれる
109
+ pos0 = Vector3D[0.0, 0.0, 0.0]
110
+ pos1 = Vector3D[2.0, 0.0, 0.0]
111
+ seg01 = Segment.new(pos0, pos1)
112
+ assert_raise(Triangle::NoIntersectionError){ @t00.intersection(seg01, $tolerance) }
113
+
114
+ # 平行ではないが、三角形の外を通過。
115
+ pos2 = Vector3D[0.0,10.0, 0.0]
116
+ pos3 = Vector3D[0.0,10.0, 2.0]
117
+ seg01 = Segment.new(pos0, pos1)
118
+ assert_raise(Triangle::NoIntersectionError){ @t00.intersection(seg01, $tolerance) }
119
+
120
+ # 三角形を通る
121
+ pos2 = Vector3D[0.5, 0.5,-1.0]
122
+ pos3 = Vector3D[0.5, 0.5, 2.0]
123
+ seg01 = Segment.new(pos2, pos3)
124
+ assert_equal(Vector3D[0.5, 0.5, 0.0], @t00.intersection(seg01, $tolerance))
125
+ #
126
+ pos2 = Vector3D[0.5, 0.5, 0.0]
127
+ pos3 = Vector3D[0.5, 0.5, 1.0]
128
+ seg01 = Segment.new(pos2, pos3)
129
+ assert_equal(Vector3D[0.5, 0.5, 0.0], @t00.intersection(seg01, $tolerance))
130
+ #
131
+ pos2 = Vector3D[ 1.5, 1.5, 1.0]
132
+ pos3 = Vector3D[-0.5,-0.5,-1.0]
133
+ seg01 = Segment.new(pos2, pos3)
134
+ assert_equal(Vector3D[0.5, 0.5, 0.0], @t00.intersection(seg01, $tolerance))
135
+ #
136
+ pos2 = Vector3D[ 0.00, 0.00, 0.00]
137
+ pos3 = Vector3D[ 1.00, 1.00, 1.00]
138
+ seg01 = Segment.new(pos2, pos3)
139
+ t = @t01.intersection(seg01, $tolerance)
140
+ assert_in_delta(1.0/3.0, t[0], $tolerance)
141
+ assert_in_delta(1.0/3.0, t[1], $tolerance)
142
+ assert_in_delta(1.0/3.0, t[2], $tolerance)
143
+ #
144
+ pos2 = Vector3D[ 0.25, 0.25, 0.00]
145
+ pos3 = Vector3D[ 0.25, 0.25, 1.00]
146
+ seg01 = Segment.new(pos2, pos3)
147
+ assert_equal(Vector3D[0.25, 0.25, 0.50], @t01.intersection(seg01, $tolerance))
148
+ end
149
+
150
+ #def test_intersect?
151
+ # TODO
152
+ #end
153
+
154
+ def test_parallel_segment?
155
+ # 平行
156
+ pos0 = Vector3D[0.0, 0.0, 2.0]
157
+ pos1 = Vector3D[2.0, 0.0, 2.0]
158
+ seg01 = Segment.new(pos0, pos1)
159
+ assert_equal(true , @t00.parallel_segment?(seg01))
160
+
161
+ # 面に含まれる
162
+ pos0 = Vector3D[0.0, 0.0, 0.0]
163
+ pos1 = Vector3D[2.0, 0.0, 0.0]
164
+ seg01 = Segment.new(pos0, pos1)
165
+ assert_equal(false, @t00.parallel_segment?(seg01))
166
+
167
+ # 平行ではない。
168
+ pos2 = Vector3D[0.0,10.0, 0.0]
169
+ pos3 = Vector3D[0.0,10.0, 2.0]
170
+ seg01 = Segment.new(pos0, pos1)
171
+ assert_equal(false, @t00.parallel_segment?(seg01))
172
+ end
173
+
174
+ def test_normal_vector
175
+ t = Vector3D[0.0, 0.0, 1.0]
176
+ assert_equal(t, @t00.normal_vector)
177
+
178
+ t = Vector3D[1.0, 1.0, 1.0]
179
+ t = t * (1.0/t.r)
180
+ assert_equal(t, @t01.normal_vector)
181
+
182
+ t = Vector3D[0.0, 0.0, 1.0]
183
+ assert_equal(t, @t02.normal_vector)
184
+ end
185
+
186
+ def test_equivalent?
187
+ assert_raise(Triangle::TypeError){
188
+ @t00.equivalent?([[ 0.0, 0.0, 0.0], [ 1.0, 0.0, 0.0], [ 0.0, 1.0, 0.0]])
189
+ }
190
+
191
+ t = Triangle.new([[ 0.0, 0.0, 0.0], [ 1.0, 0.0, 0.0], [ 0.0, 1.0, 0.0]])
192
+ assert_equal(true , @t00.eql?(t))
193
+
194
+ t = Triangle.new([[ 1.0, 0.0, 0.0], [ 0.0, 0.0, 0.0], [ 0.0, 1.0, 0.0]])
195
+ assert_equal(true , @t00.eql?(t))
196
+
197
+ t = Triangle.new([[ 1.0, 0.0, 0.0], [ 0.0, 1.0, 0.0], [ 0.0, 0.0, 0.0]])
198
+ assert_equal(true , @t00.eql?(t))
199
+
200
+ t = Triangle.new([[ 0.0, 0.0, 1.0], [ 1.0, 0.0, 0.0], [ 0.0, 1.0, 0.0]])
201
+ assert_equal(false, @t00.eql?(t))
202
+
203
+ # tolerance を設定の上 0.0
204
+ t = Triangle.new([[ 0.0, 0.0, 1.0], [ 1.0, 0.0, 0.0], [ 0.0, 1.0, 0.0]])
205
+ assert_equal(false, @t00.eql?(t, 0.0))
206
+
207
+ # tolerance を 1.0 に設定
208
+ t = Triangle.new([[ 0.0, 0.0, 1.0], [ 1.0, 0.0, 0.0], [ 0.0, 1.0, 0.0]])
209
+ assert_equal(true, @t00.eql?(t, 1.0))
210
+ end
211
+
212
+ def test_equal2
213
+ t = Triangle.new([[ 0.0, 0.0, 0.0], [ 1.0, 0.0, 0.0], [ 0.0, 1.0, 0.0]])
214
+ assert_equal(true , @t00 == t)
215
+
216
+ t = Triangle.new([[ 1.0, 0.0, 0.0], [ 0.0, 0.0, 0.0], [ 0.0, 1.0, 0.0]])
217
+ assert_equal(false, @t00 == t)
218
+
219
+ t = Triangle.new([[ 1.0, 0.0, 0.0], [ 0.0, 1.0, 0.0], [ 0.0, 0.0, 0.0]])
220
+ assert_equal(false, @t00 == t)
221
+
222
+ t = Triangle.new([[ 0.0, 0.0, 1.0], [ 1.0, 0.0, 0.0], [ 0.0, 1.0, 0.0]])
223
+ assert_equal(false, @t00 == t)
224
+ end
225
+
226
+ def test_internal_axes
227
+ @t01 = Triangle.new([[ 1.0, 0.0, 0.0], [ 0.0, 1.0, 0.0], [ 0.0, 0.0, 1.0]])
228
+ @t02 = Triangle.new([[10.0,10.0,10.0], [20.0,10.0,10.0], [10.0,20.0,10.0]])
229
+
230
+ t = Axes.new([[ 1.0, 0.0, 0.0], [ 0.0, 1.0, 0.0], [ 0.0, 0.0, 1.0]])
231
+ assert_equal(t, @t00.internal_axes)
232
+
233
+ t = @t01.internal_axes
234
+ assert_in_delta(-1.0, t[0][0] , $tolerance)
235
+ assert_in_delta( 1.0, t[0][1] , $tolerance)
236
+ assert_in_delta( 0.0, t[0][2] , $tolerance)
237
+ assert_in_delta(-1.0, t[1][0] , $tolerance)
238
+ assert_in_delta( 0.0, t[1][1] , $tolerance)
239
+ assert_in_delta( 1.0, t[1][2] , $tolerance)
240
+ assert_in_delta(1.0/Math.sqrt(3.0), t[2][0], $tolerance)
241
+ assert_in_delta(1.0/Math.sqrt(3.0), t[2][1], $tolerance)
242
+ assert_in_delta(1.0/Math.sqrt(3.0), t[2][2], $tolerance)
243
+
244
+ t = Axes.new([[ 10.0, 0.0, 0.0], [ 0.0, 10.0, 0.0], [ 0.0, 0.0, 1.0]])
245
+ assert_equal(t, @t02.internal_axes)
246
+ end
247
+
248
+ def test_edges
249
+ t = @t00.edges
250
+ assert_equal(3, t.size)
251
+
252
+ assert_equal(Segment.new(VEC_O, VEC_X), t[0])
253
+ assert_equal(Segment.new(VEC_X, VEC_Y), t[1])
254
+ assert_equal(Segment.new(VEC_Y, VEC_O), t[2])
255
+ end
257
256
  end
258
257