-
Notifications
You must be signed in to change notification settings - Fork 7
/
Mat2.hx
364 lines (314 loc) · 7.56 KB
/
Mat2.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
#if (vector_math_f32 && (cpp || hl || cs || java))
// override Float (usually f64) type with f32
@:eager private typedef Float = Single;
#end
abstract Mat2(Mat2Data) from Mat2Data to Mat2Data {
#if !macro
public inline function new(
a00: Float, a01: Float,
a10: Float, a11: Float
) {
this = new Mat2Data(
a00, a01,
a10, a11
);
}
public inline function set(
a00: Float, a01: Float,
a10: Float, a11: Float
) {
this.c0.set(a00, a01);
this.c1.set(a10, a11);
}
public inline function copyFrom(v: Mat2) {
var v: Mat2Data = v;
this.c0.copyFrom(v.c0);
this.c1.copyFrom(v.c1);
return this;
}
public inline function clone(): Mat2 {
return new Mat2(
this.c0.x, this.c0.y,
this.c1.x, this.c1.y
);
}
public inline function matrixCompMult(n: Mat2): Mat2 {
var n: Mat2Data = n;
return new Mat2(
this.c0.x * n.c0.x, this.c0.y * n.c0.y,
this.c1.x * n.c1.x, this.c1.y * n.c1.y
);
}
// extended methods
public inline function transpose(): Mat2 {
return new Mat2(
this.c0.x, this.c1.x,
this.c0.y, this.c1.y
);
}
public inline function determinant(): Float {
var m = this;
return m.c0.x * m.c1.y - m.c1.x * m.c0.y;
}
public inline function inverse(): Mat2 {
var m = this;
var f = 1.0 / determinant();
return new Mat2(
m.c1.y * f, -m.c0.y * f,
-m.c1.x * f, m.c0.x * f
);
}
public inline function adjoint(): Mat2 {
var m = this;
return new Mat2(
m.c1.y, -m.c0.y,
-m.c1.x, m.c0.x
);
}
public inline function toString() {
return 'mat2(' +
'${this.c0.x}, ${this.c0.y}, ' +
'${this.c1.x}, ${this.c1.y}' +
')';
}
@:op([])
inline function arrayRead(i: Int)
return switch i {
case 0: this.c0;
case 1: this.c1;
default: null;
}
@:op([])
inline function arrayWrite(i: Int, v: Vec2)
return switch i {
case 0: this.c0.copyFrom(v);
case 1: this.c1.copyFrom(v);
default: null;
}
@:op(-a)
static inline function neg(m: Mat2) {
var m: Mat2Data = m;
return new Mat2(
-m.c0.x, -m.c0.y,
-m.c1.x, -m.c1.y
);
}
@:op(++a)
static inline function prefixIncrement(m: Mat2) {
var _m: Mat2Data = m;
++_m.c0.x; ++_m.c0.y;
++_m.c1.x; ++_m.c1.y;
return m.clone();
}
@:op(--a)
static inline function prefixDecrement(m: Mat2) {
var _m: Mat2Data = m;
--_m.c0.x; --_m.c0.y;
--_m.c1.x; --_m.c1.y;
return m.clone();
}
@:op(a++)
static inline function postfixIncrement(m: Mat2) {
var ret = m.clone();
var m: Mat2Data = m;
++m.c0.x; ++m.c0.y;
++m.c1.x; ++m.c1.y;
return ret;
}
@:op(a--)
static inline function postfixDecrement(m: Mat2) {
var ret = m.clone();
var m: Mat2Data = m;
--m.c0.x; --m.c0.y;
--m.c1.x; --m.c1.y;
return ret;
}
// assignment overload should come before other binary ops to ensure they have priority
@:op(a *= b)
static inline function mulEq(a: Mat2, b: Mat2): Mat2
return a.copyFrom(a * b);
@:op(a *= b)
static inline function mulEqScalar(a: Mat2, f: Float): Mat2
return a.copyFrom(a * f);
@:op(a /= b)
static inline function divEq(a: Mat2, b: Mat2): Mat2
return a.copyFrom(a / b);
@:op(a /= b)
static inline function divEqScalar(a: Mat2, f: Float): Mat2
return a.copyFrom(a / f);
@:op(a += b)
static inline function addEq(a: Mat2, b: Mat2): Mat2
return a.copyFrom(a + b);
@:op(a += b)
static inline function addEqScalar(a: Mat2, f: Float): Mat2
return a.copyFrom(a + f);
@:op(a -= b)
static inline function subEq(a: Mat2, b: Mat2): Mat2
return a.copyFrom(a - b);
@:op(a -= b)
static inline function subEqScalar(a: Mat2, f: Float): Mat2
return a.copyFrom(a - f);
@:op(a + b)
static inline function add(m: Mat2, n: Mat2): Mat2 {
var m: Mat2Data = m;
var n: Mat2Data = n;
return new Mat2(
m.c0.x + n.c0.x, m.c0.y + n.c0.y,
m.c1.x + n.c1.x, m.c1.y + n.c1.y
);
}
@:op(a + b) @:commutative
static inline function addScalar(m: Mat2, f: Float): Mat2 {
var m: Mat2Data = m;
return new Mat2(
m.c0.x + f, m.c0.y + f,
m.c1.x + f, m.c1.y + f
);
}
@:op(a - b)
static inline function sub(m: Mat2, n: Mat2): Mat2 {
var m: Mat2Data = m;
var n: Mat2Data = n;
return new Mat2(
m.c0.x - n.c0.x, m.c0.y - n.c0.y,
m.c1.x - n.c1.x, m.c1.y - n.c1.y
);
}
@:op(a - b)
static inline function subScalar(m: Mat2, f: Float): Mat2 {
var m: Mat2Data = m;
return new Mat2(
m.c0.x - f, m.c0.y - f,
m.c1.x - f, m.c1.y - f
);
}
@:op(a - b)
static inline function subScalarInv(f: Float, m: Mat2): Mat2 {
var m: Mat2Data = m;
return new Mat2(
f - m.c0.x, f - m.c0.y,
f - m.c1.x, f - m.c1.y
);
}
@:op(a * b)
static inline function mul(m: Mat2, n: Mat2): Mat2 {
var m: Mat2Data = m;
var n: Mat2Data = n;
return new Mat2(
m.c0.x * n.c0.x + m.c1.x * n.c0.y,
m.c0.y * n.c0.x + m.c1.y * n.c0.y,
m.c0.x * n.c1.x + m.c1.x * n.c1.y,
m.c0.y * n.c1.x + m.c1.y * n.c1.y
);
}
@:op(a * b)
static inline function postMulVec2(m: Mat2, v: Vec2): Vec2 {
var m: Mat2Data = m;
return new Vec2(
m.c0.x * v.x + m.c1.x * v.y,
m.c0.y * v.x + m.c1.y * v.y
);
}
@:op(a * b)
static inline function preMulVec2(v: Vec2, m: Mat2): Vec2 {
var m: Mat2Data = m;
return new Vec2(
v.dot(m.c0),
v.dot(m.c1)
);
}
@:op(a * b) @:commutative
static inline function mulScalar(m: Mat2, f: Float): Mat2 {
var m: Mat2Data = m;
return new Mat2(
m.c0.x * f, m.c0.y * f,
m.c1.x * f, m.c1.y * f
);
}
@:op(a / b)
static inline function div(m: Mat2, n: Mat2): Mat2 {
return m.matrixCompMult(1.0 / n);
}
@:op(a / b)
static inline function divScalar(m: Mat2, f: Float): Mat2 {
var m: Mat2Data = m;
return new Mat2(
m.c0.x / f, m.c0.y / f,
m.c1.x / f, m.c1.y / f
);
}
@:op(a / b)
static inline function divScalarInv(f: Float, m: Mat2): Mat2 {
var m: Mat2Data = m;
return new Mat2(
f / m.c0.x, f / m.c0.y,
f / m.c1.x, f / m.c1.y
);
}
@:op(a == b)
static inline function equal(m: Mat2, n: Mat2): Bool {
var m: Mat2Data = m;
var n: Mat2Data = n;
return
m.c0 == n.c0 &&
m.c1 == n.c1;
}
@:op(a != b)
static inline function notEqual(m: Mat2, n: Mat2): 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<Mat2>, 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 + 2);
array;
}
}
/**
Copies matrix elements in column-major order from a type that supports array-read access
**/
@:overload(function<T>(arrayLike: T, index: Int): Mat2 {})
public macro function copyFromArray(self: haxe.macro.Expr.ExprOf<Mat2>, 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 + 2);
self;
}
}
// static macros
@:overload(function<T>(arrayLike: T, index: Int): T {})
public static macro function fromArray(array: ExprOf<ArrayAccess<Float>>, index: ExprOf<Int>): ExprOf<Mat2> {
return macro {
var array = $array;
var i: Int = $index;
new Mat2(
array[0 + i], array[1 + i],
array[2 + i], array[3 + i]
);
}
}
}
@:noCompletion
class Mat2Data {
#if !macro
public var c0: Vec2;
public var c1: Vec2;
public inline function new(
a00: Float, a01: Float,
a10: Float, a11: Float
) {
c0 = new Vec2(a00, a01);
c1 = new Vec2(a10, a11);
}
#end
}