geom3d 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/.gitignore ADDED
@@ -0,0 +1,5 @@
1
+ pkg/*
2
+ *.gem
3
+ .bundle
4
+ Gemfile.lock
5
+ .rvmrc
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in Geom3d.gemspec
4
+ gemspec
data/Rakefile ADDED
@@ -0,0 +1,7 @@
1
+ require 'bundler'
2
+ Bundler::GemHelper.install_tasks
3
+
4
+ require 'rspec/core/rake_task'
5
+ RSpec::Core::RakeTask.new(:spec)
6
+
7
+ task :default => :spec
data/geom3d.gemspec ADDED
@@ -0,0 +1,26 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "geom3d/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "geom3d"
7
+ s.version = Geom3d::VERSION
8
+ s.platform = Gem::Platform::RUBY
9
+ s.authors = ["Nemo157 (Wim Looman)"]
10
+ s.email = ["ghostunderscore@gmail.com"]
11
+ s.homepage = ""
12
+ s.summary = %q{A simple pure ruby 3d geometry library}
13
+ s.description = %q{A simple pure ruby 3d geometry library}
14
+ s.licenses = ["Apache License 2.0"]
15
+
16
+ s.rubyforge_project = "geom3d"
17
+
18
+ s.files = `git ls-files`.split("\n")
19
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
20
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
21
+ s.require_paths = ["lib"]
22
+
23
+ s.required_ruby_version = '>=1.9.2'
24
+
25
+ s.add_development_dependency 'rspec', '~>2.3.0'
26
+ end
data/lib/geom3d/eps.rb ADDED
@@ -0,0 +1 @@
1
+ EPS = 10 ** -12
@@ -0,0 +1,20 @@
1
+ require 'geom3d/point'
2
+
3
+ module Geom3d
4
+ class Line
5
+ attr_reader :p1, :p2
6
+
7
+ def initialize p1, p2
8
+ @p1 = Point.new(p1)
9
+ @p2 = Point.new(p2)
10
+ end
11
+
12
+ def pos t
13
+ @p1 + t * (@p2 - @p1)
14
+ end
15
+
16
+ def to_s
17
+ "Line(#{@p1.to_s}, #{@p2.to_s})"
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,44 @@
1
+ require 'geom3d/eps'
2
+ require 'geom3d/vector'
3
+
4
+ module Geom3d
5
+ class Point
6
+ attr_accessor :x, :y, :z
7
+
8
+ def initialize *args
9
+ @x, @y, @z = args.flatten
10
+ end
11
+
12
+ def - other
13
+ case other
14
+ when Point
15
+ Vector.new(@x - other.x, @y - other.y, @z - other.z)
16
+ when Vector
17
+ Point.new(@x - other.dx, @y - other.dy, @z - other.dz)
18
+ else
19
+ throw ArgumentError
20
+ end
21
+ end
22
+
23
+ def + other
24
+ Point.new(@x + other.dx, @y + other.dy, @z + other.dz)
25
+ end
26
+
27
+ def == other
28
+ (@x - other.x) < EPS && (@y - other.y) < EPS && (@z - other.z) < EPS
29
+ end
30
+
31
+ def to_s
32
+ "Point3(%.3f,%.3f,%.3f)" % [@x, @y, @z]
33
+ end
34
+
35
+ # Allows recursive flatten and multiple assignment to work with this class
36
+ def to_ary
37
+ [@x, @y, @z]
38
+ end
39
+
40
+ def flatten
41
+ to_ary.flatten
42
+ end
43
+ end
44
+ end
data/lib/geom3d/ray.rb ADDED
@@ -0,0 +1,21 @@
1
+ require 'geom3d/point'
2
+ require 'geom3d/vector'
3
+
4
+ module Geom3d
5
+ class Ray
6
+ attr_reader :start, :dir
7
+
8
+ def initialize start, dir
9
+ @start = Point.new(start)
10
+ @dir = Vector.new(dir).norm
11
+ end
12
+
13
+ def pos t
14
+ @start + t * @dir
15
+ end
16
+
17
+ def to_s
18
+ "Ray(#{@start.to_s},#{@dir.to_s}"
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,116 @@
1
+ require 'geom3d/eps'
2
+
3
+ module Geom3d
4
+ class Vector
5
+ attr_accessor :dx, :dy, :dz
6
+
7
+ def initialize *args
8
+ @dx, @dy, @dz = args.flatten
9
+ end
10
+
11
+ def - other
12
+ Vector.new(@dx - other.dx, @dy - other.dy, @dz - other.dz)
13
+ end
14
+
15
+ def + other
16
+ Vector.new(@dx + other.dx, @dy + other.dy, @dz + other.dz)
17
+ end
18
+
19
+ def * scale
20
+ Vector.new(@dx * scale, @dy * scale, @dz * scale)
21
+ end
22
+
23
+ def ** power
24
+ Vector.new(@dx ** power, @dy ** power, @dz ** power)
25
+ end
26
+
27
+ def / scale
28
+ self * (1.0/scale)
29
+ end
30
+
31
+ def +@
32
+ self
33
+ end
34
+
35
+ def -@
36
+ Vector.new(-@dx, -@dy, -@dz)
37
+ end
38
+
39
+ def == other
40
+ (@dx - other.dx).abs < EPS && (@dy - other.dy).abs < EPS && (@dz - other.dz).abs < EPS
41
+ end
42
+
43
+ def dot other
44
+ @dx * other.dx + @dy * other.dy + @dz * other.dz
45
+ end
46
+
47
+ def cross other
48
+ Vector.new(@dy * other.dz - @dz * other.dy,
49
+ @dz * other.dx - @dx * other.dz,
50
+ @dx * other.dy - @dy * other.dx)
51
+ end
52
+
53
+ def norm
54
+ self / self.length
55
+ end
56
+
57
+ alias :unit :norm
58
+
59
+ def length
60
+ Math.sqrt(self.dot(self))
61
+ end
62
+
63
+ def to_s
64
+ "Vector(%.3f,%.3f,%.3f)" % [@dx, @dy, @dz]
65
+ end
66
+
67
+ # Allows flatten and multiple assignment to work with this class
68
+ def to_ary
69
+ [@dx, @dy, @dz]
70
+ end
71
+
72
+ def flatten
73
+ to_ary.flatten
74
+ end
75
+ end
76
+ end
77
+
78
+
79
+ class Fixnum
80
+ alias :times_without_geom3d :'*'
81
+
82
+ def * other
83
+ case other
84
+ when Geom3d::Vector
85
+ Geom3d::Vector.new(self * other.dx, self * other.dy, self * other.dz)
86
+ else
87
+ self.times_without_geom3d(other)
88
+ end
89
+ end
90
+ end
91
+
92
+ class Bignum
93
+ alias :times_without_geom3d :'*'
94
+
95
+ def * other
96
+ case other
97
+ when Geom3d::Vector
98
+ Geom3d::Vector.new(self * other.dx, self * other.dy, self * other.dz)
99
+ else
100
+ self.times_without_geom3d(other)
101
+ end
102
+ end
103
+ end
104
+
105
+ class Float
106
+ alias :times_without_geom3d :'*'
107
+
108
+ def * other
109
+ case other
110
+ when Geom3d::Vector
111
+ Geom3d::Vector.new(self * other.dx, self * other.dy, self * other.dz)
112
+ else
113
+ self.times_without_geom3d(other)
114
+ end
115
+ end
116
+ end
@@ -0,0 +1,3 @@
1
+ module Geom3d
2
+ VERSION = "0.1.0"
3
+ end
data/lib/geom3d.rb ADDED
@@ -0,0 +1,13 @@
1
+ %w{
2
+ geom3d/eps
3
+ geom3d/line
4
+ geom3d/point
5
+ geom3d/ray
6
+ geom3d/vector
7
+ }.each do |file|
8
+ require file
9
+ end
10
+
11
+ module Geom3d
12
+ # Your code goes here...
13
+ end
@@ -0,0 +1,59 @@
1
+ require_relative '../spec_helper.rb'
2
+
3
+ require 'geom3d/point'
4
+
5
+ module Geom3d
6
+ describe Point do
7
+ context 'when constructed with three parameters' do
8
+ before :each do
9
+ @x, @y, @z = rand, rand, rand
10
+ @point = Point.new(@x, @y, @z)
11
+ end
12
+
13
+ [:x, :y, :z].each do |axis|
14
+ it "allows accessing the #{axis} co-ordinate via the name" do
15
+ eval "@point.#{axis}.should == @#{axis}"
16
+ end
17
+ end
18
+ end
19
+
20
+ context 'when constructed with an array' do
21
+ before :each do
22
+ @x, @y, @z = rand, rand, rand
23
+ @point = Point.new([@x, @y, @z])
24
+ end
25
+
26
+ [:x, :y, :z].each do |axis|
27
+ it "allows accessing the #{axis} co-ordinate via the name" do
28
+ eval "@point.#{axis}.should == @#{axis}"
29
+ end
30
+ end
31
+ end
32
+
33
+ describe 'point - point' do
34
+ before :each do
35
+ @x1, @x2, @y1, @y2, @z1, @z2 = rand, rand, rand, rand, rand, rand
36
+ @point1 = Point.new(@x1, @y1, @z1)
37
+ @point2 = Point.new(@x2, @y2, @z2)
38
+ @result = @point2 - @point1
39
+ end
40
+
41
+ it 'returns a difference vector' do
42
+ @result.should == Vector.new(@x2 - @x1, @y2 - @y1, @z2 - @z1)
43
+ end
44
+ end
45
+
46
+ describe 'point + vector' do
47
+ before :each do
48
+ @x, @y, @z, @dx, @dy, @dz = rand, rand, rand, rand, rand, rand
49
+ @point = Point.new(@x, @y, @z)
50
+ @vector = Vector.new(@dx, @dy, @dz)
51
+ @result = @point + @vector
52
+ end
53
+
54
+ it 'returns a shifted point' do
55
+ @result.should == Point.new(@x + @dx, @y + @dy, @z + @dz)
56
+ end
57
+ end
58
+ end
59
+ end
@@ -0,0 +1 @@
1
+ require 'rspec'
metadata ADDED
@@ -0,0 +1,94 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: geom3d
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 1
8
+ - 0
9
+ version: 0.1.0
10
+ platform: ruby
11
+ authors:
12
+ - Nemo157 (Wim Looman)
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2011-01-13 00:00:00 +13:00
18
+ default_executable:
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: rspec
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ none: false
25
+ requirements:
26
+ - - ~>
27
+ - !ruby/object:Gem::Version
28
+ segments:
29
+ - 2
30
+ - 3
31
+ - 0
32
+ version: 2.3.0
33
+ type: :development
34
+ version_requirements: *id001
35
+ description: A simple pure ruby 3d geometry library
36
+ email:
37
+ - ghostunderscore@gmail.com
38
+ executables: []
39
+
40
+ extensions: []
41
+
42
+ extra_rdoc_files: []
43
+
44
+ files:
45
+ - .gitignore
46
+ - Gemfile
47
+ - Rakefile
48
+ - geom3d.gemspec
49
+ - lib/geom3d.rb
50
+ - lib/geom3d/eps.rb
51
+ - lib/geom3d/line.rb
52
+ - lib/geom3d/point.rb
53
+ - lib/geom3d/ray.rb
54
+ - lib/geom3d/vector.rb
55
+ - lib/geom3d/version.rb
56
+ - spec/geom3d/point_spec.rb
57
+ - spec/spec_helper.rb
58
+ has_rdoc: true
59
+ homepage: ""
60
+ licenses:
61
+ - Apache License 2.0
62
+ post_install_message:
63
+ rdoc_options: []
64
+
65
+ require_paths:
66
+ - lib
67
+ required_ruby_version: !ruby/object:Gem::Requirement
68
+ none: false
69
+ requirements:
70
+ - - ">="
71
+ - !ruby/object:Gem::Version
72
+ segments:
73
+ - 1
74
+ - 9
75
+ - 2
76
+ version: 1.9.2
77
+ required_rubygems_version: !ruby/object:Gem::Requirement
78
+ none: false
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ segments:
83
+ - 0
84
+ version: "0"
85
+ requirements: []
86
+
87
+ rubyforge_project: geom3d
88
+ rubygems_version: 1.3.7
89
+ signing_key:
90
+ specification_version: 3
91
+ summary: A simple pure ruby 3d geometry library
92
+ test_files:
93
+ - spec/geom3d/point_spec.rb
94
+ - spec/spec_helper.rb