-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathvec3.lua
593 lines (485 loc) · 11.8 KB
/
vec3.lua
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
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
--[[
3d vector type
]]
--import vec2 if not defined globally
local path = (...):gsub("vec3", "")
local class = require(path .. "class")
local vec2 = require(path .. "vec2")
local math = require(path .. "mathx") --shadow global math module
local make_pooled = require(path .. "make_pooled")
local vec3 = class({
name = "vec3",
})
--stringification
function vec3:__tostring()
return ("(%.2f, %.2f, %.2f)"):format(self.x, self.y, self.z)
end
--probably-too-flexible ctor
function vec3:new(x, y, z)
if type(x) == "number" or type(x) == "nil" then
self:sset(x or 0, y, z)
elseif type(x) == "table" then
if type(x.type) == "function" and x:type() == "vec3" then
self:vset(x)
elseif x[1] then
self:sset(x[1], x[2], x[3])
else
self:sset(x.x, x.y, x.z)
end
end
end
--explicit ctors
function vec3:copy()
return vec3(self.x, self.y, self.z)
end
function vec3:xyz(x, y, z)
return vec3(x, y, z)
end
function vec3:filled(x, y, z)
return vec3(x, y, z)
end
function vec3:zero()
return vec3(0, 0, 0)
end
--unpack for multi-args
function vec3:unpack()
return self.x, self.y, self.z
end
--pack when a sequence is needed
function vec3:pack()
return {self:unpack()}
end
--handle pooling
make_pooled(vec3, 128)
--get a pooled copy of an existing vector
function vec3:pooled_copy()
return vec3:pooled():vset(self)
end
--modify
function vec3:sset(x, y, z)
self.x = x
self.y = y or x
self.z = z or y or x
return self
end
function vec3:vset(v)
self.x = v.x
self.y = v.y
self.z = v.z
return self
end
function vec3:swap(v)
local sx, sy, sz = self.x, self.y, self.z
self:vset(v)
v:sset(sx, sy, sz)
return self
end
-----------------------------------------------------------
--equality comparison
-----------------------------------------------------------
--threshold for equality in each dimension
local EQUALS_EPSILON = 1e-9
--true if a and b are functionally equivalent
function vec3.equals(a, b)
return (
math.abs(a.x - b.x) <= EQUALS_EPSILON and
math.abs(a.y - b.y) <= EQUALS_EPSILON and
math.abs(a.z - b.z) <= EQUALS_EPSILON
)
end
--true if a and b are not functionally equivalent
--(very slightly faster than `not vec3.equals(a, b)`)
function vec3.nequals(a, b)
return (
math.abs(a.x - b.x) > EQUALS_EPSILON or
math.abs(a.y - b.y) > EQUALS_EPSILON or
math.abs(a.z - b.z) > EQUALS_EPSILON
)
end
-----------------------------------------------------------
--arithmetic
-----------------------------------------------------------
--immediate mode
--vector
function vec3:vaddi(v)
self.x = self.x + v.x
self.y = self.y + v.y
self.z = self.z + v.z
return self
end
function vec3:vsubi(v)
self.x = self.x - v.x
self.y = self.y - v.y
self.z = self.z - v.z
return self
end
function vec3:vmuli(v)
self.x = self.x * v.x
self.y = self.y * v.y
self.z = self.z * v.z
return self
end
function vec3:vdivi(v)
self.x = self.x / v.x
self.y = self.y / v.y
self.z = self.z / v.z
return self
end
--scalar
function vec3:saddi(x, y, z)
if not y then y = x end
if not z then z = y end
self.x = self.x + x
self.y = self.y + y
self.z = self.z + z
return self
end
function vec3:ssubi(x, y, z)
if not y then y = x end
if not z then z = y end
self.x = self.x - x
self.y = self.y - y
self.z = self.z - z
return self
end
function vec3:smuli(x, y, z)
if not y then y = x end
if not z then z = y end
self.x = self.x * x
self.y = self.y * y
self.z = self.z * z
return self
end
function vec3:sdivi(x, y, z)
if not y then y = x end
if not z then z = y end
self.x = self.x / x
self.y = self.y / y
self.z = self.z / z
return self
end
--garbage mode
function vec3:vadd(v)
return self:copy():vaddi(v)
end
function vec3:vsub(v)
return self:copy():vsubi(v)
end
function vec3:vmul(v)
return self:copy():vmuli(v)
end
function vec3:vdiv(v)
return self:copy():vdivi(v)
end
function vec3:sadd(x, y, z)
return self:copy():saddi(x, y, z)
end
function vec3:ssub(x, y, z)
return self:copy():ssubi(x, y, z)
end
function vec3:smul(x, y, z)
return self:copy():smuli(x, y, z)
end
function vec3:sdiv(x, y, z)
return self:copy():sdivi(x, y, z)
end
--fused multiply-add (a + (b * t))
function vec3:fmai(v, t)
self.x = self.x + (v.x * t)
self.y = self.y + (v.y * t)
self.z = self.z + (v.z * t)
return self
end
function vec3:fma(v, t)
return self:copy():fmai(v, t)
end
-----------------------------------------------------------
-- geometric methods
-----------------------------------------------------------
function vec3:length_squared()
return self.x * self.x + self.y * self.y + self.z * self.z
end
function vec3:length()
return math.sqrt(self:length_squared())
end
function vec3:distance_squared(other)
local dx = self.x - other.x
local dy = self.y - other.y
local dz = self.z - other.z
return dx * dx + dy * dy + dz * dz
end
function vec3:distance(other)
return math.sqrt(self:distance_squared(other))
end
--immediate mode
function vec3:normalisei_both()
local len = self:length()
if len == 0 then
return self, 0
end
return self:sdivi(len), len
end
function vec3:normalisei()
local v, len = self:normalisei_both()
return v
end
function vec3:normalisei_len()
local v, len = self:normalisei_both()
return len
end
function vec3:inversei()
return self:smuli(-1)
end
--swizzle extraction
--not as nice as property accessors so might be worth doing that later :)
--also dog slow, so there's that
local _swizzle_x_byte = ("x"):byte()
local _swizzle_y_byte = ("y"):byte()
local _swizzle_z_byte = ("z"):byte()
local _allowed_swizzle = {
[_swizzle_x_byte] = "x",
[_swizzle_y_byte] = "y",
[_swizzle_z_byte] = "z",
}
function vec3:encode_swizzle_field(swizzle)
if type(swizzle) == "string" then
swizzle = swizzle:byte()
end
return _allowed_swizzle[swizzle] or "x"
end
function vec3:extract_single(swizzle)
return self[self:encode_swizzle_field(swizzle)]
end
function vec3:infuse_single(swizzle, v)
self[self:encode_swizzle_field(swizzle)] = v
return self
end
function vec3:extract_vec2(swizzle, into)
if not into then into = vec2:zero() end
local x = self:extract_single(swizzle:byte(1))
local y = self:extract_single(swizzle:byte(2))
return into:sset(x, y)
end
function vec3:infuse_vec2(swizzle, v)
self:infuse_single(swizzle:byte(1), v.x)
self:infuse_single(swizzle:byte(2), v.y)
return self
end
--rotate around a swizzle
--todo: angle-axis version
function vec3:rotatei(swizzle, angle)
if angle == 0 then --early out
return self
end
local v = vec2:pooled()
self:extract_vec2(swizzle, v)
v:rotatei(angle)
self:infuse_vec2(swizzle, v)
v:release()
return self
end
function vec3:rotate_euleri(angle_x_axis, angle_y_axis, angle_z_axis)
self:rotatei("yz", angle_x_axis)
self:rotatei("xz", angle_y_axis)
self:rotatei("xy", angle_z_axis)
return self
end
--todo: 90deg rotations
vec3.rot180i = vec3.inversei --alias
function vec3:rotate_aroundi(swizzle, angle, pivot)
self:vsubi(pivot)
self:rotatei(swizzle, angle)
self:vaddi(pivot)
return self
end
--garbage mode
function vec3:normalised()
return self:copy():normalisei()
end
function vec3:normalised_len()
local v = self:copy()
local len = v:normalisei_len()
return v, len
end
function vec3:inverse()
return self:copy():inversei()
end
function vec3:rotate(swizzle, angle)
return self:copy():rotatei(swizzle, angle)
end
function vec3:rotate_euler(angle_x_axis, angle_y_axis, angle_z_axis)
return self:copy():rotate_euleri(angle_x_axis, angle_y_axis, angle_z_axis)
end
vec3.rot180 = vec3.inverse --alias
function vec3:rotate_around(swizzle, angle, pivot)
return self:copy():rotate_aroundi(swizzle, angle, pivot)
end
-----------------------------------------------------------
-- per-component clamping ops
-----------------------------------------------------------
function vec3:mini(v)
self.x = math.min(self.x, v.x)
self.y = math.min(self.y, v.y)
self.z = math.min(self.z, v.z)
return self
end
function vec3:maxi(v)
self.x = math.max(self.x, v.x)
self.y = math.max(self.y, v.y)
self.z = math.max(self.z, v.z)
return self
end
function vec3:clampi(min, max)
self.x = math.clamp(self.x, min.x, max.x)
self.y = math.clamp(self.y, min.y, max.y)
self.z = math.clamp(self.z, min.z, max.z)
return self
end
function vec3:min(v)
return self:copy():mini(v)
end
function vec3:max(v)
return self:copy():maxi(v)
end
function vec3:clamp(min, max)
return self:copy():clampi(min, max)
end
-----------------------------------------------------------
-- absolute value
-----------------------------------------------------------
function vec3:absi()
self.x = math.abs(self.x)
self.y = math.abs(self.y)
self.z = math.abs(self.z)
return self
end
function vec3:abs()
return self:copy():absi()
end
-----------------------------------------------------------
-- truncation/rounding
-----------------------------------------------------------
function vec3:floori()
self.x = math.floor(self.x)
self.y = math.floor(self.y)
self.z = math.floor(self.z)
return self
end
function vec3:ceili()
self.x = math.ceil(self.x)
self.y = math.ceil(self.y)
self.z = math.ceil(self.z)
return self
end
function vec3:roundi()
self.x = math.round(self.x)
self.y = math.round(self.y)
self.z = math.round(self.z)
return self
end
function vec3:floor()
return self:copy():floori()
end
function vec3:ceil()
return self:copy():ceili()
end
function vec3:round()
return self:copy():roundi()
end
-----------------------------------------------------------
-- interpolation
-----------------------------------------------------------
function vec3:lerpi(other, amount)
self.x = math.lerp(self.x, other.x, amount)
self.y = math.lerp(self.y, other.y, amount)
self.z = math.lerp(self.z, other.z, amount)
return self
end
function vec3:lerp(other, amount)
return self:copy():lerpi(other, amount)
end
function vec3:lerp_epsi(other, amount, eps)
self.x = math.lerp_eps(self.x, other.x, amount, eps)
self.y = math.lerp_eps(self.y, other.y, amount, eps)
self.z = math.lerp_eps(self.z, other.z, amount, eps)
return self
end
function vec3:lerp_eps(other, amount, eps)
return self:copy():lerp_epsi(other, amount, eps)
end
-----------------------------------------------------------
-- vector products and projections
-----------------------------------------------------------
function vec3.dot(a, b)
return a.x * b.x + a.y * b.y + a.z * b.z
end
function vec3.cross(a, b, into)
if not into then into = vec3:zero() end
return into:sset(
a.y * b.z - a.z * b.y,
a.z * b.x - a.x * b.z,
a.x * b.y - a.y * b.x
)
end
--scalar projection a onto b
function vec3.sproj(a, b)
local len = b:length()
if len == 0 then
return 0
end
return a:dot(b) / len
end
--vector projection a onto b (writes into a)
function vec3.vproji(a, b)
local div = b:dot(b)
if div == 0 then
return a:sset(0, 0, 0)
end
local fac = a:dot(b) / div
return a:vset(b):smuli(fac)
end
function vec3.vproj(a, b)
return a:copy():vproji(b)
end
--vector rejection a onto b (writes into a)
function vec3.vreji(a, b)
local tx, ty, tz = a.x, a.y, a.z
a:vproji(b)
a:sset(tx - a.x, ty - a.y, tz - a.z)
return a
end
function vec3.vrej(a, b)
return a:copy():vreji(b)
end
-----------------------------------------------------------
-- vector extension methods for special purposes
-- (any common vector ops worth naming)
-----------------------------------------------------------
--"physical" friction
local _v_friction = vec3:zero() --avoid alloc
function vec3:apply_friction(mu, dt)
_v_friction:vset(self):smuli(mu * dt)
if _v_friction:length_squared() > self:length_squared() then
self:sset(0, 0)
else
self:vsubi(_v_friction)
end
return self
end
--"gamey" friction in one dimension
local function apply_friction_1d(v, mu, dt)
local friction = mu * v * dt
if math.abs(friction) > math.abs(v) then
return 0
else
return v - friction
end
end
--"gamey" friction in both dimensions
function vec3:apply_friction_xy(mu_x, mu_y, dt)
self.x = apply_friction_1d(self.x, mu_x, dt)
self.y = apply_friction_1d(self.y, mu_y, dt)
self.z = apply_friction_1d(self.z, mu_y, dt)
return self
end
return vec3