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,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