Main Page | Modules | Namespace List | Class Hierarchy | Class List | File List | Namespace Members | Class Members

ImplicitSphere.hh

00001 /*===========================================================================*\
00002  *                                                                           *
00003  *                                IsoEx                                      *
00004  *        Copyright (C) 2002 by Computer Graphics Group, RWTH Aachen         *
00005  *                         www.rwth-graphics.de                              *
00006  *                                                                           *
00007  *---------------------------------------------------------------------------* 
00008  *                                                                           *
00009  *                                License                                    *
00010  *                                                                           *
00011  *  This library is free software; you can redistribute it and/or modify it  *
00012  *  under the terms of the GNU Library General Public License as published   *
00013  *  by the Free Software Foundation, version 2.                              *
00014  *                                                                           *
00015  *  This library is distributed in the hope that it will be useful, but      *
00016  *  WITHOUT ANY WARRANTY; without even the implied warranty of               *
00017  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU        *
00018  *  Library General Public License for more details.                         *
00019  *                                                                           *
00020  *  You should have received a copy of the GNU Library General Public        *
00021  *  License along with this library; if not, write to the Free Software      *
00022  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.                *
00023  *                                                                           *
00024 \*===========================================================================*/
00025 
00026 //=============================================================================
00027 //
00028 //  CLASS ImplicitSphere
00029 //
00030 //=============================================================================
00031 
00032 
00033 #ifndef ISOEX_IMPLICITSPHERE_HH
00034 #define ISOEX_IMPLICITSPHERE_HH
00035 
00036 
00037 //== INCLUDES =================================================================
00038 
00039 #include <IsoEx/Implicits/Implicit.hh>
00040 
00041 //== NAMESPACES ===============================================================
00042 
00043 namespace IsoEx {
00044 
00045 //== CLASS DEFINITION =========================================================
00046 
00047               
00054 class ImplicitSphere : public Implicit
00055 {
00056 public:
00057    
00059 
00060 
00062   ImplicitSphere(const OpenMesh::Vec3f& _center, float _radius)
00063     : center_(_center), 
00064       radius_(_radius), 
00065       sqr_radius_(_radius*_radius)
00066   {}
00067 
00069   ~ImplicitSphere() {}
00070 
00072 
00073 
00074 
00076 
00077 
00078   bool is_inside(const OpenMesh::Vec3f& _point) const 
00079   {
00080     return (center_ - _point).sqrnorm() <= sqr_radius_;
00081   }
00082 
00083   float scalar_distance(const OpenMesh::Vec3f& _point) const 
00084   {
00085     return (center_ - _point).norm() - radius_;
00086   }
00087 
00088   bool directed_distance(const OpenMesh::Vec3f&  _p0,
00089                          const OpenMesh::Vec3f&  _p1,
00090                          OpenMesh::Vec3f&        _point,
00091                          OpenMesh::Vec3f&        _normal,
00092                          float&                  _distance) const
00093   {
00094     OpenMesh::Vec3f orig(_p0), dir(_p1-_p0);
00095 
00096     double a = dir.sqrnorm();
00097     double b = 2.0*(dir | (orig - center_));
00098     double c = (orig - center_).sqrnorm() - radius_*radius_;
00099     double d = b*b - 4.0*a*c;
00100 
00101     if (d >= 0)
00102     {
00103       d = sqrt(d);
00104 
00105       double t1 = (-b-d) / (2.0*a);
00106       double t2 = (-b+d) / (2.0*a);
00107       double t  = 1.00001;
00108       if (t1 >= 0.0 && t1 < t) t = t1;
00109       if (t2 >= 0.0 && t2 < t) t = t2;
00110 
00111       if (t != 1.00001)
00112       {
00113         _point    = orig + dir*t;
00114         _normal   = (_point - center_) / radius_;
00115         _distance = ((dir | _normal) < 0.0) ? dir.norm()*t : -dir.norm()*t;
00116         return true;
00117       }
00118     }
00119 
00120     return false;
00121   }
00122   
00124 
00125 
00126 private:
00127 
00128   OpenMesh::Vec3f  center_;
00129   float            radius_;
00130   float            sqr_radius_;
00131 };
00132 
00133 
00134 //=============================================================================
00135 } // namespace IsoEx
00136 //=============================================================================
00137 #endif // ISOEX_IMPLICITSPHERE_HH defined
00138 //=============================================================================
00139 

Generated on Mon Jul 5 18:07:08 2004 for IsoEx by doxygen 1.3.6-20040222