core/vgl/vgl_homg_plane_3d.h
Go to the documentation of this file.
00001 // This is core/vgl/vgl_homg_plane_3d.h
00002 #ifndef vgl_homg_plane_3d_h
00003 #define vgl_homg_plane_3d_h
00004 #ifdef VCL_NEEDS_PRAGMA_INTERFACE
00005 #pragma interface
00006 #endif
00007 //:
00008 // \file
00009 // \brief homogeneous plane in 3D projective space
00010 // \author Don HAMILTON Peter TU
00011 //
00012 // \verbatim
00013 //  Modifications
00014 //   Peter Vanroose  6 July 2001: Now using vgl_vector_3d for normal direction
00015 //   Peter Vanroose  6 July 2001: Added normal(); replaced data_[4] by a_ b_ c_ d_
00016 //   Peter Vanroose  6 July 2001: Added constructor from 3 points
00017 //   CJB (Manchester) 16/03/2001: Tidied up the documentation
00018 //   Peter Vanroose 15 July 2002: Added constructor from two concurrent lines
00019 //   cbw (imorphics) 31 Mar 2008: Corrected constructor with normal and point (negated d) and added test
00020 //   Peter Vanroose 7 March 2009: Added normalize(), similar to the one in vgl_homg_line_2d<T>
00021 // \endverbatim
00022 
00023 #include <vcl_iosfwd.h>
00024 #include <vgl/vgl_fwd.h> // forward declare vgl_plane_3d and vgl_homg_point_3d
00025 #include <vgl/vgl_vector_3d.h>
00026 #include <vcl_cassert.h>
00027 
00028 //: Represents a homogeneous 3D plane
00029 template <class Type>
00030 class vgl_homg_plane_3d
00031 {
00032   // the four homogeneous coordinates of the plane (dual of a point).
00033   Type a_;
00034   Type b_;
00035   Type c_;
00036   Type d_;
00037 
00038  public:
00039   inline vgl_homg_plane_3d () {}
00040 
00041   //: Construct from four Types.
00042   inline vgl_homg_plane_3d(Type ta, Type tb, Type tc, Type td) : a_(ta), b_(tb), c_(tc), d_(td) {}
00043 
00044   //: Construct from 4-vector.
00045   inline vgl_homg_plane_3d(const Type v[4]) : a_(v[0]), b_(v[1]), c_(v[2]), d_(v[3]) {}
00046 
00047   //: Construct from non-homogeneous plane.
00048   vgl_homg_plane_3d (vgl_plane_3d<Type> const& pl);
00049 
00050   //: Construct from normal and a point
00051   //  The given point must not be at infinity.
00052   vgl_homg_plane_3d (vgl_vector_3d<Type> const& n, vgl_homg_point_3d<Type> const& p);
00053 
00054   //: Construct from three non-collinear points
00055   vgl_homg_plane_3d (vgl_homg_point_3d<Type> const& p1,
00056                      vgl_homg_point_3d<Type> const& p2,
00057                      vgl_homg_point_3d<Type> const& p3);
00058 
00059   //: Construct from two concurrent lines
00060   vgl_homg_plane_3d (vgl_homg_line_3d_2_points<Type> const& l1,
00061                      vgl_homg_line_3d_2_points<Type> const& l2);
00062 
00063   // Data Access-------------------------------------------------------------
00064 
00065   //: Return \a x coefficient
00066   inline Type a() const {return a_;}
00067   inline Type nx() const {return a_;}
00068   //: Return \a y coefficient
00069   inline Type b() const {return b_;}
00070   inline Type ny() const {return b_;}
00071   //: Return \a z coefficient
00072   inline Type c() const {return c_;}
00073   inline Type nz() const {return c_;}
00074   //: Return homogeneous scaling coefficient
00075   inline Type d() const {return d_;}
00076 
00077   //: Set equation \a a*x+b*y+c*z+d*w=0
00078   inline void set(Type ta,Type tb,Type tc,Type td) { assert(ta||tb||tc||td);a_=ta;b_=tb;c_=tc;d_=td; }
00079 
00080   //: the comparison operator
00081   bool operator==( vgl_homg_plane_3d<Type> const& pl) const;
00082   inline bool operator!=( vgl_homg_plane_3d<Type>const& pl) const { return !operator==(pl); }
00083 
00084   //: Return true iff the plane is the plane at infinity.
00085   // The method checks that max(|a|,|b|,|c|) <= tol * |d|
00086   // If called without an argument, tol=0, i.e., a, b and c must be 0.
00087   inline bool ideal(Type tol = (Type)0) const
00088   {
00089 #define vgl_Abs(x) (x<0?-x:x) // avoid #include of vcl_cmath.h AND vcl_cstdlib.h
00090     return vgl_Abs(a()) <= tol * vgl_Abs(d()) &&
00091            vgl_Abs(b()) <= tol * vgl_Abs(d()) &&
00092            vgl_Abs(c()) <= tol * vgl_Abs(d());
00093 #undef vgl_Abs
00094   }
00095 
00096   inline vgl_vector_3d<double> normal() const { return normalized(vgl_vector_3d<double>(a(),b(),c())); }
00097 
00098   //: divide all coefficients by sqrt(a^2 + b^2 + c^2)
00099   void normalize();
00100 };
00101 
00102 //: Return true iff p is the plane at infinity
00103 // The method checks that max(|a|,|b|,|c|) <= tol * |d|
00104 // \relatesalso vgl_homg_plane_3d
00105 template <class Type>
00106 inline bool is_ideal(vgl_homg_plane_3d<Type> const& p, Type tol=(Type)0) { return p.ideal(tol); }
00107 
00108 // stream operators
00109 
00110 template <class Type>
00111 vcl_ostream&  operator<<(vcl_ostream& s, const vgl_homg_plane_3d<Type>& p);
00112 
00113 template <class Type>
00114 vcl_istream&  operator>>(vcl_istream& is, vgl_homg_plane_3d<Type>& p);
00115 
00116 #define VGL_HOMG_PLANE_3D_INSTANTIATE(T) extern "please include vgl/vgl_homg_plane_3d.txx first"
00117 
00118 #endif // vgl_homg_plane_3d_h