• 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

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

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

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

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

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

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

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

65
                return new(x, y, z, w)
74✔
66

67
        -- {x, y, z, w} or {x=x, y=y, z=z, w=w}
68
        elseif type(x) == "table" then
14✔
69
                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]
4✔
70
                assert(type(xx) == "number", "new: Wrong argument type for x (<number> expected)")
4✔
71
                assert(type(yy) == "number", "new: Wrong argument type for y (<number> expected)")
4✔
72
                assert(type(zz) == "number", "new: Wrong argument type for z (<number> expected)")
4✔
73
                assert(type(ww) == "number", "new: Wrong argument type for w (<number> expected)")
4✔
74

75
                return new(xx, yy, zz, ww)
4✔
76
        end
77

78
        return new(0, 0, 0, 1)
10✔
79
end
80

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

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

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

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

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

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

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

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

180
        dot = min(max(dot, -1), 1)
12✔
181

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

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

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

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

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

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

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

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

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

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

270
        tmp.x = -a.x
4✔
271
        tmp.y = -a.y
4✔
272
        tmp.z = -a.z
4✔
273
        tmp.w =  a.w
4✔
274

275
        return tmp:scale(1 / a:len2())
4✔
276
end
277

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

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

295
        if dot < 0 then
2✔
296
                a   = -a
×
297
                dot = -dot
×
298
        end
299

300
        if dot > DOT_THRESHOLD then
2✔
301
                return a:lerp(b, s)
2✔
302
        end
303

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

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

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

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

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

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

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

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

365
--- Convert a quaternion into an angle plus axis components.
366
-- @tparam quat a Quaternion to convert
367
-- @tparam identityAxis vec3 of axis to use on identity/degenerate quaternions (optional, default returns 0,0,0,1)
368
-- @treturn number angle
369
-- @treturn x axis-x
370
-- @treturn y axis-y
371
-- @treturn z axis-z
372
function quat.to_angle_axis_unpack(a, identityAxis)
8✔
373
        if a.w > 1 or a.w < -1 then
14✔
374
                a = a:normalize()
4✔
375
        end
376

377
        -- If length of xyz components is less than DBL_EPSILON, this is zero or close enough (an identity quaternion)
378
        -- Normally an identity quat would return a nonsense answer, so we return an arbitrary zero rotation early.
379
        -- FIXME: Is it safe to assume there are *no* valid quaternions with nonzero degenerate lengths?
380
        if a.x*a.x + a.y*a.y + a.z*a.z < constants.DBL_EPSILON*constants.DBL_EPSILON then
14✔
381
                if identityAxis then
4✔
382
                        return 0,identityAxis:unpack()
2✔
383
                else
384
                        return 0,0,0,1
2✔
385
                end
386
        end
387

388
        local x, y, z
389
        local angle = 2 * acos(a.w)
10✔
390
        local s     = sqrt(1 - a.w * a.w)
10✔
391

392
        if s < DBL_EPSILON then
10✔
393
                x = a.x
2✔
394
                y = a.y
2✔
395
                z = a.z
2✔
396
        else
397
                x = a.x / s
8✔
398
                y = a.y / s
8✔
399
                z = a.z / s
8✔
400
        end
401

402
        return angle, x, y, z
10✔
403
end
404

405
--- Convert a quaternion into an angle/axis pair.
406
-- @tparam quat a Quaternion to convert
407
-- @tparam identityAxis vec3 of axis to use on identity/degenerate quaternions (optional, default returns 0,vec3(0,0,1))
408
-- @treturn number angle
409
-- @treturn vec3 axis
410
function quat.to_angle_axis(a, identityAxis)
8✔
411
        local angle, x, y, z = a:to_angle_axis_unpack(identityAxis)
10✔
412
        return angle, vec3(x, y, z)
10✔
413
end
414

415
--- Convert a quaternion into a vec3.
416
-- @tparam quat a Quaternion to convert
417
-- @treturn vec3 out
418
function quat.to_vec3(a)
8✔
419
        return vec3(a.x, a.y, a.z)
2✔
420
end
421

422
--- Return a formatted string.
423
-- @tparam quat a Quaternion to be turned into a string
424
-- @treturn string formatted
425
function quat.to_string(a)
8✔
426
        return string.format("(%+0.3f,%+0.3f,%+0.3f,%+0.3f)", a.x, a.y, a.z, a.w)
2✔
427
end
428

429
quat_mt.__index    = quat
8✔
430
quat_mt.__tostring = quat.to_string
8✔
431

432
function quat_mt.__call(_, x, y, z, w)
8✔
433
        return quat.new(x, y, z, w)
86✔
434
end
435

436
function quat_mt.__unm(a)
8✔
437
        return a:scale(-1)
2✔
438
end
439

440
function quat_mt.__eq(a,b)
8✔
441
        if not quat.is_quat(a) or not quat.is_quat(b) then
14✔
442
                return false
×
443
        end
444
        return a.x == b.x and a.y == b.y and a.z == b.z and a.w == b.w
14✔
445
end
446

447
function quat_mt.__add(a, b)
8✔
448
        assert(quat.is_quat(a), "__add: Wrong argument type for left hand operand. (<cpml.quat> expected)")
6✔
449
        assert(quat.is_quat(b), "__add: Wrong argument type for right hand operand. (<cpml.quat> expected)")
6✔
450
        return a:add(b)
6✔
451
end
452

453
function quat_mt.__sub(a, b)
8✔
454
        assert(quat.is_quat(a), "__sub: Wrong argument type for left hand operand. (<cpml.quat> expected)")
6✔
455
        assert(quat.is_quat(b), "__sub: Wrong argument type for right hand operand. (<cpml.quat> expected)")
6✔
456
        return a:sub(b)
6✔
457
end
458

459
function quat_mt.__mul(a, b)
8✔
460
        assert(quat.is_quat(a), "__mul: Wrong argument type for left hand operand. (<cpml.quat> expected)")
30✔
461
        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)")
30✔
462

463
        if quat.is_quat(b) then
30✔
464
                return a:mul(b)
4✔
465
        end
466

467
        if type(b) == "number" then
26✔
468
                return a:scale(b)
18✔
469
        end
470

471
        return a:mul_vec3(b)
8✔
472
end
473

474
function quat_mt.__pow(a, n)
8✔
475
        assert(quat.is_quat(a), "__pow: Wrong argument type for left hand operand. (<cpml.quat> expected)")
6✔
476
        assert(type(n) == "number", "__pow: Wrong argument type for right hand operand. (<number> expected)")
6✔
477
        return a:pow(n)
6✔
478
end
479

480
if status then
8✔
481
        xpcall(function() -- Allow this to silently fail; assume failure means someone messed with package.loaded
×
482
                ffi.metatype(new, quat_mt)
×
483
        end, function() end)
×
484
end
485

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