BALL
1.4.1
|
00001 // -*- Mode: C++; tab-width: 2; -*- 00002 // vi: set ts=2: 00003 // 00004 00005 #ifndef BALL_MATHS_SPHERE3_H 00006 #define BALL_MATHS_SPHERE3_H 00007 00008 #ifdef BALL_HAS_IEEEFP_H 00009 # include <ieeefp.h> 00010 #endif 00011 00012 #include <iostream> 00013 00014 #ifndef BALL_MATHS_PLANE3_H 00015 # include <BALL/MATHS/plane3.h> 00016 #endif 00017 00018 #ifndef BALL_MATHS_VECTOR3_H 00019 # include <BALL/MATHS/vector3.h> 00020 #endif 00021 00022 namespace BALL 00023 { 00028 00031 template <typename T> 00032 class TSphere3 00033 { 00034 public: 00035 00036 BALL_CREATE(TSphere3) 00037 00038 00041 00046 TSphere3() 00047 : p(), 00048 radius(0) 00049 { 00050 } 00051 00056 TSphere3(const TSphere3& sphere) 00057 : p(sphere.p), 00058 radius(sphere.radius) 00059 { 00060 } 00061 00067 TSphere3(const TVector3<T>& point, const T& radius) 00068 : p(point), 00069 radius(radius) 00070 { 00071 } 00072 00076 virtual ~TSphere3() 00077 { 00078 } 00079 00082 virtual void clear() 00083 { 00084 p.clear(); 00085 radius = (T) 0; 00086 } 00088 00092 00096 void swap(TSphere3& sphere) 00097 { 00098 TVector3<T> temp_point(p); 00099 p = sphere.p; 00100 sphere.p = temp_point; 00101 00102 T temp = radius; 00103 radius = sphere.radius; 00104 sphere.radius = temp; 00105 } 00106 00110 void set(const TSphere3& sphere) 00111 { 00112 p = sphere.p; 00113 radius = sphere.radius; 00114 } 00115 00121 void set(const TVector3<T>& point, const T& r) 00122 { 00123 p = point; 00124 radius = r; 00125 } 00126 00131 TSphere3& operator = (const TSphere3& sphere) 00132 { 00133 p = sphere.p; 00134 radius = sphere.radius; 00135 return *this; 00136 } 00137 00142 void get(TSphere3& sphere) const 00143 { 00144 sphere.p = p; 00145 sphere.radius = radius; 00146 } 00147 00152 void get(TVector3<T>& point, T& r) const 00153 { 00154 point = p; 00155 r = radius; 00156 } 00157 00159 00162 00166 bool operator == (const TSphere3& sphere) const 00167 { 00168 return (p == sphere.p && Maths::isEqual(radius, sphere.radius)); 00169 } 00170 00174 bool operator != (const TSphere3& sphere) const 00175 { 00176 return (p != sphere.p || Maths::isNotEqual(radius, sphere.radius)); 00177 } 00178 00185 bool has(const TVector3<T>& point, bool on_surface = false) const 00186 { 00187 if (on_surface) 00188 { 00189 return Maths::isEqual(p.getDistance(point), radius); 00190 } 00191 else 00192 { 00193 return Maths::isLessOrEqual(p.getDistance(point), radius); 00194 } 00195 } 00196 00200 bool isEmpty() const 00201 00202 { 00203 return Maths::isZero(radius); 00204 } 00205 00207 00210 00215 bool isValid() const 00216 00217 { 00218 return true; 00219 } 00220 00227 void dump(std::ostream& s = std::cout, Size depth = 0) const 00228 00229 { 00230 BALL_DUMP_STREAM_PREFIX(s); 00231 00232 BALL_DUMP_HEADER(s, this, this); 00233 00234 BALL_DUMP_DEPTH(s, depth); 00235 s << " position: " << p << std::endl; 00236 00237 BALL_DUMP_DEPTH(s, depth); 00238 s << " radius: " << radius << std::endl; 00239 00240 BALL_DUMP_STREAM_SUFFIX(s); 00241 } 00242 00244 00247 00250 TVector3<T> p; 00251 00254 T radius; 00255 00257 }; 00259 00266 00270 template <typename T> 00271 std::istream& operator >> (std::istream& s, TSphere3<T>& sphere) 00272 00273 { 00274 char c; 00275 s >> c >> sphere.p >> sphere.radius >> c; 00276 return s; 00277 } 00278 00285 template <typename T> 00286 std::ostream& operator << (std::ostream& s, const TSphere3<T>& sphere) 00287 00288 { 00289 s << '(' << sphere.p << ' ' << sphere.radius << ')'; 00290 return s; 00291 } 00293 00299 typedef TSphere3<float> Sphere3; 00300 00301 } // namespace BALL 00302 00303 #endif // BALL_MATHS_SPHERE3_H