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

excessive / cpml / 13079282556

07 May 2022 10:53PM UTC coverage: 57.717% (+43.7%) from 14.013%
13079282556

push

github

shakesoda
fix typo in mat4.mul

0 of 1 new or added line in 1 file covered. (0.0%)

527 existing lines in 19 files now uncovered.

4581 of 7937 relevant lines covered (57.72%)

52.86 hits per line

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

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

4
local modules       = (...):gsub('%.[^%.]+$', '') .. "."
4✔
5
local constants     = require(modules .. "constants")
4✔
6
local vec3          = require(modules .. "vec3")
4✔
7
local precond       = require(modules .. "_private_precond")
4✔
8
local private       = require(modules .. "_private_utils")
4✔
9
local DOT_THRESHOLD = constants.DOT_THRESHOLD
4✔
10
local DBL_EPSILON   = constants.DBL_EPSILON
4✔
11
local acos          = math.acos
4✔
12
local cos           = math.cos
4✔
13
local sin           = math.sin
4✔
14
local min           = math.min
4✔
15
local max           = math.max
4✔
16
local sqrt          = math.sqrt
4✔
17
local quat          = {}
4✔
18
local quat_mt       = {}
4✔
19

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

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

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

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

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

67
                return new(x, y, z, w)
37✔
68

69
        -- {x, y, z, w} or {x=x, y=y, z=z, w=w}
70
        elseif type(x) == "table" then
7✔
71
                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]
2✔
72
                precond.typeof(xx, "number", "new: Wrong argument type for x")
2✔
73
                precond.typeof(yy, "number", "new: Wrong argument type for y")
2✔
74
                precond.typeof(zz, "number", "new: Wrong argument type for z")
2✔
75
                precond.typeof(ww, "number", "new: Wrong argument type for w")
2✔
76

77
                return new(xx, yy, zz, ww)
2✔
78
        end
79

80
        return new(0, 0, 0, 1)
5✔
81
end
82

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

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

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

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

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

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

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

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

182
        dot = min(max(dot, -1), 1)
6✔
183

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

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

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

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

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

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

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

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

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

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

272
        tmp.x = -a.x
2✔
273
        tmp.y = -a.y
2✔
274
        tmp.z = -a.z
2✔
275
        tmp.w =  a.w
2✔
276

277
        return tmp:scale(1 / a:len2())
2✔
278
end
279

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

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

297
        if dot < 0 then
1✔
298
                a   = -a
×
UNCOV
299
                dot = -dot
×
300
        end
301

302
        if dot > DOT_THRESHOLD then
1✔
303
                return a:lerp(b, s)
1✔
304
        end
305

UNCOV
306
        dot = min(max(dot, -1), 1)
×
307

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

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

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

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

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

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

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

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

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

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

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

404
        if s < DBL_EPSILON then
5✔
405
                x = a.x
1✔
406
                y = a.y
1✔
407
                z = a.z
1✔
408
        else
409
                x = a.x / s
4✔
410
                y = a.y / s
4✔
411
                z = a.z / s
4✔
412
        end
413

414
        return angle, x, y, z
5✔
415
end
416

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

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

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

441
quat_mt.__index    = quat
4✔
442
quat_mt.__tostring = quat.to_string
4✔
443

444
function quat_mt.__call(_, x, y, z, w)
4✔
445
        return quat.new(x, y, z, w)
43✔
446
end
447

448
function quat_mt.__unm(a)
4✔
449
        return a:scale(-1)
1✔
450
end
451

452
function quat_mt.__eq(a,b)
4✔
453
        if not quat.is_quat(a) or not quat.is_quat(b) then
7✔
454
                return false
×
455
        end
456
        return a.x == b.x and a.y == b.y and a.z == b.z and a.w == b.w
7✔
457
end
458

459
function quat_mt.__add(a, b)
4✔
460
        precond.assert(quat.is_quat(a), "__add: Wrong argument type '%s' for left hand operand. (<cpml.quat> expected)", type(a))
3✔
461
        precond.assert(quat.is_quat(b), "__add: Wrong argument type '%s' for right hand operand. (<cpml.quat> expected)", type(b))
3✔
462
        return a:add(b)
3✔
463
end
464

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

471
function quat_mt.__mul(a, b)
4✔
472
        precond.assert(quat.is_quat(a), "__mul: Wrong argument type '%s' for left hand operand. (<cpml.quat> expected)", type(a))
15✔
473
        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)")
15✔
474

475
        if quat.is_quat(b) then
15✔
476
                return a:mul(b)
2✔
477
        end
478

479
        if type(b) == "number" then
13✔
480
                return a:scale(b)
9✔
481
        end
482

483
        return a:mul_vec3(b)
4✔
484
end
485

486
function quat_mt.__pow(a, n)
4✔
487
        precond.assert(quat.is_quat(a), "__pow: Wrong argument type '%s' for left hand operand. (<cpml.quat> expected)", type(a))
3✔
488
        precond.typeof(n, "number", "__pow: Wrong argument type for right hand operand.")
3✔
489
        return a:pow(n)
3✔
490
end
491

492
if status then
4✔
493
        xpcall(function() -- Allow this to silently fail; assume failure means someone messed with package.loaded
×
494
                ffi.metatype(new, quat_mt)
×
495
        end, function() end)
×
496
end
497

498
return setmetatable({}, quat_mt)
4✔
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