1 // ball.h (A n-dimensional ball)
2 //
3 //  The WorldForge Project
4 //  Copyright (C) 2000, 2001  The WorldForge Project
5 //
6 //  This program is free software; you can redistribute it and/or modify
7 //  it under the terms of the GNU General Public License as published by
8 //  the Free Software Foundation; either version 2 of the License, or
9 //  (at your option) any later version.
10 //
11 //  This program is distributed in the hope that it will be useful,
12 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
13 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 //  GNU General Public License for more details.
15 //
16 //  You should have received a copy of the GNU General Public License
17 //  along with this program; if not, write to the Free Software
18 //  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 //
20 //  For information about WorldForge and its authors, please contact
21 //  the Worldforge Web Site at http://www.worldforge.org.
22 //
23 
24 // Author: Ron Steinke
25 
26 #ifndef WFMATH_BALL_H
27 #define WFMATH_BALL_H
28 
29 #include <wfmath/point.h>
30 #include <wfmath/intersect_decls.h>
31 
32 namespace WFMath {
33 
34 template<int dim> class Ball;
35 
36 /// get the minimal bounding sphere for a set of points
37 template<int dim, template<class, class> class container>
38 Ball<dim> BoundingSphere(const container<Point<dim>, std::allocator<Point<dim> > >& c);
39 /// get a bounding sphere for a set of points
40 template<int dim, template<class, class> class container>
41 Ball<dim> BoundingSphereSloppy(const container<Point<dim>, std::allocator<Point<dim> > >& c);
42 
43 template<int dim>
44 std::ostream& operator<<(std::ostream& os, const Ball<dim>& m);
45 template<int dim>
46 std::istream& operator>>(std::istream& is, Ball<dim>& m);
47 
48 /// A dim dimensional ball
49 /**
50  * This class implements the full shape interface, as described in
51  * the fake class Shape.
52  *
53  * This class is called Ball<> instead of Sphere to be more in tune
54  * with the usual mathematical naming conventions, where a ball is
55  * a filled object, while a sphere is just the outer shell. It also
56  * helps that a Ball<n> corresponds to an n-ball, while a Sphere<n>
57  * would correspond to an (n-1)-sphere.
58  **/
59 template<int dim = 3>
60 class Ball
61 {
62  public:
63   /// construct an uninitialized ball
Ball()64   Ball() : m_center(), m_radius(0.f) {}
65   /// construct a ball with the given center and radius
Ball(const Point<dim> & center,CoordType radius)66   Ball(const Point<dim>& center, CoordType radius)
67   : m_center(center), m_radius(radius) { if (radius < 0) m_center.setValid(false); }
68   /// construct a copy of a ball
Ball(const Ball & b)69   Ball(const Ball& b) : m_center(b.m_center), m_radius(b.m_radius) {}
70   /// Construct a ball from an object passed by Atlas
71   explicit Ball(const AtlasInType& a);
72 
~Ball()73   ~Ball() {}
74 
75   friend std::ostream& operator<< <dim>(std::ostream& os, const Ball& b);
76   friend std::istream& operator>> <dim>(std::istream& is, Ball& b);
77 
78   /// Create an Atlas object from the box
79   AtlasOutType toAtlas() const;
80   /// Set the box's value to that given by an Atlas object
81   void fromAtlas(const AtlasInType& a);
82 
83   Ball& operator=(const Ball& b)
84   {m_radius = b.m_radius; m_center = b.m_center; return *this;}
85 
86   bool isEqualTo(const Ball& b, CoordType epsilon = numeric_constants<CoordType>::epsilon()) const;
87 
88   bool operator==(const Ball& b) const	{return isEqualTo(b);}
89   bool operator!=(const Ball& b) const	{return !isEqualTo(b);}
90 
isValid()91   bool isValid() const {return m_center.isValid();}
92 
93   // Descriptive characteristics
94 
numCorners()95   size_t numCorners() const {return 0;}
96   // This next function exists so that Ball can be used by code
97   // that finds the number of corners with numCorners(), and does something
98   // with each corner with getCorner(). No idea how useful that is, but
99   // it's not a particularly complicated function to write.
getCorner(size_t)100   Point<dim> getCorner(size_t) const {return m_center;}
getCenter()101   Point<dim> getCenter() const {return m_center;}
102 
103   /// get the center of the ball
center()104   const Point<dim>& center() const {return m_center;}
105   /// get the center of the ball
center()106   Point<dim>& center() {return m_center;}
107   /// get the radius of the ball
radius()108   CoordType radius() const {return m_radius;}
109   /// get the radius of the ball
radius()110   CoordType& radius() {return m_radius;}
111 
112   // Movement functions
113 
shift(const Vector<dim> & v)114   Ball& shift(const Vector<dim>& v) {m_center += v; return *this;}
moveCornerTo(const Point<dim> &,size_t)115   Ball& moveCornerTo(const Point<dim>&, size_t) {return *this;}
moveCenterTo(const Point<dim> & p)116   Ball& moveCenterTo(const Point<dim>& p) {m_center = p; return *this;}
117 
rotateCorner(const RotMatrix<dim> &,size_t)118   Ball& rotateCorner(const RotMatrix<dim>&, size_t) {return *this;}
rotateCenter(const RotMatrix<dim> &)119   Ball& rotateCenter(const RotMatrix<dim>&) {return *this;}
rotatePoint(const RotMatrix<dim> & m,const Point<dim> & p)120   Ball& rotatePoint(const RotMatrix<dim>& m, const Point<dim>& p)
121   {m_center.rotate(m, p); return *this;}
122 
123   // 3D rotation function
124   Ball& rotateCorner(const Quaternion&, size_t corner);
125   Ball& rotateCenter(const Quaternion&);
126   Ball& rotatePoint(const Quaternion& q, const Point<dim>& p);
127 
128   // Intersection functions
129 
130   AxisBox<dim> boundingBox() const;
boundingSphere()131   Ball boundingSphere() const		{return *this;}
boundingSphereSloppy()132   Ball boundingSphereSloppy() const	{return *this;}
133 
134   Ball toParentCoords(const Point<dim>& origin,
135       const RotMatrix<dim>& rotation = RotMatrix<dim>().identity()) const
136         {return Ball(m_center.toParentCoords(origin, rotation), m_radius);}
toParentCoords(const AxisBox<dim> & coords)137   Ball toParentCoords(const AxisBox<dim>& coords) const
138         {return Ball(m_center.toParentCoords(coords), m_radius);}
toParentCoords(const RotBox<dim> & coords)139   Ball toParentCoords(const RotBox<dim>& coords) const
140         {return Ball(m_center.toParentCoords(coords), m_radius);}
141 
142   // toLocal is just like toParent, expect we reverse the order of
143   // translation and rotation and use the opposite sense of the rotation
144   // matrix
145 
146   Ball toLocalCoords(const Point<dim>& origin,
147       const RotMatrix<dim>& rotation = RotMatrix<dim>().identity()) const
148         {return Ball(m_center.toLocalCoords(origin, rotation), m_radius);}
toLocalCoords(const AxisBox<dim> & coords)149   Ball toLocalCoords(const AxisBox<dim>& coords) const
150         {return Ball(m_center.toLocalCoords(coords), m_radius);}
toLocalCoords(const RotBox<dim> & coords)151   Ball toLocalCoords(const RotBox<dim>& coords) const
152         {return Ball(m_center.toLocalCoords(coords), m_radius);}
153 
154   // 3D only
155   Ball toParentCoords(const Point<dim>& origin, const Quaternion& rotation) const;
156   Ball toLocalCoords(const Point<dim>& origin, const Quaternion& rotation) const;
157 
158   friend bool Intersect<dim>(const Ball& b, const Point<dim>& p, bool proper);
159   friend bool Contains<dim>(const Point<dim>& p, const Ball& b, bool proper);
160 
161   friend bool Intersect<dim>(const Ball& b, const AxisBox<dim>& a, bool proper);
162   friend bool Contains<dim>(const Ball& b, const AxisBox<dim>& a, bool proper);
163   friend bool Contains<dim>(const AxisBox<dim>& a, const Ball& b, bool proper);
164 
165   friend bool Intersect<dim>(const Ball& b1, const Ball& b2, bool proper);
166   friend bool Contains<dim>(const Ball& outer, const Ball& inner, bool proper);
167 
168   friend bool Intersect<dim>(const Segment<dim>& s, const Ball& b, bool proper);
169   friend bool Contains<dim>(const Segment<dim>& s, const Ball& b, bool proper);
170 
171   friend bool Intersect<dim>(const RotBox<dim>& r, const Ball& b, bool proper);
172   friend bool Contains<dim>(const RotBox<dim>& r, const Ball& b, bool proper);
173   friend bool Contains<dim>(const Ball& b, const RotBox<dim>& r, bool proper);
174 
175   friend bool Intersect<dim>(const Polygon<dim>& p, const Ball& b, bool proper);
176   friend bool Contains<dim>(const Polygon<dim>& p, const Ball& b, bool proper);
177   friend bool Contains<dim>(const Ball& b, const Polygon<dim>& p, bool proper);
178 
179  private:
180 
181   Point<dim> m_center;
182   CoordType m_radius;
183 };
184 
185 template<int dim>
isEqualTo(const Ball<dim> & b,CoordType epsilon)186 inline bool Ball<dim>::isEqualTo(const Ball<dim>& b, CoordType epsilon) const
187 {
188   return Equal(m_center, b.m_center, epsilon)
189       && Equal(m_radius, b.m_radius, epsilon);
190 }
191 
192 } // namespace WFMath
193 
194 #endif  // WFMATH_BALL_H
195