snow-math 1.3.1 → 1.4.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,134 @@
1
+ /*
2
+ 2D vector maths
3
+ Written by Noel Cower
4
+
5
+ See COPYING for license information
6
+ */
7
+
8
+ #define __SNOW__VEC2_C__
9
+
10
+ #include "maths_local.h"
11
+
12
+ #if defined(__cplusplus)
13
+ extern "C"
14
+ {
15
+ #endif /* __cplusplus */
16
+
17
+ const vec2_t g_vec2_zero = {s_float_lit(0.0), s_float_lit(0.0)};
18
+ const vec2_t g_vec2_one = {s_float_lit(1.0), s_float_lit(1.0)};
19
+
20
+ void vec2_copy(const vec2_t in, vec2_t out)
21
+ {
22
+ out[1] = in[1];
23
+ out[0] = in[0];
24
+ }
25
+
26
+ void vec2_set(s_float_t x, s_float_t y, vec2_t v)
27
+ {
28
+ v[0] = x;
29
+ v[1] = y;
30
+ }
31
+
32
+ /*!
33
+ * Gets the squared length of a vector. Useful for approximations and when
34
+ * you don't need the actual magnitude.
35
+ */
36
+ s_float_t vec2_length_squared(const vec2_t v)
37
+ {
38
+ return (v[0] * v[0]) + (v[1] * v[1]);
39
+ }
40
+
41
+ /*!
42
+ * Gets the length/magnitude of a vector.
43
+ */
44
+ s_float_t vec2_length(const vec2_t v)
45
+ {
46
+ return s_sqrt((v[0] * v[0]) + (v[1] * v[1]));
47
+ }
48
+
49
+ void vec2_normalize(const vec2_t in, vec2_t out)
50
+ {
51
+ s_float_t mag = vec2_length(in);
52
+ if (mag) mag = s_float_lit(1.0) / mag;
53
+ out[1] = in[1] * mag;
54
+ out[0] = in[0] * mag;
55
+ }
56
+
57
+ void vec2_subtract(const vec2_t left, const vec2_t right, vec2_t out)
58
+ {
59
+ out[1] = left[1] - right[1];
60
+ out[0] = left[0] - right[0];
61
+ }
62
+
63
+ void vec2_add(const vec2_t left, const vec2_t right, vec2_t out)
64
+ {
65
+ out[1] = left[1] + right[1];
66
+ out[0] = left[0] + right[0];
67
+ }
68
+
69
+ void vec2_multiply(const vec2_t left, const vec2_t right, vec2_t out)
70
+ {
71
+ out[1] = left[1] * right[1];
72
+ out[0] = left[0] * right[0];
73
+ }
74
+
75
+ void vec2_negate(const vec2_t v, vec2_t out)
76
+ {
77
+ out[1] = -v[1];
78
+ out[0] = -v[0];
79
+ }
80
+
81
+ void vec2_inverse(const vec2_t v, vec2_t out)
82
+ {
83
+ out[1] = (!float_is_zero(v[1])) ? (s_float_lit(1.0) / v[1]) : v[1];
84
+ out[0] = (!float_is_zero(v[0])) ? (s_float_lit(1.0) / v[0]) : v[0];
85
+ }
86
+
87
+ void vec2_project(const vec2_t in, const vec2_t normal, vec2_t out)
88
+ {
89
+ vec2_scale(normal, vec2_dot_product(in, normal), out);
90
+ }
91
+
92
+ void vec2_reflect(const vec2_t in, const vec2_t normal, vec2_t out)
93
+ {
94
+ vec2_t temp;
95
+ vec2_scale(normal, s_float_lit(2.0) * vec2_dot_product(in, normal), temp);
96
+ vec2_subtract(in, temp, out);
97
+ }
98
+
99
+ s_float_t vec2_dot_product(const vec2_t left, const vec2_t right)
100
+ {
101
+ return ((left[0] * right[0]) + (left[1] * right[1]));
102
+ }
103
+
104
+ void vec2_scale(const vec2_t v, s_float_t scalar, vec2_t out)
105
+ {
106
+ out[1] = v[1] * scalar;
107
+ out[0] = v[0] * scalar;
108
+ }
109
+
110
+ /*!
111
+ * Divides the given vector by the divisor.
112
+ * \returns Zero if successful, otherwise nonzero if the result is undefined.
113
+ */
114
+ int vec2_divide(const vec2_t v, s_float_t divisor, vec2_t out)
115
+ {
116
+ if (divisor) {
117
+ divisor = s_float_lit(1.0) / divisor;
118
+ out[1] = v[1] * divisor;
119
+ out[0] = v[0] * divisor;
120
+ return 1;
121
+ }
122
+ return 0;
123
+ }
124
+
125
+ int vec2_equals(const vec2_t left, const vec2_t right)
126
+ {
127
+ return float_equals(left[0], right[0]) &&
128
+ float_equals(left[1], right[1]);
129
+ }
130
+
131
+ #if defined(__cplusplus)
132
+ }
133
+ #endif /* __cplusplus */
134
+
data/lib/snow-math.rb CHANGED
@@ -3,6 +3,7 @@
3
3
  # See COPYING for license details.
4
4
 
5
5
  require 'snow-math/bindings'
6
+ require 'snow-math/vec2'
6
7
  require 'snow-math/vec3'
7
8
  require 'snow-math/vec4'
8
9
  require 'snow-math/mat3'
@@ -21,6 +22,6 @@ module Snow
21
22
  #
22
23
  # snow-math bindings version string.
23
24
  #
24
- SNOW_MATH_VERSION = '1.3.0'
25
+ SNOW_MATH_VERSION = '1.4.2'
25
26
 
26
27
  end
@@ -2,7 +2,8 @@
2
2
  # Copyright (c) 2013 Noel Raymond Cower. All rights reserved.
3
3
  # See COPYING for license details.
4
4
 
5
- require 'snow-math'
5
+ require 'snow-math/bindings'
6
+ require 'snow-math/to_a'
6
7
 
7
8
  module Snow
8
9
 
@@ -34,12 +35,17 @@ module Snow
34
35
 
35
36
  end
36
37
 
38
+ class Vec2 ; include ::Snow::InspectSupport ; end
37
39
  class Vec3 ; include ::Snow::InspectSupport ; end
38
40
  class Vec4 ; include ::Snow::InspectSupport ; end
39
41
  class Quat ; include ::Snow::InspectSupport ; end
40
42
  class Mat3 ; include ::Snow::InspectSupport ; end
41
43
  class Mat4 ; include ::Snow::InspectSupport ; end
42
44
 
45
+ if const_defined?(:Vec2Array)
46
+ class Vec2Array ; include ::Snow::InspectSupport ; end
47
+ end
48
+
43
49
  if const_defined?(:Vec3Array)
44
50
  class Vec3Array ; include ::Snow::InspectSupport ; end
45
51
  end
@@ -1,4 +1,5 @@
1
1
  require 'snow-math/bindings'
2
+ require 'snow-math/to_a'
2
3
 
3
4
  module Snow ; end
4
5
 
@@ -46,12 +47,17 @@ module Snow::ArrayMarshalSupport # :nodoc: all
46
47
  end
47
48
 
48
49
  module Snow
50
+ class Vec2 ; include ::Snow::BaseMarshalSupport ; end
49
51
  class Vec3 ; include ::Snow::BaseMarshalSupport ; end
50
52
  class Vec4 ; include ::Snow::BaseMarshalSupport ; end
51
53
  class Quat ; include ::Snow::BaseMarshalSupport ; end
52
54
  class Mat3 ; include ::Snow::BaseMarshalSupport ; end
53
55
  class Mat4 ; include ::Snow::BaseMarshalSupport ; end
54
56
 
57
+ if const_defined?(:Vec2Array)
58
+ class Vec2Array ; include ::Snow::ArrayMarshalSupport ; end
59
+ end
60
+
55
61
  if const_defined?(:Vec3Array)
56
62
  class Vec3Array ; include ::Snow::ArrayMarshalSupport ; end
57
63
  end
@@ -36,6 +36,10 @@ class Snow::Mat3
36
36
  alias_method :clone, :copy
37
37
 
38
38
 
39
+ def to_quat
40
+ Quat.new(self)
41
+ end
42
+
39
43
  #
40
44
  # Calls #transpose(self)
41
45
  #
@@ -37,6 +37,10 @@ class Snow::Mat4
37
37
  alias_method :clone, :copy
38
38
 
39
39
 
40
+ def to_quat
41
+ Quat.new(self)
42
+ end
43
+
40
44
  # Calls #transpose(self)
41
45
  #
42
46
  # call-seq: transpose! -> self
@@ -136,7 +140,7 @@ class Snow::Mat4
136
140
  # call-seq: translate!(vec3) -> self
137
141
  # call-seq: translate!(x, y, z) -> self
138
142
  def translate!(*args)
139
- translate *args, self
143
+ translate(*args, self)
140
144
  end
141
145
 
142
146
  # Calls #inverse_affine(self)
data/lib/snow-math/ptr.rb CHANGED
@@ -2,7 +2,7 @@
2
2
  # Copyright (c) 2013 Noel Raymond Cower. All rights reserved.
3
3
  # See COPYING for license details.
4
4
 
5
- require 'snow-math'
5
+ require 'snow-math/bindings'
6
6
  require 'fiddle'
7
7
 
8
8
  module Snow
@@ -27,6 +27,10 @@ module Snow
27
27
  end
28
28
  end
29
29
 
30
+ class Vec2
31
+ include ::Snow::FiddlePointerSupport
32
+ end
33
+
30
34
  class Vec3
31
35
  include ::Snow::FiddlePointerSupport
32
36
  end
@@ -47,6 +51,12 @@ module Snow
47
51
  include ::Snow::FiddlePointerSupport
48
52
  end
49
53
 
54
+ if const_defined?(:Vec2Array)
55
+ class Vec2Array
56
+ include ::Snow::FiddlePointerSupport
57
+ end
58
+ end
59
+
50
60
  if const_defined?(:Vec3Array)
51
61
  class Vec3Array
52
62
  include ::Snow::FiddlePointerSupport
@@ -33,6 +33,30 @@ class Snow::Quat
33
33
  alias_method :clone, :copy
34
34
 
35
35
 
36
+ def to_vec2
37
+ Vec2.new(self)
38
+ end
39
+
40
+ def to_vec3
41
+ Vec3.new(self)
42
+ end
43
+
44
+ def to_vec4
45
+ Vec4.new(self)
46
+ end
47
+
48
+ def to_quat
49
+ Quat.new(self)
50
+ end
51
+
52
+ def to_mat3
53
+ Mat3.new(self)
54
+ end
55
+
56
+ def to_mat4
57
+ Mat4.new(self)
58
+ end
59
+
36
60
  # Returns the X component of the quaternion.
37
61
  #
38
62
  # call-seq: x -> float
@@ -2,7 +2,8 @@
2
2
  # Copyright (c) 2013 Noel Raymond Cower. All rights reserved.
3
3
  # See COPYING for license details.
4
4
 
5
- require 'snow-math'
5
+ require 'snow-math/bindings'
6
+ require 'snow-math/to_a'
6
7
 
7
8
  module Snow ; end
8
9
 
@@ -80,20 +81,26 @@ module Snow
80
81
 
81
82
  end
82
83
 
84
+ class Snow::Vec2
85
+ @@SWIZZLE_CHARS = /^[xy]{2,4}$/
86
+ @@SWIZZLE_MAPPING = { 2 => self, 3 => ::Snow::Vec3, 4 => ::Snow::Vec4, 'x' => 0, 'y' => 1 }
87
+ include ::Snow::SwizzleSupport
88
+ end
89
+
83
90
  class Snow::Vec3
84
- @@SWIZZLE_CHARS = /^[xyz]{3,4}$/
85
- @@SWIZZLE_MAPPING = { 3 => self, 4 => ::Snow::Vec4, 'x' => 0, 'y' => 1, 'z' => 2 }
91
+ @@SWIZZLE_CHARS = /^[xyz]{2,4}$/
92
+ @@SWIZZLE_MAPPING = { 2 => ::Snow::Vec2, 3 => self, 4 => ::Snow::Vec4, 'x' => 0, 'y' => 1, 'z' => 2 }
86
93
  include ::Snow::SwizzleSupport
87
94
  end
88
95
 
89
96
  class Snow::Vec4
90
- @@SWIZZLE_CHARS = /^[xyzw]{3,4}$/
91
- @@SWIZZLE_MAPPING = { 3 => ::Snow::Vec3, 4 => self, 'x' => 0, 'y' => 1, 'z' => 2, 'w' => 3 }
97
+ @@SWIZZLE_CHARS = /^[xyzw]{2,4}$/
98
+ @@SWIZZLE_MAPPING = { 2 => ::Snow::Vec2, 3 => ::Snow::Vec3, 4 => self, 'x' => 0, 'y' => 1, 'z' => 2, 'w' => 3 }
92
99
  include ::Snow::SwizzleSupport
93
100
  end
94
101
 
95
102
  class Snow::Quat
96
- @@SWIZZLE_CHARS = /^[xyzw]{3,4}$/
97
- @@SWIZZLE_MAPPING = { 3 => ::Snow::Vec3, 4 => self, 'x' => 0, 'y' => 1, 'z' => 2, 'w' => 3 }
103
+ @@SWIZZLE_CHARS = /^[xyzw]{2,4}$/
104
+ @@SWIZZLE_MAPPING = { 2 => ::Snow::Vec2, 3 => ::Snow::Vec3, 4 => self, 'x' => 0, 'y' => 1, 'z' => 2, 'w' => 3 }
98
105
  include ::Snow::SwizzleSupport
99
106
  end
@@ -2,7 +2,7 @@
2
2
  # Copyright (c) 2013 Noel Raymond Cower. All rights reserved.
3
3
  # See COPYING for license details.
4
4
 
5
- require 'snow-math'
5
+ require 'snow-math/bindings'
6
6
 
7
7
  module Snow
8
8
 
@@ -107,12 +107,30 @@ module Snow
107
107
 
108
108
  end
109
109
 
110
+ class Vec2 ; include ::Snow::ArraySupport ; end
110
111
  class Vec3 ; include ::Snow::ArraySupport ; end
111
112
  class Vec4 ; include ::Snow::ArraySupport ; end
112
113
  class Quat ; include ::Snow::ArraySupport ; end
113
114
  class Mat3 ; include ::Snow::ArraySupport ; end
114
115
  class Mat4 ; include ::Snow::ArraySupport ; end
115
116
 
117
+ if const_defined?(:Vec2Array)
118
+ class Vec2Array
119
+ include ::Snow::ArraySupport
120
+
121
+ #
122
+ # Duplicates the Vec2Array and returns it.
123
+ #
124
+ # call-seq: dup -> new vec3_array
125
+ #
126
+ def dup
127
+ self.class.new(self)
128
+ end
129
+
130
+ alias_method :clone, :dup
131
+ end
132
+ end
133
+
116
134
  if const_defined?(:Vec3Array)
117
135
  class Vec3Array
118
136
  include ::Snow::ArraySupport
@@ -125,6 +143,8 @@ module Snow
125
143
  def dup
126
144
  self.class.new(self)
127
145
  end
146
+
147
+ alias_method :clone, :dup
128
148
  end
129
149
  end
130
150
 
@@ -140,6 +160,8 @@ module Snow
140
160
  def dup
141
161
  self.class.new(self)
142
162
  end
163
+
164
+ alias_method :clone, :dup
143
165
  end
144
166
  end
145
167
 
@@ -155,6 +177,8 @@ module Snow
155
177
  def dup
156
178
  self.class.new(self)
157
179
  end
180
+
181
+ alias_method :clone, :dup
158
182
  end
159
183
  end
160
184
 
@@ -170,6 +194,8 @@ module Snow
170
194
  def dup
171
195
  self.class.new(self)
172
196
  end
197
+
198
+ alias_method :clone, :dup
173
199
  end
174
200
  end
175
201
 
@@ -185,6 +211,8 @@ module Snow
185
211
  def dup
186
212
  self.class.new(self)
187
213
  end
214
+
215
+ alias_method :clone, :dup
188
216
  end
189
217
  end
190
218
 
@@ -0,0 +1,168 @@
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?(:Vec2Array)
10
+ #
11
+ # A contiguous array of Vec2s. 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
+ # Useful also to represent texture coordinates and 2D positions and
16
+ # displacements.
17
+ #
18
+ class Snow::Vec2Array
19
+ class << self ; alias_method :[], :new ; end
20
+
21
+ alias_method :[], :fetch
22
+ alias_method :[]=, :store
23
+ end
24
+ end
25
+
26
+ #
27
+ # A 2-component vector class.
28
+ #
29
+ class Snow::Vec2
30
+
31
+ class << self ; alias_method :[], :new ; end
32
+
33
+ alias_method :[], :fetch
34
+ alias_method :[]=, :store
35
+ alias_method :dup, :copy
36
+ alias_method :clone, :copy
37
+
38
+
39
+ def to_vec2
40
+ Vec2.new(self)
41
+ end
42
+
43
+ def to_vec3
44
+ Vec3.new(self)
45
+ end
46
+
47
+ def to_vec4
48
+ Vec4.new(self)
49
+ end
50
+
51
+ def to_quat
52
+ Quat.new(self)
53
+ end
54
+
55
+ # Returns the X component of the vector.
56
+ #
57
+ # call-seq: x -> float
58
+ def x
59
+ self[0]
60
+ end
61
+
62
+ # Sets the X component of the vector.
63
+ #
64
+ # call-seq: x = value -> value
65
+ def x=(value)
66
+ self[0] = value
67
+ end
68
+
69
+ # Returns the Y component of the vector.
70
+ #
71
+ # call-seq: y -> float
72
+ def y
73
+ self[1]
74
+ end
75
+
76
+ # Sets the Y component of the vector.
77
+ #
78
+ # call-seq: y = value -> value
79
+ def y=(value)
80
+ self[1] = value
81
+ end
82
+
83
+ # Calls #normalize(self)
84
+ #
85
+ # call-seq: normalize! -> self
86
+ def normalize!
87
+ normalize self
88
+ end
89
+
90
+ # Calls #inverse(self)
91
+ #
92
+ # call-seq: inverse! -> self
93
+ def inverse!
94
+ inverse self
95
+ end
96
+
97
+ # Calls #negate(self)
98
+ #
99
+ # call-seq: negate! -> self
100
+ def negate!
101
+ negate self
102
+ end
103
+
104
+ # Calls #multiply_vec2(rhs, self)
105
+ #
106
+ # call-seq: multiply_vec2!(rhs) -> self
107
+ def multiply_vec2!(rhs)
108
+ multiply_vec2 rhs, self
109
+ end
110
+
111
+ # Calls #multiply_vec2 and #scale, respectively.
112
+ #
113
+ # call-seq:
114
+ # multiply(vec2, output = nil) -> output or new vec2
115
+ # multiply(scalar, output = nil) -> output or new vec2
116
+ def multiply(rhs, output = nil)
117
+ case rhs
118
+ when ::Snow::Vec2, ::Snow::Vec3, ::Snow::Vec4, ::Snow::Quat then multiply_vec2(rhs, output)
119
+ when Numeric then scale(rhs, output)
120
+ else raise TypeError, "Invalid type for RHS"
121
+ end
122
+ end
123
+
124
+ # Calls #multiply(rhs, self)
125
+ #
126
+ # call-seq: multiply!(rhs) -> self
127
+ def multiply!(rhs)
128
+ multiply rhs, self
129
+ end
130
+
131
+ # Calls #add(rhs, self)
132
+ #
133
+ # call-seq: add!(rhs) -> self
134
+ def add!(rhs)
135
+ add rhs, self
136
+ end
137
+
138
+ # Calls #subtract(rhs, self)
139
+ #
140
+ # call-seq: subtract!(rhs) -> self
141
+ def subtract!(rhs)
142
+ subtract rhs, self
143
+ end
144
+
145
+ # Calls #scale(rhs, self)
146
+ #
147
+ # call-seq: scale!(rhs) -> self
148
+ def scale!(rhs)
149
+ scale rhs, self
150
+ end
151
+
152
+ # Calls #divide(rhs, self)
153
+ #
154
+ # call-seq: divide!(rhs) -> self
155
+ def divide!(rhs)
156
+ divide rhs, self
157
+ end
158
+
159
+
160
+ alias_method :-, :subtract
161
+ alias_method :+, :add
162
+ alias_method :*, :multiply
163
+ alias_method :/, :divide
164
+ alias_method :**, :dot_product
165
+ alias_method :-@, :negate
166
+ alias_method :~, :inverse
167
+
168
+ end