glc_boundingbox.cpp

Go to the documentation of this file.
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, packaged on July 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 Lesser General Public License as published by
00011  the Free Software Foundation; either version 3 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 Lesser General Public License for more details.
00018 
00019  You should have received a copy of the GNU Lesser 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 #include "glc_boundingbox.h"
00028 #include "maths/glc_matrix4x4.h"
00029 
00030 quint32 GLC_BoundingBox::m_ChunkId= 0xA707;
00031 
00033 // Constructor Destructor
00035 
00036 GLC_BoundingBox::GLC_BoundingBox()
00037 : m_Lower(0, 0, 0)
00038 , m_Upper(0, 0, 0)
00039 , m_IsEmpty(true)
00040 {
00041 
00042 }
00043 
00044 GLC_BoundingBox::GLC_BoundingBox(const GLC_BoundingBox& boundingBox)
00045 : m_Lower(boundingBox.m_Lower)
00046 , m_Upper(boundingBox.m_Upper)
00047 , m_IsEmpty(boundingBox.m_IsEmpty)
00048 {
00049 }
00050 
00051 GLC_BoundingBox::GLC_BoundingBox(const GLC_Point3d& lower, const GLC_Point3d& upper)
00052 : m_Lower(lower)
00053 , m_Upper(upper)
00054 , m_IsEmpty(false)
00055 {
00056 
00057 }
00058 
00060 // Get Functions
00062 
00063 quint32 GLC_BoundingBox::chunckID()
00064 {
00065         return m_ChunkId;
00066 }
00067 
00068 bool GLC_BoundingBox::intersect(const GLC_Point3d& point) const
00069 {
00070         if (!m_IsEmpty)
00071         {
00072                 bool result= (point.x() < m_Upper.x()) && (point.y() < m_Upper.y())
00073                 && (point.z() < m_Upper.z()) && (point.x() > m_Lower.x())
00074                 && (point.y() > m_Lower.y()) && (point.z() > m_Lower.z());
00075 
00076                 return result;
00077         }
00078         else
00079         {
00080                 return false;
00081         }
00082 }
00083 
00084 bool GLC_BoundingBox::intersectBoundingSphere(const GLC_Point3d& point) const
00085 {
00086         const double distance= (center() - point).length();
00087         return distance < boundingSphereRadius();
00088 }
00089 
00090 bool GLC_BoundingBox::intersectBoundingSphere(const GLC_BoundingBox& boundingSphere) const
00091 {
00092         const double distance= (center() - boundingSphere.center()).length();
00093         return distance < (boundingSphereRadius() + boundingSphere.boundingSphereRadius());
00094 }
00095 
00097 // Set Functions
00099 
00100 GLC_BoundingBox& GLC_BoundingBox::combine(const GLC_Point3d& point)
00101 {
00102         if (m_IsEmpty)
00103         {
00104                 m_Lower= point;
00105                 m_Upper= point;
00106                 m_IsEmpty= false;
00107         }
00108         else
00109         {
00110                 double lowerX= qMin(point.x(), m_Lower.x());
00111                 double lowerY= qMin(point.y(), m_Lower.y());
00112                 double lowerZ= qMin(point.z(), m_Lower.z());
00113                 m_Lower.setVect(lowerX, lowerY, lowerZ);
00114 
00115                 double upperX= qMax(point.x(), m_Upper.x());
00116                 double upperY= qMax(point.y(), m_Upper.y());
00117                 double upperZ= qMax(point.z(), m_Upper.z());
00118                 m_Upper.setVect(upperX, upperY, upperZ);
00119         }
00120         return *this;
00121 }
00122 
00123 GLC_BoundingBox& GLC_BoundingBox::combine(const GLC_Point3df& pointf)
00124 {
00125         GLC_Point3d point(pointf);
00126         if (m_IsEmpty)
00127         {
00128                 m_Lower= point;
00129                 m_Upper= point;
00130                 m_IsEmpty= false;
00131         }
00132         else
00133         {
00134                 double lowerX= qMin(point.x(), m_Lower.x());
00135                 double lowerY= qMin(point.y(), m_Lower.y());
00136                 double lowerZ= qMin(point.z(), m_Lower.z());
00137                 m_Lower.setVect(lowerX, lowerY, lowerZ);
00138 
00139                 double upperX= qMax(point.x(), m_Upper.x());
00140                 double upperY= qMax(point.y(), m_Upper.y());
00141                 double upperZ= qMax(point.z(), m_Upper.z());
00142                 m_Upper.setVect(upperX, upperY, upperZ);
00143         }
00144         return *this;
00145 }
00146 
00147 GLC_BoundingBox& GLC_BoundingBox::combine(const GLC_BoundingBox& box)
00148 {
00149         if (m_IsEmpty && !box.m_IsEmpty)
00150         {
00151                 m_Lower= box.m_Lower;
00152                 m_Upper= box.m_Upper;
00153                 m_IsEmpty= box.m_IsEmpty;
00154         }
00155         else if (! box.m_IsEmpty)
00156         {
00157                 double lowerX= qMin(box.m_Lower.x(), m_Lower.x());
00158                 double lowerY= qMin(box.m_Lower.y(), m_Lower.y());
00159                 double lowerZ= qMin(box.m_Lower.z(), m_Lower.z());
00160                 m_Lower.setVect(lowerX, lowerY, lowerZ);
00161 
00162                 double upperX= qMax(box.m_Upper.x(), m_Upper.x());
00163                 double upperY= qMax(box.m_Upper.y(), m_Upper.y());
00164                 double upperZ= qMax(box.m_Upper.z(), m_Upper.z());
00165                 m_Upper.setVect(upperX, upperY, upperZ);
00166         }
00167 
00168         return *this;
00169 }
00170 
00171 GLC_BoundingBox& GLC_BoundingBox::transform(const GLC_Matrix4x4& matrix)
00172 {
00173         if (!m_IsEmpty)
00174         {
00175                 GLC_Point3d newLower= matrix * m_Lower;
00176                 GLC_Point3d newUpper= matrix * m_Upper;
00177                 GLC_BoundingBox boundingBox;
00178                 boundingBox.combine(newLower);
00179                 boundingBox.combine(newUpper);
00180 
00181                 m_Lower= boundingBox.m_Lower;
00182                 m_Upper= boundingBox.m_Upper;
00183         }
00184 
00185         return *this;
00186 }
00187 
00188 QDataStream &operator<<(QDataStream &stream, const GLC_BoundingBox &bBox)
00189 {
00190         quint32 chunckId= GLC_BoundingBox::m_ChunkId;
00191         stream << chunckId;
00192 
00193         stream << bBox.m_IsEmpty;
00194         stream << bBox.lowerCorner();
00195         stream << bBox.upperCorner();
00196 
00197         return stream;
00198 }
00199 
00200 QDataStream &operator>>(QDataStream &stream, GLC_BoundingBox &bBox)
00201 {
00202         quint32 chunckId;
00203         stream >> chunckId;
00204         Q_ASSERT(chunckId == GLC_BoundingBox::m_ChunkId);
00205 
00206         stream >> bBox.m_IsEmpty;
00207         stream >> bBox.m_Lower;
00208         stream >> bBox.m_Upper;
00209 
00210         return stream;
00211 }
00212 

SourceForge.net Logo

©2005-2010 Laurent Ribon