diff options
author | Kester Maddock <Christopher.Maddock.1@uni.massey.ac.nz> | 2004-05-16 16:54:44 +0400 |
---|---|---|
committer | Kester Maddock <Christopher.Maddock.1@uni.massey.ac.nz> | 2004-05-16 16:54:44 +0400 |
commit | c50055204df1a73583f646bba688d5f7a76531a8 (patch) | |
tree | 07cc15029e144d08f138e7164e5f6d1edc4651d8 /source/gameengine/SceneGraph/SG_BBox.cpp | |
parent | 3e6b05f60719f3e04e511c9d52a8f27d7ce3a1a4 (diff) |
SceneGraph support for bounding boxs
Diffstat (limited to 'source/gameengine/SceneGraph/SG_BBox.cpp')
-rw-r--r-- | source/gameengine/SceneGraph/SG_BBox.cpp | 179 |
1 files changed, 179 insertions, 0 deletions
diff --git a/source/gameengine/SceneGraph/SG_BBox.cpp b/source/gameengine/SceneGraph/SG_BBox.cpp new file mode 100644 index 00000000000..1430313afaf --- /dev/null +++ b/source/gameengine/SceneGraph/SG_BBox.cpp @@ -0,0 +1,179 @@ +/** + * $Id$ + * + * ***** BEGIN GPL/BL DUAL LICENSE BLOCK ***** + * + * 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. The Blender + * Foundation also sells licenses for use in proprietary software under + * the Blender License. See http://www.blender.org/BL/ for information + * about this. + * + * 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. + * + * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV. + * All rights reserved. + * + * The Original Code is: all of this file. + * + * Contributor(s): none yet. + * + * ***** END GPL/BL DUAL LICENSE BLOCK ***** + * Bounding Box + */ + +#include <math.h> + + #include "SG_BBox.h" + #include "SG_Node.h" + +SG_BBox::SG_BBox() : + m_min(MT_Point3(0., 0., 0.)), + m_max(MT_Point3(0., 0., 0.)) +{ +} + +SG_BBox::SG_BBox(const MT_Point3 &min, const MT_Point3 &max) : + m_min(min), + m_max(max) +{ +} + +SG_BBox::SG_BBox(const SG_BBox &other, const MT_Transform &world) : + m_min(world(other.m_min)), + m_max(world(other.m_max)) +{ +} + +SG_BBox::SG_BBox(const SG_BBox &other) : + m_min(other.m_min), + m_max(other.m_max) +{ +} + +SG_BBox::~ SG_BBox() +{ +} + +SG_BBox& SG_BBox::operator +=(MT_Point3 &point) +{ + if (point[0] < m_min[0]) + m_min[0] = point[0]; + else if (point[0] > m_max[0]) + m_max[0] = point[0]; + + if (point[1] < m_min[1]) + m_min[1] = point[1]; + else if (point[1] > m_max[1]) + m_max[1] = point[1]; + + if (point[2] < m_min[2]) + m_min[2] = point[2]; + else if (point[2] > m_max[2]) + m_max[2] = point[2]; + + return *this; +} + +SG_BBox& SG_BBox::operator += (SG_BBox &bbox) +{ + *this += bbox.m_min; + *this += bbox.m_max; + + return *this; +} + +SG_BBox SG_BBox::operator +(SG_BBox &bbox2) const +{ + SG_BBox ret = *this; + ret += bbox2; + return ret; +} + +MT_Scalar SG_BBox::volume() const +{ + MT_Vector3 size = m_max - m_min; + return size[0]*size[1]*size[2]; +} +#if 0 +void SG_BBox::translate(const MT_Vector3& dx) +{ + m_min += dx; + m_max += dx; +} + +void SG_BBox::scale(const MT_Vector3& size, const MT_Point3& point) +{ + MT_Vector3 centre = (m_max - m_min)/2. + point; + m_max = (m_max - centre)*size; + m_min = (m_min - centre)*size; +} +#endif + +SG_BBox SG_BBox::transform(const MT_Transform &world) const +{ + return SG_BBox(world(m_min), world(m_max)); +} + +bool SG_BBox::inside(const MT_Point3 &point) const +{ + return point[0] >= m_min[0] && point[0] <= m_max[0] && + point[1] >= m_min[1] && point[1] <= m_max[1] && + point[2] >= m_min[2] && point[2] <= m_max[2]; +} + +bool SG_BBox::inside(const SG_BBox& other) const +{ + return inside(other.m_min) && inside(other.m_max); +} + +bool SG_BBox::intersects(const SG_BBox& other) const +{ + return inside(other.m_min) != inside(other.m_max); +} + +bool SG_BBox::outside(const SG_BBox& other) const +{ + return !inside(other.m_min) && !inside(other.m_max); +} + +SG_BBox::intersect SG_BBox::test(const SG_BBox& other) const +{ + bool point1(inside(other.m_min)), point2(inside(other.m_max)); + + return point1?(point2?INSIDE:INTERSECT):(point2?INTERSECT:OUTSIDE); +} + +void SG_BBox::get(MT_Point3 *box, const MT_Transform &world) const +{ + *box++ = world(m_min); + *box++ = world(MT_Point3(m_min[0], m_min[1], m_max[2])); + *box++ = world(MT_Point3(m_min[0], m_max[1], m_min[2])); + *box++ = world(MT_Point3(m_min[0], m_max[1], m_max[2])); + *box++ = world(MT_Point3(m_max[0], m_min[1], m_min[2])); + *box++ = world(MT_Point3(m_max[0], m_min[1], m_max[2])); + *box++ = world(MT_Point3(m_max[0], m_max[1], m_min[2])); + *box++ = world(m_max); +} + +void SG_BBox::getaa(MT_Point3 *box, const MT_Transform &world) const +{ + const MT_Point3 min(world(m_min)), max(world(m_max)); + *box++ = min; + *box++ = MT_Point3(min[0], min[1], max[2]); + *box++ = MT_Point3(min[0], max[1], min[2]); + *box++ = MT_Point3(min[0], max[1], max[2]); + *box++ = MT_Point3(max[0], min[1], min[2]); + *box++ = MT_Point3(max[0], min[1], max[2]); + *box++ = MT_Point3(max[0], max[1], min[2]); + *box++ = max; +} |