IsoEx
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Modules Pages
ImplicitSphere.hh
1 /*===========================================================================*\
2  * *
3  * IsoEx *
4  * Copyright (C) 2002 by Computer Graphics Group, RWTH Aachen *
5  * www.rwth-graphics.de *
6  * *
7  *---------------------------------------------------------------------------*
8  * *
9  * License *
10  * *
11  * This library is free software; you can redistribute it and/or modify it *
12  * under the terms of the GNU Library General Public License as published *
13  * by the Free Software Foundation, version 2. *
14  * *
15  * This library is distributed in the hope that it will be useful, but *
16  * WITHOUT ANY WARRANTY; without even the implied warranty of *
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
18  * Library General Public License for more details. *
19  * *
20  * You should have received a copy of the GNU Library General Public *
21  * License along with this library; if not, write to the Free Software *
22  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. *
23  * *
24 \*===========================================================================*/
25 
26 //=============================================================================
27 //
28 // CLASS ImplicitSphere
29 //
30 //=============================================================================
31 
32 
33 #ifndef ISOEX_IMPLICITSPHERE_HH
34 #define ISOEX_IMPLICITSPHERE_HH
35 
36 
37 //== INCLUDES =================================================================
38 
39 #include "Implicit.hh"
40 #include <IsoEx/Config/IsoExDefines.hh>
41 
42 //== NAMESPACES ===============================================================
43 
44 namespace IsoEx {
45 
46 //== CLASS DEFINITION =========================================================
47 
48 
55 template< class Vec3 >
56 class ISOEXDLLEXPORT ImplicitSphere : public Implicit<Vec3>
57 {
58 public:
59 
60  typedef typename Vec3::value_type real;
61 
63 
64 
66  ImplicitSphere(const Vec3& _center, real _radius)
67  : center_(_center),
68  radius_(_radius),
69  sqr_radius_(_radius*_radius)
70  {}
71 
74 
76 
77 
78 
80 
81 
82  bool is_inside(const Vec3& _point) const
83  {
84  return (center_ - _point).sqrnorm() <= sqr_radius_;
85  }
86 
87  real scalar_distance(const Vec3& _point) const
88  {
89  return (center_ - _point).norm() - radius_;
90  }
91 
92  bool directed_distance(const Vec3& _p0,
93  const Vec3& _p1,
94  Vec3& _point,
95  Vec3& _normal,
96  real& _distance) const
97  {
98  Vec3 orig(_p0), dir(_p1-_p0);
99 
100  double a = dir.sqrnorm();
101  double b = 2.0*(dir | (orig - center_));
102  double c = (orig - center_).sqrnorm() - radius_*radius_;
103  double d = b*b - 4.0*a*c;
104 
105  if (d >= 0)
106  {
107  d = sqrt(d);
108 
109  double t1 = (-b-d) / (2.0*a);
110  double t2 = (-b+d) / (2.0*a);
111  double t = 1.00001;
112  if (t1 >= 0.0 && t1 < t) t = t1;
113  if (t2 >= 0.0 && t2 < t) t = t2;
114 
115  if (t != 1.00001)
116  {
117  _point = orig + dir*t;
118  _normal = (_point - center_) / radius_;
119  _distance = ((dir | _normal) < 0.0) ? dir.norm()*t : -dir.norm()*t;
120  return true;
121  }
122  }
123 
124  return false;
125  }
126 
128 
129 
130 private:
131 
132  Vec3 center_;
133  real radius_;
134  real sqr_radius_;
135 };
136 
137 
138 //=============================================================================
139 } // namespace IsoEx
140 //=============================================================================
141 #endif // ISOEX_IMPLICITSPHERE_HH defined
142 //=============================================================================
143 
bool directed_distance(const Vec3 &_p0, const Vec3 &_p1, Vec3 &_point, Vec3 &_normal, real &_distance) const
Definition: ImplicitSphere.hh:92
ImplicitSphere(const Vec3 &_center, real _radius)
Constructor: given sphere center and radius.
Definition: ImplicitSphere.hh:66
bool is_inside(const Vec3 &_point) const
Definition: ImplicitSphere.hh:82
Definition: ImplicitSphere.hh:56
~ImplicitSphere()
Empty destructor.
Definition: ImplicitSphere.hh:73
real scalar_distance(const Vec3 &_point) const
Definition: ImplicitSphere.hh:87
Definition: Implicit.hh:57
A type for volume images, or 3D textures.