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_tri_mesh.rb
CHANGED
@@ -1,186 +1,186 @@
|
|
1
|
-
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
2
|
-
require 'helper'
|
3
|
-
|
4
|
-
include GMath3D
|
5
|
-
|
6
|
-
MiniTest::Unit.autorun
|
7
|
-
|
8
|
-
class TriMeshTestCase < MiniTest::Unit::TestCase
|
9
|
-
def get_box_mesh
|
10
|
-
box = Box.new(Vector3.new(-1,-1,-1), Vector3.new(2,3,4))
|
11
|
-
return TriMesh.from_box(box)
|
12
|
-
end
|
13
|
-
|
14
|
-
def get_plane_mesh
|
15
|
-
rect = Rectangle.new(Vector3.new(-1,-1,-1), Vector3.new(2,0,0), Vector3.new(0,4,0) )
|
16
|
-
return TriMesh.from_rectangle(rect)
|
17
|
-
end
|
18
|
-
|
19
|
-
def test_initalize
|
20
|
-
plane_mesh = get_plane_mesh()
|
21
|
-
assert_equal( 2, plane_mesh.tri_indices.size)
|
22
|
-
assert_equal( 4, plane_mesh.vertices.size)
|
23
|
-
|
24
|
-
box_mesh = get_box_mesh()
|
25
|
-
assert_equal( 12, box_mesh.tri_indices.size)
|
26
|
-
assert_equal( 8, box_mesh.vertices.size)
|
27
|
-
|
28
|
-
assert_raises ArgumentError do
|
29
|
-
invalidResult = TriMesh.new(nil)
|
30
|
-
end
|
31
|
-
assert_raises ArgumentError do
|
32
|
-
invalidResult = TriMesh.new(Vector3.new(), 4.0)
|
33
|
-
end
|
34
|
-
end
|
35
|
-
|
36
|
-
def test_equal
|
37
|
-
plane_mesh = get_plane_mesh()
|
38
|
-
assert( plane_mesh != nil )
|
39
|
-
assert( plane_mesh != "String" )
|
40
|
-
|
41
|
-
shallow = plane_mesh
|
42
|
-
assert( shallow == plane_mesh )
|
43
|
-
assert( shallow.equal?(plane_mesh) )
|
44
|
-
|
45
|
-
new_mesh = get_plane_mesh()
|
46
|
-
assert( shallow == new_mesh )
|
47
|
-
assert( !shallow.equal?(new_mesh) )
|
48
|
-
end
|
49
|
-
|
50
|
-
def test_clone
|
51
|
-
plane_mesh = get_plane_mesh()
|
52
|
-
shallow = plane_mesh
|
53
|
-
|
54
|
-
shallow.vertices[0].x = 12
|
55
|
-
shallow.vertices[2] = Vector3.new(-3,2,5)
|
56
|
-
shallow.tri_indices[0] = [3,2,1]
|
57
|
-
shallow.tri_indices[1][2] = 2
|
58
|
-
|
59
|
-
assert_equal( 12, shallow.vertices[0].x )
|
60
|
-
assert_equal( 12, plane_mesh.vertices[0].x )
|
61
|
-
assert_equal( Vector3.new(-3,2,5), shallow.vertices[2] )
|
62
|
-
assert_equal( Vector3.new(-3,2,5), plane_mesh.vertices[2] )
|
63
|
-
assert_equal( [3,2,1], shallow.tri_indices[0] )
|
64
|
-
assert_equal( [3,2,1], plane_mesh.tri_indices[0] )
|
65
|
-
assert_equal( 2, shallow.tri_indices[1][2] )
|
66
|
-
assert_equal( 2, plane_mesh.tri_indices[1][2] )
|
67
|
-
|
68
|
-
deep = plane_mesh.clone
|
69
|
-
assert( deep == plane_mesh )
|
70
|
-
assert( !deep.equal?(plane_mesh) )
|
71
|
-
|
72
|
-
deep.vertices[0].x = -1
|
73
|
-
deep.vertices[2] = Vector3.new(4,2,1)
|
74
|
-
deep.tri_indices[0] = [4,2,2]
|
75
|
-
deep.tri_indices[1][2] = 5
|
76
|
-
|
77
|
-
assert_equal( -1, deep.vertices[0].x )
|
78
|
-
assert_equal( 12, plane_mesh.vertices[0].x )
|
79
|
-
assert_equal( Vector3.new(4,2,1), deep.vertices[2] )
|
80
|
-
assert_equal( Vector3.new(-3,2,5), plane_mesh.vertices[2] )
|
81
|
-
assert_equal( [4,2,2], deep.tri_indices[0] )
|
82
|
-
assert_equal( [3,2,1], plane_mesh.tri_indices[0] )
|
83
|
-
assert_equal( 5, deep.tri_indices[1][2] )
|
84
|
-
assert_equal( 2, plane_mesh.tri_indices[1][2] )
|
85
|
-
end
|
86
|
-
|
87
|
-
def test_to_s
|
88
|
-
assert_equal( "TriMesh[triangle_count:12, vertex_count:8]", get_box_mesh().to_s)
|
89
|
-
end
|
90
|
-
|
91
|
-
def test_box
|
92
|
-
box_mesh = get_box_mesh
|
93
|
-
plane_mesh = get_plane_mesh
|
94
|
-
assert_equal( Vector3.new(-1,-1,-1), box_mesh.box.min_point )
|
95
|
-
assert_equal( Vector3.new( 2, 3, 4), box_mesh.box.max_point )
|
96
|
-
assert_equal( Vector3.new(-1,-1,-1), plane_mesh.box.min_point )
|
97
|
-
assert_equal( Vector3.new( 1, 3,-1), plane_mesh.box.max_point )
|
98
|
-
end
|
99
|
-
|
100
|
-
def test_triangles
|
101
|
-
vertices = [Vector3.new(0,0,0),Vector3.new(2,0,0),Vector3.new(2,2,0),Vector3.new(0,2,0)]
|
102
|
-
tri_indices = [[0,1,3],[1,2,3]]
|
103
|
-
tri_mesh = TriMesh.new(vertices, tri_indices)
|
104
|
-
triangles = tri_mesh.triangles
|
105
|
-
assert_equal(2, triangles.size)
|
106
|
-
assert_equal(Vector3.new(0,0,0), triangles[0].vertices[0])
|
107
|
-
assert_equal(Vector3.new(2,0,0), triangles[0].vertices[1])
|
108
|
-
assert_equal(Vector3.new(0,2,0), triangles[0].vertices[2])
|
109
|
-
assert_equal(Vector3.new(2,0,0), triangles[1].vertices[0])
|
110
|
-
assert_equal(Vector3.new(2,2,0), triangles[1].vertices[1])
|
111
|
-
assert_equal(Vector3.new(0,2,0), triangles[1].vertices[2])
|
112
|
-
|
113
|
-
triangle = tri_mesh.triangle(1)
|
114
|
-
assert_equal(Vector3.new(2,0,0), triangle.vertices[0])
|
115
|
-
assert_equal(Vector3.new(2,2,0), triangle.vertices[1])
|
116
|
-
assert_equal(Vector3.new(0,2,0), triangle.vertices[2])
|
117
|
-
|
118
|
-
triangle = tri_mesh.triangle(3)
|
119
|
-
assert_equal(nil, triangle)
|
120
|
-
end
|
121
|
-
|
122
|
-
def test_from_triangles
|
123
|
-
tris = Array.new(8)
|
124
|
-
tris[0] = Triangle.new( Vector3.new(0,0,0), Vector3.new(1,0,0), Vector3.new(0,1,1) )
|
125
|
-
tris[1] = Triangle.new( Vector3.new(1,0,0), Vector3.new(1,1,1), Vector3.new(0,1,1) )
|
126
|
-
tris[2] = Triangle.new( Vector3.new(1,0,0), Vector3.new(2,0,0), Vector3.new(1,1,1) )
|
127
|
-
tris[3] = Triangle.new( Vector3.new(2,0,0), Vector3.new(2,1,1), Vector3.new(1,1,1) )
|
128
|
-
tris[4] = Triangle.new( Vector3.new(0,1,1), Vector3.new(1,1,1), Vector3.new(0,2,2) )
|
129
|
-
tris[5] = Triangle.new( Vector3.new(1,1,1), Vector3.new(1,2,2), Vector3.new(0,2,2) )
|
130
|
-
tris[6] = Triangle.new( Vector3.new(1,1,1), Vector3.new(2,1,1), Vector3.new(1,2,2) )
|
131
|
-
tris[7] = Triangle.new( Vector3.new(2,1,1), Vector3.new(2,2,2), Vector3.new(1,2,2) )
|
132
|
-
trimesh_from_tris = TriMesh::from_triangles(tris)
|
133
|
-
assert_equal( 9, trimesh_from_tris.vertices.size)
|
134
|
-
assert_equal( 8, trimesh_from_tris.tri_indices.size)
|
135
|
-
end
|
136
|
-
|
137
|
-
def test_from_convex_polyline
|
138
|
-
vertices = Array.new(6)
|
139
|
-
vertices[0] = Vector3.new(1,0,0)
|
140
|
-
vertices[1] = Vector3.new(2,0,0)
|
141
|
-
vertices[2] = Vector3.new(3,1,0)
|
142
|
-
vertices[3] = Vector3.new(2,2,0)
|
143
|
-
vertices[4] = Vector3.new(1,2,0)
|
144
|
-
vertices[5] = Vector3.new(0,1,0)
|
145
|
-
polyline_closed = Polyline.new( vertices, false ) # closed Polyline
|
146
|
-
polyline_open = Polyline.new( vertices, true ) # open Polyline
|
147
|
-
trimesh_from_convex_polyline1 = TriMesh.from_convex_polyline( polyline_closed )
|
148
|
-
trimesh_from_convex_polyline2 = TriMesh.from_convex_polyline( polyline_open )
|
149
|
-
|
150
|
-
assert_equal(4, trimesh_from_convex_polyline1.area)
|
151
|
-
assert_equal(4, trimesh_from_convex_polyline2.area)
|
152
|
-
end
|
153
|
-
|
154
|
-
def test_from_extruded_polyline
|
155
|
-
vertices = Array.new(6)
|
156
|
-
vertices[0] = Vector3.new(1,0,0)
|
157
|
-
vertices[1] = Vector3.new(2,0,0)
|
158
|
-
vertices[2] = Vector3.new(3,1,0)
|
159
|
-
vertices[3] = Vector3.new(2,2,0)
|
160
|
-
vertices[4] = Vector3.new(1,2,0)
|
161
|
-
vertices[5] = Vector3.new(0,1,0)
|
162
|
-
polyline_closed = Polyline.new( vertices, false ) # closed Polyline
|
163
|
-
polyline_open = Polyline.new( vertices, true ) # open Polyline
|
164
|
-
extrude_direction = Vector3.new(0,0,2)
|
165
|
-
trimesh_from_extruded_polyline1 = TriMesh.from_extrude_polyline( polyline_closed , extrude_direction )
|
166
|
-
trimesh_from_extruded_polyline2 = TriMesh.from_extrude_polyline( polyline_open , extrude_direction )
|
167
|
-
|
168
|
-
assert_in_delta(4+8*(Math.sqrt(2)), trimesh_from_extruded_polyline1.area, 1e-10)
|
169
|
-
assert_in_delta(4+6*(Math.sqrt(2)), trimesh_from_extruded_polyline2.area, 1e-10)
|
170
|
-
end
|
171
|
-
|
172
|
-
def test_area
|
173
|
-
box_mesh = get_box_mesh()
|
174
|
-
assert_equal(94, box_mesh.area)
|
175
|
-
end
|
176
|
-
|
177
|
-
def test_normals_for_each_vertices
|
178
|
-
box = Box.new(Vector3.new(-1,-1,-1), Vector3.new(1,1,1))
|
179
|
-
box_mesh = TriMesh.from_box(box)
|
180
|
-
result = box_mesh.normals_for_each_vertices
|
181
|
-
assert_equal( box_mesh.vertices.size, result.size )
|
182
|
-
box_mesh.vertices.each do |vertex|
|
183
|
-
assert_equal(vertex.normalize, result[vertex])
|
184
|
-
end
|
185
|
-
end
|
186
|
-
end
|
1
|
+
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
2
|
+
require 'helper'
|
3
|
+
|
4
|
+
include GMath3D
|
5
|
+
|
6
|
+
MiniTest::Unit.autorun
|
7
|
+
|
8
|
+
class TriMeshTestCase < MiniTest::Unit::TestCase
|
9
|
+
def get_box_mesh
|
10
|
+
box = Box.new(Vector3.new(-1,-1,-1), Vector3.new(2,3,4))
|
11
|
+
return TriMesh.from_box(box)
|
12
|
+
end
|
13
|
+
|
14
|
+
def get_plane_mesh
|
15
|
+
rect = Rectangle.new(Vector3.new(-1,-1,-1), Vector3.new(2,0,0), Vector3.new(0,4,0) )
|
16
|
+
return TriMesh.from_rectangle(rect)
|
17
|
+
end
|
18
|
+
|
19
|
+
def test_initalize
|
20
|
+
plane_mesh = get_plane_mesh()
|
21
|
+
assert_equal( 2, plane_mesh.tri_indices.size)
|
22
|
+
assert_equal( 4, plane_mesh.vertices.size)
|
23
|
+
|
24
|
+
box_mesh = get_box_mesh()
|
25
|
+
assert_equal( 12, box_mesh.tri_indices.size)
|
26
|
+
assert_equal( 8, box_mesh.vertices.size)
|
27
|
+
|
28
|
+
assert_raises ArgumentError do
|
29
|
+
invalidResult = TriMesh.new(nil)
|
30
|
+
end
|
31
|
+
assert_raises ArgumentError do
|
32
|
+
invalidResult = TriMesh.new(Vector3.new(), 4.0)
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
def test_equal
|
37
|
+
plane_mesh = get_plane_mesh()
|
38
|
+
assert( plane_mesh != nil )
|
39
|
+
assert( plane_mesh != "String" )
|
40
|
+
|
41
|
+
shallow = plane_mesh
|
42
|
+
assert( shallow == plane_mesh )
|
43
|
+
assert( shallow.equal?(plane_mesh) )
|
44
|
+
|
45
|
+
new_mesh = get_plane_mesh()
|
46
|
+
assert( shallow == new_mesh )
|
47
|
+
assert( !shallow.equal?(new_mesh) )
|
48
|
+
end
|
49
|
+
|
50
|
+
def test_clone
|
51
|
+
plane_mesh = get_plane_mesh()
|
52
|
+
shallow = plane_mesh
|
53
|
+
|
54
|
+
shallow.vertices[0].x = 12
|
55
|
+
shallow.vertices[2] = Vector3.new(-3,2,5)
|
56
|
+
shallow.tri_indices[0] = [3,2,1]
|
57
|
+
shallow.tri_indices[1][2] = 2
|
58
|
+
|
59
|
+
assert_equal( 12, shallow.vertices[0].x )
|
60
|
+
assert_equal( 12, plane_mesh.vertices[0].x )
|
61
|
+
assert_equal( Vector3.new(-3,2,5), shallow.vertices[2] )
|
62
|
+
assert_equal( Vector3.new(-3,2,5), plane_mesh.vertices[2] )
|
63
|
+
assert_equal( [3,2,1], shallow.tri_indices[0] )
|
64
|
+
assert_equal( [3,2,1], plane_mesh.tri_indices[0] )
|
65
|
+
assert_equal( 2, shallow.tri_indices[1][2] )
|
66
|
+
assert_equal( 2, plane_mesh.tri_indices[1][2] )
|
67
|
+
|
68
|
+
deep = plane_mesh.clone
|
69
|
+
assert( deep == plane_mesh )
|
70
|
+
assert( !deep.equal?(plane_mesh) )
|
71
|
+
|
72
|
+
deep.vertices[0].x = -1
|
73
|
+
deep.vertices[2] = Vector3.new(4,2,1)
|
74
|
+
deep.tri_indices[0] = [4,2,2]
|
75
|
+
deep.tri_indices[1][2] = 5
|
76
|
+
|
77
|
+
assert_equal( -1, deep.vertices[0].x )
|
78
|
+
assert_equal( 12, plane_mesh.vertices[0].x )
|
79
|
+
assert_equal( Vector3.new(4,2,1), deep.vertices[2] )
|
80
|
+
assert_equal( Vector3.new(-3,2,5), plane_mesh.vertices[2] )
|
81
|
+
assert_equal( [4,2,2], deep.tri_indices[0] )
|
82
|
+
assert_equal( [3,2,1], plane_mesh.tri_indices[0] )
|
83
|
+
assert_equal( 5, deep.tri_indices[1][2] )
|
84
|
+
assert_equal( 2, plane_mesh.tri_indices[1][2] )
|
85
|
+
end
|
86
|
+
|
87
|
+
def test_to_s
|
88
|
+
assert_equal( "TriMesh[triangle_count:12, vertex_count:8]", get_box_mesh().to_s)
|
89
|
+
end
|
90
|
+
|
91
|
+
def test_box
|
92
|
+
box_mesh = get_box_mesh
|
93
|
+
plane_mesh = get_plane_mesh
|
94
|
+
assert_equal( Vector3.new(-1,-1,-1), box_mesh.box.min_point )
|
95
|
+
assert_equal( Vector3.new( 2, 3, 4), box_mesh.box.max_point )
|
96
|
+
assert_equal( Vector3.new(-1,-1,-1), plane_mesh.box.min_point )
|
97
|
+
assert_equal( Vector3.new( 1, 3,-1), plane_mesh.box.max_point )
|
98
|
+
end
|
99
|
+
|
100
|
+
def test_triangles
|
101
|
+
vertices = [Vector3.new(0,0,0),Vector3.new(2,0,0),Vector3.new(2,2,0),Vector3.new(0,2,0)]
|
102
|
+
tri_indices = [[0,1,3],[1,2,3]]
|
103
|
+
tri_mesh = TriMesh.new(vertices, tri_indices)
|
104
|
+
triangles = tri_mesh.triangles
|
105
|
+
assert_equal(2, triangles.size)
|
106
|
+
assert_equal(Vector3.new(0,0,0), triangles[0].vertices[0])
|
107
|
+
assert_equal(Vector3.new(2,0,0), triangles[0].vertices[1])
|
108
|
+
assert_equal(Vector3.new(0,2,0), triangles[0].vertices[2])
|
109
|
+
assert_equal(Vector3.new(2,0,0), triangles[1].vertices[0])
|
110
|
+
assert_equal(Vector3.new(2,2,0), triangles[1].vertices[1])
|
111
|
+
assert_equal(Vector3.new(0,2,0), triangles[1].vertices[2])
|
112
|
+
|
113
|
+
triangle = tri_mesh.triangle(1)
|
114
|
+
assert_equal(Vector3.new(2,0,0), triangle.vertices[0])
|
115
|
+
assert_equal(Vector3.new(2,2,0), triangle.vertices[1])
|
116
|
+
assert_equal(Vector3.new(0,2,0), triangle.vertices[2])
|
117
|
+
|
118
|
+
triangle = tri_mesh.triangle(3)
|
119
|
+
assert_equal(nil, triangle)
|
120
|
+
end
|
121
|
+
|
122
|
+
def test_from_triangles
|
123
|
+
tris = Array.new(8)
|
124
|
+
tris[0] = Triangle.new( Vector3.new(0,0,0), Vector3.new(1,0,0), Vector3.new(0,1,1) )
|
125
|
+
tris[1] = Triangle.new( Vector3.new(1,0,0), Vector3.new(1,1,1), Vector3.new(0,1,1) )
|
126
|
+
tris[2] = Triangle.new( Vector3.new(1,0,0), Vector3.new(2,0,0), Vector3.new(1,1,1) )
|
127
|
+
tris[3] = Triangle.new( Vector3.new(2,0,0), Vector3.new(2,1,1), Vector3.new(1,1,1) )
|
128
|
+
tris[4] = Triangle.new( Vector3.new(0,1,1), Vector3.new(1,1,1), Vector3.new(0,2,2) )
|
129
|
+
tris[5] = Triangle.new( Vector3.new(1,1,1), Vector3.new(1,2,2), Vector3.new(0,2,2) )
|
130
|
+
tris[6] = Triangle.new( Vector3.new(1,1,1), Vector3.new(2,1,1), Vector3.new(1,2,2) )
|
131
|
+
tris[7] = Triangle.new( Vector3.new(2,1,1), Vector3.new(2,2,2), Vector3.new(1,2,2) )
|
132
|
+
trimesh_from_tris = TriMesh::from_triangles(tris)
|
133
|
+
assert_equal( 9, trimesh_from_tris.vertices.size)
|
134
|
+
assert_equal( 8, trimesh_from_tris.tri_indices.size)
|
135
|
+
end
|
136
|
+
|
137
|
+
def test_from_convex_polyline
|
138
|
+
vertices = Array.new(6)
|
139
|
+
vertices[0] = Vector3.new(1,0,0)
|
140
|
+
vertices[1] = Vector3.new(2,0,0)
|
141
|
+
vertices[2] = Vector3.new(3,1,0)
|
142
|
+
vertices[3] = Vector3.new(2,2,0)
|
143
|
+
vertices[4] = Vector3.new(1,2,0)
|
144
|
+
vertices[5] = Vector3.new(0,1,0)
|
145
|
+
polyline_closed = Polyline.new( vertices, false ) # closed Polyline
|
146
|
+
polyline_open = Polyline.new( vertices, true ) # open Polyline
|
147
|
+
trimesh_from_convex_polyline1 = TriMesh.from_convex_polyline( polyline_closed )
|
148
|
+
trimesh_from_convex_polyline2 = TriMesh.from_convex_polyline( polyline_open )
|
149
|
+
|
150
|
+
assert_equal(4, trimesh_from_convex_polyline1.area)
|
151
|
+
assert_equal(4, trimesh_from_convex_polyline2.area)
|
152
|
+
end
|
153
|
+
|
154
|
+
def test_from_extruded_polyline
|
155
|
+
vertices = Array.new(6)
|
156
|
+
vertices[0] = Vector3.new(1,0,0)
|
157
|
+
vertices[1] = Vector3.new(2,0,0)
|
158
|
+
vertices[2] = Vector3.new(3,1,0)
|
159
|
+
vertices[3] = Vector3.new(2,2,0)
|
160
|
+
vertices[4] = Vector3.new(1,2,0)
|
161
|
+
vertices[5] = Vector3.new(0,1,0)
|
162
|
+
polyline_closed = Polyline.new( vertices, false ) # closed Polyline
|
163
|
+
polyline_open = Polyline.new( vertices, true ) # open Polyline
|
164
|
+
extrude_direction = Vector3.new(0,0,2)
|
165
|
+
trimesh_from_extruded_polyline1 = TriMesh.from_extrude_polyline( polyline_closed , extrude_direction )
|
166
|
+
trimesh_from_extruded_polyline2 = TriMesh.from_extrude_polyline( polyline_open , extrude_direction )
|
167
|
+
|
168
|
+
assert_in_delta(4+8*(Math.sqrt(2)), trimesh_from_extruded_polyline1.area, 1e-10)
|
169
|
+
assert_in_delta(4+6*(Math.sqrt(2)), trimesh_from_extruded_polyline2.area, 1e-10)
|
170
|
+
end
|
171
|
+
|
172
|
+
def test_area
|
173
|
+
box_mesh = get_box_mesh()
|
174
|
+
assert_equal(94, box_mesh.area)
|
175
|
+
end
|
176
|
+
|
177
|
+
def test_normals_for_each_vertices
|
178
|
+
box = Box.new(Vector3.new(-1,-1,-1), Vector3.new(1,1,1))
|
179
|
+
box_mesh = TriMesh.from_box(box)
|
180
|
+
result = box_mesh.normals_for_each_vertices
|
181
|
+
assert_equal( box_mesh.vertices.size, result.size )
|
182
|
+
box_mesh.vertices.each do |vertex|
|
183
|
+
assert_equal(vertex.normalize, result[vertex])
|
184
|
+
end
|
185
|
+
end
|
186
|
+
end
|
data/test/test_triangle.rb
CHANGED
@@ -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
|