00001 /**************************************************************************** 00002 00003 This file is part of the GLC-lib library. 00004 Copyright (C) 2005-2008 Laurent Ribon (laumaya@users.sourceforge.net) 00005 http://glc-lib.sourceforge.net 00006 00007 GLC-lib is free software; you can redistribute it and/or modify 00008 it under the terms of the GNU Lesser General Public License as published by 00009 the Free Software Foundation; either version 3 of the License, or 00010 (at your option) any later version. 00011 00012 GLC-lib is distributed in the hope that it will be useful, 00013 but WITHOUT ANY WARRANTY; without even the implied warranty of 00014 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00015 GNU Lesser General Public License for more details. 00016 00017 You should have received a copy of the GNU Lesser General Public License 00018 along with GLC-lib; if not, write to the Free Software 00019 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 00020 00021 *****************************************************************************/ 00022 00024 00025 #ifndef GLC_BOUNDINGBOX_ 00026 #define GLC_BOUNDINGBOX_ 00027 00028 #include "maths/glc_vector3d.h" 00029 #include "maths/glc_utils_maths.h" 00030 #include "maths/glc_matrix4x4.h" 00031 #include <QtDebug> 00032 #include "glc_config.h" 00033 00036 00041 00042 00043 class GLC_LIB_EXPORT GLC_BoundingBox 00044 { 00045 friend GLC_LIB_EXPORT QDataStream &operator<<(QDataStream &, const GLC_BoundingBox &); 00046 friend GLC_LIB_EXPORT QDataStream &operator>>(QDataStream &, GLC_BoundingBox &); 00047 00049 00051 00052 public: 00054 GLC_BoundingBox(); 00055 00057 GLC_BoundingBox(const GLC_BoundingBox& boundingBox); 00058 00060 GLC_BoundingBox(const GLC_Point3d& lower, const GLC_Point3d& upper); 00061 00063 00064 00066 00067 public: 00069 static quint32 chunckID(); 00070 00072 bool isEmpty(void) const 00073 { 00074 return m_IsEmpty; 00075 } 00076 00078 bool intersect(const GLC_Point3d& point) const; 00079 00081 inline bool intersect(const GLC_BoundingBox& boundingBox) const; 00082 00084 bool intersectBoundingSphere(const GLC_Point3d&) const; 00085 00087 bool intersectBoundingSphere(const GLC_BoundingBox&) const; 00088 00090 inline const GLC_Point3d& lowerCorner() const 00091 {return m_Lower;} 00092 00094 inline const GLC_Point3d& upperCorner() const 00095 {return m_Upper;} 00096 00098 inline GLC_Point3d center() const; 00099 00101 inline double boundingSphereRadius() const 00102 {return GLC_Vector3d(m_Lower - m_Upper).length() / 2.0;} 00103 00105 inline bool operator == (const GLC_BoundingBox& boundingBox); 00106 00108 inline bool operator != (const GLC_BoundingBox& boundingBox) 00109 {return !(*this == boundingBox);} 00110 00112 inline double xLength() const 00113 {return fabs(m_Upper.x() - m_Lower.x());} 00114 00116 inline double yLength() const 00117 {return fabs(m_Upper.y() - m_Lower.y());} 00118 00120 inline double zLength() const 00121 {return fabs(m_Upper.z() - m_Lower.z());} 00122 00123 00125 00127 00129 00130 public: 00132 GLC_BoundingBox& combine(const GLC_Point3d& point); 00133 00135 GLC_BoundingBox& combine(const GLC_Point3df& point); 00136 00138 GLC_BoundingBox& combine(const GLC_BoundingBox& box); 00139 00141 GLC_BoundingBox& transform(const GLC_Matrix4x4& matrix); 00142 00144 00146 // Private members 00148 private: 00150 GLC_Point3d m_Lower; 00151 00153 GLC_Point3d m_Upper; 00154 00156 bool m_IsEmpty; 00157 00159 static quint32 m_ChunkId; 00160 }; 00161 00163 GLC_LIB_EXPORT QDataStream &operator<<(QDataStream &, const GLC_BoundingBox &); 00164 GLC_LIB_EXPORT QDataStream &operator>>(QDataStream &, GLC_BoundingBox &); 00165 00166 // Return true if the given bounding box intersect this bounding box 00167 bool GLC_BoundingBox::intersect(const GLC_BoundingBox& boundingBox) const 00168 { 00169 // Distance between bounding box center 00170 GLC_Vector3d thisCenter= center(); 00171 GLC_Vector3d otherCenter= boundingBox.center(); 00172 const double distanceX= fabs(thisCenter.x() - otherCenter.x()); 00173 const double distanceY= fabs(thisCenter.y() - otherCenter.y()); 00174 const double distanceZ= fabs(thisCenter.z() - otherCenter.z()); 00175 00176 bool intersect= distanceX < ((xLength() + boundingBox.xLength()) * 0.5); 00177 intersect= intersect && (distanceY < ((yLength() + boundingBox.yLength()) * 0.5)); 00178 intersect= intersect && (distanceZ < ((zLength() + boundingBox.zLength()) * 0.5)); 00179 return intersect; 00180 } 00181 00182 bool GLC_BoundingBox::operator == (const GLC_BoundingBox& box) 00183 { 00184 return (m_Lower == box.m_Lower) && (m_Upper == box.m_Upper); 00185 } 00186 00187 GLC_Point3d GLC_BoundingBox::center(void) const 00188 { 00189 GLC_Vector3d vectResult = (m_Lower + m_Upper) * 0.5; 00190 return vectResult; 00191 } 00192 00193 #endif /*GLC_BOUNDINGBOX_*/