GLC_lib  2.5.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
glc_boundingbox.cpp
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 #include "glc_boundingbox.h"
26 #include "maths/glc_matrix4x4.h"
27 
28 quint32 GLC_BoundingBox::m_ChunkId= 0xA707;
29 
31 // Constructor Destructor
33 
35 : m_Lower(0, 0, 0)
36 , m_Upper(0, 0, 0)
37 , m_IsEmpty(true)
38 {
39 
40 }
41 
43 : m_Lower(boundingBox.m_Lower)
44 , m_Upper(boundingBox.m_Upper)
45 , m_IsEmpty(boundingBox.m_IsEmpty)
46 {
47 }
48 
50 : m_Lower(lower)
51 , m_Upper(upper)
52 , m_IsEmpty(false)
53 {
54 
55 }
56 
58 // Get Functions
60 
62 {
63  return m_ChunkId;
64 }
65 
66 bool GLC_BoundingBox::intersect(const GLC_Point3d& point) const
67 {
68  if (!m_IsEmpty)
69  {
70  bool result= (point.x() < m_Upper.x()) && (point.y() < m_Upper.y())
71  && (point.z() < m_Upper.z()) && (point.x() > m_Lower.x())
72  && (point.y() > m_Lower.y()) && (point.z() > m_Lower.z());
73 
74  return result;
75  }
76  else
77  {
78  return false;
79  }
80 }
81 
83 {
84  const double distance= (center() - point).length();
85  return distance < boundingSphereRadius();
86 }
87 
89 {
90  const double distance= (center() - boundingSphere.center()).length();
91  return distance < (boundingSphereRadius() + boundingSphere.boundingSphereRadius());
92 }
93 
95 // Set Functions
97 
99 {
100  if (m_IsEmpty)
101  {
102  m_Lower= point;
103  m_Upper= point;
104  m_IsEmpty= false;
105  }
106  else
107  {
108  double lowerX= qMin(point.x(), m_Lower.x());
109  double lowerY= qMin(point.y(), m_Lower.y());
110  double lowerZ= qMin(point.z(), m_Lower.z());
111  m_Lower.setVect(lowerX, lowerY, lowerZ);
112 
113  double upperX= qMax(point.x(), m_Upper.x());
114  double upperY= qMax(point.y(), m_Upper.y());
115  double upperZ= qMax(point.z(), m_Upper.z());
116  m_Upper.setVect(upperX, upperY, upperZ);
117  }
118  return *this;
119 }
120 
122 {
123  GLC_Point3d point(pointf);
124  if (m_IsEmpty)
125  {
126  m_Lower= point;
127  m_Upper= point;
128  m_IsEmpty= false;
129  }
130  else
131  {
132  double lowerX= qMin(point.x(), m_Lower.x());
133  double lowerY= qMin(point.y(), m_Lower.y());
134  double lowerZ= qMin(point.z(), m_Lower.z());
135  m_Lower.setVect(lowerX, lowerY, lowerZ);
136 
137  double upperX= qMax(point.x(), m_Upper.x());
138  double upperY= qMax(point.y(), m_Upper.y());
139  double upperZ= qMax(point.z(), m_Upper.z());
140  m_Upper.setVect(upperX, upperY, upperZ);
141  }
142  return *this;
143 }
144 
146 {
147  if (m_IsEmpty && !box.m_IsEmpty)
148  {
149  m_Lower= box.m_Lower;
150  m_Upper= box.m_Upper;
151  m_IsEmpty= box.m_IsEmpty;
152  }
153  else if (! box.m_IsEmpty)
154  {
155  double lowerX= qMin(box.m_Lower.x(), m_Lower.x());
156  double lowerY= qMin(box.m_Lower.y(), m_Lower.y());
157  double lowerZ= qMin(box.m_Lower.z(), m_Lower.z());
158  m_Lower.setVect(lowerX, lowerY, lowerZ);
159 
160  double upperX= qMax(box.m_Upper.x(), m_Upper.x());
161  double upperY= qMax(box.m_Upper.y(), m_Upper.y());
162  double upperZ= qMax(box.m_Upper.z(), m_Upper.z());
163  m_Upper.setVect(upperX, upperY, upperZ);
164  }
165 
166  return *this;
167 }
168 
170 {
171  if (!m_IsEmpty)
172  {
173  // Compute Transformed BoundingBox Corner
174  GLC_Point3d corner1(m_Lower);
175  GLC_Point3d corner7(m_Upper);
176  GLC_Point3d corner2(corner7.x(), corner1.y(), corner1.z());
177  GLC_Point3d corner3(corner7.x(), corner7.y(), corner1.z());
178  GLC_Point3d corner4(corner1.x(), corner7.y(), corner1.z());
179  GLC_Point3d corner5(corner1.x(), corner1.y(), corner7.z());
180  GLC_Point3d corner6(corner7.x(), corner1.y(), corner7.z());
181  GLC_Point3d corner8(corner1.x(), corner7.y(), corner7.z());
182 
183  corner1 = (matrix * corner1);
184  corner2 = (matrix * corner2);
185  corner3 = (matrix * corner3);
186  corner4 = (matrix * corner4);
187  corner5 = (matrix * corner5);
188  corner6 = (matrix * corner6);
189  corner7 = (matrix * corner7);
190  corner8 = (matrix * corner8);
191 
192  // Compute the new BoundingBox
193  GLC_BoundingBox boundingBox;
194  boundingBox.combine(corner1);
195  boundingBox.combine(corner2);
196  boundingBox.combine(corner3);
197  boundingBox.combine(corner4);
198  boundingBox.combine(corner5);
199  boundingBox.combine(corner6);
200  boundingBox.combine(corner7);
201  boundingBox.combine(corner8);
202 
203  m_Lower= boundingBox.m_Lower;
204  m_Upper= boundingBox.m_Upper;
205  }
206 
207  return *this;
208 }
209 
210 QDataStream &operator<<(QDataStream &stream, const GLC_BoundingBox &bBox)
211 {
212  quint32 chunckId= GLC_BoundingBox::m_ChunkId;
213  stream << chunckId;
214 
215  stream << bBox.m_IsEmpty;
216  stream << bBox.m_Lower;
217  stream << bBox.m_Upper;
218 
219  return stream;
220 }
221 
222 QDataStream &operator>>(QDataStream &stream, GLC_BoundingBox &bBox)
223 {
224  quint32 chunckId;
225  stream >> chunckId;
226  Q_ASSERT(chunckId == GLC_BoundingBox::m_ChunkId);
227 
228  stream >> bBox.m_IsEmpty;
229  stream >> bBox.m_Lower;
230  stream >> bBox.m_Upper;
231 
232  return stream;
233 }
234 

©2005-2013 Laurent Ribon