gmath3D 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.document +5 -0
- data/Gemfile +11 -0
- data/Gemfile.lock +16 -0
- data/LICENSE.txt +20 -0
- data/README.rdoc +19 -0
- data/Rakefile +45 -0
- data/VERSION +1 -0
- data/gmath3D.gemspec +71 -0
- data/lib/box.rb +74 -0
- data/lib/finite_line.rb +157 -0
- data/lib/geom.rb +20 -0
- data/lib/gmath3D.rb +11 -0
- data/lib/line.rb +80 -0
- data/lib/plane.rb +86 -0
- data/lib/rectangle.rb +89 -0
- data/lib/triangle.rb +133 -0
- data/lib/util.rb +41 -0
- data/lib/vector3.rb +116 -0
- data/test/helper.rb +15 -0
- data/test/test_box.rb +90 -0
- data/test/test_finite_line.rb +245 -0
- data/test/test_geom.rb +14 -0
- data/test/test_gmath3D.rb +15 -0
- data/test/test_line.rb +105 -0
- data/test/test_plane.rb +160 -0
- data/test/test_rectangle.rb +123 -0
- data/test/test_triangle.rb +143 -0
- data/test/test_util.rb +54 -0
- data/test/test_vector3.rb +319 -0
- metadata +104 -0
@@ -0,0 +1,143 @@
|
|
1
|
+
include GMath3D
|
2
|
+
|
3
|
+
MiniTest::Unit.autorun
|
4
|
+
|
5
|
+
class TriangleTestCase < MiniTest::Unit::TestCase
|
6
|
+
def setup
|
7
|
+
@triangle = Triangle.new( Vector3.new(1,2,2), Vector3.new(1,4,2), Vector3.new(-1,3,0))
|
8
|
+
@triangle_default = Triangle.new()
|
9
|
+
end
|
10
|
+
|
11
|
+
def test_initialize
|
12
|
+
assert_equal(Vector3.new(1,2,2), @triangle.vertices[0])
|
13
|
+
assert_equal(Vector3.new(1,4,2), @triangle.vertices[1])
|
14
|
+
assert_equal(Vector3.new(-1,3,0), @triangle.vertices[2])
|
15
|
+
|
16
|
+
assert_equal(Vector3.new(0,0,0), @triangle_default.vertices[0])
|
17
|
+
assert_equal(Vector3.new(1,0,0), @triangle_default.vertices[1])
|
18
|
+
assert_equal(Vector3.new(0,1,0), @triangle_default.vertices[2])
|
19
|
+
end
|
20
|
+
|
21
|
+
def test_point
|
22
|
+
assert_equal(@triangle.vertices[0], @triangle.point( Array.new([1,0,0])))
|
23
|
+
assert_equal(@triangle.vertices[1], @triangle.point( Array.new([0,1,0])))
|
24
|
+
assert_equal(@triangle.vertices[2], @triangle.point( Array.new([0,0,1])))
|
25
|
+
|
26
|
+
#on edge
|
27
|
+
assert_equal(Vector3.new(1,3,2), @triangle.point( Array.new([0.5,0.5,0])))
|
28
|
+
assert_equal(Vector3.new(0,3.5,1), @triangle.point( Array.new([0,0.5,0.5])))
|
29
|
+
assert_equal(Vector3.new(0,2.5,1), @triangle.point( Array.new([0.5,0,0.5])))
|
30
|
+
|
31
|
+
#inside
|
32
|
+
assert_equal(Vector3.new(0, 3, 1), @triangle.point( Array.new([0.25,0.25,0.5])))
|
33
|
+
assert_equal(@triangle.center, @triangle.point( Array.new([1.0/3.0,1.0/3.0,1.0/3.0])))
|
34
|
+
end
|
35
|
+
|
36
|
+
def test_edges
|
37
|
+
edges = @triangle.edges
|
38
|
+
assert_equal(@triangle.vertices[0], edges[0].start_point)
|
39
|
+
assert_equal(@triangle.vertices[1], edges[0].end_point)
|
40
|
+
assert_equal(@triangle.vertices[1], edges[1].start_point)
|
41
|
+
assert_equal(@triangle.vertices[2], edges[1].end_point)
|
42
|
+
assert_equal(@triangle.vertices[2], edges[2].start_point)
|
43
|
+
assert_equal(@triangle.vertices[0], edges[2].end_point)
|
44
|
+
end
|
45
|
+
|
46
|
+
def test_area
|
47
|
+
assert_in_delta(Math::sqrt(8), @triangle.area, @triangle.tolerance)
|
48
|
+
assert_in_delta(0.5, @triangle_default.area, @triangle_default.tolerance)
|
49
|
+
end
|
50
|
+
|
51
|
+
def test_normal
|
52
|
+
assert_equal( Vector3.new(-1,0,1).normalize(), @triangle.normal )
|
53
|
+
assert_equal( Vector3.new(0,0,1) ,@triangle_default.normal )
|
54
|
+
end
|
55
|
+
|
56
|
+
def test_center
|
57
|
+
center = @triangle.center
|
58
|
+
assert_in_delta( 0.333333333333333, center.x, @triangle.tolerance)
|
59
|
+
assert_in_delta( 3.0, center.y, @triangle.tolerance)
|
60
|
+
assert_in_delta( 1.333333333333333, center.z, @triangle.tolerance)
|
61
|
+
|
62
|
+
barycentric_coordinate = @triangle.barycentric_coordinate(center)
|
63
|
+
assert_in_delta( 1.0/3.0, barycentric_coordinate[0], @triangle.tolerance)
|
64
|
+
assert_in_delta( 1.0/3.0, barycentric_coordinate[1], @triangle.tolerance)
|
65
|
+
assert_in_delta( 1.0/3.0, barycentric_coordinate[2], @triangle.tolerance)
|
66
|
+
end
|
67
|
+
|
68
|
+
def test_barycentric_coordinate
|
69
|
+
# on vertex
|
70
|
+
coord = @triangle.barycentric_coordinate( @triangle.vertices[1])
|
71
|
+
assert_equal( 3, coord.size )
|
72
|
+
assert_in_delta( 0, coord[0], @triangle.tolerance)
|
73
|
+
assert_in_delta( 1, coord[1], @triangle.tolerance)
|
74
|
+
assert_in_delta( 0, coord[2], @triangle.tolerance)
|
75
|
+
|
76
|
+
# on edge
|
77
|
+
coord = @triangle.barycentric_coordinate( Vector3.new(1,3,2) )
|
78
|
+
assert_in_delta( 0.5, coord[0], @triangle.tolerance)
|
79
|
+
assert_in_delta( 0.5, coord[1], @triangle.tolerance)
|
80
|
+
assert_in_delta( 0, coord[2], @triangle.tolerance)
|
81
|
+
|
82
|
+
# inside case
|
83
|
+
coord = @triangle.barycentric_coordinate( Vector3.new(0,3,1) )
|
84
|
+
assert_in_delta( 1.0, coord[0] + coord[1] + coord[2], @triangle.tolerance)
|
85
|
+
assert_in_delta( 0.25, coord[0], @triangle.tolerance)
|
86
|
+
assert_in_delta( 0.25, coord[1], @triangle.tolerance)
|
87
|
+
assert_in_delta( 0.5, coord[2], @triangle.tolerance)
|
88
|
+
|
89
|
+
# outside case
|
90
|
+
coord = @triangle_default.barycentric_coordinate( Vector3.new(2,0,0) )
|
91
|
+
assert_in_delta( -1.0, coord[0], @triangle.tolerance)
|
92
|
+
assert_in_delta( 2.0, coord[1], @triangle.tolerance)
|
93
|
+
assert_in_delta( 0.0, coord[2], @triangle.tolerance)
|
94
|
+
|
95
|
+
# outside case
|
96
|
+
coord = @triangle.barycentric_coordinate( Vector3.new(3,3,4) )
|
97
|
+
assert_in_delta( 1.0, coord[0], @triangle.tolerance)
|
98
|
+
assert_in_delta( 1.0, coord[1], @triangle.tolerance)
|
99
|
+
assert_in_delta( -1.0, coord[2], @triangle.tolerance)
|
100
|
+
|
101
|
+
#invalid argument
|
102
|
+
assert_raises ArgumentError do
|
103
|
+
coord = @triangle.barycentric_coordinate( 3 )
|
104
|
+
end
|
105
|
+
assert_raises ArgumentError do
|
106
|
+
coord = @triangle.barycentric_coordinate( nil )
|
107
|
+
end
|
108
|
+
end
|
109
|
+
|
110
|
+
def test_contains
|
111
|
+
check_point = @triangle.center + @triangle.normal*3.0
|
112
|
+
assert( !@triangle.contains( check_point) )
|
113
|
+
assert( @triangle.contains( @triangle.center) )
|
114
|
+
assert( @triangle.contains( Vector3.new( 1,3,2)))
|
115
|
+
assert( @triangle.contains( Vector3.new(-1,3,0)))
|
116
|
+
assert( @triangle_default.contains( Vector3.new( 0.5, 0.5, 0.0 ) ))
|
117
|
+
assert( !@triangle_default.contains( Vector3.new( -1.0, 2.0, 0.0 )))
|
118
|
+
assert( !@triangle_default.contains( Vector3.new( 1.0, 1.0, 0.0 )))
|
119
|
+
end
|
120
|
+
|
121
|
+
def test_distance_to_point
|
122
|
+
# on inside
|
123
|
+
check_point = @triangle.center + @triangle.normal*3.0
|
124
|
+
distance, point_on_triangle = @triangle.distance(check_point)
|
125
|
+
assert_in_delta(3.0, distance, @triangle.tolerance)
|
126
|
+
assert_equal(@triangle.center, point_on_triangle)
|
127
|
+
|
128
|
+
# on vertex
|
129
|
+
distance, point_on_triangle = @triangle_default.distance(Vector3.new(-1,-1,0.5))
|
130
|
+
assert_in_delta(Math::sqrt(2.25), distance, @triangle_default.tolerance)
|
131
|
+
assert_equal(@triangle_default.vertices[0], point_on_triangle)
|
132
|
+
|
133
|
+
# on edge
|
134
|
+
distance, point_on_triangle = @triangle.distance( Vector3.new(2,3,2))
|
135
|
+
assert_in_delta(1, distance, @triangle_default.tolerance)
|
136
|
+
assert_equal(Vector3.new(1,3,2), point_on_triangle)
|
137
|
+
|
138
|
+
# contains to inside
|
139
|
+
distance, point_on_triangle = @triangle_default.distance(@triangle_default.center)
|
140
|
+
assert_in_delta(0, distance, @triangle_default.tolerance)
|
141
|
+
assert_equal(@triangle_default.center, point_on_triangle)
|
142
|
+
end
|
143
|
+
end
|
data/test/test_util.rb
ADDED
@@ -0,0 +1,54 @@
|
|
1
|
+
include GMath3D
|
2
|
+
|
3
|
+
MiniTest::Unit.autorun
|
4
|
+
|
5
|
+
class UtilTestCase < MiniTest::Unit::TestCase
|
6
|
+
def test_check_arg_type
|
7
|
+
integerInstance = 1
|
8
|
+
floatInstance = 3.1415
|
9
|
+
stringInstance = 'string'
|
10
|
+
|
11
|
+
# no exception raise
|
12
|
+
Util.check_arg_type(Integer, integerInstance)
|
13
|
+
Util.check_arg_type(Float, floatInstance)
|
14
|
+
Util.check_arg_type(String, stringInstance)
|
15
|
+
|
16
|
+
# exception raise
|
17
|
+
assert_raises ArgumentError do
|
18
|
+
Util.check_arg_type(Integer, floatInstance)
|
19
|
+
end
|
20
|
+
assert_raises ArgumentError do
|
21
|
+
Util.check_arg_type(Integer, stringInstance)
|
22
|
+
end
|
23
|
+
assert_raises ArgumentError do
|
24
|
+
Util.check_arg_type(Float, integerInstance)
|
25
|
+
end
|
26
|
+
assert_raises ArgumentError do
|
27
|
+
Util.check_arg_type(Float, stringInstance)
|
28
|
+
end
|
29
|
+
assert_raises ArgumentError do
|
30
|
+
Util.check_arg_type(String, integerInstance)
|
31
|
+
end
|
32
|
+
assert_raises ArgumentError do
|
33
|
+
Util.check_arg_type(String, floatInstance)
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
def test_array_methods
|
38
|
+
point_ary = Array.new([Vector3.new(2,4,3),Vector3.new(-2,2.5,8),Vector3.new(9,0,-3)])
|
39
|
+
assert_equal( Vector3.new(9, 6.5, 8), point_ary.sum)
|
40
|
+
assert_equal( Vector3.new(3, 6.5/3, 8/3.0), point_ary.avg)
|
41
|
+
end
|
42
|
+
|
43
|
+
def test_matrix_equation_solving
|
44
|
+
mat = Matrix[[1,2,3],[4,5,6],[7,8,-9]]
|
45
|
+
vec = Matrix.column_vector([26,62,8])
|
46
|
+
inv_mat = mat.inverse
|
47
|
+
ans = inv_mat*vec
|
48
|
+
assert_equal(3, ans.row_size)
|
49
|
+
assert_equal(1, ans.column_size)
|
50
|
+
assert_equal(3, ans[0,0])
|
51
|
+
assert_equal(4, ans[1,0])
|
52
|
+
assert_equal(5, ans[2,0])
|
53
|
+
end
|
54
|
+
end
|
@@ -0,0 +1,319 @@
|
|
1
|
+
include GMath3D
|
2
|
+
|
3
|
+
MiniTest::Unit.autorun
|
4
|
+
|
5
|
+
class Vector3TestCase < MiniTest::Unit::TestCase
|
6
|
+
def setup
|
7
|
+
@vector_init_zero = Vector3.new()
|
8
|
+
@vector = Vector3.new(1.0, 2.0, 3.0)
|
9
|
+
end
|
10
|
+
|
11
|
+
def test_initalize
|
12
|
+
assert_equal(0, @vector_init_zero.x)
|
13
|
+
assert_equal(0, @vector_init_zero.y)
|
14
|
+
assert_equal(0, @vector_init_zero.z)
|
15
|
+
|
16
|
+
assert_equal(1.0, @vector.x)
|
17
|
+
assert_equal(2.0, @vector.y)
|
18
|
+
assert_equal(3.0, @vector.z)
|
19
|
+
|
20
|
+
assert_equal(Geom.default_tolerance, @vector.tolerance)
|
21
|
+
end
|
22
|
+
|
23
|
+
def test_assign_value
|
24
|
+
assert_equal(1.0, @vector.x)
|
25
|
+
assert_equal(2.0, @vector.y)
|
26
|
+
assert_equal(3.0, @vector.z)
|
27
|
+
|
28
|
+
@vector.x = 2.0
|
29
|
+
@vector.y *= 2.0
|
30
|
+
@vector.z -= 3.0
|
31
|
+
|
32
|
+
assert_equal(2.0, @vector.x)
|
33
|
+
assert_equal(4.0, @vector.y)
|
34
|
+
assert_equal(0.0, @vector.z)
|
35
|
+
end
|
36
|
+
|
37
|
+
def test_assign_invalid_value
|
38
|
+
#TODO unimplemented!
|
39
|
+
@vector.x = "this is a pen"
|
40
|
+
end
|
41
|
+
|
42
|
+
def test_equals
|
43
|
+
assert(!(@vector_init_zero == @vector))
|
44
|
+
assert(@vector_init_zero != @vector)
|
45
|
+
|
46
|
+
assert(@vector == @vector)
|
47
|
+
|
48
|
+
vector = Vector3.new(1,2,3)
|
49
|
+
assert(@vector == vector)
|
50
|
+
|
51
|
+
# Floating error check
|
52
|
+
floatingError = Geom.default_tolerance*0.1
|
53
|
+
vector = Vector3.new(1.0 - floatingError, 2.0 + floatingError, 3.0)
|
54
|
+
assert(@vector == vector)
|
55
|
+
|
56
|
+
floatingError2 = Geom.default_tolerance*10.0
|
57
|
+
vector = Vector3.new(1.0 - floatingError2, 2.0 + floatingError2, 3.0)
|
58
|
+
assert(@vector != vector)
|
59
|
+
|
60
|
+
#invlid value comparison
|
61
|
+
assert(@vector != "string")
|
62
|
+
assert(@vector != -4)
|
63
|
+
end
|
64
|
+
|
65
|
+
def test_add
|
66
|
+
vector = Vector3.new(4,5,6)
|
67
|
+
addedVector = vector + @vector
|
68
|
+
|
69
|
+
assert_equal(5.0, addedVector.x)
|
70
|
+
assert_equal(7.0, addedVector.y)
|
71
|
+
assert_equal(9.0, addedVector.z)
|
72
|
+
|
73
|
+
addedVector = @vector + vector
|
74
|
+
|
75
|
+
assert_equal(5.0, addedVector.x)
|
76
|
+
assert_equal(7.0, addedVector.y)
|
77
|
+
assert_equal(9.0, addedVector.z)
|
78
|
+
end
|
79
|
+
def test_add_invalid_value
|
80
|
+
assert_raises ArgumentError do
|
81
|
+
invalidResult = @vector + 5
|
82
|
+
end
|
83
|
+
assert_raises ArgumentError do
|
84
|
+
invalidResult = @vector + nil
|
85
|
+
end
|
86
|
+
end
|
87
|
+
|
88
|
+
def test_subtract
|
89
|
+
vector = Vector3.new(4,5,6)
|
90
|
+
subtractedVector = vector - @vector
|
91
|
+
|
92
|
+
assert_equal(3.0, subtractedVector.x)
|
93
|
+
assert_equal(3.0, subtractedVector.y)
|
94
|
+
assert_equal(3.0, subtractedVector.z)
|
95
|
+
|
96
|
+
subtractedVector = @vector - vector
|
97
|
+
|
98
|
+
assert_equal(-3.0, subtractedVector.x)
|
99
|
+
assert_equal(-3.0, subtractedVector.y)
|
100
|
+
assert_equal(-3.0, subtractedVector.z)
|
101
|
+
end
|
102
|
+
def test_subtract_invalid_value
|
103
|
+
assert_raises ArgumentError do
|
104
|
+
invalidResult = @vector - 5
|
105
|
+
end
|
106
|
+
assert_raises ArgumentError do
|
107
|
+
invalidResult = @vector - nil
|
108
|
+
end
|
109
|
+
end
|
110
|
+
|
111
|
+
def test_multiply
|
112
|
+
multipliedVector = @vector * 5
|
113
|
+
|
114
|
+
assert_equal( 5.0, multipliedVector.x)
|
115
|
+
assert_equal(10.0, multipliedVector.y)
|
116
|
+
assert_equal(15.0, multipliedVector.z)
|
117
|
+
|
118
|
+
multipliedVector = @vector * -3.0
|
119
|
+
|
120
|
+
assert_equal(-3.0, multipliedVector.x)
|
121
|
+
assert_equal(-6.0, multipliedVector.y)
|
122
|
+
assert_equal(-9.0, multipliedVector.z)
|
123
|
+
end
|
124
|
+
def test_multiply_invalid_value
|
125
|
+
assert_raises ArgumentError do
|
126
|
+
invalidResult = @vector * @vector
|
127
|
+
end
|
128
|
+
assert_raises ArgumentError do
|
129
|
+
invalidResult = @vector * nil
|
130
|
+
end
|
131
|
+
end
|
132
|
+
|
133
|
+
def test_divide
|
134
|
+
dividedVector = @vector / 5
|
135
|
+
|
136
|
+
assert_equal(1.0/5.0, dividedVector.x)
|
137
|
+
assert_equal(2.0/5.0, dividedVector.y)
|
138
|
+
assert_equal(3.0/5.0, dividedVector.z)
|
139
|
+
|
140
|
+
dividedVector = @vector / -3.0
|
141
|
+
|
142
|
+
assert_equal(-1.0/3.0, dividedVector.x)
|
143
|
+
assert_equal(-2.0/3.0, dividedVector.y)
|
144
|
+
assert_equal(-3.0/3.0, dividedVector.z)
|
145
|
+
end
|
146
|
+
def test_divide_invalid_value
|
147
|
+
assert_raises ArgumentError do
|
148
|
+
invalidResult = @vector / @vector
|
149
|
+
end
|
150
|
+
assert_raises ArgumentError do
|
151
|
+
invalidResult = @vector / nil
|
152
|
+
end
|
153
|
+
end
|
154
|
+
|
155
|
+
def test_dot
|
156
|
+
vector = Vector3.new(2.0, 4.0, 6.0)
|
157
|
+
innerProduct = @vector.dot(vector)
|
158
|
+
assert_in_delta( 1.0*2.0 + 2.0*4.0 + 3.0*6.0, innerProduct, @vector.tolerance)
|
159
|
+
|
160
|
+
innerProduct = @vector.dot(@vector_init_zero)
|
161
|
+
assert_in_delta( 0.0, innerProduct, @vector.tolerance)
|
162
|
+
end
|
163
|
+
def test_dot_invalid_value
|
164
|
+
assert_raises ArgumentError do
|
165
|
+
invalidResult = @vector.dot(5.0)
|
166
|
+
end
|
167
|
+
assert_raises ArgumentError do
|
168
|
+
invalidResult = @vector.dot(nil)
|
169
|
+
end
|
170
|
+
end
|
171
|
+
|
172
|
+
def test_cross
|
173
|
+
outerProduct = Vector3.new(1.0,0.0,0.0).cross(Vector3.new(0.0,1.0,0.0))
|
174
|
+
assert(outerProduct == Vector3.new(0.0,0.0,1.0))
|
175
|
+
|
176
|
+
vector = Vector3.new(1.0, 1.0, 4.0)
|
177
|
+
outerProduct = @vector.cross(vector)
|
178
|
+
assert_in_delta(2.0*4.0 - 3.0*1.0, outerProduct.x, @vector.tolerance)
|
179
|
+
assert_in_delta(3.0*1.0 - 1.0*4.0, outerProduct.y, @vector.tolerance)
|
180
|
+
assert_in_delta(1.0*1.0 - 2.0*1.0, outerProduct.z, @vector.tolerance)
|
181
|
+
|
182
|
+
outerProduct = @vector.cross(@vector_init_zero)
|
183
|
+
assert_in_delta( 0.0, outerProduct.x, @vector.tolerance)
|
184
|
+
assert_in_delta( 0.0, outerProduct.y, @vector.tolerance)
|
185
|
+
assert_in_delta( 0.0, outerProduct.z, @vector.tolerance)
|
186
|
+
end
|
187
|
+
def test_cross_invalid_value
|
188
|
+
assert_raises ArgumentError do
|
189
|
+
invalidResult = @vector.cross(5.0)
|
190
|
+
end
|
191
|
+
assert_raises ArgumentError do
|
192
|
+
invalidResult = @vector.cross(nil)
|
193
|
+
end
|
194
|
+
end
|
195
|
+
|
196
|
+
def test_length
|
197
|
+
assert_in_delta(0.0, @vector_init_zero.length, @vector_init_zero.tolerance)
|
198
|
+
assert_in_delta( Math::sqrt(1.0*1.0+2.0*2.0+3.0*3.0), @vector.length, @vector.tolerance)
|
199
|
+
vector = Vector3.new(3.0, 4.0, 0.0)
|
200
|
+
assert_in_delta( 5.0, vector.length, @vector.tolerance)
|
201
|
+
end
|
202
|
+
|
203
|
+
def test_distance
|
204
|
+
point1 = Vector3.new(1.0, 3.0, -5.0)
|
205
|
+
point2 = Vector3.new(3.0, -5.5, 2.2)
|
206
|
+
vector1to2 = point2 - point1
|
207
|
+
assert_in_delta( vector1to2.length, point2.distance(point1), vector1to2.tolerance)
|
208
|
+
assert_in_delta( vector1to2.length, point1.distance(point2), vector1to2.tolerance)
|
209
|
+
assert_in_delta( 0.0, point1.distance(point1), vector1to2.tolerance)
|
210
|
+
end
|
211
|
+
def test_distance_invalid_value
|
212
|
+
assert_raises ArgumentError do
|
213
|
+
invalidResult = @vector.distance(5.0)
|
214
|
+
end
|
215
|
+
assert_raises ArgumentError do
|
216
|
+
invalidResult = @vector.distance(nil)
|
217
|
+
end
|
218
|
+
end
|
219
|
+
|
220
|
+
def test_angle
|
221
|
+
vector1 = Vector3.new( 1.0 , 0.0, 0.0 )
|
222
|
+
vector2 = Vector3.new( 1.0 , 1.0, 0.0 )
|
223
|
+
vector3 = Vector3.new( -1.0 , 1.0, 0.0 )
|
224
|
+
vector4 = Vector3.new( -1.0 , 0.0, 0.0 )
|
225
|
+
vector5 = Vector3.new( -1.0 , -1.0, 0.0 )
|
226
|
+
vector6 = Vector3.new( 1.0 , -1.0, 0.0 )
|
227
|
+
|
228
|
+
# result should be between 0 and PI.
|
229
|
+
assert_in_delta(0.0,vector1.angle(vector1), vector1.tolerance) ;
|
230
|
+
assert_in_delta(Math::PI/4.0,vector1.angle(vector2), vector1.tolerance) ;
|
231
|
+
assert_in_delta(Math::PI*3.0/4.0, vector1.angle(vector3), vector1.tolerance) ;
|
232
|
+
assert_in_delta(Math::PI, vector1.angle(vector4), vector1.tolerance) ;
|
233
|
+
assert_in_delta(Math::PI*3.0/4.0, vector1.angle(vector5), vector1.tolerance) ;
|
234
|
+
assert_in_delta(Math::PI/4.0, vector1.angle(vector6), vector1.tolerance) ;
|
235
|
+
end
|
236
|
+
def test_angle_invalid_value
|
237
|
+
assert_raises ArgumentError do
|
238
|
+
invalidResult = @vector.angle(5.0)
|
239
|
+
end
|
240
|
+
assert_raises ArgumentError do
|
241
|
+
invalidResult = @vector.angle(nil)
|
242
|
+
end
|
243
|
+
end
|
244
|
+
|
245
|
+
def test_normalize
|
246
|
+
normalized = @vector.normalize()
|
247
|
+
assert_in_delta(1.0, normalized.length, normalized.tolerance) ;
|
248
|
+
assert(normalized.same_direction?(@vector))
|
249
|
+
end
|
250
|
+
|
251
|
+
def test_parallel
|
252
|
+
vectorZero = Vector3.new( 0.0, 0.0, 0.0) ;
|
253
|
+
vector1 = Vector3.new( 1.0, 2.0, 3.0 ) ;
|
254
|
+
vector2 = Vector3.new( 1.0, 1.0, 4.0 ) ;
|
255
|
+
vector3 = Vector3.new( 2.0, 4.0, 6.0 ) ;
|
256
|
+
vector4 = Vector3.new( -1.0, -2.0, -3.0 ) ;
|
257
|
+
vector5 = Vector3.new( -3.0, -6.0, -9.0 ) ;
|
258
|
+
|
259
|
+
assert(!vector1.parallel?(vectorZero)) ;
|
260
|
+
assert(!vectorZero.parallel?(vector2)) ;
|
261
|
+
assert(!vector1.parallel?(vector2)) ;
|
262
|
+
|
263
|
+
assert(vector1.parallel?(vector3)) ;
|
264
|
+
assert(vector1.parallel?(vector4)) ;
|
265
|
+
assert(vector1.parallel?(vector5)) ;
|
266
|
+
end
|
267
|
+
|
268
|
+
def test_same_direction
|
269
|
+
vectorZero = Vector3.new( 0.0, 0.0, 0.0) ;
|
270
|
+
vector1 = Vector3.new( 1.0, 2.0, 3.0 ) ;
|
271
|
+
vector2 = Vector3.new( 1.0, 1.0, 4.0 ) ;
|
272
|
+
vector3 = Vector3.new( 2.0, 4.0, 6.0 ) ;
|
273
|
+
vector4 = Vector3.new( -1.0, -2.0, -3.0 ) ;
|
274
|
+
vector5 = Vector3.new( -3.0, -6.0, -9.0 ) ;
|
275
|
+
|
276
|
+
assert(!vector1.same_direction?(vectorZero)) ;
|
277
|
+
assert(!vectorZero.same_direction?(vector2)) ;
|
278
|
+
assert(!vector1.same_direction?(vector2)) ;
|
279
|
+
|
280
|
+
assert(vector1.same_direction?(vector3)) ;
|
281
|
+
assert(!vector1.same_direction?(vector4)) ;
|
282
|
+
assert(!vector1.same_direction?(vector5)) ;
|
283
|
+
end
|
284
|
+
|
285
|
+
def test_project
|
286
|
+
vector1 = Vector3.new(1.0, 1.0, 0.0)
|
287
|
+
vector2 = Vector3.new(0.0, 1.0, 0.0)
|
288
|
+
|
289
|
+
projectedVector, parameter = vector2.project_to(vector1)
|
290
|
+
assert(Vector3.new(0.5,0.5,0.0) == projectedVector)
|
291
|
+
assert_in_delta(0.5, parameter, projectedVector.tolerance)
|
292
|
+
|
293
|
+
projectedVector, parameter = vector2.project_to(Vector3.new(0.0, 0.0, 0.0))
|
294
|
+
assert(Vector3.new() == projectedVector)
|
295
|
+
assert_in_delta(0.0, parameter, projectedVector.tolerance)
|
296
|
+
|
297
|
+
vector3 = Vector3.new(5.0, -1.0, 4.0)
|
298
|
+
projectedVector, parmeter = vector3.project_to(vector1)
|
299
|
+
assert(projectedVector.same_direction?(vector1))
|
300
|
+
end
|
301
|
+
|
302
|
+
def test_to_column_vector
|
303
|
+
matrix = @vector.to_column_vector
|
304
|
+
assert_equal( 3, matrix.row_size )
|
305
|
+
assert_equal( 1, matrix.column_size )
|
306
|
+
assert_equal( 1, matrix[0,0])
|
307
|
+
assert_equal( 2, matrix[1,0])
|
308
|
+
assert_equal( 3, matrix[2,0])
|
309
|
+
|
310
|
+
matrix = @vector_init_zero.to_column_vector
|
311
|
+
assert_equal( 3, matrix.row_size )
|
312
|
+
assert_equal( 1, matrix.column_size )
|
313
|
+
assert_equal( 0, matrix[0,0])
|
314
|
+
assert_equal( 0, matrix[1,0])
|
315
|
+
assert_equal( 0, matrix[2,0])
|
316
|
+
end
|
317
|
+
|
318
|
+
|
319
|
+
end
|
metadata
ADDED
@@ -0,0 +1,104 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: gmath3D
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Toshiyasu Shimizu
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2011-09-24 00:00:00.000000000 +09:00
|
13
|
+
default_executable:
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: bundler
|
17
|
+
requirement: &2164436460 !ruby/object:Gem::Requirement
|
18
|
+
none: false
|
19
|
+
requirements:
|
20
|
+
- - ~>
|
21
|
+
- !ruby/object:Gem::Version
|
22
|
+
version: 1.0.0
|
23
|
+
type: :development
|
24
|
+
prerelease: false
|
25
|
+
version_requirements: *2164436460
|
26
|
+
- !ruby/object:Gem::Dependency
|
27
|
+
name: jeweler
|
28
|
+
requirement: &2164435040 !ruby/object:Gem::Requirement
|
29
|
+
none: false
|
30
|
+
requirements:
|
31
|
+
- - ~>
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: 1.6.4
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: *2164435040
|
37
|
+
description: This library defines 3D geometric elements(point, line, plane etc..).
|
38
|
+
It can get two(or more) elements relation, like distance between two elements.
|
39
|
+
email: toshi0328@gmail.com
|
40
|
+
executables: []
|
41
|
+
extensions: []
|
42
|
+
extra_rdoc_files:
|
43
|
+
- LICENSE.txt
|
44
|
+
- README.rdoc
|
45
|
+
files:
|
46
|
+
- .document
|
47
|
+
- Gemfile
|
48
|
+
- Gemfile.lock
|
49
|
+
- LICENSE.txt
|
50
|
+
- README.rdoc
|
51
|
+
- Rakefile
|
52
|
+
- VERSION
|
53
|
+
- gmath3D.gemspec
|
54
|
+
- lib/box.rb
|
55
|
+
- lib/finite_line.rb
|
56
|
+
- lib/geom.rb
|
57
|
+
- lib/gmath3D.rb
|
58
|
+
- lib/line.rb
|
59
|
+
- lib/plane.rb
|
60
|
+
- lib/rectangle.rb
|
61
|
+
- lib/triangle.rb
|
62
|
+
- lib/util.rb
|
63
|
+
- lib/vector3.rb
|
64
|
+
- test/helper.rb
|
65
|
+
- test/test_box.rb
|
66
|
+
- test/test_finite_line.rb
|
67
|
+
- test/test_geom.rb
|
68
|
+
- test/test_gmath3D.rb
|
69
|
+
- test/test_line.rb
|
70
|
+
- test/test_plane.rb
|
71
|
+
- test/test_rectangle.rb
|
72
|
+
- test/test_triangle.rb
|
73
|
+
- test/test_util.rb
|
74
|
+
- test/test_vector3.rb
|
75
|
+
has_rdoc: true
|
76
|
+
homepage: http://github.com/toshi0328/gmath3D
|
77
|
+
licenses:
|
78
|
+
- MIT
|
79
|
+
post_install_message:
|
80
|
+
rdoc_options: []
|
81
|
+
require_paths:
|
82
|
+
- lib
|
83
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
84
|
+
none: false
|
85
|
+
requirements:
|
86
|
+
- - ! '>='
|
87
|
+
- !ruby/object:Gem::Version
|
88
|
+
version: '0'
|
89
|
+
segments:
|
90
|
+
- 0
|
91
|
+
hash: -1522515577061844315
|
92
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
93
|
+
none: false
|
94
|
+
requirements:
|
95
|
+
- - ! '>='
|
96
|
+
- !ruby/object:Gem::Version
|
97
|
+
version: '0'
|
98
|
+
requirements: []
|
99
|
+
rubyforge_project:
|
100
|
+
rubygems_version: 1.6.2
|
101
|
+
signing_key:
|
102
|
+
specification_version: 3
|
103
|
+
summary: Geometric elements in 3D
|
104
|
+
test_files: []
|