numerix 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (60) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +18 -0
  3. data/.travis.yml +5 -0
  4. data/.yardopts +5 -0
  5. data/CODE_OF_CONDUCT.md +74 -0
  6. data/Gemfile +6 -0
  7. data/LICENSE.txt +21 -0
  8. data/README.md +66 -0
  9. data/Rakefile +18 -0
  10. data/TODO.txt +25 -0
  11. data/bin/console +14 -0
  12. data/bin/setup +8 -0
  13. data/ext/numerix/common.h +107 -0
  14. data/ext/numerix/extconf.rb +3 -0
  15. data/ext/numerix/matrix3x2.c +638 -0
  16. data/ext/numerix/matrix3x2.h +52 -0
  17. data/ext/numerix/matrix4x4.c +1807 -0
  18. data/ext/numerix/matrix4x4.h +90 -0
  19. data/ext/numerix/matrix_base.c +307 -0
  20. data/ext/numerix/matrix_base.h +70 -0
  21. data/ext/numerix/numerix.c +33 -0
  22. data/ext/numerix/numerix.h +19 -0
  23. data/ext/numerix/plane.c +311 -0
  24. data/ext/numerix/plane.h +34 -0
  25. data/ext/numerix/quaternion.c +712 -0
  26. data/ext/numerix/quaternion.h +53 -0
  27. data/ext/numerix/structure.c +154 -0
  28. data/ext/numerix/structure.h +24 -0
  29. data/ext/numerix/vector.c +326 -0
  30. data/ext/numerix/vector.h +57 -0
  31. data/ext/numerix/vector2.c +641 -0
  32. data/ext/numerix/vector2.h +64 -0
  33. data/ext/numerix/vector3.c +805 -0
  34. data/ext/numerix/vector3.h +68 -0
  35. data/ext/numerix/vector4.c +727 -0
  36. data/ext/numerix/vector4.h +63 -0
  37. data/ext/numerix/vector_base.c +94 -0
  38. data/ext/numerix/vector_base.h +30 -0
  39. data/extra/numerix128.png +0 -0
  40. data/extra/numerix24.png +0 -0
  41. data/extra/numerix32.png +0 -0
  42. data/extra/numerix320.png +0 -0
  43. data/extra/numerix48.png +0 -0
  44. data/extra/numerix96.png +0 -0
  45. data/lib/numerix/error.rb +36 -0
  46. data/lib/numerix/matrix3x2.rb +420 -0
  47. data/lib/numerix/matrix4x4.rb +676 -0
  48. data/lib/numerix/matrix_base.rb +14 -0
  49. data/lib/numerix/plane.rb +154 -0
  50. data/lib/numerix/quaternion.rb +355 -0
  51. data/lib/numerix/structure.rb +124 -0
  52. data/lib/numerix/vector.rb +13 -0
  53. data/lib/numerix/vector2.rb +534 -0
  54. data/lib/numerix/vector3.rb +572 -0
  55. data/lib/numerix/vector4.rb +551 -0
  56. data/lib/numerix/vector_base.rb +14 -0
  57. data/lib/numerix/version.rb +6 -0
  58. data/lib/numerix.rb +10 -0
  59. data/numerix.gemspec +30 -0
  60. metadata +167 -0
@@ -0,0 +1,551 @@
1
+ module Numerix
2
+
3
+ ##
4
+ # A structure encapsulating four single precision floating point values.
5
+ class Vector4 < VectorBase
6
+
7
+ ##
8
+ # @return [Float] the X component of the vector.
9
+ attr_accessor :x
10
+
11
+ ##
12
+ # @return [Float] the Y component of the vector.
13
+ attr_accessor :y
14
+
15
+ ##
16
+ # @return [Float] the Z component of the vector.
17
+ attr_accessor :z
18
+
19
+ ##
20
+ # @return [Float] the W component of the vector.
21
+ attr_accessor :w
22
+
23
+ ##
24
+ # @overload initialize
25
+ # Creates a Vector with the default values of `0.0`.
26
+ #
27
+ # @overload initialize(xyzw)
28
+ # Creates a Vector with each component set to a single value.
29
+ #
30
+ # @param xyzw [Float] The value to set for all components.
31
+ #
32
+ # @overload initialize(x, y, z, w)
33
+ # Creates a Vector with the specified values.
34
+ #
35
+ # @param x [Float] The X component of the vector.
36
+ # @param y [Float] The Y component of the vector.
37
+ # @param z [Float] The Z component of the vector.
38
+ # @param w [Float] The W component of the vector.
39
+ #
40
+ # @overload initialize(xyz, w)
41
+ # Creates a Vector with the specified values.
42
+ #
43
+ # @param xyz [Vector3] Vector3 to use for the X, Y, and Z components.
44
+ # @param w [Float] The W component of the vector.
45
+ #
46
+ # @overload initialize(xy, zw)
47
+ # Creates a Vector with the specified values.
48
+ #
49
+ # @param xy [Vector2] Vector2 to use for the X and Y components.
50
+ # @param zw [Vector2] Vector2 to use for the Z and W components.
51
+ #
52
+ # @overload initialize(xy, z, w)
53
+ # Creates a Vector with the specified values.
54
+ #
55
+ # @param xy [Vector2] Vector2 to use for the X and Y components.
56
+ # @param z [Float] The Z component of the vector.
57
+ # @param w [Float] The W component of the vector.
58
+ #
59
+ def initialize(*args)
60
+ end
61
+
62
+ ##
63
+ # The equivalent of `Enumerable#map`, but returns a vector object instead of
64
+ # an Array.
65
+ #
66
+ # Invokes the given block once for each element of `self`.
67
+ #
68
+ # Creates a new vector containing the values returned by the block.
69
+ #
70
+ # @yield [component] Yields a component of the vector to the block.
71
+ # @yieldparam component [Float] The yielded component.
72
+ #
73
+ # @return [Vector4]
74
+
75
+ # @see map!
76
+ def map
77
+ end
78
+
79
+ ##
80
+ # Invokes the given block once for each element of self, replacing the
81
+ # element with the value returned by the block.
82
+ #
83
+ # The values of the vector are altered without creating a ne object.
84
+ #
85
+ # @yield [component] Yields a component of the vector to the block.
86
+ # @yieldparam component [Float] The yielded component.
87
+ #
88
+ # @return [self]
89
+ #
90
+ # @see map
91
+ def map!
92
+ end
93
+
94
+ alias_method :collect, :map
95
+ alias_method :collect!, :map!
96
+
97
+ ##
98
+ # Raises the vector to the given power.
99
+ #
100
+ # @param exponent [Float] The power to raise the vector to.
101
+ #
102
+ # @return [Vector4] New vector that is result of the operation.
103
+ def **(exponent)
104
+ end
105
+
106
+ ##
107
+ # @return [Float] the length of the vector.
108
+ def length
109
+ end
110
+
111
+ alias_method :magnitude, :length
112
+
113
+ ##
114
+ # @return [Float] the length of the vector squared.
115
+ def length_squared
116
+ end
117
+
118
+ ##
119
+ # @return [Boolean] flag indicating if all values of the vector are equal
120
+ # to `1.0`.
121
+ def one?
122
+ end
123
+
124
+ ##
125
+ # @return [Boolean] flag indicating if all values of the vector are equal
126
+ # to `0.0`.
127
+ def zero?
128
+ end
129
+
130
+ ##
131
+ # @return [Float] the lowest value of the vector's components.
132
+ def min_value
133
+ end
134
+
135
+ ##
136
+ # @return [Float] the greatest value of the vector's components.
137
+ def max_value
138
+ end
139
+
140
+ ##
141
+ # Returns the Euclidean distance between this vector and another.
142
+ #
143
+ # @param vector [Vector4] The point to get distance between.
144
+ #
145
+ # @return [Float] the distance.
146
+ def distance(vector)
147
+ end
148
+
149
+ # Returns the squared Euclidean distance between this vector and another.
150
+ #
151
+ # @param vector [Vector4] The point to get distance between.
152
+ #
153
+ # @return [Float] the distance squared.
154
+ def distance_squared(vector)
155
+ end
156
+
157
+ ##
158
+ # Returns a new vector with the same direction as the given vector, but with
159
+ # a length of `1.0`.
160
+ #
161
+ # @return [Vector4] a normalized vector.
162
+ def normalize
163
+ end
164
+
165
+ ##
166
+ # Alters this vector instance to maintain same direction, but adjust values
167
+ # so that vector has a length of `1.0`.
168
+ #
169
+ # @return [self]
170
+ def normalize!
171
+ end
172
+
173
+ ##
174
+ # Linearly interpolates between this vector and another based on the given
175
+ # weighting.
176
+ #
177
+ # @param vector [Vector4] The source vector to interpolate between.
178
+ # @param amount [Float] Value between `0.0` and `1.0` indicating the weight
179
+ # of the given vector.
180
+ #
181
+ # @return [Vector4] the interpolated vector.
182
+ #
183
+ # @see lerp!
184
+ def lerp(vector, amount)
185
+ end
186
+
187
+ ##
188
+ # Linearly interpolates between this vector and another based on the given
189
+ # weighting, altering the values of this vector.
190
+ #
191
+ # @param vector [Vector4] The source vector to interpolate between.
192
+ # @param amount [Float] Value between `0.0` and `1.0` indicating the weight
193
+ # of the given vector.
194
+ #
195
+ # @return [self]
196
+ #
197
+ # @see lerp
198
+ def lerp!(vector, amount)
199
+ end
200
+
201
+ ##
202
+ # Returns a new vector by applying a transformation.
203
+ #
204
+ # @overload transform(matrix)
205
+ # Transforms this vector by the given matrix.
206
+ #
207
+ # @param matrix [Matrix4x4] the transformation matrix.
208
+ #
209
+ # @overload transform(rotation)
210
+ # Transforms this vector by the specified rotation value.
211
+ #
212
+ # @param rotation [Quaternion] The rotation to apply.
213
+ #
214
+ # @return [Vector4] new transformed vector.
215
+ #
216
+ # @see transform!
217
+ def transform(other)
218
+ end
219
+
220
+ ##
221
+ # Transforms this vector.
222
+ #
223
+ # @overload transform(matrix)
224
+ # Transforms this vector by the given matrix.
225
+ #
226
+ # @param matrix [Matrix4x4] the transformation matrix.
227
+ #
228
+ # @overload transform(rotation)
229
+ # Transforms this vector by the specified rotation value.
230
+ #
231
+ # @param rotation [Quaternion] The rotation to apply.
232
+ #
233
+ # @return [self]
234
+ #
235
+ # @see transform
236
+ def transform!(other)
237
+ end
238
+
239
+ # @return [Vector4] a vector whose elements are the absolute values of each
240
+ # of this vector's elements.
241
+ def abs
242
+ end
243
+
244
+ # @return [Vector4] a vector whose elements are the square root of each of
245
+ # this vector's elements.
246
+ def sqrt
247
+ end
248
+
249
+ ##
250
+ # Returns the dot product of this vector and another.
251
+ #
252
+ # @param other [Vector4] the source vector to compute dot product of.
253
+ #
254
+ # @return [Float] the dot product.
255
+ def dot(other)
256
+ end
257
+
258
+ ##
259
+ # Returns a vector that is result of clamping this vector between the
260
+ # specified minimum and maximum values.
261
+ #
262
+ # @overload clamp(min, max)
263
+ # Clamps the vector's components between the specified values.
264
+ #
265
+ # @param min [Float] The minimum value.
266
+ # @param max [Float] The maximum value.
267
+ #
268
+ # @overload clamp(min, max)
269
+ # Clamps the vector's on a component-wise basis between the minimum and
270
+ # maximum values of the specified vectors.
271
+ #
272
+ # @param min [Vector4] The minimum value.
273
+ # @param max [Vector4] The maximum value.
274
+ #
275
+ # @return [Vector4] the result of clamping this vector.
276
+ #
277
+ # @see clamp!
278
+ def clamp(min, max)
279
+ end
280
+
281
+ ##
282
+ # Clamps this vector between the specified minimum and maximum values.
283
+ #
284
+ # @overload clamp!(min, max)
285
+ # Clamps the vector's components between the specified values.
286
+ #
287
+ # @param min [Float] The minimum value.
288
+ # @param max [Float] The maximum value.
289
+ #
290
+ # @overload clamp!(min, max)
291
+ # Clamps the vector's on a component-wise basis between the minimum and
292
+ # maximum values of the specified vectors.
293
+ #
294
+ # @param min [Vector4] The minimum value.
295
+ # @param max [Vector4] The maximum value.
296
+ #
297
+ # @return [Vector4]
298
+ #
299
+ # @see clamp
300
+ def clamp!(min, max)
301
+ end
302
+
303
+ ##
304
+ # @return [String] a String representation of this instance.
305
+ def to_s
306
+ end
307
+
308
+ ##
309
+ # @return [Array<Float>] an Array representation of this instance.
310
+ def to_a
311
+ end
312
+
313
+ alias_method :elements, :to_a
314
+
315
+ ##
316
+ # @return [Hash{Symbol => Float}] a Hash representation of this instance.
317
+ def to_h
318
+ end
319
+
320
+ ##
321
+ # @return [Quaternion] a {Quaternion} representation of this instance.
322
+ def to_quaternion
323
+ end
324
+
325
+ ##
326
+ # @return [Plane] a {Plane} representation of this instance.
327
+ def to_plane
328
+ end
329
+
330
+ ##
331
+ # @return [Vector2] a {Vector2} representation of this instance.
332
+ def to_vec2
333
+ end
334
+
335
+ ##
336
+ # @return [Vector3] a {Vector3} representation of this instance.
337
+ def to_vec3
338
+ end
339
+
340
+ ##
341
+ # Adds this vector with another.
342
+ #
343
+ # @param other [Vector4] The vector to add.
344
+ #
345
+ # @return [Vector4] the sum of the vectors.
346
+ def +(other)
347
+ end
348
+
349
+ # Gets the difference of this vector and another.
350
+ #
351
+ # @param other [Vector4] The vector to subtract.
352
+ #
353
+ # @return [Vector4] the difference of the vectors.
354
+ def -(other)
355
+ end
356
+
357
+ ##
358
+ # Vector multiplication.
359
+ #
360
+ # @overload *(scalar)
361
+ # Scalar vector multiplication.
362
+ #
363
+ # @param scalar [Float] The scalar value.
364
+ #
365
+ # @overload *(other)
366
+ # Multiplies this vector by another.
367
+ #
368
+ # @param other [Vector4] The source vector to multiply.
369
+ #
370
+ # @return [Vector4] the product vector.
371
+ def *(other)
372
+ end
373
+
374
+ ##
375
+ # Vector division.
376
+ #
377
+ # @overload *(scalar)
378
+ # Scalar vector division.
379
+ #
380
+ # @param scalar [Float] The scalar value.
381
+ #
382
+ # @overload *(other)
383
+ # Divides this vector by another.
384
+ #
385
+ # @param other [Vector4] The source vector to divide.
386
+ #
387
+ # @return [Vector4] the resulting vector.
388
+ def /(other)
389
+ end
390
+
391
+ ##
392
+ # Returns flag if this vector instance is equal to the given object.
393
+ #
394
+ # @param other [Object] The object to compare.
395
+ #
396
+ # @return [Boolean] `true` if objects are equal, otherwise `false`.
397
+ def ==(other)
398
+ end
399
+
400
+ ##
401
+ # Performs unary negation on this vector instance.
402
+ #
403
+ # @return [Vector4] the vector with swapped +/- values.
404
+ def -@
405
+ end
406
+
407
+ class << self
408
+
409
+ ##
410
+ # @return [Vector4] the vector `<0.0, 0.0, 0.0, 0.0>`.
411
+ def zero
412
+ end
413
+
414
+ ##
415
+ # @return [Vector4] the vector `<1.0, 1.0, 1.0, 1.0>`.
416
+ def one
417
+ end
418
+
419
+ ##
420
+ # @return [Vector4] the vector `<1.0, 0.0, 0.0, 0.0>`.
421
+ def unit_x
422
+ end
423
+
424
+ ##
425
+ # @return [Vector4] the vector `<0.0, 1.0, 0.0, 0.0>`.
426
+ def unit_y
427
+ end
428
+
429
+ ##
430
+ # @return [Vector4] the vector `<0.0, 0.0, 1.0, 0.0>`.
431
+ def unit_z
432
+ end
433
+
434
+ ##
435
+ # @return [Vector4] the vector `<0.0, 0.0, 0.0, 1.0>`.
436
+ def unit_w
437
+ end
438
+
439
+ ##
440
+ # Creates and returns a normalized vector from the specified components.
441
+ #
442
+ # This is more efficient than creating and then normalizing.
443
+ #
444
+ # @param x [Float] The X component of the vector.
445
+ # @param y [Float] The Y component of the vector.
446
+ # @param z [Float] The Z component of the vector.
447
+ # @param w [Float] The W component of the vector.
448
+ #
449
+ # @return [Vector4] the newly created normalized vector.
450
+ def create_norm(x, y, z, w)
451
+ end
452
+
453
+ ##
454
+ # Returns a vector that is result of clamping a vector between the
455
+ # specified minimum and maximum values.
456
+ #
457
+ # @overload clamp(min, max)
458
+ # Clamps the vector's components between the specified values.
459
+ #
460
+ # @param min [Float] The minimum value.
461
+ # @param max [Float] The maximum value.
462
+ #
463
+ # @overload clamp(min, max)
464
+ # Clamps the vector's on a component-wise basis between the minimum and
465
+ # maximum values of the specified vectors.
466
+ #
467
+ # @param min [Vector4] The minimum value.
468
+ # @param max [Vector4] The maximum value.
469
+ #
470
+ # @return [Vector4] the result of clamping this vector.
471
+ def clamp(vector, min, max)
472
+ end
473
+
474
+ ##
475
+ # Linearly interpolates between two vectors based on the given weighting.
476
+ #
477
+ # @param vector1 [Vector4] The first source vector.
478
+ # @param vector2 [Vector4] The second source vector.
479
+ # @param amount [Float] Value between `0.0` and `1.0` indicating the
480
+ # weight of the second source vector.
481
+ #
482
+ # @return [Vector4] the interpolated vector.
483
+ def lerp(vector1, vector2, amount)
484
+ end
485
+
486
+ ##
487
+ # Creates a Vector4 by transforming the specified vector and
488
+ # transformation matrix or quaternion rotation.
489
+ #
490
+ # @overload transform(vector, matrix)
491
+ # Transforms a vector by the given matrix.
492
+ #
493
+ # @param vector [Vector2, Vector3, Vector4] The source vector.
494
+ # @param matrix [Matrix4x4] The transformation matrix.
495
+ #
496
+ # @return [Vector4] A transformed vector.
497
+ #
498
+ # @overload transform(vector, rotation)
499
+ # Transforms a vector by the given Quaternion rotation value.
500
+ #
501
+ # @param vector [Vector2, Vector3, Vector4] The source vector to rotate.
502
+ # @param rotation [Quaternion] The rotation to apply.
503
+ #
504
+ # @return [Vector4] A rotated vector.
505
+ def transform(vector, other)
506
+ end
507
+
508
+ ##
509
+ # Returns a vector with a minimum set of values.
510
+ #
511
+ # @overload min(vector, other)
512
+ # Returns a vector whose elements are the minimum of each of the pairs
513
+ # of elements in the two source vectors.
514
+ #
515
+ # @param vector [Vector4] The first source vector.
516
+ # @param other [Vector4] The second source vector.
517
+ #
518
+ # @overload min(vector, value)
519
+ # Returns a vector whose elements are the minimum of each of vector
520
+ # element and the specified value.
521
+ #
522
+ # @param vector [Vector4] The source vector.
523
+ # @param value [Float] The minimum value.
524
+ #
525
+ # @return [Vector4] the minimized vector.
526
+ def min(vector, other)
527
+ end
528
+
529
+ ##
530
+ # Returns a vector with a maximum set of values.
531
+ #
532
+ # @overload max(vector, other)
533
+ # Returns a vector whose elements are the maximum of each of the pairs
534
+ # of elements in the two source vectors.
535
+ #
536
+ # @param vector [Vector4] The first source vector.
537
+ # @param other [Vector4] The second source vector.
538
+ #
539
+ # @overload max(vector, value)
540
+ # Returns a vector whose elements are the maximum of each of vector
541
+ # element and the specified value.
542
+ #
543
+ # @param vector [Vector4] The source vector.
544
+ # @param value [Float] The maximum value.
545
+ #
546
+ # @return [Vector4] the maximized vector.
547
+ def max(vector, other)
548
+ end
549
+ end
550
+ end
551
+ end
@@ -0,0 +1,14 @@
1
+ module Numerix
2
+
3
+ ##
4
+ # @abstract Abstract base class for Vector objects.
5
+ #
6
+ # This class cannot be instantiated, it of only for providing a common base
7
+ # class for vector types.
8
+ class VectorBase < Structure
9
+
10
+ private_class_method :new
11
+
12
+ include Enumerable
13
+ end
14
+ end
@@ -0,0 +1,6 @@
1
+ module Numerix
2
+
3
+ ##
4
+ # Current gem version of Numerix
5
+ VERSION = "1.0.0"
6
+ end
data/lib/numerix.rb ADDED
@@ -0,0 +1,10 @@
1
+ require_relative 'numerix/version'
2
+ require_relative 'numerix/numerix'
3
+
4
+ ##
5
+ # Top-level namespace for the Numerix gem.
6
+ #
7
+ # @author Eric "ForeverZer0" Freed
8
+ module Numerix
9
+
10
+ end
data/numerix.gemspec ADDED
@@ -0,0 +1,30 @@
1
+
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "numerix/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "numerix"
8
+ spec.version = Numerix::VERSION
9
+ spec.authors = ["Eric Freed"]
10
+ spec.email = ["efreed09@gmail.com"]
11
+
12
+ spec.summary = %q{High performance vector and matrix C extension for fast vector math and simplified interop.}
13
+ spec.description = %q{Numerix strives to make working with vectors more "Ruby-like", and far exceeds Ruby's built-in implementations many times over in regards to speed and performance. Great care has been take to make the library "interop" friendly, where each class can easily be passed as a "pointer" or binary string for interop with native libraries, using Ruby's Fiddle, FFI, or even Ruby's legacy Win32API. Numerix has been built from the ground-up for Ruby, not playing middle-man between Ruby and an existing library, and is optimized specifically for it, with focus on speed and a robust collection of functionality. }
14
+ spec.homepage = "https://github.com/ForeverZer0/numerix"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
18
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
19
+ end
20
+
21
+ spec.bindir = "exe"
22
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
23
+ spec.require_paths = ["lib"]
24
+ spec.extensions = ["ext/numerix/extconf.rb"]
25
+
26
+ spec.add_development_dependency "bundler", "~> 1.16"
27
+ spec.add_development_dependency "rake", "~> 10.0"
28
+ spec.add_development_dependency "rake-compiler", '~> 0'
29
+ spec.add_development_dependency "minitest", "~> 5.0"
30
+ end