contrib/gel/vtol/vtol_topology_io.cxx
Go to the documentation of this file.
00001 // This is gel/vtol/vtol_topology_io.cxx
00002 #include "vtol_topology_io.h"
00003 
00004 #include <vcl_ostream.h>
00005 
00006 #include <vtol/vtol_list_functions.h>
00007 #include <vtol/vtol_vertex_2d.h>
00008 #include <vtol/vtol_zero_chain.h>
00009 #include <vtol/vtol_edge_2d.h>
00010 #include <vtol/vtol_one_chain.h>
00011 #include <vtol/vtol_face_2d.h>
00012 #include <vtol/vtol_two_chain.h>
00013 #include <vtol/vtol_block.h>
00014 
00015 
00016 // default constructor
00017 
00018 vtol_topology_io::vtol_topology_io()
00019 {
00020 }
00021 
00022 // default destructor
00023 vtol_topology_io::~vtol_topology_io()
00024 {
00025 }
00026 
00027 
00028 // write out a list of topology objects
00029 
00030 void vtol_topology_io::write(vcl_list<vtol_topology_object_sptr> &topo_objects,
00031                              vcl_ostream &strm)
00032 {
00033   // write out the dtd table
00034 
00035   strm << "<?xml version=\"1.0\" standalone=\"yes\"?>" << vcl_endl;
00036 
00037   write_dtd(strm);
00038 
00039   strm << "<vxl>" << vcl_endl;
00040 
00041   // ****** get lists of all the topology types *******
00042 
00043   // start off with getting a list of all the vertices
00044 
00045   vcl_list<vtol_topology_object_sptr> vertices;
00046   vcl_list<vtol_topology_object_sptr> zero_chains;
00047   vcl_list<vtol_topology_object_sptr> edges;
00048   vcl_list<vtol_topology_object_sptr> one_chains;
00049   vcl_list<vtol_topology_object_sptr> faces;
00050   vcl_list<vtol_topology_object_sptr> two_chains;
00051   vcl_list<vtol_topology_object_sptr> blocks;
00052 
00053   vcl_list<vtol_topology_object_sptr>::iterator ti;
00054   for (ti=topo_objects.begin();ti!= topo_objects.end();ti++)
00055   {
00056     // *** get the vertices
00057 
00058     vertex_list vl; (*ti)->vertices(vl);
00059 
00060     // copy vl into vertices
00061 
00062     vertex_list::iterator vi;
00063     for (vi=vl.begin();vi!=vl.end();vi++)
00064     {
00065       vertices.push_back((*vi)->cast_to_vertex());
00066     }
00067 
00068     // *** get the zero chains
00069 
00070     zero_chain_list zcl; (*ti)->zero_chains(zcl);
00071 
00072     // copy zcl into the zero_chains
00073 
00074     zero_chain_list::iterator zci;
00075     for (zci=zcl.begin();zci!=zcl.end();zci++)
00076     {
00077       zero_chains.push_back((*zci)->cast_to_zero_chain());
00078     }
00079 
00080     // *** get the edges
00081 
00082     edge_list el; (*ti)->edges(el);
00083 
00084     // copy el into the edges
00085 
00086     edge_list::iterator ei;
00087     for (ei=el.begin();ei!=el.end();ei++)
00088     {
00089       edges.push_back((*ei)->cast_to_edge());
00090     }
00091 
00092     // *** get the one chains
00093 
00094     one_chain_list ocl; (*ti)->one_chains(ocl);
00095 
00096     // copy ocl into the one_chains
00097 
00098     one_chain_list::iterator oci;
00099     for (oci=ocl.begin();oci!=ocl.end();oci++)
00100     {
00101       one_chains.push_back((*oci)->cast_to_one_chain());
00102     }
00103 
00104     // *** get the faces
00105 
00106     face_list fl; (*ti)->faces(fl);
00107 
00108     // copy fl into the faces
00109 
00110     face_list::iterator fi;
00111     for (fi=fl.begin();fi!=fl.end();fi++)
00112     {
00113       faces.push_back((*fi)->cast_to_face());
00114     }
00115 
00116     // *** get the two chains
00117 
00118     two_chain_list tcl; (*ti)->two_chains(tcl);
00119 
00120     // copy tcl into the two_chains
00121 
00122     two_chain_list::iterator tci;
00123     for (tci=tcl.begin();tci!=tcl.end();tci++)
00124     {
00125       two_chains.push_back((*tci)->cast_to_two_chain());
00126     }
00127 
00128     // *** get the blocks
00129 
00130     block_list bl; (*ti)->blocks(bl);
00131 
00132     // copy bl into the blocks
00133 
00134     block_list::iterator bi;
00135     for (bi=bl.begin();bi!=bl.end();bi++)
00136     {
00137       blocks.push_back((*bi)->cast_to_block());
00138     }
00139   }
00140 
00141   // ******* get rid of duplicates *********
00142 
00143   tagged_union(&vertices);
00144   tagged_union(&zero_chains);
00145   tagged_union(&edges);
00146   tagged_union(&one_chains);
00147   tagged_union(&faces);
00148   tagged_union(&two_chains);
00149   tagged_union(&blocks);
00150 
00151   // ******** write the topology objects
00152   //
00153 
00154   int id =1;
00155 
00156   for (ti=vertices.begin();ti!= vertices.end();ti++)
00157   {
00158     vtol_vertex *v= (*ti)->cast_to_vertex();
00159     if (v)
00160     {
00161       vtol_vertex_2d *v2d = v->cast_to_vertex_2d();
00162       if (v2d)
00163       {
00164         // set the id of the vertex
00165         v2d->set_id(id);
00166         id++;
00167         // write the vertex
00168         write_vertex_2d(v2d,strm);
00169       }
00170     }
00171   }
00172 
00173   id =1;
00174 
00175   for (ti=zero_chains.begin();ti!= zero_chains.end();ti++)
00176   {
00177     vtol_zero_chain *zc= (*ti)->cast_to_zero_chain();
00178     if (zc)
00179     {
00180       zc->set_id(id);
00181       id++;
00182       // write the zero chain
00183       write_zero_chain(zc,strm);
00184     }
00185   }
00186 
00187   id =1;
00188 
00189   for (ti=edges.begin();ti!= edges.end();ti++)
00190   {
00191     vtol_edge *e= (*ti)->cast_to_edge();
00192     if (e)
00193     {
00194       vtol_edge_2d *e2d = e->cast_to_edge_2d();
00195       if (e2d)
00196       {
00197         // set the id of the edge
00198         e2d->set_id(id);
00199         id++;
00200         // write the edge
00201 
00202         write_edge_2d(e2d,strm);
00203       }
00204     }
00205   }
00206 
00207   id =1;
00208 
00209   for (ti=one_chains.begin();ti!= one_chains.end();ti++)
00210   {
00211     vtol_one_chain *oc= (*ti)->cast_to_one_chain();
00212     if (oc)
00213     {
00214       oc->set_id(id);
00215       id++;
00216       // write the one_chain
00217       write_one_chain(oc,strm);
00218     }
00219   }
00220 
00221   id=1;
00222 
00223   for (ti=faces.begin();ti!= faces.end();ti++)
00224   {
00225     vtol_face *f= (*ti)->cast_to_face();
00226     if (f)
00227     {
00228       vtol_face_2d *f2d = f->cast_to_face_2d();
00229       if (f2d)
00230       {
00231         // set the id of the face
00232         f2d->set_id(id);
00233         id++;
00234         // write the face
00235         write_face_2d(f2d,strm);
00236       }
00237     }
00238   }
00239 
00240   id =1;
00241 
00242   for (ti=two_chains.begin();ti!= two_chains.end();ti++)
00243   {
00244     vtol_two_chain *tc= (*ti)->cast_to_two_chain();
00245     if (tc)
00246     {
00247       tc->set_id(id);
00248       id++;
00249       // write the two_chain
00250       write_two_chain(tc,strm);
00251     }
00252   }
00253 
00254   id =1;
00255 
00256   for (ti=blocks.begin();ti!= blocks.end();ti++)
00257   {
00258     vtol_block *b= (*ti)->cast_to_block();
00259     if (b)
00260     {
00261       b->set_id(id);
00262       id++;
00263       // write the block
00264       write_block(b,strm);
00265     }
00266   }
00267 
00268   strm << "</vxl>" << vcl_endl;
00269 }
00270 
00271 
00272 // write out a 2d vertex
00273 
00274 void vtol_topology_io::write_vertex_2d(vtol_vertex_2d_sptr const& v,
00275                                        vcl_ostream &strm)
00276 {
00277   // ok lets write this vertex out
00278   // it looks something like this
00279   //    <vxl_vertex_2d id="v1">
00280   //       <vxl_point_2d id="p1" x="0" y="0"/>
00281   //    </vxl_vertex_2d>
00282 
00283   strm << "<vxl_vertex_2d id=\"v2d" << v->get_id() << "\">" << vcl_endl
00284        << " <vxl_point_2d x=\""<< v->x() << '"' << " y=\"" << v->y() << "\"/>" << vcl_endl
00285        << "</vxl_vertex_2d>" << vcl_endl;
00286 }
00287 
00288 void vtol_topology_io::write_zero_chain(vtol_zero_chain_sptr const& zc,
00289                                         vcl_ostream &strm)
00290 {
00291   strm << "<vxl_zero_chain_2d id=\"zc_" << zc->get_id() << "\">" << vcl_endl;
00292 
00293   // write the inferiors
00294 
00295   const topology_list *inferiors = zc->inferiors();
00296 
00297   topology_list::const_iterator i;
00298   for (i=inferiors->begin();i!=inferiors->end();++i)
00299   {
00300     strm << " <vxl_vertex_2d_sptr id=\"v2d" << (*i)->get_id() << "\"/>" << vcl_endl;
00301   }
00302 
00303   strm << "</vxl_zero_chain_2d>" << vcl_endl;
00304 }
00305 
00306 void vtol_topology_io::write_edge_2d(vtol_edge_2d_sptr const& e, vcl_ostream &strm)
00307 {
00308   strm << "<vxl_edge_2d id=\"e2d" << e->get_id() << "\">" << vcl_endl;
00309 
00310   // write the inferiors
00311 
00312   const topology_list *inferiors = e->inferiors();
00313 
00314   topology_list::const_iterator i;
00315   for (i=inferiors->begin();i!=inferiors->end();++i)
00316   {
00317     strm << " <vxl_zero_chain_2d_sptr id=\"zc_" << (*i)->get_id() << "\"/>" << vcl_endl;
00318   }
00319 
00320   strm << "</vxl_edge_2d>" << vcl_endl;
00321 }
00322 
00323 
00324 void vtol_topology_io::write_one_chain(vtol_one_chain_sptr const& oc, vcl_ostream &strm)
00325 {
00326   strm << "<vxl_one_chain_2d id=\"oc_" << oc->get_id() << "\">" << vcl_endl;
00327 
00328   // write the inferiors
00329 
00330   const topology_list *inferiors = oc->inferiors();
00331 
00332   topology_list::const_iterator i;
00333   for (i=inferiors->begin();i!=inferiors->end();++i)
00334   {
00335     vtol_edge *e = (*i)->cast_to_edge();
00336     if (e)
00337     {
00338       if (e->cast_to_edge_2d())
00339       {
00340         // determine the direction of the edge
00341         if (oc->direction(*e) ==1)
00342         {
00343           strm << " <vxl_edge_2d_sptr id=\"e2d" << (*i)->get_id() << "\"/>" << vcl_endl;
00344         }
00345         else
00346         {
00347           // we have to signal a flip
00348           strm << " <vxl_edge_2d_sptr id=\"e2d" << (*i)->get_id() << "\"/><vxl_flip/>"  << vcl_endl;
00349         }
00350       }
00351     }
00352   }
00353 
00354   strm << "</vxl_one_chain_2d>" << vcl_endl;
00355 }
00356 
00357 
00358 void vtol_topology_io::write_face_2d(vtol_face_2d_sptr const& f, vcl_ostream &strm)
00359 {
00360   strm << "<vxl_face_2d id=\"f2d" << f->get_id() << "\">" << vcl_endl;
00361 
00362   // write the inferiors
00363 
00364   const topology_list *inferiors = f->inferiors();
00365 
00366   topology_list::const_iterator i;
00367   for (i=inferiors->begin();i!=inferiors->end();++i)
00368   {
00369     strm << " <vxl_one_chain_2d_sptr id=\"oc_" << (*i)->get_id() << "\"/>" << vcl_endl;
00370   }
00371 
00372   strm << "</vxl_face_2d>" << vcl_endl;
00373 }
00374 
00375 
00376 void vtol_topology_io::write_two_chain(vtol_two_chain_sptr const& tc, vcl_ostream &strm)
00377 {
00378   strm << "<vxl_two_chain_2d id=\"tc_" << tc->get_id() << "\">" << vcl_endl;
00379 
00380   // write the inferiors
00381 
00382   const topology_list *inferiors = tc->inferiors();
00383 
00384   topology_list::const_iterator i;
00385   for (i=inferiors->begin();i!=inferiors->end();++i)
00386   {
00387     vtol_face *f = (*i)->cast_to_face();
00388     if (f)
00389     {
00390       if (f->cast_to_face_2d())
00391       {
00392         // determine the direction of the edge
00393         if (tc->direction(*f) ==1)
00394         {
00395           strm << " <vxl_face_2d_sptr id=\"f2d" << (*i)->get_id() << "\"/>" << vcl_endl;
00396         }
00397         else
00398         {
00399           // we have to signal a flip
00400           strm << " <vxl_face_2d_sptr id=\"f2d" << (*i)->get_id() << "\"/><vxl_flip/>"  << vcl_endl;
00401         }
00402       }
00403     }
00404   }
00405 
00406   strm << "</vxl_two_chain_2d>" << vcl_endl;
00407 }
00408 
00409 
00410 void vtol_topology_io::write_block(vtol_block_sptr const& b, vcl_ostream &strm)
00411 {
00412   strm << "<vxl_block_2d id=\"b__" << b->get_id() << "\">" << vcl_endl;
00413 
00414   // write the inferiors
00415 
00416   const topology_list *inferiors = b->inferiors();
00417 
00418   topology_list::const_iterator i;
00419   for (i=inferiors->begin();i!=inferiors->end();++i)
00420   {
00421     strm << " <vxl_two_chain_2d_sptr id=\"tc_" << (*i)->get_id() << "\"/>" << vcl_endl;
00422   }
00423 
00424   strm << "</vxl_block_2d>" << vcl_endl;
00425 }
00426 
00427 void vtol_topology_io::write_dtd(vcl_ostream &strm)
00428 {
00429   #include <vtol/vtol_dtd.h>
00430 
00431   strm << vtol_dtd0
00432        << vtol_dtd1
00433        << vtol_dtd2
00434        << vtol_dtd3;
00435 }