geo3d 0.0.3 → 0.0.4

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: d922bc02f451ded2c7a44b2b3b8aa96f0606b023
4
- data.tar.gz: 9d02cbf3e7580833d546447d011e7f360a879727
3
+ metadata.gz: bb43721b8bbf57fe410e61ea947c754644f50ff2
4
+ data.tar.gz: 6e913f011af0bc1ecac10e0ba19891b70dc9039d
5
5
  SHA512:
6
- metadata.gz: 09bc0f46c7f284253784270a80937bc41c7e610a0b869fee9d1333f86d5a1231b4d428f834ad4eb42645c26be71e15fb567cd397c102dabe33cde6ac5b0a57aa
7
- data.tar.gz: 2c0a45d96a0824b1ed1e1b5be2e9e8fe6a0c7751c0f8081f69afbc09e34b8245c3299bfdce9106237377e2940933921fcc5d53b345d53dd8ab37657b8e02819b
6
+ metadata.gz: 88c2a7d26f6aca9cbe58106fcf10a0c5e5ce9721f9344fa0d9a4bf44c5d12715a9f203a007c66aa321033ca4cb8678596e0d409c15caf9feebe138bea4dfd68a
7
+ data.tar.gz: 59916b7493f0c27a3ba3f2fecbaecdb9cbcd3e0fdc1612aabe57fb1a7f158f7aa4337c2cec8c0c037d4c08ea59558b0b5ec5d1b3192e2f2437e85f737451ebb6
data/README.md CHANGED
@@ -165,6 +165,7 @@ View matrix constructors
165
165
  Misc constructors
166
166
  ```
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
+ Geo3d::Matrix.shadow light_position, plane #returns a shadow matrix
168
169
  ```
169
170
 
170
171
 
@@ -1,3 +1,3 @@
1
1
  module Geo3d
2
- VERSION = "0.0.3"
2
+ VERSION = "0.0.4"
3
3
  end
@@ -1,3 +1,5 @@
1
+ require 'utils'
2
+
1
3
  module Geo3d
2
4
  class Matrix
3
5
  attr_accessor :_11, :_12, :_13, :_14
@@ -6,7 +8,22 @@ module Geo3d
6
8
  attr_accessor :_41, :_42, :_43, :_44
7
9
 
8
10
  def initialize *args
9
- @_11 = 0, @_12 = 0, @_13 = 0, @_14 = 0, @_21 = 0, @_22 = 0, @_23 = 0, @_24 = 0, @_31 = 0, @_32 = 0, @_33 = 0, @_34 = 0, @_41 = 0, @_42 = 0, @_43 = 0, @_44 = 0
11
+ @_11 = 0
12
+ @_12 = 0
13
+ @_13 = 0
14
+ @_14 = 0
15
+ @_21 = 0
16
+ @_22 = 0
17
+ @_23 = 0
18
+ @_24 = 0
19
+ @_31 = 0
20
+ @_32 = 0
21
+ @_33 = 0
22
+ @_34 = 0
23
+ @_41 = 0
24
+ @_42 = 0
25
+ @_43 = 0
26
+ @_44 = 0
10
27
  @_11 = args[0] if args.size > 0
11
28
  @_12 = args[1] if args.size > 1
12
29
  @_13 = args[2] if args.size > 2
@@ -30,11 +47,24 @@ module Geo3d
30
47
  end
31
48
 
32
49
  def [] x, y
33
- to_a[4*y + x]
50
+ to_a[4*x + y]
34
51
  end
35
52
 
36
53
  def []= x, y, v
37
- send (%w{_11 _12 _13 _14 _21 _22 _23 _24 _31 _32 _33 _34 _41 _42 _43 _44}[4*y + x] + '=').to_sym, v
54
+ send (%w{_11 _12 _13 _14 _21 _22 _23 _24 _31 _32 _33 _34 _41 _42 _43 _44}[4*x + y] + '=').to_sym, v
55
+ end
56
+
57
+ def == m
58
+ a = to_a
59
+ b = m.to_a
60
+ for i in 0..15
61
+ return false unless Geo3d::Utils.float_cmp a[i], b[i]
62
+ end
63
+ true
64
+ end
65
+
66
+ def != m
67
+ !(self == m)
38
68
  end
39
69
 
40
70
  def +@
@@ -46,7 +76,7 @@ module Geo3d
46
76
  end
47
77
 
48
78
  def + mat
49
- sum = Matrix.new
79
+ sum = self.class.new
50
80
 
51
81
  sum._11 = _11 + mat._11
52
82
  sum._12 = _12 + mat._12
@@ -72,7 +102,7 @@ module Geo3d
72
102
  end
73
103
 
74
104
  def - mat
75
- sum = Matrix.new
105
+ sum = self.class.new
76
106
 
77
107
  sum._11 = _11 - mat._11
78
108
  sum._12 = _12 - mat._12
@@ -98,9 +128,9 @@ module Geo3d
98
128
  end
99
129
 
100
130
  def * v
101
- result = Matrix.new
131
+ result = self.class.new
102
132
 
103
- if Matrix == v.class
133
+ if self.class == v.class
104
134
  matrix = v
105
135
 
106
136
  result._11 = _11 * matrix._11 + _12 * matrix._21 + _13 * matrix._31 + _14 * matrix._41
@@ -154,12 +184,12 @@ module Geo3d
154
184
  end
155
185
 
156
186
  def / v
157
- if Matrix == v.class
187
+ if self.class == v.class
158
188
  self * v.inverse
159
189
  elsif Vector == v.class
160
190
  raise 'dividing matrices by vectors not currently supported'
161
191
  else
162
- result = Matrix.new
192
+ result = self.class.new
163
193
  scalar = v
164
194
  result._11 = _11 / scalar
165
195
  result._12 = _12 / scalar
@@ -194,8 +224,36 @@ module Geo3d
194
224
  self * vec
195
225
  end
196
226
 
227
+ def identity?
228
+ self == self.class.identity
229
+ end
230
+
231
+ def translation_component
232
+ Vector.new _41, _42, _43
233
+ end
234
+
235
+ def scaling_component
236
+ Vector.new Vector.new(_11, _12, _13).length, Vector.new(_21, _22, _23).length, Vector.new(_31, _32, _33).length
237
+ end
238
+
239
+ def rotation_component
240
+ scaling = scaling_component
241
+ return nil if scaling.x.zero? || scaling.y.zero? || scaling.z.zero?
242
+ m = Matrix.new
243
+ m._11=_11 / scaling.x
244
+ m._12=_12/scaling.x
245
+ m._13=_13/scaling.x
246
+ m._21=_21/scaling.y
247
+ m._22=_22/scaling.y
248
+ m._23=_23/scaling.y
249
+ m._31=_31/scaling.z
250
+ m._32=_32/scaling.z
251
+ m._33=_33/scaling.z
252
+ Quaternion.from_matrix m
253
+ end
254
+
197
255
  def self.identity
198
- identity_matrix = Matrix.new
256
+ identity_matrix = self.new
199
257
  identity_matrix._12 = identity_matrix._13 = identity_matrix._14 = 0
200
258
  identity_matrix._21 = identity_matrix._23 = identity_matrix._24 = 0
201
259
  identity_matrix._31 = identity_matrix._32 = identity_matrix._34 = 0
@@ -204,6 +262,10 @@ module Geo3d
204
262
  identity_matrix
205
263
  end
206
264
 
265
+ def determinant
266
+ inverse(true).last
267
+ end
268
+
207
269
  def inverse with_determinant = false
208
270
  mat = to_a
209
271
  dst = Array.new 16
@@ -291,7 +353,7 @@ module Geo3d
291
353
  dst[j] *= det
292
354
  end
293
355
 
294
- inverted_matrix = Matrix.new *dst
356
+ inverted_matrix = self.class.new *dst
295
357
 
296
358
  if with_determinant
297
359
  [inverted_matrix, det]
@@ -301,7 +363,7 @@ module Geo3d
301
363
  end
302
364
 
303
365
  def transpose
304
- transposed_matrix = Matrix.new
366
+ transposed_matrix = self.class.new
305
367
  transposed_matrix._11 = _11
306
368
  transposed_matrix._12 = _21
307
369
  transposed_matrix._13 = _31
@@ -347,7 +409,7 @@ module Geo3d
347
409
  zf = zf.to_f
348
410
  y_scale = 1.0 / Math.tan(0.5*fovy)
349
411
  x_scale = y_scale / aspect
350
- matrix = Matrix.new
412
+ matrix = self.new
351
413
  matrix._11 = x_scale
352
414
  matrix._22 = y_scale
353
415
  matrix._33 = zf/(zn - zf)
@@ -363,7 +425,7 @@ module Geo3d
363
425
  zf = zf.to_f
364
426
  y_scale = 1.0 / Math.tan(0.5*fovy)
365
427
  x_scale = y_scale / aspect
366
- matrix = Matrix.new
428
+ matrix = self.new
367
429
  matrix._11 = x_scale
368
430
  matrix._22 = y_scale
369
431
  matrix._33 = zf/(zf - zn)
@@ -411,7 +473,7 @@ module Geo3d
411
473
  xaxis = up_direction.cross(zaxis).normalize
412
474
  yaxis = zaxis.cross xaxis
413
475
 
414
- matrix = Matrix.new
476
+ matrix = self.new
415
477
 
416
478
  # set column one
417
479
  matrix._11 = xaxis.x
@@ -443,7 +505,7 @@ module Geo3d
443
505
  xaxis = up_direction.cross(zaxis).normalize
444
506
  yaxis = zaxis.cross xaxis
445
507
 
446
- matrix = Matrix.new
508
+ matrix = self.new
447
509
 
448
510
  # set column one
449
511
  matrix._11 = xaxis.x
@@ -471,7 +533,7 @@ module Geo3d
471
533
  end
472
534
 
473
535
  def self.reflection reflection_plane
474
- reflection_matrix = Matrix.new
536
+ reflection_matrix = self.new
475
537
 
476
538
  plane_magnitude = Vector.new(reflection_plane.x, reflection_plane.y, reflection_plane.z, 0).length
477
539
  normalized_plane = reflection_plane / plane_magnitude
@@ -508,7 +570,7 @@ module Geo3d
508
570
  normalized_plane = plane / norm
509
571
  dot = normalized_plane.dot(light_position)
510
572
 
511
- m = Matrix.new
573
+ m = self.new
512
574
  m._11 = dot - normalized_plane.a * light_position.x
513
575
  m._12 = -normalized_plane.a * light_position.y
514
576
  m._13 = -normalized_plane.a * light_position.z
@@ -531,7 +593,7 @@ module Geo3d
531
593
 
532
594
 
533
595
  def self.translation x, y, z
534
- translation_matrix = Matrix.new
596
+ translation_matrix = self.new
535
597
  translation_matrix._11 = translation_matrix._22 = translation_matrix._33 = translation_matrix._44 = 1
536
598
  #todo: consider simplifying with identity
537
599
  translation_matrix._41 = x
@@ -541,7 +603,7 @@ module Geo3d
541
603
  end
542
604
 
543
605
  def self.scaling x, y, z
544
- scaling_matrix = Matrix.new
606
+ scaling_matrix = self.new
545
607
  scaling_matrix._11 = x
546
608
  scaling_matrix._22 = y
547
609
  scaling_matrix._33 = z
@@ -2,18 +2,82 @@ module Geo3d
2
2
  class Quaternion
3
3
  attr_reader :x, :y, :z, :w
4
4
 
5
- def initialize rotation_axis = nil, radians = 0
6
- @x = @y = @z = @w = 0
5
+ def initialize
6
+ @x = @y = @z = @w = 0.0
7
+ end
8
+
9
+ def x= v
10
+ @x = v.to_f
11
+ end
12
+
13
+ def y= v
14
+ @y = v.to_f
15
+ end
16
+
17
+ def z= v
18
+ @z = v.to_f
19
+ end
20
+
21
+ def w= v
22
+ @w = v.to_f
23
+ end
24
+
25
+ def self.from_axis rotation_axis, radians = 0
26
+ normalized_rotation_axis = rotation_axis.normalize
27
+ #const float radians = GeoConvertToRadians( degrees );
28
+
29
+ q = self.new
30
+ q.x = Math.sin(radians / 2.0) * normalized_rotation_axis.x
31
+ q.y = Math.sin(radians / 2.0) * normalized_rotation_axis.y
32
+ q.z = Math.sin(radians / 2.0) * normalized_rotation_axis.z
33
+ q.w = Math.cos(radians / 2.0)
34
+ q
35
+ end
36
+
37
+ def self.from_matrix pm
38
+ pout = self.new
39
+
40
+ trace = pm._11 + pm._22 + pm._33 + 1.0
41
+ if trace > 0
42
+ pout.x = (pm._23 - pm._32) / (2.0 * Math.sqrt(trace))
43
+ pout.y = (pm._31 - pm._13) / (2.0 * Math.sqrt(trace))
44
+ pout.z = (pm._12- pm._21) / (2.0 * Math.sqrt(trace))
45
+ pout.w = Math.sqrt(trace) / 2.0
46
+ return pout
47
+ end
48
+ maxi = 0
49
+ maxdiag = pm._11
50
+
51
+
52
+ for i in 1..2
53
+ if pm[i, i] > maxdiag #todo: indexing might need to be fixed > maxdiag
54
+ maxi = i
55
+ maxdiag = pm[i, i] #todo: indexing might need to be fixed
56
+ end
57
+ end
58
+ case maxi
59
+ when 0
60
+ s = 2.0 * Math.sqrt(1.0 + pm._11 - pm._22 - pm._33)
61
+ pout.x = 0.25 * s
62
+ pout.y = (pm._12 + pm._21) / s
63
+ pout.z = (pm._13 + pm._31) / s
64
+ pout.w = (pm._23 - pm._32) / s
7
65
 
8
- if rotation_axis
66
+ when 1
67
+ s = 2.0 * Math.sqrt(1.0 + pm._22 - pm._11 - pm._33)
68
+ pout.x = (pm._12 + pm._21) / s
69
+ pout.y = 0.25 * s
70
+ pout.z = (pm._23 + pm._32) / s
71
+ pout.w = (pm._31 - pm._13) / s
9
72
 
10
- normalized_rotation_axis = rotation_axis.normalize
11
- #const float radians = GeoConvertToRadians( degrees );
12
- @x = Math.sin(radians / 2.0) * normalized_rotation_axis.x
13
- @y = Math.sin(radians / 2.0) * normalized_rotation_axis.y
14
- @z = Math.sin(radians / 2.0) * normalized_rotation_axis.z
15
- @w = Math.cos(radians / 2.0);
73
+ when 2
74
+ s = 2.0 * Math.sqrt(1.0 + pm._33 - pm._11 - pm._22)
75
+ pout.x = (pm._13 + pm._31) / s
76
+ pout.y = (pm._23 + pm._32) / s
77
+ pout.z = 0.25 * s
78
+ pout.w = (pm._12 - pm._21) / s
16
79
  end
80
+ pout
17
81
  end
18
82
 
19
83
  def * quat
@@ -41,7 +105,11 @@ module Geo3d
41
105
  end
42
106
 
43
107
  def axis
44
- Vector.new(x, y, z).normalize
108
+ 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)
112
+ v
45
113
  end
46
114
 
47
115
  def angle
@@ -0,0 +1,7 @@
1
+ module Geo3d
2
+ module Utils
3
+ def self.float_cmp a, b, tolerance = 0.01
4
+ (a-b).abs < tolerance
5
+ end
6
+ end
7
+ end
@@ -7,7 +7,10 @@ module Geo3d
7
7
  alias :d :w
8
8
 
9
9
  def initialize *args
10
- @x, @y, @z, @w = 0, 0, 0, 0
10
+ @x = 0.0
11
+ @y = 0.0
12
+ @z = 0.0
13
+ @w = 0.0
11
14
  @x = args[0].to_f if args.size > 0
12
15
  @y = args[1].to_f if args.size > 1
13
16
  @z = args[2].to_f if args.size > 2
@@ -47,11 +50,11 @@ module Geo3d
47
50
  end
48
51
 
49
52
  def == vec
50
- x == vec.x && y == vec.y && z == vec.z && w == vec.w
53
+ Geo3d::Utils.float_cmp(x, vec.x) && Geo3d::Utils.float_cmp(y, vec.y) && Geo3d::Utils.float_cmp(z, vec.z) && Geo3d::Utils.float_cmp(w, vec.w)
51
54
  end
52
55
 
53
56
  def != vec
54
- x != vec.x || y != vec.y || z != vec.z || w != vec.w
57
+ !(self == vec)
55
58
  end
56
59
 
57
60
  def cross vec
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.3
4
+ version: 0.0.4
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-11 00:00:00.000000000 Z
11
+ date: 2014-01-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -55,6 +55,7 @@ files:
55
55
  - lib/geo3d/version.rb
56
56
  - lib/matrix.rb
57
57
  - lib/quaternion.rb
58
+ - lib/utils.rb
58
59
  - lib/vector.rb
59
60
  homepage: https://github.com/MishaConway/geo3d
60
61
  licenses: