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/test/test_geom.rb CHANGED
@@ -1,17 +1,17 @@
1
- $LOAD_PATH.unshift(File.dirname(__FILE__))
2
- require 'helper'
3
-
4
- include GMath3D
5
-
6
- MiniTest::Unit.autorun
7
-
8
- class GeomTestCase < MiniTest::Unit::TestCase
9
- def test_tolerance
10
- default_tolerance = Geom.default_tolerance
11
- geomObject = Geom.new
12
- assert_equal(default_tolerance, geomObject.tolerance)
13
- geomObject.tolerance = 1e-8
14
- assert_equal(1e-8, geomObject.tolerance)
15
- assert_equal(default_tolerance, Geom.default_tolerance)
16
- end
17
- end
1
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
2
+ require 'helper'
3
+
4
+ include GMath3D
5
+
6
+ MiniTest::Unit.autorun
7
+
8
+ class GeomTestCase < MiniTest::Unit::TestCase
9
+ def test_tolerance
10
+ default_tolerance = Geom.default_tolerance
11
+ geomObject = Geom.new
12
+ assert_equal(default_tolerance, geomObject.tolerance)
13
+ geomObject.tolerance = 1e-8
14
+ assert_equal(1e-8, geomObject.tolerance)
15
+ assert_equal(default_tolerance, Geom.default_tolerance)
16
+ end
17
+ end
data/test/test_line.rb CHANGED
@@ -1,146 +1,146 @@
1
- $LOAD_PATH.unshift(File.dirname(__FILE__))
2
- require 'helper'
3
-
4
- include GMath3D
5
-
6
- MiniTest::Unit.autorun
7
-
8
- class LineTestCase < MiniTest::Unit::TestCase
9
- def test_initialize
10
- base_point_tmp = Vector3.new(2.0, 3.0, 5.0)
11
- direction_tmp = Vector3.new(1.0, 1.0, 1.0)
12
- line = Line.new(base_point_tmp, direction_tmp)
13
-
14
- assert(base_point_tmp == line.base_point)
15
- assert(direction_tmp == line.direction )
16
-
17
- lineDefault = Line.new()
18
- assert(Vector3.new(0,0,0) == lineDefault.base_point)
19
- assert(Vector3.new(1,0,0) == lineDefault.direction )
20
- end
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
-
55
- def test_to_s
56
- line = Line.new(Vector3.new(2.0, 3, 5), Vector3.new(1.0, 1.0, 1.0))
57
- assert_equal("Line[point[2.0, 3, 5], vector[1.0, 1.0, 1.0]", line.to_s);
58
- end
59
-
60
- def test_point
61
- base_point_tmp = Vector3.new(2.0, 3.0, 5.0)
62
- direction_tmp = Vector3.new(1.0, 1.0, 0.0)
63
- line = Line.new(base_point_tmp, direction_tmp)
64
-
65
- assert(Vector3.new(2.0, 3.0, 5.0) == line.point(0.0))
66
- assert(Vector3.new(3.0, 4.0, 5.0) == line.point(1.0))
67
- assert(Vector3.new(2.5, 3.5, 5.0) == line.point(0.5))
68
- assert(Vector3.new(4.0, 5.0, 5.0) == line.point(2))
69
- assert(Vector3.new(1.0, 2.0, 5.0) == line.point(-1.0))
70
- end
71
-
72
- def test_distance_to_point
73
- base_point_tmp = Vector3.new(2.0, 3.0, 5.0)
74
- direction_tmp = Vector3.new(1.0, 1.0, 0.0)
75
- line = Line.new(base_point_tmp, direction_tmp)
76
-
77
- check_point1 = base_point_tmp + Vector3.new(-1.0, 1.0, 0.0)
78
- distance, closest_point, parameter = line.distance(check_point1)
79
- assert_in_delta(Math::sqrt(2.0), distance, line.tolerance)
80
- assert(base_point_tmp == closest_point)
81
- assert_in_delta(0.0, parameter, line.tolerance)
82
-
83
- check_point2 = check_point1 + direction_tmp
84
- distance, closest_point, parameter = line.distance(check_point2)
85
- assert_in_delta(Math::sqrt(2.0), distance, line.tolerance)
86
- assert(base_point_tmp + direction_tmp == closest_point)
87
- assert_in_delta(1.0, parameter, line.tolerance)
88
-
89
- check_point3 = line.point(0.3)
90
- distance, closest_point = line.distance(check_point3)
91
- assert_in_delta(0.0, distance, line.tolerance)
92
- assert(check_point3 == closest_point)
93
-
94
- target_point4 = Vector3.new(2,3,3)
95
- line2 = Line.new( Vector3.new(1,2,3), Vector3.new(0,2,0))
96
- distance, point_on_line, parameter = line2.distance( target_point4 )
97
- assert_in_delta( 1, distance, line.tolerance )
98
- assert_equal( Vector3.new(1,3,3), point_on_line )
99
- assert_in_delta( 0.5, parameter, line.tolerance )
100
- end
101
-
102
- def test_distance_to_line
103
- base_point_tmp1 = Vector3.new(0.0, 0.0, 0.0)
104
- direction_tmp1 = Vector3.new(1.0, 1.0, 0.0)
105
- line1 = Line.new(base_point_tmp1, direction_tmp1)
106
-
107
- base_point_tmp2 = Vector3.new(1.0, 1.0, 3.0)
108
- direction_tmp2 = Vector3.new(-1.0, 1.0, 0.0)
109
- line2 = Line.new(base_point_tmp2, direction_tmp2)
110
-
111
- distance, point1, point2, parameter1, parameter2 = line1.distance(line2)
112
- assert_in_delta(3.0, distance, line1.tolerance)
113
- assert(Vector3.new(1.0, 1.0, 0.0) == point1)
114
- assert(Vector3.new(1.0, 1.0, 3.0) == point2)
115
- assert_in_delta(1.0, parameter1, line1.tolerance)
116
- assert_in_delta(0.0, parameter2, line2.tolerance)
117
-
118
- # distance for same lines
119
- distance, point1, point2, parameter1, parameter2 = line1.distance(line1)
120
- assert_in_delta(0.0, distance, line1.tolerance)
121
- assert_equal(nil, point1)
122
- assert_equal(nil, point2)
123
- assert_equal(nil, parameter1)
124
- assert_equal(nil, parameter2)
125
-
126
- # parallel case
127
- base_point_tmp3 = Vector3.new(0.0, 0.0, 4.0)
128
- direction_tmp3 = Vector3.new(-1.0, -1.0, 0.0)
129
- line3 = Line.new(base_point_tmp3, direction_tmp3)
130
- distance, point1, point2, parameter1, parameter2 = line1.distance(line3)
131
- assert_in_delta(4.0, distance, line3.tolerance)
132
- assert_equal(nil, point1)
133
- assert_equal(nil, point2)
134
- assert_equal(nil, parameter1)
135
- assert_equal(nil, parameter2)
136
- end
137
-
138
- def test_distance_to_invalid_value
139
- assert_raises ArgumentError do
140
- invalidResult = Line.new.distance(nil)
141
- end
142
- assert_raises ArgumentError do
143
- invalidResult = Line.new.distance(5.0)
144
- end
145
- end
146
- end
1
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
2
+ require 'helper'
3
+
4
+ include GMath3D
5
+
6
+ MiniTest::Unit.autorun
7
+
8
+ class LineTestCase < MiniTest::Unit::TestCase
9
+ def test_initialize
10
+ base_point_tmp = Vector3.new(2.0, 3.0, 5.0)
11
+ direction_tmp = Vector3.new(1.0, 1.0, 1.0)
12
+ line = Line.new(base_point_tmp, direction_tmp)
13
+
14
+ assert(base_point_tmp == line.base_point)
15
+ assert(direction_tmp == line.direction )
16
+
17
+ lineDefault = Line.new()
18
+ assert(Vector3.new(0,0,0) == lineDefault.base_point)
19
+ assert(Vector3.new(1,0,0) == lineDefault.direction )
20
+ end
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
+
55
+ def test_to_s
56
+ line = Line.new(Vector3.new(2.0, 3, 5), Vector3.new(1.0, 1.0, 1.0))
57
+ assert_equal("Line[point[2.0, 3, 5], vector[1.0, 1.0, 1.0]", line.to_s);
58
+ end
59
+
60
+ def test_point
61
+ base_point_tmp = Vector3.new(2.0, 3.0, 5.0)
62
+ direction_tmp = Vector3.new(1.0, 1.0, 0.0)
63
+ line = Line.new(base_point_tmp, direction_tmp)
64
+
65
+ assert(Vector3.new(2.0, 3.0, 5.0) == line.point(0.0))
66
+ assert(Vector3.new(3.0, 4.0, 5.0) == line.point(1.0))
67
+ assert(Vector3.new(2.5, 3.5, 5.0) == line.point(0.5))
68
+ assert(Vector3.new(4.0, 5.0, 5.0) == line.point(2))
69
+ assert(Vector3.new(1.0, 2.0, 5.0) == line.point(-1.0))
70
+ end
71
+
72
+ def test_distance_to_point
73
+ base_point_tmp = Vector3.new(2.0, 3.0, 5.0)
74
+ direction_tmp = Vector3.new(1.0, 1.0, 0.0)
75
+ line = Line.new(base_point_tmp, direction_tmp)
76
+
77
+ check_point1 = base_point_tmp + Vector3.new(-1.0, 1.0, 0.0)
78
+ distance, closest_point, parameter = line.distance(check_point1)
79
+ assert_in_delta(Math::sqrt(2.0), distance, line.tolerance)
80
+ assert(base_point_tmp == closest_point)
81
+ assert_in_delta(0.0, parameter, line.tolerance)
82
+
83
+ check_point2 = check_point1 + direction_tmp
84
+ distance, closest_point, parameter = line.distance(check_point2)
85
+ assert_in_delta(Math::sqrt(2.0), distance, line.tolerance)
86
+ assert(base_point_tmp + direction_tmp == closest_point)
87
+ assert_in_delta(1.0, parameter, line.tolerance)
88
+
89
+ check_point3 = line.point(0.3)
90
+ distance, closest_point = line.distance(check_point3)
91
+ assert_in_delta(0.0, distance, line.tolerance)
92
+ assert(check_point3 == closest_point)
93
+
94
+ target_point4 = Vector3.new(2,3,3)
95
+ line2 = Line.new( Vector3.new(1,2,3), Vector3.new(0,2,0))
96
+ distance, point_on_line, parameter = line2.distance( target_point4 )
97
+ assert_in_delta( 1, distance, line.tolerance )
98
+ assert_equal( Vector3.new(1,3,3), point_on_line )
99
+ assert_in_delta( 0.5, parameter, line.tolerance )
100
+ end
101
+
102
+ def test_distance_to_line
103
+ base_point_tmp1 = Vector3.new(0.0, 0.0, 0.0)
104
+ direction_tmp1 = Vector3.new(1.0, 1.0, 0.0)
105
+ line1 = Line.new(base_point_tmp1, direction_tmp1)
106
+
107
+ base_point_tmp2 = Vector3.new(1.0, 1.0, 3.0)
108
+ direction_tmp2 = Vector3.new(-1.0, 1.0, 0.0)
109
+ line2 = Line.new(base_point_tmp2, direction_tmp2)
110
+
111
+ distance, point1, point2, parameter1, parameter2 = line1.distance(line2)
112
+ assert_in_delta(3.0, distance, line1.tolerance)
113
+ assert(Vector3.new(1.0, 1.0, 0.0) == point1)
114
+ assert(Vector3.new(1.0, 1.0, 3.0) == point2)
115
+ assert_in_delta(1.0, parameter1, line1.tolerance)
116
+ assert_in_delta(0.0, parameter2, line2.tolerance)
117
+
118
+ # distance for same lines
119
+ distance, point1, point2, parameter1, parameter2 = line1.distance(line1)
120
+ assert_in_delta(0.0, distance, line1.tolerance)
121
+ assert_equal(nil, point1)
122
+ assert_equal(nil, point2)
123
+ assert_equal(nil, parameter1)
124
+ assert_equal(nil, parameter2)
125
+
126
+ # parallel case
127
+ base_point_tmp3 = Vector3.new(0.0, 0.0, 4.0)
128
+ direction_tmp3 = Vector3.new(-1.0, -1.0, 0.0)
129
+ line3 = Line.new(base_point_tmp3, direction_tmp3)
130
+ distance, point1, point2, parameter1, parameter2 = line1.distance(line3)
131
+ assert_in_delta(4.0, distance, line3.tolerance)
132
+ assert_equal(nil, point1)
133
+ assert_equal(nil, point2)
134
+ assert_equal(nil, parameter1)
135
+ assert_equal(nil, parameter2)
136
+ end
137
+
138
+ def test_distance_to_invalid_value
139
+ assert_raises ArgumentError do
140
+ invalidResult = Line.new.distance(nil)
141
+ end
142
+ assert_raises ArgumentError do
143
+ invalidResult = Line.new.distance(5.0)
144
+ end
145
+ end
146
+ end
@@ -1,84 +1,84 @@
1
- # -*- coding: cp932 -*-
2
- $LOAD_PATH.unshift(File.dirname(__FILE__))
3
- require 'helper'
4
-
5
- include GMath3D
6
-
7
- MiniTest::Unit.autorun
8
-
9
- class MatrixTestCase < MiniTest::Unit::TestCase
10
- def setup
11
- @mat_default = Matrix.zero(3)
12
- @mat = Matrix[[1,2,3],[4,5,6],[7,8,9]]
13
- end
14
-
15
- def test_initalize
16
- @mat_default.each do |cell|
17
- assert_equal(0, cell)
18
- end
19
-
20
- i = 1
21
- @mat.each do |cell|
22
- assert_equal(i, cell)
23
- i+=1
24
- end
25
- end
26
-
27
- def test_to_s
28
- assert_equal("Matrix[[0, 0, 0], [0, 0, 0], [0, 0, 0]]", @mat_default.to_s)
29
- assert_equal("Matrix[[1, 2, 3], [4, 5, 6], [7, 8, 9]]", @mat.to_s)
30
- end
31
-
32
- def test_multiply
33
- mat = Matrix[[3,2,1],[-4,2,1],[3,4,1]]
34
- vec = Matrix.column_vector([2,3,4])
35
- ans = mat*vec
36
-
37
- assert_equal(ans[0,0], 16)
38
- assert_equal(ans[1,0], 2 )
39
- assert_equal(ans[2,0], 22)
40
-
41
- vec2 = Vector3.new(2,3,4)
42
- ans2 = mat*(vec2)
43
- assert_equal(Vector3.new(16,2,22), ans2)
44
- end
45
-
46
- def test_from_axis
47
- local_point = Vector3.new(1,0,1)
48
- angle = 45*Math::PI/180
49
- local_cod_x = Vector3.new(Math.cos(angle), Math.sin(angle),0)
50
- x_vec = Vector3.new(1,0,0)
51
- diff_angle = x_vec.angle(local_cod_x)
52
- axis = x_vec.cross(local_cod_x).normalize()
53
-
54
- rotate_matrix = Matrix.from_axis(axis, diff_angle)
55
- rotated_point = rotate_matrix.inv * local_point
56
- assert_equal(Vector3.new(Math.sqrt(2)/2, Math.sqrt(2)/2, 1), rotated_point)
57
-
58
- local_point = Vector3.new(1,0,0)
59
- local_cod_x = Vector3.new(1,1,1).normalize()
60
- diff_angle = x_vec.angle(local_cod_x)
61
- axis = x_vec.cross(local_cod_x).normalize()
62
-
63
- assert_equal(Math::PI/2, local_cod_x.angle(axis))
64
- rotate_matrix = Matrix.from_axis(axis, diff_angle)
65
- rotated_point = rotate_matrix.inv()*local_point
66
-
67
- assert_in_delta(rotated_point.x, rotated_point.y, rotated_point.tolerance)
68
- assert_in_delta(rotated_point.x, rotated_point.z, rotated_point.tolerance)
69
- end
70
-
71
- def test_from_quat
72
- axis = Vector3.new(1,2,4).normalize()
73
- angle = 46*Math::PI/180
74
- quat = Quat.from_axis(axis, angle)
75
- mat_expected = Matrix.from_axis(axis, angle)
76
- mat_actual = Matrix.from_quat(quat)
77
-
78
- [0,1,2].each do |i|
79
- [0,1,2].each do |j|
80
- assert_in_delta( mat_expected[i,j], mat_actual[i,j], 1e-8)
81
- end
82
- end
83
- end
84
- end
1
+ # -*- coding: cp932 -*-
2
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
3
+ require 'helper'
4
+
5
+ include GMath3D
6
+
7
+ MiniTest::Unit.autorun
8
+
9
+ class MatrixTestCase < MiniTest::Unit::TestCase
10
+ def setup
11
+ @mat_default = Matrix.zero(3)
12
+ @mat = Matrix[[1,2,3],[4,5,6],[7,8,9]]
13
+ end
14
+
15
+ def test_initalize
16
+ @mat_default.each do |cell|
17
+ assert_equal(0, cell)
18
+ end
19
+
20
+ i = 1
21
+ @mat.each do |cell|
22
+ assert_equal(i, cell)
23
+ i+=1
24
+ end
25
+ end
26
+
27
+ def test_to_s
28
+ assert_equal("Matrix[[0, 0, 0], [0, 0, 0], [0, 0, 0]]", @mat_default.to_s)
29
+ assert_equal("Matrix[[1, 2, 3], [4, 5, 6], [7, 8, 9]]", @mat.to_s)
30
+ end
31
+
32
+ def test_multiply
33
+ mat = Matrix[[3,2,1],[-4,2,1],[3,4,1]]
34
+ vec = Matrix.column_vector([2,3,4])
35
+ ans = mat*vec
36
+
37
+ assert_equal(ans[0,0], 16)
38
+ assert_equal(ans[1,0], 2 )
39
+ assert_equal(ans[2,0], 22)
40
+
41
+ vec2 = Vector3.new(2,3,4)
42
+ ans2 = mat*(vec2)
43
+ assert_equal(Vector3.new(16,2,22), ans2)
44
+ end
45
+
46
+ def test_from_axis
47
+ local_point = Vector3.new(1,0,1)
48
+ angle = 45*Math::PI/180
49
+ local_cod_x = Vector3.new(Math.cos(angle), Math.sin(angle),0)
50
+ x_vec = Vector3.new(1,0,0)
51
+ diff_angle = x_vec.angle(local_cod_x)
52
+ axis = x_vec.cross(local_cod_x).normalize()
53
+
54
+ rotate_matrix = Matrix.from_axis(axis, diff_angle)
55
+ rotated_point = rotate_matrix.inv * local_point
56
+ assert_equal(Vector3.new(Math.sqrt(2)/2, Math.sqrt(2)/2, 1), rotated_point)
57
+
58
+ local_point = Vector3.new(1,0,0)
59
+ local_cod_x = Vector3.new(1,1,1).normalize()
60
+ diff_angle = x_vec.angle(local_cod_x)
61
+ axis = x_vec.cross(local_cod_x).normalize()
62
+
63
+ assert_equal(Math::PI/2, local_cod_x.angle(axis))
64
+ rotate_matrix = Matrix.from_axis(axis, diff_angle)
65
+ rotated_point = rotate_matrix.inv()*local_point
66
+
67
+ assert_in_delta(rotated_point.x, rotated_point.y, rotated_point.tolerance)
68
+ assert_in_delta(rotated_point.x, rotated_point.z, rotated_point.tolerance)
69
+ end
70
+
71
+ def test_from_quat
72
+ axis = Vector3.new(1,2,4).normalize()
73
+ angle = 46*Math::PI/180
74
+ quat = Quat.from_axis(axis, angle)
75
+ mat_expected = Matrix.from_axis(axis, angle)
76
+ mat_actual = Matrix.from_quat(quat)
77
+
78
+ [0,1,2].each do |i|
79
+ [0,1,2].each do |j|
80
+ assert_in_delta( mat_expected[i,j], mat_actual[i,j], 1e-8)
81
+ end
82
+ end
83
+ end
84
+ end