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

excessive / cpml / 6132588926

09 Sep 2023 06:38PM UTC coverage: 14.013% (-44.7%) from 58.701%
6132588926

push

github

FatalError42O
fixed Busted support (hopefully)

975 of 6958 relevant lines covered (14.01%)

10.82 hits per line

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

0.44
/modules/quat.lua
1
--- A quaternion and associated utilities.
2
-- @module quat
3

4
local constants     = require(modules .. "constants")
3✔
5
local vec3          = require(modules .. "vec3")
×
6
local precond       = require(modules .. "_private_precond")
×
7
local private       = require(modules .. "_private_utils")
×
8
local DOT_THRESHOLD = constants.DOT_THRESHOLD
×
9
local DBL_EPSILON   = constants.DBL_EPSILON
×
10
local acos          = math.acos
×
11
local cos           = math.cos
×
12
local sin           = math.sin
×
13
local min           = math.min
×
14
local max           = math.max
×
15
local sqrt          = math.sqrt
×
16
local quat          = {}
×
17
local quat_mt       = {}
×
18

19
-- Private constructor.
20
local function new(x, y, z, w)
21
        return setmetatable({
×
22
                x = x or 0,
23
                y = y or 0,
24
                z = z or 0,
25
                w = w or 1
×
26
        }, quat_mt)
×
27
end
28

29
-- Do the check to see if JIT is enabled. If so use the optimized FFI structs.
30
local status, ffi
31
if type(jit) == "table" and jit.status() then
×
32
        status, ffi = pcall(require, "ffi")
×
33
        if status then
×
34
                ffi.cdef "typedef struct { double x, y, z, w;} cpml_quat;"
×
35
                new = ffi.typeof("cpml_quat")
×
36
        end
37
end
38

39
-- Statically allocate a temporary variable used in some of our functions.
40
local tmp = new()
×
41
local qv, uv, uuv = vec3(), vec3(), vec3()
×
42

43
--- Constants
44
-- @table quat
45
-- @field unit Unit quaternion
46
-- @field zero Empty quaternion
47
quat.unit = new(0, 0, 0, 1)
×
48
quat.zero = new(0, 0, 0, 0)
×
49

50
--- The public constructor.
51
-- @param x Can be of two types: </br>
52
-- number x X component
53
-- table {x, y, z, w} or {x=x, y=y, z=z, w=w}
54
-- @tparam number y Y component
55
-- @tparam number z Z component
56
-- @tparam number w W component
57
-- @treturn quat out
58
function quat.new(x, y, z, w)
×
59
        -- number, number, number, number
60
        if x and y and z and w then
×
61
                precond.typeof(x, "number", "new: Wrong argument type for x")
×
62
                precond.typeof(y, "number", "new: Wrong argument type for y")
×
63
                precond.typeof(z, "number", "new: Wrong argument type for z")
×
64
                precond.typeof(w, "number", "new: Wrong argument type for w")
×
65

66
                return new(x, y, z, w)
×
67

68
        -- {x, y, z, w} or {x=x, y=y, z=z, w=w}
69
        elseif type(x) == "table" then
×
70
                local xx, yy, zz, ww = x.x or x[1], x.y or x[2], x.z or x[3], x.w or x[4]
×
71
                precond.typeof(xx, "number", "new: Wrong argument type for x")
×
72
                precond.typeof(yy, "number", "new: Wrong argument type for y")
×
73
                precond.typeof(zz, "number", "new: Wrong argument type for z")
×
74
                precond.typeof(ww, "number", "new: Wrong argument type for w")
×
75

76
                return new(xx, yy, zz, ww)
×
77
        end
78

79
        return new(0, 0, 0, 1)
×
80
end
81

82
--- Create a quaternion from an angle/axis pair.
83
-- @tparam number angle Angle (in radians)
84
-- @param axis/x -- Can be of two types, a vec3 axis, or the x component of that axis
85
-- @param y axis -- y component of axis (optional, only if x component param used)
86
-- @param z axis -- z component of axis (optional, only if x component param used)
87
-- @treturn quat out
88
function quat.from_angle_axis(angle, axis, a3, a4)
×
89
        if axis and a3 and a4 then
×
90
                local x, y, z = axis, a3, a4
×
91
                local s = sin(angle * 0.5)
×
92
                local c = cos(angle * 0.5)
×
93
                return new(x * s, y * s, z * s, c)
×
94
        else
95
                return quat.from_angle_axis(angle, axis.x, axis.y, axis.z)
×
96
        end
97
end
98

99
--- Create a quaternion from a normal/up vector pair.
100
-- @tparam vec3 normal
101
-- @tparam vec3 up (optional)
102
-- @treturn quat out
103
function quat.from_direction(normal, up)
×
104
        local u = up or vec3.unit_z
×
105
        local n = normal:normalize()
×
106
        local a = u:cross(n)
×
107
        local d = u:dot(n)
×
108
        return new(a.x, a.y, a.z, d + 1)
×
109
end
110

111
--- Clone a quaternion.
112
-- @tparam quat a Quaternion to clone
113
-- @treturn quat out
114
function quat.clone(a)
×
115
        return new(a.x, a.y, a.z, a.w)
×
116
end
117

118
--- Add two quaternions.
119
-- @tparam quat a Left hand operand
120
-- @tparam quat b Right hand operand
121
-- @treturn quat out
122
function quat.add(a, b)
×
123
        return new(
×
124
                a.x + b.x,
×
125
                a.y + b.y,
×
126
                a.z + b.z,
×
127
                a.w + b.w
×
128
        )
129
end
130

131
--- Subtract a quaternion from another.
132
-- @tparam quat a Left hand operand
133
-- @tparam quat b Right hand operand
134
-- @treturn quat out
135
function quat.sub(a, b)
×
136
        return new(
×
137
                a.x - b.x,
×
138
                a.y - b.y,
×
139
                a.z - b.z,
×
140
                a.w - b.w
×
141
        )
142
end
143

144
--- Multiply two quaternions.
145
-- @tparam quat a Left hand operand
146
-- @tparam quat b Right hand operand
147
-- @treturn quat quaternion equivalent to "apply b, then a"
148
function quat.mul(a, b)
×
149
        return new(
×
150
                a.x * b.w + a.w * b.x + a.y * b.z - a.z * b.y,
×
151
                a.y * b.w + a.w * b.y + a.z * b.x - a.x * b.z,
×
152
                a.z * b.w + a.w * b.z + a.x * b.y - a.y * b.x,
×
153
                a.w * b.w - a.x * b.x - a.y * b.y - a.z * b.z
×
154
        )
155
end
156

157
--- Multiply a quaternion and a vec3.
158
-- @tparam quat a Left hand operand
159
-- @tparam vec3 b Right hand operand
160
-- @treturn vec3 out
161
function quat.mul_vec3(a, b)
×
162
        qv.x = a.x
×
163
        qv.y = a.y
×
164
        qv.z = a.z
×
165
        uv   = qv:cross(b)
×
166
        uuv  = qv:cross(uv)
×
167
        return b + ((uv * a.w) + uuv) * 2
×
168
end
169

170
--- Raise a normalized quaternion to a scalar power.
171
-- @tparam quat a Left hand operand (should be a unit quaternion)
172
-- @tparam number s Right hand operand
173
-- @treturn quat out
174
function quat.pow(a, s)
×
175
        -- Do it as a slerp between identity and a (code borrowed from slerp)
176
        if a.w < 0 then
×
177
                a   = -a
×
178
        end
179
        local dot = a.w
×
180

181
        dot = min(max(dot, -1), 1)
×
182

183
        local theta = acos(dot) * s
×
184
        local c = new(a.x, a.y, a.z, 0):normalize() * sin(theta)
×
185
        c.w = cos(theta)
×
186
        return c
×
187
end
188

189
--- Normalize a quaternion.
190
-- @tparam quat a Quaternion to normalize
191
-- @treturn quat out
192
function quat.normalize(a)
×
193
        if a:is_zero() then
×
194
                return new(0, 0, 0, 0)
×
195
        end
196
        return a:scale(1 / a:len())
×
197
end
198

199
--- Get the dot product of two quaternions.
200
-- @tparam quat a Left hand operand
201
-- @tparam quat b Right hand operand
202
-- @treturn number dot
203
function quat.dot(a, b)
×
204
        return a.x * b.x + a.y * b.y + a.z * b.z + a.w * b.w
×
205
end
206

207
--- Return the length of a quaternion.
208
-- @tparam quat a Quaternion to get length of
209
-- @treturn number len
210
function quat.len(a)
×
211
        return sqrt(a.x * a.x + a.y * a.y + a.z * a.z + a.w * a.w)
×
212
end
213

214
--- Return the squared length of a quaternion.
215
-- @tparam quat a Quaternion to get length of
216
-- @treturn number len
217
function quat.len2(a)
×
218
        return a.x * a.x + a.y * a.y + a.z * a.z + a.w * a.w
×
219
end
220

221
--- Multiply a quaternion by a scalar.
222
-- @tparam quat a Left hand operand
223
-- @tparam number s Right hand operand
224
-- @treturn quat out
225
function quat.scale(a, s)
×
226
        return new(
×
227
                a.x * s,
×
228
                a.y * s,
×
229
                a.z * s,
×
230
                a.w * s
×
231
        )
232
end
233

234
--- Alias of from_angle_axis.
235
-- @tparam number angle Angle (in radians)
236
-- @param axis/x -- Can be of two types, a vec3 axis, or the x component of that axis
237
-- @param y axis -- y component of axis (optional, only if x component param used)
238
-- @param z axis -- z component of axis (optional, only if x component param used)
239
-- @treturn quat out
240
function quat.rotate(angle, axis, a3, a4)
×
241
        return quat.from_angle_axis(angle, axis, a3, a4)
×
242
end
243

244
--- Return the conjugate of a quaternion.
245
-- @tparam quat a Quaternion to conjugate
246
-- @treturn quat out
247
function quat.conjugate(a)
×
248
        return new(-a.x, -a.y, -a.z, a.w)
×
249
end
250

251
--- Return the inverse of a quaternion.
252
-- @tparam quat a Quaternion to invert
253
-- @treturn quat out
254
function quat.inverse(a)
×
255
        tmp.x = -a.x
×
256
        tmp.y = -a.y
×
257
        tmp.z = -a.z
×
258
        tmp.w =  a.w
×
259
        return tmp:normalize()
×
260
end
261

262
--- Return the reciprocal of a quaternion.
263
-- @tparam quat a Quaternion to reciprocate
264
-- @treturn quat out
265
function quat.reciprocal(a)
×
266
        if a:is_zero() then
×
267
                error("Cannot reciprocate a zero quaternion")
×
268
                return false
×
269
        end
270

271
        tmp.x = -a.x
×
272
        tmp.y = -a.y
×
273
        tmp.z = -a.z
×
274
        tmp.w =  a.w
×
275

276
        return tmp:scale(1 / a:len2())
×
277
end
278

279
--- Lerp between two quaternions.
280
-- @tparam quat a Left hand operand
281
-- @tparam quat b Right hand operand
282
-- @tparam number s Step value
283
-- @treturn quat out
284
function quat.lerp(a, b, s)
×
285
        return (a + (b - a) * s):normalize()
×
286
end
287

288
--- Slerp between two quaternions.
289
-- @tparam quat a Left hand operand
290
-- @tparam quat b Right hand operand
291
-- @tparam number s Step value
292
-- @treturn quat out
293
function quat.slerp(a, b, s)
×
294
        local dot = a:dot(b)
×
295

296
        if dot < 0 then
×
297
                a   = -a
×
298
                dot = -dot
×
299
        end
300

301
        if dot > DOT_THRESHOLD then
×
302
                return a:lerp(b, s)
×
303
        end
304

305
        dot = min(max(dot, -1), 1)
×
306

307
        local theta = acos(dot) * s
×
308
        local c = (b - a * dot):normalize()
×
309
        return a * cos(theta) + c * sin(theta)
×
310
end
311

312
--- Unpack a quaternion into individual components.
313
-- @tparam quat a Quaternion to unpack
314
-- @treturn number x
315
-- @treturn number y
316
-- @treturn number z
317
-- @treturn number w
318
function quat.unpack(a)
×
319
        return a.x, a.y, a.z, a.w
×
320
end
321

322
--- Return a boolean showing if a table is or is not a quat.
323
-- @tparam quat a Quaternion to be tested
324
-- @treturn boolean is_quat
325
function quat.is_quat(a)
×
326
        if type(a) == "cdata" then
×
327
                return ffi.istype("cpml_quat", a)
×
328
        end
329

330
        return
×
331
                type(a)   == "table"  and
×
332
                type(a.x) == "number" and
×
333
                type(a.y) == "number" and
×
334
                type(a.z) == "number" and
×
335
                type(a.w) == "number"
×
336
end
337

338
--- Return a boolean showing if a table is or is not a zero quat.
339
-- @tparam quat a Quaternion to be tested
340
-- @treturn boolean is_zero
341
function quat.is_zero(a)
×
342
        return
×
343
                a.x == 0 and
×
344
                a.y == 0 and
×
345
                a.z == 0 and
×
346
                a.w == 0
×
347
end
348

349
--- Return a boolean showing if a table is or is not a real quat.
350
-- @tparam quat a Quaternion to be tested
351
-- @treturn boolean is_real
352
function quat.is_real(a)
×
353
        return
×
354
                a.x == 0 and
×
355
                a.y == 0 and
×
356
                a.z == 0
×
357
end
358

359
--- Return a boolean showing if a table is or is not an imaginary quat.
360
-- @tparam quat a Quaternion to be tested
361
-- @treturn boolean is_imaginary
362
function quat.is_imaginary(a)
×
363
        return a.w == 0
×
364
end
365

366
--- Return whether any component is NaN
367
-- @tparam quat a Quaternion to be tested
368
-- @treturn boolean if x,y,z, or w is NaN
369
function quat.has_nan(a)
×
370
        return private.is_nan(a.x) or
×
371
                private.is_nan(a.y) or
×
372
                private.is_nan(a.z) or
×
373
                private.is_nan(a.w)
×
374
end
375

376
--- Convert a quaternion into an angle plus axis components.
377
-- @tparam quat a Quaternion to convert
378
-- @tparam identityAxis vec3 of axis to use on identity/degenerate quaternions (optional, default returns 0,0,0,1)
379
-- @treturn number angle
380
-- @treturn x axis-x
381
-- @treturn y axis-y
382
-- @treturn z axis-z
383
function quat.to_angle_axis_unpack(a, identityAxis)
×
384
        if a.w > 1 or a.w < -1 then
×
385
                a = a:normalize()
×
386
        end
387

388
        -- If length of xyz components is less than DBL_EPSILON, this is zero or close enough (an identity quaternion)
389
        -- Normally an identity quat would return a nonsense answer, so we return an arbitrary zero rotation early.
390
        -- FIXME: Is it safe to assume there are *no* valid quaternions with nonzero degenerate lengths?
391
        if a.x*a.x + a.y*a.y + a.z*a.z < constants.DBL_EPSILON*constants.DBL_EPSILON then
×
392
                if identityAxis then
×
393
                        return 0,identityAxis:unpack()
×
394
                else
395
                        return 0,0,0,1
×
396
                end
397
        end
398

399
        local x, y, z
400
        local angle = 2 * acos(a.w)
×
401
        local s     = sqrt(1 - a.w * a.w)
×
402

403
        if s < DBL_EPSILON then
×
404
                x = a.x
×
405
                y = a.y
×
406
                z = a.z
×
407
        else
408
                x = a.x / s
×
409
                y = a.y / s
×
410
                z = a.z / s
×
411
        end
412

413
        return angle, x, y, z
×
414
end
415

416
--- Convert a quaternion into an angle/axis pair.
417
-- @tparam quat a Quaternion to convert
418
-- @tparam identityAxis vec3 of axis to use on identity/degenerate quaternions (optional, default returns 0,vec3(0,0,1))
419
-- @treturn number angle
420
-- @treturn vec3 axis
421
function quat.to_angle_axis(a, identityAxis)
×
422
        local angle, x, y, z = a:to_angle_axis_unpack(identityAxis)
×
423
        return angle, vec3(x, y, z)
×
424
end
425

426
--- Convert a quaternion into a vec3.
427
-- @tparam quat a Quaternion to convert
428
-- @treturn vec3 out
429
function quat.to_vec3(a)
×
430
        return vec3(a.x, a.y, a.z)
×
431
end
432

433
--- Return a formatted string.
434
-- @tparam quat a Quaternion to be turned into a string
435
-- @treturn string formatted
436
function quat.to_string(a)
×
437
        return string.format("(%+0.3f,%+0.3f,%+0.3f,%+0.3f)", a.x, a.y, a.z, a.w)
×
438
end
439

440

441
--- Convert a quaternion to an Euler angle
442
-- @tparam quat a Quaternion to convert
443
-- @treturn vec3 euler angle in radians
444
function quat.to_euler()
×
445
end
446

447
quat_mt.__index    = quat
×
448
quat_mt.__tostring = quat.to_string
×
449

450
function quat_mt.__call(_, x, y, z, w)
×
451
        return quat.new(x, y, z, w)
×
452
end
453

454
function quat_mt.__unm(a)
×
455
        return a:scale(-1)
×
456
end
457

458
function quat_mt.__eq(a,b)
×
459
        if not quat.is_quat(a) or not quat.is_quat(b) then
×
460
                return false
×
461
        end
462
        return a.x == b.x and a.y == b.y and a.z == b.z and a.w == b.w
×
463
end
464

465
function quat_mt.__add(a, b)
×
466
        precond.assert(quat.is_quat(a), "__add: Wrong argument type '%s' for left hand operand. (<cpml.quat> expected)", type(a))
×
467
        precond.assert(quat.is_quat(b), "__add: Wrong argument type '%s' for right hand operand. (<cpml.quat> expected)", type(b))
×
468
        return a:add(b)
×
469
end
470

471
function quat_mt.__sub(a, b)
×
472
        precond.assert(quat.is_quat(a), "__sub: Wrong argument type '%s' for left hand operand. (<cpml.quat> expected)", type(a))
×
473
        precond.assert(quat.is_quat(b), "__sub: Wrong argument type '%s' for right hand operand. (<cpml.quat> expected)", type(b))
×
474
        return a:sub(b)
×
475
end
476

477
function quat_mt.__mul(a, b)
×
478
        precond.assert(quat.is_quat(a), "__mul: Wrong argument type '%s' for left hand operand. (<cpml.quat> expected)", type(a))
×
479
        assert(quat.is_quat(b) or vec3.is_vec3(b) or type(b) == "number", "__mul: Wrong argument type for right hand operand. (<cpml.quat> or <cpml.vec3> or <number> expected)")
×
480

481
        if quat.is_quat(b) then
×
482
                return a:mul(b)
×
483
        end
484

485
        if type(b) == "number" then
×
486
                return a:scale(b)
×
487
        end
488

489
        return a:mul_vec3(b)
×
490
end
491

492
function quat_mt.__pow(a, n)
×
493
        precond.assert(quat.is_quat(a), "__pow: Wrong argument type '%s' for left hand operand. (<cpml.quat> expected)", type(a))
×
494
        precond.typeof(n, "number", "__pow: Wrong argument type for right hand operand.")
×
495
        return a:pow(n)
×
496
end
497

498
if status then
×
499
        xpcall(function() -- Allow this to silently fail; assume failure means someone messed with package.loaded
×
500
                ffi.metatype(new, quat_mt)
×
501
        end, function() end)
×
502
end
503

504
return setmetatable({}, quat_mt)
×
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

© 2026 Coveralls, Inc