mageo 0.0.0 → 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGES +9 -0
- data/Gemfile +5 -4
- data/VERSION +1 -1
- data/lib/mageo/axes.rb +77 -78
- data/lib/mageo/cylinder.rb +10 -11
- data/lib/mageo/octahedron.rb +48 -49
- data/lib/mageo/polar2d.rb +78 -79
- data/lib/mageo/polar3d.rb +33 -34
- data/lib/mageo/polyhedron.rb +60 -61
- data/lib/mageo/segment.rb +73 -74
- data/lib/mageo/sphere.rb +6 -7
- data/lib/mageo/tetrahedron.rb +26 -27
- data/lib/mageo/triangle.rb +208 -209
- data/lib/mageo/vector.rb +13 -14
- data/lib/mageo/vector3d.rb +212 -213
- data/lib/mageo/vector3dinternal.rb +107 -108
- data/mageo.gemspec +19 -15
- data/test/test_axes.rb +198 -199
- data/test/test_cylinder.rb +8 -9
- data/test/test_octahedron.rb +140 -142
- data/test/test_polar2d.rb +136 -137
- data/test/test_polar3d.rb +61 -63
- data/test/test_polyhedron.rb +3 -3
- data/test/test_segment.rb +71 -72
- data/test/test_sphere.rb +7 -8
- data/test/test_tetrahedron.rb +115 -117
- data/test/test_triangle.rb +244 -245
- data/test/test_vector.rb +53 -54
- data/test/test_vector3d.rb +409 -411
- data/test/test_vector3dinternal.rb +133 -134
- metadata +34 -52
data/CHANGES
ADDED
data/Gemfile
CHANGED
@@ -6,10 +6,11 @@ source "http://rubygems.org"
|
|
6
6
|
# Add dependencies to develop your gem here.
|
7
7
|
# Include everything needed to run rake, tests, features, etc.
|
8
8
|
group :development do
|
9
|
-
gem "
|
10
|
-
gem "bundler", "~> 1.
|
9
|
+
gem "builtinextension", ">= 0"
|
10
|
+
gem "bundler", "~> 1.2.2"
|
11
11
|
gem "jeweler", "~> 1.8.3"
|
12
|
+
gem "malge", ">= 0.0.2"
|
13
|
+
gem "psych", ">= 0"
|
14
|
+
gem "rdoc", "~> 3.12"
|
12
15
|
gem "simplecov", ">= 0"
|
13
|
-
gem "builtinextension", ">= 0"
|
14
|
-
gem "malge", ">= 0.0.1"
|
15
16
|
end
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.0.
|
1
|
+
0.0.1
|
data/lib/mageo/axes.rb
CHANGED
@@ -5,83 +5,82 @@ require "matrix"
|
|
5
5
|
|
6
6
|
# n 次元空間における座標系を表現する n 本のベクトルを扱うクラス。
|
7
7
|
class Axes
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
8
|
+
attr_reader :axes
|
9
|
+
|
10
|
+
class InitializeError < Exception; end
|
11
|
+
|
12
|
+
|
13
|
+
# 要素数が nxn でなければ例外。0x0 も例外。
|
14
|
+
# 角各要素は to_f メソッドを持たなければならない。
|
15
|
+
def initialize(vectors)
|
16
|
+
raise InitializeError if vectors.size == 0
|
17
|
+
@axes = vectors.map{|vector| Vector[*vector]}
|
18
|
+
|
19
|
+
#raise InitializeError unless @axes.regular? # この判定はうまくいかない。バグ?
|
20
|
+
@axes.each do |vector|
|
21
|
+
raise InitializeError unless @axes.size == vector.size
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
def self.independent?(axes)
|
26
|
+
return Matrix[* axes.map{|i| i.to_a}].regular?
|
27
|
+
end
|
28
|
+
|
29
|
+
# Return true is the vector is not independent.
|
30
|
+
def self.dependent?(axes)
|
31
|
+
return ! (self.independent?(axes))
|
32
|
+
end
|
33
|
+
|
34
|
+
# Return a number of vectors in axes, always three.
|
35
|
+
# Mimic for Array.
|
36
|
+
def size
|
37
|
+
return @axes.size
|
38
|
+
end
|
39
|
+
|
40
|
+
# Check equal.
|
41
|
+
# Usually, this method should not be used, except for tests.
|
42
|
+
def ==(other)
|
43
|
+
result = true
|
44
|
+
size.times do |i|
|
45
|
+
size.times do |j|
|
46
|
+
result = false if @axes[i][j] != other.axes[i][j]
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
return result
|
51
|
+
end
|
52
|
+
|
53
|
+
def dependent?
|
54
|
+
self.class.dependent?(@axes)
|
55
|
+
end
|
56
|
+
|
57
|
+
def independent?
|
58
|
+
self.class.independent?(@axes)
|
59
|
+
end
|
60
|
+
|
61
|
+
def to_a
|
62
|
+
result = [
|
63
|
+
@axes[0].to_a,
|
64
|
+
@axes[1].to_a,
|
65
|
+
@axes[2].to_a,
|
66
|
+
]
|
67
|
+
return result
|
68
|
+
end
|
69
|
+
|
70
|
+
# Item access for three axes in cartesian coordinates.
|
71
|
+
# Note: []= method is not provided.
|
72
|
+
def [](index)
|
73
|
+
@axes[ index ]
|
74
|
+
end
|
75
|
+
|
76
|
+
# Iterate each vector of axes.
|
77
|
+
def each
|
78
|
+
@axes.each { |i| yield(i) }
|
79
|
+
end
|
80
|
+
|
81
|
+
# Convert to Array. Non-destructive.
|
82
|
+
def to_a
|
83
|
+
return @axes.map { |vector| vector.to_a }
|
84
|
+
end
|
86
85
|
end
|
87
86
|
|
data/lib/mageo/cylinder.rb
CHANGED
@@ -7,17 +7,16 @@ require "mageo/vector3d.rb"
|
|
7
7
|
# 球を表現するクラス。
|
8
8
|
#
|
9
9
|
class Cylinder
|
10
|
-
|
11
|
-
|
12
|
-
# 座標と半径
|
13
|
-
# positions は 両底面の中心座標を入れた配列。
|
14
|
-
def initialize(position, radius)
|
15
|
-
@positions = [
|
16
|
-
Vector3D[*position[0]],
|
17
|
-
Vector3D[*position[1]]
|
18
|
-
]
|
19
|
-
@radius = radius
|
20
|
-
end
|
10
|
+
attr_reader :positions, :radius
|
21
11
|
|
12
|
+
# 座標と半径
|
13
|
+
# positions は 両底面の中心座標を入れた配列。
|
14
|
+
def initialize(position, radius)
|
15
|
+
@positions = [
|
16
|
+
Vector3D[*position[0]],
|
17
|
+
Vector3D[*position[1]]
|
18
|
+
]
|
19
|
+
@radius = radius
|
20
|
+
end
|
22
21
|
end
|
23
22
|
|
data/lib/mageo/octahedron.rb
CHANGED
@@ -10,53 +10,52 @@ require "mageo/polyhedron.rb"
|
|
10
10
|
# 3次元空間中の八面体を表現するクラス
|
11
11
|
class Octahedron < Polyhedron
|
12
12
|
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
13
|
+
class InitializeError < Exception; end
|
14
|
+
|
15
|
+
#八面体は 6個の頂点で構成されるが、これを3組の対体角で指定する。
|
16
|
+
#e.g.,
|
17
|
+
# [
|
18
|
+
# [ [ -1, 0, 0 ], [ 1, 0, 0 ] ],
|
19
|
+
# [ [ 0, -1, 0 ], [ 0, 1, 0 ] ],
|
20
|
+
# [ [ 0, 0, -1 ], [ 0, 0, 1 ] ],
|
21
|
+
# ]
|
22
|
+
#
|
23
|
+
#凸包であることのチェックは難しいのでしない。
|
24
|
+
#TODO: 頂点が重複している、面上にあるなどのチェックも本来はすべきだが、入れていない。
|
25
|
+
def initialize( pairs )
|
26
|
+
raise InitializeError if pairs.class != Array
|
27
|
+
raise InitializeError if pairs.size != 3
|
28
|
+
pairs.each do |pair|
|
29
|
+
raise InitializeError unless pair.class == Array
|
30
|
+
raise InitializeError if pair.size != 2
|
31
|
+
pair.each do |pos|
|
32
|
+
raise InitializeError if pos.size != 3
|
33
|
+
raise InitializeError unless pos.methods.include?( :[] )
|
34
|
+
raise InitializeError unless pos.methods.include?( :map )
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
pairs.flatten.each do |vertex|
|
39
|
+
raise InitializeError if vertex.class == Vector3DInternal
|
40
|
+
end
|
41
|
+
|
42
|
+
|
43
|
+
@vertices = []
|
44
|
+
pairs.each do |pair|
|
45
|
+
pair.each do |vertex|
|
46
|
+
@vertices << vertex.to_v3d
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
@vertex_indices_of_triangles = [
|
51
|
+
[ 0, 2, 4],
|
52
|
+
[ 0, 2, 5],
|
53
|
+
[ 0, 3, 4],
|
54
|
+
[ 0, 3, 5],
|
55
|
+
[ 1, 2, 4],
|
56
|
+
[ 1, 2, 5],
|
57
|
+
[ 1, 3, 4],
|
58
|
+
[ 1, 3, 5]
|
59
|
+
]
|
60
|
+
end
|
62
61
|
end
|
data/lib/mageo/polar2d.rb
CHANGED
@@ -5,32 +5,32 @@ require 'matrix'
|
|
5
5
|
|
6
6
|
|
7
7
|
class Vector
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
8
|
+
include Math
|
9
|
+
|
10
|
+
#Polar2D クラスインスタンスへの変換。
|
11
|
+
def to_p2d
|
12
|
+
raise Vector::SizeError if self.size != 2
|
13
|
+
x, y = *self
|
14
|
+
r = Math::sqrt( x**2 + y**2 )
|
15
|
+
|
16
|
+
theta = 0.0
|
17
|
+
#ゼロ割り対策
|
18
|
+
if ( ( x == 0 ) && ( y == 0 ) )
|
19
|
+
theta = 0.0 * PI
|
20
|
+
elsif ( ( x == 0 ) && ( y > 0 ) )
|
21
|
+
theta = 0.5 * PI
|
22
|
+
elsif ( ( x == 0 ) && ( y < 0 ) )
|
23
|
+
theta = -0.5 * PI
|
24
|
+
elsif ( ( x > 0 ) && ( y == 0 ) )
|
25
|
+
theta = 0.0 * PI
|
26
|
+
elsif ( ( x < 0 ) && ( y == 0 ) )
|
27
|
+
theta = 1.0 * PI
|
28
|
+
else
|
29
|
+
theta = Math::atan( y/x )
|
30
|
+
theta += PI if ( x < 0)
|
31
|
+
end
|
32
|
+
Polar2D.new( r, theta ).minimize_theta
|
33
|
+
end
|
34
34
|
end
|
35
35
|
|
36
36
|
#2次元極座標。
|
@@ -39,57 +39,56 @@ end
|
|
39
39
|
#まあ人間用に degree 用インターフェイスも用意することもあるかもしれんが。
|
40
40
|
class Polar2D
|
41
41
|
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
42
|
+
include Math
|
43
|
+
|
44
|
+
##クラスメソッド
|
45
|
+
|
46
|
+
#与えられた角度 radian を 0 <= radian < 2*PI の間の角度に変換する破壊破壊的メソッド。
|
47
|
+
def Polar2D.minimum_radian( radian )
|
48
|
+
tmp = ( radian / (2.0*PI) )
|
49
|
+
tmp = tmp - tmp.floor
|
50
|
+
(2.0*PI) * tmp
|
51
|
+
end
|
52
|
+
|
53
|
+
|
54
|
+
##インスタンスメソッド
|
55
|
+
|
56
|
+
attr_reader :r, :theta
|
57
|
+
|
58
|
+
#
|
59
|
+
def initialize( r, theta)
|
60
|
+
@r = r
|
61
|
+
@theta = theta
|
62
|
+
end
|
63
|
+
|
64
|
+
#2次元 Vector に変換。
|
65
|
+
def to_v
|
66
|
+
Vector[ @r * cos( @theta ), @r * sin( @theta ) ]
|
67
|
+
end
|
68
|
+
|
69
|
+
#極座標を回転させる破壊的メソッド。
|
70
|
+
#radian は左回りを正方向とした角度(ラジアン)。
|
71
|
+
def rotate!( radian )
|
72
|
+
@theta += radian
|
73
|
+
end
|
74
|
+
|
75
|
+
#極座標を回転させたオブジェクトを返す非破壊破壊的メソッド。
|
76
|
+
#theta は左回りを正方向とした角度。
|
77
|
+
def rotate( radian )
|
78
|
+
result = Marshal.load( Marshal.dump( self ) )
|
79
|
+
result.rotate!( radian )
|
80
|
+
result
|
81
|
+
end
|
82
|
+
|
83
|
+
#theta を 0 <= theta < 2*PI の間の角度に変換する破壊破壊的メソッド。
|
84
|
+
def minimize_theta!
|
85
|
+
@theta = Polar2D.minimum_radian( @theta )
|
86
|
+
end
|
87
|
+
|
88
|
+
#theta を 0 <= theta < 2*PI の間の角度に変換したオブジェクトを返す非破壊破壊的メソッド。
|
89
|
+
def minimize_theta
|
90
|
+
result = Marshal.load( Marshal.dump( self ) )
|
91
|
+
result.minimize_theta!
|
92
|
+
result
|
93
|
+
end
|
95
94
|
end
|