gmath3D 0.2.4 → 0.2.5
Sign up to get free protection for your applications and to get access to all the features.
- data/.document +5 -5
- data/Gemfile +5 -11
- data/LICENSE.txt +20 -20
- data/README.rdoc +19 -19
- data/Rakefile +45 -45
- data/VERSION +1 -1
- data/gmath3D.gemspec +79 -80
- data/lib/box.rb +145 -145
- data/lib/ellipse.rb +11 -11
- data/lib/ext.rb +82 -82
- data/lib/finite_line.rb +244 -244
- data/lib/geom.rb +20 -20
- data/lib/gmath3D.rb +22 -22
- data/lib/line.rb +122 -122
- data/lib/plane.rb +131 -131
- data/lib/polyline.rb +73 -73
- data/lib/quat.rb +170 -170
- data/lib/rectangle.rb +155 -155
- data/lib/tri_mesh.rb +258 -258
- data/lib/triangle.rb +281 -281
- data/lib/util.rb +36 -23
- data/lib/vector3.rb +227 -227
- data/test/helper.rb +15 -15
- data/test/test_box.rb +167 -167
- data/test/test_ellipse.rb +55 -55
- data/test/test_finite_line.rb +306 -306
- data/test/test_geom.rb +17 -17
- data/test/test_line.rb +146 -146
- data/test/test_matrix_util.rb +84 -84
- data/test/test_plane.rb +200 -200
- data/test/test_polyline.rb +93 -93
- data/test/test_quat.rb +144 -144
- data/test/test_rectangle.rb +184 -184
- data/test/test_tri_mesh.rb +186 -186
- data/test/test_triangle.rb +318 -318
- data/test/test_util.rb +88 -57
- data/test/test_vector3.rb +439 -439
- metadata +8 -11
- data/Gemfile.lock +0 -16
data/.document
CHANGED
@@ -1,5 +1,5 @@
|
|
1
|
-
lib/**/*.rb
|
2
|
-
bin/*
|
3
|
-
-
|
4
|
-
features/**/*.feature
|
5
|
-
LICENSE.txt
|
1
|
+
lib/**/*.rb
|
2
|
+
bin/*
|
3
|
+
-
|
4
|
+
features/**/*.feature
|
5
|
+
LICENSE.txt
|
data/Gemfile
CHANGED
@@ -1,11 +1,5 @@
|
|
1
|
-
source "http://rubygems.org"
|
2
|
-
|
3
|
-
|
4
|
-
|
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 "bundler", "~> 1.0.0"
|
10
|
-
gem "jeweler", "~> 1.6.4"
|
11
|
-
end
|
1
|
+
source "http://rubygems.org"
|
2
|
+
group :development do
|
3
|
+
gem "bundler", "~> 1.0.0"
|
4
|
+
gem "jeweler", "~> 1.6.4"
|
5
|
+
end
|
data/LICENSE.txt
CHANGED
@@ -1,20 +1,20 @@
|
|
1
|
-
Copyright (c) 2011 Toshiyasu Shimizu
|
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.
|
1
|
+
Copyright (c) 2011 Toshiyasu Shimizu
|
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
CHANGED
@@ -1,19 +1,19 @@
|
|
1
|
-
= gmath3D
|
2
|
-
|
3
|
-
This library defines 3D geometric elements(point, line, plane etc..). It can get two(or more) elements relation, like distance between two elements.
|
4
|
-
|
5
|
-
== Class list in gmath3D
|
6
|
-
* Vector3 class signifies point and vector in 3D.
|
7
|
-
* Line class signifies inifinite line in 3D.
|
8
|
-
* FiniteLine class signifies finite line in 3D.
|
9
|
-
* Plane class signifies inifinite plane in 3D.
|
10
|
-
* Rectangle class signifies finite plane that has 4 edges in 3.
|
11
|
-
* Triangle class signifies finite plane that has 3 edges in 3.
|
12
|
-
* Box class signifies axially aligned box.
|
13
|
-
* Geom class is parent class of all 3D elements.
|
14
|
-
|
15
|
-
== Copyright
|
16
|
-
|
17
|
-
Copyright (c) 2011 Toshiyasu Shimizu. See LICENSE.txt for
|
18
|
-
further details.
|
19
|
-
|
1
|
+
= gmath3D
|
2
|
+
|
3
|
+
This library defines 3D geometric elements(point, line, plane etc..). It can get two(or more) elements relation, like distance between two elements.
|
4
|
+
|
5
|
+
== Class list in gmath3D
|
6
|
+
* Vector3 class signifies point and vector in 3D.
|
7
|
+
* Line class signifies inifinite line in 3D.
|
8
|
+
* FiniteLine class signifies finite line in 3D.
|
9
|
+
* Plane class signifies inifinite plane in 3D.
|
10
|
+
* Rectangle class signifies finite plane that has 4 edges in 3.
|
11
|
+
* Triangle class signifies finite plane that has 3 edges in 3.
|
12
|
+
* Box class signifies axially aligned box.
|
13
|
+
* Geom class is parent class of all 3D elements.
|
14
|
+
|
15
|
+
== Copyright
|
16
|
+
|
17
|
+
Copyright (c) 2011 Toshiyasu Shimizu. See LICENSE.txt for
|
18
|
+
further details.
|
19
|
+
|
data/Rakefile
CHANGED
@@ -1,45 +1,45 @@
|
|
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 = "gmath3D"
|
18
|
-
gem.homepage = "http://github.com/toshi0328/gmath3D"
|
19
|
-
gem.license = "MIT"
|
20
|
-
gem.summary = %Q{Geometric elements in 3D}
|
21
|
-
gem.description = %Q{This library defines 3D geometric elements(point, line, plane etc..). It can get two(or more) elements relation, like distance between two elements.}
|
22
|
-
gem.email = "toshi0328@gmail.com"
|
23
|
-
gem.authors = ["Toshiyasu Shimizu"]
|
24
|
-
# dependencies defined in Gemfile
|
25
|
-
end
|
26
|
-
Jeweler::RubygemsDotOrgTasks.new
|
27
|
-
|
28
|
-
require 'rake/testtask'
|
29
|
-
Rake::TestTask.new(:test) do |test|
|
30
|
-
test.libs << 'lib' << 'test'
|
31
|
-
test.pattern = 'test/**/test_*.rb'
|
32
|
-
test.verbose = true
|
33
|
-
end
|
34
|
-
|
35
|
-
task :default => :test
|
36
|
-
|
37
|
-
require 'rake/rdoctask'
|
38
|
-
Rake::RDocTask.new do |rdoc|
|
39
|
-
version = File.exist?('VERSION') ? File.read('VERSION') : ""
|
40
|
-
|
41
|
-
rdoc.rdoc_dir = 'rdoc'
|
42
|
-
rdoc.title = "gmath3D #{version}"
|
43
|
-
rdoc.rdoc_files.include('README*')
|
44
|
-
rdoc.rdoc_files.include('lib/**/*.rb')
|
45
|
-
end
|
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 = "gmath3D"
|
18
|
+
gem.homepage = "http://github.com/toshi0328/gmath3D"
|
19
|
+
gem.license = "MIT"
|
20
|
+
gem.summary = %Q{Geometric elements in 3D}
|
21
|
+
gem.description = %Q{This library defines 3D geometric elements(point, line, plane etc..). It can get two(or more) elements relation, like distance between two elements.}
|
22
|
+
gem.email = "toshi0328@gmail.com"
|
23
|
+
gem.authors = ["Toshiyasu Shimizu"]
|
24
|
+
# dependencies defined in Gemfile
|
25
|
+
end
|
26
|
+
Jeweler::RubygemsDotOrgTasks.new
|
27
|
+
|
28
|
+
require 'rake/testtask'
|
29
|
+
Rake::TestTask.new(:test) do |test|
|
30
|
+
test.libs << 'lib' << 'test'
|
31
|
+
test.pattern = 'test/**/test_*.rb'
|
32
|
+
test.verbose = true
|
33
|
+
end
|
34
|
+
|
35
|
+
task :default => :test
|
36
|
+
|
37
|
+
require 'rake/rdoctask'
|
38
|
+
Rake::RDocTask.new do |rdoc|
|
39
|
+
version = File.exist?('VERSION') ? File.read('VERSION') : ""
|
40
|
+
|
41
|
+
rdoc.rdoc_dir = 'rdoc'
|
42
|
+
rdoc.title = "gmath3D #{version}"
|
43
|
+
rdoc.rdoc_files.include('README*')
|
44
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
45
|
+
end
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.2.
|
1
|
+
0.2.5
|
data/gmath3D.gemspec
CHANGED
@@ -1,80 +1,79 @@
|
|
1
|
-
# Generated by jeweler
|
2
|
-
# DO NOT EDIT THIS FILE DIRECTLY
|
3
|
-
# Instead, edit Jeweler::Tasks in
|
4
|
-
# -*- encoding: utf-8 -*-
|
5
|
-
|
6
|
-
Gem::Specification.new do |s|
|
7
|
-
s.name = %q{gmath3D}
|
8
|
-
s.version = "0.2.
|
9
|
-
|
10
|
-
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
|
-
s.authors = ["Toshiyasu Shimizu"]
|
12
|
-
s.date = %q{2011-
|
13
|
-
s.description = %q{This library defines 3D geometric elements(point, line, plane etc..). It can get two(or more) elements relation, like distance between two elements.}
|
14
|
-
s.email = %q{toshi0328@gmail.com}
|
15
|
-
s.extra_rdoc_files = [
|
16
|
-
"LICENSE.txt",
|
17
|
-
"README.rdoc"
|
18
|
-
]
|
19
|
-
s.files = [
|
20
|
-
".document",
|
21
|
-
"Gemfile",
|
22
|
-
"
|
23
|
-
"
|
24
|
-
"
|
25
|
-
"
|
26
|
-
"
|
27
|
-
"
|
28
|
-
"lib/
|
29
|
-
"lib/
|
30
|
-
"lib/
|
31
|
-
"lib/
|
32
|
-
"lib/
|
33
|
-
"lib/
|
34
|
-
"lib/
|
35
|
-
"lib/
|
36
|
-
"lib/
|
37
|
-
"lib/
|
38
|
-
"lib/
|
39
|
-
"lib/
|
40
|
-
"lib/
|
41
|
-
"lib/
|
42
|
-
"
|
43
|
-
"test/
|
44
|
-
"test/
|
45
|
-
"test/
|
46
|
-
"test/
|
47
|
-
"test/
|
48
|
-
"test/
|
49
|
-
"test/
|
50
|
-
"test/
|
51
|
-
"test/
|
52
|
-
"test/
|
53
|
-
"test/
|
54
|
-
"test/
|
55
|
-
"test/
|
56
|
-
"test/
|
57
|
-
|
58
|
-
|
59
|
-
s.
|
60
|
-
s.
|
61
|
-
s.
|
62
|
-
s.
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
s.add_development_dependency(%q<
|
70
|
-
|
71
|
-
|
72
|
-
s.add_dependency(%q<
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
s.add_dependency(%q<
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
1
|
+
# Generated by jeweler
|
2
|
+
# DO NOT EDIT THIS FILE DIRECTLY
|
3
|
+
# Instead, edit Jeweler::Tasks in rakefile, and run 'rake gemspec'
|
4
|
+
# -*- encoding: utf-8 -*-
|
5
|
+
|
6
|
+
Gem::Specification.new do |s|
|
7
|
+
s.name = %q{gmath3D}
|
8
|
+
s.version = "0.2.5"
|
9
|
+
|
10
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
|
+
s.authors = ["Toshiyasu Shimizu"]
|
12
|
+
s.date = %q{2011-12-01}
|
13
|
+
s.description = %q{This library defines 3D geometric elements(point, line, plane etc..). It can get two(or more) elements relation, like distance between two elements.}
|
14
|
+
s.email = %q{toshi0328@gmail.com}
|
15
|
+
s.extra_rdoc_files = [
|
16
|
+
"LICENSE.txt",
|
17
|
+
"README.rdoc"
|
18
|
+
]
|
19
|
+
s.files = [
|
20
|
+
".document",
|
21
|
+
"Gemfile",
|
22
|
+
"LICENSE.txt",
|
23
|
+
"README.rdoc",
|
24
|
+
"Rakefile",
|
25
|
+
"VERSION",
|
26
|
+
"gmath3D.gemspec",
|
27
|
+
"lib/box.rb",
|
28
|
+
"lib/ellipse.rb",
|
29
|
+
"lib/ext.rb",
|
30
|
+
"lib/finite_line.rb",
|
31
|
+
"lib/geom.rb",
|
32
|
+
"lib/gmath3D.rb",
|
33
|
+
"lib/line.rb",
|
34
|
+
"lib/plane.rb",
|
35
|
+
"lib/polyline.rb",
|
36
|
+
"lib/quat.rb",
|
37
|
+
"lib/rectangle.rb",
|
38
|
+
"lib/tri_mesh.rb",
|
39
|
+
"lib/triangle.rb",
|
40
|
+
"lib/util.rb",
|
41
|
+
"lib/vector3.rb",
|
42
|
+
"test/helper.rb",
|
43
|
+
"test/test_box.rb",
|
44
|
+
"test/test_ellipse.rb",
|
45
|
+
"test/test_finite_line.rb",
|
46
|
+
"test/test_geom.rb",
|
47
|
+
"test/test_line.rb",
|
48
|
+
"test/test_matrix_util.rb",
|
49
|
+
"test/test_plane.rb",
|
50
|
+
"test/test_polyline.rb",
|
51
|
+
"test/test_quat.rb",
|
52
|
+
"test/test_rectangle.rb",
|
53
|
+
"test/test_tri_mesh.rb",
|
54
|
+
"test/test_triangle.rb",
|
55
|
+
"test/test_util.rb",
|
56
|
+
"test/test_vector3.rb"
|
57
|
+
]
|
58
|
+
s.homepage = %q{http://github.com/toshi0328/gmath3D}
|
59
|
+
s.licenses = ["MIT"]
|
60
|
+
s.require_paths = ["lib"]
|
61
|
+
s.rubygems_version = %q{1.7.2}
|
62
|
+
s.summary = %q{Geometric elements in 3D}
|
63
|
+
|
64
|
+
if s.respond_to? :specification_version then
|
65
|
+
s.specification_version = 3
|
66
|
+
|
67
|
+
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
68
|
+
s.add_development_dependency(%q<bundler>, ["~> 1.0.0"])
|
69
|
+
s.add_development_dependency(%q<jeweler>, ["~> 1.6.4"])
|
70
|
+
else
|
71
|
+
s.add_dependency(%q<bundler>, ["~> 1.0.0"])
|
72
|
+
s.add_dependency(%q<jeweler>, ["~> 1.6.4"])
|
73
|
+
end
|
74
|
+
else
|
75
|
+
s.add_dependency(%q<bundler>, ["~> 1.0.0"])
|
76
|
+
s.add_dependency(%q<jeweler>, ["~> 1.6.4"])
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
data/lib/box.rb
CHANGED
@@ -1,145 +1,145 @@
|
|
1
|
-
require 'gmath3D'
|
2
|
-
|
3
|
-
module GMath3D
|
4
|
-
#
|
5
|
-
# Box represents an axitially aligned box on 3D space.
|
6
|
-
#
|
7
|
-
class Box < Geom
|
8
|
-
attr_reader :min_point
|
9
|
-
attr_reader :max_point
|
10
|
-
|
11
|
-
# [Input]
|
12
|
-
# _point1_ and _point2_ should be Vector3.
|
13
|
-
# Each points are opposing corners.
|
14
|
-
# [Output]
|
15
|
-
# return new instance of Box.
|
16
|
-
def initialize(point1 = Vector3.new(0,0,0), point2 = Vector3.new(1,1,1))
|
17
|
-
Util3D.check_arg_type(Vector3, point1)
|
18
|
-
Util3D.check_arg_type(Vector3, point2)
|
19
|
-
super()
|
20
|
-
@min_point = Vector3.new();
|
21
|
-
@max_point = Vector3.new();
|
22
|
-
@min_point.x = [ point1.x, point2.x ].min
|
23
|
-
@min_point.y = [ point1.y, point2.y ].min
|
24
|
-
@min_point.z = [ point1.z, point2.z ].min
|
25
|
-
@max_point.x = [ point1.x, point2.x ].max
|
26
|
-
@max_point.y = [ point1.y, point2.y ].max
|
27
|
-
@max_point.z = [ point1.z, point2.z ].max
|
28
|
-
end
|
29
|
-
|
30
|
-
# [Input]
|
31
|
-
# _points_ should be Array of Vector3.
|
32
|
-
# Each points are opposing corners.
|
33
|
-
# [Output]
|
34
|
-
# return new instance of Box.
|
35
|
-
def self.from_points( points )
|
36
|
-
return nil if (points == nil || points.size <=0)
|
37
|
-
box = Box.new(points[0], points[0])
|
38
|
-
box += points
|
39
|
-
return box
|
40
|
-
end
|
41
|
-
|
42
|
-
def to_s
|
43
|
-
"Box[min#{min_point.to_element_s}, max#{max_point.to_element_s}]"
|
44
|
-
end
|
45
|
-
|
46
|
-
def ==(rhs)
|
47
|
-
equals_inner(rhs)
|
48
|
-
end
|
49
|
-
|
50
|
-
# [Input]
|
51
|
-
# _rhs_ shold be Vector3 or Box or Array of them.
|
52
|
-
# [Output]
|
53
|
-
# return added result as Box.
|
54
|
-
def +(rhs)
|
55
|
-
add(rhs)
|
56
|
-
end
|
57
|
-
|
58
|
-
# [Output]
|
59
|
-
# return cente point of Box as Vector3.
|
60
|
-
def center
|
61
|
-
return (@min_point + @max_point) * 0.5
|
62
|
-
end
|
63
|
-
|
64
|
-
# [Output]
|
65
|
-
# return width, height, depth as [Numeric, Numeric, Numeric]
|
66
|
-
def length
|
67
|
-
return max_point.x - min_point.x, max_point.y - min_point.y, max_point.z - min_point.z
|
68
|
-
end
|
69
|
-
|
70
|
-
# [Output]
|
71
|
-
# return volume of Box as Numeric.
|
72
|
-
def volume
|
73
|
-
width, height, depth = self.length
|
74
|
-
return width*height*depth
|
75
|
-
end
|
76
|
-
|
77
|
-
# [Output]
|
78
|
-
# return all vertices of Box.
|
79
|
-
def vertices
|
80
|
-
verts = Array.new(8)
|
81
|
-
length_ary = self.length
|
82
|
-
verts[0] = @min_point.clone
|
83
|
-
verts[1] = @min_point + Vector3.new(length_ary[0], 0, 0 )
|
84
|
-
verts[2] = @min_point + Vector3.new(length_ary[0], length_ary[1], 0 )
|
85
|
-
verts[3] = @min_point + Vector3.new( 0, length_ary[1], 0 )
|
86
|
-
verts[4] = @min_point + Vector3.new( 0, 0, length_ary[2])
|
87
|
-
verts[5] = @min_point + Vector3.new(length_ary[0], 0, length_ary[2])
|
88
|
-
verts[6] = @min_point + Vector3.new(length_ary[0], length_ary[1], length_ary[2])
|
89
|
-
verts[7] = @min_point + Vector3.new( 0, length_ary[1], length_ary[2])
|
90
|
-
return verts
|
91
|
-
end
|
92
|
-
|
93
|
-
# [input]
|
94
|
-
# _vec_ should be Vector3.
|
95
|
-
# [Output]
|
96
|
-
# return translated box as Box.
|
97
|
-
def translate(vec)
|
98
|
-
return Box.new(min_point + vec, max_point + vec)
|
99
|
-
end
|
100
|
-
|
101
|
-
# [input]
|
102
|
-
# _quat_ should be Quat.
|
103
|
-
# [Output]
|
104
|
-
# return rotated box as Box.
|
105
|
-
# since Box is AABB, returned box might be bigger than original one.
|
106
|
-
def rotate(quat)
|
107
|
-
rot_matrix = Matrix.from_quat(quat)
|
108
|
-
verts = self.vertices
|
109
|
-
verts = verts.collect {|item| rot_matrix*item}
|
110
|
-
return Box.from_points(verts)
|
111
|
-
end
|
112
|
-
|
113
|
-
private
|
114
|
-
def equals_inner(rhs)
|
115
|
-
return false if( !rhs.kind_of?(Box) )
|
116
|
-
return false if(self.min_point != rhs.min_point)
|
117
|
-
return false if(self.max_point != rhs.max_point)
|
118
|
-
true
|
119
|
-
end
|
120
|
-
def add(rhs)
|
121
|
-
return self if (rhs == nil)
|
122
|
-
if( rhs.kind_of?(Vector3))
|
123
|
-
added_box = Box.new()
|
124
|
-
added_box.min_point.x = [ self.min_point.x, rhs.x ].min
|
125
|
-
added_box.min_point.y = [ self.min_point.y, rhs.y ].min
|
126
|
-
added_box.min_point.z = [ self.min_point.z, rhs.z ].min
|
127
|
-
added_box.max_point.x = [ self.max_point.x, rhs.x ].max
|
128
|
-
added_box.max_point.y = [ self.max_point.y, rhs.y ].max
|
129
|
-
added_box.max_point.z = [ self.max_point.z, rhs.z ].max
|
130
|
-
return added_box
|
131
|
-
elsif( rhs.kind_of?(Box))
|
132
|
-
min_max_point_ary = [rhs.min_point, rhs.max_point]
|
133
|
-
return self + min_max_point_ary
|
134
|
-
elsif(rhs.kind_of?(Array))
|
135
|
-
added_box = self;
|
136
|
-
rhs.each do |item|
|
137
|
-
added_box = added_box + item
|
138
|
-
end
|
139
|
-
return added_box
|
140
|
-
end
|
141
|
-
Util3D.raise_argurment_error(rhs)
|
142
|
-
end
|
143
|
-
end
|
144
|
-
end
|
145
|
-
|
1
|
+
require 'gmath3D'
|
2
|
+
|
3
|
+
module GMath3D
|
4
|
+
#
|
5
|
+
# Box represents an axitially aligned box on 3D space.
|
6
|
+
#
|
7
|
+
class Box < Geom
|
8
|
+
attr_reader :min_point
|
9
|
+
attr_reader :max_point
|
10
|
+
|
11
|
+
# [Input]
|
12
|
+
# _point1_ and _point2_ should be Vector3.
|
13
|
+
# Each points are opposing corners.
|
14
|
+
# [Output]
|
15
|
+
# return new instance of Box.
|
16
|
+
def initialize(point1 = Vector3.new(0,0,0), point2 = Vector3.new(1,1,1))
|
17
|
+
Util3D.check_arg_type(Vector3, point1)
|
18
|
+
Util3D.check_arg_type(Vector3, point2)
|
19
|
+
super()
|
20
|
+
@min_point = Vector3.new();
|
21
|
+
@max_point = Vector3.new();
|
22
|
+
@min_point.x = [ point1.x, point2.x ].min
|
23
|
+
@min_point.y = [ point1.y, point2.y ].min
|
24
|
+
@min_point.z = [ point1.z, point2.z ].min
|
25
|
+
@max_point.x = [ point1.x, point2.x ].max
|
26
|
+
@max_point.y = [ point1.y, point2.y ].max
|
27
|
+
@max_point.z = [ point1.z, point2.z ].max
|
28
|
+
end
|
29
|
+
|
30
|
+
# [Input]
|
31
|
+
# _points_ should be Array of Vector3.
|
32
|
+
# Each points are opposing corners.
|
33
|
+
# [Output]
|
34
|
+
# return new instance of Box.
|
35
|
+
def self.from_points( points )
|
36
|
+
return nil if (points == nil || points.size <=0)
|
37
|
+
box = Box.new(points[0], points[0])
|
38
|
+
box += points
|
39
|
+
return box
|
40
|
+
end
|
41
|
+
|
42
|
+
def to_s
|
43
|
+
"Box[min#{min_point.to_element_s}, max#{max_point.to_element_s}]"
|
44
|
+
end
|
45
|
+
|
46
|
+
def ==(rhs)
|
47
|
+
equals_inner(rhs)
|
48
|
+
end
|
49
|
+
|
50
|
+
# [Input]
|
51
|
+
# _rhs_ shold be Vector3 or Box or Array of them.
|
52
|
+
# [Output]
|
53
|
+
# return added result as Box.
|
54
|
+
def +(rhs)
|
55
|
+
add(rhs)
|
56
|
+
end
|
57
|
+
|
58
|
+
# [Output]
|
59
|
+
# return cente point of Box as Vector3.
|
60
|
+
def center
|
61
|
+
return (@min_point + @max_point) * 0.5
|
62
|
+
end
|
63
|
+
|
64
|
+
# [Output]
|
65
|
+
# return width, height, depth as [Numeric, Numeric, Numeric]
|
66
|
+
def length
|
67
|
+
return max_point.x - min_point.x, max_point.y - min_point.y, max_point.z - min_point.z
|
68
|
+
end
|
69
|
+
|
70
|
+
# [Output]
|
71
|
+
# return volume of Box as Numeric.
|
72
|
+
def volume
|
73
|
+
width, height, depth = self.length
|
74
|
+
return width*height*depth
|
75
|
+
end
|
76
|
+
|
77
|
+
# [Output]
|
78
|
+
# return all vertices of Box.
|
79
|
+
def vertices
|
80
|
+
verts = Array.new(8)
|
81
|
+
length_ary = self.length
|
82
|
+
verts[0] = @min_point.clone
|
83
|
+
verts[1] = @min_point + Vector3.new(length_ary[0], 0, 0 )
|
84
|
+
verts[2] = @min_point + Vector3.new(length_ary[0], length_ary[1], 0 )
|
85
|
+
verts[3] = @min_point + Vector3.new( 0, length_ary[1], 0 )
|
86
|
+
verts[4] = @min_point + Vector3.new( 0, 0, length_ary[2])
|
87
|
+
verts[5] = @min_point + Vector3.new(length_ary[0], 0, length_ary[2])
|
88
|
+
verts[6] = @min_point + Vector3.new(length_ary[0], length_ary[1], length_ary[2])
|
89
|
+
verts[7] = @min_point + Vector3.new( 0, length_ary[1], length_ary[2])
|
90
|
+
return verts
|
91
|
+
end
|
92
|
+
|
93
|
+
# [input]
|
94
|
+
# _vec_ should be Vector3.
|
95
|
+
# [Output]
|
96
|
+
# return translated box as Box.
|
97
|
+
def translate(vec)
|
98
|
+
return Box.new(min_point + vec, max_point + vec)
|
99
|
+
end
|
100
|
+
|
101
|
+
# [input]
|
102
|
+
# _quat_ should be Quat.
|
103
|
+
# [Output]
|
104
|
+
# return rotated box as Box.
|
105
|
+
# since Box is AABB, returned box might be bigger than original one.
|
106
|
+
def rotate(quat)
|
107
|
+
rot_matrix = Matrix.from_quat(quat)
|
108
|
+
verts = self.vertices
|
109
|
+
verts = verts.collect {|item| rot_matrix*item}
|
110
|
+
return Box.from_points(verts)
|
111
|
+
end
|
112
|
+
|
113
|
+
private
|
114
|
+
def equals_inner(rhs)
|
115
|
+
return false if( !rhs.kind_of?(Box) )
|
116
|
+
return false if(self.min_point != rhs.min_point)
|
117
|
+
return false if(self.max_point != rhs.max_point)
|
118
|
+
true
|
119
|
+
end
|
120
|
+
def add(rhs)
|
121
|
+
return self if (rhs == nil)
|
122
|
+
if( rhs.kind_of?(Vector3))
|
123
|
+
added_box = Box.new()
|
124
|
+
added_box.min_point.x = [ self.min_point.x, rhs.x ].min
|
125
|
+
added_box.min_point.y = [ self.min_point.y, rhs.y ].min
|
126
|
+
added_box.min_point.z = [ self.min_point.z, rhs.z ].min
|
127
|
+
added_box.max_point.x = [ self.max_point.x, rhs.x ].max
|
128
|
+
added_box.max_point.y = [ self.max_point.y, rhs.y ].max
|
129
|
+
added_box.max_point.z = [ self.max_point.z, rhs.z ].max
|
130
|
+
return added_box
|
131
|
+
elsif( rhs.kind_of?(Box))
|
132
|
+
min_max_point_ary = [rhs.min_point, rhs.max_point]
|
133
|
+
return self + min_max_point_ary
|
134
|
+
elsif(rhs.kind_of?(Array))
|
135
|
+
added_box = self;
|
136
|
+
rhs.each do |item|
|
137
|
+
added_box = added_box + item
|
138
|
+
end
|
139
|
+
return added_box
|
140
|
+
end
|
141
|
+
Util3D.raise_argurment_error(rhs)
|
142
|
+
end
|
143
|
+
end
|
144
|
+
end
|
145
|
+
|