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

excessive / cpml / 13079282556

07 May 2022 10:53PM UTC coverage: 57.717% (+43.7%) from 14.013%
13079282556

push

github

shakesoda
fix typo in mat4.mul

0 of 1 new or added line in 1 file covered. (0.0%)

527 existing lines in 19 files now uncovered.

4581 of 7937 relevant lines covered (57.72%)

52.86 hits per line

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

84.29
/modules/bound2.lua
1
--- A 2 component bounding box.
2
-- @module bound2
3

4
local modules = (...):gsub('%.[^%.]+$', '') .. "."
1✔
5
local vec2    = require(modules .. "vec2")
1✔
6

7
local bound2    = {}
1✔
8
local bound2_mt = {}
1✔
9

10
-- Private constructor.
11
local function new(min, max)
12
        return setmetatable({
76✔
13
                min=min, -- min: vec2, minimum value for each component 
38✔
14
                max=max, -- max: vec2, maximum value for each component 
38✔
15
        }, bound2_mt)
76✔
16
end
17

18
-- Do the check to see if JIT is enabled. If so use the optimized FFI structs.
19
local status, ffi
20
if type(jit) == "table" and jit.status() then
1✔
21
        status, ffi = pcall(require, "ffi")
×
22
        if status then
×
23
                ffi.cdef "typedef struct { cpml_vec2 min, max; } cpml_bound2;"
×
UNCOV
24
                new = ffi.typeof("cpml_bound2")
×
25
        end
26
end
27

28
bound2.zero = new(vec2.zero, vec2.zero)
1✔
29

30
--- The public constructor.
31
-- @param min Can be of two types: </br>
32
-- vec2 min, minimum value for each component
33
-- nil Create bound at single point 0,0
34
-- @tparam vec2 max, maximum value for each component
35
-- @treturn bound2 out
36
function bound2.new(min, max)
1✔
37
        if min and max then
36✔
38
                return new(min:clone(), max:clone())
35✔
39
        elseif min or max then
1✔
UNCOV
40
                error("Unexpected nil argument to bound2.new")
×
41
        else
42
                return new(vec2.zero, vec2.zero)
1✔
43
        end
44
end
45

46
--- Clone a bound.
47
-- @tparam bound2 a bound to be cloned
48
-- @treturn bound2 out
49
function bound2.clone(a)
1✔
50
        return new(a.min, a.max)
1✔
51
end
52

53
--- Construct a bound covering one or two points 
54
-- @tparam vec2 a Any vector
55
-- @tparam vec2 b Any second vector (optional)
56
-- @treturn vec2 Minimum bound containing the given points
57
function bound2.at(a, b) -- "bounded by". b may be nil
1✔
58
        if b then
1✔
59
                return bound2.new(a,b):check()
1✔
60
        else
UNCOV
61
                return bound2.zero:with_center(a)
×
62
        end
63
end
64

65
--- Extend bound to include point
66
-- @tparam bound2 a bound
67
-- @tparam vec2 point to include
68
-- @treturn bound2 Bound covering current min, current max and new point
69
function bound2.extend(a, center)
1✔
70
        return bound2.new(a.min:component_min(center), a.max:component_max(center))
7✔
71
end
72

73
--- Extend bound to entirety of other bound
74
-- @tparam bound2 a bound
75
-- @tparam bound2 bound to cover
76
-- @treturn bound2 Bound covering current min and max of each bound in the pair
77
function bound2.extend_bound(a, b)
1✔
78
        return a:extend(b.min):extend(b.max)
2✔
79
end
80

81
--- Get size of bounding box as a vector 
82
-- @tparam bound2 a bound
83
-- @treturn vec2 Vector spanning min to max points
84
function bound2.size(a)
1✔
85
        return a.max - a.min
2✔
86
end
87

88
--- Resize bounding box from minimum corner
89
-- @tparam bound2 a bound
90
-- @tparam vec2 new size
91
-- @treturn bound2 resized bound
92
function bound2.with_size(a, size)
1✔
93
        return bound2.new(a.min, a.min + size)
1✔
94
end
95

96
--- Get half-size of bounding box as a vector. A more correct term for this is probably "apothem"
97
-- @tparam bound2 a bound
98
-- @treturn vec2 Vector spanning center to max point
99
function bound2.radius(a)
1✔
100
        return a:size()/2
1✔
101
end
102

103
--- Get center of bounding box
104
-- @tparam bound2 a bound
105
-- @treturn bound2 Point in center of bound
106
function bound2.center(a)
1✔
107
        return (a.min + a.max)/2
3✔
108
end
109

110
--- Move bounding box to new center
111
-- @tparam bound2 a bound
112
-- @tparam vec2 new center
113
-- @treturn bound2 Bound with same size as input but different center
114
function bound2.with_center(a, center)
1✔
115
        return bound2.offset(a, center - a:center())
1✔
116
end
117

118
--- Resize bounding box from center
119
-- @tparam bound2 a bound
120
-- @tparam vec2 new size
121
-- @treturn bound2 resized bound
122
function bound2.with_size_centered(a, size)
1✔
123
        local center = a:center()
1✔
124
        local rad = size/2
1✔
125
        return bound2.new(center - rad, center + rad)
1✔
126
end
127

128
--- Convert possibly-invalid bounding box to valid one
129
-- @tparam bound2 a bound
130
-- @treturn bound2 bound with all components corrected for min-max property
131
function bound2.check(a)
1✔
132
        if a.min.x > a.max.x or a.min.y > a.max.y then
2✔
133
                return bound2.new(vec2.component_min(a.min, a.max), vec2.component_max(a.min, a.max))
2✔
134
        end
UNCOV
135
        return a
×
136
end
137

138
--- Shrink bounding box with fixed margin
139
-- @tparam bound2 a bound
140
-- @tparam vec2 a margin
141
-- @treturn bound2 bound with margin subtracted from all edges. May not be valid, consider calling check()
142
function bound2.inset(a, v)
1✔
143
        return bound2.new(a.min + v, a.max - v)
1✔
144
end
145

146
--- Expand bounding box with fixed margin
147
-- @tparam bound2 a bound
148
-- @tparam vec2 a margin
149
-- @treturn bound2 bound with margin added to all edges. May not be valid, consider calling check()
150
function bound2.outset(a, v)
1✔
151
        return bound2.new(a.min - v, a.max + v)
1✔
152
end
153

154
--- Offset bounding box
155
-- @tparam bound2 a bound
156
-- @tparam vec2 offset
157
-- @treturn bound2 bound with same size, but position moved by offset
158
function bound2.offset(a, v)
1✔
159
        return bound2.new(a.min + v, a.max + v)
2✔
160
end
161

162
--- Test if point in bound
163
-- @tparam bound2 a bound
164
-- @tparam vec2 point to test
165
-- @treturn boolean true if point in bounding box
166
function bound2.contains(a, v)
1✔
167
        return a.min.x <= v.x and a.min.y <= v.y
7✔
168
           and a.max.x >= v.x and a.max.y >= v.y
7✔
169
end
170

171
-- Round all components of all vectors to nearest int (or other precision).
172
-- @tparam vec3 a bound to round.
173
-- @tparam precision Digits after the decimal (round number if unspecified)
174
-- @treturn vec3 Rounded bound
175
function bound2.round(a, precision)
1✔
176
        return bound2.new(a.min:round(precision), a.max:round(precision))
1✔
177
end
178

179
--- Return a formatted string.
180
-- @tparam bound2 a bound to be turned into a string
181
-- @treturn string formatted
182
function bound2.to_string(a)
1✔
UNCOV
183
        return string.format("(%s-%s)", a.min, a.max)
×
184
end
185

186
bound2_mt.__index    = bound2
1✔
187
bound2_mt.__tostring = bound2.to_string
1✔
188

189
function bound2_mt.__call(_, a, b)
1✔
190
        return bound2.new(a, b)
16✔
191
end
192

193
if status then
1✔
194
        xpcall(function() -- Allow this to silently fail; assume failure means someone messed with package.loaded
×
195
                ffi.metatype(new, bound2_mt)
×
UNCOV
196
        end, function() end)
×
197
end
198

199
return setmetatable({}, bound2_mt)
1✔
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