/* Copyright (C) 2017 Eric Wasylishen This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA See file, 'COPYING', for details. */ #ifndef __COMMON_AABB_HH__ #define __COMMON_AABB_HH__ #include /** * touching a side/edge/corner is considered touching */ template class aabb { public: class intersection_t { public: bool valid; aabb bbox; intersection_t() : valid(false), bbox(V(0), V(0)) {} intersection_t(const aabb &i) : valid(true), bbox(i) {} bool operator==(const intersection_t &other) const { return valid == other.valid && bbox == other.bbox; } }; private: V m_mins, m_maxs; void fix() { for (int i=0; i &other) : m_mins(other.m_mins), m_maxs(other.m_maxs) { fix(); } bool operator==(const aabb &other) const { return m_mins == other.m_mins && m_maxs == other.m_maxs; } const V &mins() const { return m_mins; } const V &maxs() const { return m_maxs; } bool disjoint(const aabb &other) const { for (int i=0; i other.m_maxs[i]) return true; } return false; } bool contains(const aabb &other) const { for (int i=0; i<3; i++) { if (other.m_mins[i] < m_mins[i]) return false; if (other.m_maxs[i] > m_maxs[i]) return false; } return true; } bool containsPoint(const V &p) const { for (int i=0; i= m_mins[i] && p[i] <= m_maxs[i])) return false; } return true; } aabb expand(const V &pt) const { V mins, maxs; for (int i=0; i(mins, maxs); } aabb unionWith(const aabb &other) const { return expand(other.m_mins).expand(other.m_maxs); } intersection_t intersectWith(const aabb &other) const { V mins, maxs; for (int i=0; i maxs[i]) { // empty intersection return intersection_t(); } } return intersection_t(aabb(mins, maxs)); } V size() const { V result = m_maxs - m_mins; return result; } aabb grow(const V &size) const { return aabb(m_mins - size, m_maxs + size); } }; using aabb3d = aabb<3, qvec3d>; using aabb2d = aabb<2, qvec2d>; using aabb3f = aabb<3, qvec3f>; using aabb2f = aabb<2, qvec2f>; #endif /* __COMMON_AABB_HH__ */