WFMath 0.3.11
|
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/point.h> 00030 #include <wfmath/intersect_decls.h> 00031 00032 namespace WFMath { 00033 00034 template<const int dim> class Ball; 00035 00036 #ifndef WFMATH_NO_TEMPLATES_AS_TEMPLATE_PARAMETERS 00037 00038 template<const int dim, template<class, class> class container> 00039 Ball<dim> BoundingSphere(const container<Point<dim>, std::allocator<Point<dim> > >& c); 00041 template<const int dim, template<class, class> class container> 00042 Ball<dim> BoundingSphereSloppy(const container<Point<dim>, std::allocator<Point<dim> > >& c); 00043 #endif 00044 00045 template<const int dim> 00046 std::ostream& operator<<(std::ostream& os, const Ball<dim>& m); 00047 template<const int dim> 00048 std::istream& operator>>(std::istream& is, Ball<dim>& m); 00049 00051 00061 template<const int dim = 3> 00062 class Ball 00063 { 00064 public: 00066 Ball() {} 00068 Ball(const Point<dim>& center, CoordType radius) 00069 : m_center(center), m_radius(radius) { if (radius < 0) m_center.setValid(false); } 00071 Ball(const Ball& b) : m_center(b.m_center), m_radius(b.m_radius) {} 00073 explicit Ball(const AtlasInType& a); 00074 00075 ~Ball() {} 00076 00077 friend std::ostream& operator<< <dim>(std::ostream& os, const Ball& b); 00078 friend std::istream& operator>> <dim>(std::istream& is, Ball& b); 00079 00081 AtlasOutType toAtlas() const; 00083 void fromAtlas(const AtlasInType& a); 00084 00085 Ball& operator=(const Ball& b) 00086 {m_radius = b.m_radius; m_center = b.m_center; return *this;} 00087 00088 bool isEqualTo(const Ball& b, double epsilon = WFMATH_EPSILON) const; 00089 00090 bool operator==(const Ball& b) const {return isEqualTo(b);} 00091 bool operator!=(const Ball& b) const {return !isEqualTo(b);} 00092 00093 bool isValid() const {return m_center.isValid();} 00094 00095 // Descriptive characteristics 00096 00097 int numCorners() const {return 0;} 00098 // This next function exists so that Ball can be used by code 00099 // that finds the number of corners with numCorners(), and does something 00100 // with each corner with getCorner(). No idea how useful that is, but 00101 // it's not a particularly complicated function to write. 00102 Point<dim> getCorner(int i) const {return m_center;} 00103 Point<dim> getCenter() const {return m_center;} 00104 00106 const Point<dim>& center() const {return m_center;} 00108 Point<dim>& center() {return m_center;} 00110 CoordType radius() const {return m_radius;} 00112 CoordType& radius() {return m_radius;} 00113 00114 // Movement functions 00115 00116 Ball& shift(const Vector<dim>& v) {m_center += v; return *this;} 00117 Ball& moveCornerTo(const Point<dim>& p, int corner) {return *this;} 00118 Ball& moveCenterTo(const Point<dim>& p) {m_center = p; return *this;} 00119 00120 Ball& rotateCorner(const RotMatrix<dim>& m, int corner) {return *this;} 00121 Ball& rotateCenter(const RotMatrix<dim>& m) {return *this;} 00122 Ball& rotatePoint(const RotMatrix<dim>& m, const Point<dim>& p) 00123 {m_center.rotate(m, p); return *this;} 00124 00125 // 3D rotation function 00126 Ball& rotateCorner(const Quaternion&, int corner); 00127 Ball& rotateCenter(const Quaternion&); 00128 Ball& rotatePoint(const Quaternion& q, const Point<dim>& p); 00129 00130 // Intersection functions 00131 00132 AxisBox<dim> boundingBox() const; 00133 Ball boundingSphere() const {return *this;} 00134 Ball boundingSphereSloppy() const {return *this;} 00135 00136 Ball toParentCoords(const Point<dim>& origin, 00137 const RotMatrix<dim>& rotation = RotMatrix<dim>().identity()) const 00138 {return Ball(m_center.toParentCoords(origin, rotation), m_radius);} 00139 Ball toParentCoords(const AxisBox<dim>& coords) const 00140 {return Ball(m_center.toParentCoords(coords), m_radius);} 00141 Ball toParentCoords(const RotBox<dim>& coords) const 00142 {return Ball(m_center.toParentCoords(coords), m_radius);} 00143 00144 // toLocal is just like toParent, expect we reverse the order of 00145 // translation and rotation and use the opposite sense of the rotation 00146 // matrix 00147 00148 Ball toLocalCoords(const Point<dim>& origin, 00149 const RotMatrix<dim>& rotation = RotMatrix<dim>().identity()) const 00150 {return Ball(m_center.toLocalCoords(origin, rotation), m_radius);} 00151 Ball toLocalCoords(const AxisBox<dim>& coords) const 00152 {return Ball(m_center.toLocalCoords(coords), m_radius);} 00153 Ball toLocalCoords(const RotBox<dim>& coords) const 00154 {return Ball(m_center.toLocalCoords(coords), m_radius);} 00155 00156 // 3D only 00157 Ball toParentCoords(const Point<dim>& origin, const Quaternion& rotation) const; 00158 Ball toLocalCoords(const Point<dim>& origin, const Quaternion& rotation) const; 00159 00160 friend bool Intersect<dim>(const Ball& b, const Point<dim>& p, bool proper); 00161 friend bool Contains<dim>(const Point<dim>& p, const Ball& b, bool proper); 00162 00163 friend bool Intersect<dim>(const Ball& b, const AxisBox<dim>& a, bool proper); 00164 friend bool Contains<dim>(const Ball& b, const AxisBox<dim>& a, bool proper); 00165 friend bool Contains<dim>(const AxisBox<dim>& a, const Ball& b, bool proper); 00166 00167 friend bool Intersect<dim>(const Ball& b1, const Ball& b2, bool proper); 00168 friend bool Contains<dim>(const Ball& outer, const Ball& inner, bool proper); 00169 00170 friend bool Intersect<dim>(const Segment<dim>& s, const Ball& b, bool proper); 00171 friend bool Contains<dim>(const Segment<dim>& s, const Ball& b, bool proper); 00172 00173 friend bool Intersect<dim>(const RotBox<dim>& r, const Ball& b, bool proper); 00174 friend bool Contains<dim>(const RotBox<dim>& r, const Ball& b, bool proper); 00175 friend bool Contains<dim>(const Ball& b, const RotBox<dim>& r, bool proper); 00176 00177 friend bool Intersect<dim>(const Polygon<dim>& p, const Ball& b, bool proper); 00178 friend bool Contains<dim>(const Polygon<dim>& p, const Ball& b, bool proper); 00179 friend bool Contains<dim>(const Ball& b, const Polygon<dim>& p, bool proper); 00180 00181 private: 00182 00183 Point<dim> m_center; 00184 CoordType m_radius; 00185 }; 00186 00187 } // namespace WFMath 00188 00189 #endif // WFMATH_BALL_H