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