wfmath  1.0.3
A math library for the Worldforge system.
ball.h
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 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 
37 template<int dim, template<class, class> class container>
38 Ball<dim> BoundingSphere(const container<Point<dim>, std::allocator<Point<dim> > >& c);
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 
49 
59 template<int dim = 3>
60 class Ball
61 {
62  public:
64  Ball() : m_center{}, m_radius(0) {}
67  : m_center(center), m_radius(radius) { if (radius < 0) m_center.setValid(false); }
69  Ball(const Ball& b) = default;
71  explicit Ball(const AtlasInType& a);
72 
73  ~Ball() = default;
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 
79  AtlasOutType toAtlas() const;
81  void fromAtlas(const AtlasInType& a);
82 
83  Ball& operator=(const Ball& b) = default;
84 
85  bool isEqualTo(const Ball& b, CoordType epsilon = numeric_constants<CoordType>::epsilon()) const;
86 
87  bool operator==(const Ball& b) const {return isEqualTo(b);}
88  bool operator!=(const Ball& b) const {return !isEqualTo(b);}
89 
90  bool isValid() const {return m_center.isValid();}
91 
92  // Descriptive characteristics
93 
94  size_t numCorners() const {return 0;}
95  // This next function exists so that Ball can be used by code
96  // that finds the number of corners with numCorners(), and does something
97  // with each corner with getCorner(). No idea how useful that is, but
98  // it's not a particularly complicated function to write.
99  Point<dim> getCorner(size_t) const {return m_center;}
100  Point<dim> getCenter() const {return m_center;}
101 
103  const Point<dim>& center() const {return m_center;}
105  Point<dim>& center() {return m_center;}
107  CoordType radius() const {return m_radius;}
109  CoordType& radius() {return m_radius;}
110 
111  // Movement functions
112 
113  Ball& shift(const Vector<dim>& v) {m_center += v; return *this;}
114  Ball& moveCornerTo(const Point<dim>&, size_t) {return *this;}
115  Ball& moveCenterTo(const Point<dim>& p) {m_center = p; return *this;}
116 
117  Ball& rotateCorner(const RotMatrix<dim>&, size_t) {return *this;}
118  Ball& rotateCenter(const RotMatrix<dim>&) {return *this;}
119  Ball& rotatePoint(const RotMatrix<dim>& m, const Point<dim>& p)
120  {m_center.rotate(m, p); return *this;}
121 
122  // 3D rotation function
123  Ball& rotateCorner(const Quaternion&, size_t corner);
124  Ball& rotateCenter(const Quaternion&);
125  Ball& rotatePoint(const Quaternion& q, const Point<dim>& p);
126 
127  // Intersection functions
128 
129  AxisBox<dim> boundingBox() const;
130  Ball boundingSphere() const {return *this;}
131  Ball boundingSphereSloppy() const {return *this;}
132 
133  Ball toParentCoords(const Point<dim>& origin,
134  const RotMatrix<dim>& rotation = RotMatrix<dim>().identity()) const
135  {return Ball(m_center.toParentCoords(origin, rotation), m_radius);}
136  Ball toParentCoords(const AxisBox<dim>& coords) const
137  {return Ball(m_center.toParentCoords(coords), m_radius);}
138  Ball toParentCoords(const RotBox<dim>& coords) const
139  {return Ball(m_center.toParentCoords(coords), m_radius);}
140 
141  // toLocal is just like toParent, expect we reverse the order of
142  // translation and rotation and use the opposite sense of the rotation
143  // matrix
144 
145  Ball toLocalCoords(const Point<dim>& origin,
146  const RotMatrix<dim>& rotation = RotMatrix<dim>().identity()) const
147  {return Ball(m_center.toLocalCoords(origin, rotation), m_radius);}
148  Ball toLocalCoords(const AxisBox<dim>& coords) const
149  {return Ball(m_center.toLocalCoords(coords), m_radius);}
150  Ball toLocalCoords(const RotBox<dim>& coords) const
151  {return Ball(m_center.toLocalCoords(coords), m_radius);}
152 
153  // 3D only
154  Ball toParentCoords(const Point<dim>& origin, const Quaternion& rotation) const;
155  Ball toLocalCoords(const Point<dim>& origin, const Quaternion& rotation) const;
156 
157  friend bool Intersect<dim>(const Ball& b, const Point<dim>& p, bool proper);
158  friend bool Contains<dim>(const Point<dim>& p, const Ball& b, bool proper);
159 
160  friend bool Intersect<dim>(const Ball& b, const AxisBox<dim>& a, bool proper);
161  friend bool Contains<dim>(const Ball& b, const AxisBox<dim>& a, bool proper);
162  friend bool Contains<dim>(const AxisBox<dim>& a, const Ball& b, bool proper);
163 
164  friend bool Intersect<dim>(const Ball& b1, const Ball& b2, bool proper);
165  friend bool Contains<dim>(const Ball& outer, const Ball& inner, bool proper);
166 
167  friend bool Intersect<dim>(const Segment<dim>& s, const Ball& b, bool proper);
168  friend bool Contains<dim>(const Segment<dim>& s, const Ball& b, bool proper);
169 
170  friend bool Intersect<dim>(const RotBox<dim>& r, const Ball& b, bool proper);
171  friend bool Contains<dim>(const RotBox<dim>& r, const Ball& b, bool proper);
172  friend bool Contains<dim>(const Ball& b, const RotBox<dim>& r, bool proper);
173 
174  friend bool Intersect<dim>(const Polygon<dim>& p, const Ball& b, bool proper);
175  friend bool Contains<dim>(const Polygon<dim>& p, const Ball& b, bool proper);
176  friend bool Contains<dim>(const Ball& b, const Polygon<dim>& p, bool proper);
177 
178  private:
179 
180  Point<dim> m_center;
181  CoordType m_radius;
182 };
183 
184 template<int dim>
185 inline bool Ball<dim>::isEqualTo(const Ball<dim>& b, CoordType epsilon) const
186 {
187  return Equal(m_center, b.m_center, epsilon)
188  && Equal(m_radius, b.m_radius, epsilon);
189 }
190 
191 } // namespace WFMath
192 
193 #endif // WFMATH_BALL_H
WFMath::Ball::radius
CoordType & radius()
get the radius of the ball
Definition: ball.h:109
WFMath::Ball::Ball
Ball(const Ball &b)=default
construct a copy of a ball
WFMath::BoundingSphere
Ball< dim > BoundingSphere(const container< Point< dim >, std::allocator< Point< dim > > > &c)
get the minimal bounding sphere for a set of points
Definition: ball_funcs.h:57
WFMath::Ball::toAtlas
AtlasOutType toAtlas() const
Create an Atlas object from the box.
Definition: atlasconv.h:309
WFMath::Ball
A dim dimensional ball.
Definition: ball.h:34
WFMath
Generic library namespace.
Definition: shape.h:41
WFMath::Ball::Ball
Ball()
construct an uninitialized ball
Definition: ball.h:64
WFMath::Vector
A dim dimensional vector.
Definition: const.h:55
WFMath::Ball::center
const Point< dim > & center() const
get the center of the ball
Definition: ball.h:103
WFMath::Ball::center
Point< dim > & center()
get the center of the ball
Definition: ball.h:105
WFMath::CoordType
double CoordType
Basic floating point type.
Definition: const.h:140
WFMath::numeric_constants
Definition: const.h:65
WFMath::Ball::radius
CoordType radius() const
get the radius of the ball
Definition: ball.h:107
WFMath::AtlasOutType
Definition: atlasconv.h:68
WFMath::AtlasInType
Definition: atlasconv.h:54
WFMath::BoundingSphereSloppy
Ball< dim > BoundingSphereSloppy(const container< Point< dim >, std::allocator< Point< dim > > > &c)
get a bounding sphere for a set of points
Definition: ball_funcs.h:92
WFMath::Ball::Ball
Ball(const Point< dim > &center, CoordType radius)
construct a ball with the given center and radius
Definition: ball.h:66
WFMath::Ball::fromAtlas
void fromAtlas(const AtlasInType &a)
Set the box's value to that given by an Atlas object.
Definition: atlasconv.h:280
WFMath::Point
A dim dimensional point.
Definition: const.h:50