geom 0.0.1

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.
@@ -0,0 +1,193 @@
1
+ require_relative '../spec_helper.rb'
2
+ require 'geom/vector'
3
+
4
+ module Geom
5
+ describe Vector do
6
+ before do
7
+ @valid_attributes = [1.1, -2, 10]
8
+ end
9
+
10
+ describe "Construction" do
11
+ it "should create a valid instance from an array of coordinates" do
12
+ vector = Vector.new(@valid_attributes)
13
+ vector.x.should == @valid_attributes[0]
14
+ vector.y.should == @valid_attributes[1]
15
+ vector.z.should == @valid_attributes[2]
16
+ end
17
+
18
+ it "should create a valid instance from three numbers" do
19
+ vector = Vector.new(@valid_attributes[0], @valid_attributes[1], @valid_attributes[2])
20
+ vector.x.should == @valid_attributes[0]
21
+ vector.y.should == @valid_attributes[1]
22
+ vector.z.should == @valid_attributes[2]
23
+ end
24
+
25
+ it "should create a valid instance from two points" do
26
+ from_point = Point.new(0,-3,-1)
27
+ to_point = Point.new(3,-3,6)
28
+ result = Vector.new(3,0,7)
29
+ Vector.new(from_point, to_point).should == result
30
+ end
31
+ end
32
+
33
+ describe "Arithmetic" do
34
+ before do
35
+ @first_vector = Vector.new(0,0,0)
36
+ @second_vector = Vector.new(1,2,3)
37
+ end
38
+
39
+ it "should allow addition with another vector" do
40
+ result = @first_vector + @second_vector
41
+ result.x.should == 1
42
+ result.y.should == 2
43
+ result.z.should == 3
44
+ end
45
+
46
+ it "should allow subtraction with another vector" do
47
+ result = @first_vector - @second_vector
48
+ result.x.should == -1
49
+ result.y.should == -2
50
+ result.z.should == -3
51
+ end
52
+
53
+ it "should calculate dot product" do
54
+ v1 = Vector.new(3, -2, 1)
55
+ v2 = Vector.new(0, 2, 4)
56
+ v1.dot(v2).should == 0.0
57
+ end
58
+
59
+ it "should calculate cross product" do
60
+ v1 = Vector.new(1, 0, 0)
61
+ v2 = Vector.new(0, 1, 0)
62
+ v1.cross(v2).should == Vector.new(0, 0, 1)
63
+
64
+ v3 = Vector.new(2, 1, 1)
65
+ v4 = Vector.new(-4, 3, 1)
66
+ v3.cross(v4).should == Vector.new(-2, -6, 10)
67
+ end
68
+ end
69
+
70
+ it "should reverse direction" do
71
+ Vector.new(1,-1,9).reverse.should == Vector.new(-1,1,-9)
72
+ end
73
+
74
+ describe "should calculate angle to another vector" do
75
+ before do
76
+ @v1 = Vector.new(2, 0, 0)
77
+ @v2 = Vector.new(0, -3, 0)
78
+ @v3 = Vector.new(0, 3, 0)
79
+ @v4 = Vector.new(-2, -2, 0)
80
+ @v5 = Vector.new(0, 1, 0)
81
+ @v6 = Vector.new(0, -1, 0)
82
+ end
83
+
84
+ it "when vectors are separated by 90 degrees and in fourth quadrant" do
85
+ @v1.angle_between(@v2).should == Math::PI / 2
86
+ end
87
+
88
+ it "when vectors are separated by 90 degrees and in first quadrant" do
89
+ @v1.angle_between(@v3).should == Math::PI / 2
90
+ end
91
+
92
+ it "when vectors are separated by 135 degrees and in the first and second quadrants" do
93
+ @v1.angle_between(@v4).should == Math::PI * (3 / 4.0)
94
+ end
95
+
96
+ it "when vectors are separated by 180 degrees" do
97
+ @v5.angle_between(@v6).should == Math::PI
98
+ end
99
+ end
100
+
101
+ describe "should determine if vectors have same direction" do
102
+ before do
103
+ @v1 = Vector.new(1, 0, 0)
104
+ @v2 = Vector.new(1, 1, 0)
105
+ @v3 = Vector.new(-1, 0.001, 0)
106
+ end
107
+
108
+ it "when vectors are 45 degrees apart" do
109
+ @v1.same_direction?(@v2).should be_true
110
+ end
111
+
112
+ it "when vectors are 180 degrees apart" do
113
+ @v1.same_direction?(@v3).should be_false
114
+ end
115
+ end
116
+
117
+ describe "should determine if parallel with another vector" do
118
+ before do
119
+ @v1 = Vector.new(1, 0, 0)
120
+ @v2 = Vector.new(-2, 0, 0)
121
+ @v3 = Vector.new(-2, 0.001, 0)
122
+ end
123
+ it "when vectors are 180 degrees apart" do
124
+ @v1.parallel?(@v2).should be_true
125
+ end
126
+
127
+ it "when vectors are slightly misaligned" do
128
+ @v1.parallel?(@v3).should be_false
129
+ end
130
+ end
131
+
132
+ it "should determine if zero vector" do
133
+ v1 = Vector.new(0, 0, 0)
134
+ v2 = Vector.new(-2, 0, 0)
135
+ v1.zero?.should be_true
136
+ v2.zero?.should be_false
137
+ end
138
+
139
+ it "should calculated the average of an array of vectors" do
140
+ vectors = [Vector.new(0,0,0), Vector.new(1,1,1), Vector.new(10,-10,2)]
141
+ average_vector = Vector.new(11/3.0, -9/3.0, 3/3.0)
142
+ Vector.average(vectors).should == average_vector
143
+ end
144
+
145
+ it "should calculated the sum of an array of vectors" do
146
+ vectors = [Vector.new(0,0,0), Vector.new(1,1,1), Vector.new(10,-10,2)]
147
+ result_vector = Vector.new(11, -9, 3)
148
+ Vector.sum(vectors).should == result_vector
149
+ end
150
+
151
+ it "should calculate a new vector rotated around an axis vector a supplied angle" do
152
+ vector_1 = Vector.new(3.2, 0, 0)
153
+ axis = Vector.new(0, 0, -1)
154
+ vector_2 = vector_1.rotate(axis, (33 * Math::PI / 180))
155
+ vector_2.x.should be_within(0.001).of(2.684)
156
+ vector_2.y.should be_within(0.001).of(-1.743)
157
+ vector_2.z.should be_within(0.001).of(0.0)
158
+ end
159
+
160
+ describe "Return Types" do
161
+ it "should return as point" do
162
+ Vector.new(@valid_attributes).to_point.should == Point.new(@valid_attributes)
163
+ end
164
+
165
+ it "should return as array" do
166
+ Vector.new(@valid_attributes).to_a.should == @valid_attributes
167
+ end
168
+ it "should return a summary string" do
169
+ Vector.new(1,2,3).to_s.should == "Vector(1.000,2.000,3.000)"
170
+ end
171
+
172
+ it "should return a hash code" do
173
+ Vector.new(1,2.88,-45.111).hash.should == -48
174
+ end
175
+ end
176
+
177
+ describe "Translation" do
178
+ end
179
+
180
+ describe "Transformation:" do
181
+ it "should transform into a coordinate system" do
182
+ v1 = Vector.new(2,2,0)
183
+ p2 = Point.new(5,5,0)
184
+ vx = Vector.new(1,0,0)
185
+ vy = Vector.new(0,1,0)
186
+ vz = Vector.new(0,0,1)
187
+ rcs = RectangularCoordinateSystem.new_from_xvector_and_xyplane(p2, vy, vz)
188
+ v1.transform(rcs).should == Vector.new(-3,3,0)
189
+ end
190
+ end
191
+
192
+ end
193
+ end
@@ -0,0 +1,7 @@
1
+ require 'simplecov'
2
+ SimpleCov.start do
3
+ add_filter "/spec/"
4
+ end
5
+
6
+ require 'rspec'
7
+ require 'geom'
metadata ADDED
@@ -0,0 +1,82 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: geom
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 0.0.1
6
+ platform: ruby
7
+ authors:
8
+ - Adrian Smith
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2012-01-04 00:00:00 +10:00
14
+ default_executable:
15
+ dependencies: []
16
+
17
+ description: A 3D geometry library that includes Point, Vector, Line, Plane, Coordinate System and Transformation objects
18
+ email:
19
+ - adrian.smith@ennova.com.au
20
+ executables: []
21
+
22
+ extensions: []
23
+
24
+ extra_rdoc_files: []
25
+
26
+ files:
27
+ - .gitignore
28
+ - Gemfile
29
+ - Rakefile
30
+ - geom.gemspec
31
+ - lib/geom.rb
32
+ - lib/geom/line.rb
33
+ - lib/geom/plane.rb
34
+ - lib/geom/point.rb
35
+ - lib/geom/rectangular_coordinate_system.rb
36
+ - lib/geom/tolerance.rb
37
+ - lib/geom/transformation.rb
38
+ - lib/geom/vector.rb
39
+ - lib/geom/version.rb
40
+ - spec/geom/line_spec.rb
41
+ - spec/geom/plane_spec.rb
42
+ - spec/geom/point_spec.rb
43
+ - spec/geom/rectangular_coordinate_system_spec.rb
44
+ - spec/geom/transformation_spec.rb
45
+ - spec/geom/vector_spec.rb
46
+ - spec/spec_helper.rb
47
+ has_rdoc: true
48
+ homepage: https://github.com/AdrianSmith/geom
49
+ licenses: []
50
+
51
+ post_install_message:
52
+ rdoc_options: []
53
+
54
+ require_paths:
55
+ - lib
56
+ required_ruby_version: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: "0"
62
+ required_rubygems_version: !ruby/object:Gem::Requirement
63
+ none: false
64
+ requirements:
65
+ - - ">="
66
+ - !ruby/object:Gem::Version
67
+ version: "0"
68
+ requirements: []
69
+
70
+ rubyforge_project:
71
+ rubygems_version: 1.6.2
72
+ signing_key:
73
+ specification_version: 3
74
+ summary: A 3D geometry library
75
+ test_files:
76
+ - spec/geom/line_spec.rb
77
+ - spec/geom/plane_spec.rb
78
+ - spec/geom/point_spec.rb
79
+ - spec/geom/rectangular_coordinate_system_spec.rb
80
+ - spec/geom/transformation_spec.rb
81
+ - spec/geom/vector_spec.rb
82
+ - spec/spec_helper.rb