ESyS-Particle  4.0.1
BoundingSphere.hpp
1 
2 // //
3 // Copyright (c) 2003-2011 by The University of Queensland //
4 // Earth Systems Science Computational Centre (ESSCC) //
5 // http://www.uq.edu.au/esscc //
6 // //
7 // Primary Business: Brisbane, Queensland, Australia //
8 // Licensed under the Open Software License version 3.0 //
9 // http://www.opensource.org/licenses/osl-3.0.php //
10 // //
12 
13 
14 #include "Foundation/BoundingSphere.h"
15 
16 namespace esys
17 {
18 
19  namespace lsm
20  {
21  BoundingSphere::BoundingSphere()
22  : m_centre(Vec3::ZERO),
23  m_radius(0.0)
24  {
25  }
26 
27  BoundingSphere::BoundingSphere(const Vec3 &centre, double radius)
28  : m_centre(centre),
29  m_radius(radius)
30  {
31  }
32 
33  BoundingSphere::~BoundingSphere()
34  {
35  }
36 
37  const Vec3 &BoundingSphere::getCentre() const
38  {
39  return m_centre;
40  }
41 
42  double BoundingSphere::getRadius() const
43  {
44  return m_radius;
45  }
46 
47  BoundingBox BoundingSphere::getBBox() const
48  {
49  return BoundingBox(getCentre()-getRadius(), getCentre()+getRadius());
50  }
51 
52  BoundingBox BoundingSphere::get2dBBox() const
53  {
54  return
55  BoundingBox(
56  Vec3(getCentre()[0]-getRadius(), getCentre()[1]-getRadius(), 0.0),
57  Vec3(getCentre()[0]+getRadius(), getCentre()[1]+getRadius(), 0.0)
58  );
59  }
60 
61  bool BoundingSphere::operator==(const BoundingSphere &bSphere) const
62  {
63  return
64  (
65  (getCentre() == bSphere.getCentre())
66  &&
67  (getRadius() == bSphere.getRadius())
68  );
69  }
70 
71  bool BoundingSphere::contains(const Vec3 &pt, double tolerance) const
72  {
73  const double r = (getRadius() + tolerance);
74  return ((getCentre()-pt).norm2() <= (r*r));
75  }
76 
77  bool BoundingSphere::contains(
78  const BoundingSphere &bSphere,
79  double tolerance
80  ) const
81  {
82  const double r = (getRadius()-bSphere.getRadius() + tolerance);
83  return ((getCentre()-bSphere.getCentre()).norm2() <= (r*r));
84  }
85 
86  std::ostream &operator<<(std::ostream &oStream, const BoundingSphere &bSphere)
87  {
88  oStream << bSphere.getCentre() << " " << bSphere.getRadius();
89  return oStream;
90  }
91  };
92 };