mageo 0.0.0
Sign up to get free protection for your applications and to get access to all the features.
- 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
data/.document
ADDED
data/Gemfile
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
source "http://rubygems.org"
|
2
|
+
# Add dependencies required to use your gem here.
|
3
|
+
# Example:
|
4
|
+
# gem "activesupport", ">= 2.3.5"
|
5
|
+
|
6
|
+
# Add dependencies to develop your gem here.
|
7
|
+
# Include everything needed to run rake, tests, features, etc.
|
8
|
+
group :development do
|
9
|
+
gem "rdoc", "~> 3.12"
|
10
|
+
gem "bundler", "~> 1.1.3"
|
11
|
+
gem "jeweler", "~> 1.8.3"
|
12
|
+
gem "simplecov", ">= 0"
|
13
|
+
gem "builtinextension", ">= 0"
|
14
|
+
gem "malge", ">= 0.0.1"
|
15
|
+
end
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2012 ippei94da
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.rdoc
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
= mageo
|
2
|
+
|
3
|
+
Description goes here.
|
4
|
+
|
5
|
+
== Contributing to mageo
|
6
|
+
|
7
|
+
* Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet.
|
8
|
+
* Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it.
|
9
|
+
* Fork the project.
|
10
|
+
* Start a feature/bugfix branch.
|
11
|
+
* Commit and push until you are happy with your contribution.
|
12
|
+
* Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
|
13
|
+
* Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
|
14
|
+
|
15
|
+
== Copyright
|
16
|
+
|
17
|
+
Copyright (c) 2012 ippei94da. See LICENSE.txt for
|
18
|
+
further details.
|
19
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1,56 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require 'rubygems'
|
4
|
+
require 'bundler'
|
5
|
+
begin
|
6
|
+
Bundler.setup(:default, :development)
|
7
|
+
rescue Bundler::BundlerError => e
|
8
|
+
$stderr.puts e.message
|
9
|
+
$stderr.puts "Run `bundle install` to install missing gems"
|
10
|
+
exit e.status_code
|
11
|
+
end
|
12
|
+
require 'rake'
|
13
|
+
|
14
|
+
require 'jeweler'
|
15
|
+
Jeweler::Tasks.new do |gem|
|
16
|
+
# gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
|
17
|
+
gem.name = "mageo"
|
18
|
+
gem.homepage = "http://github.com/ippei94da/mageo"
|
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.
|
22
|
+
Cartesian and internal coordinate systems can be used.
|
23
|
+
This includes besic objects in 3 dimensional space.
|
24
|
+
}
|
25
|
+
gem.email = "ippei94da@gmail.com"
|
26
|
+
gem.authors = ["ippei94da"]
|
27
|
+
# dependencies defined in Gemfile
|
28
|
+
end
|
29
|
+
Jeweler::RubygemsDotOrgTasks.new
|
30
|
+
|
31
|
+
require 'rake/testtask'
|
32
|
+
Rake::TestTask.new(:test) do |test|
|
33
|
+
test.libs << 'lib' << 'test'
|
34
|
+
test.pattern = 'test/**/test_*.rb'
|
35
|
+
test.verbose = true
|
36
|
+
end
|
37
|
+
|
38
|
+
#require 'rcov/rcovtask'
|
39
|
+
#Rcov::RcovTask.new do |test|
|
40
|
+
# test.libs << 'test'
|
41
|
+
# test.pattern = 'test/**/test_*.rb'
|
42
|
+
# test.verbose = true
|
43
|
+
# test.rcov_opts << '--exclude "gems/*"'
|
44
|
+
#end
|
45
|
+
|
46
|
+
task :default => :test
|
47
|
+
|
48
|
+
require 'rdoc/task'
|
49
|
+
Rake::RDocTask.new do |rdoc|
|
50
|
+
version = File.exist?('VERSION') ? File.read('VERSION') : ""
|
51
|
+
|
52
|
+
rdoc.rdoc_dir = 'rdoc'
|
53
|
+
rdoc.title = "mageo #{version}"
|
54
|
+
rdoc.rdoc_files.include('README*')
|
55
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
56
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.0.0
|
data/lib/mageo.rb
ADDED
File without changes
|
data/lib/mageo/axes.rb
ADDED
@@ -0,0 +1,87 @@
|
|
1
|
+
#! /usr/bin/env ruby
|
2
|
+
# coding: utf-8
|
3
|
+
|
4
|
+
require "matrix"
|
5
|
+
|
6
|
+
# n 次元空間における座標系を表現する n 本のベクトルを扱うクラス。
|
7
|
+
class Axes
|
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
|
85
|
+
|
86
|
+
end
|
87
|
+
|
@@ -0,0 +1,23 @@
|
|
1
|
+
#! /usr/bin/env ruby
|
2
|
+
# coding: utf-8
|
3
|
+
|
4
|
+
require "mageo/vector3d.rb"
|
5
|
+
|
6
|
+
#
|
7
|
+
# 球を表現するクラス。
|
8
|
+
#
|
9
|
+
class Cylinder
|
10
|
+
attr_reader :positions, :radius
|
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
|
21
|
+
|
22
|
+
end
|
23
|
+
|
@@ -0,0 +1,62 @@
|
|
1
|
+
#! /usr/bin/env ruby
|
2
|
+
# coding: utf-8
|
3
|
+
|
4
|
+
require "mageo/vector3d.rb"
|
5
|
+
require "mageo/triangle.rb"
|
6
|
+
#require "mageo/tetrahedron.rb"
|
7
|
+
require "mageo/polyhedron.rb"
|
8
|
+
|
9
|
+
|
10
|
+
# 3次元空間中の八面体を表現するクラス
|
11
|
+
class Octahedron < Polyhedron
|
12
|
+
|
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
|
61
|
+
|
62
|
+
end
|
@@ -0,0 +1,95 @@
|
|
1
|
+
#! /usr/bin/env ruby
|
2
|
+
# coding: utf-8
|
3
|
+
|
4
|
+
require 'matrix'
|
5
|
+
|
6
|
+
|
7
|
+
class Vector
|
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
|
+
end
|
35
|
+
|
36
|
+
#2次元極座標。
|
37
|
+
#極座標ライブラリでは、角度は基本的に radian を使用する。
|
38
|
+
#degree は人間の都合で決められた尺度だろう。
|
39
|
+
#まあ人間用に degree 用インターフェイスも用意することもあるかもしれんが。
|
40
|
+
class Polar2D
|
41
|
+
|
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
|
94
|
+
|
95
|
+
end
|
@@ -0,0 +1,47 @@
|
|
1
|
+
#! /usr/bin/env ruby
|
2
|
+
# coding: utf-8
|
3
|
+
|
4
|
+
require 'matrix'
|
5
|
+
require 'mageo/vector3d.rb'
|
6
|
+
|
7
|
+
#3次元極座標。
|
8
|
+
#極座標ライブラリでは、角度は基本的に radian を使用する。
|
9
|
+
#degree は人間の都合で決められた尺度だろう。
|
10
|
+
#まあ人間用に degree 用インターフェイスも用意することもあるかもしれんが。
|
11
|
+
class Polar3D
|
12
|
+
|
13
|
+
include Math
|
14
|
+
|
15
|
+
attr_reader :r, :theta, :phi
|
16
|
+
|
17
|
+
#
|
18
|
+
def initialize( r, theta, phi)
|
19
|
+
@r = r
|
20
|
+
@theta = theta
|
21
|
+
@phi = phi
|
22
|
+
end
|
23
|
+
|
24
|
+
#3次元 Vector に変換。
|
25
|
+
def to_v3d
|
26
|
+
#Vector[ @r * cos( @theta ), @r * sin( @theta ) ]
|
27
|
+
x = @r * sin( @theta ) * cos( @phi )
|
28
|
+
y = @r * sin( @theta ) * sin( @phi )
|
29
|
+
z = @r * cos( @theta )
|
30
|
+
Vector3D[ x, y, z ]
|
31
|
+
end
|
32
|
+
|
33
|
+
#phi を 0 <= phi < 2*PI の間の角度に変換する破壊破壊的メソッド。
|
34
|
+
def minimize_phi!
|
35
|
+
tmp = ( @phi / (2.0*PI) )
|
36
|
+
tmp = tmp - tmp.floor
|
37
|
+
@phi = (2.0*PI) * tmp
|
38
|
+
end
|
39
|
+
|
40
|
+
#phi を 0 <= phi < 2*PI の間の角度に変換したオブジェクトを返す非破壊破壊的メソッド。
|
41
|
+
def minimize_phi
|
42
|
+
result = Marshal.load( Marshal.dump( self ) )
|
43
|
+
result.minimize_phi!
|
44
|
+
result
|
45
|
+
end
|
46
|
+
|
47
|
+
end
|