From 816860d38ea06f4f0c183bc9edac6297cc3593ec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Branimir=20Karad=C5=BEi=C4=87?= Date: Mon, 26 May 2014 16:55:46 -0700 Subject: [PATCH] Added AABB overlap test. --- examples/common/bounds.cpp | 29 +++++++++++++++++++++++++++++ examples/common/bounds.h | 7 +++++++ 2 files changed, 36 insertions(+) diff --git a/examples/common/bounds.cpp b/examples/common/bounds.cpp index dce2c1a42..947320f14 100644 --- a/examples/common/bounds.cpp +++ b/examples/common/bounds.cpp @@ -119,6 +119,35 @@ void calcAabb(Aabb& _aabb, const float* _mtx, const void* _vertices, uint32_t _n _aabb.m_max[2] = max[2]; } +void aabbExpand(Aabb& _aabb, float _factor) +{ + _aabb.m_min[0] -= _factor; + _aabb.m_min[1] -= _factor; + _aabb.m_min[2] -= _factor; + _aabb.m_max[0] += _factor; + _aabb.m_max[1] += _factor; + _aabb.m_max[2] += _factor; +} + +uint32_t aabbOverlapTest(Aabb& _aabb0, Aabb& _aabb1) +{ + const uint32_t ltMinX = _aabb0.m_max[0] < _aabb1.m_min[0]; + const uint32_t gtMaxX = _aabb0.m_min[0] > _aabb1.m_max[0]; + const uint32_t ltMinY = _aabb0.m_max[1] < _aabb1.m_min[1]; + const uint32_t gtMaxY = _aabb0.m_min[1] > _aabb1.m_max[1]; + const uint32_t ltMinZ = _aabb0.m_max[2] < _aabb1.m_min[2]; + const uint32_t gtMaxZ = _aabb0.m_min[2] > _aabb1.m_max[2]; + + return 0 + | (ltMinX<<0) + | (gtMaxX<<1) + | (ltMinY<<2) + | (gtMaxY<<3) + | (ltMinZ<<4) + | (gtMaxZ<<5) + ; +} + void calcObb(Obb& _obb, const void* _vertices, uint32_t _numVertices, uint32_t _stride, uint32_t _steps) { Aabb aabb; diff --git a/examples/common/bounds.h b/examples/common/bounds.h index fc12a6900..4d9d75e9a 100644 --- a/examples/common/bounds.h +++ b/examples/common/bounds.h @@ -38,6 +38,13 @@ void calcAabb(Aabb& _aabb, const void* _vertices, uint32_t _numVertices, uint32_ /// Transform vertices and calculate axis aligned bounding box. void calcAabb(Aabb& _aabb, const float* _mtx, const void* _vertices, uint32_t _numVertices, uint32_t _stride); +/// Expand AABB. +void aabbExpand(Aabb& _aabb, float _factor); + +/// Returns 0 is two AABB don't overlap, otherwise returns flags of overlap +/// test. +uint32_t aabbOverlapTest(Aabb& _aabb0, Aabb& _aabb1); + /// Calculate oriented bounding box. void calcObb(Obb& _obb, const void* _vertices, uint32_t _numVertices, uint32_t _stride, uint32_t _steps = 17);