mageo 0.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,17 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+ begin
4
+ Bundler.setup(:default, :development)
5
+ rescue Bundler::BundlerError => e
6
+ $stderr.puts e.message
7
+ $stderr.puts "Run `bundle install` to install missing gems"
8
+ exit e.status_code
9
+ end
10
+ require 'test/unit'
11
+
12
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
13
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
14
+ require 'mageo'
15
+
16
+ class Test::Unit::TestCase
17
+ end
@@ -0,0 +1,209 @@
1
+ #! /usr/bin/env ruby
2
+ # coding: utf-8
3
+
4
+ require "helper"
5
+ require "test/unit"
6
+ require "mageo/axes.rb"
7
+
8
+ class TC_Axes < Test::Unit::TestCase
9
+ $tolerance = 1.0 * 10.0**(-10)
10
+
11
+ def setup
12
+ @a10 = Axes.new([[1.0]])
13
+ @a20 = Axes.new([[1.0, 0.0], [0.0, 1.0]])
14
+ @a30 = Axes.new([[1.0, 0.0, 0.0], [0.0, 1.0, 0.0], [0.0, 0.0, 1.0]])
15
+ @a40 = Axes.new([
16
+ [1.0, 0.0, 0.0, 0.0],
17
+ [0.0, 1.0, 0.0, 0.0],
18
+ [0.0, 0.0, 1.0, 0.0],
19
+ [0.0, 0.0, 0.0, 1.0]
20
+ ]
21
+ )
22
+
23
+ @a31 = Axes.new( [ [1.0, 0.0, 0.0], [0.0, 1.0, 0.0], [0.0, 0.0, 1.0] ] )
24
+ @a32 = Axes.new( [ [0.5, 0.5, 0.0], [0.5, 0.0, 0.5], [0.0, 0.5, 0.5] ] )
25
+ @a33 = Axes.new( [ [1.0, 0.0, 0.0], [1.0, 0.0, 0.0], [0.0, 0.0, 1.0] ] )
26
+ @a34 = Axes.new( [ [0.5, 0.5, 0.0], [0.5, 0.0, 0.0], [0.0, 0.5, 0.0] ] )
27
+ @a35 = Axes.new( [ [1.0, 1.0, 1.0], [0.0, 1.0, 1.0], [0.0, 0.0, 1.0] ] )
28
+ @a36 = Axes.new( [ [1.0, 1.0, 1.0], [0.0,-1.0,-1.0], [0.0, 0.0, 1.0] ] )
29
+
30
+ @vec_x = Vector[ 1.0, 0.0, 0.0 ]
31
+ @vec_y = Vector[ 0.0, 1.0, 0.0 ]
32
+ @vec_z = Vector[ 0.0, 0.0, 1.0 ]
33
+ @vec_0 = Vector[ 0.0, 0.0, 0.0 ]
34
+ @vec_1 = Vector[ 1.0, 1.0, 0.0 ]
35
+ end
36
+
37
+ def test_initialize
38
+ assert_raise(Axes::InitializeError) { Axes.new([[]]) }
39
+ assert_raise(Axes::InitializeError) { Axes.new([[1.0, 0.0, 0.0], [0.0, 1.0, 0.0]]) }
40
+ assert_raise(Axes::InitializeError) { Axes.new([[1.0, 0.0, 0.0], [0.0, 1.0, 0.0], [1.0, 0.0, 0.0], [0.0, 1.0, 1.0]]) }
41
+ assert_raise(Axes::InitializeError) { Axes.new([[1.0, 0.0], [0.0, 1.0, 0.0], [0.0, 0.0, 1.0]]) }
42
+ assert_raise(Axes::InitializeError) { Axes.new([[1.0, 0.0, 0.0], [0.0, 1.0], [0.0, 0.0, 1.0]]) }
43
+ assert_raise(Axes::InitializeError) { Axes.new([[1.0, 0.0, 0.0], [0.0, 1.0, 0.0], [0.0, 0.0]]) }
44
+ assert_raise(Axes::InitializeError) { Axes.new([[1.0, 0.0, 0.0], [0.0, 1.0, 0.0], [0.0, 0.0, 1.0, 0.0]]) }
45
+
46
+ assert_nothing_raised{ Axes.new([["1.0", "0.0", "0.0"], [0.0, 1.0, 0.0], [0.0, 0.0, 0.0]]) }
47
+ assert_nothing_raised{ Axes.new([[1, 0, 0], [0, 1, 0], [0, 0, 0]]) }
48
+ end
49
+
50
+ def test_self_dependent?
51
+ # Righthand
52
+ assert_equal( false, Axes.dependent?( [ @vec_x, @vec_y, @vec_z ] ) )
53
+ assert_equal( false, Axes.dependent?( [ @vec_y, @vec_z, @vec_x ] ) )
54
+ assert_equal( false, Axes.dependent?( [ @vec_z, @vec_x, @vec_y ] ) )
55
+
56
+ # Lefthand
57
+ assert_equal( false, Axes.dependent?( [ @vec_z, @vec_y, @vec_x ] ) )
58
+ assert_equal( false, Axes.dependent?( [ @vec_x, @vec_z, @vec_y ] ) )
59
+ assert_equal( false, Axes.dependent?( [ @vec_y, @vec_x, @vec_z ] ) )
60
+
61
+ # Including zero vector.
62
+ assert_equal( true , Axes.dependent?( [ @vec_0, @vec_y, @vec_z ] ) )
63
+ assert_equal( true , Axes.dependent?( [ @vec_0, @vec_z, @vec_x ] ) )
64
+ assert_equal( true , Axes.dependent?( [ @vec_0, @vec_x, @vec_y ] ) )
65
+
66
+ # One vector is on the plane of residual two vectors.
67
+ assert_equal( true , Axes.dependent?( [ @vec_x, @vec_y, @vec_1 ] ) )
68
+ end
69
+
70
+ def test_self_independent?
71
+ # Righthand
72
+ assert_equal( true , Axes.independent?( [ @vec_x, @vec_y, @vec_z ] ) )
73
+ assert_equal( true , Axes.independent?( [ @vec_y, @vec_z, @vec_x ] ) )
74
+ assert_equal( true , Axes.independent?( [ @vec_z, @vec_x, @vec_y ] ) )
75
+
76
+ # Lefthand
77
+ assert_equal( true , Axes.independent?( [ @vec_z, @vec_y, @vec_x ] ) )
78
+ assert_equal( true , Axes.independent?( [ @vec_x, @vec_z, @vec_y ] ) )
79
+ assert_equal( true , Axes.independent?( [ @vec_y, @vec_x, @vec_z ] ) )
80
+
81
+ # Including zero vector.
82
+ assert_equal( false, Axes.independent?( [ @vec_0, @vec_y, @vec_z ] ) )
83
+ assert_equal( false, Axes.independent?( [ @vec_0, @vec_z, @vec_x ] ) )
84
+ assert_equal( false, Axes.independent?( [ @vec_0, @vec_x, @vec_y ] ) )
85
+
86
+ # One vector is on the plane of residual two vectors.
87
+ assert_equal( false, Axes.independent?( [ @vec_x, @vec_y, @vec_1 ] ) )
88
+ end
89
+
90
+ def test_size
91
+ assert_equal( 1, @a10.size )
92
+ assert_equal( 2, @a20.size )
93
+ assert_equal( 3, @a30.size )
94
+ assert_equal( 4, @a40.size )
95
+ end
96
+
97
+ def test_equal
98
+ assert_equal( true , @a31 == Axes.new( [ [1.0, 0.0, 0.0], [0.0, 1.0, 0.0], [0.0, 0.0, 1.0] ] ))
99
+ assert_equal( false, @a31 == Axes.new( [ [0.5, 0.5, 0.0], [0.0, 0.5, 0.5], [0.5, 0.0, 0.5] ] ))
100
+ assert_equal( false, @a31 == Axes.new( [ [1.0, 1.0, 1.0], [0.0, 1.0, 1.0], [0.0, 0.0, 1.0] ] ))
101
+
102
+ assert_equal( false, @a32 == Axes.new( [ [1.0, 0.0, 0.0], [0.0, 1.0, 0.0], [0.0, 0.0, 1.0] ] ))
103
+ assert_equal( true , @a32 == Axes.new( [ [0.5, 0.5, 0.0], [0.5, 0.0, 0.5], [0.0, 0.5, 0.5] ] ))
104
+ assert_equal( false, @a32 == Axes.new( [ [1.0, 1.0, 1.0], [0.0, 1.0, 1.0], [0.0, 0.0, 1.0] ] ))
105
+
106
+ assert_equal( false, @a35 == Axes.new( [ [1.0, 0.0, 0.0], [0.0, 1.0, 0.0], [0.0, 0.0, 1.0] ] ))
107
+ assert_equal( false, @a35 == Axes.new( [ [0.5, 0.5, 0.0], [0.0, 0.5, 0.5], [0.5, 0.0, 0.5] ] ))
108
+ assert_equal( true , @a35 == Axes.new( [ [1.0, 1.0, 1.0], [0.0, 1.0, 1.0], [0.0, 0.0, 1.0] ] ))
109
+ end
110
+
111
+ def test_dependent?
112
+ assert_equal( false, @a31.dependent? )
113
+ assert_equal( false, @a32.dependent? )
114
+ assert_equal( true , @a33.dependent? )
115
+ assert_equal( true , @a34.dependent? )
116
+ end
117
+
118
+ def test_independent?
119
+ assert_equal( true , @a31.independent? )
120
+ assert_equal( true , @a32.independent? )
121
+ assert_equal( false, @a33.independent? )
122
+ assert_equal( false, @a34.independent? )
123
+ end
124
+
125
+ def test_to_a
126
+ assert_equal( Array, @a31.to_a.class )
127
+ assert_equal( Array, @a31.to_a[0].class )
128
+ assert_equal( Array, @a31.to_a[1].class )
129
+ assert_equal( Array, @a31.to_a[2].class )
130
+ assert_in_delta( 1.0, @a31.to_a[0][0], $tolerance )
131
+ assert_in_delta( 0.0, @a31.to_a[0][1], $tolerance )
132
+ assert_in_delta( 0.0, @a31.to_a[0][2], $tolerance )
133
+ assert_in_delta( 0.0, @a31.to_a[1][0], $tolerance )
134
+ assert_in_delta( 1.0, @a31.to_a[1][1], $tolerance )
135
+ assert_in_delta( 0.0, @a31.to_a[1][2], $tolerance )
136
+ assert_in_delta( 0.0, @a31.to_a[2][0], $tolerance )
137
+ assert_in_delta( 0.0, @a31.to_a[2][1], $tolerance )
138
+ assert_in_delta( 1.0, @a31.to_a[2][2], $tolerance )
139
+
140
+ assert_equal( Array, @a35.to_a.class )
141
+ assert_equal( Array, @a35.to_a[0].class )
142
+ assert_equal( Array, @a35.to_a[1].class )
143
+ assert_equal( Array, @a35.to_a[2].class )
144
+ assert_in_delta( 1.0, @a35.to_a[0][0], $tolerance )
145
+ assert_in_delta( 1.0, @a35.to_a[0][1], $tolerance )
146
+ assert_in_delta( 1.0, @a35.to_a[0][2], $tolerance )
147
+ assert_in_delta( 0.0, @a35.to_a[1][0], $tolerance )
148
+ assert_in_delta( 1.0, @a35.to_a[1][1], $tolerance )
149
+ assert_in_delta( 1.0, @a35.to_a[1][2], $tolerance )
150
+ assert_in_delta( 0.0, @a35.to_a[2][0], $tolerance )
151
+ assert_in_delta( 0.0, @a35.to_a[2][1], $tolerance )
152
+ assert_in_delta( 1.0, @a35.to_a[2][2], $tolerance )
153
+ end
154
+
155
+ def test_paren # []
156
+ assert_equal( Vector, @a31[0].class )
157
+ assert_equal( Vector, @a31[1].class )
158
+ assert_equal( Vector, @a31[2].class )
159
+ assert_in_delta( 1.0, @a31[0][0], $tolerance )
160
+ assert_in_delta( 0.0, @a31[0][1], $tolerance )
161
+ assert_in_delta( 0.0, @a31[0][2], $tolerance )
162
+ assert_in_delta( 0.0, @a31[1][0], $tolerance )
163
+ assert_in_delta( 1.0, @a31[1][1], $tolerance )
164
+ assert_in_delta( 0.0, @a31[1][2], $tolerance )
165
+ assert_in_delta( 0.0, @a31[2][0], $tolerance )
166
+ assert_in_delta( 0.0, @a31[2][1], $tolerance )
167
+ assert_in_delta( 1.0, @a31[2][2], $tolerance )
168
+ end
169
+
170
+ def test_each
171
+ i = 0
172
+ @a31.each { |vec|
173
+ assert_equal( @a31[i], vec )
174
+ i += 1
175
+ }
176
+
177
+ assert_raise( NoMethodError ) { @a31.map{ |vec| vec * 2.0 } }
178
+ end
179
+
180
+ def test_to_a
181
+ t = @a10.to_a
182
+ assert_equal(Array, t.class)
183
+ assert_equal([[1.0]], t)
184
+
185
+ t = @a20.to_a
186
+ assert_equal(Array, t.class)
187
+ assert_equal([[1.0, 0.0], [0.0, 1.0]], t)
188
+
189
+ t = @a30.to_a
190
+ assert_equal(Array, t.class)
191
+ assert_equal([[1.0, 0.0, 0.0], [0.0, 1.0, 0.0], [0.0, 0.0, 1.0]] , t)
192
+
193
+ t = @a40.to_a
194
+ assert_equal(Array, t.class)
195
+ assert_equal( [
196
+ [1.0, 0.0, 0.0, 0.0],
197
+ [0.0, 1.0, 0.0, 0.0],
198
+ [0.0, 0.0, 1.0, 0.0],
199
+ [0.0, 0.0, 0.0, 1.0]
200
+ ], t
201
+ )
202
+
203
+ t = @a32.to_a
204
+ assert_equal(Array, t.class)
205
+ assert_equal( [ [0.5, 0.5, 0.0], [0.5, 0.0, 0.5], [0.0, 0.5, 0.5] ] , t)
206
+ end
207
+
208
+ end
209
+
@@ -0,0 +1,21 @@
1
+ #! /usr/bin/env ruby
2
+ # coding: utf-8
3
+
4
+ require "test/unit"
5
+ require "helper"
6
+ require "mageo/cylinder.rb"
7
+ require "mageo/vector3d.rb"
8
+
9
+ class TC_Cylinder < Test::Unit::TestCase
10
+ def setup
11
+ @c00 = Cylinder.new([[0.0, 1.0, 2.0], [1.0, 2.0, 3.0]], 3.0)
12
+ end
13
+
14
+ def test_initialize
15
+ assert_equal(Vector3D[0.0, 1.0, 2.0], @c00.positions[0])
16
+ assert_equal(Vector3D[1.0, 2.0, 3.0], @c00.positions[1])
17
+ assert_equal(3.0, @c00.radius)
18
+ end
19
+
20
+ end
21
+
@@ -0,0 +1,157 @@
1
+ #! /usr/bin/env ruby
2
+ # coding: utf-8
3
+
4
+ require "test/unit"
5
+ require "helper"
6
+ require "mageo/octahedron.rb"
7
+ gem "builtinextension"
8
+ require "array_include_eql.rb"
9
+
10
+ class Octahedron
11
+ public :center
12
+ end
13
+
14
+ class TC_Octahedron < Test::Unit::TestCase
15
+ $tolerance = 10**(-10)
16
+
17
+ V_X_PLUS = Vector3D[ 1, 0, 0 ]
18
+ V_Y_PLUS = Vector3D[ 0, 1, 0 ]
19
+ V_Z_PLUS = Vector3D[ 0, 0, 1 ]
20
+
21
+ V_X_MINUS = Vector3D[ -1, 0, 0 ]
22
+ V_Y_MINUS = Vector3D[ 0, -1, 0 ]
23
+ V_Z_MINUS = Vector3D[ 0, 0, -1 ]
24
+
25
+ def setup
26
+ @o00 = Octahedron.new(
27
+ [ [V_X_MINUS, V_X_PLUS ],
28
+ [V_Y_MINUS, V_Y_PLUS ],
29
+ [V_Z_MINUS, V_Z_PLUS ] ]
30
+ )
31
+ @o01 = Octahedron.new(
32
+ [ [ [ -0.5, 0.5, 0.5 ], [ 1.5, 0.5, 0.5 ] ],
33
+ [ [ 0.5, -0.5, 0.5 ], [ 0.5, 1.5, 0.5 ] ],
34
+ [ [ 0.5, 0.5, -0.5 ], [ 0.5, 0.5, 1.5 ] ] ]
35
+ )
36
+ end
37
+
38
+ def test_initialize
39
+ assert_raise( ArgumentError ){ Octahedron.new }
40
+ assert_raise( ArgumentError ){ Octahedron.new() }
41
+ assert_raise( Octahedron::InitializeError ){ Octahedron.new( nil ) }
42
+ assert_raise( Octahedron::InitializeError ){ Octahedron.new( [] ) }
43
+ assert_raise( Octahedron::InitializeError ){ Octahedron.new( [ 0, 1, 2 ] ) }
44
+ assert_raise( Octahedron::InitializeError ){ Octahedron.new( [ [], [], [] ] ) }
45
+ assert_raise( Octahedron::InitializeError ){
46
+ Octahedron.new(
47
+ [ [ V_X_MINUS, V_X_PLUS ],
48
+ [ V_Y_MINUS, V_Y_PLUS ],
49
+ [ V_Z_MINUS, [ 0, 0 ] ]
50
+ ]
51
+ )
52
+ }
53
+ assert_raise( Octahedron::InitializeError ){
54
+ Octahedron.new(
55
+ [ [ V_X_MINUS, V_X_PLUS],
56
+ [ V_Y_MINUS, [ 0, 1, 0, 2 ] ],
57
+ [ V_Z_MINUS, V_Z_PLUS ]
58
+ ]
59
+ )
60
+ }
61
+ assert_raise( Octahedron::InitializeError ){
62
+ Octahedron.new(
63
+ [ [ V_X_MINUS, V_X_PLUS ],
64
+ [ V_Y_MINUS, V_Y_PLUS ],
65
+ [ V_Z_MINUS, V_Z_PLUS ],
66
+ [ [ -5, -5, -5 ], [ 5, 5, 5 ] ]
67
+ ]
68
+ )
69
+ }
70
+
71
+ assert_raise( Octahedron::InitializeError ){
72
+ Octahedron.new(
73
+ [ [ Vector3DInternal[ -0.5, 0.5, 0.5 ], Vector3DInternal[ 1.5, 0.5, 0.5 ] ],
74
+ [ Vector3DInternal[ 0.5, -0.5, 0.5 ], Vector3DInternal[ 0.5, 1.5, 0.5 ] ],
75
+ [ Vector3DInternal[ 0.5, 0.5, -0.5 ], Vector3DInternal[ 0.5, 0.5, 1.5 ] ] ]
76
+ )
77
+ }
78
+
79
+ assert_nothing_raised{
80
+ Octahedron.new(
81
+ [ [ Vector3D[ -0.5, 0.5, 0.5 ], Vector3D[ 1.5, 0.5, 0.5 ] ],
82
+ [ Vector3D[ 0.5, -0.5, 0.5 ], Vector3D[ 0.5, 1.5, 0.5 ] ],
83
+ [ Vector3D[ 0.5, 0.5, -0.5 ], Vector3D[ 0.5, 0.5, 1.5 ] ] ]
84
+ )
85
+ }
86
+ end
87
+
88
+ def test_inside?
89
+ assert_equal( true , @o00.inside?( [0.0, 0.2, 0.4] ) )
90
+ assert_equal( false, @o00.inside?( [1.0, 0.0, 0.0] ) ) #境界上
91
+ assert_equal( false, @o00.inside?( [2.0, 0.2, 0.4] ) )
92
+ end
93
+
94
+ def test_include?
95
+ assert_equal( true , @o00.include?( [0.0, 0.2, 0.4], $tolerance ) )
96
+ assert_equal( true , @o00.include?( [1.0, 0.0, 0.0], $tolerance ) ) #境界上
97
+ assert_equal( false, @o00.include?( [2.0, 0.2, 0.4], $tolerance ) )
98
+ end
99
+
100
+ def test_volume
101
+ assert_in_delta( 8.0/6.0 , @o00.volume, $tolerance )
102
+ end
103
+
104
+ def test_center
105
+ assert_in_delta( 0.0, @o00.center[0], $tolerance)
106
+ assert_in_delta( 0.0, @o00.center[1], $tolerance)
107
+ assert_in_delta( 0.0, @o00.center[2], $tolerance)
108
+
109
+ assert_in_delta( 0.5, @o01.center[0], $tolerance)
110
+ assert_in_delta( 0.5, @o01.center[1], $tolerance)
111
+ assert_in_delta( 0.5, @o01.center[2], $tolerance)
112
+ end
113
+
114
+ def test_vertices
115
+ t = @o00.vertices
116
+ assert_equal(6, t.size)
117
+ assert_equal(true , t.include_eql?(V_X_MINUS))
118
+ assert_equal(true , t.include_eql?(V_X_PLUS ))
119
+ assert_equal(true , t.include_eql?(V_Y_MINUS))
120
+ assert_equal(true , t.include_eql?(V_Y_PLUS ))
121
+ assert_equal(true , t.include_eql?(V_Z_MINUS))
122
+ assert_equal(true , t.include_eql?(V_Z_PLUS ))
123
+ end
124
+
125
+ def test_triangles
126
+ t = @o00.triangles
127
+ assert_equal(8, t.size)
128
+ assert_equal(true, t.include_eql?(Triangle.new([V_X_PLUS, V_Y_PLUS, V_Z_PLUS])))
129
+ assert_equal(true, t.include_eql?(Triangle.new([V_X_PLUS, V_Y_PLUS, V_Z_MINUS])))
130
+ assert_equal(true, t.include_eql?(Triangle.new([V_X_PLUS, V_Y_MINUS, V_Z_PLUS])))
131
+ assert_equal(true, t.include_eql?(Triangle.new([V_X_PLUS, V_Y_MINUS, V_Z_MINUS])))
132
+ assert_equal(true, t.include_eql?(Triangle.new([V_X_MINUS, V_Y_PLUS, V_Z_PLUS])))
133
+ assert_equal(true, t.include_eql?(Triangle.new([V_X_MINUS, V_Y_PLUS, V_Z_MINUS])))
134
+ assert_equal(true, t.include_eql?(Triangle.new([V_X_MINUS, V_Y_MINUS, V_Z_PLUS])))
135
+ assert_equal(true, t.include_eql?(Triangle.new([V_X_MINUS, V_Y_MINUS, V_Z_MINUS])))
136
+ end
137
+
138
+ def test_edges
139
+ t = @o00.edges
140
+ assert_equal(12, t.size)
141
+ assert_equal(true, (t.include_eql?(Segment.new(V_X_PLUS , V_Y_PLUS))))
142
+ assert_equal(true, (t.include_eql?(Segment.new(V_X_PLUS , V_Y_MINUS))))
143
+ assert_equal(true, (t.include_eql?(Segment.new(V_X_PLUS , V_Z_PLUS))))
144
+ assert_equal(true, (t.include_eql?(Segment.new(V_X_PLUS , V_Z_MINUS))))
145
+ assert_equal(true, (t.include_eql?(Segment.new(V_X_MINUS, V_Y_PLUS))))
146
+ assert_equal(true, (t.include_eql?(Segment.new(V_X_MINUS, V_Y_MINUS))))
147
+ assert_equal(true, (t.include_eql?(Segment.new(V_X_MINUS, V_Z_PLUS))))
148
+ assert_equal(true, (t.include_eql?(Segment.new(V_X_MINUS, V_Z_MINUS))))
149
+ assert_equal(true, (t.include_eql?(Segment.new(V_Y_PLUS , V_Z_PLUS))))
150
+ assert_equal(true, (t.include_eql?(Segment.new(V_Y_PLUS , V_Z_MINUS))))
151
+ assert_equal(true, (t.include_eql?(Segment.new(V_Y_MINUS, V_Z_PLUS))))
152
+ assert_equal(true, (t.include_eql?(Segment.new(V_Y_MINUS, V_Z_MINUS))))
153
+ end
154
+
155
+
156
+ end
157
+
@@ -0,0 +1,153 @@
1
+ #! /usr/bin/env ruby
2
+ # coding: utf-8
3
+
4
+ require "test/unit"
5
+ require "helper"
6
+ require "mageo/vector.rb"
7
+ require "mageo/polar2d.rb"
8
+
9
+
10
+ class TC_Vector < Test::Unit::TestCase
11
+ include Math
12
+
13
+ $tolerance = 10.0**(-10)
14
+
15
+ def setup
16
+ @v0 = Vector[ 1.0, 2.0, 3.0 ]
17
+ @v1 = Vector[ 1.0, 1.0 ]
18
+ @v2 = Vector[ 1.0, 2.0, 3.0, 4.0 ]
19
+ end
20
+
21
+ def test_to_p2d
22
+ assert_raise( Vector::SizeError ){ @v0.to_p2d }
23
+ assert_raise( Vector::SizeError ){ @v2.to_p2d }
24
+ assert_equal( Polar2D , @v1.to_p2d.class )
25
+ assert_equal( Math::sqrt(2.0), @v1.to_p2d.r )
26
+ assert_equal( 0.25*PI , @v1.to_p2d.theta )
27
+
28
+ assert_in_delta( 0.00*PI, Vector[ 0.0, 0.0 ].to_p2d.theta, $tolerance )
29
+ assert_in_delta( 0.00*PI, Vector[ 2.0, 0.0 ].to_p2d.theta, $tolerance )
30
+ assert_in_delta( 0.25*PI, Vector[ 2.0, 2.0 ].to_p2d.theta, $tolerance )
31
+ assert_in_delta( 0.50*PI, Vector[ 0.0, 2.0 ].to_p2d.theta, $tolerance )
32
+ assert_in_delta( 0.75*PI, Vector[ -2.0, 2.0 ].to_p2d.theta, $tolerance )
33
+ assert_in_delta( 1.00*PI, Vector[ -2.0, 0.0 ].to_p2d.theta, $tolerance )
34
+ assert_in_delta( 1.25*PI, Vector[ -2.0, -2.0 ].to_p2d.theta, $tolerance )
35
+ assert_in_delta( 1.50*PI, Vector[ 0.0, -2.0 ].to_p2d.theta, $tolerance )
36
+ assert_in_delta( 1.75*PI, Vector[ 2.0, -2.0 ].to_p2d.theta, $tolerance )
37
+ end
38
+ end
39
+
40
+
41
+ class TC_Polar2D < Test::Unit::TestCase
42
+ $tolerance = 10**(-10)
43
+
44
+ include Math
45
+
46
+ def setup
47
+ @p2d00 = Polar2D.new( 0.0, 0.0*PI)
48
+ @p2d01 = Polar2D.new( 0.0, 2.0*PI)
49
+ @p2d02 = Polar2D.new( 2.0, 0.0*PI)
50
+ @p2d03 = Polar2D.new( 2.0, 0.1*PI)
51
+ end
52
+
53
+ def test_to_v
54
+ assert_equal( Vector[ 0.0, 0.0 ], @p2d00.to_v )
55
+ assert_equal( Vector[ 0.0, 0.0 ], @p2d01.to_v )
56
+ assert_equal( Vector[ 2.0, 0.0 ], @p2d02.to_v )
57
+ assert_equal( Vector[ 2.0*cos(0.1*PI), 2.0*sin(0.1*PI) ], @p2d03.to_v )
58
+ end
59
+
60
+ def test_rotate
61
+ assert_equal( Polar2D, @p2d00.rotate( 0.2 * PI ).class )
62
+
63
+ assert_in_delta( 0.0 , @p2d00.rotate( 0.2 * PI ).r , $tolerance )
64
+ assert_in_delta( 0.2*PI, @p2d00.rotate( 0.2 * PI ).theta, $tolerance )
65
+ assert_in_delta( 0.0 , @p2d01.rotate( 0.2 * PI ).r , $tolerance )
66
+ assert_in_delta( 2.2*PI, @p2d01.rotate( 0.2 * PI ).theta, $tolerance )
67
+ assert_in_delta( 2.0 , @p2d02.rotate( 0.2 * PI ).r , $tolerance )
68
+ assert_in_delta( 0.2*PI, @p2d02.rotate( 0.2 * PI ).theta, $tolerance )
69
+ assert_in_delta( 2.0 , @p2d03.rotate( 0.2 * PI ).r , $tolerance )
70
+ assert_in_delta( 0.3*PI, @p2d03.rotate( 0.2 * PI ).theta, $tolerance )
71
+
72
+ #変化していないことを確認
73
+ assert_equal( Vector[ 0.0, 0.0 ], @p2d00.to_v )
74
+ assert_equal( Vector[ 0.0, 0.0 ], @p2d01.to_v )
75
+ assert_equal( Vector[ 2.0, 0.0 ], @p2d02.to_v )
76
+ assert_equal( Vector[ 2.0*cos(0.1*PI), 2.0*sin(0.1*PI) ], @p2d03.to_v )
77
+ end
78
+
79
+ def test_rotate!
80
+ @p2d00.rotate!( 0.2 * PI )
81
+ @p2d01.rotate!( 0.2 * PI )
82
+ @p2d02.rotate!( 0.2 * PI )
83
+ @p2d03.rotate!( 0.2 * PI )
84
+
85
+ assert_in_delta( 0.0 , @p2d00.r , $tolerance )
86
+ assert_in_delta( 0.2*PI, @p2d00.theta, $tolerance )
87
+ assert_in_delta( 0.0 , @p2d01.r , $tolerance )
88
+ assert_in_delta( 2.2*PI, @p2d01.theta, $tolerance )
89
+ assert_in_delta( 2.0 , @p2d02.r , $tolerance )
90
+ assert_in_delta( 0.2*PI, @p2d02.theta, $tolerance )
91
+ assert_in_delta( 2.0 , @p2d03.r , $tolerance )
92
+ assert_in_delta( 0.3*PI, @p2d03.theta, $tolerance )
93
+ end
94
+
95
+ def test_minimize_theta!
96
+ p2pA = Polar2D.new( 2.0, -2.5*PI )
97
+ p2pA.minimize_theta!
98
+ assert_in_delta( 1.5*PI, p2pA.theta, $tolerance )
99
+
100
+ p2pB = Polar2D.new( 2.0, -0.5*PI )
101
+ p2pB.minimize_theta!
102
+ assert_in_delta( 1.5*PI, p2pB.theta, $tolerance )
103
+
104
+ p2pC = Polar2D.new( 2.0, 1.5*PI )
105
+ p2pC.minimize_theta!
106
+ assert_in_delta( 1.5*PI, p2pC.theta, $tolerance )
107
+
108
+ p2pD = Polar2D.new( 2.0, 3.5*PI )
109
+ p2pD.minimize_theta!
110
+ assert_in_delta( 1.5*PI, p2pD.theta, $tolerance )
111
+
112
+ p2pE = Polar2D.new( 2.0, 5.5*PI )
113
+ p2pE.minimize_theta!
114
+ assert_in_delta( 1.5*PI, p2pE.theta, $tolerance )
115
+
116
+ p2pF = Polar2D.new( 2.0, 4.5*PI )
117
+ p2pF.minimize_theta!
118
+ assert_in_delta( 0.5*PI, p2pF.theta, $tolerance )
119
+
120
+ end
121
+
122
+ def test_minimize_theta
123
+ p2pA = Polar2D.new( 2.0, -2.5*PI ).minimize_theta
124
+ assert_in_delta( 1.5*PI, p2pA.theta, $tolerance )
125
+
126
+ p2pB = Polar2D.new( 2.0, -0.5*PI ).minimize_theta
127
+ assert_in_delta( 1.5*PI, p2pB.theta, $tolerance )
128
+
129
+ p2pC = Polar2D.new( 2.0, 1.5*PI ).minimize_theta
130
+ assert_in_delta( 1.5*PI, p2pC.theta, $tolerance )
131
+
132
+ p2pD = Polar2D.new( 2.0, 3.5*PI ).minimize_theta
133
+ assert_in_delta( 1.5*PI, p2pD.theta, $tolerance )
134
+
135
+ p2pE = Polar2D.new( 2.0, 5.5*PI ).minimize_theta
136
+ assert_in_delta( 1.5*PI, p2pE.theta, $tolerance )
137
+
138
+ p2pF = Polar2D.new( 2.0, 4.5*PI ).minimize_theta
139
+ assert_in_delta( 0.5*PI, p2pF.theta, $tolerance )
140
+
141
+ end
142
+
143
+ def test_minimum_radian
144
+ assert_in_delta( 1.5*PI, Polar2D.minimum_radian( -2.5*PI ), $tolerance )
145
+ assert_in_delta( 1.5*PI, Polar2D.minimum_radian( -0.5*PI ), $tolerance )
146
+ assert_in_delta( 1.5*PI, Polar2D.minimum_radian( 1.5*PI ), $tolerance )
147
+ assert_in_delta( 1.5*PI, Polar2D.minimum_radian( 3.5*PI ), $tolerance )
148
+ assert_in_delta( 0.5*PI, Polar2D.minimum_radian( 4.5*PI ), $tolerance )
149
+ assert_in_delta( 1.5*PI, Polar2D.minimum_radian( 5.5*PI ), $tolerance )
150
+ end
151
+
152
+ end
153
+