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