sphere_3d.inl
Go to the documentation of this file.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
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045 #ifndef LASS_GUARDIAN_OF_INCLUSION_PRIM_SPHERE3D_INL
00046 #define LASS_GUARDIAN_OF_INCLUSION_PRIM_SPHERE3D_INL
00047
00048
00049 #include "sphere_3d.h"
00050 #include "../num/floating_point_comparison.h"
00051
00052 namespace lass
00053 {
00054 namespace prim
00055 {
00056
00057
00058
00059 template <typename T>
00060 Sphere3D<T>::Sphere3D():
00061 center_(),
00062 radius_(TNumTraits::zero)
00063 {
00064 LASS_ASSERT(center_.isZero());
00065 }
00066
00067
00068
00069 template <typename T>
00070 Sphere3D<T>::Sphere3D(const TPoint& iCenter, TParam iRadius):
00071 center_(iCenter),
00072 radius_(iRadius)
00073 {
00074 }
00075
00076
00077
00078 template <typename T>
00079 const typename Sphere3D<T>::TPoint&
00080 Sphere3D<T>::center() const
00081 {
00082 return center_;
00083 }
00084
00085
00086
00087 template <typename T>
00088 typename Sphere3D<T>::TPoint&
00089 Sphere3D<T>::center()
00090 {
00091 return center_;
00092 }
00093
00094
00095
00096 template<typename T>
00097 typename Sphere3D<T>::TConstReference
00098 Sphere3D<T>::radius() const
00099 {
00100 return radius_;
00101 }
00102
00103
00104
00105 template<typename T>
00106 typename Sphere3D<T>::TReference
00107 Sphere3D<T>::radius()
00108 {
00109 return radius_;
00110 }
00111
00112
00113
00114
00115
00116 template<typename T>
00117 const typename Sphere3D<T>::TValue
00118 Sphere3D<T>::area() const
00119 {
00120 return 4 * TNumTraits::pi * num::sqr(radius_);
00121 }
00122
00123
00124
00125
00126
00127 template<typename T>
00128 const typename Sphere3D<T>::TValue
00129 Sphere3D<T>::volume() const
00130 {
00131 return (4 * TNumTraits::pi * num::sqr(radius_) * radius_) / 3;
00132 }
00133
00134
00135
00136
00137
00138
00139 template<typename T>
00140 const Side Sphere3D<T>::classify(const TPoint& iPoint) const
00141 {
00142 const TValue eq = equation(iPoint);
00143 return eq > TNumTraits::zero ? sOutside : (eq < TNumTraits::zero ? sInside : sSurface);
00144 }
00145
00146
00147
00148
00149
00150
00151 template<typename T>
00152 const typename Sphere3D<T>::TValue
00153 Sphere3D<T>::equation(const TPoint& iPoint) const
00154 {
00155 const TVector difference = iPoint - center_;
00156 return difference.squaredNorm() - num::sqr(radius_);
00157 }
00158
00159
00160
00161
00162
00163
00164 template<typename T>
00165 const typename Sphere3D<T>::TValue
00166 Sphere3D<T>::signedDistance(const TPoint& iPoint) const
00167 {
00168 const TValue eq = equation(iPoint);
00169 return eq >= TNumTraits::zero ? num::sqrt(eq) : -num::sqrt(-eq);
00170 }
00171
00172
00173
00174
00175
00176 template<typename T>
00177 const typename Sphere3D<T>::TValue
00178 Sphere3D<T>::squaredDistance(const TPoint& iPoint) const
00179 {
00180 return num::abs(equation(iPoint));
00181 }
00182
00183
00184
00185
00186
00187
00188 template<typename T>
00189 const bool Sphere3D<T>::contains(const TPoint& iPoint) const
00190 {
00191 const TValue eq = equation(iPoint);
00192 return eq <= TNumTraits::zero;
00193 }
00194
00195
00196
00197
00198
00199
00200 template<typename T>
00201 const Side Sphere3D<T>::classify(const TPoint& iPoint, TParam iRelativeTolerance) const
00202 {
00203 const TValue eq = equation(iPoint, iRelativeTolerance);
00204 return eq > TNumTraits::zero ? sOutside : (eq < TNumTraits::zero ? sInside : sSurface);
00205 }
00206
00207
00208
00209
00210
00211
00212 template<typename T>
00213 const typename Sphere3D<T>::TValue
00214 Sphere3D<T>::equation(const TPoint& iPoint, TParam iRelativeTolerance) const
00215 {
00216 const TVector difference = iPoint - center_;
00217 const TValue d2 = difference.squaredNorm();
00218 const TValue r2 = num::sqr(radius_);
00219 return num::almostEqual(d2, r2, iRelativeTolerance) ? TNumTraits::zero : (d2 - r2);
00220 }
00221
00222
00223
00224
00225
00226
00227 template<typename T>
00228 const typename Sphere3D<T>::TValue
00229 Sphere3D<T>::signedDistance(const TPoint& iPoint, TParam iRelativeTolerance) const
00230 {
00231 const TValue eq = equation(iPoint, iRelativeTolerance);
00232 return eq >= TNumTraits::zero ? num::sqrt(eq) : -num::sqrt(-eq);
00233 }
00234
00235
00236
00237
00238
00239 template<typename T>
00240 const typename Sphere3D<T>::TValue
00241 Sphere3D<T>::squaredDistance(const TPoint& iPoint, TParam iRelativeTolerance) const
00242 {
00243 return num::abs(equation(iPoint, iRelativeTolerance));
00244 }
00245
00246
00247
00248
00249
00250
00251 template<typename T>
00252 const bool Sphere3D<T>::contains(const TPoint& iPoint, TParam iRelativeTolerance) const
00253 {
00254 const TValue eq = equation(iPoint, iRelativeTolerance);
00255 return eq <= TNumTraits::zero;
00256 }
00257
00258
00259
00260
00261
00262 template <typename T>
00263 const bool Sphere3D<T>::isValid() const
00264 {
00265 return radius_ >= TNumTraits::zero;
00266 }
00267
00268
00269
00270
00271
00272
00273
00274
00275
00276
00277
00278
00279
00280
00281
00282 template <typename T>
00283 const T squaredDistance(const Sphere3D<T>& sphere, const Point3D<T>& point)
00284 {
00285 return sphere.squaredDistance(point);
00286 }
00287
00288
00289
00290
00291
00292 template <typename T>
00293 const T distance(const Sphere3D<T>& sphere, const Point3D<T>& point)
00294 {
00295 return num::sqrt(sphere.squaredDistance(point));
00296 }
00297
00298
00299
00300
00301
00302 template<typename T>
00303 std::ostream& operator<<(std::ostream& ioOStream, const Sphere3D<T>& iSphere)
00304 {
00305 LASS_ENFORCE(ioOStream) << "{S=" << iSphere.center() << ", r=" << iSphere.radius() << "}";
00306 return ioOStream;
00307 }
00308
00309
00310
00311
00312
00313 template<typename T>
00314 io::XmlOStream& operator<<(io::XmlOStream& ioOStream, const Sphere3D<T>& iSphere)
00315 {
00316 LASS_ENFORCE_STREAM(ioOStream)
00317 << "<Sphere3D>\n"
00318 << "<center>" << iSphere.center() << "</center>\n"
00319 << "<radius>" << iSphere.radius() << "</radius>\n"
00320 << "</Sphere3D>\n";
00321
00322 return ioOStream;
00323 }
00324
00325
00326
00327 }
00328
00329 }
00330
00331 #endif
00332
00333