-
Notifications
You must be signed in to change notification settings - Fork 7
/
Vec4.hx
519 lines (471 loc) · 12.1 KB
/
Vec4.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
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
#if macro
import haxe.macro.Expr.ExprOf;
#end
#if (vector_math_f32 && (cpp || hl || cs || java))
// override Float (usually f64) type with f32
@:eager private typedef Float = Single;
#end
@:nullSafety
#if !macro @:build(VectorMath.Swizzle.generateFields(4)) #end
abstract Vec4(Vec4Data) to Vec4Data from Vec4Data {
#if !macro
public var x (get, set): Float;
inline function get_x() return this.x;
inline function set_x(v: Float) return this.x = v;
public var y (get, set): Float;
inline function get_y() return this.y;
inline function set_y(v: Float) return this.y = v;
public var z (get, set): Float;
inline function get_z() return this.z;
inline function set_z(v: Float) return this.z = v;
public var w (get, set): Float;
inline function get_w() return this.w;
inline function set_w(v: Float) return this.w = v;
public inline function new(x: Float, y: Float, z: Float, w: Float) {
this = new Vec4Data(x, y, z, w);
}
public inline function set(x: Float, y: Float, z: Float, w: Float) {
this.x = x;
this.y = y;
this.z = z;
this.w = w;
}
public inline function clone() {
return new Vec4(x, y, z, w);
}
// Trigonometric
public inline function radians(): Vec4 {
return (this: Vec4) * Math.PI / 180;
}
public inline function degrees(): Vec4 {
return (this: Vec4) * 180 / Math.PI;
}
public inline function sin(): Vec4 {
return new Vec4(
Math.sin(x),
Math.sin(y),
Math.sin(z),
Math.sin(w)
);
}
public inline function cos(): Vec4 {
return new Vec4(
Math.cos(x),
Math.cos(y),
Math.cos(z),
Math.cos(w)
);
}
public inline function tan(): Vec4 {
return new Vec4(
Math.tan(x),
Math.tan(y),
Math.tan(z),
Math.tan(w)
);
}
public inline function asin(): Vec4 {
return new Vec4(
Math.asin(x),
Math.asin(y),
Math.asin(z),
Math.asin(w)
);
}
public inline function acos(): Vec4 {
return new Vec4(
Math.acos(x),
Math.acos(y),
Math.acos(z),
Math.acos(w)
);
}
public inline function atan(): Vec4 {
return new Vec4(
Math.atan(x),
Math.atan(y),
Math.atan(z),
Math.atan(w)
);
}
public inline function atan2(b: Vec4): Vec4 {
return new Vec4(
Math.atan2(x, b.x),
Math.atan2(y, b.y),
Math.atan2(z, b.z),
Math.atan2(w, b.w)
);
}
// Exponential
public inline function pow(e: Vec4): Vec4 {
return new Vec4(
Math.pow(x, e.x),
Math.pow(y, e.y),
Math.pow(z, e.z),
Math.pow(w, e.w)
);
}
public inline function exp(): Vec4 {
return new Vec4(
Math.exp(x),
Math.exp(y),
Math.exp(z),
Math.exp(w)
);
}
public inline function log(): Vec4 {
return new Vec4(
Math.log(x),
Math.log(y),
Math.log(z),
Math.log(w)
);
}
public inline function exp2(): Vec4 {
return new Vec4(
Math.pow(2, x),
Math.pow(2, y),
Math.pow(2, z),
Math.pow(2, w)
);
}
public inline function log2(): Vec4 @:privateAccess {
return new Vec4(
VectorMath.log2f(x),
VectorMath.log2f(y),
VectorMath.log2f(z),
VectorMath.log2f(w)
);
}
public inline function sqrt(): Vec4 {
return new Vec4(
Math.sqrt(x),
Math.sqrt(y),
Math.sqrt(z),
Math.sqrt(w)
);
}
public inline function inverseSqrt(): Vec4 {
return 1.0 / sqrt();
}
// Common
public inline function abs(): Vec4 {
return new Vec4(
Math.abs(x),
Math.abs(y),
Math.abs(z),
Math.abs(w)
);
}
public inline function sign(): Vec4 {
return new Vec4(
x > 0. ? 1. : (x < 0. ? -1. : 0.),
y > 0. ? 1. : (y < 0. ? -1. : 0.),
z > 0. ? 1. : (z < 0. ? -1. : 0.),
w > 0. ? 1. : (w < 0. ? -1. : 0.)
);
}
public inline function floor(): Vec4 {
return new Vec4(
Math.floor(x),
Math.floor(y),
Math.floor(z),
Math.floor(w)
);
}
public inline function ceil(): Vec4 {
return new Vec4(
Math.ceil(x),
Math.ceil(y),
Math.ceil(z),
Math.ceil(w)
);
}
public inline function fract(): Vec4 {
return (this: Vec4) - floor();
}
public extern overload inline function mod(d: Float): Vec4 {
return (this: Vec4) - d * ((this: Vec4) / d).floor();
}
public extern overload inline function mod(d: Vec4): Vec4 {
return (this: Vec4) - d * ((this: Vec4) / d).floor();
}
public extern overload inline function min(b: Vec4): Vec4 {
return new Vec4(
b.x < x ? b.x : x,
b.y < y ? b.y : y,
b.z < z ? b.z : z,
b.w < w ? b.w : w
);
}
public extern overload inline function min(b: Float): Vec4 {
return new Vec4(
b < x ? b : x,
b < y ? b : y,
b < z ? b : z,
b < w ? b : w
);
}
public extern overload inline function max(b: Vec4): Vec4 {
return new Vec4(
x < b.x ? b.x : x,
y < b.y ? b.y : y,
z < b.z ? b.z : z,
w < b.w ? b.w : w
);
}
public extern overload inline function max(b: Float): Vec4 {
return new Vec4(
x < b ? b : x,
y < b ? b : y,
z < b ? b : z,
w < b ? b : w
);
}
public extern overload inline function clamp(minLimit: Vec4, maxLimit: Vec4) {
return max(minLimit).min(maxLimit);
}
public extern overload inline function clamp(minLimit: Float, maxLimit: Float) {
return max(minLimit).min(maxLimit);
}
public extern overload inline function mix(b: Vec4, t: Vec4): Vec4 {
return (this: Vec4) * (1.0 - t) + b * t;
}
public extern overload inline function mix(b: Vec4, t: Float): Vec4 {
return (this: Vec4) * (1.0 - t) + b * t;
}
public extern overload inline function step(edge: Vec4): Vec4 {
return new Vec4(
x < edge.x ? 0.0 : 1.0,
y < edge.y ? 0.0 : 1.0,
z < edge.z ? 0.0 : 1.0,
w < edge.w ? 0.0 : 1.0
);
}
public extern overload inline function step(edge: Float): Vec4 {
return new Vec4(
x < edge ? 0.0 : 1.0,
y < edge ? 0.0 : 1.0,
z < edge ? 0.0 : 1.0,
w < edge ? 0.0 : 1.0
);
}
public extern overload inline function smoothstep(edge0: Vec4, edge1: Vec4): Vec4 {
var t = (((this: Vec4) - edge0) / (edge1 - edge0)).clamp(0, 1);
return t * t * (3.0 - 2.0 * t);
}
public extern overload inline function smoothstep(edge0: Float, edge1: Float): Vec4 {
var t = (((this: Vec4) - edge0) / (edge1 - edge0)).clamp(0, 1);
return t * t * (3.0 - 2.0 * t);
}
// Geometric
public inline function length(): Float {
return Math.sqrt(x*x + y*y + z*z + w*w);
}
public inline function distance(b: Vec4): Float {
return (b - this).length();
}
public inline function dot(b: Vec4): Float {
return x * b.x + y * b.y + z * b.z + w * b.w;
}
public inline function normalize(): Vec4 {
var v: Vec4 = this;
var lenSq = v.dot(this);
var denominator = lenSq == 0.0 ? 1.0 : Math.sqrt(lenSq); // for 0 length, return zero vector rather than infinity
return v / denominator;
}
public inline function faceforward(I: Vec4, Nref: Vec4): Vec4 {
return new Vec4(x, y, z, w) * (Nref.dot(I) < 0 ? 1 : -1);
}
public inline function reflect(N: Vec4): Vec4 {
var I = (this: Vec4);
return I - 2 * N.dot(I) * N;
}
public inline function refract(N: Vec4, eta: Float): Vec4 {
var I = (this: Vec4);
var nDotI = N.dot(I);
var k = 1.0 - eta * eta * (1.0 - nDotI * nDotI);
return
(eta * I - (eta * nDotI + Math.sqrt(k)) * N)
* (k < 0.0 ? 0.0 : 1.0); // if k < 0, result should be 0 vector
}
public inline function toString() {
return 'vec4(${x}, ${y}, ${z}, ${w})';
}
@:op([])
inline function arrayRead(i: Int)
return switch i {
case 0: x;
case 1: y;
case 2: z;
case 3: w;
default: null;
}
@:op([])
inline function arrayWrite(i: Int, v: Float)
return switch i {
case 0: x = v;
case 1: y = v;
case 2: z = v;
case 3: w = v;
default: null;
}
@:op(-a)
static inline function neg(a: Vec4)
return new Vec4(-a.x, -a.y, -a.z, -a.w);
@:op(++a)
static inline function prefixIncrement(a: Vec4) {
++a.x; ++a.y; ++a.z; ++a.w;
return a.clone();
}
@:op(--a)
static inline function prefixDecrement(a: Vec4) {
--a.x; --a.y; --a.z; --a.w;
return a.clone();
}
@:op(a++)
static inline function postfixIncrement(a: Vec4) {
var ret = a.clone();
++a.x; ++a.y; ++a.z; ++a.w;
return ret;
}
@:op(a--)
static inline function postfixDecrement(a: Vec4) {
var ret = a.clone();
--a.x; --a.y; --a.z; --a.w;
return ret;
}
@:op(a * b)
static inline function mul(a: Vec4, b: Vec4): Vec4
return new Vec4(a.x * b.x, a.y * b.y, a.z * b.z, a.w * b.w);
@:op(a * b) @:commutative
static inline function mulScalar(a: Vec4, b: Float): Vec4
return new Vec4(a.x * b, a.y * b, a.z * b, a.w * b);
@:op(a / b)
static inline function div(a: Vec4, b: Vec4): Vec4
return new Vec4(a.x / b.x, a.y / b.y, a.z / b.z, a.w / b.w);
@:op(a / b)
static inline function divScalar(a: Vec4, b: Float): Vec4
return new Vec4(a.x / b, a.y / b, a.z / b, a.w / b);
@:op(a / b)
static inline function divScalarInv(a: Float, b: Vec4): Vec4
return new Vec4(a / b.x, a / b.y, a / b.z, a / b.w);
@:op(a + b)
static inline function add(a: Vec4, b: Vec4): Vec4
return new Vec4(a.x + b.x, a.y + b.y, a.z + b.z, a.w + b.w);
@:op(a + b) @:commutative
static inline function addScalar(a: Vec4, b: Float): Vec4
return new Vec4(a.x + b, a.y + b, a.z + b, a.w + b);
@:op(a - b)
static inline function sub(a: Vec4, b: Vec4): Vec4
return new Vec4(a.x - b.x, a.y - b.y, a.z - b.z, a.w - b.w);
@:op(a - b)
static inline function subScalar(a: Vec4, b: Float): Vec4
return new Vec4(a.x - b, a.y - b, a.z - b, a.w - b);
@:op(b - a)
static inline function subScalarInv(a: Float, b: Vec4): Vec4
return new Vec4(a - b.x, a - b.y, a - b.z, a - b.w);
@:op(a == b)
static inline function equal(a: Vec4, b: Vec4): Bool
return a.x == b.x && a.y == b.y && a.z == b.z && a.w == b.w;
@:op(a != b)
static inline function notEqual(a: Vec4, b: Vec4): Bool
return !equal(a, b);
#end // !macro
// macros
/**
* Copy from any object with .x .y .z .w fields
*/
@:overload(function(source: {x: Float, y: Float, z: Float, w: Float}): Vec4 {})
public macro function copyFrom(self: ExprOf<Vec4>, source: ExprOf<{x: Float, y: Float, z: Float, w: Float}>): ExprOf<Vec4> {
return macro {
var self = $self;
var source = $source;
self.x = source.x;
self.y = source.y;
self.z = source.z;
self.w = source.w;
self;
}
}
/**
* Copy into any object with .x .y .z .w fields
*/
@:overload(function(target: {x: Float, y: Float, z: Float, w: Float}): {x: Float, y: Float, z: Float, w: Float} {})
public macro function copyInto(self: ExprOf<Vec4>, target: ExprOf<{x: Float, y: Float, z: Float}>): ExprOf<{x: Float, y: Float, z: Float}> {
return macro {
var self = $self;
var target = $target;
target.x = self.x;
target.y = self.y;
target.z = self.z;
target.w = self.w;
target;
}
}
@:overload(function<T>(arrayLike: T, index: Int): T {})
public macro function copyIntoArray(self: ExprOf<Vec4>, array: ExprOf<ArrayAccess<Float>>, index: ExprOf<Int>) {
return macro {
var self = $self;
var array = $array;
var i: Int = $index;
array[0 + i] = self.x;
array[1 + i] = self.y;
array[2 + i] = self.z;
array[3 + i] = self.w;
array;
}
}
@:overload(function<T>(arrayLike: T, index: Int): T {})
public macro function copyFromArray(self: ExprOf<Vec4>, array: ExprOf<ArrayAccess<Float>>, index: ExprOf<Int>) {
return macro {
var self = $self;
var array = $array;
var i: Int = $index;
self.x = array[0 + i];
self.y = array[1 + i];
self.z = array[2 + i];
self.w = array[3 + i];
self;
}
}
// static macros
/**
* Create from any object with .x .y .z .w fields
*/
@:overload(function(source: {x: Float, y: Float, z: Float, w: Float}): Vec4 {})
public static macro function from(xyzw: ExprOf<{x: Float, y: Float, z: Float, w: Float}>): ExprOf<Vec4> {
return macro {
var source = $xyzw;
new Vec4(source.x, source.y, source.z, source.w);
}
}
@:overload(function<T>(arrayLike: T, index: Int): T {})
public static macro function fromArray(array: ExprOf<ArrayAccess<Float>>, index: ExprOf<Int>): ExprOf<Vec4> {
return macro {
var array = $array;
var i: Int = $index;
new Vec4(
array[0 + i],
array[1 + i],
array[2 + i],
array[3 + i]
);
}
}
}
@:noCompletion
class Vec4Data {
#if !macro
public var x: Float;
public var y: Float;
public var z: Float;
public var w: Float;
public inline function new(x: Float, y: Float, z: Float, w: Float) {
this.x = x;
this.y = y;
this.z = z;
this.w = w;
}
#end
}