snow-math 1.2.4 → 1.3.0pre0
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 +47 -569
- data/ext/snow-math/mat3.c +1 -1
- data/ext/snow-math/quat.c +3 -3
- data/ext/snow-math/snow-math.c +3794 -709
- data/ext/snow-math/vec3.c +3 -3
- data/ext/snow-math/vec4.c +4 -4
- data/lib/snow-math.rb +12 -2
- data/lib/snow-math/inspect.rb +48 -20
- data/lib/snow-math/marshal.rb +75 -0
- data/lib/snow-math/mat3.rb +66 -1
- data/lib/snow-math/mat4.rb +82 -0
- data/lib/snow-math/ptr.rb +66 -10
- data/lib/snow-math/quat.rb +91 -5
- data/lib/snow-math/swizzle.rb +77 -40
- data/lib/snow-math/to_a.rb +178 -35
- data/lib/snow-math/vec3.rb +67 -4
- data/lib/snow-math/vec4.rb +70 -4
- metadata +7 -5
- data/lib/snow-math/array_cache.rb +0 -42
data/ext/snow-math/vec3.c
CHANGED
@@ -87,9 +87,9 @@ void vec3_negate(const vec3_t v, vec3_t out)
|
|
87
87
|
|
88
88
|
void vec3_inverse(const vec3_t v, vec3_t out)
|
89
89
|
{
|
90
|
-
out[2] = (
|
91
|
-
out[1] = (
|
92
|
-
out[0] = (
|
90
|
+
out[2] = (!float_is_zero(v[2])) ? (s_float_lit(1.0) / v[2]) : v[2];
|
91
|
+
out[1] = (!float_is_zero(v[1])) ? (s_float_lit(1.0) / v[1]) : v[1];
|
92
|
+
out[0] = (!float_is_zero(v[0])) ? (s_float_lit(1.0) / v[0]) : v[0];
|
93
93
|
}
|
94
94
|
|
95
95
|
void vec3_project(const vec3_t in, const vec3_t normal, vec3_t out)
|
data/ext/snow-math/vec4.c
CHANGED
@@ -95,10 +95,10 @@ void vec4_negate(const vec4_t v, vec4_t out)
|
|
95
95
|
|
96
96
|
void vec4_inverse(const vec4_t v, vec4_t out)
|
97
97
|
{
|
98
|
-
out[3] = (
|
99
|
-
out[2] = (
|
100
|
-
out[1] = (
|
101
|
-
out[0] = (
|
98
|
+
out[3] = (!float_is_zero(v[3])) ? (s_float_lit(1.0) / v[3]) : v[3];
|
99
|
+
out[2] = (!float_is_zero(v[2])) ? (s_float_lit(1.0) / v[2]) : v[2];
|
100
|
+
out[1] = (!float_is_zero(v[1])) ? (s_float_lit(1.0) / v[1]) : v[1];
|
101
|
+
out[0] = (!float_is_zero(v[0])) ? (s_float_lit(1.0) / v[0]) : v[0];
|
102
102
|
}
|
103
103
|
|
104
104
|
void vec4_project(const vec4_t in, const vec4_t normal, vec4_t out)
|
data/lib/snow-math.rb
CHANGED
@@ -11,6 +11,16 @@ require 'snow-math/quat'
|
|
11
11
|
require 'snow-math/swizzle'
|
12
12
|
require 'snow-math/inspect'
|
13
13
|
require 'snow-math/to_a'
|
14
|
-
require 'snow-math/
|
14
|
+
require 'snow-math/marshal'
|
15
15
|
|
16
|
-
|
16
|
+
#
|
17
|
+
# Snow module -- covers Snow-related classes and methods.
|
18
|
+
#
|
19
|
+
module Snow
|
20
|
+
|
21
|
+
#
|
22
|
+
# snow-math bindings version string.
|
23
|
+
#
|
24
|
+
SNOW_MATH_VERSION = '1.3.0pre0'
|
25
|
+
|
26
|
+
end
|
data/lib/snow-math/inspect.rb
CHANGED
@@ -6,30 +6,58 @@ require 'snow-math'
|
|
6
6
|
|
7
7
|
module Snow
|
8
8
|
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
9
|
+
#
|
10
|
+
# Provides support for inspect calls on Snow math types.
|
11
|
+
#
|
12
|
+
module InspectSupport
|
13
|
+
|
14
|
+
#
|
15
|
+
# Returns a serializable string for a given type.
|
16
|
+
#
|
17
|
+
# Output is defined as `<Type:Address Elements>`. For example:
|
18
|
+
#
|
19
|
+
# Vec4[1, 2, 3, 4] # => <Snow::Vec4:0x7f831981f5e0 1.0 2.0 3.0 4.0>
|
20
|
+
#
|
21
|
+
# # The following example inner Vec3's elements have been made 0.0 even
|
22
|
+
# # though they would ordinarily be garbage values.
|
23
|
+
# Vec3Array[1] # => <Snow::Vec3Array:0x7fe193b297a0 <Snow::Vec3:0x7fe193b297a0 0.0 0.0 0.0>>
|
24
|
+
#
|
25
|
+
# Bear in mind that the address may not be useful for serialization beyond
|
26
|
+
# determining when a given object has already been deserialized. You may
|
27
|
+
# want to treat it as semi-unique ID if serializing a math object, though
|
28
|
+
# there is absolutely no guarantee it will be unique between allocations and
|
29
|
+
# deallocations, as memory may obviously be reused.
|
30
|
+
#
|
31
|
+
def inspect
|
32
|
+
"<#{self.class.name}:0x#{self.address.to_s(16)} #{(0 ... self.length).map { |i| self.fetch(i).inspect }.join(' ')}>"
|
33
|
+
end
|
13
34
|
|
14
|
-
|
15
|
-
"<#{self.class.name}:0x#{self.address.to_s(16)} #{(0 ... self.length).map { |i| self.fetch(i) }.join(' ')}>"
|
16
|
-
end
|
35
|
+
end
|
17
36
|
|
18
|
-
|
19
|
-
|
20
|
-
|
37
|
+
class Vec3 ; include ::Snow::InspectSupport ; end
|
38
|
+
class Vec4 ; include ::Snow::InspectSupport ; end
|
39
|
+
class Quat ; include ::Snow::InspectSupport ; end
|
40
|
+
class Mat3 ; include ::Snow::InspectSupport ; end
|
41
|
+
class Mat4 ; include ::Snow::InspectSupport ; end
|
21
42
|
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
const_get(klass_sym).class_exec {
|
43
|
+
if const_defined?(:Vec3Array)
|
44
|
+
class Vec3Array ; include ::Snow::InspectSupport ; end
|
45
|
+
end
|
26
46
|
|
27
|
-
|
28
|
-
|
29
|
-
|
47
|
+
if const_defined?(:Vec4Array)
|
48
|
+
class Vec4Array ; include ::Snow::InspectSupport ; end
|
49
|
+
end
|
30
50
|
|
31
|
-
|
32
|
-
end
|
33
|
-
|
51
|
+
if const_defined?(:QuatArray)
|
52
|
+
class QuatArray ; include ::Snow::InspectSupport ; end
|
53
|
+
end
|
54
|
+
|
55
|
+
if const_defined?(:Mat3Array)
|
56
|
+
class Mat3Array ; include ::Snow::InspectSupport ; end
|
57
|
+
end
|
58
|
+
|
59
|
+
if const_defined?(:Mat4Array)
|
60
|
+
class Mat4Array ; include ::Snow::InspectSupport ; end
|
61
|
+
end
|
34
62
|
|
35
63
|
end
|
@@ -0,0 +1,75 @@
|
|
1
|
+
require 'snow-math/bindings'
|
2
|
+
|
3
|
+
module Snow ; end
|
4
|
+
|
5
|
+
module Snow::BaseMarshalSupport # :nodoc: all
|
6
|
+
|
7
|
+
#
|
8
|
+
def _dump(level)
|
9
|
+
# level ignored because it's not applicable
|
10
|
+
Marshal.dump(self.to_a)
|
11
|
+
end
|
12
|
+
|
13
|
+
module MarshalLoadSupport
|
14
|
+
def _load(args)
|
15
|
+
new(*Marshal.load(args))
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
def self.included(base)
|
20
|
+
base.extend(MarshalLoadSupport)
|
21
|
+
end
|
22
|
+
|
23
|
+
end
|
24
|
+
|
25
|
+
module Snow::ArrayMarshalSupport # :nodoc: all
|
26
|
+
|
27
|
+
def _dump(level)
|
28
|
+
to_dump = [self.length, *self.to_a.map { |elem| elem.copy }]
|
29
|
+
Marshal.dump(to_dump)
|
30
|
+
end
|
31
|
+
|
32
|
+
module MarshalLoadSupport
|
33
|
+
def _load(args)
|
34
|
+
info = Marshal.load(args)
|
35
|
+
arr = new(info[0])
|
36
|
+
# if not equal, then either something is corrupt or depth was 0
|
37
|
+
(1 ... info.length).each { |index| arr.store(index - 1, info[index]) }
|
38
|
+
arr
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
def self.included(base)
|
43
|
+
base.extend(MarshalLoadSupport)
|
44
|
+
end
|
45
|
+
|
46
|
+
end
|
47
|
+
|
48
|
+
module Snow
|
49
|
+
class Vec3 ; include ::Snow::BaseMarshalSupport ; end
|
50
|
+
class Vec4 ; include ::Snow::BaseMarshalSupport ; end
|
51
|
+
class Quat ; include ::Snow::BaseMarshalSupport ; end
|
52
|
+
class Mat3 ; include ::Snow::BaseMarshalSupport ; end
|
53
|
+
class Mat4 ; include ::Snow::BaseMarshalSupport ; end
|
54
|
+
|
55
|
+
if const_defined?(:Vec3Array)
|
56
|
+
class Vec3Array ; include ::Snow::ArrayMarshalSupport ; end
|
57
|
+
end
|
58
|
+
|
59
|
+
if const_defined?(:Vec4Array)
|
60
|
+
class Vec4Array ; include ::Snow::ArrayMarshalSupport ; end
|
61
|
+
end
|
62
|
+
|
63
|
+
if const_defined?(:QuatArray)
|
64
|
+
class QuatArray ; include ::Snow::ArrayMarshalSupport ; end
|
65
|
+
end
|
66
|
+
|
67
|
+
if const_defined?(:Mat3Array)
|
68
|
+
class Mat3Array ; include ::Snow::ArrayMarshalSupport ; end
|
69
|
+
end
|
70
|
+
|
71
|
+
if const_defined?(:Mat4Array)
|
72
|
+
class Mat4Array ; include ::Snow::ArrayMarshalSupport ; end
|
73
|
+
end
|
74
|
+
|
75
|
+
end
|
data/lib/snow-math/mat3.rb
CHANGED
@@ -6,7 +6,15 @@ require 'snow-math/bindings'
|
|
6
6
|
|
7
7
|
module Snow ; end
|
8
8
|
|
9
|
-
if Snow.const_defined?(:
|
9
|
+
if Snow.const_defined?(:Mat3Array)
|
10
|
+
#
|
11
|
+
# A contiguous array of Mat3s. Allocated as a single block of memory so that
|
12
|
+
# it can easily be passed back to C libraries (like OpenGL) and to aid with
|
13
|
+
# cache locality.
|
14
|
+
#
|
15
|
+
# May be useful when subclassed as a stack to recreate now-drepcated OpenGL
|
16
|
+
# functionality, though perhaps less-so than a Mat4 stack.
|
17
|
+
#
|
10
18
|
class Snow::Mat3Array
|
11
19
|
class << self ; alias_method :[], :new ; end
|
12
20
|
|
@@ -15,33 +23,77 @@ if Snow.const_defined?(:Mat4Array)
|
|
15
23
|
end
|
16
24
|
end
|
17
25
|
|
26
|
+
#
|
27
|
+
# A 3x3 matrix class. Often useful for representation rotations.
|
28
|
+
#
|
18
29
|
class Snow::Mat3
|
19
30
|
|
20
31
|
class << self ; alias_method :[], :new ; end
|
21
32
|
|
22
33
|
alias_method :[], :fetch
|
23
34
|
alias_method :[]=, :store
|
35
|
+
alias_method :dup, :copy
|
36
|
+
alias_method :clone, :copy
|
24
37
|
|
38
|
+
|
39
|
+
#
|
40
|
+
# Calls #transpose(self)
|
41
|
+
#
|
42
|
+
# call-seq: transpose! -> self
|
43
|
+
#
|
25
44
|
def transpose!
|
26
45
|
transpose self
|
27
46
|
end
|
28
47
|
|
48
|
+
#
|
49
|
+
# Calls #inverse(self)
|
50
|
+
#
|
51
|
+
# call-seq: inverse! -> self
|
52
|
+
#
|
29
53
|
def inverse!
|
30
54
|
inverse self
|
31
55
|
end
|
32
56
|
|
57
|
+
#
|
58
|
+
# Calls #adjoint(self)
|
59
|
+
#
|
60
|
+
# call-seq: adjoint! -> self
|
61
|
+
#
|
33
62
|
def adjoint!
|
34
63
|
adjoint self
|
35
64
|
end
|
36
65
|
|
66
|
+
#
|
67
|
+
# Calls #cofactor(self)
|
68
|
+
#
|
69
|
+
# call-seq: cofactor! -> self
|
70
|
+
#
|
37
71
|
def cofactor!
|
38
72
|
cofactor self
|
39
73
|
end
|
40
74
|
|
75
|
+
#
|
76
|
+
# Calls #multiply_mat3(rhs, self)
|
77
|
+
#
|
78
|
+
# call-seq: multiply_mat3!(rhs) -> self
|
79
|
+
#
|
41
80
|
def multiply_mat3!(rhs)
|
42
81
|
multiply_mat3 rhs, self
|
43
82
|
end
|
44
83
|
|
84
|
+
#
|
85
|
+
# Multiplies self and RHS and returns the result. This is a wrapper around
|
86
|
+
# other multiply methods. See multiply_mat3, rotate_vec3, and #scale for more
|
87
|
+
# reference.
|
88
|
+
#
|
89
|
+
# In the third form, the scalar value provided is passed for all three columns
|
90
|
+
# when calling scale.
|
91
|
+
#
|
92
|
+
# call-seq:
|
93
|
+
# multiply(mat3, output = nil) -> output or new mat3
|
94
|
+
# multiply(vec3, output = nil) -> output or new vec3
|
95
|
+
# multiply(scalar, output = nil) -> output or new mat3
|
96
|
+
#
|
45
97
|
def multiply(rhs, out = nil)
|
46
98
|
raise "Invalid type for output, must be the same as RHS" if !out.nil? && !out.kind_of?(rhs.class)
|
47
99
|
case rhs
|
@@ -52,6 +104,13 @@ class Snow::Mat3
|
|
52
104
|
end
|
53
105
|
end
|
54
106
|
|
107
|
+
#
|
108
|
+
# Calls #multiply(rhs, self).
|
109
|
+
#
|
110
|
+
# call-seq: multiply!(mat3) -> self
|
111
|
+
# call-seq: multiply!(vec3) -> vec3
|
112
|
+
# call-seq: multiply!(scalar) -> self
|
113
|
+
#
|
55
114
|
def multiply!(rhs)
|
56
115
|
multiply rhs, case rhs
|
57
116
|
when Mat3, Numeric then self
|
@@ -60,10 +119,16 @@ class Snow::Mat3
|
|
60
119
|
end
|
61
120
|
end
|
62
121
|
|
122
|
+
#
|
123
|
+
# Calls scale(x, y, z, self)
|
124
|
+
#
|
125
|
+
# call-seq: scale!(x, y, z) -> self
|
126
|
+
#
|
63
127
|
def scale!(x, y, z)
|
64
128
|
scale x, y, z, self
|
65
129
|
end
|
66
130
|
|
131
|
+
|
67
132
|
alias_method :*, :multiply
|
68
133
|
alias_method :**, :scale
|
69
134
|
alias_method :~, :transpose
|
data/lib/snow-math/mat4.rb
CHANGED
@@ -7,6 +7,14 @@ require 'snow-math/bindings'
|
|
7
7
|
module Snow ; end
|
8
8
|
|
9
9
|
if Snow.const_defined?(:Mat4Array)
|
10
|
+
#
|
11
|
+
# A contiguous array of Mat4s. Allocated as a single block of memory so that
|
12
|
+
# it can easily be passed back to C libraries (like OpenGL) and to aid with
|
13
|
+
# cache locality.
|
14
|
+
#
|
15
|
+
# May also be useful to subclass as a stack of Mat4s akin to now-deprecated
|
16
|
+
# functionality in OpenGL.
|
17
|
+
#
|
10
18
|
class Snow::Mat4Array
|
11
19
|
class << self ; alias_method :[], :new ; end
|
12
20
|
|
@@ -15,29 +23,87 @@ if Snow.const_defined?(:Mat4Array)
|
|
15
23
|
end
|
16
24
|
end
|
17
25
|
|
26
|
+
#
|
27
|
+
# A 4x4 matrix. Useful for anything from rotation to projection to almost any
|
28
|
+
# other 3D transformation you might need.
|
29
|
+
#
|
18
30
|
class Snow::Mat4
|
19
31
|
|
20
32
|
class << self ; alias_method :[], :new ; end
|
21
33
|
|
22
34
|
alias_method :[], :fetch
|
23
35
|
alias_method :[]=, :store
|
36
|
+
alias_method :dup, :copy
|
37
|
+
alias_method :clone, :copy
|
24
38
|
|
39
|
+
|
40
|
+
# Calls #transpose(self)
|
41
|
+
#
|
42
|
+
# call-seq: transpose! -> self
|
25
43
|
def transpose!
|
26
44
|
transpose self
|
27
45
|
end
|
28
46
|
|
47
|
+
# Calls #inverse_orthogonal(self)
|
48
|
+
#
|
49
|
+
# call-seq: inverse_orthogonal! -> self
|
29
50
|
def inverse_orthogonal!
|
30
51
|
inverse_orthogonal self
|
31
52
|
end
|
32
53
|
|
54
|
+
# Calls #adjoint(self)
|
55
|
+
#
|
56
|
+
# call-seq: adjoint! -> self
|
33
57
|
def adjoint!
|
34
58
|
adjoint self
|
35
59
|
end
|
36
60
|
|
61
|
+
# Calls #multiply_mat4(rhs, self)
|
62
|
+
#
|
63
|
+
# call-seq: multiply_mat4!(rhs) -> self
|
37
64
|
def multiply_mat4!(rhs)
|
38
65
|
multiply_mat4 rhs, self
|
39
66
|
end
|
40
67
|
|
68
|
+
# Calls #multiply_vec4(rhs, rhs)
|
69
|
+
#
|
70
|
+
# call-seq: multiply_vec4!(rhs) -> rhs
|
71
|
+
def multiply_vec4!(rhs)
|
72
|
+
multiply_vec4 rhs, rhs
|
73
|
+
end
|
74
|
+
|
75
|
+
# Calls #transform_vec3(rhs, rhs)
|
76
|
+
#
|
77
|
+
# call-seq: transform_vec3!(rhs) -> rhs
|
78
|
+
def transform_vec3!(rhs)
|
79
|
+
transform_vec3 rhs, rhs
|
80
|
+
end
|
81
|
+
|
82
|
+
# Calls #inverse_transform_vec3(rhs, rhs)
|
83
|
+
#
|
84
|
+
# call-seq: inverse_transform_vec3!(rhs) -> rhs
|
85
|
+
def rotate_vec3!(rhs)
|
86
|
+
inverse_transform_vec3 rhs, rhs
|
87
|
+
end
|
88
|
+
|
89
|
+
# Calls #inverse_rotate_vec3(rhs, rhs)
|
90
|
+
#
|
91
|
+
# call-seq: inverse_rotate_vec3!(rhs) -> rhs
|
92
|
+
def inverse_rotate_vec3!(rhs)
|
93
|
+
inverse_rotate_vec3 rhs, rhs
|
94
|
+
end
|
95
|
+
|
96
|
+
# Calls #multiply_mat4, #multiply_vec4, #transform_vec3, and #scale,
|
97
|
+
# respectively.
|
98
|
+
#
|
99
|
+
# When calling multiply with scalar as rhs, scalar is passed as the value to
|
100
|
+
# scale all columns by.
|
101
|
+
#
|
102
|
+
# call-seq:
|
103
|
+
# multiply(mat4, output = nil) -> output or new mat4
|
104
|
+
# multiply(vec4, output = nil) -> output or new vec4
|
105
|
+
# multiply(vec3, output = nil) -> output or new vec3
|
106
|
+
# multiply(scalar, output = nil) -> output or new mat4
|
41
107
|
def multiply(rhs, out = nil)
|
42
108
|
raise "Invalid type for output, must be the same as RHS" if !out.nil? && !out.kind_of?(rhs.class)
|
43
109
|
case rhs
|
@@ -49,6 +115,8 @@ class Snow::Mat4
|
|
49
115
|
end
|
50
116
|
end
|
51
117
|
|
118
|
+
# Calls #multiply(rhs, self) when rhs is a scalar or Mat4, otherwise calls
|
119
|
+
# #multiply(rhs, rhs).
|
52
120
|
def multiply!(rhs)
|
53
121
|
multiply rhs, case rhs
|
54
122
|
when Mat4, Numeric then self
|
@@ -57,22 +125,36 @@ class Snow::Mat4
|
|
57
125
|
end
|
58
126
|
end
|
59
127
|
|
128
|
+
# Calls #scale(x, y, z, self)
|
129
|
+
#
|
130
|
+
# call-seq: scale!(x, y, z) -> self
|
60
131
|
def scale!(x, y, z)
|
61
132
|
scale x, y, z, self
|
62
133
|
end
|
63
134
|
|
135
|
+
# Calls #translate(*args, self)
|
136
|
+
#
|
137
|
+
# call-seq: translate!(vec3) -> self
|
138
|
+
# call-seq: translate!(x, y, z) -> self
|
64
139
|
def translate!(*args)
|
65
140
|
translate *args, self
|
66
141
|
end
|
67
142
|
|
143
|
+
# Calls #inverse_affine(self)
|
144
|
+
#
|
145
|
+
# call-seq: inverse_affine! -> self
|
68
146
|
def inverse_affine!
|
69
147
|
inverse_affine self
|
70
148
|
end
|
71
149
|
|
150
|
+
# Calls #inverse_general(self)
|
151
|
+
#
|
152
|
+
# call-seq: inverse_general! -> self
|
72
153
|
def inverse_general!
|
73
154
|
inverse_general self
|
74
155
|
end
|
75
156
|
|
157
|
+
|
76
158
|
alias_method :*, :multiply
|
77
159
|
alias_method :**, :scale
|
78
160
|
alias_method :~, :transpose
|