00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
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/rotbox.h>
00034 #include <wfmath/intersect_decls.h>
00035
00036 #include <cstdlib>
00037
00038 namespace WFMath {
00039
00040 template<const int dim> class Ball;
00041
00042 #ifndef WFMATH_NO_TEMPLATES_AS_TEMPLATE_PARAMETERS
00043
00044 template<const int dim, template<class, class> class container>
00045 Ball<dim> BoundingSphere(const container<Point<dim>, std::allocator<Point<dim> > >& c);
00047 template<const int dim, template<class, class> class container>
00048 Ball<dim> BoundingSphereSloppy(const container<Point<dim>, std::allocator<Point<dim> > >& c);
00049 #endif
00050
00051 template<const int dim>
00052 std::ostream& operator<<(std::ostream& os, const Ball<dim>& m);
00053 template<const int dim>
00054 std::istream& operator>>(std::istream& is, Ball<dim>& m);
00055
00057
00067 template<const int dim>
00068 class Ball
00069 {
00070 public:
00072 Ball() {}
00074 Ball(const Point<dim>& center, CoordType radius)
00075 : m_center(center), m_radius(radius) {assert(radius >= 0);}
00077 Ball(const Ball& b) : m_center(b.m_center), m_radius(b.m_radius) {}
00079 explicit Ball(const AtlasInType& a)
00080 : m_center(Point<dim>::ZERO()), m_radius(0)
00081 {fromAtlas(a);}
00082
00083 ~Ball() {}
00084
00085 friend std::ostream& operator<< <dim>(std::ostream& os, const Ball& b);
00086 friend std::istream& operator>> <dim>(std::istream& is, Ball& b);
00087
00089 AtlasOutType toAtlas() const;
00091 void fromAtlas(const AtlasInType& a);
00092
00093 Ball& operator=(const Ball& b)
00094 {m_radius = b.m_radius; m_center = b.m_center; return *this;}
00095
00096 bool isEqualTo(const Ball& b, double epsilon = WFMATH_EPSILON) const;
00097
00098 bool operator==(const Ball& b) const {return isEqualTo(b);}
00099 bool operator!=(const Ball& b) const {return !isEqualTo(b);}
00100
00101 bool isValid() const {return m_center.isValid();}
00102
00103
00104
00105 int numCorners() const {return 0;}
00106
00107
00108
00109
00110 Point<dim> getCorner(int i) const {abort(); return m_center;}
00111 Point<dim> getCenter() const {return m_center;}
00112
00114 const Point<dim>& center() const {return m_center;}
00116 Point<dim>& center() {return m_center;}
00118 CoordType radius() const {return m_radius;}
00120 CoordType& radius() {return m_radius;}
00121
00122
00123
00124 Ball& shift(const Vector<dim>& v) {m_center += v; return *this;}
00125 Ball& moveCornerTo(const Point<dim>& p, int corner) {abort(); return *this;}
00126 Ball& moveCenterTo(const Point<dim>& p) {m_center = p; return *this;}
00127
00128 Ball& rotateCorner(const RotMatrix<dim>& m, int corner) {abort(); return *this;}
00129 Ball& rotateCenter(const RotMatrix<dim>& m) {return *this;}
00130 Ball& rotatePoint(const RotMatrix<dim>& m, const Point<dim>& p)
00131 {m_center.rotate(m, p); return *this;}
00132
00133
00134 Ball<3>& rotateCorner(const Quaternion&, int corner) {abort(); return *this;}
00135 Ball<3>& rotateCenter(const Quaternion&) {return *this;}
00136 Ball<3>& rotatePoint(const Quaternion& q, const Point<3>& p)
00137 {m_center.rotate(q, p); return *this;}
00138
00139
00140
00141 AxisBox<dim> boundingBox() const;
00142 Ball boundingSphere() const {return *this;}
00143 Ball boundingSphereSloppy() const {return *this;}
00144
00145 Ball toParentCoords(const Point<dim>& origin,
00146 const RotMatrix<dim>& rotation = RotMatrix<dim>().identity()) const
00147 {return Ball(m_center.toParentCoords(origin, rotation), m_radius);}
00148 Ball toParentCoords(const AxisBox<dim>& coords) const
00149 {return Ball(m_center.toParentCoords(coords), m_radius);}
00150 Ball toParentCoords(const RotBox<dim>& coords) const
00151 {return Ball(m_center.toParentCoords(coords), m_radius);}
00152
00153
00154
00155
00156
00157 Ball toLocalCoords(const Point<dim>& origin,
00158 const RotMatrix<dim>& rotation = RotMatrix<dim>().identity()) const
00159 {return Ball(m_center.toLocalCoords(origin, rotation), m_radius);}
00160 Ball toLocalCoords(const AxisBox<dim>& coords) const
00161 {return Ball(m_center.toLocalCoords(coords), m_radius);}
00162 Ball toLocalCoords(const RotBox<dim>& coords) const
00163 {return Ball(m_center.toLocalCoords(coords), m_radius);}
00164
00165
00166 Ball<3> toParentCoords(const Point<3>& origin, const Quaternion& rotation) const
00167 {return Ball<3>(m_center.toParentCoords(origin, rotation), m_radius);}
00168 Ball<3> toLocalCoords(const Point<3>& origin, const Quaternion& rotation) const
00169 {return Ball<3>(m_center.toLocalCoords(origin, rotation), m_radius);}
00170
00171 friend bool Intersect<dim>(const Ball& b, const Point<dim>& p, bool proper);
00172 friend bool Contains<dim>(const Point<dim>& p, const Ball& b, bool proper);
00173
00174 friend bool Intersect<dim>(const Ball& b, const AxisBox<dim>& a, bool proper);
00175 friend bool Contains<dim>(const Ball& b, const AxisBox<dim>& a, bool proper);
00176 friend bool Contains<dim>(const AxisBox<dim>& a, const Ball& b, bool proper);
00177
00178 friend bool Intersect<dim>(const Ball& b1, const Ball& b2, bool proper);
00179 friend bool Contains<dim>(const Ball& outer, const Ball& inner, bool proper);
00180
00181 friend bool Intersect<dim>(const Segment<dim>& s, const Ball& b, bool proper);
00182 friend bool Contains<dim>(const Segment<dim>& s, const Ball& b, bool proper);
00183
00184 friend bool Intersect<dim>(const RotBox<dim>& r, const Ball& b, bool proper);
00185 friend bool Contains<dim>(const RotBox<dim>& r, const Ball& b, bool proper);
00186 friend bool Contains<dim>(const Ball& b, const RotBox<dim>& r, bool proper);
00187
00188 friend bool Intersect<dim>(const Polygon<dim>& p, const Ball& b, bool proper);
00189 friend bool Contains<dim>(const Polygon<dim>& p, const Ball& b, bool proper);
00190 friend bool Contains<dim>(const Ball& b, const Polygon<dim>& p, bool proper);
00191
00192 private:
00193
00194 Point<dim> m_center;
00195 CoordType m_radius;
00196 };
00197
00198 }
00199
00200 #include <wfmath/ball_funcs.h>
00201
00202 #endif // WFMATH_BALL_H