core/vgl/vgl_ray_3d.h
Go to the documentation of this file.
00001 // This is core/vgl/vgl_ray_3d.h
00002 #ifndef vgl_ray_3d_h_
00003 #define vgl_ray_3d_h_
00004 #ifdef VCL_NEEDS_PRAGMA_INTERFACE
00005 #pragma interface
00006 #endif
00007 //:
00008 // \file
00009 // \brief A 3-d ray defined by an origin and a direction vector
00010 // \author  J.L. Mundy
00011 //
00012 // \verbatim
00013 // Modifications
00014 // Initial version Sept. 17,  2010
00015 // \endverbatim
00016 
00017 #include <vcl_iosfwd.h>
00018 #include <vgl/vgl_vector_3d.h>
00019 #include <vgl/vgl_point_3d.h>
00020 #include <vgl/vgl_line_segment_3d.h>
00021 #include <vgl/vgl_line_3d_2_points.h>
00022 
00023 //: Represents a 3-d ray
00024 //  The ray origin is p0 and the direction is t_.
00025 
00026 template <class Type>
00027 class vgl_ray_3d
00028 {
00029   vgl_point_3d<Type> p0_; //!< The ray origin
00030   vgl_vector_3d<Type> t_;  //!< ray direction vector
00031  public:
00032   //: Default constructor - does not initialise!
00033   inline vgl_ray_3d() {}
00034 
00035   //: Copy constructor
00036   inline vgl_ray_3d(vgl_ray_3d<Type> const& l)
00037     : p0_(l.origin()), t_(l.direction()) {}
00038 
00039   //: Construct from orign and direction
00040   inline vgl_ray_3d(vgl_point_3d<Type> const& p0,
00041                     vgl_vector_3d<Type> const& direction)
00042     : p0_(p0), t_(direction) {t_ = t_/static_cast<Type>(t_.length());}
00043 
00044   //: Construct from two points
00045   inline vgl_ray_3d(vgl_point_3d<Type> const& origin,
00046                     vgl_point_3d<Type> const& p)
00047     : p0_(origin), t_(p-origin) {t_ = t_/static_cast<Type>(t_.length());}
00048   //: Construct from a line segment, direction from 1 to 2
00049   inline vgl_ray_3d(vgl_line_segment_3d<Type> const& ls)
00050   {
00051     p0_ = ls.point1(); t_ = ls.point2()-p0_;
00052     t_ = t_/static_cast<Type>(t_.length());
00053   }
00054 
00055   //: Construct from a line 2 points direction from 1 to 2
00056   inline vgl_ray_3d(vgl_line_3d_2_points<Type> const& ls)
00057   {
00058     p0_ = ls.point1(); t_ = ls.point2()-p0_; 
00059     t_ = t_/static_cast<Type>(t_.length());
00060   }
00061 
00062   //: Destructor
00063   inline ~vgl_ray_3d() {}
00064 
00065   //: Accessors
00066   inline vgl_point_3d<Type> origin() const { return p0_; } // return a copy
00067 
00068   inline vgl_vector_3d<Type> direction() const
00069   { return t_/static_cast<Type>(t_.length()); } // return a copy
00070 
00071   //: The comparison operator
00072   inline bool operator==(vgl_ray_3d<Type> const& r) const
00073   { return (this==&r)||(direction()==r.direction() && origin()==r.origin());}
00074 
00075   inline bool operator!=(vgl_ray_3d<Type>const& other) const
00076   { return !operator==(other); }
00077 
00078   //: Assignment
00079   inline void set(vgl_point_3d<Type> const& p0, vgl_vector_3d<Type> const& direction)
00080   { 
00081     p0_ = p0; t_ = direction;
00082     t_=t_/static_cast<Type>(t_.length());
00083   }
00084 
00085   //: Check if point \a p is on the ray and lies in the positive ray direction
00086   bool contains(const vgl_point_3d<Type>& p ) const;
00087 
00088 };
00089 
00090 //: Write to stream
00091 // \relatesalso vgl_ray_3d
00092 template <class Type>
00093 vcl_ostream&  operator<<(vcl_ostream& s, const vgl_ray_3d<Type>& p);
00094 
00095 //: Read from stream
00096 // \relatesalso vgl_ray_3d
00097 template <class Type>
00098 vcl_istream&  operator>>(vcl_istream& is,  vgl_ray_3d<Type>& p);
00099 //: public functions
00100 template <class Type>
00101 //: angle between rays
00102 double angle(vgl_ray_3d<Type> const& r0, vgl_ray_3d<Type> const& r1)
00103 {
00104   return angle(r0.direction(), r1.direction());
00105 } 
00106 #define VGL_RAY_3D_INSTANTIATE(T) extern "please include vgl/vgl_ray_3d.txx first"
00107 
00108 #endif // vgl_ray_3d_h_