contrib/gel/vtol/vtol_block.cxx
Go to the documentation of this file.
00001 // This is gel/vtol/vtol_block.cxx
00002 #include "vtol_block.h"
00003 //:
00004 // \file
00005 
00006 #include <vtol/vtol_edge.h>
00007 #include <vtol/vtol_two_chain.h>
00008 #include <vtol/vtol_macros.h>
00009 #include <vtol/vtol_list_functions.h>
00010 #include <vcl_vector.h>
00011 #include <vcl_cassert.h>
00012 
00013 //***************************************************************************
00014 // Initialization
00015 //***************************************************************************
00016 
00017 void vtol_block::link_inferior(vtol_two_chain_sptr inf)
00018 {
00019   vtol_topology_object::link_inferior(inf->cast_to_topology_object());
00020 }
00021 
00022 void vtol_block::unlink_inferior(vtol_two_chain_sptr inf)
00023 {
00024   vtol_topology_object::unlink_inferior(inf->cast_to_topology_object());
00025 }
00026 
00027 //---------------------------------------------------------------------------
00028 //: Constructor from a two-chain (inferior)
00029 //---------------------------------------------------------------------------
00030 vtol_block::vtol_block(vtol_two_chain_sptr const& faceloop)
00031 {
00032   link_inferior(faceloop);
00033 }
00034 
00035 //---------------------------------------------------------------------------
00036 //: Constructor from a list of two-chains.
00037 // The first two-chain is the outside boundary.
00038 // The remaining two-chains are considered holes inside the outside volume.
00039 //---------------------------------------------------------------------------
00040 vtol_block::vtol_block(two_chain_list const& faceloops)
00041 {
00042   if (faceloops.size()>0)
00043   {
00044     link_inferior(faceloops.front());
00045   }
00046 
00047   vtol_two_chain_sptr twoch=get_boundary_cycle();
00048 
00049   if (twoch)
00050     for (unsigned int i=1;i<faceloops.size();++i)
00051       twoch->link_chain_inferior(faceloops[i]);
00052 }
00053 
00054 //---------------------------------------------------------------------------
00055 //: Constructor from a list of faces
00056 //---------------------------------------------------------------------------
00057 vtol_block::vtol_block(face_list const& new_face_list)
00058 {
00059   link_inferior(new vtol_two_chain(new_face_list));
00060 }
00061 
00062 //---------------------------------------------------------------------------
00063 //: Pseudo-copy constructor. Deep copy.
00064 //---------------------------------------------------------------------------
00065 vtol_block::vtol_block(vtol_block_sptr const& other)
00066 {
00067   edge_list edgs;    other->edges(edgs);
00068   vertex_list verts; other->vertices(verts);
00069 
00070   topology_list newedges(edgs.size());
00071   topology_list newverts(verts.size());
00072 
00073   int i=0;
00074   for (vertex_list::iterator vi=verts.begin();vi!=verts.end();++vi,++i)
00075   {
00076     vtol_vertex_sptr v= *vi;
00077     newverts[i]=v->clone()->cast_to_topology_object();
00078     v->set_id(i);
00079   }
00080 
00081   int j=0;
00082   for (edge_list::iterator ei=edgs.begin();ei!=edgs.end();++ei,++j)
00083   {
00084     vtol_edge_sptr e = *ei;
00085 
00086     newedges[j]=newverts[e->v1()->get_id()]->cast_to_vertex()->new_edge(
00087                               newverts[e->v2()->get_id()]->cast_to_vertex())->cast_to_topology_object();
00088 
00089     e->set_id(j);
00090   }
00091 
00092   const topology_list *old2chains = other->inferiors();
00093 
00094   topology_list::const_iterator tci;
00095   for (tci=old2chains->begin();tci != old2chains->end();tci++)
00096   {
00097     vtol_two_chain_sptr new2ch=(*tci)->cast_to_two_chain()->copy_with_arrays(newverts,newedges);
00098     assert(*new2ch == *(*tci));
00099     link_inferior(new2ch);
00100   }
00101 }
00102 
00103 //---------------------------------------------------------------------------
00104 // Destructor
00105 //---------------------------------------------------------------------------
00106 vtol_block::~vtol_block()
00107 {
00108   unlink_all_inferiors();
00109 }
00110 
00111 //---------------------------------------------------------------------------
00112 //: Clone `this': creation of a new object and initialization
00113 // See Prototype pattern
00114 //---------------------------------------------------------------------------
00115 vsol_spatial_object_2d* vtol_block::clone() const
00116 {
00117   return new vtol_block(vtol_block_sptr(const_cast<vtol_block*>(this)));
00118 }
00119 
00120 //: outside boundary vertices
00121 
00122 vertex_list *vtol_block::outside_boundary_vertices()
00123 {
00124   vertex_list *result=new vertex_list();
00125   vcl_vector<vtol_vertex *> *ptr_list=outside_boundary_compute_vertices();
00126 
00127   // copy the lists
00128   vcl_vector<vtol_vertex*>::iterator i;
00129   for (i=ptr_list->begin();i!=ptr_list->end();++i)
00130     result->push_back(*i);
00131   delete ptr_list;
00132 
00133   return result;
00134 }
00135 
00136 vcl_vector<vtol_vertex *> *vtol_block::outside_boundary_compute_vertices()
00137 {
00138   OUTSIDE_BOUNDARY(vtol_vertex,two_chain,compute_vertices);
00139 }
00140 
00141 //: get vertex list
00142 
00143 vcl_vector<vtol_vertex *> *vtol_block::compute_vertices()
00144 {
00145   SEL_INF(vtol_vertex,compute_vertices);
00146 }
00147 
00148 //: get outside boundary zero chains
00149 
00150 zero_chain_list *vtol_block::outside_boundary_zero_chains()
00151 {
00152   zero_chain_list *result=new zero_chain_list();
00153   vcl_vector<vtol_zero_chain *> *ptr_list=outside_boundary_compute_zero_chains();
00154 
00155   // copy the lists
00156   vcl_vector<vtol_zero_chain*>::iterator i;
00157   for (i=ptr_list->begin();i!=ptr_list->end();++i)
00158     result->push_back(*i);
00159   delete ptr_list;
00160 
00161   return result;
00162 }
00163 
00164 
00165 vcl_vector<vtol_zero_chain *> *
00166 vtol_block::outside_boundary_compute_zero_chains()
00167 {
00168   OUTSIDE_BOUNDARY(vtol_zero_chain,two_chain,compute_zero_chains);
00169 }
00170 
00171 //: get zero chains
00172 
00173 vcl_vector<vtol_zero_chain *> *vtol_block::compute_zero_chains()
00174 {
00175  SEL_INF(vtol_zero_chain,compute_zero_chains);
00176 }
00177 
00178 //: outside boundary edges
00179 
00180 edge_list *vtol_block::outside_boundary_edges()
00181 {
00182   edge_list *result=new edge_list();
00183   vcl_vector<vtol_edge *> *ptr_list=outside_boundary_compute_edges();
00184 
00185   // copy the lists
00186   vcl_vector<vtol_edge*>::iterator i;
00187   for (i=ptr_list->begin();i!=ptr_list->end();++i)
00188     result->push_back(*i);
00189   delete ptr_list;
00190 
00191   return result;
00192 }
00193 
00194 //: outside boundary edges
00195 
00196 vcl_vector<vtol_edge *> *vtol_block::outside_boundary_compute_edges()
00197 {
00198   OUTSIDE_BOUNDARY(vtol_edge,two_chain,compute_edges);
00199 }
00200 
00201 //: get edges
00202 vcl_vector<vtol_edge *> *vtol_block::compute_edges()
00203 {
00204   SEL_INF(vtol_edge,compute_edges);
00205 }
00206 
00207 //: get outside boundary one chains
00208 
00209 one_chain_list *vtol_block::outside_boundary_one_chains()
00210 {
00211   one_chain_list *result=new one_chain_list;
00212   vcl_vector<vtol_one_chain *> *ptr_list=outside_boundary_compute_one_chains();
00213 
00214   vcl_vector<vtol_one_chain*>::iterator i;
00215   for (i=ptr_list->begin();i!=ptr_list->end();++i)
00216     result->push_back(*i);
00217   delete ptr_list;
00218 
00219   return result;
00220 }
00221 
00222 //: get outside boundary one chains
00223 
00224 vcl_vector<vtol_one_chain *> *
00225 vtol_block::outside_boundary_compute_one_chains()
00226 {
00227   OUTSIDE_BOUNDARY(vtol_one_chain,two_chain,compute_one_chains);
00228 }
00229 
00230 //: get the one chains
00231 vcl_vector<vtol_one_chain *> *vtol_block::compute_one_chains()
00232 {
00233   SEL_INF(vtol_one_chain,compute_one_chains);
00234 }
00235 
00236 //: get the outside boundary faces
00237 
00238 face_list *vtol_block::outside_boundary_faces()
00239 {
00240   face_list *result=new face_list();
00241   vcl_vector<vtol_face *> *ptr_list=outside_boundary_compute_faces();
00242 
00243   vcl_vector<vtol_face*>::iterator i;
00244   for (i=ptr_list->begin();i!=ptr_list->end();++i)
00245     result->push_back(*i);
00246   delete ptr_list;
00247 
00248   return result;
00249 }
00250 
00251 //: get the outside boundary faces
00252 
00253 vcl_vector<vtol_face *> *vtol_block::outside_boundary_compute_faces()
00254 {
00255   OUTSIDE_BOUNDARY(vtol_face,two_chain,compute_faces);
00256 }
00257 
00258 //: get the faces
00259 vcl_vector<vtol_face *> *vtol_block::compute_faces()
00260 {
00261   SEL_INF(vtol_face,compute_faces);
00262 }
00263 
00264 //: get the outside boundary two chains
00265 
00266 two_chain_list *vtol_block::outside_boundary_two_chains()
00267 {
00268   two_chain_list *result=new two_chain_list();
00269   vcl_vector<vtol_two_chain *> *ptr_list=outside_boundary_compute_two_chains();
00270 
00271   vcl_vector<vtol_two_chain*>::iterator i;
00272   for (i=ptr_list->begin();i!=ptr_list->end();++i)
00273     result->push_back(*i);
00274   delete ptr_list;
00275 
00276   return result;
00277 }
00278 
00279 //: get the outside boundary two chains
00280 
00281 vcl_vector<vtol_two_chain *> *
00282 vtol_block::outside_boundary_compute_two_chains()
00283 {
00284   OUTSIDE_BOUNDARY(vtol_two_chain,two_chain,compute_two_chains);
00285 }
00286 
00287 //: get the two chains
00288 
00289 vcl_vector<vtol_two_chain *> *vtol_block::compute_two_chains()
00290 {
00291   SEL_INF(vtol_two_chain,compute_two_chains);
00292 }
00293 
00294 //: get blocks
00295 vcl_vector<vtol_block *> *vtol_block::compute_blocks()
00296 {
00297   LIST_SELF(vtol_block);
00298 }
00299 
00300 
00301 // ******************************************************
00302 //
00303 //    Operators Functions
00304 //
00305 
00306 // This is hardly an equality test...but we`ll leave it for now....pav
00307 // June 1992.
00308 
00309 bool vtol_block::operator==(const vtol_block &other) const
00310 {
00311   if (this==&other)
00312     return true;
00313 
00314   if (numinf()!=other.numinf())
00315     return false;
00316 
00317   topology_list::const_iterator bi1=inferiors()->begin();
00318   topology_list::const_iterator bi2=other.inferiors()->begin();
00319   for (; bi1!=inferiors()->end(); ++bi1,++bi2)
00320   {
00321     vtol_two_chain_sptr twoch1=(*bi1)->cast_to_two_chain();
00322     vtol_two_chain_sptr twoch2=(*bi2)->cast_to_two_chain();
00323 
00324     if (!(*twoch1 == *twoch2))
00325       return false;
00326   }
00327 
00328   return true;
00329 }
00330 
00331 //: spatial object equality
00332 
00333 bool vtol_block::operator==(const vsol_spatial_object_2d& obj) const
00334 {
00335   return
00336    obj.cast_to_topology_object() &&
00337    obj.cast_to_topology_object()->cast_to_block() &&
00338    *this == *obj.cast_to_topology_object()->cast_to_block();
00339 }
00340 
00341 //: get a hole cycle
00342 two_chain_list *vtol_block::hole_cycles() const
00343 {
00344   two_chain_list *result=new two_chain_list;
00345 
00346   topology_list::const_iterator ti;
00347   for (ti=inferiors_.begin();ti!=inferiors_.end();++ti)
00348   {
00349     two_chain_list *templist=(*ti)->cast_to_two_chain()->inferior_two_chains();
00350     for (two_chain_list::iterator ii=templist->begin();ii!=templist->end();++ii)
00351       result->push_back(*ii);
00352     delete templist;
00353   }
00354   return result;
00355 }
00356 
00357 //: get the boundary cycle
00358 vtol_two_chain_sptr vtol_block::get_boundary_cycle()
00359 {
00360   return
00361     (inferiors_.size() > 0)
00362     ? inferiors_[0]->cast_to_two_chain()
00363     : 0;
00364 }
00365 
00366 //: add a hole cycle
00367 
00368 bool vtol_block::add_hole_cycle(vtol_two_chain_sptr new_hole)
00369 {
00370   vtol_two_chain_sptr twoch=get_boundary_cycle();
00371   if (! twoch) return false;
00372   twoch->link_chain_inferior(new_hole);
00373   return true;
00374 }
00375 
00376 // ******************************************************
00377 //
00378 //    Print Functions
00379 //
00380 
00381 
00382 //: print data
00383 
00384 void vtol_block::print(vcl_ostream &strm) const
00385 {
00386   strm<<"<vtol_block "<<inferiors()->size()<<"  "<<(void const*)this<<">\n";
00387 }
00388 
00389 void vtol_block::describe(vcl_ostream &strm,
00390                           int blanking) const
00391 {
00392   for (int i=0; i<blanking; ++i) strm << ' ';
00393   print(strm);
00394   describe_inferiors(strm,blanking);
00395   describe_superiors(strm,blanking);
00396 }