aabb: add expand call

This commit is contained in:
Danny Robson 2015-04-09 14:05:01 +10:00
parent c94535dfae
commit afad51db9d
4 changed files with 104 additions and 2 deletions

View File

@ -244,6 +244,7 @@ AM_LDFLAGS += $(BOOST_LDFLAGS) $(BOOST_FILESYSTEM_LIB) $(BOOST_SYSTEM_LIB)
AM_CXXFLAGS += -I$(top_srcdir)
TEST_BIN = \
test/aabb \
test/backtrace \
test/bezier \
test/bitwise \

View File

@ -74,13 +74,58 @@ AABB<S,T>::closest (point<S,T> q) const
}
///////////////////////////////////////////////////////////////////////////////
template <size_t S, typename T>
AABB<S,T>&
AABB<S,T>::expand (util::vector<S,T> mag)
{
p0 -= mag / T{2};
p1 += mag / T{2};
return *this;
}
//-----------------------------------------------------------------------------
template <size_t S, typename T>
AABB<S,T>&
AABB<S,T>::expand (T t)
{
return expand (vector<S,T> {t});
}
//-----------------------------------------------------------------------------
template <size_t S, typename T>
AABB<S,T>
AABB<S,T>::expanded (vector<S,T> mag)
{
auto ret = *this;
ret.expand (mag);
return ret;
}
//-----------------------------------------------------------------------------
template <size_t S, typename T>
AABB<S,T>
AABB<S,T>::expanded (T t)
{
return expanded (vector<S,T> {t});
}
///////////////////////////////////////////////////////////////////////////////
template <size_t S, typename T>
AABB<S,T>&
AABB<S,T>::contract (util::vector<S,T> mag)
{
p0 -= mag / T{2};
p1 += mag / T{2};
// Avoid contracting magnitudes larger than our extent
auto diff = p1 - p0;
auto delta = min (diff, mag);
p0 += delta / T{2};
p1 -= delta / T{2};
return *this;
}

View File

@ -39,6 +39,11 @@ namespace util {
point<S,T> closest (point<S,T>) const;
AABB<S,T>& expand (util::vector<S,T>);
AABB<S,T>& expand (T);
AABB<S,T> expanded (util::vector<S,T>);
AABB<S,T> expanded (T);
AABB<S,T>& contract (util::vector<S,T>);
AABB<S,T>& contract (T);
AABB<S,T> contracted (util::vector<S,T>) const;

51
test/aabb.cpp Normal file
View File

@ -0,0 +1,51 @@
#include "aabb.hpp"
int
main (int, char**)
{
{
// Test contraction
util::AABB2f box {
{ 2, 2 },
{ 8, 8 }
};
box.contract (2.f);
CHECK_EQ (box.p0.x, 3);
CHECK_EQ (box.p0.y, 3);
CHECK_EQ (box.p1.x, 7);
CHECK_EQ (box.p1.y, 7);
}
{
// Test expansion
util::AABB2f box {
{ 2, 2 },
{ 8, 8 }
};
box.expand (2.f);
CHECK_EQ (box.p0.x, 1);
CHECK_EQ (box.p0.y, 1);
CHECK_EQ (box.p1.x, 9);
CHECK_EQ (box.p1.y, 9);
}
{
// Ensure we don't wrap-around on unsigned position types when contracting
util::AABB2f small {
{ 0, 0 },
{ 1, 1 }
};
small.contract (10);
CHECK_EQ (small.p0.x, 0.5f);
CHECK_EQ (small.p0.y, 0.5f);
CHECK_EQ (small.p1.x, 0.5f);
CHECK_EQ (small.p1.y, 0.5f);
}
}