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

excessive / cpml / 1

21 Apr 2022 05:46PM UTC coverage: 54.321% (+0.7%) from 53.574%
1

push

github

web-flow
Merge pull request #76 from xiejiangzhi/xjz

Add Vec3.angle_to

23 of 23 new or added lines in 2 files covered. (100.0%)

130 existing lines in 7 files now uncovered.

5424 of 9985 relevant lines covered (54.32%)

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

20
-- Private constructor.
21
local function new(x, y, z, w)
22
        return setmetatable({
1,540✔
23
                x = x or 0,
770✔
24
                y = y or 0,
770✔
25
                z = z or 0,
770✔
26
                w = w or 1
770✔
27
        }, quat_mt)
1,540✔
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
28✔
33
        status, ffi = pcall(require, "ffi")
×
34
        if status then
×
UNCOV
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()
28✔
42
local qv, uv, uuv = vec3(), vec3(), vec3()
28✔
43

44
--- Constants
45
-- @table quat
46
-- @field unit Unit quaternion
47
-- @field zero Empty quaternion
48
quat.unit = new(0, 0, 0, 1)
28✔
49
quat.zero = new(0, 0, 0, 0)
28✔
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)
28✔
60
        -- number, number, number, number
61
        if x and y and z and w then
308✔
62
                precond.typeof(x, "number", "new: Wrong argument type for x")
259✔
63
                precond.typeof(y, "number", "new: Wrong argument type for y")
259✔
64
                precond.typeof(z, "number", "new: Wrong argument type for z")
259✔
65
                precond.typeof(w, "number", "new: Wrong argument type for w")
259✔
66

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

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

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

80
        return new(0, 0, 0, 1)
35✔
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)
28✔
90
        if axis and a3 and a4 then
35✔
91
                local x, y, z = axis, a3, a4
21✔
92
                local s = sin(angle * 0.5)
21✔
93
                local c = cos(angle * 0.5)
21✔
94
                return new(x * s, y * s, z * s, c)
21✔
95
        else
96
                return quat.from_angle_axis(angle, axis.x, axis.y, axis.z)
14✔
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)
28✔
105
        local u = up or vec3.unit_z
7✔
106
        local n = normal:normalize()
7✔
107
        local a = u:cross(n)
7✔
108
        local d = u:dot(n)
7✔
109
        return new(a.x, a.y, a.z, d + 1)
7✔
110
end
111

112
--- Clone a quaternion.
113
-- @tparam quat a Quaternion to clone
114
-- @treturn quat out
115
function quat.clone(a)
28✔
116
        return new(a.x, a.y, a.z, a.w)
7✔
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)
28✔
124
        return new(
56✔
125
                a.x + b.x,
28✔
126
                a.y + b.y,
28✔
127
                a.z + b.z,
28✔
128
                a.w + b.w
28✔
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)
28✔
137
        return new(
56✔
138
                a.x - b.x,
28✔
139
                a.y - b.y,
28✔
140
                a.z - b.z,
28✔
141
                a.w - b.w
28✔
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)
28✔
150
        return new(
42✔
151
                a.x * b.w + a.w * b.x + a.y * b.z - a.z * b.y,
21✔
152
                a.y * b.w + a.w * b.y + a.z * b.x - a.x * b.z,
21✔
153
                a.z * b.w + a.w * b.z + a.x * b.y - a.y * b.x,
21✔
154
                a.w * b.w - a.x * b.x - a.y * b.y - a.z * b.z
21✔
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 quat out
162
function quat.mul_vec3(a, b)
28✔
163
        qv.x = a.x
35✔
164
        qv.y = a.y
35✔
165
        qv.z = a.z
35✔
166
        uv   = qv:cross(b)
35✔
167
        uuv  = qv:cross(uv)
35✔
168
        return b + ((uv * a.w) + uuv) * 2
35✔
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)
28✔
176
        -- Do it as a slerp between identity and a (code borrowed from slerp)
177
        if a.w < 0 then
42✔
UNCOV
178
                a   = -a
×
179
        end
180
        local dot = a.w
42✔
181

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

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

190
--- Normalize a quaternion.
191
-- @tparam quat a Quaternion to normalize
192
-- @treturn quat out
193
function quat.normalize(a)
28✔
194
        if a:is_zero() then
126✔
UNCOV
195
                return new(0, 0, 0, 0)
×
196
        end
197
        return a:scale(1 / a:len())
126✔
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)
28✔
205
        return a.x * b.x + a.y * b.y + a.z * b.z + a.w * b.w
28✔
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)
28✔
212
        return sqrt(a.x * a.x + a.y * a.y + a.z * a.z + a.w * a.w)
133✔
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)
28✔
219
        return a.x * a.x + a.y * a.y + a.z * a.z + a.w * a.w
21✔
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)
28✔
227
        return new(
434✔
228
                a.x * s,
217✔
229
                a.y * s,
217✔
230
                a.z * s,
217✔
231
                a.w * s
217✔
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)
28✔
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)
28✔
249
        return new(-a.x, -a.y, -a.z, a.w)
7✔
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)
28✔
256
        tmp.x = -a.x
7✔
257
        tmp.y = -a.y
7✔
258
        tmp.z = -a.z
7✔
259
        tmp.w =  a.w
7✔
260
        return tmp:normalize()
7✔
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)
28✔
267
        if a:is_zero() then
14✔
UNCOV
268
                error("Cannot reciprocate a zero quaternion")
×
UNCOV
269
                return false
×
270
        end
271

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

277
        return tmp:scale(1 / a:len2())
14✔
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)
28✔
286
        return (a + (b - a) * s):normalize()
14✔
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)
28✔
295
        local dot = a:dot(b)
7✔
296

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

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

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

308
        local theta = acos(dot) * s
×
UNCOV
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)
28✔
320
        return a.x, a.y, a.z, a.w
7✔
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)
28✔
327
        if type(a) == "cdata" then
525✔
UNCOV
328
                return ffi.istype("cpml_quat", a)
×
329
        end
330

UNCOV
331
        return
×
332
                type(a)   == "table"  and
525✔
333
                type(a.x) == "number" and
399✔
334
                type(a.y) == "number" and
399✔
335
                type(a.z) == "number" and
399✔
336
                type(a.w) == "number"
525✔
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)
28✔
UNCOV
343
        return
×
344
                a.x == 0 and
147✔
345
                a.y == 0 and
7✔
346
                a.z == 0 and
7✔
347
                a.w == 0
147✔
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)
28✔
UNCOV
354
        return
×
355
                a.x == 0 and
7✔
356
                a.y == 0 and
7✔
357
                a.z == 0
7✔
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)
28✔
364
        return a.w == 0
7✔
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)
28✔
UNCOV
371
        return private.is_nan(a.x) or
×
UNCOV
372
                private.is_nan(a.y) or
×
UNCOV
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)
28✔
385
        if a.w > 1 or a.w < -1 then
49✔
386
                a = a:normalize()
14✔
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
49✔
393
                if identityAxis then
14✔
394
                        return 0,identityAxis:unpack()
7✔
395
                else
396
                        return 0,0,0,1
7✔
397
                end
398
        end
399

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

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

414
        return angle, x, y, z
35✔
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)
28✔
423
        local angle, x, y, z = a:to_angle_axis_unpack(identityAxis)
35✔
424
        return angle, vec3(x, y, z)
35✔
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)
28✔
431
        return vec3(a.x, a.y, a.z)
7✔
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)
28✔
438
        return string.format("(%+0.3f,%+0.3f,%+0.3f,%+0.3f)", a.x, a.y, a.z, a.w)
7✔
439
end
440

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

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

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

452
function quat_mt.__eq(a,b)
28✔
453
        if not quat.is_quat(a) or not quat.is_quat(b) then
49✔
UNCOV
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
49✔
457
end
458

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

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

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

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

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

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

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

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

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