snow-math 1.1.0 → 1.2.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 46e87b537dadcdf09df7d18126a3fdd43bae0269
4
- data.tar.gz: 48ac51001b1c24bf65e0916a2d8253745bf61680
3
+ metadata.gz: acf4b914538998803baaaa10019c764a2ddb6795
4
+ data.tar.gz: ca803853cb9d6ae15934dae390f13eb794640ca9
5
5
  SHA512:
6
- metadata.gz: d69f5b6d5224531c115093013049b1273905fc66a9fd1628d1ecee557a24932fcdac3bf1851b9814415c60596d9be65e93265fce947cdeb3c820b1ff2bf0ea9c
7
- data.tar.gz: be97770e8846a16d83746f28ec00334e7c472d8b19f6eaa54145ab70c181caa264ee21a96211b2c5f658813cdc443f7099d19c6078e1053a5f536bbb6798480b
6
+ metadata.gz: 708e55617a3bafb8e761e6fb64ac71260dc3a532687e168e829a255be508f77174d123ae94298d8f680ee043860d16a6f6f5ccea38bda038e61584477ddb83ff
7
+ data.tar.gz: 058f627a57fda1e6710863aa882ca1b6040d7731b51ba6d2b8ae854c3e0b12e17da7eb90815f2cfe811d7cafecc0a7826e111679807301732cb3ba323f3519d2
data/README.md CHANGED
@@ -13,6 +13,7 @@ provides four 3D math types:
13
13
  - Snow::Vec3
14
14
  - Snow::Vec4
15
15
  - Snow::Quat
16
+ - Snow::Mat3
16
17
  - Snow::Mat4
17
18
 
18
19
  _Most_ of their functionality is implemented in the C bindings, particularly
@@ -159,40 +160,47 @@ obvious but are as follows:
159
160
 
160
161
  def inverse(output = nil) -> new vec3 or output
161
162
  def inverse! -> self
163
+ ~vec3 -> new vec3
162
164
  Returns a vector whose components are the multiplicative inverse of the
163
165
  vector's components.
164
- Aliased as :~
165
166
 
166
167
  def negate(output = nil) -> new vec3 or output
167
168
  def negate! -> self
169
+ -vec3 -> new vec3
168
170
  Returns a vector whose components are the negated form of the vector's
169
171
  components. Beware of -0.0 here.
170
- Aliased as :-@
171
172
 
172
173
  def normalize(output = nil) -> new vec3 or output
173
174
  def normalize!() -> self
174
175
 
175
- def cross_product(rhs, output = nil) -> new vec3 or output
176
- def cross_product!(rhs) -> self
177
-
178
176
  def multiply_vec3(rhs, output = nil) -> new vec3 or output
179
177
  Multiplies both vector's components together and returns the result.
180
178
 
181
179
  def multiply(vec3, output = nil) -> same as multiply_vec3
182
180
  def multiply(scalar, output = nil) -> same as scale
183
181
  def multiply!(rhs) -> same as multiply(rhs, self)
184
- Non-mutable form aliased as :*
182
+ vec3 * rhs -> vec3.multiply(rhs)
185
183
 
186
184
  def add(rhs, output = nil) -> new vec3 or output
187
185
  def add!(rhs)
188
- Aliased as :+
186
+ vec3 + vec3
189
187
 
190
188
  def subtract(rhs, output = nil) -> new vec3 or output
191
189
  def subtract!(rhs) -> self
192
- Aliased as :-
190
+ vec3 - vec3
191
+
192
+ def project(normal, output = nil) -> new vec4 or output
193
+ def reflect(normal, output = nil) -> new vec4 or output
194
+ Returns either the projected or reflected vector, respectively. These
195
+ methods expect the 'normal' argument to be normalized -- that is, they
196
+ will not normalize their input for you.
197
+
198
+ def cross_product(rhs, output = nil) -> new vec3 or output
199
+ def cross_product!(rhs) -> self
200
+ vec3 ^ vec3 -> new vec3
193
201
 
194
202
  def dot_product(rhs) -> Float
195
- Aliased as :**
203
+ vec3 ** vec3 -> float
196
204
 
197
205
  def magnitude_squared -> Float
198
206
  def magnitude -> Float
@@ -202,10 +210,11 @@ obvious but are as follows:
202
210
  of two things, you can skip a call to sqrt and use the squared magnitude.
203
211
 
204
212
  def scale(scalar, output = nil) -> new vec3 or output
213
+ vec3 * scalar
205
214
  def divide(scalar, output = nil) -> new vec3 or output
215
+ vec3 / scalar
206
216
  Multiplies or divides all components of the vector by a scalar. In the case
207
217
  of divide, a scalar of 0 is undefined behaviour.
208
- divide is aliased as :/
209
218
 
210
219
  def size -> size in bytes
211
220
  def length -> length in floats
@@ -259,11 +268,13 @@ component. In addition, it is at times interchangeable with Quat.
259
268
 
260
269
  def inverse(output = nil) -> new vec4 or output
261
270
  def inverse! -> self
271
+ ~vec4 -> new vec4
262
272
  Returns a vector whose components are the multiplicative inverse of the
263
273
  vector's components.
264
274
 
265
275
  def negate(output = nil) -> new vec4 or output
266
276
  def negate! -> self
277
+ -vec4 -> new vec4
267
278
  Returns a vector whose components are the negated form of the vector's
268
279
  components. Beware of -0.0 here.
269
280
 
@@ -272,20 +283,30 @@ component. In addition, it is at times interchangeable with Quat.
272
283
 
273
284
  def multiply_vec4(rhs, output = nil) -> new vec4 or output
274
285
  def multiply_vec4!(rhs) -> self
286
+ vec4 * vec4 -> new vec4
275
287
  Multiplies both vector's components together and returns the result.
276
288
 
277
289
  def multiply(vec4, output = nil) -> same as multiply_vec4
278
290
  def multiply(numeric, output = nil) -> same as scale
279
291
  def multiply!(rhs) -> same as multiply(rhs, self)
280
- Aliased as :*
292
+ vec4 * rhs -> vec4.multiply(rhs)
281
293
 
282
294
  def add(rhs, output = nil) -> new vec4 or output
283
- def add!(rhs)
295
+ def add!(rhs) -> self
296
+ vec4 + vec4 -> new vec4
284
297
 
285
298
  def subtract(rhs, output = nil) -> new vec4 or output
286
299
  def subtract!(rhs) -> self
300
+ vec4 - vec4 -> new vec4
301
+
302
+ def project(normal, output = nil) -> new vec4 or output
303
+ def reflect(normal, output = nil) -> new vec4 or output
304
+ Returns either the projected or reflected vector, respectively. These
305
+ methods expect the 'normal' argument to be normalized -- that is, they
306
+ will not normalize their input for you.
287
307
 
288
308
  def dot_product(rhs) -> Float
309
+ vec4 ** vec4 -> Float
289
310
 
290
311
  def magnitude_squared -> Float
291
312
  def magnitude -> Float
@@ -295,7 +316,9 @@ component. In addition, it is at times interchangeable with Quat.
295
316
  of two things, you can skip a call to sqrt and use the squared magnitude.
296
317
 
297
318
  def scale(scalar, output = nil) -> new vec4 or output
319
+ vec4 * scalar -> new vec4
298
320
  def divide(scalar, output = nil) -> new vec4 or output
321
+ vec4 / scalar -> new vec4
299
322
  Multiplies or divides all components of the vector by a scalar. In the case
300
323
  of divide, a scalar of 0 is undefined behaviour.
301
324
 
@@ -365,11 +388,13 @@ and an attempt to make code more readable.
365
388
 
366
389
  def inverse(output = nil) -> new quat or output
367
390
  def inverse! -> self
391
+ ~quat -> new quat
368
392
  Returns the inverse of this quaternion (or writes the inverse to an output
369
393
  or itself). This is not the same as a vector's inverse -- keep that in mind.
370
394
 
371
395
  def negate(output = nil) -> new quat or output
372
396
  def negate! -> self
397
+ -quat -> new quat
373
398
  Returns a quaternion whose components are the negated form of the vector's
374
399
  components. Beware of -0.0 here. This is the same as negating a vector.
375
400
 
@@ -378,28 +403,34 @@ and an attempt to make code more readable.
378
403
 
379
404
  def multiply_quat(rhs: Quat, output = nil) -> new quat or output
380
405
  def multiply!(rhs: Quat) -> self
406
+ quat * quat -> new quat
381
407
  The first form, multiply_quat, multiplies two quaternions and returns the
382
408
  resulting quaternion. The second form, multiply!, is the same as calling
383
409
  multiply_quat(rhs, self).
384
410
 
385
411
  def mutliply_vec3(rhs: Vec3, output = nil) -> new vec3 or output
412
+ quat * vec3 -> new vec3
386
413
  Multiplies a vec3 by a quat and returns the resulting vec3.
387
414
 
388
415
  def multiply(vec3, output = nil) -> new vec3 or output
389
416
  def multiply(quat, output = nil) -> new quat or output
390
417
  def multiply(numeric, output = nil) -> new quat or output
418
+ quat * rhs -> quat.multiply(rhs)
391
419
  In its first form, it's the same as #multiply_vec3. In its second, it's the
392
420
  same as #multiply_quat. In its third, it's the same as #scale.
393
421
 
394
422
  def add(rhs, output = nil) -> new quat or output
395
423
  def add!(rhs)
424
+ quat + quat -> new quat
396
425
  Same as Vec4#add and Vec4#add!.
397
426
 
398
427
  def subtract(rhs, output = nil) -> new quat or output
399
428
  def subtract!(rhs) -> self
429
+ quat - quat -> new quat
400
430
  Same as Vec4#subtract and Vec4#subtract!.
401
431
 
402
432
  def dot_product(rhs) -> Float
433
+ quat ** quat -> new quat
403
434
  Same as Vec4#dot_product.
404
435
 
405
436
  def magnitude_squared -> Float
@@ -410,7 +441,9 @@ and an attempt to make code more readable.
410
441
  of two things, you can skip a call to sqrt and use the squared magnitude.
411
442
 
412
443
  def scale(scalar, output = nil) -> new quat or output
444
+ quat * scalar -> new quat
413
445
  def divide(scalar, output = nil) -> new quat or output
446
+ quat / scalar -> new quat
414
447
  Multiplies or divides all components of the quaternion by a scalar. In the
415
448
  case of divide, a scalar of 0 is undefined behaviour.
416
449
 
@@ -439,6 +472,78 @@ and an attempt to make code more readable.
439
472
 
440
473
 
441
474
 
475
+ #### Snow::Mat3
476
+
477
+ class Snow::Mat3
478
+
479
+ def self.new() -> identity mat3
480
+ def self.new(m1, m2, m3, ..., m9) -> mat3 with the given components
481
+ def self.new([Vec3, Vec3, Vec3]) -> mat3 with rows defined by the given Vec3s
482
+ def self.new(Quat) -> Mat3 from Quat
483
+ def self.new(Mat3) -> copy of the given Mat3
484
+ def self.new(Mat4) -> copy of the given Mat4 as a Mat3
485
+ Aliased as `[]`, so you can use Mat3[...] to create a new Mat3.
486
+
487
+ def self.angle_axis(angle_degrees, axis: Vec3, output = nil) -> new mat3 or output
488
+ Returns a new rotation matrix for the given angle and axis. The angle is in
489
+ degrees.
490
+
491
+ def set(...) -> self
492
+ Same variations and arguments as self.new(...)
493
+
494
+ def load_identity() -> self
495
+ Resets self to the identity matrix.
496
+
497
+ def to_mat4(output = nil) -> new mat4 or output
498
+
499
+ def copy(output = nil) -> new mat3 or output
500
+ Copies self to the given output matrix.
501
+
502
+ def transpose(output = nil) -> new mat3 or output
503
+ def transpose!() -> self
504
+
505
+ def inverse(output = nil) -> new mat3 or output
506
+ def inverse!() -> self on success, nil on failure
507
+
508
+ def multiply_mat3(rhs, output = nil) -> new mat3 or output
509
+ def multiply_mat3!(rhs) -> self
510
+ Multiplies this and the rhs matrix and returns the result.
511
+
512
+ def rotate_vec3(rhs, output = nil) -> new vec3 or output
513
+ Rotates a vec3 by the matrix and returns the result.
514
+
515
+ def inverse_rotate_vec3(vec3, output = nil) -> new vec3 or output
516
+ Essentially just a convenience function.
517
+
518
+ def multiply(mat3, output = nil) -> same as mutliply_mat3
519
+ def multiply(vec3, output = nil) -> same as mutliply_vec3
520
+ def multiply(numeric, output = nil) -> same as scale(N, N, N, output)
521
+ def multiply!(rhs) -> same as multiply(rhs, self)
522
+ The fourth form, multiply!, will fail if attempting to multiply a vec3.
523
+
524
+ def adjoint(output = nil) -> new mat3 or output
525
+ def adjoint!() -> new mat3 or output
526
+
527
+ def cofactor(output = nil) -> new mat3 or output
528
+ def cofactor!() -> new mat3 or output
529
+
530
+ def determinant -> Float
531
+
532
+ def scale(x, y, z, output = nil) -> new mat3 or output
533
+ def scale!(x, y, z) -> new mat3 or output
534
+ Returns a matrix with its columns scaled by the given components.
535
+
536
+ def set_row3, set_column3(index, V) -> self
537
+ V is a vector with the number of components in the function name. Sets the
538
+ Mat3's row or column to the components of the given Vec3.
539
+
540
+ def get_row3, get_column3(index, output = nil) -> new vec3 for row/column
541
+ Returns a Vec3 for the given row or column in the Mat3.
542
+
543
+ end
544
+
545
+
546
+
442
547
  #### Snow::Mat4
443
548
 
444
549
  class Snow::Mat4
@@ -447,6 +552,7 @@ and an attempt to make code more readable.
447
552
  def self.new(m1, m2, m3, ..., m16) -> mat4 with the given components
448
553
  def self.new([Vec4, Vec4, Vec4, Vec4]) -> mat4 with rows defined by the given Vec4s
449
554
  def self.new(Quat) -> Mat4 from Quat
555
+ def self.new(Mat3) -> copy of the given Mat3 as a Mat4
450
556
  def self.new(Mat4) -> copy of the given Mat4
451
557
  Aliased as `[]`, so you can use Mat4[...] to create a new Mat4.
452
558
 
@@ -474,6 +580,8 @@ and an attempt to make code more readable.
474
580
  def load_identity() -> self
475
581
  Resets self to the identity matrix.
476
582
 
583
+ def to_mat3(output = nil) -> new mat3 or output
584
+
477
585
  def copy(output = nil) -> new mat4 or output
478
586
  Copies self to the given output matrix.
479
587
 
@@ -524,27 +632,28 @@ and an attempt to make code more readable.
524
632
 
525
633
  def scale(x, y, z, output = nil) -> new mat4 or output
526
634
  def scale!(x, y, z) -> new mat4 or output
527
- Returns a matrix with its inner 3x3 matrix's rows scaled by the given
635
+ Returns a matrix with its inner 3x3 matrix's columns scaled by the given
528
636
  components.
529
637
 
530
638
  def set_row3, set_column3, set_row4, set_column4(index, V) -> self
531
- V is a vector with the number of components in the function name. Sets the
532
- Mat4's row or column to the components of the given vec3 or vec4.
639
+ Sets the Mat4's row or column to the components of the given vec3 or vec4.
640
+ V must be a vector with the number of components specified by the function
641
+ name.
533
642
 
534
- def get_row3, get_column3, get_row4, get_column4(index, output = nil) -> row/column
535
- Returns a vector with the number of components as specific by the name for
643
+ def get_row3, get_column3, get_row4, get_column4(index, output = nil) -> new vec3 or vec4 for row/column
644
+ Returns a vector with the number of components as specified by the name for
536
645
  the given row or column in the Mat4.
537
646
 
538
647
  end
539
648
 
540
649
 
541
650
 
542
- #### Snow::Vec3Array, Snow::Vec4Array, Snow::QuatArray, Snow::Mat4Array
651
+ #### Snow::Vec3Array, Snow::Vec4Array, Snow::QuatArray, Snow::Mat3Array, Snow::Mat4Array
543
652
 
544
653
  All typed arrays have the same interface and differ only in the kind of object
545
654
  they contain. As such, this applies to all typed arrays.
546
655
 
547
- class Snow::Vec3Array, Snow::Vec4Array, Snow::QuatArray, Snow::Mat4Array
656
+ class Snow::Vec3Array, Snow::Vec4Array, Snow::QuatArray, Snow::Mat3Array, Snow::Mat4Array
548
657
 
549
658
  def self.new(length) -> new array
550
659
  def self.new(array) -> copy of array
data/ext/extconf.rb CHANGED
File without changes
@@ -0,0 +1,455 @@
1
+ /*
2
+ 3x3 (rotation / scaling) matrix
3
+ Written by Noel Cower
4
+
5
+ See COPYING for license information
6
+ */
7
+
8
+ #define __SNOW__MAT3_C__
9
+
10
+ #include "maths_local.h"
11
+
12
+ #if defined(__cplusplus)
13
+ extern "C"
14
+ {
15
+ #endif /* __cplusplus */
16
+
17
+
18
+
19
+ const mat3_t g_mat3_identity = {
20
+ 1.0, 0.0, 0.0,
21
+ 0.0, 1.0, 0.0,
22
+ 0.0, 0.0, 1.0
23
+ };
24
+
25
+
26
+
27
+ // Rows
28
+ #define S_MR 0
29
+ #define S_MS 3
30
+ #define S_MT 6
31
+
32
+ // row components
33
+ #define S_MR_X 0
34
+ #define S_MR_Y 1
35
+ #define S_MR_Z 2
36
+ #define S_MS_X 3
37
+ #define S_MS_Y 4
38
+ #define S_MS_Z 5
39
+ #define S_MT_X 6
40
+ #define S_MT_Y 7
41
+ #define S_MT_Z 8
42
+
43
+ // columns
44
+ #define S_MIR_X 0
45
+ #define S_MIR_Y 3
46
+ #define S_MIR_Z 6
47
+ #define S_MIS_X 1
48
+ #define S_MIS_Y 4
49
+ #define S_MIS_Z 7
50
+ #define S_MIT_X 2
51
+ #define S_MIT_Y 5
52
+ #define S_MIT_Z 8
53
+
54
+
55
+
56
+ void mat3_identity(mat3_t out)
57
+ {
58
+ out[0] =
59
+ out[4] =
60
+ out[8] = 1;
61
+
62
+ out[1] =
63
+ out[2] =
64
+ out[3] =
65
+ out[5] =
66
+ out[6] =
67
+ out[7] = 0;
68
+ }
69
+
70
+
71
+
72
+ void mat3_to_mat4(const mat3_t in, mat4_t out)
73
+ {
74
+ out[0] = in[0];
75
+ out[1] = in[1];
76
+ out[2] = in[2];
77
+ out[3] = 0;
78
+ out[4] = in[3];
79
+ out[5] = in[4];
80
+ out[6] = in[5];
81
+ out[7] = 0;
82
+ out[8] = in[6];
83
+ out[9] = in[7];
84
+ out[10] = in[8];
85
+ out[14] =
86
+ out[13] =
87
+ out[12] =
88
+ out[11] = 0;
89
+ out[15] = 1;
90
+ }
91
+
92
+
93
+
94
+ void mat3_set(s_float_t m00, s_float_t m10, s_float_t m20,
95
+ s_float_t m01, s_float_t m11, s_float_t m21,
96
+ s_float_t m02, s_float_t m12, s_float_t m22,
97
+ mat3_t out)
98
+ {
99
+ out[0] = m00;
100
+ out[1] = m10;
101
+ out[2] = m20;
102
+ out[3] = m01;
103
+ out[4] = m11;
104
+ out[5] = m21;
105
+ out[6] = m02;
106
+ out[7] = m12;
107
+ out[8] = m22;
108
+ }
109
+
110
+
111
+
112
+ void mat3_rotation(s_float_t angle, s_float_t x, s_float_t y, s_float_t z, mat3_t out)
113
+ {
114
+ const s_float_t angle_rad = angle * S_DEG2RAD;
115
+ const s_float_t c = s_cos(angle_rad);
116
+ const s_float_t s = s_sin(angle_rad);
117
+ const s_float_t ic = 1.0 - c;
118
+ const s_float_t xy = x * y * ic;
119
+ const s_float_t yz = y * z * ic;
120
+ const s_float_t xz = x * z * ic;
121
+ const s_float_t xs = s * x;
122
+ const s_float_t ys = s * y;
123
+ const s_float_t zs = s * z;
124
+
125
+ out[0] = ((x * x) * ic) + c;
126
+ out[1] = xy + zs;
127
+ out[2] = xz - ys;
128
+ out[3] = xy - zs;
129
+ out[4] = ((y * y) * ic) + c;
130
+ out[5] = yz + xs;
131
+ out[6] = xz + ys;
132
+ out[7] = yz - xs;
133
+ out[8] = ((z * z) * ic) + c;
134
+ }
135
+
136
+
137
+
138
+ void mat3_from_quat(const quat_t in, mat3_t out)
139
+ {
140
+ s_float_t tx, ty, tz, xx, xy, xz, yy, yz, zz, wx, wy, wz;
141
+
142
+ tx = 2.0 * in[0];
143
+ ty = 2.0 * in[1];
144
+ tz = 2.0 * in[2];
145
+
146
+ xx = tx * in[0];
147
+ xy = tx * in[1];
148
+ xz = tx * in[2];
149
+
150
+ yy = ty * in[1];
151
+ yz = tz * in[1];
152
+
153
+ zz = tz * in[3];
154
+
155
+ wx = tx * in[3];
156
+ wy = ty * in[3];
157
+ wz = tz * in[3];
158
+
159
+ out[0] = 1.0 - (yy + zz);
160
+ out[1] = xy - wz;
161
+ out[2] = xz + wy;
162
+ out[3] = xy + wz;
163
+ out[4] = 1.0 - (xx + zz);
164
+ out[5] = yz - wx;
165
+ out[6] = xz - wy;
166
+ out[7] = yz + wx;
167
+ out[8] = 1.0 - (xx + yy);
168
+ }
169
+
170
+
171
+
172
+ void mat3_transpose(const mat3_t in, mat3_t out)
173
+ {
174
+ s_float_t temp;
175
+ #define S_SWAP_ELEM(LHS, RHS) temp = in[LHS]; out[LHS] = in[RHS]; out[RHS] = temp
176
+ out[0] = in[0];
177
+ out[4] = in[4];
178
+ out[8] = in[8];
179
+ S_SWAP_ELEM(1, 3);
180
+ S_SWAP_ELEM(2, 6);
181
+ S_SWAP_ELEM(5, 7);
182
+ #undef S_SWAP_ELEM
183
+ }
184
+
185
+
186
+
187
+ void mat3_copy(const mat3_t in, mat3_t out)
188
+ {
189
+ out[0] = in[0];
190
+ out[1] = in[1];
191
+ out[2] = in[2];
192
+ out[3] = in[3];
193
+ out[4] = in[4];
194
+ out[5] = in[5];
195
+ out[6] = in[6];
196
+ out[7] = in[7];
197
+ out[8] = in[8];
198
+ }
199
+
200
+
201
+
202
+ int mat3_equals(const mat3_t lhs, const mat3_t rhs)
203
+ {
204
+ return
205
+ float_equals(rhs[0], lhs[0]) &&
206
+ float_equals(rhs[1], lhs[1]) &&
207
+ float_equals(rhs[2], lhs[2]) &&
208
+ float_equals(rhs[3], lhs[3]) &&
209
+ float_equals(rhs[4], lhs[4]) &&
210
+ float_equals(rhs[5], lhs[5]) &&
211
+ float_equals(rhs[6], lhs[6]) &&
212
+ float_equals(rhs[7], lhs[7]) &&
213
+ float_equals(rhs[8], lhs[8]);
214
+ }
215
+
216
+
217
+
218
+ void mat3_scale(const mat3_t in, s_float_t x, s_float_t y, s_float_t z, mat3_t out)
219
+ {
220
+ out[0] = in[0] * x;
221
+ out[1] = in[1] * y;
222
+ out[2] = in[2] * z;
223
+ out[3] = in[3] * x;
224
+ out[4] = in[4] * y;
225
+ out[5] = in[5] * z;
226
+ out[6] = in[6] * x;
227
+ out[7] = in[7] * y;
228
+ out[8] = in[8] * z;
229
+ }
230
+
231
+
232
+
233
+ void mat3_orthogonal(const mat3_t in, mat3_t out)
234
+ {
235
+ vec3_normalize(&in[S_MT], &out[S_MT]);
236
+ vec3_cross_product(&in[S_MS], &in[S_MT], &out[S_MR]);
237
+ vec3_normalize(&in[S_MR], &out[S_MR]);
238
+ vec3_cross_product(&in[S_MT], &in[S_MR], &out[S_MS]);
239
+ }
240
+
241
+
242
+
243
+ void mat3_multiply(const mat3_t lhs, const mat3_t rhs, mat3_t out)
244
+ {
245
+ mat3_t temp;
246
+ s_float_t cx, cy, cz;
247
+
248
+ mat3_transpose(lhs, temp);
249
+
250
+ cx = temp[0];
251
+ cy = temp[1];
252
+ cz = temp[2];
253
+
254
+ temp[0] = (cx * rhs[0 ]) + (cy * rhs[1 ]) + (cz * rhs[2 ]);
255
+ temp[1] = (cx * rhs[3 ]) + (cy * rhs[4 ]) + (cz * rhs[5 ]);
256
+ temp[2] = (cx * rhs[6 ]) + (cy * rhs[7 ]) + (cz * rhs[8]);
257
+
258
+ cx = temp[3];
259
+ cy = temp[4];
260
+ cz = temp[5];
261
+
262
+ temp[3] = (cx * rhs[0 ]) + (cy * rhs[1 ]) + (cz * rhs[2 ]);
263
+ temp[4] = (cx * rhs[3 ]) + (cy * rhs[4 ]) + (cz * rhs[5 ]);
264
+ temp[5] = (cx * rhs[6 ]) + (cy * rhs[7 ]) + (cz * rhs[8]);
265
+
266
+ cx = temp[6];
267
+ cy = temp[7];
268
+ cz = temp[8];
269
+
270
+ temp[6] = (cx * rhs[0 ]) + (cy * rhs[1 ]) + (cz * rhs[2 ]);
271
+ temp[7] = (cx * rhs[3 ]) + (cy * rhs[4 ]) + (cz * rhs[5 ]);
272
+ temp[8] = (cx * rhs[6 ]) + (cy * rhs[7 ]) + (cz * rhs[8]);
273
+
274
+ mat3_transpose(temp, out);
275
+ }
276
+
277
+
278
+
279
+ void mat3_rotate_vec3(const mat3_t lhs, const vec3_t rhs, vec3_t out)
280
+ {
281
+ mat3_t temp;
282
+ s_float_t x;
283
+ s_float_t y;
284
+ s_float_t z;
285
+
286
+ mat3_transpose(lhs, temp);
287
+
288
+ x = temp[0] * rhs[0] + temp[1] * rhs[1] + temp[2] * rhs[2];
289
+ y = temp[3] * rhs[0] + temp[4] * rhs[1] + temp[5] * rhs[2];
290
+ z = temp[6] * rhs[0] + temp[7] * rhs[1] + temp[8] * rhs[2];
291
+
292
+ out[0] = x;
293
+ out[1] = y;
294
+ out[2] = z;
295
+ }
296
+
297
+
298
+
299
+ void mat3_inv_rotate_vec3(const mat3_t lhs, const vec3_t rhs, vec3_t out)
300
+ {
301
+ s_float_t x;
302
+ s_float_t y;
303
+ s_float_t z;
304
+
305
+ x = lhs[0] * rhs[0] + lhs[1] * rhs[1] + lhs[2] * rhs[2];
306
+ y = lhs[3] * rhs[0] + lhs[4] * rhs[1] + lhs[5] * rhs[2];
307
+ z = lhs[6] * rhs[0] + lhs[7] * rhs[1] + lhs[8] * rhs[2];
308
+
309
+ out[0] = x;
310
+ out[1] = y;
311
+ out[2] = z;
312
+ }
313
+
314
+
315
+
316
+ void mat3_cofactor(const mat3_t in, mat3_t out)
317
+ {
318
+ const mat3_t temp = {
319
+ (in[S_MS_Y] * in[S_MT_Z] - in[S_MS_Z] * in[S_MT_Y]),
320
+ -(in[S_MS_X] * in[S_MT_Z] - in[S_MS_Z] * in[S_MT_X]),
321
+ (in[S_MS_X] * in[S_MT_Y] - in[S_MS_Y] * in[S_MT_X]),
322
+
323
+ -(in[S_MR_Y] * in[S_MT_Z] - in[S_MR_Z] * in[S_MT_Y]),
324
+ (in[S_MR_X] * in[S_MT_Z] - in[S_MR_Z] * in[S_MT_X]),
325
+ -(in[S_MR_X] * in[S_MT_Y] - in[S_MR_Y] * in[S_MT_X]),
326
+
327
+ (in[S_MR_Y] * in[S_MS_Z] - in[S_MR_Z] * in[S_MS_Y]),
328
+ -(in[S_MR_X] * in[S_MS_Z] - in[S_MR_Z] * in[S_MS_X]),
329
+ (in[S_MR_X] * in[S_MS_Y] - in[S_MR_Y] * in[S_MS_X])
330
+ };
331
+ mat3_copy(temp, out);
332
+ }
333
+
334
+
335
+
336
+ s_float_t mat3_determinant(const mat3_t in)
337
+ {
338
+ return in[S_MR_X] * (in[S_MS_Y] * in[S_MT_Z] - in[S_MS_Z] * in[S_MT_Y]) +
339
+ in[S_MR_Y] * (in[S_MS_Z] * in[S_MT_X] - in[S_MS_X] * in[S_MT_Z]) +
340
+ in[S_MR_Z] * (in[S_MS_X] * in[S_MT_Y] - in[S_MS_Y] * in[S_MT_X]);
341
+ }
342
+
343
+
344
+
345
+ void mat3_adjoint(const mat3_t in, mat3_t out)
346
+ {
347
+ mat3_cofactor(in, out);
348
+ mat3_transpose(out, out);
349
+ }
350
+
351
+
352
+
353
+ int mat3_inverse(const mat3_t in, mat3_t out)
354
+ {
355
+ s_float_t determinant = mat3_determinant(in);
356
+ if (determinant != 0.0 && determinant != -0.0) {
357
+ determinant = 1.0 / determinant;
358
+ } else {
359
+ return 0;
360
+ }
361
+
362
+ mat3_cofactor(in, out);
363
+ mat3_transpose(out, out);
364
+ out[0] *= determinant;
365
+ out[1] *= determinant;
366
+ out[2] *= determinant;
367
+ out[3] *= determinant;
368
+ out[4] *= determinant;
369
+ out[5] *= determinant;
370
+ out[6] *= determinant;
371
+ out[7] *= determinant;
372
+ out[8] *= determinant;
373
+
374
+ return 1;
375
+ }
376
+
377
+
378
+
379
+ void mat3_get_row3(const mat3_t in, int row, vec3_t out)
380
+ {
381
+ const s_float_t *p = &in[0];
382
+ switch (row) {
383
+ case 2: { p += 3; }
384
+ case 1: { p += 3; }
385
+ case 0: {
386
+ out[0] = in[0];
387
+ out[1] = in[1];
388
+ out[2] = in[2];
389
+ break;
390
+ }
391
+ default: { break; }
392
+ }
393
+ }
394
+
395
+
396
+
397
+ void mat3_get_column3(const mat3_t in, int column, vec3_t out)
398
+ {
399
+ const s_float_t *p = &in[0];
400
+ switch (column) {
401
+ case 2: { ++p; }
402
+ case 1: { ++p; }
403
+ case 0: {
404
+ out[0] = in[0];
405
+ out[1] = in[3];
406
+ out[2] = in[6];
407
+ break;
408
+ }
409
+ default: { break; }
410
+ }
411
+ }
412
+
413
+
414
+
415
+ void mat3_set_row3(int row, const vec3_t in, mat3_t out)
416
+ {
417
+ const s_float_t *p = &out[0];
418
+ switch (row) {
419
+ case 2: { p += 3; }
420
+ case 1: { p += 3; }
421
+ case 0: {
422
+ out[0] = in[0];
423
+ out[1] = in[1];
424
+ out[2] = in[2];
425
+ break;
426
+ }
427
+ default: { break; }
428
+ }
429
+ }
430
+
431
+
432
+
433
+ void mat3_set_column3(int column, const vec3_t in, mat3_t out)
434
+ {
435
+ const s_float_t *p = &out[0];
436
+ switch (column) {
437
+ case 2: { ++p; }
438
+ case 1: { ++p; }
439
+ case 0: {
440
+ out[0] = in[0];
441
+ out[3] = in[1];
442
+ out[6] = in[2];
443
+ break;
444
+ }
445
+ default: { break; }
446
+ }
447
+ }
448
+
449
+
450
+
451
+
452
+
453
+ #if defined(__cplusplus)
454
+ }
455
+ #endif