geo3d 0.0.4 → 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: bb43721b8bbf57fe410e61ea947c754644f50ff2
4
- data.tar.gz: 6e913f011af0bc1ecac10e0ba19891b70dc9039d
3
+ metadata.gz: 8af038ca077d7a6bdfd933b9486cf4a63ab8a73f
4
+ data.tar.gz: 75bb46be5985112547d52bcdfb06046c651ba2af
5
5
  SHA512:
6
- metadata.gz: 88c2a7d26f6aca9cbe58106fcf10a0c5e5ce9721f9344fa0d9a4bf44c5d12715a9f203a007c66aa321033ca4cb8678596e0d409c15caf9feebe138bea4dfd68a
7
- data.tar.gz: 59916b7493f0c27a3ba3f2fecbaecdb9cbcd3e0fdc1612aabe57fb1a7f158f7aa4337c2cec8c0c037d4c08ea59558b0b5ec5d1b3192e2f2437e85f737451ebb6
6
+ metadata.gz: 81c47c990a4c186edb1c07d632f1c287d3854686ab589e071f6a496733d08b8c04929989ee473c7a08d91b5451e6cc1583311604cf87e4fb859a998b7c31b216
7
+ data.tar.gz: 2799ed1ff5754f41b74ce781105c68a5ba864661346d36967012be1ca5cdbeebf8411b726713ea0ed95e3d23b4aaf845788de786c892157a13f64b1007c8a67f
data/README.md CHANGED
@@ -167,10 +167,38 @@ Misc constructors
167
167
  Geo3d::Matrix.reflection reflection_plane #returns a reflection matrix where reflection_plane is a Geo3d::Vector that corresponds to the normal of the plane
168
168
  Geo3d::Matrix.shadow light_position, plane #returns a shadow matrix
169
169
  ```
170
+ Matrix Decomposition
171
+ ```
172
+ matrix.scaling_component
173
+ matrix.translation_component
174
+ matrix.rotation_component
175
+ ```
170
176
 
171
177
 
172
178
  ## Quaternion
173
179
 
180
+ A mathematical construct to represent rotations in 3d space.
181
+
182
+ Quaternion Multiplication
183
+ ```
184
+ quat_a * quat_b
185
+ ```
186
+ Getting axis and angle
187
+ ```
188
+ quat.axis
189
+ quat.angle
190
+ ```
191
+ Converting to a matrix
192
+ ```
193
+ quat.to_matrix
194
+ ```
195
+ Constructors
196
+ ```
197
+ Geo3d::Quaternion.from_axis rotation_axis, radians #returns a quaternion from an axis and angle
198
+ Geo3d::Quaternion.from_matrix m #returns a quaternion from a rotation matrix
199
+
200
+ ```
201
+
174
202
  Documentation coming soon!
175
203
 
176
204
 
data/lib/geo3d/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Geo3d
2
- VERSION = "0.0.4"
2
+ VERSION = "0.0.5"
3
3
  end
data/lib/quaternion.rb CHANGED
@@ -2,8 +2,15 @@ module Geo3d
2
2
  class Quaternion
3
3
  attr_reader :x, :y, :z, :w
4
4
 
5
- def initialize
6
- @x = @y = @z = @w = 0.0
5
+ def initialize *args
6
+ @x = 0.0
7
+ @y = 0.0
8
+ @z = 0.0
9
+ @w = 0.0
10
+ @x = args[0].to_f if args.size > 0
11
+ @y = args[1].to_f if args.size > 1
12
+ @z = args[2].to_f if args.size > 2
13
+ @w = args[3].to_f if args.size > 3
7
14
  end
8
15
 
9
16
  def x= v
@@ -22,6 +29,22 @@ module Geo3d
22
29
  @w = v.to_f
23
30
  end
24
31
 
32
+ def +@
33
+ self.class.new x, y, z, w
34
+ end
35
+
36
+ def -@
37
+ self.class.new -x, -y, -z, -w
38
+ end
39
+
40
+ def == q
41
+ Geo3d::Utils.float_cmp(x, q.x) && Geo3d::Utils.float_cmp(y, q.y) && Geo3d::Utils.float_cmp(z, q.z) && Geo3d::Utils.float_cmp(w, q.w)
42
+ end
43
+
44
+ def != vec
45
+ !(self == vec)
46
+ end
47
+
25
48
  def self.from_axis rotation_axis, radians = 0
26
49
  normalized_rotation_axis = rotation_axis.normalize
27
50
  #const float radians = GeoConvertToRadians( degrees );
@@ -34,6 +57,10 @@ module Geo3d
34
57
  q
35
58
  end
36
59
 
60
+ def self.from_axis_degrees rotation_axis, degrees = 0
61
+ from_axis rotation_axis, Geo3d::Utils.to_radians(degrees)
62
+ end
63
+
37
64
  def self.from_matrix pm
38
65
  pout = self.new
39
66
 
@@ -80,13 +107,30 @@ module Geo3d
80
107
  pout
81
108
  end
82
109
 
83
- def * quat
84
- out = Quat.new
85
- out.w = w * quat.w - x * quat.x - y * quat.y - z * quat.z
86
- out.x = w * quat.x + x * quat.w + y * quat.z - z * quat.y
87
- out.y = w * quat.y - x * quat.z + y * quat.w + z * quat.x
88
- out.z = w * quat.z + x * quat.y - y * quat.x + z * quat.w
89
- out
110
+ def + quat
111
+ self.class.new x + quat.x, y + quat.y, z + quat.z, w + quat.w
112
+ end
113
+
114
+ def - quat
115
+ self.class.new x - quat.x, y - quat.y, z - quat.z, w - quat.w
116
+ end
117
+
118
+ def * v
119
+ if Quaternion == v.class
120
+ quat = v
121
+ out = self.class.new
122
+ out.w = w * quat.w - x * quat.x - y * quat.y - z * quat.z
123
+ out.x = w * quat.x + x * quat.w + y * quat.z - z * quat.y
124
+ out.y = w * quat.y - x * quat.z + y * quat.w + z * quat.x
125
+ out.z = w * quat.z + x * quat.y - y * quat.x + z * quat.w
126
+ out
127
+ else
128
+ self.class.new x*v, y*v, z*v, w*v
129
+ end
130
+ end
131
+
132
+ def / v
133
+ self.class.new x/v, y/v, z/v, w/v
90
134
  end
91
135
 
92
136
  def to_matrix
@@ -105,15 +149,70 @@ module Geo3d
105
149
  end
106
150
 
107
151
  def axis
152
+ q = normalize
108
153
  v = Vector.new
109
- v.x = x / Math.sqrt(1-w*w)
110
- v.y = y / Math.sqrt(1-w*w)
111
- v.z = z / Math.sqrt(1-w*w)
154
+ v.x = q.x / Math.sqrt(1-q.w*q.w)
155
+ v.y = q.y / Math.sqrt(1-q.w*q.w)
156
+ v.z = q.z / Math.sqrt(1-q.w*q.w)
112
157
  v
113
158
  end
114
159
 
115
160
  def angle
116
- Math.acos(w) * 2.0
161
+ q = normalize
162
+ Math.acos(q.w) * 2.0
163
+ end
164
+
165
+ def angle_degrees
166
+ Geo3d::Utils.to_degrees angle
167
+ end
168
+
169
+ def length_squared
170
+ dot self
171
+ end
172
+
173
+ def length
174
+ Math.sqrt length_squared
175
+ end
176
+
177
+ def dot quat
178
+ x * quat.x + y * quat.y + z * quat.z + w * quat.w
179
+ end
180
+
181
+ def normalize!
182
+ len = length
183
+ if length > 0
184
+ @x /= len
185
+ @y /= len
186
+ @z /= len
187
+ @w /= len
188
+ end
189
+ end
190
+
191
+ def normalize
192
+ q = self.class.new x, y, z, w
193
+ q.normalize!
194
+ q
195
+ end
196
+
197
+ def conjugate
198
+ self.class.new -x, -y, -z, w
199
+ end
200
+
201
+ def inverse
202
+ norm = length_squared
203
+ if norm.zero?
204
+ self.class.new 0, 0, 0, 0
205
+ else
206
+ conjugate / norm
207
+ end
208
+ end
209
+
210
+ def identity?
211
+ self == self.class.identity
212
+ end
213
+
214
+ def self.identity
215
+ self.new 0, 0, 0, 1
117
216
  end
118
217
  end
119
218
  end
data/lib/utils.rb CHANGED
@@ -3,5 +3,13 @@ module Geo3d
3
3
  def self.float_cmp a, b, tolerance = 0.01
4
4
  (a-b).abs < tolerance
5
5
  end
6
+
7
+ def self.to_degrees radians
8
+ radians * 180.0 / Math::PI
9
+ end
10
+
11
+ def self.to_radians degrees
12
+ degrees * Math::PI / 180.0
13
+ end
6
14
  end
7
15
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: geo3d
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.0.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Misha Conway
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-01-12 00:00:00.000000000 Z
11
+ date: 2014-01-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler