• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

excessive / cpml / 1

23 Jun 2021 11:35PM UTC coverage: 53.574% (+9.4%) from 44.185%
1

push

github

web-flow
Merge pull request #62 from idbrii/actions-busted

Run tests on gh actions

4452 of 8310 relevant lines covered (53.57%)

91.18 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

90.48
/modules/vec3.lua
1
--- A 3 component vector.
2
-- @module vec3
3

4
local modules = (...):gsub('%.[^%.]+$', '') .. "."
22✔
5
local private = require(modules .. "_private_utils")
22✔
6
local sqrt    = math.sqrt
22✔
7
local cos     = math.cos
22✔
8
local sin     = math.sin
22✔
9
local vec3    = {}
22✔
10
local vec3_mt = {}
22✔
11

12
-- Private constructor.
13
local function new(x, y, z)
14
        return setmetatable({
2,260✔
15
                x = x or 0,
1,130✔
16
                y = y or 0,
1,130✔
17
                z = z or 0
1,130✔
18
        }, vec3_mt)
2,260✔
19
end
20

21
-- Do the check to see if JIT is enabled. If so use the optimized FFI structs.
22
local status, ffi
23
if type(jit) == "table" and jit.status() then
22✔
24
        status, ffi = pcall(require, "ffi")
×
25
        if status then
×
26
                ffi.cdef "typedef struct { double x, y, z;} cpml_vec3;"
×
27
                new = ffi.typeof("cpml_vec3")
×
28
        end
29
end
30

31
--- Constants
32
-- @table vec3
33
-- @field unit_x X axis of rotation
34
-- @field unit_y Y axis of rotation
35
-- @field unit_z Z axis of rotation
36
-- @field zero Empty vector
37
vec3.unit_x = new(1, 0, 0)
22✔
38
vec3.unit_y = new(0, 1, 0)
22✔
39
vec3.unit_z = new(0, 0, 1)
22✔
40
vec3.zero   = new(0, 0, 0)
22✔
41

42
--- The public constructor.
43
-- @param x Can be of three types: </br>
44
-- number X component
45
-- table {x, y, z} or {x=x, y=y, z=z}
46
-- scalar To fill the vector eg. {x, x, x}
47
-- @tparam number y Y component
48
-- @tparam number z Z component
49
-- @treturn vec3 out
50
function vec3.new(x, y, z)
22✔
51
        -- number, number, number
52
        if x and y and z then
494✔
53
                assert(type(x) == "number", "new: Wrong argument type for x (<number> expected)")
370✔
54
                assert(type(y) == "number", "new: Wrong argument type for y (<number> expected)")
370✔
55
                assert(type(z) == "number", "new: Wrong argument type for z (<number> expected)")
370✔
56

57
                return new(x, y, z)
370✔
58

59
        -- {x, y, z} or {x=x, y=y, z=z}
60
        elseif type(x) == "table" or type(x) == "cdata" then -- table in vanilla lua, cdata in luajit
124✔
61
                local xx, yy, zz = x.x or x[1], x.y or x[2], x.z or x[3]
10✔
62
                assert(type(xx) == "number", "new: Wrong argument type for x (<number> expected)")
10✔
63
                assert(type(yy) == "number", "new: Wrong argument type for y (<number> expected)")
10✔
64
                assert(type(zz) == "number", "new: Wrong argument type for z (<number> expected)")
10✔
65

66
                return new(xx, yy, zz)
10✔
67

68
        -- number
69
        elseif type(x) == "number" then
114✔
70
                return new(x, x, x)
50✔
71
        else
72
                return new()
64✔
73
        end
74
end
75

76
--- Clone a vector.
77
-- @tparam vec3 a Vector to be cloned
78
-- @treturn vec3 out
79
function vec3.clone(a)
22✔
80
        return new(a.x, a.y, a.z)
142✔
81
end
82

83
--- Add two vectors.
84
-- @tparam vec3 a Left hand operand
85
-- @tparam vec3 b Right hand operand
86
-- @treturn vec3 out
87
function vec3.add(a, b)
22✔
88
        return new(
176✔
89
                a.x + b.x,
88✔
90
                a.y + b.y,
88✔
91
                a.z + b.z
88✔
92
        )
93
end
94

95
--- Subtract one vector from another.
96
-- @tparam vec3 a Left hand operand
97
-- @tparam vec3 b Right hand operand
98
-- @treturn vec3 out
99
function vec3.sub(a, b)
22✔
100
        return new(
188✔
101
                a.x - b.x,
94✔
102
                a.y - b.y,
94✔
103
                a.z - b.z
94✔
104
        )
105
end
106

107
--- Multiply a vector by another vectorr.
108
-- @tparam vec3 a Left hand operand
109
-- @tparam vec3 b Right hand operand
110
-- @treturn vec3 out
111
function vec3.mul(a, b)
22✔
112
        return new(
×
113
                a.x * b.x,
×
114
                a.y * b.y,
×
115
                a.z * b.z
×
116
        )
117
end
118

119
--- Divide a vector by a scalar.
120
-- @tparam vec3 a Left hand operand
121
-- @tparam vec3 b Right hand operand
122
-- @treturn vec3 out
123
function vec3.div(a, b)
22✔
124
        return new(
8✔
125
                a.x / b.x,
4✔
126
                a.y / b.y,
4✔
127
                a.z / b.z
4✔
128
        )
129
end
130

131
--- Get the normal of a vector.
132
-- @tparam vec3 a Vector to normalize
133
-- @treturn vec3 out
134
function vec3.normalize(a)
22✔
135
        if a:is_zero() then
22✔
136
                return new()
×
137
        end
138
        return a:scale(1 / a:len())
22✔
139
end
140

141
--- Trim a vector to a given length
142
-- @tparam vec3 a Vector to be trimmed
143
-- @tparam number len Length to trim the vector to
144
-- @treturn vec3 out
145
function vec3.trim(a, len)
22✔
146
        return a:normalize():scale(math.min(a:len(), len))
2✔
147
end
148

149
--- Get the cross product of two vectors.
150
-- @tparam vec3 a Left hand operand
151
-- @tparam vec3 b Right hand operand
152
-- @treturn vec3 out
153
function vec3.cross(a, b)
22✔
154
        return new(
104✔
155
                a.y * b.z - a.z * b.y,
52✔
156
                a.z * b.x - a.x * b.z,
52✔
157
                a.x * b.y - a.y * b.x
52✔
158
        )
159
end
160

161
--- Get the dot product of two vectors.
162
-- @tparam vec3 a Left hand operand
163
-- @tparam vec3 b Right hand operand
164
-- @treturn number dot
165
function vec3.dot(a, b)
22✔
166
        return a.x * b.x + a.y * b.y + a.z * b.z
118✔
167
end
168

169
--- Get the length of a vector.
170
-- @tparam vec3 a Vector to get the length of
171
-- @treturn number len
172
function vec3.len(a)
22✔
173
        return sqrt(a.x * a.x + a.y * a.y + a.z * a.z)
52✔
174
end
175

176
--- Get the squared length of a vector.
177
-- @tparam vec3 a Vector to get the squared length of
178
-- @treturn number len
179
function vec3.len2(a)
22✔
180
        return a.x * a.x + a.y * a.y + a.z * a.z
26✔
181
end
182

183
--- Get the distance between two vectors.
184
-- @tparam vec3 a Left hand operand
185
-- @tparam vec3 b Right hand operand
186
-- @treturn number dist
187
function vec3.dist(a, b)
22✔
188
        local dx = a.x - b.x
22✔
189
        local dy = a.y - b.y
22✔
190
        local dz = a.z - b.z
22✔
191
        return sqrt(dx * dx + dy * dy + dz * dz)
22✔
192
end
193

194
--- Get the squared distance between two vectors.
195
-- @tparam vec3 a Left hand operand
196
-- @tparam vec3 b Right hand operand
197
-- @treturn number dist
198
function vec3.dist2(a, b)
22✔
199
        local dx = a.x - b.x
2✔
200
        local dy = a.y - b.y
2✔
201
        local dz = a.z - b.z
2✔
202
        return dx * dx + dy * dy + dz * dz
2✔
203
end
204

205
--- Scale a vector by a scalar.
206
-- @tparam vec3 a Left hand operand
207
-- @tparam number b Right hand operand
208
-- @treturn vec3 out
209
function vec3.scale(a, b)
22✔
210
        return new(
224✔
211
                a.x * b,
112✔
212
                a.y * b,
112✔
213
                a.z * b
112✔
214
        )
215
end
216

217
--- Rotate vector about an axis.
218
-- @tparam vec3 a Vector to rotate
219
-- @tparam number phi Angle to rotate vector by (in radians)
220
-- @tparam vec3 axis Axis to rotate by
221
-- @treturn vec3 out
222
function vec3.rotate(a, phi, axis)
22✔
223
        if not vec3.is_vec3(axis) then
6✔
224
                return a
2✔
225
        end
226

227
        local u = axis:normalize()
4✔
228
        local c = cos(phi)
4✔
229
        local s = sin(phi)
4✔
230

231
        -- Calculate generalized rotation matrix
232
        local m1 = new((c + u.x * u.x * (1 - c)),       (u.x * u.y * (1 - c) - u.z * s), (u.x * u.z * (1 - c) + u.y * s))
4✔
233
        local m2 = new((u.y * u.x * (1 - c) + u.z * s), (c + u.y * u.y * (1 - c)),       (u.y * u.z * (1 - c) - u.x * s))
4✔
234
        local m3 = new((u.z * u.x * (1 - c) - u.y * s), (u.z * u.y * (1 - c) + u.x * s), (c + u.z * u.z * (1 - c))      )
4✔
235

236
        return new(
8✔
237
                a:dot(m1),
4✔
238
                a:dot(m2),
4✔
239
                a:dot(m3)
4✔
240
        )
241
end
242

243
--- Get the perpendicular vector of a vector.
244
-- @tparam vec3 a Vector to get perpendicular axes from
245
-- @treturn vec3 out
246
function vec3.perpendicular(a)
22✔
247
        return new(-a.y, a.x, 0)
2✔
248
end
249

250
--- Lerp between two vectors.
251
-- @tparam vec3 a Left hand operand
252
-- @tparam vec3 b Right hand operand
253
-- @tparam number s Step value
254
-- @treturn vec3 out
255
function vec3.lerp(a, b, s)
22✔
256
        return a + (b - a) * s
2✔
257
end
258

259
-- Round all components to nearest int (or other precision).
260
-- @tparam vec3 a Vector to round.
261
-- @tparam precision Digits after the decimal (round numebr if unspecified)
262
-- @treturn vec3 Rounded vector
263
function vec3.round(a, precision)
22✔
264
        return vec3.new(private.round(a.x, precision), private.round(a.y, precision), private.round(a.z, precision))
6✔
265
end
266

267
--- Unpack a vector into individual components.
268
-- @tparam vec3 a Vector to unpack
269
-- @treturn number x
270
-- @treturn number y
271
-- @treturn number z
272
function vec3.unpack(a)
22✔
273
        return a.x, a.y, a.z
6✔
274
end
275

276
--- Return the component-wise minimum of two vectors.
277
-- @tparam vec3 a Left hand operand
278
-- @tparam vec3 b Right hand operand
279
-- @treturn vec3 A vector where each component is the lesser value for that component between the two given vectors.
280
function vec3.component_min(a, b)
22✔
281
        return new(math.min(a.x, b.x), math.min(a.y, b.y), math.min(a.z, b.z))
18✔
282
end
283

284
--- Return the component-wise maximum of two vectors.
285
-- @tparam vec3 a Left hand operand
286
-- @tparam vec3 b Right hand operand
287
-- @treturn vec3 A vector where each component is the lesser value for that component between the two given vectors.
288
function vec3.component_max(a, b)
22✔
289
        return new(math.max(a.x, b.x), math.max(a.y, b.y), math.max(a.z, b.z))
18✔
290
end
291

292
-- Negate x axis only of vector.
293
-- @tparam vec2 a Vector to x-flip.
294
-- @treturn vec2 x-flipped vector
295
function vec3.flip_x(a)
22✔
296
        return vec3.new(-a.x, a.y, a.z)
2✔
297
end
298

299
-- Negate y axis only of vector.
300
-- @tparam vec2 a Vector to y-flip.
301
-- @treturn vec2 y-flipped vector
302
function vec3.flip_y(a)
22✔
303
        return vec3.new(a.x, -a.y, a.z)
2✔
304
end
305

306
-- Negate z axis only of vector.
307
-- @tparam vec2 a Vector to z-flip.
308
-- @treturn vec2 z-flipped vector
309
function vec3.flip_z(a)
22✔
310
        return vec3.new(a.x, a.y, -a.z)
2✔
311
end
312

313
--- Return a boolean showing if a table is or is not a vec3.
314
-- @tparam vec3 a Vector to be tested
315
-- @treturn boolean is_vec3
316
function vec3.is_vec3(a)
22✔
317
        if type(a) == "cdata" then
810✔
318
                return ffi.istype("cpml_vec3", a)
×
319
        end
320

321
        return
×
322
                type(a)   == "table"  and
810✔
323
                type(a.x) == "number" and
618✔
324
                type(a.y) == "number" and
598✔
325
                type(a.z) == "number"
810✔
326
end
327

328
--- Return a boolean showing if a table is or is not a zero vec3.
329
-- @tparam vec3 a Vector to be tested
330
-- @treturn boolean is_zero
331
function vec3.is_zero(a)
22✔
332
        return a.x == 0 and a.y == 0 and a.z == 0
24✔
333
end
334

335
--- Return a formatted string.
336
-- @tparam vec3 a Vector to be turned into a string
337
-- @treturn string formatted
338
function vec3.to_string(a)
22✔
339
        return string.format("(%+0.3f,%+0.3f,%+0.3f)", a.x, a.y, a.z)
2✔
340
end
341

342
vec3_mt.__index    = vec3
22✔
343
vec3_mt.__tostring = vec3.to_string
22✔
344

345
function vec3_mt.__call(_, x, y, z)
22✔
346
        return vec3.new(x, y, z)
480✔
347
end
348

349
function vec3_mt.__unm(a)
22✔
350
        return new(-a.x, -a.y, -a.z)
2✔
351
end
352

353
function vec3_mt.__eq(a, b)
22✔
354
        if not vec3.is_vec3(a) or not vec3.is_vec3(b) then
62✔
355
                return false
×
356
        end
357
        return a.x == b.x and a.y == b.y and a.z == b.z
62✔
358
end
359

360
function vec3_mt.__add(a, b)
22✔
361
        assert(vec3.is_vec3(a), "__add: Wrong argument type for left hand operand. (<cpml.vec3> expected)")
86✔
362
        assert(vec3.is_vec3(b), "__add: Wrong argument type for right hand operand. (<cpml.vec3> expected)")
86✔
363
        return a:add(b)
86✔
364
end
365

366
function vec3_mt.__sub(a, b)
22✔
367
        assert(vec3.is_vec3(a), "__sub: Wrong argument type for left hand operand. (<cpml.vec3> expected)")
92✔
368
        assert(vec3.is_vec3(b), "__sub: Wrong argument type for right hand operand. (<cpml.vec3> expected)")
92✔
369
        return a:sub(b)
92✔
370
end
371

372
function vec3_mt.__mul(a, b)
22✔
373
        assert(vec3.is_vec3(a), "__mul: Wrong argument type for left hand operand. (<cpml.vec3> expected)")
76✔
374
        assert(vec3.is_vec3(b) or type(b) == "number", "__mul: Wrong argument type for right hand operand. (<cpml.vec3> or <number> expected)")
76✔
375

376
        if vec3.is_vec3(b) then
76✔
377
                return a:mul(b)
×
378
        end
379

380
        return a:scale(b)
76✔
381
end
382

383
function vec3_mt.__div(a, b)
22✔
384
        assert(vec3.is_vec3(a), "__div: Wrong argument type for left hand operand. (<cpml.vec3> expected)")
12✔
385
        assert(vec3.is_vec3(b) or type(b) == "number", "__div: Wrong argument type for right hand operand. (<cpml.vec3> or <number> expected)")
12✔
386

387
        if vec3.is_vec3(b) then
12✔
388
                return a:div(b)
2✔
389
        end
390

391
        return a:scale(1 / b)
10✔
392
end
393

394
if status then
22✔
395
        xpcall(function() -- Allow this to silently fail; assume failure means someone messed with package.loaded
×
396
                ffi.metatype(new, vec3_mt)
×
397
        end, function() end)
×
398
end
399

400
return setmetatable({}, vec3_mt)
22✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc