Main Page   Namespace List   Class Hierarchy   Compound List   File List   Namespace Members   Compound Members  

ball.h

00001 // ball.h (A n-dimensional ball)
00002 //
00003 //  The WorldForge Project
00004 //  Copyright (C) 2000, 2001  The WorldForge Project
00005 //
00006 //  This program is free software; you can redistribute it and/or modify
00007 //  it under the terms of the GNU General Public License as published by
00008 //  the Free Software Foundation; either version 2 of the License, or
00009 //  (at your option) any later version.
00010 //
00011 //  This program 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
00014 //  GNU General Public License for more details.
00015 //
00016 //  You should have received a copy of the GNU General Public License
00017 //  along with this program; if not, write to the Free Software
00018 //  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00019 //
00020 //  For information about WorldForge and its authors, please contact
00021 //  the Worldforge Web Site at http://www.worldforge.org.
00022 //
00023 
00024 // Author: Ron Steinke
00025 
00026 #ifndef WFMATH_BALL_H
00027 #define WFMATH_BALL_H
00028 
00029 #include <wfmath/const.h>
00030 #include <wfmath/vector.h>
00031 #include <wfmath/point.h>
00032 #include <wfmath/axisbox.h>
00033 #include <wfmath/intersect_decls.h>
00034 
00035 namespace WFMath {
00036 
00037 template<const int dim> class Ball;
00038 
00039 #ifndef WFMATH_NO_TEMPLATES_AS_TEMPLATE_PARAMETERS
00040 
00041 template<const int dim, template<class> class container>
00042 Ball<dim> BoundingSphere(const container<Point<dim> >& c);
00044 template<const int dim, template<class> class container>
00045 Ball<dim> BoundingSphereSloppy(const container<Point<dim> >& c);
00046 #endif
00047 
00048 template<const int dim>
00049 std::ostream& operator<<(std::ostream& os, const Ball<dim>& m);
00050 template<const int dim>
00051 std::istream& operator>>(std::istream& is, Ball<dim>& m);
00052 
00054 
00064 template<const int dim>
00065 class Ball
00066 {
00067  public:
00069   Ball() {}
00071   Ball(const Point<dim>& center, CoordType radius)
00072         : m_center(center), m_radius(radius) {assert(radius >= 0);}
00074   Ball(const Ball& b) : m_center(b.m_center), m_radius(b.m_radius) {}
00075 
00076   ~Ball() {}
00077 
00078   friend std::ostream& operator<< <dim>(std::ostream& os, const Ball& b);
00079   friend std::istream& operator>> <dim>(std::istream& is, Ball& b);
00080 
00081   Ball& operator=(const Ball& b)
00082         {m_radius = b.m_radius; m_center = b.m_center; return *this;}
00083 
00084   bool isEqualTo(const Ball& b, double epsilon = WFMATH_EPSILON) const;
00085 
00086   bool operator==(const Ball& b) const  {return isEqualTo(b);}
00087   bool operator!=(const Ball& b) const  {return !isEqualTo(b);}
00088 
00089   bool isValid() const {return m_center.isValid();}
00090 
00091   bool operator< (const Ball& b) const;
00092 
00093   // Descriptive characteristics
00094 
00095   int numCorners() const {return 0;}
00096   // This next function exists so that Ball can be used by code
00097   // that finds the number of corners with numCorners(), and does something
00098   // with each corner with getCorner(). No idea how useful that is, but
00099   // it's not a particularly complicated function to write.
00100   Point<dim> getCorner(int i) const {assert(false);}
00101   Point<dim> getCenter() const {return m_center;}
00102 
00104   const Point<dim>& center() const {return m_center;}
00106   Point<dim>& center() {return m_center;}
00108   CoordType radius() const {return m_radius;}
00110   CoordType& radius() {return m_radius;}
00111 
00112   // Movement functions
00113 
00114   Ball& shift(const Vector<dim>& v) {m_center += v; return *this;}
00115   Ball& moveCornerTo(const Point<dim>& p, int corner) {assert(false);}
00116   Ball& moveCenterTo(const Point<dim>& p) {m_center = p; return *this;}
00117 
00118   Ball& rotateCorner(const RotMatrix<dim>& m, int corner) {assert(false);}
00119   Ball& rotateCenter(const RotMatrix<dim>& m) {return *this;}
00120   Ball& rotatePoint(const RotMatrix<dim>& m, const Point<dim>& p)
00121         {m_center.rotate(m, p); return *this;}
00122 
00123   // Intersection functions
00124 
00125   AxisBox<dim> boundingBox() const;
00126   Ball boundingSphere() const           {return *this;}
00127   Ball boundingSphereSloppy() const     {return *this;}
00128 
00129   friend bool Intersect<dim>(const Ball& b, const Point<dim>& p, bool proper);
00130   friend bool Contains<dim>(const Point<dim>& p, const Ball& b, bool proper);
00131 
00132   friend bool Intersect<dim>(const Ball& b, const AxisBox<dim>& a, bool proper);
00133   friend bool Contains<dim>(const Ball& b, const AxisBox<dim>& a, bool proper);
00134   friend bool Contains<dim>(const AxisBox<dim>& a, const Ball& b, bool proper);
00135 
00136   friend bool Intersect<dim>(const Ball& b1, const Ball& b2, bool proper);
00137   friend bool Contains<dim>(const Ball& outer, const Ball& inner, bool proper);
00138 
00139   friend bool Intersect<dim>(const Segment<dim>& s, const Ball& b, bool proper);
00140   friend bool Contains<dim>(const Segment<dim>& s, const Ball& b, bool proper);
00141 
00142   friend bool Intersect<dim>(const RotBox<dim>& r, const Ball& b, bool proper);
00143   friend bool Contains<dim>(const RotBox<dim>& r, const Ball& b, bool proper);
00144   friend bool Contains<dim>(const Ball& b, const RotBox<dim>& r, bool proper);
00145 
00146   friend bool Intersect<dim>(const Polygon<dim>& p, const Ball& b, bool proper);
00147   friend bool Contains<dim>(const Polygon<dim>& p, const Ball& b, bool proper);
00148   friend bool Contains<dim>(const Ball& b, const Polygon<dim>& p, bool proper);
00149 
00150  private:
00151 
00152   Point<dim> m_center;
00153   CoordType m_radius;
00154 };
00155 
00156 } // namespace WFMath
00157 
00158 #include <wfmath/ball_funcs.h>
00159 
00160 #endif  // WFMATH_BALL_H

Generated on Wed May 28 09:20:31 2003 for WFMath by doxygen1.3-rc3