00001
00002
00003
00004
00005
00006
00007 #ifndef BALL_MATHS_SPHERE3_H
00008 #define BALL_MATHS_SPHERE3_H
00009
00010 #ifdef BALL_HAS_IEEEFP_H
00011 # include <ieeefp.h>
00012 #endif
00013
00014 #include <iostream>
00015
00016 #ifndef BALL_MATHS_PLANE3_H
00017 # include <BALL/MATHS/plane3.h>
00018 #endif
00019
00020 #ifndef BALL_MATHS_VECTOR3_H
00021 # include <BALL/MATHS/vector3.h>
00022 #endif
00023
00024 namespace BALL
00025 {
00030
00033 template <typename T>
00034 class TSphere3
00035 {
00036 public:
00037
00038 BALL_CREATE(TSphere3)
00039
00040
00043
00048 TSphere3()
00049
00050 : p(),
00051 radius(0)
00052 {
00053 }
00054
00059 TSphere3(const TSphere3& sphere)
00060
00061 : p(sphere.p),
00062 radius(sphere.radius)
00063 {
00064 }
00065
00071 TSphere3(const TVector3<T>& point, const T& radius)
00072
00073 : p(point),
00074 radius(radius)
00075 {
00076 }
00077
00081 virtual ~TSphere3()
00082
00083 {
00084 }
00085
00088 virtual void clear()
00089
00090 {
00091 p.clear();
00092 radius = (T) 0;
00093 }
00095
00099
00103 void swap(TSphere3& sphere)
00104
00105 {
00106 TVector3<T> temp_point(p);
00107 p = sphere.p;
00108 sphere.p = temp_point;
00109
00110 T temp = radius;
00111 radius = sphere.radius;
00112 sphere.radius = temp;
00113 }
00114
00118 void set(const TSphere3& sphere)
00119
00120 {
00121 p = sphere.p;
00122 radius = sphere.radius;
00123 }
00124
00130 void set(const TVector3<T>& point, const T& r)
00131
00132 {
00133 p = point;
00134 radius = r;
00135 }
00136
00141 TSphere3& operator = (const TSphere3& sphere)
00142
00143 {
00144 p = sphere.p;
00145 radius = sphere.radius;
00146 return *this;
00147 }
00148
00153 void get(TSphere3& sphere) const
00154
00155 {
00156 sphere.p = p;
00157 sphere.radius = radius;
00158 }
00159
00164 void get(TVector3<T>& point, T& r) const
00165
00166 {
00167 point = p;
00168 r = radius;
00169 }
00170
00172
00175
00179 bool operator == (const TSphere3& sphere) const
00180
00181 {
00182 return (p == sphere.p && Maths::isEqual(radius, sphere.radius));
00183 }
00184
00188 bool operator != (const TSphere3& sphere) const
00189
00190 {
00191 return (p != sphere.p || Maths::isNotEqual(radius, sphere.radius));
00192 }
00193
00200 bool has(const TVector3<T>& point, bool on_surface = false) const
00201
00202 {
00203 if (on_surface)
00204 {
00205 return Maths::isEqual(p.getDistance(point), radius);
00206 }
00207 else
00208 {
00209 return Maths::isLessOrEqual(p.getDistance(point), radius);
00210 }
00211 }
00212
00216 bool isEmpty() const
00217
00218 {
00219 return Maths::isZero(radius);
00220 }
00221
00223
00226
00231 bool isValid() const
00232
00233 {
00234 return true;
00235 }
00236
00243 void dump(std::ostream& s = std::cout, Size depth = 0) const
00244
00245 {
00246 BALL_DUMP_STREAM_PREFIX(s);
00247
00248 BALL_DUMP_HEADER(s, this, this);
00249
00250 BALL_DUMP_DEPTH(s, depth);
00251 s << " position: " << p << std::endl;
00252
00253 BALL_DUMP_DEPTH(s, depth);
00254 s << " radius: " << radius << std::endl;
00255
00256 BALL_DUMP_STREAM_SUFFIX(s);
00257 }
00258
00260
00263
00266 TVector3<T> p;
00267
00270 T radius;
00271
00273 };
00275
00282
00286 template <typename T>
00287 std::istream& operator >> (std::istream& s, TSphere3<T>& sphere)
00288
00289 {
00290 char c;
00291 s >> c >> sphere.p >> sphere.radius >> c;
00292 return s;
00293 }
00294
00301 template <typename T>
00302 std::ostream& operator << (std::ostream& s, const TSphere3<T>& sphere)
00303
00304 {
00305 s << '(' << sphere.p << ' ' << sphere.radius << ')';
00306 return s;
00307 }
00309
00315 typedef TSphere3<float> Sphere3;
00316
00317 }
00318
00319 #endif // BALL_MATHS_SPHERE3_H