snow-math 1.1.0 → 1.2.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.
- checksums.yaml +4 -4
- data/README.md +128 -19
- data/ext/extconf.rb +0 -0
- data/ext/snow-math/mat3.c +455 -0
- data/ext/snow-math/mat4.c +105 -51
- data/ext/snow-math/maths_local.h +57 -14
- data/ext/snow-math/quat.c +66 -4
- data/ext/snow-math/snow-math.c +484 -7
- data/ext/snow-math/vec3.c +13 -1
- data/ext/snow-math/vec4.c +13 -1
- data/lib/snow-math.rb +1 -0
- data/lib/snow-math/inspect.rb +25 -10
- data/lib/snow-math/mat3.rb +71 -0
- data/lib/snow-math/ptr.rb +12 -26
- data/lib/snow-math/quat.rb +1 -1
- data/lib/snow-math/swizzle.rb +40 -35
- data/lib/snow-math/to_a.rb +40 -43
- metadata +3 -2
data/ext/snow-math/vec3.c
CHANGED
@@ -45,7 +45,7 @@ s_float_t vec3_length_squared(const vec3_t v)
|
|
45
45
|
*/
|
46
46
|
s_float_t vec3_length(const vec3_t v)
|
47
47
|
{
|
48
|
-
return
|
48
|
+
return s_sqrt((v[0] * v[0]) + (v[1] * v[1]) + (v[2] * v[2]));
|
49
49
|
}
|
50
50
|
|
51
51
|
void vec3_normalize(const vec3_t in, vec3_t out)
|
@@ -92,6 +92,18 @@ void vec3_inverse(const vec3_t v, vec3_t out)
|
|
92
92
|
out[0] = (v[0] != 0.0 && v[0] != -0.0) ? (1.0 / v[0]) : v[0];
|
93
93
|
}
|
94
94
|
|
95
|
+
void vec3_project(const vec3_t in, const vec3_t normal, vec3_t out)
|
96
|
+
{
|
97
|
+
vec3_scale(normal, vec3_dot_product(in, normal), out);
|
98
|
+
}
|
99
|
+
|
100
|
+
void vec3_reflect(const vec3_t in, const vec3_t normal, vec3_t out)
|
101
|
+
{
|
102
|
+
vec3_t temp;
|
103
|
+
vec3_scale(normal, 2 * vec3_dot_product(in, normal), temp);
|
104
|
+
vec3_subtract(in, temp, out);
|
105
|
+
}
|
106
|
+
|
95
107
|
void vec3_cross_product(const vec3_t left, const vec3_t right, vec3_t out)
|
96
108
|
{
|
97
109
|
s_float_t x, y, z;
|
data/ext/snow-math/vec4.c
CHANGED
@@ -48,7 +48,7 @@ s_float_t vec4_length_squared(const vec4_t v)
|
|
48
48
|
*/
|
49
49
|
s_float_t vec4_length(const vec4_t v)
|
50
50
|
{
|
51
|
-
return
|
51
|
+
return s_sqrt((v[0] * v[0]) + (v[1] * v[1]) + (v[2] * v[2]) + (v[3] * v[3]));
|
52
52
|
}
|
53
53
|
|
54
54
|
void vec4_normalize(const vec4_t in, vec4_t out)
|
@@ -101,6 +101,18 @@ void vec4_inverse(const vec4_t v, vec4_t out)
|
|
101
101
|
out[0] = (v[0] != 0.0 && v[0] != -0.0) ? (1.0 / v[0]) : v[0];
|
102
102
|
}
|
103
103
|
|
104
|
+
void vec4_project(const vec4_t in, const vec4_t normal, vec4_t out)
|
105
|
+
{
|
106
|
+
vec4_scale(normal, vec4_dot_product(in, normal), out);
|
107
|
+
}
|
108
|
+
|
109
|
+
void vec4_reflect(const vec4_t in, const vec4_t normal, vec4_t out)
|
110
|
+
{
|
111
|
+
vec4_t temp;
|
112
|
+
vec4_scale(normal, 2 * vec4_dot_product(in, normal), temp);
|
113
|
+
vec4_subtract(in, temp, out);
|
114
|
+
}
|
115
|
+
|
104
116
|
s_float_t vec4_dot_product(const vec4_t left, const vec4_t right)
|
105
117
|
{
|
106
118
|
return ((left[0] * right[0]) + (left[1] * right[1]) + (left[2] * right[2]) + (left[3] * right[3]));
|
data/lib/snow-math.rb
CHANGED
data/lib/snow-math/inspect.rb
CHANGED
@@ -4,17 +4,32 @@
|
|
4
4
|
|
5
5
|
require 'snow-math'
|
6
6
|
|
7
|
-
module Snow
|
7
|
+
module Snow
|
8
8
|
|
9
|
-
|
9
|
+
[:Vec3, :Vec4, :Quat, :Mat3, :Mat4].each {
|
10
|
+
|klass_sym|
|
11
|
+
if const_defined?(klass_sym)
|
12
|
+
const_get(klass_sym).class_exec {
|
10
13
|
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
+
def inspect
|
15
|
+
"<#{self.class.name}:0x#{self.address.to_s(16)} #{(0 ... self.length).map { |i| self.fetch(i) }.join(' ')}>"
|
16
|
+
end
|
14
17
|
|
15
|
-
|
18
|
+
}
|
19
|
+
end
|
20
|
+
}
|
21
|
+
|
22
|
+
[:Vec3Array, :Vec4Array, :QuatArray, :Mat3Array, :Mat4Array].each {
|
23
|
+
|klass_sym|
|
24
|
+
if const_defined?(klass_sym)
|
25
|
+
const_get(klass_sym).class_exec {
|
26
|
+
|
27
|
+
def inspect
|
28
|
+
"<#{self.class.name}:0x#{self.address.to_s(16)} #{(0 ... self.length).map { |i| self.fetch(i).inspect }.join(' ')}>"
|
29
|
+
end
|
16
30
|
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
31
|
+
}
|
32
|
+
end
|
33
|
+
}
|
34
|
+
|
35
|
+
end
|
data/lib/snow-math/mat3.rb
CHANGED
@@ -0,0 +1,71 @@
|
|
1
|
+
# This file is part of ruby-snowmath.
|
2
|
+
# Copyright (c) 2013 Noel Raymond Cower. All rights reserved.
|
3
|
+
# See COPYING for license details.
|
4
|
+
|
5
|
+
require 'snow-math/bindings'
|
6
|
+
|
7
|
+
module Snow ; end
|
8
|
+
|
9
|
+
if Snow.const_defined?(:Mat4Array)
|
10
|
+
class Snow::Mat3Array
|
11
|
+
class << self ; alias_method :[], :new ; end
|
12
|
+
|
13
|
+
alias_method :[], :fetch
|
14
|
+
alias_method :[]=, :store
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
class Snow::Mat3
|
19
|
+
|
20
|
+
class << self ; alias_method :[], :new ; end
|
21
|
+
|
22
|
+
alias_method :[], :fetch
|
23
|
+
alias_method :[]=, :store
|
24
|
+
|
25
|
+
def transpose!
|
26
|
+
transpose self
|
27
|
+
end
|
28
|
+
|
29
|
+
def inverse!
|
30
|
+
inverse self
|
31
|
+
end
|
32
|
+
|
33
|
+
def adjoint!
|
34
|
+
adjoint self
|
35
|
+
end
|
36
|
+
|
37
|
+
def cofactor!
|
38
|
+
cofactor self
|
39
|
+
end
|
40
|
+
|
41
|
+
def multiply_mat3!(rhs)
|
42
|
+
multiply_mat3 rhs, self
|
43
|
+
end
|
44
|
+
|
45
|
+
def multiply(rhs, out = nil)
|
46
|
+
raise "Invalid type for output, must be the same as RHS" if !out.nil? && !out.kind_of?(rhs.class)
|
47
|
+
case rhs
|
48
|
+
when ::Snow::Mat3 then multiply_mat3(rhs, out)
|
49
|
+
when ::Snow::Vec3 then rotate_vec3(rhs, out)
|
50
|
+
when Numeric then scale(rhs, rhs, rhs, out)
|
51
|
+
else raise TypeError, "Invalid type for RHS"
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
def multiply!(rhs)
|
56
|
+
multiply rhs, case rhs
|
57
|
+
when Mat3, Numeric then self
|
58
|
+
when Vec3 then rhs
|
59
|
+
else raise TypeError, "Invalid type for RHS"
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
def scale!(x, y, z)
|
64
|
+
scale x, y, z, self
|
65
|
+
end
|
66
|
+
|
67
|
+
alias_method :*, :multiply
|
68
|
+
alias_method :**, :scale
|
69
|
+
alias_method :~, :transpose
|
70
|
+
|
71
|
+
end
|
data/lib/snow-math/ptr.rb
CHANGED
@@ -5,34 +5,20 @@
|
|
5
5
|
require 'snow-math'
|
6
6
|
require 'fiddle'
|
7
7
|
|
8
|
-
module Snow
|
8
|
+
module Snow
|
9
9
|
|
10
|
-
|
10
|
+
[:Vec3, :Vec4, :Quat, :Mat3, :Mat4,
|
11
|
+
:Vec3Array, :Vec4Array, :QuatArray, :Mat3Array, :Mat4Array].each {
|
12
|
+
|klass_sym|
|
13
|
+
if const_defined?(klass_sym)
|
14
|
+
const_get(klass_sym).class_exec {
|
11
15
|
|
12
|
-
|
13
|
-
|
14
|
-
|
16
|
+
def to_ptr
|
17
|
+
Fiddle::Pointer.new(self.address, self.size)
|
18
|
+
end
|
15
19
|
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
class Snow::Vec4 ; include Snow::MathPointers ; end
|
20
|
-
class Snow::Quat ; include Snow::MathPointers ; end
|
21
|
-
class Snow::Mat4 ; include Snow::MathPointers ; end
|
20
|
+
}
|
21
|
+
end
|
22
|
+
}
|
22
23
|
|
23
|
-
if Snow.const_defined?(:Vec3Array)
|
24
|
-
class Snow::Vec3Array ; include Snow::MathPointers ; end
|
25
24
|
end
|
26
|
-
|
27
|
-
if Snow.const_defined?(:Vec4Array)
|
28
|
-
class Snow::Vec4Array ; include Snow::MathPointers ; end
|
29
|
-
end
|
30
|
-
|
31
|
-
if Snow.const_defined?(:QuatArray)
|
32
|
-
class Snow::QuatArray ; include Snow::MathPointers ; end
|
33
|
-
end
|
34
|
-
|
35
|
-
if Snow.const_defined?(:Mat4Array)
|
36
|
-
class Snow::Mat4Array ; include Snow::MathPointers ; end
|
37
|
-
end
|
38
|
-
|
data/lib/snow-math/quat.rb
CHANGED
@@ -106,7 +106,7 @@ class Snow::Quat
|
|
106
106
|
alias_method :-, :subtract
|
107
107
|
alias_method :+, :add
|
108
108
|
alias_method :**, :dot_product
|
109
|
-
alias_method :*, :
|
109
|
+
alias_method :*, :multiply
|
110
110
|
alias_method :/, :divide
|
111
111
|
alias_method :-@, :negate
|
112
112
|
alias_method :~, :inverse
|
data/lib/snow-math/swizzle.rb
CHANGED
@@ -6,52 +6,57 @@ require 'snow-math'
|
|
6
6
|
|
7
7
|
module Snow ; end
|
8
8
|
|
9
|
-
module Snow::Swizzle
|
10
|
-
|
11
|
-
def method_missing(sym, *args)
|
12
|
-
chars = sym.to_s
|
13
|
-
if chars =~ self.class.class_variable_get(:@@SWIZZLE_CHARS)
|
14
|
-
mapping = self.class.class_variable_get(:@@SWIZZLE_MAPPING)
|
15
|
-
arg_indices = chars.each_char.map {
|
16
|
-
|char|
|
17
|
-
index = mapping[char]
|
18
|
-
if index.nil?
|
19
|
-
raise ArgumentError, "No index mapping for swizzle character #{char} found"
|
20
|
-
end
|
21
|
-
index
|
22
|
-
}
|
23
|
-
swizzle_klass = mapping[chars.length]
|
24
|
-
|
25
|
-
if swizzle_klass.nil?
|
26
|
-
raise ArgumentError, "No swizzle class defined for #{chars.length} components"
|
27
|
-
end
|
28
|
-
|
29
|
-
self.class.class_exec(arg_indices, swizzle_klass) {
|
30
|
-
|indices, klass|
|
31
|
-
define_method(sym) { klass.new(indices.map { |index| self.fetch(index) }) }
|
32
|
-
}
|
33
|
-
return self.send(sym)
|
34
|
-
end
|
35
|
-
|
36
|
-
super sym, *args
|
37
|
-
end
|
38
|
-
|
39
|
-
end
|
40
|
-
|
41
9
|
class Snow::Vec3
|
42
10
|
@@SWIZZLE_CHARS = /^[xyz]{3,4}$/
|
43
11
|
@@SWIZZLE_MAPPING = { 3 => self, 4 => ::Snow::Vec4, 'x' => 0, 'y' => 1, 'z' => 2 }
|
44
|
-
include ::Snow::Swizzle
|
45
12
|
end
|
46
13
|
|
47
14
|
class Snow::Vec4
|
48
15
|
@@SWIZZLE_CHARS = /^[xyzw]{3,4}$/
|
49
16
|
@@SWIZZLE_MAPPING = { 3 => ::Snow::Vec3, 4 => self, 'x' => 0, 'y' => 1, 'z' => 2, 'w' => 3 }
|
50
|
-
include ::Snow::Swizzle
|
51
17
|
end
|
52
18
|
|
53
19
|
class Snow::Quat
|
54
20
|
@@SWIZZLE_CHARS = /^[xyzw]{3,4}$/
|
55
21
|
@@SWIZZLE_MAPPING = { 3 => ::Snow::Vec3, 4 => self, 'x' => 0, 'y' => 1, 'z' => 2, 'w' => 3 }
|
56
|
-
|
22
|
+
end
|
23
|
+
|
24
|
+
module Snow
|
25
|
+
|
26
|
+
[:Vec3, :Vec4, :Quat].each {
|
27
|
+
|klass_sym|
|
28
|
+
|
29
|
+
const_get(klass_sym).class_exec {
|
30
|
+
|
31
|
+
def method_missing(sym, *args)
|
32
|
+
chars = sym.to_s
|
33
|
+
if chars =~ self.class.class_variable_get(:@@SWIZZLE_CHARS)
|
34
|
+
mapping = self.class.class_variable_get(:@@SWIZZLE_MAPPING)
|
35
|
+
arg_indices = chars.each_char.map {
|
36
|
+
|char|
|
37
|
+
index = mapping[char]
|
38
|
+
if index.nil?
|
39
|
+
raise ArgumentError, "No index mapping for swizzle character #{char} found"
|
40
|
+
end
|
41
|
+
index
|
42
|
+
}
|
43
|
+
swizzle_klass = mapping[chars.length]
|
44
|
+
|
45
|
+
if swizzle_klass.nil?
|
46
|
+
raise ArgumentError, "No swizzle class defined for #{chars.length} components"
|
47
|
+
end
|
48
|
+
|
49
|
+
self.class.class_exec(arg_indices, swizzle_klass) {
|
50
|
+
|indices, klass|
|
51
|
+
define_method(sym) { klass.new(indices.map { |index| self.fetch(index) }) }
|
52
|
+
}
|
53
|
+
return self.send(sym)
|
54
|
+
end
|
55
|
+
|
56
|
+
super sym, *args
|
57
|
+
end
|
58
|
+
|
59
|
+
}
|
60
|
+
}
|
61
|
+
|
57
62
|
end
|
data/lib/snow-math/to_a.rb
CHANGED
@@ -4,48 +4,45 @@
|
|
4
4
|
|
5
5
|
require 'snow-math'
|
6
6
|
|
7
|
-
module Snow
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
7
|
+
module Snow
|
8
|
+
|
9
|
+
[:Vec3, :Vec4, :Quat, :Mat3, :Mat4,
|
10
|
+
:Vec3Array, :Vec4Array, :QuatArray, :Mat3Array, :Mat4Array].each {
|
11
|
+
|klass_sym|
|
12
|
+
if const_defined?(klass_sym)
|
13
|
+
const_get(klass_sym).class_exec {
|
14
|
+
|
15
|
+
include ::Enumerable
|
16
|
+
|
17
|
+
def to_a
|
18
|
+
(0 ... self.length).each.map { |index| fetch(index) }
|
19
|
+
end
|
20
|
+
|
21
|
+
def each(&block)
|
22
|
+
return to_enum(:each) unless block_given?
|
23
|
+
(0 ... self.length).each {
|
24
|
+
|index|
|
25
|
+
yield(fetch(index))
|
26
|
+
}
|
27
|
+
self
|
28
|
+
end
|
29
|
+
|
30
|
+
def map!(&block)
|
31
|
+
return to_enum(:map!) unless block_given?
|
32
|
+
(0 ... self.length).each {
|
33
|
+
|index|
|
34
|
+
store(index, yield(fetch(index)))
|
35
|
+
}
|
36
|
+
self
|
37
|
+
end
|
38
|
+
|
39
|
+
def map(&block)
|
40
|
+
return to_enum(:map) unless block_given?
|
41
|
+
self.dup.map!(&block)
|
42
|
+
end
|
43
|
+
|
44
|
+
}
|
45
|
+
end
|
46
|
+
}
|
38
47
|
|
39
48
|
end
|
40
|
-
|
41
|
-
if Snow.const_defined?(:Vec3Array)
|
42
|
-
class Snow::Vec3Array ; include Snow::EnumeratorSupport ; end
|
43
|
-
class Snow::Vec4Array ; include Snow::EnumeratorSupport ; end
|
44
|
-
class Snow::QuatArray ; include Snow::EnumeratorSupport ; end
|
45
|
-
class Snow::Mat4Array ; include Snow::EnumeratorSupport ; end
|
46
|
-
end
|
47
|
-
|
48
|
-
class Snow::Vec3 ; include Snow::EnumeratorSupport ; end
|
49
|
-
class Snow::Vec4 ; include Snow::EnumeratorSupport ; end
|
50
|
-
class Snow::Quat ; include Snow::EnumeratorSupport ; end
|
51
|
-
class Snow::Mat4 ; include Snow::EnumeratorSupport ; end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: snow-math
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.1
|
4
|
+
version: 1.2.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Noel Raymond Cower
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2013-06
|
11
|
+
date: 2013-07-06 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: Math types built on the SnowPalm math code
|
14
14
|
email: ncower@gmail.com
|
@@ -33,6 +33,7 @@ files:
|
|
33
33
|
- lib/snow-math/vec3.rb
|
34
34
|
- lib/snow-math/vec4.rb
|
35
35
|
- lib/snow-math.rb
|
36
|
+
- ext/snow-math/mat3.c
|
36
37
|
- ext/snow-math/mat4.c
|
37
38
|
- ext/snow-math/maths.c
|
38
39
|
- ext/snow-math/quat.c
|