contrib/gel/vsol/vsol_group_3d.cxx
Go to the documentation of this file.
00001 // This is gel/vsol/vsol_group_3d.cxx
00002 #include "vsol_group_3d.h"
00003 //:
00004 // \file
00005 #include <vcl_cassert.h>
00006 #include <vsl/vsl_string_io.h>
00007 #include <vsl/vsl_vector_io.h>
00008 
00009 //***************************************************************************
00010 // Initialization
00011 //***************************************************************************
00012 
00013 //---------------------------------------------------------------------------
00014 //: Default Constructor: with no child
00015 //---------------------------------------------------------------------------
00016 vsol_group_3d::vsol_group_3d(void)
00017   : vsol_spatial_object_3d()
00018 {
00019   storage_=new vcl_vector<vsol_spatial_object_3d_sptr>();
00020 }
00021 
00022 //---------------------------------------------------------------------------
00023 //: Copy constructor.
00024 // Description: The objects of the group are not duplicated
00025 //---------------------------------------------------------------------------
00026 vsol_group_3d::vsol_group_3d(vsol_group_3d const& other)
00027   : vsol_spatial_object_3d(other)
00028 {
00029   storage_=new vcl_vector<vsol_spatial_object_3d_sptr>(*other.storage_);
00030 }
00031 
00032 //---------------------------------------------------------------------------
00033 //: Destructor
00034 // Description: The objects of the group are not deleted
00035 //---------------------------------------------------------------------------
00036 vsol_group_3d::~vsol_group_3d()
00037 {
00038   delete storage_;
00039 }
00040 
00041 //---------------------------------------------------------------------------
00042 //: Clone `this': creation of a new object and initialization
00043 // See Prototype pattern
00044 //---------------------------------------------------------------------------
00045 vsol_spatial_object_3d* vsol_group_3d::clone(void) const
00046 {
00047   return new vsol_group_3d(*this);
00048 }
00049 
00050 //***************************************************************************
00051 // Access
00052 //***************************************************************************
00053 
00054 //---------------------------------------------------------------------------
00055 //: Return the object `i'
00056 // Require: i>=0 and i<size()
00057 //---------------------------------------------------------------------------
00058 vsol_spatial_object_3d_sptr vsol_group_3d::object(unsigned int i) const
00059 {
00060   // require
00061   assert(i<size());
00062 
00063   return (*storage_)[i];
00064 }
00065 
00066 //***************************************************************************
00067 // Status report
00068 //***************************************************************************
00069 
00070 //---------------------------------------------------------------------------
00071 //: Return the real type of a group. It is a SPATIALGROUP
00072 //---------------------------------------------------------------------------
00073 vsol_spatial_object_3d::vsol_spatial_object_3d_type
00074 vsol_group_3d::spatial_type(void) const
00075 {
00076   return vsol_spatial_object_3d::SPATIALGROUP;
00077 }
00078 
00079 //---------------------------------------------------------------------------
00080 //: Compute the bounding box of `this'
00081 // Require: size()>0
00082 //---------------------------------------------------------------------------
00083 void vsol_group_3d::compute_bounding_box(void) const
00084 {
00085   // require
00086   assert(size()>0);
00087 
00088   vcl_vector<vsol_spatial_object_3d_sptr>::iterator i = storage_->begin();
00089   set_bounding_box(   (*i)->get_min_x(), (*i)->get_min_y(), (*i)->get_min_z());
00090   add_to_bounding_box((*i)->get_max_x(), (*i)->get_max_y(), (*i)->get_max_z());
00091   for (++i; i!=storage_->end(); ++i)
00092   {
00093     add_to_bounding_box((*i)->get_min_x(), (*i)->get_min_y(), (*i)->get_min_z());
00094     add_to_bounding_box((*i)->get_max_x(), (*i)->get_max_y(), (*i)->get_max_z());
00095   }
00096 }
00097 
00098 //---------------------------------------------------------------------------
00099 //: Return the number of objects of the group
00100 //---------------------------------------------------------------------------
00101 unsigned int vsol_group_3d::deep_size(void) const
00102 {
00103   int result=0;
00104   vcl_vector<vsol_spatial_object_3d_sptr>::iterator i;
00105   for (i=storage_->begin(); i!=storage_->end(); ++i)
00106   {
00107     vsol_group_3d const* g=(*i)->cast_to_group();
00108     if (g!=0)
00109       result+=g->deep_size();
00110     else
00111       ++result;
00112   }
00113   return result;
00114 }
00115 
00116 //***************************************************************************
00117 // Element change
00118 //***************************************************************************
00119 
00120 //---------------------------------------------------------------------------
00121 //: Add an object `new_object' to `this'
00122 // Require: !is_child(new_object)
00123 //---------------------------------------------------------------------------
00124 void vsol_group_3d::add_object(vsol_spatial_object_3d_sptr const& new_object)
00125 {
00126   // require
00127   assert(!is_child(new_object));
00128 
00129   storage_->push_back(new_object);
00130 }
00131 
00132 //***************************************************************************
00133 // Removal
00134 //***************************************************************************
00135 
00136 //---------------------------------------------------------------------------
00137 //: Remove object `i' of `this' (not delete it)
00138 // Require: i>=0 and i<size()
00139 //---------------------------------------------------------------------------
00140 void vsol_group_3d::remove_object(unsigned int i)
00141 {
00142   // require
00143   assert(i<size());
00144 
00145   vcl_vector<vsol_spatial_object_3d_sptr>::iterator j = storage_->begin() + i;
00146   storage_->erase(j);
00147 }
00148 
00149 //---------------------------------------------------------------------------
00150 //: Is `new_object' a child (direct or not) of `this' ?
00151 //---------------------------------------------------------------------------
00152 bool
00153 vsol_group_3d::is_child(vsol_spatial_object_3d_sptr const& new_object) const
00154 {
00155   vcl_vector<vsol_spatial_object_3d_sptr>::iterator i;
00156   for (i=storage_->begin(); i!=storage_->end(); ++i)
00157   {
00158     if ((*i).ptr()==new_object.ptr())
00159       return true;
00160     vsol_group_3d const* g=(*i)->cast_to_group();
00161     if (g!=0 && g->is_child(new_object))
00162       return true;
00163   }
00164   return false;
00165 }
00166 
00167 bool vsol_group_3d::operator==(vsol_group_3d const& other) const
00168 {
00169   //groups must have the same number of shallow elements
00170   if (this->size()!= other.size())
00171     return false;
00172   //groups must have the same number of deep (flattened) elements
00173   if (this->deep_size()!= other.deep_size())
00174     return false;
00175   //groups must have the same elements and in the same order
00176   for (unsigned int i = 0; i<this->size(); i++)
00177     if (*(this->object(i))!=*(other.object(i)))
00178       return false;
00179   return true;
00180 }
00181 
00182 bool vsol_group_3d::operator==(vsol_spatial_object_3d const& obj) const
00183 {
00184   return obj.cast_to_group() && *this == *obj.cast_to_group();
00185 }
00186 
00187 //----------------------------------------------------------------
00188 // ================   Binary I/O Methods ========================
00189 //----------------------------------------------------------------
00190 
00191 //: Binary save self to stream.
00192 void vsol_group_3d::b_write(vsl_b_ostream &os) const
00193 {
00194   vsl_b_write(os, version());
00195   vsl_b_write(os, *storage_);
00196 #if 0
00197   for (unsigned int i = 0; i<this->size(); i++)
00198   {
00199     vsol_spatial_object_3d_sptr so = this->object(i);
00200     vsol_point_3d_sptr p = so->cast_to_point();
00201     if (p)
00202     {
00203       vsl_b_write(os, p->is_a());
00204       vsl_b_write(os, p);
00205       continue;
00206     }
00207     vsol_curve_3d* c = so->cast_to_curve();
00208     if (c)
00209     {
00210       vsol_line_3d_sptr l = c->cast_to_line();
00211       if (l)
00212       {
00213         vsl_b_write(os, l->is_a());
00214         vsl_b_write(os, l);
00215         continue;
00216       }
00217       vsol_conic_3d_sptr cn = c->cast_to_conic();
00218       if (cn)
00219       {
00220         vsl_b_write(os, cn->is_a());
00221         vsl_b_write(os, cn);
00222         continue;
00223       }
00224       vsol_polyline_3d_sptr pl = c->cast_to_polyline();
00225       if (pl)
00226       {
00227         vsl_b_write(os, pl->is_a());
00228         vsl_b_write(os, pl);
00229         continue;
00230       }
00231     }
00232     vsol_region_3d* r = so->cast_to_region();
00233     if (r)
00234     {
00235       vsol_polygon_3d_sptr pg = r->cast_to_polygon();
00236       if (pg)
00237       {
00238         vsol_triangle_3d_sptr tr = pg->cast_to_triangle();
00239         if (tr)
00240         {
00241           vsl_b_write(os, tr->is_a());
00242           vsl_b_write(os, tr);
00243           continue;
00244         }
00245         vsol_rectangle_3d_sptr rc = pg->cast_to_rectangle();
00246         if (rc)
00247         {
00248           vsl_b_write(os, rc->is_a());
00249           vsl_b_write(os, rc);
00250           continue;
00251         }
00252         vsl_b_write(os, pg->is_a());
00253         vsl_b_write(os, pg);
00254         continue;
00255       }
00256     }
00257     vsol_group_3d* g = so->cast_to_group();
00258     if (g)
00259     {
00260       vsl_b_write(os, g->is_a());
00261       g->b_write(os);
00262     }
00263   }
00264   vsl_b_write(os, vcl_string("vsol_group_3d_end"));
00265 #endif // 0
00266 }
00267 
00268 //: Binary load self from stream (not typically used)
00269 void vsol_group_3d::b_read(vsl_b_istream &is)
00270 {
00271   if (!is)
00272     return;
00273   short ver;
00274   vsl_b_read(is, ver);
00275   switch (ver)
00276   {
00277    default:
00278     assert(!"vsol_group_3d I/O version should be 1");
00279    case 1:
00280     vsl_b_read(is, *storage_);
00281 #if 0
00282     vcl_string type;
00283     while (true)
00284     {
00285       vsl_b_read(is, type);
00286       if (type=="vsol_point_3d")
00287       {
00288         vsol_point_3d_sptr p;
00289         vsl_b_read(is, p);
00290         if (p)
00291           storage_->push_back(p);
00292       }
00293       if (type=="vsol_line_3d")
00294       {
00295         vsol_line_3d_sptr l;
00296         vsl_b_read(is, l);
00297         if (l)
00298           storage_->push_back(l);
00299       }
00300       if (type=="vsol_conic_3d")
00301       {
00302         vsol_conic_3d_sptr cn;
00303         vsl_b_read(is, cn);
00304         if (cn)
00305           storage_->push_back(cn);
00306       }
00307       if (type=="vsol_polyline_3d")
00308       {
00309         vsol_polyline_3d_sptr pl;
00310         vsl_b_read(is, pl);
00311         if (pl)
00312           storage_->push_back(pl);
00313       }
00314       if (type=="vsol_polygon_3d")
00315       {
00316         vsol_polygon_3d_sptr pg;
00317         vsl_b_read(is, pg);
00318         if (pg)
00319           storage_->push_back(pg);
00320       }
00321       if (type=="vsol_triangle_3d")
00322       {
00323         vsol_triangle_3d_sptr t;
00324         vsl_b_read(is, t);
00325         if (t)
00326           storage_->push_back(t);
00327       }
00328       if (type=="vsol_rectangle_3d")
00329       {
00330         vsol_rectangle_3d_sptr r;
00331         vsl_b_read(is, r);
00332         if (r)
00333           storage_->push_back(r);
00334       }
00335       if (type=="vsol_group_3d")
00336       {
00337         vsol_group_3d_sptr g;
00338         vsl_b_read(is, g);
00339         if (g)
00340           storage_->push_back(g);
00341       }
00342       if (type=="vsol_group_3d_end")
00343         return;
00344     }
00345 #endif // 0
00346   }
00347 }
00348 
00349 //: Return IO version number;
00350 short vsol_group_3d::version() const
00351 {
00352   return 1;
00353 }
00354 
00355 //: Print an ascii summary to the stream
00356 void vsol_group_3d::print_summary(vcl_ostream &os) const
00357 {
00358   os << *this;
00359 }
00360 
00361 //external functions
00362 
00363 //: Binary save vsol_group_3d* to stream.
00364 void
00365 vsl_b_write(vsl_b_ostream &os, vsol_group_3d const* g)
00366 {
00367   if (!g)
00368     vsl_b_write(os,false); // Indicate null pointer stored
00369   else {
00370     vsl_b_write(os,true);  // Indicate non-null pointer stored
00371     g->b_write(os);
00372   }
00373 }
00374 
00375 //: Binary load vsol_group_3d* from stream.
00376 void
00377 vsl_b_read(vsl_b_istream &is, vsol_group_3d* &g)
00378 {
00379   delete g;
00380   bool not_null_ptr;
00381   vsl_b_read(is, not_null_ptr);
00382   if (not_null_ptr) {
00383     g = new vsol_group_3d();
00384     g->b_read(is);
00385   }
00386   else
00387     g = 0;
00388 }