mageo 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGES CHANGED
@@ -1,6 +1,12 @@
1
1
  = vasputils changelog
2
2
 
3
3
  == Master (for 0.0.2)
4
+ - Make a namespace 'Mageo'.
5
+ - Most of classes are included into the namespace 'Mageo'.
6
+ - Adjust to malge-0.0.8
7
+ - Move Tetrahedron to Mageo::Tetrahedron
8
+ - Set default value of tolerance=0.0 in argument of Polyhedron#include?
9
+ - Adjust to builtinextension-0.1.0.
4
10
 
5
11
  == Version (for 0.0.1)
6
12
  - Change indent style from tab char to two spaces.
data/Gemfile CHANGED
@@ -6,11 +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 "builtinextension", ">= 0"
10
- gem "bundler", "~> 1.2.2"
9
+ gem "builtinextension", ">= 0.1.0"
10
+ gem "bundler", "~> 1.3.5"
11
11
  gem "jeweler", "~> 1.8.3"
12
- gem "malge", ">= 0.0.2"
13
- gem "psych", ">= 0"
12
+ gem "malge", ">= 0.0.8"
13
+ #gem "psych", ">= 0"
14
14
  gem "rdoc", "~> 3.12"
15
15
  gem "simplecov", ">= 0"
16
16
  end
data/Rakefile CHANGED
@@ -17,8 +17,8 @@ Jeweler::Tasks.new do |gem|
17
17
  gem.name = "mageo"
18
18
  gem.homepage = "http://github.com/ippei94da/mageo"
19
19
  gem.license = "MIT"
20
- gem.summary = %Q{MAth GEOmetry library to deal with 2 and 3 dimension space.}
21
- gem.description = %Q{MAth GEOmetry library to deal with 2 and 3 dimension space.
20
+ gem.summary = %Q{MAth GEOmetry library to deal with 2 and 3 dimensional spaces.}
21
+ gem.description = %Q{MAth GEOmetry library to deal with 2 and 3 dimensional spaces.
22
22
  Cartesian and internal coordinate systems can be used.
23
23
  This includes besic objects in 3 dimensional space.
24
24
  }
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.1
1
+ 0.0.2
data/lib/mageo.rb CHANGED
@@ -0,0 +1,20 @@
1
+ module Mageo; end
2
+
3
+ require "rubygems"
4
+ #gem "malge"
5
+ require "malge"
6
+
7
+ require "mageo/axes.rb"
8
+ require "mageo/cylinder.rb"
9
+ require "mageo/polar2d.rb"
10
+ require "mageo/polar3d.rb"
11
+ require "mageo/segment.rb"
12
+ require "mageo/sphere.rb"
13
+ require "mageo/triangle.rb"
14
+ require "mageo/vector.rb"
15
+ require "mageo/vector3d.rb"
16
+ require "mageo/vector3dinternal.rb"
17
+
18
+ require "mageo/polyhedron.rb"
19
+ require "mageo/tetrahedron.rb"
20
+ require "mageo/octahedron.rb"
data/lib/mageo/axes.rb CHANGED
@@ -1,10 +1,8 @@
1
1
  #! /usr/bin/env ruby
2
2
  # coding: utf-8
3
3
 
4
- require "matrix"
5
-
6
4
  # n 次元空間における座標系を表現する n 本のベクトルを扱うクラス。
7
- class Axes
5
+ class Mageo::Axes
8
6
  attr_reader :axes
9
7
 
10
8
  class InitializeError < Exception; end
@@ -1,20 +1,18 @@
1
1
  #! /usr/bin/env ruby
2
2
  # coding: utf-8
3
3
 
4
- require "mageo/vector3d.rb"
5
-
6
4
  #
7
- # 球を表現するクラス。
5
+ # Class for cylinder
8
6
  #
9
- class Cylinder
7
+ class Mageo::Cylinder
10
8
  attr_reader :positions, :radius
11
9
 
12
10
  # 座標と半径
13
11
  # positions は 両底面の中心座標を入れた配列。
14
12
  def initialize(position, radius)
15
13
  @positions = [
16
- Vector3D[*position[0]],
17
- Vector3D[*position[1]]
14
+ Mageo::Vector3D[*position[0]],
15
+ Mageo::Vector3D[*position[1]]
18
16
  ]
19
17
  @radius = radius
20
18
  end
@@ -1,14 +1,8 @@
1
1
  #! /usr/bin/env ruby
2
2
  # coding: utf-8
3
3
 
4
- require "mageo/vector3d.rb"
5
- require "mageo/triangle.rb"
6
- #require "mageo/tetrahedron.rb"
7
- require "mageo/polyhedron.rb"
8
-
9
-
10
4
  # 3次元空間中の八面体を表現するクラス
11
- class Octahedron < Polyhedron
5
+ class Mageo::Octahedron < Mageo::Polyhedron
12
6
 
13
7
  class InitializeError < Exception; end
14
8
 
@@ -36,7 +30,7 @@ class Octahedron < Polyhedron
36
30
  end
37
31
 
38
32
  pairs.flatten.each do |vertex|
39
- raise InitializeError if vertex.class == Vector3DInternal
33
+ raise InitializeError if vertex.class == Mageo::Vector3DInternal
40
34
  end
41
35
 
42
36
 
data/lib/mageo/polar2d.rb CHANGED
@@ -1,13 +1,10 @@
1
1
  #! /usr/bin/env ruby
2
2
  # coding: utf-8
3
3
 
4
- require 'matrix'
5
-
6
-
7
4
  class Vector
8
5
  include Math
9
6
 
10
- #Polar2D クラスインスタンスへの変換。
7
+ #Mageo::Polar2D クラスインスタンスへの変換。
11
8
  def to_p2d
12
9
  raise Vector::SizeError if self.size != 2
13
10
  x, y = *self
@@ -29,7 +26,7 @@ class Vector
29
26
  theta = Math::atan( y/x )
30
27
  theta += PI if ( x < 0)
31
28
  end
32
- Polar2D.new( r, theta ).minimize_theta
29
+ Mageo::Polar2D.new( r, theta ).minimize_theta
33
30
  end
34
31
  end
35
32
 
@@ -37,14 +34,14 @@ end
37
34
  #極座標ライブラリでは、角度は基本的に radian を使用する。
38
35
  #degree は人間の都合で決められた尺度だろう。
39
36
  #まあ人間用に degree 用インターフェイスも用意することもあるかもしれんが。
40
- class Polar2D
37
+ class Mageo::Polar2D
41
38
 
42
39
  include Math
43
40
 
44
41
  ##クラスメソッド
45
42
 
46
43
  #与えられた角度 radian を 0 <= radian < 2*PI の間の角度に変換する破壊破壊的メソッド。
47
- def Polar2D.minimum_radian( radian )
44
+ def self.minimum_radian( radian )
48
45
  tmp = ( radian / (2.0*PI) )
49
46
  tmp = tmp - tmp.floor
50
47
  (2.0*PI) * tmp
@@ -82,7 +79,7 @@ class Polar2D
82
79
 
83
80
  #theta を 0 <= theta < 2*PI の間の角度に変換する破壊破壊的メソッド。
84
81
  def minimize_theta!
85
- @theta = Polar2D.minimum_radian( @theta )
82
+ @theta = self.class.minimum_radian( @theta )
86
83
  end
87
84
 
88
85
  #theta を 0 <= theta < 2*PI の間の角度に変換したオブジェクトを返す非破壊破壊的メソッド。
data/lib/mageo/polar3d.rb CHANGED
@@ -1,14 +1,11 @@
1
1
  #! /usr/bin/env ruby
2
2
  # coding: utf-8
3
3
 
4
- require 'matrix'
5
- require 'mageo/vector3d.rb'
6
-
7
4
  #3次元極座標。
8
5
  #極座標ライブラリでは、角度は基本的に radian を使用する。
9
6
  #degree は人間の都合で決められた尺度だろう。
10
7
  #まあ人間用に degree 用インターフェイスも用意することもあるかもしれんが。
11
- class Polar3D
8
+ class Mageo::Polar3D
12
9
 
13
10
  include Math
14
11
 
@@ -27,7 +24,7 @@ class Polar3D
27
24
  x = @r * sin( @theta ) * cos( @phi )
28
25
  y = @r * sin( @theta ) * sin( @phi )
29
26
  z = @r * cos( @theta )
30
- Vector3D[ x, y, z ]
27
+ Mageo::Vector3D[ x, y, z ]
31
28
  end
32
29
 
33
30
  #phi を 0 <= phi < 2*PI の間の角度に変換する破壊破壊的メソッド。
@@ -2,24 +2,24 @@
2
2
  # coding: utf-8
3
3
 
4
4
  require "rubygems"
5
- gem "builtinextension"
6
- require "array_include_eql.rb"
5
+ gem "builtinextension"
6
+ require "array/includeeql.rb"
7
7
 
8
8
  # 多面体を表現する抽象クラス。
9
9
  # 面は必ず三角形で、たとえば四角形も2つの三角形であると考える。
10
10
  # initialize メソッドは subclass で再定義する。
11
11
  # subclass の注意点。
12
12
  # - 凸包であることを前提とする。
13
- # チェック機構は Polyhedron クラスで持っているべきだが、面倒なので後回し。
13
+ # チェック機構は Mageo::Polyhedron クラスで持っているべきだが、面倒なので後回し。
14
14
  # 3次元凸包判定の方法をぐぐったが、これといったものが見つからない。
15
15
  # - 定義された面同士の間に隙間がないことを前提とする。
16
- # チェック機構は Polyhedron クラスで持っているべきだが、面倒なので後回し。
16
+ # チェック機構は Mageo::Polyhedron クラスで持っているべきだが、面倒なので後回し。
17
17
  # - 頂点リスト @vertices と、面リスト @vertex_indices_of_triangles を持つ。
18
- # ただし、@vertex_indices_of_triangles は Triangle クラスインスタンスではなく、
18
+ # ただし、@vertex_indices_of_triangles は Mageo::Triangle クラスインスタンスではなく、
19
19
  # @vertices 内の index。
20
- # see Tetrahedron.rb
21
- # - メインのテストは 四面体 Tetrahedron クラスで行っている。
22
- class Polyhedron
20
+ # see Mageo::Tetrahedron.rb
21
+ # - メインのテストは 四面体 Mageo::Tetrahedron クラスで行っている。
22
+ class Mageo::Polyhedron
23
23
  attr_reader :vertices
24
24
 
25
25
  class TypeError < Exception; end
@@ -42,14 +42,14 @@ class Polyhedron
42
42
 
43
43
  def triangles
44
44
  results = @vertex_indices_of_triangles.map do |indices|
45
- Triangle.new( indices.map{|i| @vertices[i] } )
45
+ Mageo::Triangle.new( indices.map{|i| @vertices[i] } )
46
46
  end
47
47
  return results
48
48
  end
49
49
 
50
50
  #面で囲まれた空間の中にあれば true を返す。
51
51
  def inside?( pos )
52
- raise TypeError if pos.class == Vector3DInternal
52
+ raise TypeError if pos.class == Mageo::Vector3DInternal
53
53
 
54
54
  result = true
55
55
  triangles.each do |triangle|
@@ -58,8 +58,8 @@ class Polyhedron
58
58
  return result
59
59
  end
60
60
 
61
- def include?(pos, tolerance)
62
- raise TypeError if pos.class == Vector3DInternal
61
+ def include?(pos, tolerance = 0.0)
62
+ raise TypeError if pos.class == Mageo::Vector3DInternal
63
63
 
64
64
  return true if inside?( pos )
65
65
  triangles.each do |triangle|
@@ -75,7 +75,7 @@ class Polyhedron
75
75
  result = 0.0
76
76
  @vertex_indices_of_triangles.each do |tri_vertices|
77
77
  vectors = tri_vertices.map { |i| @vertices[i] - center }
78
- volume = Vector3D.scalar_triple_product( *vectors ).abs
78
+ volume = Mageo::Vector3D.scalar_triple_product( *vectors ).abs
79
79
  volume /= 6.0
80
80
  result += volume
81
81
  end
@@ -84,7 +84,7 @@ class Polyhedron
84
84
 
85
85
  #各頂点の座標の平均値を返す。
86
86
  def center
87
- tmp = Vector3D[ 0.0, 0.0, 0.0 ]
87
+ tmp = Mageo::Vector3D[ 0.0, 0.0, 0.0 ]
88
88
  @vertices.each do |vertex|
89
89
  tmp += vertex
90
90
  end
data/lib/mageo/segment.rb CHANGED
@@ -1,10 +1,8 @@
1
1
  #! /usr/bin/env ruby
2
2
  # coding: utf-8
3
3
 
4
- require "mageo/vector3d.rb"
5
-
6
4
  # 線分を表すクラス。
7
- class Segment
5
+ class Mageo::Segment
8
6
  attr_reader :endpoints
9
7
 
10
8
  class InitializeError < Exception; end
@@ -13,8 +11,8 @@ class Segment
13
11
 
14
12
  # 端点を2つ渡す。
15
13
  def initialize(vector0, vector1)
16
- raise InitializeError if vector0.class != Vector3D
17
- raise InitializeError if vector1.class != Vector3D
14
+ raise InitializeError if vector0.class != Mageo::Vector3D
15
+ raise InitializeError if vector1.class != Mageo::Vector3D
18
16
  raise InitializeError if vector0 == vector1
19
17
 
20
18
  @endpoints = [vector0, vector1]
@@ -22,9 +20,9 @@ class Segment
22
20
 
23
21
  # position で与えられた点が線分の中にある点か?
24
22
  # tolerance = 0.0 では計算誤差のためにほとんど真とならない。
25
- # position は Vector3D クラスインスタンスでなければならない。
23
+ # position は Mageo::Vector3D クラスインスタンスでなければならない。
26
24
  def include?(position, tolerance)
27
- raise TypeError if position.class != Vector3D
25
+ raise TypeError if position.class != Mageo::Vector3D
28
26
 
29
27
  vec_self = @endpoints[1] - @endpoints[0]
30
28
  vec_other = position - @endpoints[0]
@@ -56,7 +54,7 @@ class Segment
56
54
  end
57
55
 
58
56
  # endpoints で取り出せる座標2つのうち、最初のものから最後のものへのベクトルを表す
59
- # Vector3D クラスインスタンスを返す。
57
+ # Mageo::Vector3D クラスインスタンスを返す。
60
58
  def to_v3d
61
59
  return @endpoints[1] - @endpoints[0]
62
60
  end
@@ -64,7 +62,7 @@ class Segment
64
62
  # 等価チェック。
65
63
  # uniq できるようにするため。
66
64
  def eql?(other)
67
- raise TypeError if other.class != Segment
65
+ raise TypeError if other.class != Mageo::Segment
68
66
  @endpoints.each do |point|
69
67
  return false unless other.endpoints.include?(point)
70
68
  end
@@ -72,7 +70,7 @@ class Segment
72
70
  end
73
71
 
74
72
  def ==(other)
75
- raise TypeError if other.class != Segment
73
+ raise TypeError if other.class != Mageo::Segment
76
74
  @endpoints.size.times do |i|
77
75
  return false unless other.endpoints[i] == @endpoints[i]
78
76
  end
data/lib/mageo/sphere.rb CHANGED
@@ -1,17 +1,14 @@
1
1
  #! /usr/bin/env ruby
2
2
  # coding: utf-8
3
3
 
4
- require "mageo/vector3d.rb"
5
-
6
- #
7
4
  # 球を表現するクラス。
8
5
  #
9
- class Sphere
6
+ class Mageo::Sphere
10
7
  attr_reader :position, :radius
11
8
 
12
9
  # 座標と半径
13
10
  def initialize(position, radius)
14
- @position = Vector3D[*position]
11
+ @position = Mageo::Vector3D[*position]
15
12
  @radius = radius
16
13
  end
17
14
  end
@@ -1,15 +1,10 @@
1
1
  #! /usr/bin/env ruby
2
2
  # coding: utf-8
3
3
 
4
- require "mageo/vector3d.rb"
5
- require "mageo/triangle.rb"
6
- require "mageo/polyhedron.rb"
7
-
8
-
9
4
  #
10
5
  # 直交座標系 3次元空間内の四面体を表現するクラス。
11
6
  #
12
- class Tetrahedron < Polyhedron
7
+ class Mageo::Tetrahedron < Mageo::Polyhedron
13
8
 
14
9
  class InitializeError < Exception; end
15
10
 
@@ -23,7 +18,7 @@ class Tetrahedron < Polyhedron
23
18
  raise InitializeError unless vertex.methods.include?( :map )
24
19
  end
25
20
  vertices.each do |vertex|
26
- raise InitializeError if vertex.class == Vector3DInternal
21
+ raise InitializeError if vertex.class == Mageo::Vector3DInternal
27
22
  end
28
23
 
29
24
  @vertices = vertices.map { |vertex| vertex.to_v3d }
@@ -1,18 +1,13 @@
1
1
  #! /usr/bin/env ruby
2
2
  # coding: utf-8
3
3
 
4
- require "mageo/vector3d.rb"
5
- require "mageo/vector3dinternal.rb"
6
- require "mageo/axes.rb"
7
- require "mageo/segment.rb"
8
-
9
4
  #3次元空間中の3角形を表現するクラス。
10
5
  #
11
- #法線ベクトル( Vector3D クラスインスタンス )を返すメソッドは定義しない。
6
+ #法線ベクトル( Mageo::Vector3D クラスインスタンス )を返すメソッドは定義しない。
12
7
  # 法線ベクトルは2通りの方向を取りうるため。
13
8
  # initialize 時に点の指定の順序を決めることで定義はできるが、
14
9
  # そうすると簡潔性が損なわれる。
15
- class Triangle
10
+ class Mageo::Triangle
16
11
  attr_reader :vertices
17
12
 
18
13
  class InitializeError < Exception; end
@@ -22,11 +17,11 @@ class Triangle
22
17
  class NoIntersectionError < Exception; end
23
18
 
24
19
  #An argument 'vertices' can be Array of 3 items, Vector of 3 items,
25
- # or Vector3D class instance, which have [] and map methods.
20
+ # or Mageo::Vector3D class instance, which have [] and map methods.
26
21
  #当面は Array を前提とする。
27
22
  #座標が整数で入っていたとしても内部的には Float に変換して使用する。
28
23
  #3点が1直線上に並んでいて三角形を囲まない場合は
29
- #例外 Triangle::LinearException を投げる。
24
+ #例外 Mageo::Triangle::LinearException を投げる。
30
25
  def initialize( vertices )
31
26
  raise InitializeError unless vertices.methods.include?( :size )
32
27
  raise InitializeError if vertices.size != 3
@@ -42,7 +37,7 @@ class Triangle
42
37
  #Checking on linear.
43
38
  edge1 = @vertices[1] - @vertices[0]
44
39
  edge2 = @vertices[2] - @vertices[0]
45
- if ( Vector3D[0.0, 0.0, 0.0] == Vector3D.vector_product( edge1, edge2 ))
40
+ if ( Mageo::Vector3D[0.0, 0.0, 0.0] == Mageo::Vector3D.vector_product( edge1, edge2 ))
46
41
  raise LinearException
47
42
  end
48
43
 
@@ -51,16 +46,16 @@ class Triangle
51
46
  #引数で与えられた 2 つの座標が、三角形の面に対して同じ側にあれば true を返す。
52
47
  #どちらか、もしくは両方が、面上の点(当然頂点、辺上を含む)であれば必ず false を返す。
53
48
  def same_side?( pos0, pos1 )
54
- raise TypeError if pos0.class != Vector3D
55
- raise TypeError if pos1.class != Vector3D
49
+ raise TypeError if pos0.class != Mageo::Vector3D
50
+ raise TypeError if pos1.class != Mageo::Vector3D
56
51
 
57
52
  edge1 = @vertices[1] - @vertices[0]
58
53
  edge2 = @vertices[2] - @vertices[0]
59
54
  pos0 = pos0.to_v3d - @vertices[0]
60
55
  pos1 = pos1.to_v3d - @vertices[0]
61
56
 
62
- triple_product_pos0 = Vector3D.scalar_triple_product( edge1, edge2, pos0 )
63
- triple_product_pos1 = Vector3D.scalar_triple_product( edge1, edge2, pos1 )
57
+ triple_product_pos0 = Mageo::Vector3D.scalar_triple_product( edge1, edge2, pos0 )
58
+ triple_product_pos1 = Mageo::Vector3D.scalar_triple_product( edge1, edge2, pos1 )
64
59
  if triple_product_pos0 * triple_product_pos1 > 0
65
60
  return true
66
61
  else
@@ -69,11 +64,11 @@ class Triangle
69
64
  end
70
65
 
71
66
  # 3点で張られる面上にあり、三角形の内側にあれば true を返す。
72
- # pos が Vector3D クラスインスタンスでなければ例外。
67
+ # pos が Mageo::Vector3D クラスインスタンスでなければ例外。
73
68
  # ただし、面の法線方向には tolerance だけの許容値を設ける。
74
69
  # 計算誤差の問題のため、これを設定しないと殆ど真とならない。
75
70
  def include?(pos, tolerance)
76
- raise TypeError if pos.class != Vector3D
71
+ raise TypeError if pos.class != Mageo::Vector3D
77
72
 
78
73
  axes = internal_axes
79
74
  #一次独立チェックは initialize 時にされている筈。
@@ -167,7 +162,7 @@ class Triangle
167
162
  end
168
163
 
169
164
  # 3つの頂点の座標が順不同で対応すれば真を返す。
170
- # other が Triangle クラス以外のインスタンスなら例外 Triangle::TypeError を投げる。
165
+ # other が Mageo::Triangle クラス以外のインスタンスなら例外 Triangle::TypeError を投げる。
171
166
  # MEMO:
172
167
  # 当初 eql? という名前を付けていたが、
173
168
  # これは hash メソッドと関連があるので危険。
@@ -175,7 +170,7 @@ class Triangle
175
170
  # しかし eql? に依存したコードが残っているので当面 alias を残す。
176
171
  # そのうち obsolete する。
177
172
  def equivalent?(other, tolerance = 0.0)
178
- raise TypeError unless other.class == Triangle
173
+ raise TypeError unless other.class == Mageo::Triangle
179
174
 
180
175
  vertices.each do |v_self|
181
176
  if (other.vertices.find{|v_other| v_self.equal_in_delta?(v_other, tolerance) })
@@ -204,21 +199,21 @@ class Triangle
204
199
 
205
200
  def edges
206
201
  results = []
207
- results << Segment.new(@vertices[0], @vertices[1])
208
- results << Segment.new(@vertices[1], @vertices[2])
209
- results << Segment.new(@vertices[2], @vertices[0])
202
+ results << Mageo::Segment.new(@vertices[0], @vertices[1])
203
+ results << Mageo::Segment.new(@vertices[1], @vertices[2])
204
+ results << Mageo::Segment.new(@vertices[2], @vertices[0])
210
205
  return results
211
206
  end
212
207
 
213
208
  private
214
209
 
215
210
  # 三角形の2辺のベクトルと、これらからなる外積ベクトル、
216
- # 合計3つのベクトルから Axes クラスインスタンスを作る。
211
+ # 合計3つのベクトルから Mageo::Axes クラスインスタンスを作る。
217
212
  # vertices で取り出せる3頂点のうち、0th 要素を原点とし、
218
213
  # 1st, 2nd 要素をそれぞれ順に軸としたものとする。
219
214
  def internal_axes
220
215
  edge1 = (@vertices[1] - @vertices[0])
221
216
  edge2 = (@vertices[2] - @vertices[0])
222
- return Axes.new([edge1, edge2, normal_vector])
217
+ return Mageo::Axes.new([edge1, edge2, normal_vector])
223
218
  end
224
219
  end