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

excessive / cpml / 1

31 Mar 2022 01:05PM UTC coverage: 44.185% (-13.5%) from 57.717%
1

push

github

web-flow
Merge pull request #75 from idbrii/precond

Include offending type in precondition failure msg

50 of 60 new or added lines in 6 files covered. (83.33%)

205 existing lines in 6 files now uncovered.

76716 of 173626 relevant lines covered (44.18%)

1272.48 hits per line

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

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

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

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

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

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

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

4✔
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
4✔
59
function quat.new(x, y, z, w)
2,264✔
60
        -- number, number, number, number
48✔
61
        if x and y and z and w then
24,941✔
62
                precond.typeof(x, "number", "new: Wrong argument type for x")
21,023✔
63
                precond.typeof(y, "number", "new: Wrong argument type for y")
21,016✔
64
                precond.typeof(z, "number", "new: Wrong argument type for z")
21,016✔
65
                precond.typeof(w, "number", "new: Wrong argument type for w")
20,979✔
66

74✔
67
                return new(x, y, z, w)
20,942✔
68

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

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

5✔
80
        return new(0, 0, 0, 1)
2,830✔
81
end
5✔
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
4✔
89
function quat.from_angle_axis(angle, axis, a3, a4)
2,269✔
90
        if axis and a3 and a4 then
2,837✔
91
                local x, y, z = axis, a3, a4
1,706✔
92
                local s = sin(angle * 0.5)
1,704✔
93
                local c = cos(angle * 0.5)
1,704✔
94
                return new(x * s, y * s, z * s, c)
1,701✔
95
        else
5✔
96
                return quat.from_angle_axis(angle, axis.x, axis.y, axis.z)
1,132✔
97
        end
2✔
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
4✔
104
function quat.from_direction(normal, up)
2,261✔
105
        local u = up or vec3.unit_z
574✔
106
        local n = normal:normalize()
568✔
107
        local a = u:cross(n)
568✔
108
        local d = u:dot(n)
568✔
109
        return new(a.x, a.y, a.z, d + 1)
567✔
110
end
2✔
111

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

119
--- Add two quaternions.
120
-- @tparam quat a Left hand operand
121
-- @tparam quat b Right hand operand
122
-- @treturn quat out
4✔
123
function quat.add(a, b)
2,268✔
124
        return new(
4,532✔
125
                a.x + b.x,
2,280✔
126
                a.y + b.y,
2,272✔
127
                a.z + b.z,
2,272✔
128
                a.w + b.w
2,268✔
129
        )
8✔
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
4✔
136
function quat.sub(a, b)
2,268✔
137
        return new(
4,532✔
138
                a.x - b.x,
2,280✔
139
                a.y - b.y,
2,272✔
140
                a.z - b.z,
2,272✔
141
                a.w - b.w
2,268✔
142
        )
8✔
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"
4✔
149
function quat.mul(a, b)
2,266✔
150
        return new(
3,401✔
151
                a.x * b.w + a.w * b.x + a.y * b.z - a.z * b.y,
1,710✔
152
                a.y * b.w + a.w * b.y + a.z * b.x - a.x * b.z,
1,704✔
153
                a.z * b.w + a.w * b.z + a.x * b.y - a.y * b.x,
1,704✔
154
                a.w * b.w - a.x * b.x - a.y * b.y - a.z * b.z
1,701✔
155
        )
6✔
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 quat out
4✔
162
function quat.mul_vec3(a, b)
2,265✔
163
        qv.x = a.x
2,838✔
164
        qv.y = a.y
2,840✔
165
        qv.z = a.z
2,840✔
166
        uv   = qv:cross(b)
2,840✔
167
        uuv  = qv:cross(uv)
2,840✔
168
        return b + ((uv * a.w) + uuv) * 2
2,835✔
169
end
10✔
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
4✔
175
function quat.pow(a, s)
2,260✔
176
        -- Do it as a slerp between identity and a (code borrowed from slerp)
14✔
177
        if a.w < 0 then
3,390✔
UNCOV
178
                a   = -a
12✔
179
        end
6✔
180
        local dot = a.w
3,390✔
181

18✔
182
        dot = min(max(dot, -1), 1)
3,390✔
183

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

190
--- Normalize a quaternion.
191
-- @tparam quat a Quaternion to normalize
192
-- @treturn quat out
4✔
193
function quat.normalize(a)
2,278✔
194
        if a:is_zero() then
10,178✔
UNCOV
195
                return new(0, 0, 0, 0)
36✔
196
        end
18✔
197
        return a:scale(1 / a:len())
10,170✔
198
end
36✔
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
4✔
204
function quat.dot(a, b)
2,264✔
205
        return a.x * b.x + a.y * b.y + a.z * b.z + a.w * b.w
2,268✔
206
end
8✔
207

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

215
--- Return the squared length of a quaternion.
216
-- @tparam quat a Quaternion to get length of
217
-- @treturn number len
4✔
218
function quat.len2(a)
2,263✔
219
        return a.x * a.x + a.y * a.y + a.z * a.z + a.w * a.w
1,703✔
220
end
6✔
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
4✔
226
function quat.scale(a, s)
2,322✔
227
        return new(
35,069✔
228
                a.x * s,
17,670✔
229
                a.y * s,
17,608✔
230
                a.z * s,
17,608✔
231
                a.w * s
17,577✔
232
        )
62✔
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
4✔
241
function quat.rotate(angle, axis, a3, a4)
2,260✔
UNCOV
242
        return quat.from_angle_axis(angle, axis, a3, a4)
8✔
243
end
244

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

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

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

2✔
272
        tmp.x = -a.x
1,132✔
273
        tmp.y = -a.y
1,136✔
274
        tmp.z = -a.z
1,136✔
275
        tmp.w =  a.w
1,134✔
276

6✔
277
        return tmp:scale(1 / a:len2())
1,130✔
278
end
4✔
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
4✔
285
function quat.lerp(a, b, s)
2,262✔
286
        return (a + (b - a) * s):normalize()
1,138✔
287
end
4✔
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
4✔
294
function quat.slerp(a, b, s)
2,261✔
295
        local dot = a:dot(b)
573✔
296

3✔
297
        if dot < 0 then
565✔
UNCOV
298
                a   = -a
2✔
299
                dot = -dot
300
        end
301

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

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

UNCOV
308
        local theta = acos(dot) * s
309
        local c = (b - a * dot):normalize()
×
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
4✔
319
function quat.unpack(a)
2,261✔
320
        return a.x, a.y, a.z, a.w
573✔
321
end
2✔
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
4✔
326
function quat.is_quat(a)
2,335✔
327
        if type(a) == "cdata" then
42,383✔
UNCOV
328
                return ffi.istype("cpml_quat", a)
150✔
329
        end
330

UNCOV
331
        return
75✔
332
                type(a)   == "table"  and
42,432✔
333
                type(a.x) == "number" and
32,412✔
334
                type(a.y) == "number" and
32,376✔
335
                type(a.z) == "number" and
32,394✔
336
                type(a.w) == "number"
42,489✔
337
end
150✔
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
4✔
342
function quat.is_zero(a)
2,260✔
UNCOV
343
        return
29✔
344
                a.x == 0 and
11,867✔
345
                a.y == 0 and
609✔
346
                a.z == 0 and
588✔
347
                a.w == 0
11,868✔
348
end
42✔
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
4✔
353
function quat.is_real(a)
2,260✔
UNCOV
354
        return
9✔
355
                a.x == 0 and
566✔
356
                a.y == 0 and
568✔
357
                a.z == 0
567✔
358
end
2✔
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
4✔
363
function quat.is_imaginary(a)
2,261✔
364
        return a.w == 0
573✔
365
end
2✔
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
4✔
370
function quat.has_nan(a)
2,260✔
UNCOV
371
        return private.is_nan(a.x) or
8✔
372
                private.is_nan(a.y) or
×
373
                private.is_nan(a.z) or
×
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
4✔
384
function quat.to_angle_axis_unpack(a, identityAxis)
2,267✔
385
        if a.w > 1 or a.w < -1 then
3,971✔
386
                a = a:normalize()
1,144✔
387
        end
4✔
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?
7✔
392
        if a.x*a.x + a.y*a.y + a.z*a.z < constants.DBL_EPSILON*constants.DBL_EPSILON then
3,963✔
393
                if identityAxis then
1,145✔
394
                        return 0,identityAxis:unpack()
569✔
395
                else
3✔
396
                        return 0,0,0,1
565✔
397
                end
2✔
398
        end
399

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

15✔
404
        if s < DBL_EPSILON then
2,832✔
405
                x = a.x
576✔
406
                y = a.y
568✔
407
                z = a.z
567✔
408
        else
6✔
409
                x = a.x / s
2,270✔
410
                y = a.y / s
2,278✔
411
                z = a.z / s
2,274✔
412
        end
8✔
413

5✔
414
        return angle, x, y, z
2,831✔
415
end
10✔
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
4✔
422
function quat.to_angle_axis(a, identityAxis)
2,265✔
423
        local angle, x, y, z = a:to_angle_axis_unpack(identityAxis)
2,844✔
424
        return angle, vec3(x, y, z)
2,841✔
425
end
10✔
426

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

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

4✔
441
quat_mt.__index    = quat
2,260✔
442
quat_mt.__tostring = quat.to_string
2,268✔
443

16✔
444
function quat_mt.__call(_, x, y, z, w)
2,299✔
445
        return quat.new(x, y, z, w)
24,264✔
446
end
129✔
447

4✔
448
function quat_mt.__unm(a)
2,257✔
449
        return a:scale(-1)
576✔
450
end
3✔
451

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

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

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

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

60✔
475
        if quat.is_quat(b) then
8,462✔
476
                return a:mul(b)
1,173✔
477
        end
6✔
478

13✔
479
        if type(b) == "number" then
7,341✔
480
                return a:scale(b)
5,115✔
481
        end
27✔
482

4✔
483
        return a:mul_vec3(b)
2,256✔
484
end
12✔
485

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

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

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