contrib/gel/vsol/vsol_polyhedron.h
Go to the documentation of this file.
00001 // This is gel/vsol/vsol_polyhedron.h
00002 #ifndef vsol_polyhedron_h_
00003 #define vsol_polyhedron_h_
00004 //*****************************************************************************
00005 //:
00006 // \file
00007 // \brief Polyhedral volume in 3D space
00008 //
00009 //  Representation of an arbitrary polyhedral volume, i.e., a volume that
00010 //  is bounded by any set of flat polygons.
00011 //
00012 //  A sufficient minimal representation for this is the set of corner points
00013 //  together with the polygonal faces they form.
00014 //
00015 // \author Peter Vanroose
00016 // \date   5 July 2000.
00017 //
00018 // \verbatim
00019 //  Modifications
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_volume_3d.h>
00026 #include <vsol/vsol_point_3d.h>
00027 #include <vsol/vsol_point_3d_sptr.h>
00028 #include <vcl_vector.h>
00029 #include <vcl_iosfwd.h>
00030 #include <vsl/vsl_binary_io.h>
00031 class vsol_tetrahedron;
00032 
00033 class vsol_polyhedron : public vsol_volume_3d
00034 {
00035  protected:
00036   //***************************************************************************
00037   // Data members
00038   //***************************************************************************
00039 
00040   //---------------------------------------------------------------------------
00041   //: List of vertices
00042   //---------------------------------------------------------------------------
00043   vcl_vector<vsol_point_3d_sptr> storage_;
00044 
00045  public:
00046   //***************************************************************************
00047   // Initialization
00048   //***************************************************************************
00049 
00050   //---------------------------------------------------------------------------
00051   //: Constructor from a vcl_vector (i.e., a list of points)
00052   //  REQUIRE: new_vertices.size()>=4
00053   //---------------------------------------------------------------------------
00054   explicit vsol_polyhedron(vcl_vector<vsol_point_3d_sptr> const& new_vertices);
00055 
00056   //---------------------------------------------------------------------------
00057   //: Copy constructor
00058   //---------------------------------------------------------------------------
00059   vsol_polyhedron(vsol_polyhedron const& other);
00060 
00061   //---------------------------------------------------------------------------
00062   //: Destructor
00063   //---------------------------------------------------------------------------
00064   virtual ~vsol_polyhedron();
00065 
00066   //---------------------------------------------------------------------------
00067   //: Clone `this': creation of a new object and initialization
00068   //  See Prototype pattern
00069   //---------------------------------------------------------------------------
00070   virtual vsol_spatial_object_3d* clone(void) const { return new vsol_polyhedron(*this); }
00071 
00072   //---------------------------------------------------------------------------
00073   //: Safe down-casting methods
00074   //---------------------------------------------------------------------------
00075   virtual vsol_polyhedron *cast_to_polyhedron(void) {return this;}
00076   virtual vsol_polyhedron const* cast_to_polyhedron(void) const {return this;}
00077 
00078   virtual vsol_tetrahedron* cast_to_tetrahedron(void) {return 0;}
00079   virtual const vsol_tetrahedron* cast_to_tetrahedron(void) const {return 0;}
00080 
00081   //***************************************************************************
00082   // Access
00083   //***************************************************************************
00084 
00085   //---------------------------------------------------------------------------
00086   //: Return vertex `i'
00087   //  REQUIRE: valid_index(i)
00088   //---------------------------------------------------------------------------
00089   vsol_point_3d_sptr vertex(int i) const;
00090 
00091   //***************************************************************************
00092   // Comparison
00093   //***************************************************************************
00094 
00095   //---------------------------------------------------------------------------
00096   //: Is `this' spanning the same the same volume than `other' ?
00097   //---------------------------------------------------------------------------
00098   virtual bool operator==(vsol_polyhedron const& other) const;
00099   virtual bool operator==(vsol_spatial_object_3d const& obj) const; // virtual of vsol_spatial_object_3d
00100 
00101   //---------------------------------------------------------------------------
00102   //: Negation of operator==
00103   //---------------------------------------------------------------------------
00104   bool operator!=(vsol_polyhedron const& o) const { return !operator==(o); }
00105 
00106   //***************************************************************************
00107   // Status report
00108   //***************************************************************************
00109 
00110   //---------------------------------------------------------------------------
00111   //: Return the volume type of a polyhedron.  Its spatial type is a VOLUME.
00112   //---------------------------------------------------------------------------
00113   vsol_volume_3d_type volume_type(void)const{return vsol_volume_3d::POLYHEDRON;}
00114 
00115   //---------------------------------------------------------------------------
00116   //: Compute the bounding box of `this'
00117   //---------------------------------------------------------------------------
00118   virtual void compute_bounding_box(void) const;
00119 
00120   //---------------------------------------------------------------------------
00121   //: Return the number of vertices
00122   //---------------------------------------------------------------------------
00123   unsigned int size(void) const { return storage_.size(); }
00124   unsigned int num_vertices(void) const { return storage_.size(); }
00125 
00126   //---------------------------------------------------------------------------
00127   //: Return the volume of `this'
00128   //---------------------------------------------------------------------------
00129   virtual double volume(void) const;
00130 
00131   //---------------------------------------------------------------------------
00132   //: Is `this' convex ?
00133   //---------------------------------------------------------------------------
00134   virtual bool is_convex(void) const;
00135 
00136   //---------------------------------------------------------------------------
00137   //: Is `i' a valid index for the list of vertices ?
00138   //---------------------------------------------------------------------------
00139   bool valid_index(unsigned int i) const { return i<storage_.size(); }
00140 
00141   //***************************************************************************
00142   // Basic operations
00143   //***************************************************************************
00144 
00145   //---------------------------------------------------------------------------
00146   //: Is `p' in `this' ?
00147   //---------------------------------------------------------------------------
00148   virtual bool in(vsol_point_3d_sptr const& p) const;
00149 
00150   // ==== Binary IO methods ======
00151 
00152   //: Binary save self to stream.
00153   void b_write(vsl_b_ostream &os) const;
00154 
00155   //: Binary load self from stream.
00156   void b_read(vsl_b_istream &is);
00157 
00158   //: Return IO version number;
00159   short version() const;
00160 
00161   //: Print an ascii summary to the stream
00162   void print_summary(vcl_ostream &os) const;
00163 
00164   //: Return a platform independent string identifying the class
00165   virtual vcl_string is_a() const { return vcl_string("vsol_polyhedron"); }
00166 
00167   //: Return true if the argument matches the string identifying the class or any parent class
00168   virtual bool is_class(vcl_string const& cls) const
00169   { return cls==is_a() || vsol_volume_3d::is_class(cls); }
00170 
00171   //---------------------------------------------------------------------------
00172   //: output description to stream
00173   //---------------------------------------------------------------------------
00174   void describe(vcl_ostream &strm, int blanking=0) const;
00175 
00176  protected:
00177   //---------------------------------------------------------------------------
00178   //: Default constructor. Do nothing. Just to enable inheritance. Protected.
00179   //---------------------------------------------------------------------------
00180   vsol_polyhedron() {}
00181 };
00182 
00183 //: Binary save vsol_polyhedron* to stream.
00184 void vsl_b_write(vsl_b_ostream &os, const vsol_polyhedron* p);
00185 
00186 //: Binary load vsol_polyhedron* from stream.
00187 void vsl_b_read(vsl_b_istream &is, vsol_polyhedron* &p);
00188 
00189 #endif // vsol_polyhedron_h_