gmath3D 0.2.1 → 0.2.2

Sign up to get free protection for your applications and to get access to all the features.
data/lib/vector3.rb CHANGED
@@ -37,6 +37,15 @@ public
37
37
  # [Output]
38
38
  # return true if rhs equals myself.
39
39
  def ==(rhs)
40
+ return false if( !rhs.kind_of?(Vector3) )
41
+ equals_inner(rhs)
42
+ end
43
+
44
+ # For using Vector3 as hash key
45
+ def hash
46
+ [@x.to_f, @y.to_f, @z.to_f].hash
47
+ end
48
+ def eql?(rhs)
40
49
  equals_inner(rhs)
41
50
  end
42
51
 
@@ -169,7 +178,6 @@ public
169
178
 
170
179
  private
171
180
  def equals_inner(rhs)
172
- return false if( !rhs.kind_of?(Vector3) )
173
181
  return false if((self.x - rhs.x).abs > @tolerance)
174
182
  return false if((self.y - rhs.y).abs > @tolerance)
175
183
  return false if((self.z - rhs.z).abs > @tolerance)
@@ -25,6 +25,13 @@ class FiniteLineTestCase < MiniTest::Unit::TestCase
25
25
  end
26
26
 
27
27
  def test_equals
28
+ line = FiniteLine.new()
29
+ shallow_copied = line
30
+ assert(line == shallow_copied)
31
+ assert(line.equal?(shallow_copied))
32
+ assert(line != nil)
33
+ assert(line != "string")
34
+
28
35
  assert_equal(FiniteLine.new(Vector3.new(1,2,3), Vector3.new(2,3,4)),
29
36
  FiniteLine.new(Vector3.new(1.0,2.0,3.0), Vector3.new(2.0,3.0,4.0)))
30
37
 
@@ -33,6 +40,23 @@ class FiniteLineTestCase < MiniTest::Unit::TestCase
33
40
  assert(FiniteLine.new(Vector3.new(1,2,3), Vector3.new(2,3,4)) != FiniteLine.new(Vector3.new(2,3,4), Vector3.new(1,2,3)))
34
41
  end
35
42
 
43
+ def test_clone
44
+ line = FiniteLine.new()
45
+ shallow_copied = line
46
+ shallow_copied.start_point.x = -1
47
+ assert(line == shallow_copied)
48
+ assert(line.equal?(shallow_copied))
49
+ assert_equal(-1, shallow_copied.start_point.x)
50
+
51
+ cloned = line.clone
52
+ assert(line == cloned)
53
+ assert(!line.equal?(cloned))
54
+ cloned.start_point.x = -2
55
+ assert_equal(-2, cloned.start_point.x)
56
+
57
+ assert_equal(-1, line.start_point.x) # original never changed in editing cloned one.
58
+ end
59
+
36
60
  def test_direction
37
61
  start_point_tmp = Vector3.new(1.0, 0.0, 2.0)
38
62
  end_point_tmp = Vector3.new(1.0, -3.5, 1.0)
data/test/test_line.rb CHANGED
@@ -19,6 +19,39 @@ class LineTestCase < MiniTest::Unit::TestCase
19
19
  assert(Vector3.new(1,0,0) == lineDefault.direction )
20
20
  end
21
21
 
22
+ def test_equals
23
+ line = Line.new()
24
+ shallow_copied = line
25
+ assert(line.equal?(shallow_copied))
26
+ assert(line == shallow_copied)
27
+ assert(line != nil)
28
+ assert(line != "string")
29
+
30
+ assert_equal(Line.new(Vector3.new(1,2,3), Vector3.new(2,3,4)), Line.new(Vector3.new(1.0,2.0,3.0), Vector3.new(2.0,3.0,4.0)))
31
+
32
+ assert(Line.new(Vector3.new(1,2,3), Vector3.new(2,3,4)) == Line.new(Vector3.new(1.0,2.0,3.0), Vector3.new(2.0,3.0,4.0)))
33
+ assert(Line.new(Vector3.new(1,2,3), Vector3.new(2,3,3)) != Line.new(Vector3.new(1.0,2.0,3.0), Vector3.new(2.0,3.0,4.0)))
34
+ assert(Line.new(Vector3.new(1,2,3), Vector3.new(2,3,4)) != Line.new(Vector3.new(2,3,4), Vector3.new(1,2,3)))
35
+ end
36
+
37
+ def test_clone
38
+ line = Line.new()
39
+ shallow_copied = line
40
+ shallow_copied.base_point.x = -1
41
+ assert(line == shallow_copied)
42
+ assert(line.equal?(shallow_copied))
43
+ assert_equal(-1, shallow_copied.base_point.x)
44
+ assert_equal(-1, line.base_point.x)
45
+
46
+ cloned = line.clone
47
+ assert(line == cloned)
48
+ assert(!line.equal?(cloned))
49
+ cloned.base_point.x = -2
50
+ assert_equal(-2, cloned.base_point.x)
51
+
52
+ assert_equal(-1, line.base_point.x) # original never changed in editing cloned one.
53
+ end
54
+
22
55
  def test_to_s
23
56
  line = Line.new(Vector3.new(2.0, 3, 5), Vector3.new(1.0, 1.0, 1.0))
24
57
  assert_equal("Line[point[2.0, 3, 5], vector[1.0, 1.0, 1.0]", line.to_s);
data/test/test_plane.rb CHANGED
@@ -25,6 +25,39 @@ class PlaneTestCase < MiniTest::Unit::TestCase
25
25
  assert_equal("Plane[point[0, 0, 1], normal[0.0, 0.0, 1.0]]", @plane.to_s)
26
26
  end
27
27
 
28
+ def test_equals
29
+ plane = Plane.new()
30
+ shallow_copied = plane
31
+ assert(plane.equal?(shallow_copied))
32
+ assert(plane == shallow_copied)
33
+ assert(plane != nil)
34
+ assert(plane != "string")
35
+
36
+ assert_equal(Plane.new(Vector3.new(1,2,3), Vector3.new(2,3,4)), Plane.new(Vector3.new(1.0,2.0,3.0), Vector3.new(2.0,3.0,4.0)))
37
+
38
+ assert(Plane.new(Vector3.new(1,2,3), Vector3.new(2,3,4)) == Plane.new(Vector3.new(1.0,2.0,3.0), Vector3.new(2.0,3.0,4.0)))
39
+ assert(Plane.new(Vector3.new(1,2,3), Vector3.new(2,3,4)) == Plane.new(Vector3.new(1.0,2.0,3.0), Vector3.new(4.0,6,8.0)))
40
+ assert(Plane.new(Vector3.new(1,2,3), Vector3.new(2,3,3)) != Plane.new(Vector3.new(1.0,2.0,3.0), Vector3.new(2.0,3.0,4.0)))
41
+ assert(Plane.new(Vector3.new(1,2,3), Vector3.new(2,3,4)) != Plane.new(Vector3.new(2,3,4), Vector3.new(1,2,3)))
42
+ end
43
+
44
+ def test_clone
45
+ plane = Plane.new()
46
+ shallow_copied = plane
47
+ shallow_copied.base_point.x = -1
48
+ assert(plane == shallow_copied)
49
+ assert(plane.equal?(shallow_copied))
50
+ assert_equal(-1, shallow_copied.base_point.x)
51
+
52
+ cloned = plane.clone
53
+ assert(plane == cloned)
54
+ assert(!plane.equal?(cloned))
55
+ cloned.base_point.x = -2
56
+ assert_equal(-2, cloned.base_point.x)
57
+
58
+ assert_equal(-1, plane.base_point.x) # original never changed in editing cloned one.
59
+ end
60
+
28
61
  def test_distance_to_point
29
62
  target_point = Vector3.new(1,2,3)
30
63
  distance, closest_point = @plane.distance(target_point)
@@ -0,0 +1,86 @@
1
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
2
+ require 'helper'
3
+
4
+ include GMath3D
5
+
6
+ MiniTest::Unit.autorun
7
+
8
+ class PolylineTestCase < MiniTest::Unit::TestCase
9
+ def setup
10
+ @vertices = Array.new(6)
11
+ @vertices[0] = Vector3.new(1,0,0)
12
+ @vertices[1] = Vector3.new(2.0,0,0)
13
+ @vertices[2] = Vector3.new(3,1,0)
14
+ @vertices[3] = Vector3.new(2,2,1)
15
+ @vertices[4] = Vector3.new(1,2,0)
16
+ @vertices[5] = Vector3.new(0,1,0)
17
+ @polyline_open = Polyline.new(@vertices, true)
18
+ @polyline_closed = Polyline.new(@vertices, false)
19
+ end
20
+
21
+ def test_initialize
22
+ idx = 0
23
+ @vertices.each do |vertex|
24
+ assert_equal(@polyline_open.vertices[idx], vertex)
25
+ assert_equal(@polyline_closed.vertices[idx], vertex)
26
+ idx += 1
27
+ end
28
+ assert_equal( true, @polyline_open.is_open )
29
+ assert_equal( false, @polyline_closed.is_open )
30
+ end
31
+
32
+ def test_equals
33
+ shallow_copied = @polyline_open
34
+ assert(@polyline_open.equal?(shallow_copied))
35
+ assert(@polyline_open == shallow_copied)
36
+ assert(@polyline_open != nil)
37
+ assert(@polyline_open != "string")
38
+
39
+ assert(@polyline_open != @polyline_closed)
40
+
41
+ polyline_new_open = Polyline.new(@vertices, true)
42
+ assert(!@polyline_open.equal?(polyline_new_open))
43
+ assert(@polyline_open == polyline_new_open)
44
+ assert_equal(@polyline_open, polyline_new_open)
45
+
46
+ new_vertices = @vertices.clone
47
+ new_vertices.push( Vector3.new(3,4,5))
48
+ polyline_new_open = Polyline.new(new_vertices, true)
49
+ assert(polyline_new_open != @polyline_open)
50
+ assert(@polyline_open != polyline_new_open)
51
+ end
52
+
53
+ def test_clone
54
+ shallow_copied = @polyline_open
55
+ vert_cnt = @polyline_open.vertices.size
56
+ shallow_copied.vertices[vert_cnt - 1].x = 20
57
+ assert(@polyline_open == shallow_copied)
58
+ assert(@polyline_open.equal?(shallow_copied))
59
+ assert_equal(20, @polyline_open.vertices[vert_cnt - 1].x)
60
+ assert_equal(20, shallow_copied.vertices[vert_cnt - 1].x)
61
+
62
+ cloned = @polyline_open.clone
63
+ assert(@polyline_open == cloned)
64
+ assert(!@polyline_open.equal?(cloned))
65
+
66
+ cloned.vertices[vert_cnt - 1].x = -20
67
+ assert_equal(-20, cloned.vertices[vert_cnt - 1].x)
68
+ assert_equal(20, @polyline_open.vertices[vert_cnt - 1].x) # original never changed in editing cloned one.
69
+
70
+ cloned.is_open = false
71
+ assert_equal(false, cloned.is_open)
72
+ assert_equal(true, @polyline_open.is_open)
73
+ end
74
+
75
+ def test_to_s
76
+ assert_equal("Polyline[[1, 0, 0], [2.0, 0, 0], [3, 1, 0], [2, 2, 1], [1, 2, 0], [0, 1, 0]] open",
77
+ @polyline_open.to_s);
78
+ assert_equal("Polyline[[1, 0, 0], [2.0, 0, 0], [3, 1, 0], [2, 2, 1], [1, 2, 0], [0, 1, 0]] closed",
79
+ @polyline_closed.to_s);
80
+ end
81
+
82
+ def test_center
83
+ assert_equal(Vector3.new(1.5, 1.0, 1.0/6.0), @polyline_open.center)
84
+ assert_equal(Vector3.new(1.5, 1.0, 1.0/6.0), @polyline_closed.center)
85
+ end
86
+ end
data/test/test_quat.rb CHANGED
@@ -132,4 +132,13 @@ class QuatTestCase < MiniTest::Unit::TestCase
132
132
  end
133
133
  end
134
134
 
135
+ def test_normalize
136
+ quat = Quat.new(-1,2,1,1)
137
+ quat = quat.normalize()
138
+ assert_equal(-1/Math.sqrt(6), quat.x)
139
+ assert_equal( 2/Math.sqrt(6), quat.y)
140
+ assert_equal( 1/Math.sqrt(6), quat.z)
141
+ assert_equal( 1/Math.sqrt(6), quat.w)
142
+ end
143
+
135
144
  end
@@ -21,6 +21,42 @@ class RectangleTestCase < MiniTest::Unit::TestCase
21
21
  assert_equal(Vector3.new(0,1,0), @rectangle_default.v_vector)
22
22
  end
23
23
 
24
+ def test_equals
25
+ assert(@rectangle != nil)
26
+ assert(@rectangle != "string")
27
+
28
+ shallow_copied = @rectangle
29
+ assert(@rectangle.equal?(shallow_copied))
30
+ assert(@rectangle == shallow_copied)
31
+
32
+ assert_equal(@rectangle, Rectangle.new( Vector3.new(1,2,3), Vector3.new(0,-0.5,0), Vector3.new(0,0,2)))
33
+ assert(@rectangle == Rectangle.new( Vector3.new(1,2,3), Vector3.new(0,-0.5,0), Vector3.new(0,0,2.0)))
34
+ assert(@rectangle != Rectangle.new( Vector3.new(1,2,3), Vector3.new(0,-0.5,0), Vector3.new(0,0,2.2)))
35
+ end
36
+
37
+ def test_clone
38
+ shallow_copied = @rectangle
39
+ shallow_copied.base_point.x = -1
40
+ assert(@rectangle == shallow_copied)
41
+ assert(@rectangle.equal?(shallow_copied))
42
+ assert_equal(-1, shallow_copied.base_point.x)
43
+ assert_equal(-1, @rectangle.base_point.x)
44
+
45
+ cloned = @rectangle.clone
46
+ assert(@rectangle == cloned)
47
+ assert(!@rectangle.equal?(cloned))
48
+ cloned.base_point.x = -2
49
+ cloned.u_vector.x = 4
50
+ cloned.v_vector.z = 6
51
+ assert_equal(-2, cloned.base_point.x)
52
+ assert_equal(4, cloned.u_vector.x)
53
+ assert_equal(6, cloned.v_vector.z)
54
+
55
+ assert_equal(-1, @rectangle.base_point.x) # original never changed in editing cloned one.
56
+ assert_equal(0, @rectangle.u_vector.x)
57
+ assert_equal(2, @rectangle.v_vector.z)
58
+ end
59
+
24
60
  def test_to_s
25
61
  assert_equal("Rectangle[base[1, 2, 3], u[0, -0.5, 0], v[0, 0, 2]", @rectangle.to_s)
26
62
  end
@@ -52,6 +88,15 @@ class RectangleTestCase < MiniTest::Unit::TestCase
52
88
  assert_equal(Vector3.new(1,2 ,3), edges[3].end_point)
53
89
  end
54
90
 
91
+ def test_vertices
92
+ vertices = @rectangle.vertices
93
+ assert_equal(4, vertices.size)
94
+ assert_equal(Vector3.new(1,2,3), vertices[0])
95
+ assert_equal(Vector3.new(1,1.5,3), vertices[1])
96
+ assert_equal(Vector3.new(1,1.5,5), vertices[2])
97
+ assert_equal(Vector3.new(1,2,5), vertices[3])
98
+ end
99
+
55
100
  def test_normal
56
101
  assert_equal(Vector3.new(-1,0,0), @rectangle.normal)
57
102
  assert_equal(Vector3.new(0,0,1), @rectangle_default.normal)
@@ -0,0 +1,167 @@
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_triangles
92
+ vertices = [Vector3.new(0,0,0),Vector3.new(2,0,0),Vector3.new(2,2,0),Vector3.new(0,2,0)]
93
+ tri_indices = [[0,1,3],[1,2,3]]
94
+ tri_mesh = TriMesh.new(vertices, tri_indices)
95
+ triangles = tri_mesh.triangles
96
+ assert_equal(2, triangles.size)
97
+ assert_equal(Vector3.new(0,0,0), triangles[0].vertices[0])
98
+ assert_equal(Vector3.new(2,0,0), triangles[0].vertices[1])
99
+ assert_equal(Vector3.new(0,2,0), triangles[0].vertices[2])
100
+ assert_equal(Vector3.new(2,0,0), triangles[1].vertices[0])
101
+ assert_equal(Vector3.new(2,2,0), triangles[1].vertices[1])
102
+ assert_equal(Vector3.new(0,2,0), triangles[1].vertices[2])
103
+
104
+ triangle = tri_mesh.triangle(1)
105
+ assert_equal(Vector3.new(2,0,0), triangle.vertices[0])
106
+ assert_equal(Vector3.new(2,2,0), triangle.vertices[1])
107
+ assert_equal(Vector3.new(0,2,0), triangle.vertices[2])
108
+
109
+ triangle = tri_mesh.triangle(3)
110
+ assert_equal(nil, triangle)
111
+ end
112
+
113
+ def test_from_triangles
114
+ tris = Array.new(8)
115
+ tris[0] = Triangle.new( Vector3.new(0,0,0), Vector3.new(1,0,0), Vector3.new(0,1,1) )
116
+ tris[1] = Triangle.new( Vector3.new(1,0,0), Vector3.new(1,1,1), Vector3.new(0,1,1) )
117
+ tris[2] = Triangle.new( Vector3.new(1,0,0), Vector3.new(2,0,0), Vector3.new(1,1,1) )
118
+ tris[3] = Triangle.new( Vector3.new(2,0,0), Vector3.new(2,1,1), Vector3.new(1,1,1) )
119
+ tris[4] = Triangle.new( Vector3.new(0,1,1), Vector3.new(1,1,1), Vector3.new(0,2,2) )
120
+ tris[5] = Triangle.new( Vector3.new(1,1,1), Vector3.new(1,2,2), Vector3.new(0,2,2) )
121
+ tris[6] = Triangle.new( Vector3.new(1,1,1), Vector3.new(2,1,1), Vector3.new(1,2,2) )
122
+ tris[7] = Triangle.new( Vector3.new(2,1,1), Vector3.new(2,2,2), Vector3.new(1,2,2) )
123
+ trimesh_from_tris = TriMesh::from_triangles(tris)
124
+ assert_equal( 9, trimesh_from_tris.vertices.size)
125
+ assert_equal( 8, trimesh_from_tris.tri_indices.size)
126
+ end
127
+
128
+ def test_from_convex_polyline
129
+ vertices = Array.new(6)
130
+ vertices[0] = Vector3.new(1,0,0)
131
+ vertices[1] = Vector3.new(2,0,0)
132
+ vertices[2] = Vector3.new(3,1,0)
133
+ vertices[3] = Vector3.new(2,2,0)
134
+ vertices[4] = Vector3.new(1,2,0)
135
+ vertices[5] = Vector3.new(0,1,0)
136
+ polyline_closed = Polyline.new( vertices, false ) # closed Polyline
137
+ polyline_open = Polyline.new( vertices, true ) # open Polyline
138
+ trimesh_from_convex_polyline1 = TriMesh.from_convex_polyline( polyline_closed )
139
+ trimesh_from_convex_polyline2 = TriMesh.from_convex_polyline( polyline_open )
140
+
141
+ assert_equal(4, trimesh_from_convex_polyline1.area)
142
+ assert_equal(4, trimesh_from_convex_polyline2.area)
143
+ end
144
+
145
+ def test_from_extruded_polyline
146
+ vertices = Array.new(6)
147
+ vertices[0] = Vector3.new(1,0,0)
148
+ vertices[1] = Vector3.new(2,0,0)
149
+ vertices[2] = Vector3.new(3,1,0)
150
+ vertices[3] = Vector3.new(2,2,0)
151
+ vertices[4] = Vector3.new(1,2,0)
152
+ vertices[5] = Vector3.new(0,1,0)
153
+ polyline_closed = Polyline.new( vertices, false ) # closed Polyline
154
+ polyline_open = Polyline.new( vertices, true ) # open Polyline
155
+ extrude_direction = Vector3.new(0,0,2)
156
+ trimesh_from_extruded_polyline1 = TriMesh.from_extrude_polyline( polyline_closed , extrude_direction )
157
+ trimesh_from_extruded_polyline2 = TriMesh.from_extrude_polyline( polyline_open , extrude_direction )
158
+
159
+ assert_in_delta(4+8*(Math.sqrt(2)), trimesh_from_extruded_polyline1.area, 1e-10)
160
+ assert_in_delta(4+6*(Math.sqrt(2)), trimesh_from_extruded_polyline2.area, 1e-10)
161
+ end
162
+
163
+ def test_area
164
+ box_mesh = get_box_mesh()
165
+ assert_equal(94, box_mesh.area)
166
+ end
167
+ end
@@ -21,6 +21,36 @@ class TriangleTestCase < MiniTest::Unit::TestCase
21
21
  assert_equal(Vector3.new(0,1,0), @triangle_default.vertices[2])
22
22
  end
23
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
+
24
54
  def test_to_s
25
55
  assert_equal("Triangle[[1, 2, 2], [1, 4, 2], [-1, 3, 0]]", @triangle.to_s)
26
56
  end
@@ -60,6 +90,13 @@ class TriangleTestCase < MiniTest::Unit::TestCase
60
90
  assert_equal( Vector3.new(0,0,1) ,@triangle_default.normal )
61
91
  end
62
92
 
93
+ def test_reverse
94
+ current_normal = @triangle.normal()
95
+ reversed_normal = @triangle.reverse().normal()
96
+ assert( current_normal.parallel?(reversed_normal) )
97
+ assert( !current_normal.same_direction?(reversed_normal) )
98
+ end
99
+
63
100
  def test_center
64
101
  center = @triangle.center
65
102
  assert_in_delta( 0.333333333333333, center.x, @triangle.tolerance)
data/test/test_vector3.rb CHANGED
@@ -54,14 +54,57 @@ class Vector3TestCase < MiniTest::Unit::TestCase
54
54
  @vector.x = "this is a pen"
55
55
  end
56
56
 
57
+ def test_clone
58
+ vec1 = Vector3.new(1,2,3)
59
+ vec1_shallow_copy = vec1
60
+ vec1_shallow_copy.x = 4
61
+ assert_equal( 4, vec1.x )
62
+ assert_equal( 2, vec1.y )
63
+ assert_equal( 3, vec1.z )
64
+
65
+ vec1_deep_copy = vec1.clone
66
+ vec1_deep_copy.x = 6
67
+ assert_equal( 4, vec1.x )
68
+ assert_equal( 6, vec1_deep_copy.x )
69
+
70
+ vec_ary = [Vector3.new(1,2,3), Vector3.new(4,5,6)]
71
+ vec_ary_shallow = vec_ary
72
+ vec_ary_shallow[0].x = 4
73
+ vec_ary_shallow[1] = Vector3.new(7,8,9)
74
+ vec_ary_shallow.push(Vector3.new(10,11,12))
75
+ assert_equal(4, vec_ary_shallow[0].x)
76
+ assert_equal(Vector3.new(7,8,9), vec_ary_shallow[1])
77
+ assert_equal(3, vec_ary_shallow.size)
78
+ assert_equal(4, vec_ary[0].x)
79
+ assert_equal(Vector3.new(7,8,9), vec_ary[1])
80
+ assert_equal(3, vec_ary.size)
81
+
82
+ vec_ary = [Vector3.new(1,2,3), Vector3.new(4,5,6)]
83
+ vec_ary_deep = vec_ary.clone
84
+ vec_ary_deep[0].x = 4
85
+ vec_ary_deep[1] = Vector3.new(7,8,9)
86
+ vec_ary_deep.push(Vector3.new(10,11,12))
87
+ assert_equal(4, vec_ary_deep[0].x)
88
+ assert_equal(Vector3.new(7,8,9), vec_ary_deep[1])
89
+ assert_equal(3, vec_ary_deep.size)
90
+
91
+ # Array.clone does not call element.clone
92
+ # assert_equal(1, vec_ary[0].x)
93
+ assert_equal(4, vec_ary[0].x)
94
+ assert_equal(Vector3.new(4,5,6), vec_ary[1])
95
+ assert_equal(2, vec_ary.size)
96
+ end
97
+
57
98
  def test_equals
58
99
  assert(!(@vector_init_zero == @vector))
59
100
  assert(@vector_init_zero != @vector)
60
101
 
61
102
  assert(@vector == @vector)
103
+ assert(@vector.eql?(@vector))
62
104
 
63
105
  vector = Vector3.new(1,2,3)
64
106
  assert(@vector == vector)
107
+ assert(@vector.eql?(vector))
65
108
 
66
109
  # Floating error check
67
110
  floatingError = Geom.default_tolerance*0.1
@@ -79,6 +122,28 @@ class Vector3TestCase < MiniTest::Unit::TestCase
79
122
  assert(@vector != -4)
80
123
  end
81
124
 
125
+ def test_equ_hash
126
+ # in case use Vector3 as Key of hash...
127
+ # Vector3#eql? and Vector3#hash should be implement
128
+ hash = Hash.new()
129
+ vec = Vector3.new(1,2,3)
130
+ hash[vec] = 1
131
+ assert_equal(1, hash.keys.count)
132
+ assert_equal(1, hash[vec])
133
+
134
+ hash[vec] = 2
135
+ assert_equal(1, hash.keys.count)
136
+ assert_equal(2, hash[vec])
137
+
138
+ hash[Vector3.new(3,2,1)] = 3
139
+ assert_equal(2, hash.keys.count)
140
+ assert_equal(2, hash[vec])
141
+
142
+ hash[@vector] = 3
143
+ assert_equal(2, hash.keys.count)
144
+ assert_equal(3, hash[vec])
145
+ end
146
+
82
147
  def test_add
83
148
  vector = Vector3.new(4,5,6)
84
149
  addedVector = vector + @vector
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: gmath3D
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.2.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,12 +9,12 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2011-10-19 00:00:00.000000000 +09:00
12
+ date: 2011-10-25 00:00:00.000000000 +09:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: bundler
17
- requirement: &2156099140 !ruby/object:Gem::Requirement
17
+ requirement: &2152196520 !ruby/object:Gem::Requirement
18
18
  none: false
19
19
  requirements:
20
20
  - - ~>
@@ -22,10 +22,10 @@ dependencies:
22
22
  version: 1.0.0
23
23
  type: :development
24
24
  prerelease: false
25
- version_requirements: *2156099140
25
+ version_requirements: *2152196520
26
26
  - !ruby/object:Gem::Dependency
27
27
  name: jeweler
28
- requirement: &2156097640 !ruby/object:Gem::Requirement
28
+ requirement: &2152195120 !ruby/object:Gem::Requirement
29
29
  none: false
30
30
  requirements:
31
31
  - - ~>
@@ -33,7 +33,7 @@ dependencies:
33
33
  version: 1.6.4
34
34
  type: :development
35
35
  prerelease: false
36
- version_requirements: *2156097640
36
+ version_requirements: *2152195120
37
37
  description: This library defines 3D geometric elements(point, line, plane etc..).
38
38
  It can get two(or more) elements relation, like distance between two elements.
39
39
  email: toshi0328@gmail.com
@@ -58,8 +58,10 @@ files:
58
58
  - lib/line.rb
59
59
  - lib/matrix_util.rb
60
60
  - lib/plane.rb
61
+ - lib/polyline.rb
61
62
  - lib/quat.rb
62
63
  - lib/rectangle.rb
64
+ - lib/tri_mesh.rb
63
65
  - lib/triangle.rb
64
66
  - lib/util.rb
65
67
  - lib/vector3.rb
@@ -70,8 +72,10 @@ files:
70
72
  - test/test_line.rb
71
73
  - test/test_matrix_util.rb
72
74
  - test/test_plane.rb
75
+ - test/test_polyline.rb
73
76
  - test/test_quat.rb
74
77
  - test/test_rectangle.rb
78
+ - test/test_tri_mesh.rb
75
79
  - test/test_triangle.rb
76
80
  - test/test_util.rb
77
81
  - test/test_vector3.rb
@@ -91,7 +95,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
91
95
  version: '0'
92
96
  segments:
93
97
  - 0
94
- hash: 3561879194129992333
98
+ hash: -2118171931574002143
95
99
  required_rubygems_version: !ruby/object:Gem::Requirement
96
100
  none: false
97
101
  requirements: