contrib/gel/vsol/vsol_polygon_3d.h
Go to the documentation of this file.
00001 // This is gel/vsol/vsol_polygon_3d.h
00002 #ifndef vsol_polygon_3d_h_
00003 #define vsol_polygon_3d_h_
00004 //*****************************************************************************
00005 //:
00006 // \file
00007 // \brief Polygon in 3D space
00008 //
00009 // The vertices are to be defined in counterclockwise order.
00010 //
00011 // \author François BERTEL
00012 // \date   2000-05-09
00013 //
00014 // \verbatim
00015 //  Modifications
00016 //   2000-05-09 François BERTEL Creation
00017 //   2000-06-17 Peter Vanroose  Implemented all operator==()s and type info
00018 //   2001-07-03 Peter Vanroose  Replaced vnl_double_3 by vgl_vector_3d
00019 //   2001-07-03 Peter Vanroose  Corrected the implementation of is_convex()
00020 //   2004-05-14 Peter Vanroose  Added describe()
00021 //   2004-09-06 Peter Vanroose  Added Binary I/O
00022 // \endverbatim
00023 //*****************************************************************************
00024 
00025 #include <vsol/vsol_region_3d.h>
00026 #include <vsol/vsol_point_3d_sptr.h>
00027 #include <vcl_vector.h>
00028 #include <vcl_iosfwd.h>
00029 #include <vsl/vsl_binary_io.h>
00030 #include <vgl/vgl_fwd.h> // vgl_vector_3d
00031 #include <vgl/vgl_homg_plane_3d.h>
00032 class vsol_triangle_3d;
00033 class vsol_rectangle_3d;
00034 
00035 class vsol_polygon_3d : public vsol_region_3d
00036 {
00037  protected:
00038   //***************************************************************************
00039   // Data members
00040   //***************************************************************************
00041 
00042   //---------------------------------------------------------------------------
00043   //: List of vertices
00044   //---------------------------------------------------------------------------
00045   vcl_vector<vsol_point_3d_sptr> *storage_;
00046 
00047   //***************************************************************************
00048   // Initialization
00049   //***************************************************************************
00050 
00051  public:
00052   //---------------------------------------------------------------------------
00053   //: Default constructor. Do nothing. 
00054   //---------------------------------------------------------------------------
00055   vsol_polygon_3d(void);
00056 
00057   //---------------------------------------------------------------------------
00058   //: Constructor from a vcl_vector (not a geometric vector but a list of points)
00059   //  REQUIRE: new_vertices.size()>=3 and valid_vertices(new_vertices)
00060   //---------------------------------------------------------------------------
00061   explicit vsol_polygon_3d(vcl_vector<vsol_point_3d_sptr> const& new_vertices);
00062 
00063   //---------------------------------------------------------------------------
00064   //: Copy constructor
00065   //---------------------------------------------------------------------------
00066   vsol_polygon_3d(vsol_polygon_3d const& other);
00067 
00068   //---------------------------------------------------------------------------
00069   //: Destructor
00070   //---------------------------------------------------------------------------
00071   virtual ~vsol_polygon_3d();
00072 
00073   //---------------------------------------------------------------------------
00074   //: Clone `this': creation of a new object and initialization
00075   //  See Prototype pattern
00076   //---------------------------------------------------------------------------
00077   virtual vsol_spatial_object_3d* clone(void) const;
00078 
00079   //---------------------------------------------------------------------------
00080   //: Safe down-casting methods
00081   //---------------------------------------------------------------------------
00082   virtual vsol_polygon_3d *cast_to_polygon(void) {return this;}
00083   virtual vsol_polygon_3d const* cast_to_polygon(void) const {return this;}
00084 
00085   virtual vsol_triangle_3d* cast_to_triangle(void) {return 0;}
00086   virtual const vsol_triangle_3d* cast_to_triangle(void) const {return 0;}
00087 
00088   virtual vsol_rectangle_3d* cast_to_rectangle(void) {return 0;}
00089   virtual const vsol_rectangle_3d* cast_to_rectangle(void) const {return 0;}
00090 
00091   //***************************************************************************
00092   // Access
00093   //***************************************************************************
00094 
00095   //---------------------------------------------------------------------------
00096   //: Return vertex `i'
00097   //  REQUIRE: valid_index(i)
00098   //---------------------------------------------------------------------------
00099   vsol_point_3d_sptr vertex(const int i) const;
00100 
00101   //***************************************************************************
00102   // Comparison
00103   //***************************************************************************
00104 
00105   //---------------------------------------------------------------------------
00106   //: Has `this' the same points than `other' in the same order ?
00107   //---------------------------------------------------------------------------
00108   virtual bool operator==(vsol_polygon_3d const& other) const;
00109   virtual bool operator==(vsol_spatial_object_3d const& obj) const; // virtual of vsol_spatial_object_3d
00110 
00111   //---------------------------------------------------------------------------
00112   //: Has `this' not the same points than `other' in the same order ?
00113   //---------------------------------------------------------------------------
00114   inline bool operator!=(vsol_polygon_3d const& o)const{return !operator==(o);}
00115 
00116   //***************************************************************************
00117   // Status report
00118   //***************************************************************************
00119 
00120   //---------------------------------------------------------------------------
00121   //: Return the region type of a polygon.  Its spatial type is a REGION
00122   //---------------------------------------------------------------------------
00123   vsol_region_3d_type region_type(void) const { return vsol_region_3d::POLYGON; }
00124 
00125   //---------------------------------------------------------------------------
00126   //: Compute the bounding box of `this'
00127   //---------------------------------------------------------------------------
00128   virtual void compute_bounding_box(void) const;
00129 
00130   //---------------------------------------------------------------------------
00131   //: Return the number of vertices
00132   //---------------------------------------------------------------------------
00133   unsigned int size(void) const { return storage_->size(); }
00134 
00135   //---------------------------------------------------------------------------
00136   //: Return the area of `this'
00137   //---------------------------------------------------------------------------
00138   virtual double area(void) const; // virtual of vsol_region_3d
00139 
00140   //---------------------------------------------------------------------------
00141   //: Return the plane where 'this' polygon resides
00142   //---------------------------------------------------------------------------
00143   vgl_homg_plane_3d<double> plane(void) const { return plane_; }
00144 
00145   //---------------------------------------------------------------------------
00146   //: Is `this' convex ?
00147   //---------------------------------------------------------------------------
00148   virtual bool is_convex(void) const;
00149 
00150   //---------------------------------------------------------------------------
00151   //: Is `i' a valid index for the list of vertices ?
00152   //---------------------------------------------------------------------------
00153   bool valid_index(unsigned int i) const { return i<storage_->size(); }
00154 
00155   //---------------------------------------------------------------------------
00156   //: Are `new_vertices' valid vertices to build a polygon of the current type?
00157   //  That is: are all vertices in the same plane ?
00158   //---------------------------------------------------------------------------
00159   virtual bool valid_vertices(const vcl_vector<vsol_point_3d_sptr> new_vertices) const;
00160 
00161   //***************************************************************************
00162   // Basic operations
00163   //***************************************************************************
00164 
00165   //---------------------------------------------------------------------------
00166   //: Is `p' in `this' ?
00167   //---------------------------------------------------------------------------
00168   virtual bool in(vsol_point_3d_sptr const& p) const;
00169 
00170   //---------------------------------------------------------------------------
00171   //: Return the unit normal vector at point `p'. Have to be deleted manually
00172   //  REQUIRE: in(p)
00173   //---------------------------------------------------------------------------
00174   virtual vgl_vector_3d<double> normal_at_point(vsol_point_3d_sptr const& p) const;
00175 
00176   //---------------------------------------------------------------------------
00177   //: Return the normal vector
00178   //---------------------------------------------------------------------------
00179   vgl_vector_3d<double> normal() const;
00180 
00181   // ==== Binary IO methods ======
00182 
00183   //: Binary save self to stream.
00184   void b_write(vsl_b_ostream &os) const;
00185 
00186   //: Binary load self from stream.
00187   void b_read(vsl_b_istream &is);
00188 
00189   //: Return IO version number;
00190   short version() const;
00191 
00192   //: Print an ascii summary to the stream
00193   void print_summary(vcl_ostream &os) const;
00194 
00195   //: Return a platform independent string identifying the class
00196   virtual vcl_string is_a() const { return vcl_string("vsol_polygon_3d"); }
00197 
00198   //: Return true if the argument matches the string identifying the class or any parent class
00199   virtual bool is_class(vcl_string const& cls) const
00200   { return cls==is_a() || vsol_region_3d::is_class(cls); }
00201 
00202   //---------------------------------------------------------------------------
00203   //: output description to stream
00204   //---------------------------------------------------------------------------
00205   void describe(vcl_ostream &strm, int blanking=0) const;
00206 
00207  protected:
00208   void compute_plane();
00209   vgl_homg_plane_3d<double> plane_;
00210 };
00211 
00212 //: Binary save vsol_polygon_3d* to stream.
00213 void vsl_b_write(vsl_b_ostream &os, const vsol_polygon_3d* p);
00214 
00215 //: Binary load vsol_polygon_3d* from stream.
00216 void vsl_b_read(vsl_b_istream &is, vsol_polygon_3d* &p);
00217 
00218 #endif // vsol_polygon_3d_h_