extend(vec) and extend_bound(bound) for bounds

expands bounds to cover new points
This commit is contained in:
mcc 2019-11-30 01:24:48 -05:00
parent 00126f19c9
commit e3f817bf7e
4 changed files with 104 additions and 0 deletions

View File

@ -62,6 +62,22 @@ function bound2.at(a, b) -- "bounded by". b may be nil
end
end
--- Extend bound to include point
-- @tparam bound2 a bound
-- @tparam vec2 point to include
-- @treturn bound2 Bound covering current min, current max and new point
function bound2.extend(a, center)
return bound2.new(a.min:component_min(center), a.max:component_max(center))
end
--- Extend bound to entirety of other bound
-- @tparam bound2 a bound
-- @tparam bound2 bound to cover
-- @treturn bound2 Bound covering current min and max of each bound in the pair
function bound2.extend_bound(a, b)
return a:extend(b.min):extend(b.max)
end
--- Get size of bounding box as a vector
-- @tparam bound2 a bound
-- @treturn vec2 Vector spanning min to max points

View File

@ -62,6 +62,22 @@ function bound3.at(a, b) -- "bounded by". b may be nil
end
end
--- Extend bound to include point
-- @tparam bound3 a bound
-- @tparam vec3 point to include
-- @treturn bound3 Bound covering current min, current max and new point
function bound3.extend(a, center)
return bound3.new(a.min:component_min(center), a.max:component_max(center))
end
--- Extend bound to entirety of other bound
-- @tparam bound3 a bound
-- @tparam bound3 bound to cover
-- @treturn bound3 Bound covering current min and max of each bound in the pair
function bound3.extend_bound(a, b)
return a:extend(b.min):extend(b.max)
end
--- Get size of bounding box as a vector
-- @tparam bound3 a bound
-- @treturn vec3 Vector spanning min to max points

View File

@ -171,6 +171,42 @@ describe("bound2:", function()
assert.is_not_true(a:contains(vec2(2,3)))
end)
it("extends a bound2 with a point", function()
local min = vec2(1,2)
local max = vec2(4,5)
local downright = vec2(8,8)
local downleft = vec2(-4,8)
local top = vec2(2, 0)
local a = bound2(min, max)
local temp
temp = a:extend(downright)
assert.is_true(a.min == min and a.max == max)
assert.is_true(temp.min == min and temp.max == downright)
temp = a:extend(downleft)
assert.is_true(temp.min == vec2(-4,2) and temp.max == vec2(4,8))
temp = a:extend(top)
assert.is_true(temp.min == vec2(1,0) and temp.max == max)
end)
it("extends a bound with another bound", function()
local min = vec2(1,2)
local max = vec2(4,5)
local leftexpand = bound2.new(vec2(0,0), vec2(1.5, 6))
local rightexpand = bound2.new(vec2(1.5,0), vec2(5, 6))
local a = bound2(min, max)
local temp
temp = a:extend_bound(leftexpand)
assert.is_equal(temp.min, vec2(0,0))
assert.is_equal(temp.max, vec2(4,6))
temp = temp:extend_bound(rightexpand)
assert.is_equal(temp.min, vec2(0,0))
assert.is_equal(temp.max, vec2(5,6))
end)
it("checks for bound2.zero", function()
assert.is.equal(0, bound2.zero.min.x)
assert.is.equal(0, bound2.zero.min.y)

View File

@ -208,6 +208,42 @@ describe("bound3:", function()
assert.is_not_true(a:contains(vec3(2,3,7)))
end)
it("extends a bound3 with a point", function()
local min = vec3(1,2,6)
local max = vec3(4,5,9)
local downright = vec3(8,8,10)
local downleft = vec3(-4,8,10)
local top = vec3(2, 0, 7)
local a = bound3(min, max)
local temp
temp = a:extend(downright)
assert.is_true(a.min == min and a.max == max)
assert.is_true(temp.min == min and temp.max == downright)
temp = a:extend(downleft)
assert.is_true(temp.min == vec3(-4,2,6) and temp.max == vec3(4,8,10))
temp = a:extend(top)
assert.is_true(temp.min == vec3(1,0,6) and temp.max == max)
end)
it("extends a bound with another bound", function()
local min = vec3(1,2,3)
local max = vec3(4,5,6)
local leftexpand = bound3.new(vec3(0,0,4), vec3(1.5,6,5))
local rightexpand = bound3.new(vec3(1.5,0,1), vec3(5,6,7))
local a = bound3(min, max)
local temp
temp = a:extend_bound(leftexpand)
assert.is_equal(temp.min, vec3(0,0,3))
assert.is_equal(temp.max, vec3(4,6,6))
temp = temp:extend_bound(rightexpand)
assert.is_equal(temp.min, vec3(0,0,1))
assert.is_equal(temp.max, vec3(5,6,7))
end)
it("checks for bound3.zero", function()
assert.is.equal(0, bound3.zero.min.x)
assert.is.equal(0, bound3.zero.min.y)