ESyS-Particle  4.0.1
BoundingSphere.hpp
00001 
00002 //                                                         //
00003 // Copyright (c) 2003-2011 by The University of Queensland //
00004 // Earth Systems Science Computational Centre (ESSCC)      //
00005 // http://www.uq.edu.au/esscc                              //
00006 //                                                         //
00007 // Primary Business: Brisbane, Queensland, Australia       //
00008 // Licensed under the Open Software License version 3.0    //
00009 // http://www.opensource.org/licenses/osl-3.0.php          //
00010 //                                                         //
00012 
00013 
00014 #include "Foundation/BoundingSphere.h"
00015 
00016 namespace esys
00017 {
00018 
00019   namespace lsm
00020   {
00021     BoundingSphere::BoundingSphere()
00022       : m_centre(Vec3::ZERO),
00023         m_radius(0.0)
00024     {
00025     }
00026 
00027     BoundingSphere::BoundingSphere(const Vec3 &centre, double radius)
00028       : m_centre(centre),
00029         m_radius(radius)
00030     {
00031     }
00032   
00033     BoundingSphere::~BoundingSphere()
00034     {
00035     }
00036 
00037     const Vec3 &BoundingSphere::getCentre() const
00038     {
00039       return m_centre;
00040     }
00041  
00042     double BoundingSphere::getRadius() const
00043     {
00044       return m_radius;
00045     }
00046 
00047     BoundingBox BoundingSphere::getBBox() const
00048     {
00049       return BoundingBox(getCentre()-getRadius(), getCentre()+getRadius());
00050     }
00051 
00052     BoundingBox BoundingSphere::get2dBBox() const
00053     {
00054       return
00055         BoundingBox(
00056           Vec3(getCentre()[0]-getRadius(), getCentre()[1]-getRadius(), 0.0),
00057           Vec3(getCentre()[0]+getRadius(), getCentre()[1]+getRadius(), 0.0)
00058         );
00059     }
00060 
00061     bool BoundingSphere::operator==(const BoundingSphere &bSphere) const
00062     {
00063       return
00064         (
00065           (getCentre() == bSphere.getCentre())
00066           &&
00067           (getRadius() == bSphere.getRadius())
00068         );
00069     }
00070     
00071     bool BoundingSphere::contains(const Vec3 &pt, double tolerance) const
00072     {
00073       const double r = (getRadius() + tolerance);
00074       return ((getCentre()-pt).norm2() <= (r*r));
00075     }
00076 
00077     bool BoundingSphere::contains(
00078       const BoundingSphere &bSphere,
00079       double tolerance
00080     ) const
00081     {
00082       const double r = (getRadius()-bSphere.getRadius() + tolerance);
00083       return ((getCentre()-bSphere.getCentre()).norm2() <= (r*r));
00084     }
00085     
00086     std::ostream &operator<<(std::ostream &oStream, const BoundingSphere &bSphere)
00087     {
00088       oStream << bSphere.getCentre() << " " << bSphere.getRadius();
00089       return oStream;
00090     }
00091   };
00092 };