GLC_lib  2.5.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
glc_boundingbox.h
Go to the documentation of this file.
1 /****************************************************************************
2 
3  This file is part of the GLC-lib library.
4  Copyright (C) 2005-2008 Laurent Ribon (laumaya@users.sourceforge.net)
5  http://glc-lib.sourceforge.net
6 
7  GLC-lib is free software; you can redistribute it and/or modify
8  it under the terms of the GNU Lesser General Public License as published by
9  the Free Software Foundation; either version 3 of the License, or
10  (at your option) any later version.
11 
12  GLC-lib is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  GNU Lesser General Public License for more details.
16 
17  You should have received a copy of the GNU Lesser General Public License
18  along with GLC-lib; if not, write to the Free Software
19  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 
21 *****************************************************************************/
22 
24 
25 #ifndef GLC_BOUNDINGBOX_
26 #define GLC_BOUNDINGBOX_
27 
28 #include "maths/glc_vector3d.h"
29 #include "maths/glc_utils_maths.h"
30 #include "maths/glc_matrix4x4.h"
31 #include <QtDebug>
32 #include "glc_config.h"
33 
36 
41 
42 
44 {
45  friend GLC_LIB_EXPORT QDataStream &operator<<(QDataStream &, const GLC_BoundingBox &);
46  friend GLC_LIB_EXPORT QDataStream &operator>>(QDataStream &, GLC_BoundingBox &);
47 
49 
51 
52 public:
55 
57  GLC_BoundingBox(const GLC_BoundingBox& boundingBox);
58 
60  GLC_BoundingBox(const GLC_Point3d& lower, const GLC_Point3d& upper);
61 
63 
64 
66 
67 public:
69  static quint32 chunckID();
70 
72  bool isEmpty(void) const
73  {
74  return m_IsEmpty;
75  }
76 
78  bool intersect(const GLC_Point3d& point) const;
79 
81  inline bool intersect(const GLC_BoundingBox& boundingBox) const;
82 
84  bool intersectBoundingSphere(const GLC_Point3d&) const;
85 
87  bool intersectBoundingSphere(const GLC_BoundingBox&) const;
88 
90  inline const GLC_Point3d& lowerCorner() const
91  {return m_Lower;}
92 
94  inline const GLC_Point3d& upperCorner() const
95  {return m_Upper;}
96 
98  inline GLC_Point3d center() const;
99 
101  inline double boundingSphereRadius() const
102  {return GLC_Vector3d(m_Lower - m_Upper).length() / 2.0;}
103 
105  inline bool operator == (const GLC_BoundingBox& boundingBox);
106 
108  inline bool operator != (const GLC_BoundingBox& boundingBox)
109  {return !(*this == boundingBox);}
110 
112  inline double xLength() const
113  {return fabs(m_Upper.x() - m_Lower.x());}
114 
116  inline double yLength() const
117  {return fabs(m_Upper.y() - m_Lower.y());}
118 
120  inline double zLength() const
121  {return fabs(m_Upper.z() - m_Lower.z());}
122 
123 
125 
127 
129 
130 public:
132  GLC_BoundingBox& combine(const GLC_Point3d& point);
133 
135  GLC_BoundingBox& combine(const GLC_Point3df& point);
136 
138  GLC_BoundingBox& combine(const GLC_BoundingBox& box);
139 
141  GLC_BoundingBox& transform(const GLC_Matrix4x4& matrix);
142 
144 
146 // Private members
148 private:
151 
154 
156  bool m_IsEmpty;
157 
159  static quint32 m_ChunkId;
160 };
161 
163 GLC_LIB_EXPORT QDataStream &operator<<(QDataStream &, const GLC_BoundingBox &);
164 GLC_LIB_EXPORT QDataStream &operator>>(QDataStream &, GLC_BoundingBox &);
165 
166 // Return true if the given bounding box intersect this bounding box
167 bool GLC_BoundingBox::intersect(const GLC_BoundingBox& boundingBox) const
168 {
169  // Distance between bounding box center
170  GLC_Vector3d thisCenter= center();
171  GLC_Vector3d otherCenter= boundingBox.center();
172  const double distanceX= fabs(thisCenter.x() - otherCenter.x());
173  const double distanceY= fabs(thisCenter.y() - otherCenter.y());
174  const double distanceZ= fabs(thisCenter.z() - otherCenter.z());
175 
176  bool intersect= distanceX < ((xLength() + boundingBox.xLength()) * 0.5);
177  intersect= intersect && (distanceY < ((yLength() + boundingBox.yLength()) * 0.5));
178  intersect= intersect && (distanceZ < ((zLength() + boundingBox.zLength()) * 0.5));
179  return intersect;
180 }
181 
183 {
184  return (m_Lower == box.m_Lower) && (m_Upper == box.m_Upper);
185 }
186 
188 {
189  GLC_Vector3d vectResult = (m_Lower + m_Upper) * 0.5;
190  return vectResult;
191 }
192 
193 #endif /*GLC_BOUNDINGBOX_*/

©2005-2013 Laurent Ribon