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/point.h>
00030 #include <wfmath/intersect_decls.h>
00031
00032 namespace WFMath {
00033
00034 template<int dim> class Ball;
00035
00037 template<int dim, template<class, class> class container>
00038 Ball<dim> BoundingSphere(const container<Point<dim>, std::allocator<Point<dim> > >& c);
00040 template<int dim, template<class, class> class container>
00041 Ball<dim> BoundingSphereSloppy(const container<Point<dim>, std::allocator<Point<dim> > >& c);
00042
00043 template<int dim>
00044 std::ostream& operator<<(std::ostream& os, const Ball<dim>& m);
00045 template<int dim>
00046 std::istream& operator>>(std::istream& is, Ball<dim>& m);
00047
00049
00059 template<int dim = 3>
00060 class Ball
00061 {
00062 public:
00064 Ball() : m_center(), m_radius(0.f) {}
00066 Ball(const Point<dim>& center, CoordType radius)
00067 : m_center(center), m_radius(radius) { if (radius < 0) m_center.setValid(false); }
00069 Ball(const Ball& b) : m_center(b.m_center), m_radius(b.m_radius) {}
00071 explicit Ball(const AtlasInType& a);
00072
00073 ~Ball() {}
00074
00075 friend std::ostream& operator<< <dim>(std::ostream& os, const Ball& b);
00076 friend std::istream& operator>> <dim>(std::istream& is, Ball& b);
00077
00079 AtlasOutType toAtlas() const;
00081 void fromAtlas(const AtlasInType& a);
00082
00083 Ball& operator=(const Ball& b)
00084 {m_radius = b.m_radius; m_center = b.m_center; return *this;}
00085
00086 bool isEqualTo(const Ball& b, CoordType epsilon = numeric_constants<CoordType>::epsilon()) const;
00087
00088 bool operator==(const Ball& b) const {return isEqualTo(b);}
00089 bool operator!=(const Ball& b) const {return !isEqualTo(b);}
00090
00091 bool isValid() const {return m_center.isValid();}
00092
00093
00094
00095 size_t numCorners() const {return 0;}
00096
00097
00098
00099
00100 Point<dim> getCorner(size_t) const {return m_center;}
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
00113
00114 Ball& shift(const Vector<dim>& v) {m_center += v; return *this;}
00115 Ball& moveCornerTo(const Point<dim>&, size_t) {return *this;}
00116 Ball& moveCenterTo(const Point<dim>& p) {m_center = p; return *this;}
00117
00118 Ball& rotateCorner(const RotMatrix<dim>&, size_t) {return *this;}
00119 Ball& rotateCenter(const RotMatrix<dim>&) {return *this;}
00120 Ball& rotatePoint(const RotMatrix<dim>& m, const Point<dim>& p)
00121 {m_center.rotate(m, p); return *this;}
00122
00123
00124 Ball& rotateCorner(const Quaternion&, size_t corner);
00125 Ball& rotateCenter(const Quaternion&);
00126 Ball& rotatePoint(const Quaternion& q, const Point<dim>& p);
00127
00128
00129
00130 AxisBox<dim> boundingBox() const;
00131 Ball boundingSphere() const {return *this;}
00132 Ball boundingSphereSloppy() const {return *this;}
00133
00134 Ball toParentCoords(const Point<dim>& origin,
00135 const RotMatrix<dim>& rotation = RotMatrix<dim>().identity()) const
00136 {return Ball(m_center.toParentCoords(origin, rotation), m_radius);}
00137 Ball toParentCoords(const AxisBox<dim>& coords) const
00138 {return Ball(m_center.toParentCoords(coords), m_radius);}
00139 Ball toParentCoords(const RotBox<dim>& coords) const
00140 {return Ball(m_center.toParentCoords(coords), m_radius);}
00141
00142
00143
00144
00145
00146 Ball toLocalCoords(const Point<dim>& origin,
00147 const RotMatrix<dim>& rotation = RotMatrix<dim>().identity()) const
00148 {return Ball(m_center.toLocalCoords(origin, rotation), m_radius);}
00149 Ball toLocalCoords(const AxisBox<dim>& coords) const
00150 {return Ball(m_center.toLocalCoords(coords), m_radius);}
00151 Ball toLocalCoords(const RotBox<dim>& coords) const
00152 {return Ball(m_center.toLocalCoords(coords), m_radius);}
00153
00154
00155 Ball toParentCoords(const Point<dim>& origin, const Quaternion& rotation) const;
00156 Ball toLocalCoords(const Point<dim>& origin, const Quaternion& rotation) const;
00157
00158 friend bool Intersect<dim>(const Ball& b, const Point<dim>& p, bool proper);
00159 friend bool Contains<dim>(const Point<dim>& p, const Ball& b, bool proper);
00160
00161 friend bool Intersect<dim>(const Ball& b, const AxisBox<dim>& a, bool proper);
00162 friend bool Contains<dim>(const Ball& b, const AxisBox<dim>& a, bool proper);
00163 friend bool Contains<dim>(const AxisBox<dim>& a, const Ball& b, bool proper);
00164
00165 friend bool Intersect<dim>(const Ball& b1, const Ball& b2, bool proper);
00166 friend bool Contains<dim>(const Ball& outer, const Ball& inner, bool proper);
00167
00168 friend bool Intersect<dim>(const Segment<dim>& s, const Ball& b, bool proper);
00169 friend bool Contains<dim>(const Segment<dim>& s, const Ball& b, bool proper);
00170
00171 friend bool Intersect<dim>(const RotBox<dim>& r, const Ball& b, bool proper);
00172 friend bool Contains<dim>(const RotBox<dim>& r, const Ball& b, bool proper);
00173 friend bool Contains<dim>(const Ball& b, const RotBox<dim>& r, bool proper);
00174
00175 friend bool Intersect<dim>(const Polygon<dim>& p, const Ball& b, bool proper);
00176 friend bool Contains<dim>(const Polygon<dim>& p, const Ball& b, bool proper);
00177 friend bool Contains<dim>(const Ball& b, const Polygon<dim>& p, bool proper);
00178
00179 private:
00180
00181 Point<dim> m_center;
00182 CoordType m_radius;
00183 };
00184
00185 template<int dim>
00186 inline bool Ball<dim>::isEqualTo(const Ball<dim>& b, CoordType epsilon) const
00187 {
00188 return Equal(m_center, b.m_center, epsilon)
00189 && Equal(m_radius, b.m_radius, epsilon);
00190 }
00191
00192 }
00193
00194 #endif // WFMATH_BALL_H