-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTVec.lua
385 lines (313 loc) · 8.3 KB
/
TVec.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
-- Name: TVec.lua
-- Description: Pooled, FFI-ed 2D vector library for Lua.
-- Author: Shahil Ahmed (IsometricShahil)
-- Github: github.com/IsometricShahil/TVec
--
-- The MIT License (MIT)
-- Copyright (c) 2020-2023 Shahil Ahmed
--
-- Permission is hereby granted, free of charge, to any person obtaining a copy
-- of this software and associated documentation files (the "Software"), to deal
-- in the Software without restriction, including without limitation the rights
-- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
-- copies of the Software, and to permit persons to whom the Software is
-- furnished to do so, subject to the following conditions:
--
-- The above copyright notice and this permission notice shall be included in
-- all copies or substantial portions of the Software.
--
-- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
-- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
-- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
-- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
-- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
-- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
-- THE SOFTWARE.
-- Localizations
local type = type
local setmetatable = setmetatable
local getmetatable = getmetatable
local sin, cos = math.sin, math.cos
local atan2 = math.atan2 or math.atan
local sqrt = math.sqrt
local abs = math.abs
local min, max = math.min, math.max
local pi, huge = math.pi, math.huge
local remove = table.remove
local insert = table.insert
local tau = pi * 2
local MAX_FREE = 100 -- Maximum number of freed vectors to hold in stack
local TVec = {}
TVec.__index = TVec
local ffi
-- We use ffi structs if:
-- 1. JIT exists
-- 2. JIT is enabled
-- 3. FFI exists
if jit and jit.status() and package.preload.ffi then
ffi = require "ffi"
end
local function clamp(v, lo, hi)
return max(lo, min(v, hi))
end
local function err(msg)
error(msg, 3)
end
local _create, vType
if ffi then
vType = ffi.typeof("struct {double x, y;}") -- Metatype is set at the end
_create = function() return vType() end
else
_create = function() return setmetatable({}, TVec) end
end
-- _create is just the version specific vector constructor
-- it doesn't set the x, y components
-- The stack to hold freed vectors
local freeStack = {}
local function new(x, y)
local v = remove(freeStack) -- Pop from the stack of free vectors
v = v or _create() -- If the stack was empty then v will be nil, construct a new vector in that case
-- Convert x and y to a number
x = tonumber(x)
y = tonumber(y)
v.x = x or 0
v.y = y or v.x
return v
end
local isVector
if ffi then
isVector = function(v)
return type(v) == "cdata" and ffi.istype(v, vType)
end
else
isVector = function(v)
return getmetatable(v) == TVec
end
end
local function fromAngle(t, m)
t = tonumber(t)
if not t then err("Number expected") end
m = tonumber(m) or 1
return new(cos(t) * m, sin(t) * m)
end
local function random(min, max)
min = tonumber(min) or 0
max = tonumber(max) or tau
local t = min + (max - min) * TVec.rand()
return fromAngle(t)
end
function TVec:getAngle()
local t = atan2(self.y, self.x)
if t < 0 then t = t + tau end
return t
end
function TVec:setAngle(t)
t = tonumber(t)
if not t then err("Number expected") end
local m = self:getMag()
self.x = cos(t) * m
self.y = sin(t) * m
return self
end
function TVec:getMag()
return sqrt((self.x * self.x) + (self.y * self.y))
end
function TVec:getMagSq()
return (self.x * self.x) + (self.y * self.y)
end
function TVec:setMag(m)
m = tonumber(m)
if not m then err("Number expected") end
self:normalize()
self.x = self.x * m
self.y = self.y * m
return self
end
function TVec:normalize()
local v = self:getMag()
if v ~= 0 then
self.x = self.x / v
self.y = self.y / v
end
return self
end
TVec.normalise = TVec.normalize -- Alias
function TVec:rotate(t)
t = tonumber(t)
if not t then err("Number expected") end
local oldT = self:getAngle()
self:setAngle(oldT+t)
return self
end
function TVec:rotate90()
self:set(-self.y, self.x)
return self
end
function TVec:dot(b)
if not isVector(b) then err("TVec expected") end
return self.x * b.x + self.y * b.y
end
function TVec:dist(b)
if not isVector(b) then err("TVec expected") end
local xd = self.x - b.x
local yd = self.y - b.y
return sqrt(xd * xd + yd * yd)
end
function TVec:distSq(b)
if not isVector(b) then err("TVec expected") end
local xd = self.x - b.x
local yd = self.y - b.y
return xd * xd + yd * yd
end
function TVec:clampMag(min, max)
min = tonumber(min)
max = tonumber(max)
if not (min and max) then
err("Number expected")
end
local m = self:getMag()
if m < min or m > max then
self:setMag(clamp(m, min, max))
end
return self
end
function TVec:clampAngle(min, max)
min = tonumber(min)
max = tonumber(max)
if not (min and max) then
err("Number expected")
end
local t = self:getAngle()
if t < min or t > max then
self:setAngle(clamp(t, min, max))
end
return self
end
function TVec:clone()
return new(self.x, self.y)
end
TVec.copy = TVec.clone -- Alias
function TVec:unpack()
return self.x, self.y
end
function TVec:set(x, y)
x = tonumber(x)
y = tonumber(y)
if x then self.x = x end
if y then self.y = y end
end
function TVec:free()
for i = 1, MAX_FREE do
if freeStack[i] == nil then
freeStack[i] = self
break
end
if freeStack[i] == self then
err("Vector is already freed")
end
end
return self
end
function TVec.__eq(a, b)
if not (isVector(a) and isVector(b)) then err("TVec expected") end
return abs(a.x - b.x) < 1e-9 and -- Avoiding usage of == on floating numbers
abs(a.y - b.y) < 1e-9
end
function TVec.__unm(v)
return new(-v.x, -v.y)
end
function TVec.__tostring(v)
return ("(%f, %f)"):format(v.x, v.y)
end
-- Make a FFI-ed vector iterable with pairs()
local function iter(vec, step)
if step == nil then
return 'x', vec.x
elseif step == 'x' then
return 'y', vec.y
end
end
function TVec.__pairs(v)
return iter, v, nil
end
local sharedCode = [[
function TVec:NAME(b) -- An inline method, used like vec:add(v)
if not (isVector(b) or tonumber(b)) then err("TVec or number expected") end
if isVector(b) then
self.x = self.x OP b.x
self.y = self.y OP b.y
else
b = tonumber(b)
self.x = self.x OP b
self.y = self.y OP b
end
return self
end
function TVec.__NAME(a, b) -- Metamethod
if isVector(b) and not isVector(a) then
return b OP a -- Reverse it
end
if not isVector(a) then err("TVec expected") end
if not (isVector(b) or tonumber(b)) then err("TVec or number expected") end
local nv
if isVector(b) then
nv = new(a.x OP b.x, a.y OP b.y)
else
b = tonumber(b)
nv = new(a.x OP b, a.y OP b)
end
return nv
end
]]
local ops = {
-- {Event, Operator}
{"add", "+"},
{"sub", "-"},
{"mul", "*"},
{"div", "/"},
{"pow", "^"},
{"mod", "%%"},
}
local env = { -- Environment to run the chunks in
TVec = TVec,
new = new,
isVector = isVector,
tonumber = tonumber,
err = err
}
for i = 1, #ops do
local v = ops[i]
local genCode = sharedCode:gsub("NAME", v[1])
genCode = genCode:gsub("OP", v[2])
local chunk, msg = load(genCode, "TVecOpGen", "t", env)
if not chunk then error(msg) end
chunk()
end
-- Don't hold these in memory
ops = nil
env = nil
-- Wrap TVec.new(x, y) as TVec(x, y)
setmetatable(TVec, {
__call = function(_, x, y)
return new(x, y)
end
})
-- Default to math.random
TVec.rand = math.random
-- Switch to love.math.random if avaliable
if type(love) == "table" and
type(love.math) == "table" and
type(love.math.random) == "function" then
TVec.rand = love.math.random
end
if ffi then
ffi.metatype(vType, TVec)
end
-- Module packup
TVec.new = new
TVec.isVector = isVector
TVec.fromAngle = fromAngle
TVec.fromPolar = fromAngle -- Alias
TVec.random = random
TVec._stack = freeStack -- Exposing this to you if you have anything in mind :)
return TVec