contrib/gel/vsol/vsol_polyhedron.cxx
Go to the documentation of this file.
00001 // This is gel/vsol/vsol_polyhedron.cxx
00002 #include "vsol_polyhedron.h"
00003 //:
00004 // \file
00005 #include <vcl_cassert.h>
00006 #include <vcl_iostream.h>
00007 #include <vsl/vsl_vector_io.h>
00008 #include <vsol/vsol_point_3d.h>
00009 
00010 //***************************************************************************
00011 // Initialization
00012 //***************************************************************************
00013 
00014 //---------------------------------------------------------------------------
00015 //: Constructor from a vcl_vector (not a geometric vector but a list of points)
00016 // Require: new_vertices.size()>=4 and valid_vertices(new_vertices)
00017 //---------------------------------------------------------------------------
00018 vsol_polyhedron::vsol_polyhedron(vcl_vector<vsol_point_3d_sptr> const& new_vertices)
00019 {
00020   // require
00021   assert(new_vertices.size()>=4);
00022 
00023   storage_=new_vertices; // element-wise assignment
00024 }
00025 
00026 //---------------------------------------------------------------------------
00027 // Copy constructor
00028 //---------------------------------------------------------------------------
00029 vsol_polyhedron::vsol_polyhedron(vsol_polyhedron const &other)
00030   : vsol_volume_3d(other)
00031 {
00032   storage_.clear();
00033   for (unsigned int i=0;i<other.storage_.size();++i)
00034     storage_.push_back(other.storage_[i]); // smart pointers do refcounting
00035 }
00036 
00037 //***************************************************************************
00038 // Comparison
00039 //***************************************************************************
00040 
00041 //---------------------------------------------------------------------------
00042 //: Has `this' the same points than `other' in the same order ?
00043 //---------------------------------------------------------------------------
00044 bool vsol_polyhedron::operator==(vsol_polyhedron const &other) const
00045 {
00046   if (this==&other) return true;
00047   if (storage_.size()!=other.storage_.size()) return false;
00048 
00049   vsol_point_3d_sptr const& p=storage_[0]; // alias
00050 
00051   unsigned int j;
00052   for (j=0; j < storage_.size(); ++j)
00053     if (p==other.storage_[j]) break;
00054   if (j == storage_.size()) return false;
00055 
00056   for (unsigned int i=1; i < storage_.size(); ++i)
00057   {
00058     if (++j >= storage_.size()) j=0;
00059     if (storage_[i]!=other.storage_[j])
00060       return false;
00061   }
00062   return true;
00063 }
00064 
00065 //: spatial object equality
00066 
00067 bool vsol_polyhedron::operator==(vsol_spatial_object_3d const& obj) const
00068 {
00069   return
00070     obj.cast_to_volume() && obj.cast_to_volume()->cast_to_polyhedron() &&
00071     *this == *obj.cast_to_volume()->cast_to_polyhedron();
00072 }
00073 
00074 //---------------------------------------------------------------------------
00075 //: Compute the bounding box of `this'
00076 //---------------------------------------------------------------------------
00077 void vsol_polyhedron::compute_bounding_box(void) const
00078 {
00079   set_bounding_box(storage_[0]->x(),
00080                    storage_[0]->y(),
00081                    storage_[0]->z());
00082   for (unsigned int i=1;i<storage_.size();++i)
00083     add_to_bounding_box(storage_[i]->x(),
00084                         storage_[i]->y(),
00085                         storage_[i]->z());
00086 }
00087 
00088 //---------------------------------------------------------------------------
00089 //: Return the volume of `this'
00090 //---------------------------------------------------------------------------
00091 double vsol_polyhedron::volume(void) const
00092 {
00093   // TO DO
00094   vcl_cerr << "Warning: vsol_polyhedron::volume() has not been implemented yet\n";
00095   return -1;
00096 }
00097 
00098 //---------------------------------------------------------------------------
00099 //: Is `this' convex ?
00100 //---------------------------------------------------------------------------
00101 bool vsol_polyhedron::is_convex(void) const
00102 {
00103   // A polyhedron is always convex since the point order is irrelevant
00104   return true;
00105 }
00106 
00107 //***************************************************************************
00108 // Basic operations
00109 //***************************************************************************
00110 
00111 //---------------------------------------------------------------------------
00112 //: Is `p' in `this' ?
00113 // \todo not yet implemented
00114 //---------------------------------------------------------------------------
00115 bool vsol_polyhedron::in(vsol_point_3d_sptr const& ) const
00116 {
00117   vcl_cerr << "Warning: vsol_polyhedron::in() has not been implemented yet\n";
00118   return false;
00119 }
00120 
00121 //---------------------------------------------------------------------------
00122 //: Destructor
00123 //---------------------------------------------------------------------------
00124 vsol_polyhedron::~vsol_polyhedron()
00125 {
00126   storage_.clear();
00127 }
00128 
00129 //---------------------------------------------------------------------------
00130 //: Return vertex `i'
00131 //  REQUIRE: valid_index(i)
00132 //---------------------------------------------------------------------------
00133 vsol_point_3d_sptr vsol_polyhedron::vertex(int i) const
00134 {
00135   assert(valid_index(i));
00136   return storage_[i];
00137 }
00138 
00139 void vsol_polyhedron::describe(vcl_ostream &strm, int blanking) const
00140 {
00141   if (blanking < 0) blanking = 0; while (blanking--) strm << ' ';
00142   strm << "[vsol_polyhedron";
00143   for (unsigned int i=0; i<size(); ++i)
00144     strm << ' ' << *(vertex(i));
00145   strm << ']' << vcl_endl;
00146 }
00147 
00148 //----------------------------------------------------------------
00149 // ================   Binary I/O Methods ========================
00150 //----------------------------------------------------------------
00151 
00152 //: Binary save self to stream.
00153 void vsol_polyhedron::b_write(vsl_b_ostream &os) const
00154 {
00155   vsl_b_write(os, version());
00156   vsol_spatial_object_3d::b_write(os);
00157   vsl_b_write(os, true); // Indicate non-null pointer stored
00158   vsl_b_write(os, storage_);
00159 }
00160 
00161 //: Binary load self from stream (not typically used)
00162 void vsol_polyhedron::b_read(vsl_b_istream &is)
00163 {
00164   short ver;
00165   vsl_b_read(is, ver);
00166   switch (ver)
00167   {
00168    case 1:
00169     vsol_spatial_object_3d::b_read(is);
00170 
00171     bool null_ptr;
00172     vsl_b_read(is, null_ptr);
00173     if (!null_ptr)
00174       return;
00175     vsl_b_read(is, storage_);
00176     break;
00177    default:
00178     vcl_cerr << "vsol_polyhedron: unknown I/O version " << ver << '\n';
00179   }
00180 }
00181 
00182 //: Return IO version number;
00183 short vsol_polyhedron::version() const
00184 {
00185   return 1;
00186 }
00187 
00188 //: Print an ascii summary to the stream
00189 void vsol_polyhedron::print_summary(vcl_ostream &os) const
00190 {
00191   os << *this;
00192 }
00193 
00194 //: Binary save vsol_polyhedron to stream.
00195 void
00196 vsl_b_write(vsl_b_ostream &os, vsol_polyhedron const* p)
00197 {
00198   if (p==0) {
00199     vsl_b_write(os, false); // Indicate null pointer stored
00200   }
00201   else{
00202     vsl_b_write(os,true); // Indicate non-null pointer stored
00203     p->b_write(os);
00204   }
00205 }
00206 
00207 //: Binary load vsol_polyhedron from stream.
00208 void
00209 vsl_b_read(vsl_b_istream &is, vsol_polyhedron* &p)
00210 {
00211   delete p;
00212   bool not_null_ptr;
00213   vsl_b_read(is, not_null_ptr);
00214   if (not_null_ptr) {
00215     p = new vsol_polyhedron(vcl_vector<vsol_point_3d_sptr>());
00216     p->b_read(is);
00217   }
00218   else
00219     p = 0;
00220 }