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 #ifndef ISOEX_IMPLICITSPHERE_HH
00034 #define ISOEX_IMPLICITSPHERE_HH
00035
00036
00037
00038
00039 #include <IsoEx/Implicits/Implicit.hh>
00040
00041
00042
00043 namespace IsoEx {
00044
00045
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 }
00136
00137 #endif // ISOEX_IMPLICITSPHERE_HH defined
00138
00139