• 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

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

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

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

18
local frexp = math.frexp or function(x)
12✔
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)
12✔
30
        return math.max(math.min(value, max), min)
×
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)
12✔
38
        return abs(value) >= size and value or 0
×
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)
12✔
46
        -- I know, it barely saves any typing at all.
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)
12✔
55
        -- I know, it barely saves any typing at all.
56
        return abs(value) <= threshold
256✔
57
end
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)
12✔
67
        return ((value) - (min_in)) * ((max_out) - (min_out)) / ((max_in) - (min_in)) + (min_out)
×
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)
12✔
77
        return low * (1 - progress) + high * progress
8✔
78
end
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)
12✔
87
        return utils.lerp(low, high, 1.0 - math.exp(-rate * dt))
2✔
88
end
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
96
function utils.smoothstep(progress, low, high)
12✔
97
        local t = utils.clamp((progress - low) / (high - low), 0.0, 1.0)
×
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
107
utils.round = private.round
12✔
108

109
--- Wrap `value` around if it exceeds `limit`.
110
-- @param value
111
-- @param limit
112
-- @return number
113
function utils.wrap(value, limit)
12✔
114
        if value < 0 then
×
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
125
function utils.is_pot(value)
12✔
126
        -- found here: https://love2d.org/forums/viewtopic.php?p=182219#p182219
127
        -- check if a number is a power-of-two
128
        return (frexp(value)) == 0.5
×
129
end
130

131
-- Originally from vec3
132
function utils.project_on(a, b)
12✔
133
        local s =
134
                (a.x * b.x + a.y * b.y + a.z or 0 * b.z or 0) /
×
135
                (b.x * b.x + b.y * b.y + b.z or 0 * b.z or 0)
×
136

137
        if a.z and b.z then
×
138
                return vec3(
×
139
                        b.x * s,
×
140
                        b.y * s,
×
141
                        b.z * s
×
142
                )
143
        end
144

145
        return vec2(
×
146
                b.x * s,
×
147
                b.y * s
×
148
        )
149
end
150

151
-- Originally from vec3
152
function utils.project_from(a, b)
12✔
153
        local s =
154
                (b.x * b.x + b.y * b.y + b.z or 0 * b.z or 0) /
×
155
                (a.x * b.x + a.y * b.y + a.z or 0 * b.z or 0)
×
156

157
        if a.z and b.z then
×
158
                return vec3(
×
159
                        b.x * s,
×
160
                        b.y * s,
×
161
                        b.z * s
×
162
                )
163
        end
164

165
        return vec2(
×
166
                b.x * s,
×
167
                b.y * s
×
168
        )
169
end
170

171
-- Originally from vec3
172
function utils.mirror_on(a, b)
12✔
173
        local s =
174
                (a.x * b.x + a.y * b.y + a.z or 0 * b.z or 0) /
×
175
                (b.x * b.x + b.y * b.y + b.z or 0 * b.z or 0) * 2
×
176

177
        if a.z and b.z then
×
178
                return vec3(
×
179
                        b.x * s - a.x,
×
180
                        b.y * s - a.y,
×
181
                        b.z * s - a.z
×
182
                )
183
        end
184

185
        return vec2(
×
186
                b.x * s - a.x,
×
187
                b.y * s - a.y
×
188
        )
189
end
190

191
-- Originally from vec3
192
function utils.reflect(i, n)
12✔
193
        return i - (n * (2 * n:dot(i)))
×
194
end
195

196
-- Originally from vec3
197
function utils.refract(i, n, ior)
12✔
198
        local d = n:dot(i)
×
199
        local k = 1 - ior * ior * (1 - d * d)
×
200

201
        if k >= 0 then
×
202
                return (i * ior) - (n * (ior * d + k ^ 0.5))
×
203
        end
204

205
        return vec3()
×
206
end
207

208
return utils
12✔
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