library of assembled shared sources

http://lass.cocamware.com

sphere_3d.inl

Go to the documentation of this file.
00001 /** @file
00002  *  @author Bram de Greve (bramz@users.sourceforge.net)
00003  *  @author Tom De Muer (tomdemuer@users.sourceforge.net)
00004  *
00005  *  *** BEGIN LICENSE INFORMATION ***
00006  *  
00007  *  The contents of this file are subject to the Common Public Attribution License 
00008  *  Version 1.0 (the "License"); you may not use this file except in compliance with 
00009  *  the License. You may obtain a copy of the License at 
00010  *  http://lass.sourceforge.net/cpal-license. The License is based on the 
00011  *  Mozilla Public License Version 1.1 but Sections 14 and 15 have been added to cover 
00012  *  use of software over a computer network and provide for limited attribution for 
00013  *  the Original Developer. In addition, Exhibit A has been modified to be consistent 
00014  *  with Exhibit B.
00015  *  
00016  *  Software distributed under the License is distributed on an "AS IS" basis, WITHOUT 
00017  *  WARRANTY OF ANY KIND, either express or implied. See the License for the specific 
00018  *  language governing rights and limitations under the License.
00019  *  
00020  *  The Original Code is LASS - Library of Assembled Shared Sources.
00021  *  
00022  *  The Initial Developer of the Original Code is Bram de Greve and Tom De Muer.
00023  *  The Original Developer is the Initial Developer.
00024  *  
00025  *  All portions of the code written by the Initial Developer are:
00026  *  Copyright (C) 2004-2007 the Initial Developer.
00027  *  All Rights Reserved.
00028  *  
00029  *  Contributor(s):
00030  *
00031  *  Alternatively, the contents of this file may be used under the terms of the 
00032  *  GNU General Public License Version 2 or later (the GPL), in which case the 
00033  *  provisions of GPL are applicable instead of those above.  If you wish to allow use
00034  *  of your version of this file only under the terms of the GPL and not to allow 
00035  *  others to use your version of this file under the CPAL, indicate your decision by 
00036  *  deleting the provisions above and replace them with the notice and other 
00037  *  provisions required by the GPL License. If you do not delete the provisions above,
00038  *  a recipient may use your version of this file under either the CPAL or the GPL.
00039  *  
00040  *  *** END LICENSE INFORMATION ***
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 // --- public --------------------------------------------------------------------------------------
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 /** return area of surface of sphere
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 /** return volume of sphere
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 /** Classify a point and tell and what side of the sphere surface it is.
00137  *  @return sInside, sSurface, sOutside
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  * (P - C)² - r²
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 /** return signed distance of point to surface of sphere.
00162  *  negative distance means point is inside the sphere.
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 /** return squared distance of point to surface of sphere.
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 /** returns if point is on inside or on surface
00186  *  @return <tt>classify(iPoint) != sOutside</tt> but may be faster
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 /** Classify a point and tell and what side of the sphere surface it is.
00198  *  @return sInside, sSurface, sOutside
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  * (P - C)² - r²
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 /** return signed distance of point to surface of sphere.
00225  *  negative distance means point is inside the sphere.
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 /** return squared distance of point to surface of sphere.
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 /** returns if point is on inside or on surface
00249  *  @return <tt>classify(iPoint, iRelativeTolerance) != sOutside</tt> but may be faster
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 /** return true if sphere has a non-negative radius
00261  */
00262 template <typename T>
00263 const bool Sphere3D<T>::isValid() const
00264 {
00265     return radius_ >= TNumTraits::zero;
00266 }
00267 
00268 
00269 
00270 // --- protected -----------------------------------------------------------------------------------
00271 
00272 
00273 
00274 // --- private -------------------------------------------------------------------------------------
00275 
00276 
00277 
00278 // --- free ----------------------------------------------------------------------------------------
00279 
00280 /** @relates lass::prim::Sphere3D
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 /** @relates lass::prim::Sphere3D
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 /** @relates lass::prim::Sphere3D
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 /** @relates lass::prim::Sphere3D
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 // --- END OF FILE ------------------------------------------------------------------------------

Generated on Mon Nov 10 14:21:39 2008 for Library of Assembled Shared Sources by doxygen 1.5.7.1
SourceForge.net Logo