From e3f817bf7e198adcbcc791e9b42351a2d67155f1 Mon Sep 17 00:00:00 2001 From: mcc Date: Sat, 30 Nov 2019 01:24:48 -0500 Subject: [PATCH] extend(vec) and extend_bound(bound) for bounds expands bounds to cover new points --- modules/bound2.lua | 16 ++++++++++++++++ modules/bound3.lua | 16 ++++++++++++++++ spec/bound2_spec.lua | 36 ++++++++++++++++++++++++++++++++++++ spec/bound3_spec.lua | 36 ++++++++++++++++++++++++++++++++++++ 4 files changed, 104 insertions(+) diff --git a/modules/bound2.lua b/modules/bound2.lua index 8c049a4..7a191bc 100644 --- a/modules/bound2.lua +++ b/modules/bound2.lua @@ -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 diff --git a/modules/bound3.lua b/modules/bound3.lua index 397640c..e6f450a 100644 --- a/modules/bound3.lua +++ b/modules/bound3.lua @@ -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 diff --git a/spec/bound2_spec.lua b/spec/bound2_spec.lua index a23322e..5d2987e 100644 --- a/spec/bound2_spec.lua +++ b/spec/bound2_spec.lua @@ -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) diff --git a/spec/bound3_spec.lua b/spec/bound3_spec.lua index e3ae232..5e54d01 100644 --- a/spec/bound3_spec.lua +++ b/spec/bound3_spec.lua @@ -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)