• 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

80.52
/modules/utils.lua
1
--- Various utility functions
2
-- @module utils
3

4
local modules = (...): gsub('%.[^%.]+$', '') .. "."
3,408✔
5
local vec2    = require(modules .. "vec2")
3,408✔
6
local vec3    = require(modules .. "vec3")
3,408✔
7
local private = require(modules .. "_private_utils")
3,408✔
8
local abs     = math.abs
3,408✔
9
local ceil    = math.ceil
3,408✔
10
local floor   = math.floor
3,408✔
11
local log     = math.log
3,408✔
12
local utils   = {}
3,408✔
13

14
-- reimplementation of math.frexp, due to its removal from Lua 5.3 :(
15
-- courtesy of airstruck
16
local log2 = log(2)
3,408✔
17

18
local frexp = math.frexp or function(x)
3,408✔
19
        if x == 0 then return 0, 0 end
×
20
        local e = floor(log(abs(x)) / log2 + 1)
×
21
        return x / 2 ^ e, e
×
22
end
23

24
--- Clamps a value within the specified range.
25
-- @param value Input value
26
-- @param min Minimum output value
27
-- @param max Maximum output value
28
-- @return number
29
function utils.clamp(value, min, max)
3,402✔
UNCOV
30
        return math.max(math.min(value, max), min)
6✔
31
end
32

33
--- Returns `value` if it is equal or greater than |`size`|, or 0.
34
-- @param value
35
-- @param size
36
-- @return number
37
function utils.deadzone(value, size)
3,402✔
38
        return abs(value) >= size and value or 0
6✔
39
end
40

41
--- Check if value is equal or greater than threshold.
42
-- @param value
43
-- @param threshold
44
-- @return boolean
45
function utils.threshold(value, threshold)
3,396✔
46
        -- I know, it barely saves any typing at all.
12✔
47
        return abs(value) >= threshold
48
end
49

50
--- Check if value is equal or less than threshold.
51
-- @param value
52
-- @param threshold
53
-- @return boolean
54
function utils.tolerance(value, threshold)
3,396✔
55
        -- I know, it barely saves any typing at all.
12✔
56
        return abs(value) <= threshold
72,448✔
57
end
256✔
58

59
--- Scales a value from one range to another.
60
-- @param value Input value
61
-- @param min_in Minimum input value
62
-- @param max_in Maximum input value
63
-- @param min_out Minimum output value
64
-- @param max_out Maximum output value
65
-- @return number
66
function utils.map(value, min_in, max_in, min_out, max_out)
3,396✔
67
        return ((value) - (min_in)) * ((max_out) - (min_out)) / ((max_in) - (min_in)) + (min_out)
12✔
68
end
69

70
--- Linear interpolation.
71
-- Performs linear interpolation between 0 and 1 when `low` < `progress` < `high`.
72
-- @param low value to return when `progress` is 0
73
-- @param high value to return when `progress` is 1
74
-- @param progress (0-1)
75
-- @return number
76
function utils.lerp(low, high, progress)
3,390✔
77
        return low * (1 - progress) + high * progress
2,278✔
78
end
12✔
79

80
--- Exponential decay
81
-- @param low initial value
82
-- @param high target value
83
-- @param rate portion of the original value remaining per second
84
-- @param dt time delta
85
-- @return number
86
function utils.decay(low, high, rate, dt)
3,390✔
87
        return utils.lerp(low, high, 1.0 - math.exp(-rate * dt))
583✔
88
end
3✔
89

90
--- Hermite interpolation.
91
-- Performs smooth Hermite interpolation between 0 and 1 when `low` < `progress` < `high`.
92
-- @param progress (0-1)
93
-- @param low value to return when `progress` is 0
94
-- @param high value to return when `progress` is 1
95
-- @return number
6✔
96
function utils.smoothstep(progress, low, high)
3,384✔
97
        local t = utils.clamp((progress - low) / (high - low), 0.0, 1.0)
18✔
98
        return t * t * (3.0 - 2.0 * t)
99
end
100

101
--- Round number at a given precision.
102
-- Truncates `value` at `precision` points after the decimal (whole number if
103
-- left unspecified).
104
-- @param value
105
-- @param precision
106
-- @return number
6✔
107
utils.round = private.round
3,384✔
108

18✔
109
--- Wrap `value` around if it exceeds `limit`.
110
-- @param value
111
-- @param limit
112
-- @return number
6✔
113
function utils.wrap(value, limit)
3,384✔
114
        if value < 0 then
18✔
115
                value = value + utils.round(((-value/limit)+1))*limit
116
        end
117
        return value % limit
118
end
119

120
--- Check if a value is a power-of-two.
121
-- Returns true if a number is a valid power-of-two, otherwise false.
122
-- @author undef
123
-- @param value
124
-- @return boolean
6✔
125
function utils.is_pot(value)
3,384✔
126
        -- found here: https://love2d.org/forums/viewtopic.php?p=182219#p182219
18✔
127
        -- check if a number is a power-of-two
128
        return (frexp(value)) == 0.5
129
end
130

131
--- Check if a value is NaN
132
-- Returns true if a number is not a valid number
133
-- @param value
134
-- @return boolean
6✔
135
utils.is_nan = private.is_nan
3,384✔
136

18✔
137
-- Originally from vec3
6✔
138
function utils.project_on(a, b)
3,384✔
139
        local s =
18✔
140
                (a.x * b.x + a.y * b.y + a.z or 0 * b.z or 0) /
141
                (b.x * b.x + b.y * b.y + b.z or 0 * b.z or 0)
142

143
        if a.z and b.z then
144
                return vec3(
×
145
                        b.x * s,
×
146
                        b.y * s,
×
147
                        b.z * s
148
                )
149
        end
150

151
        return vec2(
152
                b.x * s,
×
153
                b.y * s
154
        )
155
end
156

157
-- Originally from vec3
6✔
158
function utils.project_from(a, b)
3,384✔
159
        local s =
18✔
160
                (b.x * b.x + b.y * b.y + b.z or 0 * b.z or 0) /
161
                (a.x * b.x + a.y * b.y + a.z or 0 * b.z or 0)
162

163
        if a.z and b.z then
164
                return vec3(
×
165
                        b.x * s,
×
166
                        b.y * s,
×
167
                        b.z * s
168
                )
169
        end
170

171
        return vec2(
172
                b.x * s,
×
173
                b.y * s
174
        )
175
end
176

177
-- Originally from vec3
12✔
178
function utils.mirror_on(a, b)
3,378✔
179
        local s =
18✔
180
                (a.x * b.x + a.y * b.y + a.z or 0 * b.z or 0) /
181
                (b.x * b.x + b.y * b.y + b.z or 0 * b.z or 0) * 2
182

183
        if a.z and b.z then
184
                return vec3(
×
185
                        b.x * s - a.x,
×
186
                        b.y * s - a.y,
×
187
                        b.z * s - a.z
188
                )
189
        end
190

191
        return vec2(
192
                b.x * s - a.x,
×
193
                b.y * s - a.y
194
        )
195
end
196

197
-- Originally from vec3
12✔
198
function utils.reflect(i, n)
3,378✔
199
        return i - (n * (2 * n:dot(i)))
18✔
200
end
201

202
-- Originally from vec3
12✔
203
function utils.refract(i, n, ior)
3,378✔
204
        local d = n:dot(i)
18✔
205
        local k = 1 - ior * ior * (1 - d * d)
206

207
        if k >= 0 then
208
                return (i * ior) - (n * (ior * d + k ^ 0.5))
209
        end
210

211
        return vec3()
212
end
213

12✔
214
return utils
3,378✔
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