00001 /**************************************************************************** 00002 00003 This file is part of the GLC-lib library. 00004 Copyright (C) 2005-2008 Laurent Ribon (laumaya@users.sourceforge.net) 00005 Version 2.0.0 Beta 1, packaged on April 2010. 00006 00007 http://glc-lib.sourceforge.net 00008 00009 GLC-lib is free software; you can redistribute it and/or modify 00010 it under the terms of the GNU General Public License as published by 00011 the Free Software Foundation; either version 2 of the License, or 00012 (at your option) any later version. 00013 00014 GLC-lib is distributed in the hope that it will be useful, 00015 but WITHOUT ANY WARRANTY; without even the implied warranty of 00016 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00017 GNU General Public License for more details. 00018 00019 You should have received a copy of the GNU General Public License 00020 along with GLC-lib; if not, write to the Free Software 00021 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 00022 00023 *****************************************************************************/ 00024 00026 00027 #ifndef GLC_BOUNDINGBOX_ 00028 #define GLC_BOUNDINGBOX_ 00029 00030 #include "maths/glc_vector3d.h" 00031 #include "maths/glc_utils_maths.h" 00032 #include "maths/glc_matrix4x4.h" 00033 #include <QtDebug> 00034 #include "glc_config.h" 00035 00038 00043 00044 00045 class GLC_LIB_EXPORT GLC_BoundingBox 00046 { 00047 friend QDataStream &operator<<(QDataStream &, const GLC_BoundingBox &); 00048 friend QDataStream &operator>>(QDataStream &, GLC_BoundingBox &); 00049 00051 00053 00054 public: 00056 GLC_BoundingBox(); 00057 00059 GLC_BoundingBox(const GLC_BoundingBox& boundingBox); 00060 00062 GLC_BoundingBox(const GLC_Point3d& lower, const GLC_Point3d& upper); 00063 00065 00066 00068 00069 public: 00071 static quint32 chunckID(); 00072 00074 bool isEmpty(void) const 00075 { 00076 return m_IsEmpty; 00077 } 00078 00080 bool intersect(const GLC_Point3d& point) const; 00081 00083 inline bool intersect(const GLC_BoundingBox& boundingBox) const; 00084 00086 bool intersectBoundingSphere(const GLC_Point3d&) const; 00087 00089 bool intersectBoundingSphere(const GLC_BoundingBox&) const; 00090 00092 inline const GLC_Point3d& lowerCorner() const 00093 {return m_Lower;} 00094 00096 inline const GLC_Point3d& upperCorner() const 00097 {return m_Upper;} 00098 00100 inline GLC_Point3d center() const; 00101 00103 inline double boundingSphereRadius() const 00104 {return GLC_Vector3d(m_Lower - m_Upper).length() / 2.0;} 00105 00107 inline bool operator == (const GLC_BoundingBox& boundingBox); 00108 00110 inline bool operator != (const GLC_BoundingBox& boundingBox) 00111 {return !(*this == boundingBox);} 00112 00114 inline double xLength() const 00115 {return fabs(m_Upper.x() - m_Lower.x());} 00116 00118 inline double yLength() const 00119 {return fabs(m_Upper.y() - m_Lower.y());} 00120 00122 inline double zLength() const 00123 {return fabs(m_Upper.z() - m_Lower.z());} 00124 00125 00127 00129 00131 00132 public: 00134 GLC_BoundingBox& combine(const GLC_Point3d& point); 00135 00137 GLC_BoundingBox& combine(const GLC_Point3df& point); 00138 00140 GLC_BoundingBox& combine(const GLC_BoundingBox& box); 00141 00143 GLC_BoundingBox& transform(const GLC_Matrix4x4& matrix); 00144 00146 00148 // Private members 00150 private: 00152 GLC_Point3d m_Lower; 00153 00155 GLC_Point3d m_Upper; 00156 00158 bool m_IsEmpty; 00159 00161 static quint32 m_ChunkId; 00162 }; 00163 00165 QDataStream &operator<<(QDataStream &, const GLC_BoundingBox &); 00166 QDataStream &operator>>(QDataStream &, GLC_BoundingBox &); 00167 00168 // Return true if the given bounding box intersect this bounding box 00169 bool GLC_BoundingBox::intersect(const GLC_BoundingBox& boundingBox) const 00170 { 00171 // Distance between bounding box center 00172 GLC_Vector3d thisCenter= center(); 00173 GLC_Vector3d otherCenter= boundingBox.center(); 00174 const double distanceX= fabs(thisCenter.x() - otherCenter.x()); 00175 const double distanceY= fabs(thisCenter.y() - otherCenter.y()); 00176 const double distanceZ= fabs(thisCenter.z() - otherCenter.z()); 00177 00178 bool intersect= distanceX < ((xLength() + boundingBox.xLength()) * 0.5); 00179 intersect= intersect && (distanceY < ((yLength() + boundingBox.yLength()) * 0.5)); 00180 intersect= intersect && (distanceZ < ((zLength() + boundingBox.zLength()) * 0.5)); 00181 return intersect; 00182 } 00183 00184 bool GLC_BoundingBox::operator == (const GLC_BoundingBox& box) 00185 { 00186 return (m_Lower == box.m_Lower) && (m_Upper == box.m_Upper); 00187 } 00188 00189 GLC_Point3d GLC_BoundingBox::center(void) const 00190 { 00191 GLC_Vector3d vectResult = (m_Lower + m_Upper) * 0.5; 00192 return vectResult; 00193 } 00194 00195 #endif /*GLC_BOUNDINGBOX_*/