snow-math 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,144 @@
1
+ /*
2
+ 3D vector maths
3
+ Written by Noel Cower
4
+
5
+ See COPYING for license information
6
+ */
7
+
8
+ #define __SNOW__VEC3_C__
9
+
10
+ #include "maths_local.h"
11
+
12
+ #if defined(__cplusplus)
13
+ extern "C"
14
+ {
15
+ #endif /* __cplusplus */
16
+
17
+ const vec3_t g_vec3_zero = {0.0, 0.0, 0.0};
18
+ const vec3_t g_vec3_one = {1.0, 1.0, 1.0};
19
+
20
+ void vec3_copy(const vec3_t in, vec3_t out)
21
+ {
22
+ out[0] = in[0];
23
+ out[1] = in[1];
24
+ out[2] = in[2];
25
+ }
26
+
27
+ void vec3_set(s_float_t x, s_float_t y, s_float_t z, vec3_t v)
28
+ {
29
+ v[0] = x;
30
+ v[1] = y;
31
+ v[2] = z;
32
+ }
33
+
34
+ /*!
35
+ * Gets the squared length of a vector. Useful for approximations and when
36
+ * you don't need the actual magnitude.
37
+ */
38
+ s_float_t vec3_length_squared(const vec3_t v)
39
+ {
40
+ return (v[0] * v[0]) + (v[1] * v[1]) + (v[2] * v[2]);
41
+ }
42
+
43
+ /*!
44
+ * Gets the length/magnitude of a vector.
45
+ */
46
+ s_float_t vec3_length(const vec3_t v)
47
+ {
48
+ return sqrtf((v[0] * v[0]) + (v[1] * v[1]) + (v[2] * v[2]));
49
+ }
50
+
51
+ void vec3_normalize(const vec3_t in, vec3_t out)
52
+ {
53
+ s_float_t mag = vec3_length(in);
54
+ if (mag) mag = 1.0 / mag;
55
+ out[0] = in[0] * mag;
56
+ out[1] = in[1] * mag;
57
+ out[2] = in[2] * mag;
58
+ }
59
+
60
+ void vec3_subtract(const vec3_t left, const vec3_t right, vec3_t out)
61
+ {
62
+ out[0] = left[0] - right[0];
63
+ out[1] = left[1] - right[1];
64
+ out[2] = left[2] - right[2];
65
+ }
66
+
67
+ void vec3_add(const vec3_t left, const vec3_t right, vec3_t out)
68
+ {
69
+ out[0] = left[0] + right[0];
70
+ out[1] = left[1] + right[1];
71
+ out[2] = left[2] + right[2];
72
+ }
73
+
74
+ void vec3_multiply(const vec3_t left, const vec3_t right, vec3_t out)
75
+ {
76
+ out[0] = left[0] * right[0];
77
+ out[1] = left[1] * right[1];
78
+ out[2] = left[2] * right[2];
79
+ }
80
+
81
+ void vec3_negate(const vec3_t v, vec3_t out)
82
+ {
83
+ out[2] = -v[2];
84
+ out[1] = -v[1];
85
+ out[0] = -v[0];
86
+ }
87
+
88
+ void vec3_inverse(const vec3_t v, vec3_t out)
89
+ {
90
+ out[2] = (v[2] != 0.0 && v[2] != -0.0) ? (1.0 / v[2]) : v[2];
91
+ out[1] = (v[1] != 0.0 && v[1] != -0.0) ? (1.0 / v[1]) : v[1];
92
+ out[0] = (v[0] != 0.0 && v[0] != -0.0) ? (1.0 / v[0]) : v[0];
93
+ }
94
+
95
+ void vec3_cross_product(const vec3_t left, const vec3_t right, vec3_t out)
96
+ {
97
+ s_float_t x, y, z;
98
+ x = (left[1] * right[2]) - (left[2] * right[1]);
99
+ y = (left[0] * right[2]) - (left[2] * right[0]);
100
+ z = (left[0] * right[1]) - (left[1] * right[0]);
101
+ out[0] = x;
102
+ out[1] = y;
103
+ out[2] = z;
104
+ }
105
+
106
+ s_float_t vec3_dot_product(const vec3_t left, const vec3_t right)
107
+ {
108
+ return ((left[0] * right[0]) + (left[1] * right[1]) + (left[2] * right[2]));
109
+ }
110
+
111
+ void vec3_scale(const vec3_t v, s_float_t scalar, vec3_t out)
112
+ {
113
+ out[2] = v[2] * scalar;
114
+ out[1] = v[1] * scalar;
115
+ out[0] = v[0] * scalar;
116
+ }
117
+
118
+ /*!
119
+ * Divides the given vector by the divisor.
120
+ * \returns Zero if successful, otherwise nonzero if the result is undefined.
121
+ */
122
+ int vec3_divide(const vec3_t v, s_float_t divisor, vec3_t out)
123
+ {
124
+ if (divisor) {
125
+ divisor = ((s_float_t)1.0) / divisor;
126
+ out[2] = v[2] * divisor;
127
+ out[1] = v[1] * divisor;
128
+ out[0] = v[0] * divisor;
129
+ return 0;
130
+ }
131
+ return 1;
132
+ }
133
+
134
+ int vec3_equals(const vec3_t left, const vec3_t right)
135
+ {
136
+ return float_equals(left[0], right[0]) &&
137
+ float_equals(left[1], right[1]) &&
138
+ float_equals(left[2], right[2]);
139
+ }
140
+
141
+ #if defined(__cplusplus)
142
+ }
143
+ #endif /* __cplusplus */
144
+
@@ -0,0 +1,145 @@
1
+ /*
2
+ 4D vector maths
3
+ Written by Noel Cower
4
+
5
+ See COPYING for license information
6
+ */
7
+
8
+ #define __SNOW__VEC4_C__
9
+
10
+ #include "maths_local.h"
11
+
12
+ #if defined(__cplusplus)
13
+ extern "C"
14
+ {
15
+ #endif /* __cplusplus */
16
+
17
+ const vec4_t g_vec4_zero = {0.0, 0.0, 0.0, 0.0};
18
+ const vec4_t g_vec4_one = {1.0, 1.0, 1.0, 1.0};
19
+ const vec4_t g_vec4_identity = {0.0, 0.0, 0.0, 1.0};
20
+
21
+ void vec4_copy(const vec4_t in, vec4_t out)
22
+ {
23
+ out[0] = in[0];
24
+ out[1] = in[1];
25
+ out[2] = in[2];
26
+ out[3] = in[3];
27
+ }
28
+
29
+ void vec4_set(s_float_t x, s_float_t y, s_float_t z, s_float_t w, vec4_t v)
30
+ {
31
+ v[0] = x;
32
+ v[1] = y;
33
+ v[2] = z;
34
+ v[3] = w;
35
+ }
36
+
37
+ /*!
38
+ * Gets the squared length of a vector. Useful for approximations and when
39
+ * you don't need the actual magnitude.
40
+ */
41
+ s_float_t vec4_length_squared(const vec4_t v)
42
+ {
43
+ return (v[0] * v[0]) + (v[1] * v[1]) + (v[2] * v[2]) + (v[3] * v[3]);
44
+ }
45
+
46
+ /*!
47
+ * Gets the length/magnitude of a vector.
48
+ */
49
+ s_float_t vec4_length(const vec4_t v)
50
+ {
51
+ return sqrtf((v[0] * v[0]) + (v[1] * v[1]) + (v[2] * v[2]) + (v[3] * v[3]));
52
+ }
53
+
54
+ void vec4_normalize(const vec4_t in, vec4_t out)
55
+ {
56
+ s_float_t mag = vec4_length(in);
57
+ if (mag) mag = 1.0 / mag;
58
+ out[0] = in[0] * mag;
59
+ out[1] = in[1] * mag;
60
+ out[2] = in[2] * mag;
61
+ out[3] = in[3] * mag;
62
+ }
63
+
64
+ void vec4_subtract(const vec4_t left, const vec4_t right, vec4_t out)
65
+ {
66
+ out[0] = left[0] - right[0];
67
+ out[1] = left[1] - right[1];
68
+ out[2] = left[2] - right[2];
69
+ out[3] = left[3] - right[3];
70
+ }
71
+
72
+ void vec4_add(const vec4_t left, const vec4_t right, vec4_t out)
73
+ {
74
+ out[0] = left[0] + right[0];
75
+ out[1] = left[1] + right[1];
76
+ out[2] = left[2] + right[2];
77
+ out[3] = left[3] + right[3];
78
+ }
79
+
80
+ void vec4_multiply(const vec4_t left, const vec4_t right, vec4_t out)
81
+ {
82
+ out[0] = left[0] * right[0];
83
+ out[1] = left[1] * right[1];
84
+ out[2] = left[2] * right[2];
85
+ out[3] = left[3] * right[3];
86
+ }
87
+
88
+ void vec4_negate(const vec4_t v, vec4_t out)
89
+ {
90
+ out[3] = -v[3];
91
+ out[2] = -v[2];
92
+ out[1] = -v[1];
93
+ out[0] = -v[0];
94
+ }
95
+
96
+ void vec4_inverse(const vec4_t v, vec4_t out)
97
+ {
98
+ out[3] = (v[3] != 0.0 && v[3] != -0.0) ? (1.0 / v[3]) : v[3];
99
+ out[2] = (v[2] != 0.0 && v[2] != -0.0) ? (1.0 / v[2]) : v[2];
100
+ out[1] = (v[1] != 0.0 && v[1] != -0.0) ? (1.0 / v[1]) : v[1];
101
+ out[0] = (v[0] != 0.0 && v[0] != -0.0) ? (1.0 / v[0]) : v[0];
102
+ }
103
+
104
+ s_float_t vec4_dot_product(const vec4_t left, const vec4_t right)
105
+ {
106
+ return ((left[0] * right[0]) + (left[1] * right[1]) + (left[2] * right[2]) + (left[3] * right[3]));
107
+ }
108
+
109
+ void vec4_scale(const vec4_t v, s_float_t scalar, vec4_t out)
110
+ {
111
+ out[3] = v[3] * scalar;
112
+ out[2] = v[2] * scalar;
113
+ out[1] = v[1] * scalar;
114
+ out[0] = v[0] * scalar;
115
+ }
116
+
117
+ /*!
118
+ * Divides the given vector by the divisor.
119
+ * \returns Zero if successful, otherwise nonzero if the result is undefined.
120
+ */
121
+ int vec4_divide(const vec4_t v, s_float_t divisor, vec4_t out)
122
+ {
123
+ if (divisor) {
124
+ divisor = ((s_float_t)1.0) / divisor;
125
+ out[3] = v[3] * divisor;
126
+ out[2] = v[2] * divisor;
127
+ out[1] = v[1] * divisor;
128
+ out[0] = v[0] * divisor;
129
+ return 1;
130
+ }
131
+ return 0;
132
+ }
133
+
134
+ int vec4_equals(const vec4_t left, const vec4_t right)
135
+ {
136
+ return float_equals(left[0], right[0]) &&
137
+ float_equals(left[1], right[1]) &&
138
+ float_equals(left[2], right[2]) &&
139
+ float_equals(left[3], right[3]);
140
+ }
141
+
142
+ #if defined(__cplusplus)
143
+ }
144
+ #endif /* __cplusplus */
145
+
data/lib/snow-math.rb ADDED
@@ -0,0 +1,14 @@
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
+ require 'snow-math/vec3'
7
+ require 'snow-math/vec4'
8
+ require 'snow-math/mat4'
9
+ require 'snow-math/quat'
10
+ require 'snow-math/swizzle'
11
+ require 'snow-math/inspect'
12
+ require 'snow-math/to_a'
13
+
14
+ module Snow ; end
@@ -0,0 +1,20 @@
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'
6
+
7
+ module Snow ; end
8
+
9
+ module Snow::MathInspect
10
+
11
+ def inspect
12
+ "<#{self.class.name}:0x#{self.address.to_s(16)} #{(0 ... self.length).map { |i| self[i] }.join(' ')}>"
13
+ end
14
+
15
+ end
16
+
17
+ class Snow::Vec3 ; include Snow::MathInspect ; end
18
+ class Snow::Vec4 ; include Snow::MathInspect ; end
19
+ class Snow::Quat ; include Snow::MathInspect ; end
20
+ class Snow::Mat4 ; include Snow::MathInspect ; end
File without changes
File without changes
@@ -0,0 +1,80 @@
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::Mat4Array
11
+ class << self ; alias_method :[], :new ; end
12
+
13
+ alias_method :[], :fetch
14
+ alias_method :[]=, :store
15
+ end
16
+ end
17
+
18
+ class Snow::Mat4
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_orthogonal!
30
+ inverse_orthogonal self
31
+ end
32
+
33
+ def adjoint!
34
+ adjoint self
35
+ end
36
+
37
+ def multiply_mat4!(rhs)
38
+ multiply_mat4 rhs, self
39
+ end
40
+
41
+ def multiply(rhs, out = nil)
42
+ raise "Invalid type for output, must be the same as RHS" if !out.nil? && !out.kind_of?(rhs.class)
43
+ case rhs
44
+ when ::Snow::Mat4 then multiply_mat4(rhs, out)
45
+ when ::Snow::Vec4 then multiply_vec4(rhs, out)
46
+ when ::Snow::Vec3 then transform_vec3(rhs, out)
47
+ when Numeric then scale(rhs, rhs, rhs, out)
48
+ else raise TypeError, "Invalid type for RHS"
49
+ end
50
+ end
51
+
52
+ def multiply!(rhs)
53
+ multiply rhs, case rhs
54
+ when Mat4, Numeric then self
55
+ when Vec4, Vec3 then rhs
56
+ else raise TypeError, "Invalid type for RHS"
57
+ end
58
+ end
59
+
60
+ def scale!(x, y, z)
61
+ scale x, y, z, self
62
+ end
63
+
64
+ def translate!(*args)
65
+ translate *args, self
66
+ end
67
+
68
+ def inverse_affine!
69
+ inverse_affine self
70
+ end
71
+
72
+ def inverse_general!
73
+ inverse_general self
74
+ end
75
+
76
+ alias_method :*, :multiply
77
+ alias_method :**, :scale
78
+ alias_method :~, :transpose
79
+
80
+ end
File without changes
@@ -0,0 +1,25 @@
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'
6
+ require 'fiddle'
7
+
8
+ module Snow ; end
9
+
10
+ module Snow::MathPointers
11
+
12
+ def to_ptr
13
+ Fiddle::Pointer.new(self.address, self.size)
14
+ end
15
+
16
+ end
17
+
18
+ Snow::Vec3.include(Snow::MathPointers)
19
+ Snow::Vec4.include(Snow::MathPointers)
20
+ Snow::Quat.include(Snow::MathPointers)
21
+ Snow::Mat4.include(Snow::MathPointers)
22
+ Snow::Vec3Array.include(Snow::MathPointers) if Snow.const_defined?(:Vec3Array)
23
+ Snow::Vec4Array.include(Snow::MathPointers) if Snow.const_defined?(:Vec4Array)
24
+ Snow::QuatArray.include(Snow::MathPointers) if Snow.const_defined?(:QuatArray)
25
+ Snow::Mat4Array.include(Snow::MathPointers) if Snow.const_defined?(:Mat4Array)
@@ -0,0 +1,114 @@
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?(:QuatArray)
10
+ class Snow::QuatArray
11
+ class << self ; alias_method :[], :new ; end
12
+
13
+ alias_method :[], :fetch
14
+ alias_method :[]=, :store
15
+ end
16
+ end
17
+
18
+ class Snow::Quat
19
+
20
+ class << self ; alias_method :[], :new ; end
21
+
22
+ alias_method :[], :fetch
23
+ alias_method :[]=, :store
24
+
25
+ def x
26
+ self[0]
27
+ end
28
+
29
+ def x=(value)
30
+ self[0] = value
31
+ end
32
+
33
+ def y
34
+ self[1]
35
+ end
36
+
37
+ def y=(value)
38
+ self[1] = value
39
+ end
40
+
41
+ def z
42
+ self[2]
43
+ end
44
+
45
+ def z=(value)
46
+ self[2] = value
47
+ end
48
+
49
+ def w
50
+ self[3]
51
+ end
52
+
53
+ def w=(value)
54
+ self[3] = value
55
+ end
56
+
57
+ def dup
58
+ self.class.new(self)
59
+ end
60
+
61
+ def normalize!
62
+ normalize self
63
+ end
64
+
65
+ def inverse!
66
+ inverse self
67
+ end
68
+
69
+ def negate!
70
+ negate self
71
+ end
72
+
73
+ def multiply_quat!(rhs)
74
+ multiply_quat rhs, self
75
+ end
76
+
77
+ def multiply(rhs, output = nil)
78
+ case rhs
79
+ when Quat then multiply_quat(rhs, output)
80
+ when Vec3 then multiply_vec3(rhs, output)
81
+ when Numeric then scale(rhs, output)
82
+ else raise TypeError, "Invalid type for RHS"
83
+ end
84
+ end
85
+
86
+ def multiply!(rhs)
87
+ multiply_quat! rhs
88
+ end
89
+
90
+ def add!(rhs)
91
+ add rhs, self
92
+ end
93
+
94
+ def subtract!(rhs)
95
+ subtract rhs, self
96
+ end
97
+
98
+ def scale!(rhs)
99
+ scale rhs, self
100
+ end
101
+
102
+ def divide!(rhs)
103
+ divide rhs, self
104
+ end
105
+
106
+ alias_method :-, :subtract
107
+ alias_method :+, :add
108
+ alias_method :**, :dot_product
109
+ alias_method :*, :scale
110
+ alias_method :/, :divide
111
+ alias_method :-@, :negate
112
+ alias_method :~, :inverse
113
+
114
+ end