-
Notifications
You must be signed in to change notification settings - Fork 7
/
Vec3.hx
490 lines (442 loc) · 11.2 KB
/
Vec3.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
#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(3)) #end
abstract Vec3(Vec3Data) to Vec3Data from Vec3Data {
#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 inline function new(x: Float, y: Float, z: Float) {
this = new Vec3Data(x, y, z);
}
public inline function set(x: Float, y: Float, z: Float) {
this.x = x;
this.y = y;
this.z = z;
}
public inline function clone() {
return new Vec3(x, y, z);
}
// special case for vec3
public inline function cross(b: Vec3)
return new Vec3(
y * b.z - z * b.y,
z * b.x - x * b.z,
x * b.y - y * b.x
);
// Trigonometric
public inline function radians(): Vec3 {
return (this: Vec3) * Math.PI / 180;
}
public inline function degrees(): Vec3 {
return (this: Vec3) * 180 / Math.PI;
}
public inline function sin(): Vec3 {
return new Vec3(
Math.sin(x),
Math.sin(y),
Math.sin(z)
);
}
public inline function cos(): Vec3 {
return new Vec3(
Math.cos(x),
Math.cos(y),
Math.cos(z)
);
}
public inline function tan(): Vec3 {
return new Vec3(
Math.tan(x),
Math.tan(y),
Math.tan(z)
);
}
public inline function asin(): Vec3 {
return new Vec3(
Math.asin(x),
Math.asin(y),
Math.asin(z)
);
}
public inline function acos(): Vec3 {
return new Vec3(
Math.acos(x),
Math.acos(y),
Math.acos(z)
);
}
public inline function atan(): Vec3 {
return new Vec3(
Math.atan(x),
Math.atan(y),
Math.atan(z)
);
}
public inline function atan2(b: Vec3): Vec3 {
return new Vec3(
Math.atan2(x, b.x),
Math.atan2(y, b.y),
Math.atan2(z, b.z)
);
}
// Exponential
public inline function pow(e: Vec3): Vec3 {
return new Vec3(
Math.pow(x, e.x),
Math.pow(y, e.y),
Math.pow(z, e.z)
);
}
public inline function exp(): Vec3 {
return new Vec3(
Math.exp(x),
Math.exp(y),
Math.exp(z)
);
}
public inline function log(): Vec3 {
return new Vec3(
Math.log(x),
Math.log(y),
Math.log(z)
);
}
public inline function exp2(): Vec3 {
return new Vec3(
Math.pow(2, x),
Math.pow(2, y),
Math.pow(2, z)
);
}
public inline function log2(): Vec3 @:privateAccess {
return new Vec3(
VectorMath.log2f(x),
VectorMath.log2f(y),
VectorMath.log2f(z)
);
}
public inline function sqrt(): Vec3 {
return new Vec3(
Math.sqrt(x),
Math.sqrt(y),
Math.sqrt(z)
);
}
public inline function inverseSqrt(): Vec3 {
return 1.0 / sqrt();
}
// Common
public inline function abs(): Vec3 {
return new Vec3(
Math.abs(x),
Math.abs(y),
Math.abs(z)
);
}
public inline function sign(): Vec3 {
return new Vec3(
x > 0. ? 1. : (x < 0. ? -1. : 0.),
y > 0. ? 1. : (y < 0. ? -1. : 0.),
z > 0. ? 1. : (z < 0. ? -1. : 0.)
);
}
public inline function floor(): Vec3 {
return new Vec3(
Math.floor(x),
Math.floor(y),
Math.floor(z)
);
}
public inline function ceil(): Vec3 {
return new Vec3(
Math.ceil(x),
Math.ceil(y),
Math.ceil(z)
);
}
public inline function fract(): Vec3 {
return (this: Vec3) - floor();
}
public extern overload inline function mod(d: Vec3): Vec3 {
return (this: Vec3) - d * ((this: Vec3) / d).floor();
}
public extern overload inline function mod(d: Float): Vec3 {
return (this: Vec3) - d * ((this: Vec3) / d).floor();
}
public extern overload inline function min(b: Vec3): Vec3 {
return new Vec3(
b.x < x ? b.x : x,
b.y < y ? b.y : y,
b.z < z ? b.z : z
);
}
public extern overload inline function min(b: Float): Vec3 {
return new Vec3(
b < x ? b : x,
b < y ? b : y,
b < z ? b : z
);
}
public extern overload inline function max(b: Vec3): Vec3 {
return new Vec3(
x < b.x ? b.x : x,
y < b.y ? b.y : y,
z < b.z ? b.z : z
);
}
public extern overload inline function max(b: Float): Vec3 {
return new Vec3(
x < b ? b : x,
y < b ? b : y,
z < b ? b : z
);
}
public extern overload inline function clamp(minLimit: Vec3, maxLimit: Vec3) {
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: Vec3, t: Vec3): Vec3 {
return (this: Vec3) * (1.0 - t) + b * t;
}
public extern overload inline function mix(b: Vec3, t: Float): Vec3 {
return (this: Vec3) * (1.0 - t) + b * t;
}
public extern overload inline function step(edge: Vec3): Vec3 {
return new Vec3(
x < edge.x ? 0.0 : 1.0,
y < edge.y ? 0.0 : 1.0,
z < edge.z ? 0.0 : 1.0
);
}
public extern overload inline function step(edge: Float): Vec3 {
return new Vec3(
x < edge ? 0.0 : 1.0,
y < edge ? 0.0 : 1.0,
z < edge ? 0.0 : 1.0
);
}
public extern overload inline function smoothstep(edge0: Vec3, edge1: Vec3): Vec3 {
var t = (((this: Vec3) - edge0) / (edge1 - edge0)).clamp(0, 1);
return t * t * (3.0 - 2.0 * t);
}
public extern overload inline function smoothstep(edge0: Float, edge1: Float): Vec3 {
var t = (((this: Vec3) - 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);
}
public inline function distance(b: Vec3): Float {
return (b - this).length();
}
public inline function dot(b: Vec3): Float {
return x * b.x + y * b.y + z * b.z;
}
public inline function normalize(): Vec3 {
var v: Vec3 = 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: Vec3, Nref: Vec3): Vec3 {
return new Vec3(x, y, z) * (Nref.dot(I) < 0 ? 1 : -1);
}
public inline function reflect(N: Vec3): Vec3 {
var I = (this: Vec3);
return I - 2 * N.dot(I) * N;
}
public inline function refract(N: Vec3, eta: Float): Vec3 {
var I = (this: Vec3);
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 'vec3(${x}, ${y}, ${z})';
}
@:op([])
inline function arrayRead(i: Int)
return switch i {
case 0: x;
case 1: y;
case 2: z;
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;
default: null;
}
@:op(-a)
static inline function neg(a: Vec3)
return new Vec3(-a.x, -a.y, -a.z);
@:op(++a)
static inline function prefixIncrement(a: Vec3) {
++a.x; ++a.y; ++a.z;
return a.clone();
}
@:op(--a)
static inline function prefixDecrement(a: Vec3) {
--a.x; --a.y; --a.z;
return a.clone();
}
@:op(a++)
static inline function postfixIncrement(a: Vec3) {
var ret = a.clone();
++a.x; ++a.y; ++a.z;
return ret;
}
@:op(a--)
static inline function postfixDecrement(a: Vec3) {
var ret = a.clone();
--a.x; --a.y; --a.z;
return ret;
}
@:op(a * b)
static inline function mul(a: Vec3, b: Vec3): Vec3
return new Vec3(a.x * b.x, a.y * b.y, a.z * b.z);
@:op(a * b) @:commutative
static inline function mulScalar(a: Vec3, b: Float): Vec3
return new Vec3(a.x * b, a.y * b, a.z * b);
@:op(a / b)
static inline function div(a: Vec3, b: Vec3): Vec3
return new Vec3(a.x / b.x, a.y / b.y, a.z / b.z);
@:op(a / b)
static inline function divScalar(a: Vec3, b: Float): Vec3
return new Vec3(a.x / b, a.y / b, a.z / b);
@:op(a / b)
static inline function divScalarInv(a: Float, b: Vec3): Vec3
return new Vec3(a / b.x, a / b.y, a / b.z);
@:op(a + b)
static inline function add(a: Vec3, b: Vec3): Vec3
return new Vec3(a.x + b.x, a.y + b.y, a.z + b.z);
@:op(a + b) @:commutative
static inline function addScalar(a: Vec3, b: Float): Vec3
return new Vec3(a.x + b, a.y + b, a.z + b);
@:op(a - b)
static inline function sub(a: Vec3, b: Vec3): Vec3
return new Vec3(a.x - b.x, a.y - b.y, a.z - b.z);
@:op(a - b)
static inline function subScalar(a: Vec3, b: Float): Vec3
return new Vec3(a.x - b, a.y - b, a.z - b);
@:op(b - a)
static inline function subScalarInv(a: Float, b: Vec3): Vec3
return new Vec3(a - b.x, a - b.y, a - b.z);
@:op(a == b)
static inline function equal(a: Vec3, b: Vec3): Bool
return a.x == b.x && a.y == b.y && a.z == b.z;
@:op(a != b)
static inline function notEqual(a: Vec3, b: Vec3): Bool
return !equal(a, b);
#end // !macro
// macros
/**
* Copy from any object with .x .y .z fields
*/
@:overload(function(source: {x: Float, y: Float, z: Float}): Vec3 {})
public macro function copyFrom(self: ExprOf<Vec3>, source: ExprOf<{x: Float, y: Float, z: Float}>): ExprOf<Vec3> {
return macro {
var self = $self;
var source = $source;
self.x = source.x;
self.y = source.y;
self.z = source.z;
self;
}
}
/**
* Copy into any object with .x .y .z fields
*/
@:overload(function(target: {x: Float, y: Float, z: Float}): {x: Float, y: Float, z: Float} {})
public macro function copyInto(self: ExprOf<Vec3>, 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;
}
}
@:overload(function<T>(arrayLike: T, index: Int): T {})
public macro function copyIntoArray(self: ExprOf<Vec3>, 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;
}
}
@:overload(function<T>(arrayLike: T, index: Int): T {})
public macro function copyFromArray(self: ExprOf<Vec3>, 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;
}
}
// static macros
/**
* Create from any object with .x .y .z fields
*/
@:overload(function(source: {x: Float, y: Float, z: Float}): Vec3 {})
public static macro function from(xyz: ExprOf<{x: Float, y: Float, z: Float}>): ExprOf<Vec3> {
return macro {
var source = $xyz;
new Vec3(source.x, source.y, source.z);
}
}
@:overload(function<T>(arrayLike: T, index: Int): T {})
public static macro function fromArray(array: ExprOf<ArrayAccess<Float>>, index: ExprOf<Int>): ExprOf<Vec3> {
return macro {
var array = $array;
var i: Int = $index;
new Vec3(
array[0 + i],
array[1 + i],
array[2 + i]
);
}
}
}
@:noCompletion
class Vec3Data {
#if !macro
public var x: Float;
public var y: Float;
public var z: Float;
public inline function new(x: Float, y: Float, z: Float) {
this.x = x;
this.y = y;
this.z = z;
}
#end
}