mageo 0.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.document +5 -0
- data/Gemfile +15 -0
- data/LICENSE.txt +20 -0
- data/README.rdoc +19 -0
- data/Rakefile +56 -0
- data/VERSION +1 -0
- data/lib/mageo.rb +0 -0
- data/lib/mageo/axes.rb +87 -0
- data/lib/mageo/cylinder.rb +23 -0
- data/lib/mageo/octahedron.rb +62 -0
- data/lib/mageo/polar2d.rb +95 -0
- data/lib/mageo/polar3d.rb +47 -0
- data/lib/mageo/polyhedron.rb +95 -0
- data/lib/mageo/segment.rb +83 -0
- data/lib/mageo/sphere.rb +19 -0
- data/lib/mageo/tetrahedron.rb +42 -0
- data/lib/mageo/triangle.rb +225 -0
- data/lib/mageo/vector.rb +23 -0
- data/lib/mageo/vector3d.rb +246 -0
- data/lib/mageo/vector3dinternal.rb +125 -0
- data/mageo.gemspec +89 -0
- data/test/helper.rb +17 -0
- data/test/test_axes.rb +209 -0
- data/test/test_cylinder.rb +21 -0
- data/test/test_octahedron.rb +157 -0
- data/test/test_polar2d.rb +153 -0
- data/test/test_polar3d.rb +95 -0
- data/test/test_polyhedron.rb +14 -0
- data/test/test_segment.rb +82 -0
- data/test/test_sphere.rb +20 -0
- data/test/test_tetrahedron.rb +132 -0
- data/test/test_triangle.rb +258 -0
- data/test/test_vector.rb +73 -0
- data/test/test_vector3d.rb +429 -0
- data/test/test_vector3dinternal.rb +147 -0
- metadata +183 -0
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
#! /usr/bin/env ruby
|
|
2
|
+
# coding: utf-8
|
|
3
|
+
|
|
4
|
+
require "test/unit"
|
|
5
|
+
require "helper"
|
|
6
|
+
require "mageo/polar3d.rb"
|
|
7
|
+
require "mageo/vector3d.rb"
|
|
8
|
+
|
|
9
|
+
class TC_Polar3D < Test::Unit::TestCase
|
|
10
|
+
$tolerance = 10**(-10)
|
|
11
|
+
|
|
12
|
+
include Math
|
|
13
|
+
|
|
14
|
+
def setup
|
|
15
|
+
@p3d00 = Polar3D.new( 0.0, 0.00*PI, 0.00*PI)
|
|
16
|
+
@p3d01 = Polar3D.new( 2.0, 0.00*PI, 0.25*PI)
|
|
17
|
+
@p3d02 = Polar3D.new( 2.0, 0.25*PI, 0.00*PI)
|
|
18
|
+
@p3d03 = Polar3D.new( 2.0, 0.25*PI, 0.25*PI)
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def test_to_v3d
|
|
22
|
+
assert_equal( Vector3D, @p3d00.to_v3d.class )
|
|
23
|
+
assert_in_delta( 0.0, @p3d00.to_v3d[0], $tolerance )
|
|
24
|
+
assert_in_delta( 0.0, @p3d00.to_v3d[1], $tolerance )
|
|
25
|
+
assert_in_delta( 0.0, @p3d00.to_v3d[2], $tolerance )
|
|
26
|
+
|
|
27
|
+
assert_equal( Vector3D, @p3d01.to_v3d.class )
|
|
28
|
+
assert_in_delta( 0.0, @p3d01.to_v3d[0], $tolerance )
|
|
29
|
+
assert_in_delta( 0.0, @p3d01.to_v3d[1], $tolerance )
|
|
30
|
+
assert_in_delta( 2.0, @p3d01.to_v3d[2], $tolerance )
|
|
31
|
+
|
|
32
|
+
assert_equal( Vector3D, @p3d02.to_v3d.class )
|
|
33
|
+
assert_in_delta( Math::sqrt(2.0), @p3d02.to_v3d[0], $tolerance )
|
|
34
|
+
assert_in_delta( 0.0 , @p3d02.to_v3d[1], $tolerance )
|
|
35
|
+
assert_in_delta( Math::sqrt(2.0), @p3d02.to_v3d[2], $tolerance )
|
|
36
|
+
|
|
37
|
+
assert_equal( Vector3D, @p3d03.to_v3d.class )
|
|
38
|
+
#√2 * cos(0.25PI)
|
|
39
|
+
assert_in_delta( 1.0 , @p3d03.to_v3d[0], $tolerance)
|
|
40
|
+
assert_in_delta( 1.0 , @p3d03.to_v3d[1], $tolerance)
|
|
41
|
+
assert_in_delta( Math::sqrt(2.0), @p3d03.to_v3d[2], $tolerance )
|
|
42
|
+
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
def test_minimize_phi!
|
|
46
|
+
p2pA = Polar3D.new( 2.0, 0.5*PI, -2.5*PI )
|
|
47
|
+
p2pA.minimize_phi!
|
|
48
|
+
assert_in_delta( 1.5*PI, p2pA.phi, $tolerance )
|
|
49
|
+
|
|
50
|
+
p2pB = Polar3D.new( 2.0, 0.5*PI, -0.5*PI )
|
|
51
|
+
p2pB.minimize_phi!
|
|
52
|
+
assert_in_delta( 1.5*PI, p2pB.phi, $tolerance )
|
|
53
|
+
|
|
54
|
+
p2pC = Polar3D.new( 2.0, 0.5*PI, 1.5*PI )
|
|
55
|
+
p2pC.minimize_phi!
|
|
56
|
+
assert_in_delta( 1.5*PI, p2pC.phi, $tolerance )
|
|
57
|
+
|
|
58
|
+
p2pD = Polar3D.new( 2.0, 0.5*PI, 3.5*PI )
|
|
59
|
+
p2pD.minimize_phi!
|
|
60
|
+
assert_in_delta( 1.5*PI, p2pD.phi, $tolerance )
|
|
61
|
+
|
|
62
|
+
p2pE = Polar3D.new( 2.0, 0.5*PI, 5.5*PI )
|
|
63
|
+
p2pE.minimize_phi!
|
|
64
|
+
assert_in_delta( 1.5*PI, p2pE.phi, $tolerance )
|
|
65
|
+
|
|
66
|
+
p2pF = Polar3D.new( 2.0, 0.5*PI, 4.5*PI )
|
|
67
|
+
p2pF.minimize_phi!
|
|
68
|
+
assert_in_delta( 0.5*PI, p2pF.phi, $tolerance )
|
|
69
|
+
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
def test_minimize_phi
|
|
73
|
+
p2pA = Polar3D.new( 2.0, 0.5*PI, -2.5*PI ).minimize_phi
|
|
74
|
+
assert_in_delta( 1.5*PI, p2pA.phi, $tolerance )
|
|
75
|
+
|
|
76
|
+
p2pB = Polar3D.new( 2.0, 0.5*PI, -0.5*PI ).minimize_phi
|
|
77
|
+
assert_in_delta( 1.5*PI, p2pB.phi, $tolerance )
|
|
78
|
+
|
|
79
|
+
p2pC = Polar3D.new( 2.0, 0.5*PI, 1.5*PI ).minimize_phi
|
|
80
|
+
assert_in_delta( 1.5*PI, p2pC.phi, $tolerance )
|
|
81
|
+
|
|
82
|
+
p2pD = Polar3D.new( 2.0, 0.5*PI, 3.5*PI ).minimize_phi
|
|
83
|
+
assert_in_delta( 1.5*PI, p2pD.phi, $tolerance )
|
|
84
|
+
|
|
85
|
+
p2pE = Polar3D.new( 2.0, 0.5*PI, 5.5*PI ).minimize_phi
|
|
86
|
+
assert_in_delta( 1.5*PI, p2pE.phi, $tolerance )
|
|
87
|
+
|
|
88
|
+
p2pF = Polar3D.new( 2.0, 0.5*PI, 4.5*PI ).minimize_phi
|
|
89
|
+
assert_in_delta( 0.5*PI, p2pF.phi, $tolerance )
|
|
90
|
+
|
|
91
|
+
end
|
|
92
|
+
|
|
93
|
+
|
|
94
|
+
end
|
|
95
|
+
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
#! /usr/bin/env ruby
|
|
2
|
+
# coding: utf-8
|
|
3
|
+
|
|
4
|
+
require "test/unit"
|
|
5
|
+
require "helper"
|
|
6
|
+
require "mageo/polyhedron.rb"
|
|
7
|
+
|
|
8
|
+
# initialize でインスタンスを生成できないことのみテストする。
|
|
9
|
+
# その他の機能はサブクラスでテスト。
|
|
10
|
+
class TC_Polyhedron < Test::Unit::TestCase
|
|
11
|
+
def test_initialize
|
|
12
|
+
assert_raise( NotImplementedError ) { Polyhedron.new }
|
|
13
|
+
end
|
|
14
|
+
end
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
#! /usr/bin/env ruby
|
|
2
|
+
# coding: utf-8
|
|
3
|
+
|
|
4
|
+
require "test/unit"
|
|
5
|
+
require "helper"
|
|
6
|
+
require "mageo/segment.rb"
|
|
7
|
+
|
|
8
|
+
class TC_Segment < Test::Unit::TestCase
|
|
9
|
+
$tolerance = 1.0 * 10.0 ** (-10)
|
|
10
|
+
|
|
11
|
+
VEC_00 = Vector3D[1.0, 2.0, 3.0]
|
|
12
|
+
VEC_01 = Vector3D[4.0, 6.0, 8.0]
|
|
13
|
+
|
|
14
|
+
VEC_10 = Vector3D[0.0, 0.0, 1.0]
|
|
15
|
+
VEC_11 = Vector3D[0.0, 0.0, 3.0]
|
|
16
|
+
|
|
17
|
+
VEC_20 = Vector3D[1.0, 1.0, 1.0]
|
|
18
|
+
VEC_21 = Vector3D[0.0, 0.0, 0.0]
|
|
19
|
+
def setup
|
|
20
|
+
@s00 = Segment.new(VEC_00, VEC_01)
|
|
21
|
+
@s01 = Segment.new(VEC_10, VEC_11)
|
|
22
|
+
@s02 = Segment.new(VEC_20, VEC_21)
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
def test_initialize
|
|
26
|
+
vec00 = [1.0, 2.0, 3.0]
|
|
27
|
+
vec01 = [4.0, 6.0, 8.0]
|
|
28
|
+
assert_raise(Segment::InitializeError){
|
|
29
|
+
Segment.new(vec00, vec01)
|
|
30
|
+
}
|
|
31
|
+
assert_raise(Segment::InitializeError){
|
|
32
|
+
Segment.new(VEC_00, VEC_00)
|
|
33
|
+
}
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
def test_endpoints
|
|
37
|
+
assert_equal(VEC_00, @s00.endpoints[0])
|
|
38
|
+
assert_equal(VEC_01, @s00.endpoints[1])
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
def test_include?
|
|
42
|
+
assert_raise(Segment::TypeError){
|
|
43
|
+
@s02.include?([0.0, 0.0, 0.0], $tolerance)
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
#直線上
|
|
47
|
+
assert_equal(false, @s02.include?(Vector3D[-1.0,-1.0,-1.0], $tolerance))
|
|
48
|
+
assert_equal(true , @s02.include?(Vector3D[ 0.0, 0.0, 0.0], $tolerance))
|
|
49
|
+
assert_equal(true , @s02.include?(Vector3D[ 0.3, 0.3, 0.3], $tolerance))
|
|
50
|
+
assert_equal(true , @s02.include?(Vector3D[ 1.0, 1.0, 1.0], $tolerance))
|
|
51
|
+
assert_equal(false, @s02.include?(Vector3D[ 2.0, 2.0, 2.0], $tolerance))
|
|
52
|
+
|
|
53
|
+
#横に出てる
|
|
54
|
+
assert_equal(false, @s02.include?(Vector3D[0.1, 0.1, 0.2], $tolerance))
|
|
55
|
+
assert_equal(true , @s02.include?(Vector3D[0.1, 0.1, 0.2], 1.0))
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
def test_to_v3d
|
|
59
|
+
assert_equal(Vector3D[ 3.0, 4.0, 5.0], @s00.to_v3d)
|
|
60
|
+
assert_equal(Vector3D[ 0.0, 0.0, 2.0], @s01.to_v3d)
|
|
61
|
+
assert_equal(Vector3D[-1.0,-1.0,-1.0], @s02.to_v3d)
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
def test_eql?
|
|
65
|
+
assert_raise(Segment::TypeError){ @s00.eql?([[0,0,0],[1,1,1]])}
|
|
66
|
+
|
|
67
|
+
assert_equal(true , @s00.eql?(Segment.new(VEC_01, VEC_00)))
|
|
68
|
+
assert_equal(false, @s00.eql?(@s01))
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
def test_equal2
|
|
72
|
+
assert_raise(Segment::TypeError){ @s00.eql?([[0,0,0],[1,1,1]])}
|
|
73
|
+
|
|
74
|
+
assert_equal(true , @s00 == Segment.new(VEC_00, VEC_01))
|
|
75
|
+
assert_equal(false, @s00 == Segment.new(VEC_01, VEC_00))
|
|
76
|
+
assert_equal(false, @s00 == @s01)
|
|
77
|
+
end
|
|
78
|
+
|
|
79
|
+
#undef test_include?
|
|
80
|
+
|
|
81
|
+
end
|
|
82
|
+
|
data/test/test_sphere.rb
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
#! /usr/bin/env ruby
|
|
2
|
+
# coding: utf-8
|
|
3
|
+
|
|
4
|
+
require "helper"
|
|
5
|
+
require "test/unit"
|
|
6
|
+
require "mageo/sphere.rb"
|
|
7
|
+
require "mageo/vector3d.rb"
|
|
8
|
+
|
|
9
|
+
class TC_Sphere < Test::Unit::TestCase
|
|
10
|
+
def setup
|
|
11
|
+
@s00 = Sphere.new([0.0, 1.0, 2.0], 3.0)
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
def test_initialize
|
|
15
|
+
assert_equal(Vector3D[0.0, 1.0, 2.0], @s00.position)
|
|
16
|
+
assert_equal(3.0, @s00.radius)
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
end
|
|
20
|
+
|
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
#! /usr/bin/env ruby
|
|
2
|
+
# coding: utf-8
|
|
3
|
+
|
|
4
|
+
require "test/unit"
|
|
5
|
+
require "rubygems"
|
|
6
|
+
gem "builtinextension"
|
|
7
|
+
require "array_include_eql.rb"
|
|
8
|
+
require "mageo/tetrahedron.rb"
|
|
9
|
+
|
|
10
|
+
class Tetrahedron
|
|
11
|
+
public :center
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
class TC_Tetrahedron < Test::Unit::TestCase
|
|
15
|
+
$tolerance = 10**(-10)
|
|
16
|
+
|
|
17
|
+
V_00 = Vector3D[ 0.0, 0.0, 0.0]
|
|
18
|
+
V_01 = Vector3D[10.0, 0.0, 0.0]
|
|
19
|
+
V_02 = Vector3D[ 0.0,10.0, 0.0]
|
|
20
|
+
V_03 = Vector3D[ 0.0, 0.0,10.0]
|
|
21
|
+
|
|
22
|
+
V_10 = Vector3D[10.0,20.0,30.0]
|
|
23
|
+
V_11 = Vector3D[ 0.0,20.0,30.0]
|
|
24
|
+
V_12 = Vector3D[10.0, 0.0,30.0]
|
|
25
|
+
V_13 = Vector3D[10.0,20.0, 0.0]
|
|
26
|
+
|
|
27
|
+
def setup
|
|
28
|
+
@t00 = Tetrahedron.new( [ V_00, V_01, V_02, V_03 ])
|
|
29
|
+
@t01 = Tetrahedron.new( [ V_10, V_11, V_12, V_13 ])
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
def test_initialize
|
|
33
|
+
assert_raise( ArgumentError ){ Tetrahedron.new }
|
|
34
|
+
assert_raise( ArgumentError ){ Tetrahedron.new() }
|
|
35
|
+
assert_raise( Tetrahedron::InitializeError ){ Tetrahedron.new( nil ) }
|
|
36
|
+
assert_raise( Tetrahedron::InitializeError ){ Tetrahedron.new( [] ) }
|
|
37
|
+
assert_raise( Tetrahedron::InitializeError ){ Tetrahedron.new( [ 0, 1, 2, 3 ] ) }
|
|
38
|
+
assert_raise( Tetrahedron::InitializeError ){ Tetrahedron.new( [ [], [], [], [] ] ) }
|
|
39
|
+
assert_raise( Tetrahedron::InitializeError ){
|
|
40
|
+
Tetrahedron.new( [ V_00, V_01, V_02, [ 0.0, 0.0 ] ])
|
|
41
|
+
}
|
|
42
|
+
assert_raise( Tetrahedron::InitializeError ){
|
|
43
|
+
Tetrahedron.new( [ V_00, V_01, V_02, [ 0.0, 0.0, 1.0, 0.0 ] ])
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
# 5点ある
|
|
47
|
+
assert_raise( Tetrahedron::InitializeError ){
|
|
48
|
+
Tetrahedron.new( [ V_00, V_01, V_02, V_03, [ 1.0, 1.0, 1.0] ])
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
# 体積が 0.0 になるのはエラー
|
|
52
|
+
assert_raise( Tetrahedron::InitializeError ){
|
|
53
|
+
Tetrahedron.new( [ V_00, V_01, V_02, [ 2.0, 2.0, 0.0] ])
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
# Vector3DInternal なら 例外
|
|
57
|
+
assert_raise( Tetrahedron::InitializeError ){
|
|
58
|
+
Tetrahedron.new(
|
|
59
|
+
[ Vector3DInternal[ 0.0, 0.0, 0.0],
|
|
60
|
+
Vector3DInternal[ 1.0, 0.0, 0.0],
|
|
61
|
+
Vector3DInternal[ 0.0, 1.0, 0.0],
|
|
62
|
+
Vector3DInternal[ 0.0, 0.0, 1.0]
|
|
63
|
+
]
|
|
64
|
+
)
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
# Vector3D なら OK
|
|
68
|
+
assert_nothing_raised{
|
|
69
|
+
Tetrahedron.new( [ V_00, V_01, V_02, V_03 ])
|
|
70
|
+
}
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
def test_inside?
|
|
74
|
+
assert_equal( true , @t00.inside?( [ 1.0, 1.0, 1.0] ) )
|
|
75
|
+
assert_equal( false, @t00.inside?( [ 5.0, 5.0, 5.0] ) )
|
|
76
|
+
assert_equal( false, @t00.inside?( [-5.0,-5.0,-5.0] ) )
|
|
77
|
+
assert_equal( false, @t00.inside?( [ 0.0, 0.0, 0.0] ) ) #頂点上
|
|
78
|
+
|
|
79
|
+
assert_raise(Polyhedron::TypeError){@t00.inside?(Vector3DInternal[1.0, 1.0, 1.0])}
|
|
80
|
+
assert_raise(Tetrahedron::TypeError){@t00.inside?(Vector3DInternal[1.0, 1.0, 1.0])}
|
|
81
|
+
#pp Polyhedron::TypeError.ancestors
|
|
82
|
+
#pp Tetrahedron::TypeError.ancestors
|
|
83
|
+
end
|
|
84
|
+
|
|
85
|
+
def test_include?
|
|
86
|
+
assert_equal( true , @t00.include?([ 1.0, 1.0, 1.0], $tolerance))
|
|
87
|
+
assert_equal( false, @t00.include?([ 5.0, 5.0, 5.0], $tolerance))
|
|
88
|
+
assert_equal( false, @t00.include?([-5.0,-5.0,-5.0], $tolerance))
|
|
89
|
+
assert_equal( true , @t00.include?([ 0.0, 0.0, 0.0], $tolerance)) #頂点上
|
|
90
|
+
|
|
91
|
+
assert_equal( false, @t01.include?(Vector3D[ 3.0, 6.0, 30.0], $tolerance))
|
|
92
|
+
|
|
93
|
+
assert_raise(Polyhedron::TypeError){@t00.include?(Vector3DInternal[1.0, 1.0, 1.0], $tolerance)}
|
|
94
|
+
end
|
|
95
|
+
|
|
96
|
+
def test_volume
|
|
97
|
+
assert_in_delta( 1000.0/6.0 , @t00.volume, $tolerance )
|
|
98
|
+
end
|
|
99
|
+
|
|
100
|
+
def test_center
|
|
101
|
+
assert_in_delta( 10.0/4.0, @t00.center[0], $tolerance)
|
|
102
|
+
assert_in_delta( 10.0/4.0, @t00.center[1], $tolerance)
|
|
103
|
+
assert_in_delta( 10.0/4.0, @t00.center[2], $tolerance)
|
|
104
|
+
end
|
|
105
|
+
|
|
106
|
+
def test_vertices
|
|
107
|
+
assert_equal( [ V_00, V_01, V_02, V_03 ], @t00.vertices)
|
|
108
|
+
end
|
|
109
|
+
|
|
110
|
+
def test_triangles
|
|
111
|
+
t = @t00.triangles
|
|
112
|
+
assert_equal(4, t.size)
|
|
113
|
+
assert_equal(Triangle.new([V_00, V_01, V_02]) ,t[0])
|
|
114
|
+
assert_equal(Triangle.new([V_01, V_02, V_03]) ,t[1])
|
|
115
|
+
assert_equal(Triangle.new([V_02, V_03, V_00]) ,t[2])
|
|
116
|
+
assert_equal(Triangle.new([V_03, V_00, V_01]) ,t[3])
|
|
117
|
+
end
|
|
118
|
+
|
|
119
|
+
def test_edges
|
|
120
|
+
t = @t00.edges
|
|
121
|
+
assert_equal(6, t.size)
|
|
122
|
+
assert_equal(true, (t.include_eql?(Segment.new(V_00, V_01))))
|
|
123
|
+
assert_equal(true, (t.include_eql?(Segment.new(V_00, V_02))))
|
|
124
|
+
assert_equal(true, (t.include_eql?(Segment.new(V_00, V_03))))
|
|
125
|
+
assert_equal(true, (t.include_eql?(Segment.new(V_01, V_02))))
|
|
126
|
+
assert_equal(true, (t.include_eql?(Segment.new(V_01, V_03))))
|
|
127
|
+
assert_equal(true, (t.include_eql?(Segment.new(V_02, V_03))))
|
|
128
|
+
end
|
|
129
|
+
|
|
130
|
+
|
|
131
|
+
end
|
|
132
|
+
|
|
@@ -0,0 +1,258 @@
|
|
|
1
|
+
#! /usr/bin/env ruby
|
|
2
|
+
# coding: utf-8
|
|
3
|
+
|
|
4
|
+
require "test/unit"
|
|
5
|
+
require "mageo/triangle.rb"
|
|
6
|
+
require "mageo/vector3d.rb"
|
|
7
|
+
|
|
8
|
+
class Triangle
|
|
9
|
+
public :internal_axes
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
class TC_Triangle < Test::Unit::TestCase
|
|
13
|
+
$tolerance = 10.0 ** (-10)
|
|
14
|
+
|
|
15
|
+
VEC_O = Vector3D[0.0, 0.0, 0.0]
|
|
16
|
+
VEC_X = Vector3D[1.0, 0.0, 0.0]
|
|
17
|
+
VEC_Y = Vector3D[0.0, 1.0, 0.0]
|
|
18
|
+
VEC_Z = Vector3D[0.0, 0.0, 1.0]
|
|
19
|
+
|
|
20
|
+
def setup
|
|
21
|
+
@t00 = Triangle.new([VEC_O, VEC_X, VEC_Y])
|
|
22
|
+
@t01 = Triangle.new([VEC_X, VEC_Y, VEC_Z])
|
|
23
|
+
@t02 = Triangle.new([[10.0,10.0,10.0], [20.0,10.0,10.0], [10.0,20.0,10.0]])
|
|
24
|
+
@t03 = Triangle.new([[10.0,20.0,30.0], [ 0.0,20.0,30.0], [10.0, 0.0,30.0]])
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
def test_initialize
|
|
28
|
+
assert_raise( ArgumentError ){ Triangle.new }
|
|
29
|
+
assert_raise( ArgumentError ){ Triangle.new() }
|
|
30
|
+
assert_raise( Triangle::InitializeError ){ Triangle.new( nil ) }
|
|
31
|
+
assert_raise( Triangle::InitializeError ){ Triangle.new( [] ) }
|
|
32
|
+
assert_raise( Triangle::InitializeError ){ Triangle.new( [ 0, 1 ] ) }
|
|
33
|
+
assert_raise( Triangle::InitializeError ){ Triangle.new( [ 0, 1, 2 ] ) }
|
|
34
|
+
assert_raise( Triangle::InitializeError ){ Triangle.new( [ 0, 1, 2, 3 ] ) }
|
|
35
|
+
assert_raise( Triangle::InitializeError ){ Triangle.new( [ [ 0, 0, 0 ], [ 1, 1, 1 ], [ 2, 3 ] ] ) } #3次元座標になっていないものがある。
|
|
36
|
+
|
|
37
|
+
assert_raise( Triangle::LinearException ){ Triangle.new( [ [ 0, 0, 0 ], [ 0, 0, 0 ], [ 2, 2, 2 ] ] ) } #同一の点を含む。
|
|
38
|
+
assert_raise( Triangle::LinearException ){ Triangle.new( [ [ 0, 0, 0 ], [ 1, 1, 1 ], [ 2, 2, 2 ] ] ) } #直線上に並ぶ
|
|
39
|
+
|
|
40
|
+
assert_equal( Triangle, Triangle.new( [ Vector3D[ 0, 0, 0 ], Vector3D[ 1, 0, 0 ], Vector3D[ 0, 1, 0 ] ] ).class )
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
def test_vertices
|
|
44
|
+
assert_equal( Array, @t00.vertices.class )
|
|
45
|
+
assert_equal( 3, @t00.vertices.size )
|
|
46
|
+
assert_equal( Vector3D[0, 0, 0], @t00.vertices[0] )
|
|
47
|
+
assert_equal( Vector3D[1, 0, 0], @t00.vertices[1] )
|
|
48
|
+
assert_equal( Vector3D[0, 1, 0], @t00.vertices[2] )
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
def test_same_side?
|
|
52
|
+
assert_raise(Triangle::TypeError){ @t00.same_side?( [ 2, 3, 7 ], [ -2, -3, 2 ])}
|
|
53
|
+
|
|
54
|
+
assert_equal( true , @t00.same_side?( Vector3D[ 2, 3, 7 ], Vector3D[ -2, -3, 2 ] ) )
|
|
55
|
+
assert_equal( true , @t00.same_side?( Vector3D[ 2, 3, -7 ], Vector3D[ -2, -3, -2 ] ) )
|
|
56
|
+
assert_equal( false, @t00.same_side?( Vector3D[ 2, 3, 7 ], Vector3D[ -2, -3, -2 ] ) )
|
|
57
|
+
assert_equal( false, @t00.same_side?( Vector3D[ 2, 3, -7 ], Vector3D[ -2, -3, 2 ] ) )
|
|
58
|
+
assert_equal( false, @t00.same_side?( Vector3D[ 1, 2, 0 ], Vector3D[ 10, 10, 10 ] ) ) #pos0 が面上の点
|
|
59
|
+
assert_equal( false, @t00.same_side?( Vector3D[10, 10,10 ], Vector3D[ 1, 2, 0 ] ) ) #pos1 が面上の点
|
|
60
|
+
assert_equal( false, @t00.same_side?( Vector3D[10, 10, 0 ], Vector3D[ 1, 2, 0 ] ) ) #両方 が面上の点
|
|
61
|
+
|
|
62
|
+
assert_equal( true , @t01.same_side?( Vector3D[ 10, 10, 10 ], Vector3D[ 20, 30, 40 ] ) )
|
|
63
|
+
assert_equal( true , @t01.same_side?( Vector3D[ 0, 0, 0 ], Vector3D[ -10, -10, -10 ] ) )
|
|
64
|
+
assert_equal( false, @t01.same_side?( Vector3D[ 10, 10, 10 ], Vector3D[ -10, -10, -10 ] ) )
|
|
65
|
+
assert_equal( false, @t01.same_side?( Vector3D[ 0, 0, 0 ], Vector3D[ 20, 30, 40 ] ) )
|
|
66
|
+
assert_equal( false, @t01.same_side?( Vector3D[ 0.5, 0.5, 0 ], Vector3D[ 10, 10, 10 ] ) ) #pos0 が面上の点
|
|
67
|
+
assert_equal( false, @t01.same_side?( Vector3D[ 10, 10, 10 ], Vector3D[ 0.5, 0.5, 0 ] ) ) #pos1 が面上の点
|
|
68
|
+
assert_equal( false, @t01.same_side?( Vector3D[ 0.5, 0.5, 0 ], Vector3D[ 0, 0.5, 0.5 ] ) ) #両方 が面上の点
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
def test_include?
|
|
72
|
+
assert_raise(Triangle::TypeError){ @t00.include?([11.0, 11.0, 10.0], $tolerance)}
|
|
73
|
+
|
|
74
|
+
# on face
|
|
75
|
+
assert_equal(true, @t02.include?(Vector3D[11.0, 11.0, 10.0], $tolerance))
|
|
76
|
+
|
|
77
|
+
# on vertices
|
|
78
|
+
assert_equal(true, @t02.include?(Vector3D[10.0, 10.0, 10.0], $tolerance))
|
|
79
|
+
assert_equal(true, @t02.include?(Vector3D[20.0, 10.0, 10.0], $tolerance))
|
|
80
|
+
assert_equal(true, @t02.include?(Vector3D[10.0, 20.0, 10.0], $tolerance))
|
|
81
|
+
|
|
82
|
+
# on edge
|
|
83
|
+
assert_equal(true, @t02.include?(Vector3D[15.0, 10.0, 10.0], $tolerance))
|
|
84
|
+
assert_equal(true, @t02.include?(Vector3D[10.0, 15.0, 10.0], $tolerance))
|
|
85
|
+
assert_equal(true, @t02.include?(Vector3D[15.0, 15.0, 10.0], $tolerance))
|
|
86
|
+
|
|
87
|
+
# out
|
|
88
|
+
assert_equal(false, @t02.include?(Vector3D[ 30.0, 10.0, 10.0], $tolerance))
|
|
89
|
+
assert_equal(false, @t02.include?(Vector3D[ 10.0, 30.0, 10.0], $tolerance))
|
|
90
|
+
assert_equal(false, @t02.include?(Vector3D[-10.0, 10.0, 10.0], $tolerance))
|
|
91
|
+
assert_equal(false, @t02.include?(Vector3D[ 10.0,-10.0, 10.0], $tolerance))
|
|
92
|
+
assert_equal(false, @t02.include?(Vector3D[ 10.0, 10.0, 0.0], $tolerance))
|
|
93
|
+
|
|
94
|
+
assert_equal(false, @t03.include?(Vector3D[ 3.0, 6.0, 30.0], $tolerance))
|
|
95
|
+
|
|
96
|
+
# 計算誤差
|
|
97
|
+
assert_equal(false, @t01.include?(Vector3D[ 0.3, 0.3, 0.3], $tolerance))
|
|
98
|
+
assert_equal(true, @t01.include?(Vector3D[ 0.3, 0.3, 0.3], 1.0))
|
|
99
|
+
end
|
|
100
|
+
|
|
101
|
+
def test_intersection
|
|
102
|
+
# 平行
|
|
103
|
+
pos0 = Vector3D[0.0, 0.0, 2.0]
|
|
104
|
+
pos1 = Vector3D[2.0, 0.0, 2.0]
|
|
105
|
+
seg01 = Segment.new(pos0, pos1)
|
|
106
|
+
assert_raise(Triangle::NoIntersectionError){ @t00.intersection(seg01, $tolerance) }
|
|
107
|
+
|
|
108
|
+
# 面に含まれる
|
|
109
|
+
pos0 = Vector3D[0.0, 0.0, 0.0]
|
|
110
|
+
pos1 = Vector3D[2.0, 0.0, 0.0]
|
|
111
|
+
seg01 = Segment.new(pos0, pos1)
|
|
112
|
+
assert_raise(Triangle::NoIntersectionError){ @t00.intersection(seg01, $tolerance) }
|
|
113
|
+
|
|
114
|
+
# 平行ではないが、三角形の外を通過。
|
|
115
|
+
pos2 = Vector3D[0.0,10.0, 0.0]
|
|
116
|
+
pos3 = Vector3D[0.0,10.0, 2.0]
|
|
117
|
+
seg01 = Segment.new(pos0, pos1)
|
|
118
|
+
assert_raise(Triangle::NoIntersectionError){ @t00.intersection(seg01, $tolerance) }
|
|
119
|
+
|
|
120
|
+
# 三角形を通る
|
|
121
|
+
pos2 = Vector3D[0.5, 0.5,-1.0]
|
|
122
|
+
pos3 = Vector3D[0.5, 0.5, 2.0]
|
|
123
|
+
seg01 = Segment.new(pos2, pos3)
|
|
124
|
+
assert_equal(Vector3D[0.5, 0.5, 0.0], @t00.intersection(seg01, $tolerance))
|
|
125
|
+
#
|
|
126
|
+
pos2 = Vector3D[0.5, 0.5, 0.0]
|
|
127
|
+
pos3 = Vector3D[0.5, 0.5, 1.0]
|
|
128
|
+
seg01 = Segment.new(pos2, pos3)
|
|
129
|
+
assert_equal(Vector3D[0.5, 0.5, 0.0], @t00.intersection(seg01, $tolerance))
|
|
130
|
+
#
|
|
131
|
+
pos2 = Vector3D[ 1.5, 1.5, 1.0]
|
|
132
|
+
pos3 = Vector3D[-0.5,-0.5,-1.0]
|
|
133
|
+
seg01 = Segment.new(pos2, pos3)
|
|
134
|
+
assert_equal(Vector3D[0.5, 0.5, 0.0], @t00.intersection(seg01, $tolerance))
|
|
135
|
+
#
|
|
136
|
+
pos2 = Vector3D[ 0.00, 0.00, 0.00]
|
|
137
|
+
pos3 = Vector3D[ 1.00, 1.00, 1.00]
|
|
138
|
+
seg01 = Segment.new(pos2, pos3)
|
|
139
|
+
t = @t01.intersection(seg01, $tolerance)
|
|
140
|
+
assert_in_delta(1.0/3.0, t[0], $tolerance)
|
|
141
|
+
assert_in_delta(1.0/3.0, t[1], $tolerance)
|
|
142
|
+
assert_in_delta(1.0/3.0, t[2], $tolerance)
|
|
143
|
+
#
|
|
144
|
+
pos2 = Vector3D[ 0.25, 0.25, 0.00]
|
|
145
|
+
pos3 = Vector3D[ 0.25, 0.25, 1.00]
|
|
146
|
+
seg01 = Segment.new(pos2, pos3)
|
|
147
|
+
assert_equal(Vector3D[0.25, 0.25, 0.50], @t01.intersection(seg01, $tolerance))
|
|
148
|
+
end
|
|
149
|
+
|
|
150
|
+
#def test_intersect?
|
|
151
|
+
# TODO
|
|
152
|
+
#end
|
|
153
|
+
|
|
154
|
+
def test_parallel_segment?
|
|
155
|
+
# 平行
|
|
156
|
+
pos0 = Vector3D[0.0, 0.0, 2.0]
|
|
157
|
+
pos1 = Vector3D[2.0, 0.0, 2.0]
|
|
158
|
+
seg01 = Segment.new(pos0, pos1)
|
|
159
|
+
assert_equal(true , @t00.parallel_segment?(seg01))
|
|
160
|
+
|
|
161
|
+
# 面に含まれる
|
|
162
|
+
pos0 = Vector3D[0.0, 0.0, 0.0]
|
|
163
|
+
pos1 = Vector3D[2.0, 0.0, 0.0]
|
|
164
|
+
seg01 = Segment.new(pos0, pos1)
|
|
165
|
+
assert_equal(false, @t00.parallel_segment?(seg01))
|
|
166
|
+
|
|
167
|
+
# 平行ではない。
|
|
168
|
+
pos2 = Vector3D[0.0,10.0, 0.0]
|
|
169
|
+
pos3 = Vector3D[0.0,10.0, 2.0]
|
|
170
|
+
seg01 = Segment.new(pos0, pos1)
|
|
171
|
+
assert_equal(false, @t00.parallel_segment?(seg01))
|
|
172
|
+
end
|
|
173
|
+
|
|
174
|
+
def test_normal_vector
|
|
175
|
+
t = Vector3D[0.0, 0.0, 1.0]
|
|
176
|
+
assert_equal(t, @t00.normal_vector)
|
|
177
|
+
|
|
178
|
+
t = Vector3D[1.0, 1.0, 1.0]
|
|
179
|
+
t = t * (1.0/t.r)
|
|
180
|
+
assert_equal(t, @t01.normal_vector)
|
|
181
|
+
|
|
182
|
+
t = Vector3D[0.0, 0.0, 1.0]
|
|
183
|
+
assert_equal(t, @t02.normal_vector)
|
|
184
|
+
end
|
|
185
|
+
|
|
186
|
+
def test_equivalent?
|
|
187
|
+
assert_raise(Triangle::TypeError){
|
|
188
|
+
@t00.equivalent?([[ 0.0, 0.0, 0.0], [ 1.0, 0.0, 0.0], [ 0.0, 1.0, 0.0]])
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
t = Triangle.new([[ 0.0, 0.0, 0.0], [ 1.0, 0.0, 0.0], [ 0.0, 1.0, 0.0]])
|
|
192
|
+
assert_equal(true , @t00.eql?(t))
|
|
193
|
+
|
|
194
|
+
t = Triangle.new([[ 1.0, 0.0, 0.0], [ 0.0, 0.0, 0.0], [ 0.0, 1.0, 0.0]])
|
|
195
|
+
assert_equal(true , @t00.eql?(t))
|
|
196
|
+
|
|
197
|
+
t = Triangle.new([[ 1.0, 0.0, 0.0], [ 0.0, 1.0, 0.0], [ 0.0, 0.0, 0.0]])
|
|
198
|
+
assert_equal(true , @t00.eql?(t))
|
|
199
|
+
|
|
200
|
+
t = Triangle.new([[ 0.0, 0.0, 1.0], [ 1.0, 0.0, 0.0], [ 0.0, 1.0, 0.0]])
|
|
201
|
+
assert_equal(false, @t00.eql?(t))
|
|
202
|
+
|
|
203
|
+
# tolerance を設定の上 0.0
|
|
204
|
+
t = Triangle.new([[ 0.0, 0.0, 1.0], [ 1.0, 0.0, 0.0], [ 0.0, 1.0, 0.0]])
|
|
205
|
+
assert_equal(false, @t00.eql?(t, 0.0))
|
|
206
|
+
|
|
207
|
+
# tolerance を 1.0 に設定
|
|
208
|
+
t = Triangle.new([[ 0.0, 0.0, 1.0], [ 1.0, 0.0, 0.0], [ 0.0, 1.0, 0.0]])
|
|
209
|
+
assert_equal(true, @t00.eql?(t, 1.0))
|
|
210
|
+
end
|
|
211
|
+
|
|
212
|
+
def test_equal2
|
|
213
|
+
t = Triangle.new([[ 0.0, 0.0, 0.0], [ 1.0, 0.0, 0.0], [ 0.0, 1.0, 0.0]])
|
|
214
|
+
assert_equal(true , @t00 == t)
|
|
215
|
+
|
|
216
|
+
t = Triangle.new([[ 1.0, 0.0, 0.0], [ 0.0, 0.0, 0.0], [ 0.0, 1.0, 0.0]])
|
|
217
|
+
assert_equal(false, @t00 == t)
|
|
218
|
+
|
|
219
|
+
t = Triangle.new([[ 1.0, 0.0, 0.0], [ 0.0, 1.0, 0.0], [ 0.0, 0.0, 0.0]])
|
|
220
|
+
assert_equal(false, @t00 == t)
|
|
221
|
+
|
|
222
|
+
t = Triangle.new([[ 0.0, 0.0, 1.0], [ 1.0, 0.0, 0.0], [ 0.0, 1.0, 0.0]])
|
|
223
|
+
assert_equal(false, @t00 == t)
|
|
224
|
+
end
|
|
225
|
+
|
|
226
|
+
def test_internal_axes
|
|
227
|
+
@t01 = Triangle.new([[ 1.0, 0.0, 0.0], [ 0.0, 1.0, 0.0], [ 0.0, 0.0, 1.0]])
|
|
228
|
+
@t02 = Triangle.new([[10.0,10.0,10.0], [20.0,10.0,10.0], [10.0,20.0,10.0]])
|
|
229
|
+
|
|
230
|
+
t = Axes.new([[ 1.0, 0.0, 0.0], [ 0.0, 1.0, 0.0], [ 0.0, 0.0, 1.0]])
|
|
231
|
+
assert_equal(t, @t00.internal_axes)
|
|
232
|
+
|
|
233
|
+
t = @t01.internal_axes
|
|
234
|
+
assert_in_delta(-1.0, t[0][0] , $tolerance)
|
|
235
|
+
assert_in_delta( 1.0, t[0][1] , $tolerance)
|
|
236
|
+
assert_in_delta( 0.0, t[0][2] , $tolerance)
|
|
237
|
+
assert_in_delta(-1.0, t[1][0] , $tolerance)
|
|
238
|
+
assert_in_delta( 0.0, t[1][1] , $tolerance)
|
|
239
|
+
assert_in_delta( 1.0, t[1][2] , $tolerance)
|
|
240
|
+
assert_in_delta(1.0/Math.sqrt(3.0), t[2][0], $tolerance)
|
|
241
|
+
assert_in_delta(1.0/Math.sqrt(3.0), t[2][1], $tolerance)
|
|
242
|
+
assert_in_delta(1.0/Math.sqrt(3.0), t[2][2], $tolerance)
|
|
243
|
+
|
|
244
|
+
t = Axes.new([[ 10.0, 0.0, 0.0], [ 0.0, 10.0, 0.0], [ 0.0, 0.0, 1.0]])
|
|
245
|
+
assert_equal(t, @t02.internal_axes)
|
|
246
|
+
end
|
|
247
|
+
|
|
248
|
+
def test_edges
|
|
249
|
+
t = @t00.edges
|
|
250
|
+
assert_equal(3, t.size)
|
|
251
|
+
|
|
252
|
+
assert_equal(Segment.new(VEC_O, VEC_X), t[0])
|
|
253
|
+
assert_equal(Segment.new(VEC_X, VEC_Y), t[1])
|
|
254
|
+
assert_equal(Segment.new(VEC_Y, VEC_O), t[2])
|
|
255
|
+
end
|
|
256
|
+
|
|
257
|
+
end
|
|
258
|
+
|