-
Notifications
You must be signed in to change notification settings - Fork 7
/
Mat3.hx
419 lines (366 loc) · 10.2 KB
/
Mat3.hx
1
2
3
4
5
6
7
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
#if (vector_math_f32 && (cpp || hl || cs || java))
// override Float (usually f64) type with f32
@:eager private typedef Float = Single;
#end
abstract Mat3(Mat3Data) from Mat3Data to Mat3Data {
#if !macro
public inline function new(
a00: Float, a01: Float, a02: Float,
a10: Float, a11: Float, a12: Float,
a20: Float, a21: Float, a22: Float
) {
this = new Mat3Data(
a00, a01, a02,
a10, a11, a12,
a20, a21, a22
);
}
public inline function set(
a00: Float, a01: Float, a02: Float,
a10: Float, a11: Float, a12: Float,
a20: Float, a21: Float, a22: Float
) {
this.c0.set(a00, a01, a02);
this.c1.set(a10, a11, a12);
this.c2.set(a20, a21, a22);
}
public inline function copyFrom(v: Mat3) {
var v: Mat3Data = v;
this.c0.copyFrom(v.c0);
this.c1.copyFrom(v.c1);
this.c2.copyFrom(v.c2);
return this;
}
public inline function clone(): Mat3 {
return new Mat3(
this.c0.x, this.c0.y, this.c0.z,
this.c1.x, this.c1.y, this.c1.z,
this.c2.x, this.c2.y, this.c2.z
);
}
public inline function matrixCompMult(n: Mat3): Mat3 {
var n: Mat3Data = n;
return new Mat3(
this.c0.x * n.c0.x, this.c0.y * n.c0.y, this.c0.z * n.c0.z,
this.c1.x * n.c1.x, this.c1.y * n.c1.y, this.c1.z * n.c1.z,
this.c2.x * n.c2.x, this.c2.y * n.c2.y, this.c2.z * n.c2.z
);
}
// extended methods
public inline function transpose(): Mat3 {
return new Mat3(
this.c0.x, this.c1.x, this.c2.x,
this.c0.y, this.c1.y, this.c2.y,
this.c0.z, this.c1.z, this.c2.z
);
}
public inline function determinant(): Float {
var m = this;
return (
m.c0.x * (m.c2.z * m.c1.y - m.c1.z * m.c2.y) +
m.c0.y * (-m.c2.z * m.c1.x + m.c1.z * m.c2.x) +
m.c0.z * (m.c2.y * m.c1.x - m.c1.y * m.c2.x)
);
}
public inline function inverse(): Mat3 {
var m = this;
var b01 = m.c2.z * m.c1.y - m.c1.z * m.c2.y;
var b11 = -m.c2.z * m.c1.x + m.c1.z * m.c2.x;
var b21 = m.c2.y * m.c1.x - m.c1.y * m.c2.x;
// determinant
var det = m.c0.x * b01 + m.c0.y * b11 + m.c0.z * b21;
var f = 1.0 / det;
return new Mat3(
b01 * f,
(-m.c2.z * m.c0.y + m.c0.z * m.c2.y) * f,
(m.c1.z * m.c0.y - m.c0.z * m.c1.y) * f,
b11 * f,
(m.c2.z * m.c0.x - m.c0.z * m.c2.x) * f,
(-m.c1.z * m.c0.x + m.c0.z * m.c1.x) * f,
b21 * f,
(-m.c2.y * m.c0.x + m.c0.y * m.c2.x) * f,
(m.c1.y * m.c0.x - m.c0.y * m.c1.x) * f
);
}
public inline function adjoint(): Mat3 {
var m = this;
return new Mat3(
m.c1.y * m.c2.z - m.c1.z * m.c2.y,
m.c0.z * m.c2.y - m.c0.y * m.c2.z,
m.c0.y * m.c1.z - m.c0.z * m.c1.y,
m.c1.z * m.c2.x - m.c1.x * m.c2.z,
m.c0.x * m.c2.z - m.c0.z * m.c2.x,
m.c0.z * m.c1.x - m.c0.x * m.c1.z,
m.c1.x * m.c2.y - m.c1.y * m.c2.x,
m.c0.y * m.c2.x - m.c0.x * m.c2.y,
m.c0.x * m.c1.y - m.c0.y * m.c1.x
);
}
public inline function toString() {
return 'mat3(' +
'${this.c0.x}, ${this.c0.y}, ${this.c0.z}, ' +
'${this.c1.x}, ${this.c1.y}, ${this.c1.z}, ' +
'${this.c2.x}, ${this.c2.y}, ${this.c2.z}' +
')';
}
@:op([])
inline function arrayRead(i: Int)
return switch i {
case 0: this.c0;
case 1: this.c1;
case 2: this.c2;
default: null;
}
@:op([])
inline function arrayWrite(i: Int, v: Vec3)
return switch i {
case 0: this.c0.copyFrom(v);
case 1: this.c1.copyFrom(v);
case 2: this.c2.copyFrom(v);
default: null;
}
@:op(-a)
static inline function neg(m: Mat3) {
var m: Mat3Data = m;
return new Mat3(
-m.c0.x, -m.c0.y, -m.c0.z,
-m.c1.x, -m.c1.y, -m.c1.z,
-m.c2.x, -m.c2.y, -m.c2.z
);
}
@:op(++a)
static inline function prefixIncrement(m: Mat3) {
var _m: Mat3Data = m;
++_m.c0; ++_m.c1; ++_m.c2;
return m.clone();
}
@:op(--a)
static inline function prefixDecrement(m: Mat3) {
var _m: Mat3Data = m;
--_m.c0; --_m.c1; --_m.c2;
return m.clone();
}
@:op(a++)
static inline function postfixIncrement(m: Mat3) {
var ret = m.clone();
var m: Mat3Data = m;
++m.c0; ++m.c1; ++m.c2;
return ret;
}
@:op(a--)
static inline function postfixDecrement(m: Mat3) {
var ret = m.clone();
var m: Mat3Data = m;
--m.c0; --m.c1; --m.c2;
return ret;
}
// assignment overload should come before other binary ops to ensure they have priority
@:op(a *= b)
static inline function mulEq(a: Mat3, b: Mat3): Mat3
return a.copyFrom(a * b);
@:op(a *= b)
static inline function mulEqScalar(a: Mat3, f: Float): Mat3
return a.copyFrom(a * f);
@:op(a /= b)
static inline function divEq(a: Mat3, b: Mat3): Mat3
return a.copyFrom(a / b);
@:op(a /= b)
static inline function divEqScalar(a: Mat3, f: Float): Mat3
return a.copyFrom(a / f);
@:op(a += b)
static inline function addEq(a: Mat3, b: Mat3): Mat3
return a.copyFrom(a + b);
@:op(a += b)
static inline function addEqScalar(a: Mat3, f: Float): Mat3
return a.copyFrom(a + f);
@:op(a -= b)
static inline function subEq(a: Mat3, b: Mat3): Mat3
return a.copyFrom(a - b);
@:op(a -= b)
static inline function subEqScalar(a: Mat3, f: Float): Mat3
return a.copyFrom(a - f);
@:op(a + b)
static inline function add(m: Mat3, n: Mat3): Mat3 {
var m: Mat3Data = m;
var n: Mat3Data = n;
return new Mat3(
m.c0.x + n.c0.x, m.c0.y + n.c0.y, m.c0.z + n.c0.z,
m.c1.x + n.c1.x, m.c1.y + n.c1.y, m.c1.z + n.c1.z,
m.c2.x + n.c2.x, m.c2.y + n.c2.y, m.c2.z + n.c2.z
);
}
@:op(a + b) @:commutative
static inline function addScalar(m: Mat3, f: Float): Mat3 {
var m: Mat3Data = m;
return new Mat3(
m.c0.x + f, m.c0.y + f, m.c0.z + f,
m.c1.x + f, m.c1.y + f, m.c1.z + f,
m.c2.x + f, m.c2.y + f, m.c2.z + f
);
}
@:op(a - b)
static inline function sub(m: Mat3, n: Mat3): Mat3 {
var m: Mat3Data = m;
var n: Mat3Data = n;
return new Mat3(
m.c0.x - n.c0.x, m.c0.y - n.c0.y, m.c0.z - n.c0.z,
m.c1.x - n.c1.x, m.c1.y - n.c1.y, m.c1.z - n.c1.z,
m.c2.x - n.c2.x, m.c2.y - n.c2.y, m.c2.z - n.c2.z
);
}
@:op(a - b)
static inline function subScalar(m: Mat3, f: Float): Mat3 {
var m: Mat3Data = m;
return new Mat3(
m.c0.x - f, m.c0.y - f, m.c0.z - f,
m.c1.x - f, m.c1.y - f, m.c1.z - f,
m.c2.x - f, m.c2.y - f, m.c2.z - f
);
}
@:op(a - b)
static inline function subScalarInv(f: Float, m: Mat3): Mat3 {
var m: Mat3Data = m;
return new Mat3(
f - m.c0.x, f - m.c0.y, f - m.c0.z,
f - m.c1.x, f - m.c1.y, f - m.c1.z,
f - m.c2.x, f - m.c2.y, f - m.c2.z
);
}
@:op(a * b)
static inline function mul(m: Mat3, n: Mat3): Mat3 {
var m: Mat3Data = m;
var n: Mat3Data = n;
return new Mat3(
m.c0.x * n.c0.x + m.c1.x * n.c0.y + m.c2.x * n.c0.z,
m.c0.y * n.c0.x + m.c1.y * n.c0.y + m.c2.y * n.c0.z,
m.c0.z * n.c0.x + m.c1.z * n.c0.y + m.c2.z * n.c0.z,
m.c0.x * n.c1.x + m.c1.x * n.c1.y + m.c2.x * n.c1.z,
m.c0.y * n.c1.x + m.c1.y * n.c1.y + m.c2.y * n.c1.z,
m.c0.z * n.c1.x + m.c1.z * n.c1.y + m.c2.z * n.c1.z,
m.c0.x * n.c2.x + m.c1.x * n.c2.y + m.c2.x * n.c2.z,
m.c0.y * n.c2.x + m.c1.y * n.c2.y + m.c2.y * n.c2.z,
m.c0.z * n.c2.x + m.c1.z * n.c2.y + m.c2.z * n.c2.z
);
}
@:op(a * b)
static inline function postMulVec3(m: Mat3, v: Vec3): Vec3 {
var m: Mat3Data = m;
return new Vec3(
m.c0.x * v.x + m.c1.x * v.y + m.c2.x * v.z,
m.c0.y * v.x + m.c1.y * v.y + m.c2.y * v.z,
m.c0.z * v.x + m.c1.z * v.y + m.c2.z * v.z
);
}
@:op(a * b)
static inline function preMulVec3(v: Vec3, m: Mat3): Vec3 {
var m: Mat3Data = m;
return new Vec3(
v.dot(m.c0),
v.dot(m.c1),
v.dot(m.c2)
);
}
@:op(a * b) @:commutative
static inline function mulScalar(m: Mat3, f: Float): Mat3 {
var m: Mat3Data = m;
return new Mat3(
m.c0.x * f, m.c0.y * f, m.c0.z * f,
m.c1.x * f, m.c1.y * f, m.c1.z * f,
m.c2.x * f, m.c2.y * f, m.c2.z * f
);
}
@:op(a / b)
static inline function div(m: Mat3, n: Mat3): Mat3
return m.matrixCompMult(1.0 / n);
@:op(a / b)
static inline function divScalar(m: Mat3, f: Float): Mat3 {
var m: Mat3Data = m;
return new Mat3(
m.c0.x / f, m.c0.y / f, m.c0.z / f,
m.c1.x / f, m.c1.y / f, m.c1.z / f,
m.c2.x / f, m.c2.y / f, m.c2.z / f
);
}
@:op(a / b)
static inline function divScalarInv(f: Float, m: Mat3): Mat3 {
var m: Mat3Data = m;
return new Mat3(
f / m.c0.x, f/ m.c0.y, f / m.c0.z,
f / m.c1.x, f/ m.c1.y, f / m.c1.z,
f / m.c2.x, f/ m.c2.y, f / m.c2.z
);
}
@:op(a == b)
static inline function equal(m: Mat3, n: Mat3): Bool {
var m: Mat3Data = m;
var n: Mat3Data = n;
return
m.c0 == n.c0 &&
m.c1 == n.c1 &&
m.c2 == n.c2;
}
@:op(a != b)
static inline function notEqual(m: Mat3, n: Mat3): Bool
return !equal(m, n);
#end // !macro
/**
Copies matrix elements in column-major order into a type that supports array-write access
**/
@:overload(function<T>(arrayLike: T, index: Int): T {})
public macro function copyIntoArray(self: haxe.macro.Expr.ExprOf<Mat3>, array: haxe.macro.Expr.ExprOf<ArrayAccess<Float>>, index: haxe.macro.Expr.ExprOf<Int>) {
return macro {
var self = $self;
var array = $array;
var i: Int = $index;
self[0].copyIntoArray(array, i);
self[1].copyIntoArray(array, i + 3);
self[2].copyIntoArray(array, i + 6);
array;
}
}
/**
Copies matrix elements in column-major order from a type that supports array-read access
**/
@:overload(function<T>(arrayLike: T, index: Int): Mat3 {})
public macro function copyFromArray(self: haxe.macro.Expr.ExprOf<Mat3>, array: haxe.macro.Expr.ExprOf<ArrayAccess<Float>>, index: haxe.macro.Expr.ExprOf<Int>) {
return macro {
var self = $self;
var array = $array;
var i: Int = $index;
self[0].copyFromArray(array, i);
self[1].copyFromArray(array, i + 3);
self[2].copyFromArray(array, i + 6);
self;
}
}
// static macros
@:overload(function<T>(arrayLike: T, index: Int): T {})
public static macro function fromArray(array: ExprOf<ArrayAccess<Float>>, index: ExprOf<Int>): ExprOf<Mat3> {
return macro {
var array = $array;
var i: Int = $index;
new Mat3(
array[0 + i], array[1 + i], array[2 + i],
array[3 + i], array[4 + i], array[5 + i],
array[6 + i], array[7 + i], array[8 + i]
);
}
}
}
@:noCompletion
class Mat3Data {
#if !macro
public var c0: Vec3;
public var c1: Vec3;
public var c2: Vec3;
public inline function new(
a00: Float, a01: Float, a02: Float,
a10: Float, a11: Float, a12: Float,
a20: Float, a21: Float, a22: Float
) {
c0 = new Vec3(a00, a01, a02);
c1 = new Vec3(a10, a11, a12);
c2 = new Vec3(a20, a21, a22);
}
#end
}