core/vgl/vgl_plane_3d.h
Go to the documentation of this file.
00001 // This is core/vgl/vgl_plane_3d.h
00002 #ifndef vgl_plane_3d_h
00003 #define vgl_plane_3d_h
00004 #ifdef VCL_NEEDS_PRAGMA_INTERFACE
00005 #pragma interface
00006 #endif
00007 //:
00008 // \file
00009 // \brief a plane in 3D nonhomogeneous space
00010 // \author Don Hamilton, Peter Tu
00011 // \date   Feb 15 2000
00012 //
00013 // \verbatim
00014 //  Modifications
00015 //   Peter Vanroose  6 July 2001: Added assertion in constructors
00016 //   Peter Vanroose  6 July 2001: Now using vgl_vector_3d for normal direction
00017 //   Peter Vanroose  6 July 2001: Implemented constructor from 3 points
00018 //   Peter Vanroose  6 July 2001: Added normal(); replaced data_[4] by a_ b_ c_ d_
00019 //   Peter Vanroose  6 July 2001: Added operator== and operator!=
00020 //   Peter Vanroose 19 Aug. 2004: implementation of both constructors corrected
00021 //   Peter Vanroose 21 May  2009: istream operator>> re-implemented
00022 // \endverbatim
00023 
00024 #include <vcl_iosfwd.h>
00025 #include <vcl_cassert.h>
00026 #include <vgl/vgl_fwd.h> // forward declare vgl_homg_plane_3d, vgl_point_3d
00027 #include <vgl/vgl_vector_3d.h>
00028 
00029 //: Represents a Euclidean 3D plane
00030 //  The equation of the plane is $ a x + b y + c z + d = 0 $
00031 template <class T>
00032 class vgl_plane_3d
00033 {
00034   // the data associated with this plane
00035   T a_;
00036   T b_;
00037   T c_;
00038   T d_;
00039 
00040  public:
00041 
00042   // Constructors/Initializers/Destructor------------------------------------
00043 
00044   // Default constructor: horizontal XY-plane (equation 1.z = 0)
00045   inline vgl_plane_3d () : a_(0), b_(0), c_(1), d_(0) {}
00046 
00047 #if 0
00048   // Default copy constructor - compiler provides the correct one
00049   inline vgl_plane_3d (vgl_plane_3d<T> const& pl)
00050     : a_(pl.a()), b_(pl.b()), c_(pl.c()), d_(pl.d()) {}
00051   // Default destructor - compiler provides the correct one
00052   inline ~vgl_plane_3d () {}
00053   // Default assignment operator - compiler provides the correct one
00054   inline vgl_plane_3d<T>& operator=(vgl_plane_3d<T> const& pl)
00055   { a_ = pl.a(); b_ = pl.b(); c_ = pl.c(); d_ = pl.d(); return *this; }
00056 #endif
00057 
00058   //: Construct a vgl_plane_3d from its equation $ax+by+cz+d=0$
00059   //  At least one of a, b or c should be nonzero.
00060   inline vgl_plane_3d (T ta,T tb,T tc,T td)
00061     : a_(ta), b_(tb), c_(tc), d_(td) { assert(ta||tb||tc); }
00062 
00063   //: Construct a vgl_plane_3d from its equation $v[0]x+v[1]y+v[2]z+v[3]=0$
00064   //  At least one of v[0], v[1] or v[2] should be nonzero.
00065   inline vgl_plane_3d (const T v[4])
00066     : a_(v[0]), b_(v[1]), c_(v[2]), d_(v[3]) { assert(a_||b_||c_); }
00067 
00068   //: Construct from a homogeneous plane
00069   vgl_plane_3d (vgl_homg_plane_3d<T> const& p);
00070 
00071   //: Construct from Normal and a point
00072   //  The plane goes through the point \a p and will be orthogonal to \a normal.
00073   vgl_plane_3d (vgl_vector_3d<T> const& normal,
00074                 vgl_point_3d<T> const& p);
00075 
00076   //: Construct from three non-collinear points
00077   //  The plane will contain all three points \a p1, \a p2 and \a p3.
00078   vgl_plane_3d (vgl_point_3d<T> const& p1,
00079                 vgl_point_3d<T> const& p2,
00080                 vgl_point_3d<T> const& p3);
00081 
00082   // Data Access-------------------------------------------------------------
00083 
00084   //: Return \a x coefficient
00085   inline T a()  const {return a_;}
00086   inline T nx() const {return a_;}
00087   //: Return \a y coefficient
00088   inline T b()  const {return b_;}
00089   inline T ny() const {return b_;}
00090   //: Return \a z coefficient
00091   inline T c()  const {return c_;}
00092   inline T nz() const {return c_;}
00093   //: Return constant coefficient
00094   inline T d()  const {return d_;}
00095 
00096   //: Set this vgl_plane_3d to have the equation $ax+by+cz+d=0$
00097   inline void set(T ta,T tb,T tc,T td) { assert(ta||tb||tc); a_=ta; b_=tb; c_=tc; d_=td; }
00098 
00099   //: the comparison operator
00100   //  The equations need not be identical, but just equivalent.
00101   bool operator==( vgl_plane_3d<T> const& p) const;
00102   inline bool operator!=( vgl_plane_3d<T>const& p) const { return !operator==(p); }
00103 
00104   //: Return true iff the plane is the plane at infinity.
00105   //  Always returns false
00106   inline bool ideal(T = (T)0) const { return false; }
00107 
00108   //: Return the normal direction, i.e., a unit vector orthogonal to this plane
00109   inline vgl_vector_3d<T> normal() const
00110   { return normalized(vgl_vector_3d<T>(a(),b(),c())); }
00111 
00112   //: Return true if p is on the plane
00113   bool contains(vgl_point_3d<T> const& p, T tol = (T)0) const;
00114 
00115   // the coordinate system on the plane, (u, v), is defined as,
00116   // cases:
00117   // 1) n not parallel to Y
00118   //   u = Y x n and v = n x u
00119   //
00120   // 2) n parallel to Y
00121   //   u = n x Z and v = u x n
00122   //
00123   // the plane origin is the point in the plane closest to the world origin
00124 
00125   //: Given a 3-d point, return a 2-d point in the coord. system of the plane
00126   // If the point is not on the plane then false is returned
00127   bool plane_coords(vgl_point_3d<T> const& p3d,
00128                     vgl_point_2d<T>& p2d, T tol=(T)0 ) const;
00129 
00130   //: inverse map from plane coordinates to world coordinates
00131   vgl_point_3d<T> world_coords(vgl_point_2d<T> const& p2d) const;
00132 
00133   //: plane coordinate unit vectors
00134   void plane_coord_vectors(vgl_vector_3d<T>& uvec,
00135                            vgl_vector_3d<T>& vvec) const;
00136 };
00137 
00138 //: Return true iff p is the plane at infinity
00139 //  Always returns false
00140 template <class T> inline
00141 bool is_ideal(vgl_plane_3d<T> const&, T tol=(T)0) { return false; }
00142 
00143 
00144 //: Write to stream
00145 // \relatesalso vgl_plane_3d
00146 template <class T>
00147 vcl_ostream& operator<<(vcl_ostream& s, const vgl_plane_3d<T>& p);
00148 
00149 //: Read in four plane parameters from stream
00150 //  Either just reads four blank-separated numbers,
00151 //  or reads four comma-separated numbers,
00152 //  or reads four numbers in parenthesized form "(123, 321, -456, 777)"
00153 //  or reads a formatted line equation "123x+321y-456z+777=0"
00154 // \relatesalso vgl_plane_3d
00155 template <class T>
00156 vcl_istream& operator>>(vcl_istream& is, vgl_plane_3d<T>& p);
00157 
00158 #define VGL_PLANE_3D_INSTANTIATE(T) extern "please include vgl/vgl_plane_3d.txx first"
00159 
00160 #endif // vgl_plane_3d_h