core/vgl/vgl_homg_line_3d_2_points.h
Go to the documentation of this file.
00001 // This is core/vgl/vgl_homg_line_3d_2_points.h
00002 #ifndef vgl_homg_line_3d_2_points_h_
00003 #define vgl_homg_line_3d_2_points_h_
00004 #ifdef VCL_NEEDS_PRAGMA_INTERFACE
00005 #pragma interface
00006 #endif
00007 //:
00008 // \file
00009 // \author Don HAMILTON Peter TU François BERTEL
00010 //
00011 // \verbatim
00012 //  Modifications
00013 //   Peter Vanroose -  4 July 2001 - constructors now use force_point2_infinite()
00014 //   Peter Vanroose - 27 June 2001 - Added operator==
00015 //   Peter Vanroose - 15 July 2002 - Added concurrent(), coplanar() and intersection()
00016 // \endverbatim
00017 
00018 #include <vcl_iosfwd.h>
00019 #include <vgl/vgl_homg_point_3d.h> // data member of this class
00020 
00021 //:Represents a homogeneous 3D line using two points
00022 // A class to hold a homogeneous representation of a 3D Line.  The line is
00023 // stored as a pair of homogeneous 3d points.
00024 template <class Type>
00025 class vgl_homg_line_3d_2_points
00026 {
00027   // Data Members------------------------------------------------------------
00028 
00029   //: Any finite point on the line
00030   mutable vgl_homg_point_3d<Type> point_finite_;
00031   //: the (unique) point at infinity
00032   mutable vgl_homg_point_3d<Type> point_infinite_;
00033 
00034  public:
00035   //+**************************************************************************
00036   // Initialization
00037   //+**************************************************************************
00038 
00039   //: Default constructor with (0,0,0,1) and (1,0,0,0), which is the line \a y=z=0
00040   inline vgl_homg_line_3d_2_points(void)
00041   : point_finite_(0,0,0,1), point_infinite_(1,0,0,0) {}
00042 
00043   //: Copy constructor
00044   inline vgl_homg_line_3d_2_points(const vgl_homg_line_3d_2_points<Type> &that)
00045   : point_finite_(that.point_finite_), point_infinite_(that.point_infinite_) {}
00046 
00047   //: Construct from two points
00048   inline vgl_homg_line_3d_2_points(vgl_homg_point_3d<Type> const& point_1,
00049                                    vgl_homg_point_3d<Type> const& point_2)
00050   : point_finite_(point_1), point_infinite_(point_2) {force_point2_infinite();}
00051 
00052 #if 0
00053   //: Destructor (does nothing)
00054   inline ~vgl_homg_line_3d_2_points() {}
00055 #endif
00056 
00057   //: comparison
00058   bool operator==(vgl_homg_line_3d_2_points<Type> const& l) const;
00059   inline bool operator!=(vgl_homg_line_3d_2_points<Type> const& l) const{return !operator==(l);}
00060 
00061   // Data access
00062 
00063   //: Finite point (Could be an ideal point, if the whole line is at infinity.)
00064   inline vgl_homg_point_3d<Type> point_finite() const {return point_finite_;}
00065   //: Infinite point: the intersection of the line with the plane at infinity
00066   inline vgl_homg_point_3d<Type> point_infinite()const{return point_infinite_;}
00067 
00068   //: The point at infinity defines the direction of the line
00069   inline vgl_vector_3d<Type> direction() const{
00070     vgl_vector_3d<Type> dir(point_infinite_.x(), point_infinite_.y(),
00071                             point_infinite_.z());
00072   return dir/static_cast<Type>(dir.length());}
00073   //: Assignment
00074   inline void set(vgl_homg_point_3d<Type> const& p1, vgl_homg_point_3d<Type> const& p2)
00075   { point_finite_ = p1; point_infinite_ = p2; force_point2_infinite(); }
00076 
00077   // Utility methods
00078 
00079   //: Return true iff line is at infinity
00080   inline bool ideal(Type tol = (Type)0) const { return point_finite_.ideal(tol); }
00081 
00082  protected:
00083   //: force the point point_infinite_ to infinity, without changing the line
00084   // This is called by the constructors
00085   void force_point2_infinite(void) const; // mutable const
00086 };
00087 
00088 #define l vgl_homg_line_3d_2_points<Type>
00089 
00090 //: Return true iff line is at infinity
00091 // \relatesalso vgl_homg_line_3d_2_points
00092 template <class Type>
00093 inline bool is_ideal(l const& line, Type tol=(Type)0)
00094 { return line.ideal(tol); }
00095 
00096 //: Does a line pass through a point, i.e., are the point and the line collinear?
00097 // \relatesalso vgl_homg_line_3d_2_points
00098 // \relatesalso vgl_homg_point_3d
00099 template <class Type>
00100 inline bool collinear(l const& l1, vgl_homg_point_3d<Type> const& p)
00101 { return collinear(l1.point_finite(),l1.point_infinite(),p); }
00102 
00103 //: Are two lines coplanar, i.e., do they intersect?
00104 // \relatesalso vgl_homg_line_3d_2_points
00105 template <class Type>
00106 inline bool coplanar(l const& l1, l const& l2)
00107 { return coplanar(l1.point_finite(),l1.point_infinite(),l2.point_finite(),l2.point_infinite()); }
00108 
00109 //: Are two lines concurrent, i.e., do they intersect?
00110 // \relatesalso vgl_homg_line_3d_2_points
00111 template <class Type>
00112 inline bool concurrent(l const& l1, l const& l2) { return coplanar(l1,l2); }
00113 
00114 //: Are two points coplanar with a line?
00115 // \relatesalso vgl_homg_line_3d_2_points
00116 // \relatesalso vgl_homg_point_3d
00117 template <class Type>
00118 inline bool coplanar(l const& l1, vgl_homg_point_3d<Type> const& p1, vgl_homg_point_3d<Type> const& p2)
00119 { return coplanar(l1.point_finite(),l1.point_infinite(),p1,p2); }
00120 
00121 //: Are three lines coplanar, i.e., are they in a common plane?
00122 // \relatesalso vgl_homg_line_3d_2_points
00123 template <class Type>
00124 inline bool coplanar(l const& l1, l const& l2, l const& l3)
00125 {
00126   vgl_homg_point_3d<Type> p = l2.point_finite();
00127   if (collinear(l1,p)) p = l2.point_infinite();
00128   return coplanar(l1,l2) && coplanar(l1,l3) &&
00129          coplanar(l1,p,l3.point_finite()) &&
00130          coplanar(l1,p,l3.point_infinite());
00131 }
00132 
00133 //: Return the intersection point of two concurrent lines
00134 // \relatesalso vgl_homg_line_3d_2_points
00135 template <class Type>
00136 vgl_homg_point_3d<Type> intersection(l const& l1, l const& l2);
00137 
00138 //: Are three lines concurrent, i.e., do they pass through a common point?
00139 // \relatesalso vgl_homg_line_3d_2_points
00140 template <class Type>
00141 inline bool concurrent(l const& l1, l const& l2, l const& l3)
00142 {
00143   if (!concurrent(l1,l2) || !concurrent(l1,l3) || !concurrent(l2,l3)) return false;
00144   return intersection(l1,l2) == intersection(l1,l3);
00145 }
00146 
00147 //+****************************************************************************
00148 // stream operators
00149 //+****************************************************************************
00150 
00151 //: Write to stream (verbose)
00152 // \relatesalso vgl_homg_line_3d_2_points
00153 template <class Type>
00154 vcl_ostream &operator<<(vcl_ostream&s, l const& p);
00155 
00156 //: Read parameters from stream
00157 // \relatesalso vgl_homg_line_3d_2_points
00158 template <class Type>
00159 vcl_istream &operator>>(vcl_istream &is, l &p);
00160 
00161 #undef l
00162 
00163 #define VGL_HOMG_LINE_3D_2_POINTS_INSTANTIATE(T) extern "please include vgl/vgl_homg_line_3d_2_points.txx first"
00164 
00165 #endif // vgl_homg_line_3d_2_points_h_