Common/Geom/BoundingBox.h

Go to the documentation of this file.
00001 //-------------------------------------------------------------------
00004 // Copyright (c) 2007 Celeritive Technologies, Inc.
00005 //
00006 // This library is free software; you can redistribute it and/or
00007 // modify it under the terms of the GNU Lesser General Public
00008 // License as published by the Free Software Foundation; either
00009 // version 2.1 of the License, or (at your option) any later version.
00010 //
00011 // This library is distributed in the hope that it will be useful,
00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
00014 // Lesser General Public License for more details.
00015 //
00016 // You should have received a copy of the GNU Lesser General Public
00017 // License along with this library; if not, write to the Free Software
00018 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
00019 //
00020 //-------------------------------------------------------------------
00021 #pragma once
00022 #ifndef VOLUMILL_BOUNDINGBOX_H
00023 #define VOLUMILL_BOUNDINGBOX_H
00024 
00025 #include <Geom/Points.h>
00026 
00027 namespace geom
00028 {
00029 
00030 inline int maxBoxCoord (int) { return 0x7fffffff; }
00031 inline double maxBoxCoord (double) { return 1e30; }
00032 
00033 template<class P>
00034 class BoundingBox
00035 {
00036 public:
00037    typedef P Point;
00038    BoundingBox ()
00039    {
00040       for (int ii=0; ii < Point::Dim; ++ii)
00041       {
00042          m_min[ii] = maxBoxCoord (typename Point::Coord());
00043          m_max[ii] = -maxBoxCoord (typename Point::Coord());
00044       }
00045    }
00046    bool isValid () const { return m_min[0] <= m_max[0]; }
00047    void addPoint (const Point& pt)
00048    {
00049       for (int ii=0; ii < Point::Dim; ++ii)
00050       {
00051          if (m_min[ii] > pt[ii])
00052             m_min[ii] = pt[ii];
00053          if (m_max[ii] < pt[ii])
00054             m_max[ii] = pt[ii];
00055       }
00056    }
00057    const Point& getMin () const { return m_min; }
00058    const Point& getMax () const { return m_max; }
00059 private:
00060    Point m_min, m_max;
00061 };
00062 
00063 template<class B, class P>
00064 inline bool isPointInBox (const B& box, const P& pt)
00065 {
00066    bool inBox = true;
00067    for (int ii=0; ii < P::Dim; ++ii)
00068    {
00069       inBox &= (box.getMin()[ii] < pt[ii]+util::doubleEps && box.getMax()[ii] > pt[ii]-util::doubleEps);
00070    }
00071    return inBox;
00072 }
00073 
00074 template<class B>
00075 inline bool boxesIntersect (const B& b1, const B& b2)
00076 {
00077    bool intersect = true;
00078    for (int ii=0; ii < B::Point::Dim && intersect; ++ii)
00079    {
00080       typename B::Point::Coord lo = std::max (b1.getMin()[ii], b2.getMin()[ii]);
00081       typename B::Point::Coord hi = std::min (b1.getMax()[ii], b2.getMax()[ii]);
00082       intersect &= (lo < hi + util::doubleEps);
00083    }
00084    return intersect;
00085 }
00086 
00087 template<class B, class P>
00088 inline void getBoundingBox (const P& pt, B* pBox)
00089 {
00090    pBox->addPoint (pt);
00091 }
00092 
00093 template<class B>
00094 inline void expandBoundingBox (double eps, B* pBox)
00095 {
00096    typedef typename B::Point P;
00097    P lo (pBox->getMin()), hi (pBox->getMax());
00098    for (int ii=0; ii < P::Dim; ++ii)
00099    {
00100       lo[ii] -= eps;
00101       hi[ii] += eps;
00102    }
00103    pBox->addPoint (lo);
00104    pBox->addPoint (hi);
00105 }
00106 
00107 typedef BoundingBox<Point2d> BoundingBox2d;
00108 typedef BoundingBox<Point3d> BoundingBox3d;
00109 
00110 }
00111 
00112 #endif

Generated on Tue Jan 29 21:37:56 2008 for VoluMill Universal Client by  doxygen 1.4.6