core/vgl/vgl_sphere_3d.txx
Go to the documentation of this file.
00001 #ifndef vgl_sphere_3d_txx_
00002 #define vgl_sphere_3d_txx_
00003 #ifdef VCL_NEEDS_PRAGMA_INTERFACE
00004 #pragma implementation
00005 #endif
00006 //:
00007 // \file
00008 // \brief a sphere in 3D nonhomogeneous space
00009 // \author Ian Scott
00010 
00011 #include "vgl_sphere_3d.h"
00012 #include <vcl_cmath.h>
00013 #include <vgl/vgl_point_3d.h>
00014 #include <vgl/vgl_closest_point.h>
00015 #include <vgl/vgl_line_3d_2_points.h>
00016 
00017 
00018 //: Return true iff the point p is inside (or on) this sphere
00019 template <class T>
00020 bool vgl_sphere_3d<T>::contains(vgl_point_3d<T> const& p) const
00021 {
00022   return (p-c_).length() <= r_;
00023 }
00024 
00025 
00026 //: Calculate the end points of a line clipped by this sphere.
00027 // \return true if any of the line touches the sphere.
00028 template <class T>
00029 bool vgl_sphere_3d<T>::clip(const vgl_line_3d_2_points<T> & line,
00030                             vgl_point_3d<T> &p1, vgl_point_3d<T> &p2) const
00031 {
00032   vgl_point_3d<T> cp = vgl_closest_point(line, c_);
00033 
00034   T cp_len = (cp - c_).length();
00035   if (cp_len > r_) return false;
00036 
00037   T half_chord_len = vcl_sqrt(r_*r_ - cp_len*cp_len);
00038 
00039   vgl_vector_3d<T> linevec = line.direction();
00040   linevec *= half_chord_len / linevec.length();
00041 
00042   p1 = cp - linevec;
00043   p2 = cp + linevec;
00044 
00045   return true;
00046 }
00047 
00048 #undef VGL_SPHERE_3D_INSTANTIATE
00049 #define VGL_SPHERE_3D_INSTANTIATE(Type) \
00050 template class vgl_sphere_3d<Type >
00051 
00052 #endif // vgl_sphere_3d_txx_