core/vgl/vgl_sphere_3d.h
Go to the documentation of this file.
00001 // This is core/vgl/vgl_sphere_3d.h
00002 #ifndef vgl_sphere_3d_h
00003 #define vgl_sphere_3d_h
00004 #ifdef VCL_NEEDS_PRAGMA_INTERFACE
00005 #pragma interface
00006 #endif
00007 //:
00008 // \file
00009 // \brief a sphere in 3D nonhomogeneous space
00010 // \author Ian Scott
00011 
00012 #include <vgl/vgl_fwd.h> // forward declare vgl_line_3d_2_points
00013 #include <vgl/vgl_point_3d.h>
00014 
00015 //: Represents a cartesian 3D point
00016 template <class Type>
00017 class vgl_sphere_3d
00018 {
00019   vgl_point_3d<Type> c_; //!< centre
00020   Type r_;               //!< radius
00021  public:
00022 
00023   // Constructors/Initializers/Destructor------------------------------------
00024 
00025   //: Default constructor
00026    inline vgl_sphere_3d (): c_(0.0, 0.0, 0.0), r_(-1) {}
00027 
00028   //: Construct from four scalars: centre and radius.
00029   inline vgl_sphere_3d(Type px, Type py, Type pz, Type rad) : c_(px, py, pz), r_(rad) {}
00030 
00031   //: Construct from a 4-array, representing centre and radius.
00032   inline vgl_sphere_3d (const Type v[4]): c_(v[0], v[1], v[2]), r_(v[3]) {}
00033 
00034   //: Construct from centre point and radius.
00035   vgl_sphere_3d (vgl_point_3d<Type> const& cntr, Type rad): c_(cntr), r_(rad) {}
00036 
00037   //: Test for equality
00038   inline bool operator==(const vgl_sphere_3d<Type> &s) const { return this==&s || (c_==s.c_ && r_==s.r_); }
00039   //: Test for inequality
00040   inline bool operator!=(vgl_sphere_3d<Type>const& s) const { return !operator==(s); }
00041 
00042   // Data Access-------------------------------------------------------------
00043 
00044   inline const vgl_point_3d<Type> & centre() const {return c_;}
00045   inline Type radius() const {return r_;}
00046 
00047   //: Return true if this sphere is empty
00048   inline bool is_empty() const {
00049     return r_ < 0.0;
00050   }
00051 
00052   //: Return true iff the point p is inside (or on) this sphere
00053   bool contains(vgl_point_3d<Type> const& p) const;
00054 
00055   //: Make the sphere empty.
00056   void set_empty() {c_.set(0,0,0); r_=-1;}
00057 
00058   //: Set min \a x ordinate of box (other sides unchanged)
00059   inline void set_radius(Type r) { r_=r; }
00060   //: Set min \a y ordinate of box (other sides unchanged)
00061   inline void set_centre(const vgl_point_3d<Type> & c) { c_=c; }
00062 
00063   //: Calculate the end points of a line clipped by this sphere.
00064   bool clip(const vgl_line_3d_2_points<Type> & line,
00065             vgl_point_3d<Type> &p1, vgl_point_3d<Type> &p2) const;
00066 };
00067 
00068 #define VGL_SPHERE_3D_INSTANTIATE(T) extern "please include vgl/vgl_sphere_3d.txx first"
00069 
00070 #endif // vgl_sphere_3d_h