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 <vcl_iosfwd.h>
00013 #include <vgl/vgl_fwd.h> // forward declare vgl_line_3d_2_points
00014 #include <vgl/vgl_point_3d.h>
00015 
00016 //: Represents a cartesian 3D point
00017 template <class Type>
00018 class vgl_sphere_3d
00019 {
00020   vgl_point_3d<Type> c_; //!< centre
00021   Type r_;               //!< radius
00022  public:
00023 
00024   // Constructors/Initializers/Destructor------------------------------------
00025 
00026   //: Default constructor
00027    inline vgl_sphere_3d (): c_(0.0, 0.0, 0.0), r_(-1) {}
00028 
00029   //: Construct from four scalars: centre and radius.
00030   inline vgl_sphere_3d(Type px, Type py, Type pz, Type rad) : c_(px, py, pz), r_(rad) {}
00031 
00032   //: Construct from a 4-array, representing centre and radius.
00033   inline vgl_sphere_3d (const Type v[4]): c_(v[0], v[1], v[2]), r_(v[3]) {}
00034 
00035   //: Construct from centre point and radius.
00036   vgl_sphere_3d (vgl_point_3d<Type> const& cntr, Type rad): c_(cntr), r_(rad) {}
00037 
00038   //: Test for equality
00039   inline bool operator==(const vgl_sphere_3d<Type> &s) const { return this==&s || (c_==s.c_ && r_==s.r_); }
00040   //: Test for inequality
00041   inline bool operator!=(vgl_sphere_3d<Type>const& s) const { return !operator==(s); }
00042 
00043   // Data Access-------------------------------------------------------------
00044 
00045   inline const vgl_point_3d<Type> & centre() const {return c_;}
00046   inline Type radius() const {return r_;}
00047 
00048   //: Return true if this sphere is empty
00049   inline bool is_empty() const {
00050     return r_ < 0.0;
00051   }
00052 
00053   //: Return true iff the point p is inside (or on) this sphere
00054   bool contains(vgl_point_3d<Type> const& p) const;
00055 
00056   //: Make the sphere empty.
00057   void set_empty() {c_.set(0,0,0); r_=-1;}
00058 
00059   //: Set radius \a r of this sphere (while centre unchanged)
00060   inline void set_radius(Type r) { r_=r; }
00061   //: Set centre of this sphere to \a c (while radius unchanged)
00062   inline void set_centre(const vgl_point_3d<Type> & c) { c_=c; }
00063 
00064   //: Calculate the end points of a line clipped by this sphere.
00065   bool clip(const vgl_line_3d_2_points<Type> & line,
00066             vgl_point_3d<Type> &p1, vgl_point_3d<Type> &p2) const;
00067   
00068   //: Writes "<vgl_sphere_3d centre=vgl_point_3d<x,y,z> radius=r)>" to stream
00069   vcl_ostream& print(vcl_ostream& os) const;
00070 
00071   //: Read from stream, possibly with formatting.
00072   //  Either just reads 4 blank-separated numbers,
00073   //  or reads 4 comma-separated numbers,
00074   //  or reads 4 numbers in parenthesized form "(123, 321, 567, 890)"
00075   vcl_istream& read(vcl_istream& is);
00076 };
00077 
00078 
00079 //: Writes "<vgl_sphere_3d centre=vgl_point_3d<x,y,z> radius=r)>" to stream
00080 template <class Type>
00081 vcl_ostream& operator<<(vcl_ostream& os, const vgl_sphere_3d<Type>& sph);
00082 
00083 //: Read from stream, possibly with formatting.
00084 //  Either just reads 4 blank-separated numbers,
00085 //  or reads 4 comma-separated numbers,
00086 //  or reads 4 numbers in parenthesized form "(123, 321, 567, 890)"
00087 template <class Type>
00088 vcl_istream& operator>>(vcl_istream& is, vgl_sphere_3d<Type>& sph);
00089 
00090 
00091 #define VGL_SPHERE_3D_INSTANTIATE(T) extern "please include vgl/vgl_sphere_3d.txx first"
00092 
00093 #endif // vgl_sphere_3d_h