contrib/oxl/osl/osl_save_topology.cxx
Go to the documentation of this file.
00001 // This is oxl/osl/osl_save_topology.cxx
00002 #ifdef VCL_NEEDS_PRAGMA_INTERFACE
00003 #pragma implementation
00004 #endif
00005 //:
00006 // \file
00007 // \author fsm
00008 
00009 #include "osl_save_topology.h"
00010 #include <vcl_fstream.h>
00011 
00012 void osl_save_topology(vcl_ostream &f, vcl_list<osl_edge*> const &es, vcl_list<osl_vertex*> const &vs)
00013 {
00014   unsigned char* vid = 0;
00015   char const *name = "fred";
00016 
00017   // list of all vertices
00018   vcl_list<osl_vertex*> stashed;
00019 
00020   // first the explicit vertices
00021   for (vcl_list<osl_vertex*>::const_iterator i=vs.begin(); i!=vs.end(); ++i) {
00022     (*i)->stash_add(name, (void*)++vid);
00023     stashed.push_front(*i);
00024   }
00025 
00026   // then the edge vertices
00027   for (vcl_list<osl_edge*>::const_iterator i=es.begin(); i!=es.end(); ++i) {
00028     osl_edge *e = *i;
00029     if (! e->GetV1()->stash_retrieve(name)) {
00030       e->GetV1()->stash_add(name, (void*)++vid);
00031       stashed.push_front(e->GetV1());
00032     }
00033     if (! e->GetV2()->stash_retrieve(name)) {
00034       e->GetV2()->stash_add(name, (void*)++vid);
00035       stashed.push_front(e->GetV2());
00036     }
00037   }
00038 
00039   // version string
00040   f << "osl_save_topology 1.0\n\n";
00041 
00042   // write the vertices :
00043   f << stashed.size() << " vertices\n";
00044   for (vcl_list<osl_vertex*>::iterator i=stashed.begin(); i!=stashed.end(); ++i) {
00045     void* stashid = (void*) (*i)->stash_retrieve(name);
00046     f << stashid << ' ' << (*i)->GetId() << ' ' << (*i)->GetX() << ' ' << (*i)->GetY() << vcl_endl;
00047   }
00048   f << vcl_endl;
00049 
00050   // write the edges :
00051   f << es.size() << " edges\n";
00052   for (vcl_list<osl_edge*>::const_iterator i=es.begin(); i!=es.end(); ++i) {
00053     void* stashid1 = (void*) (*i)->GetV1()->stash_retrieve(name);
00054     void* stashid2 = (void*) (*i)->GetV2()->stash_retrieve(name);
00055     f << stashid1 << ' ' << stashid2 << vcl_endl; // endpoints
00056     f << (*i)->GetId() << vcl_endl; // id of edge
00057     (*i)->osl_edgel_chain::write_ascii(f);
00058   }
00059 
00060   // remove the stashes :
00061   for (vcl_list<osl_vertex*>::iterator i=stashed.begin(); i!=stashed.end(); ++i)
00062     (*i)->stash_remove(name);
00063 
00064   // done
00065 }
00066 
00067 void osl_save_topology(char const *f, vcl_list<osl_edge*> const &e, vcl_list<osl_vertex*> const &v)
00068 {
00069   vcl_ofstream file(f);
00070   osl_save_topology(file, e, v);
00071 }
00072 
00073 void osl_save_topology(char const *f, vcl_list<osl_edge*> const &e)
00074 {
00075   osl_save_topology(f, e, vcl_list<osl_vertex*>());
00076 }
00077 
00078 void osl_save_topology(vcl_ostream &s, vcl_list<osl_edge*> const &e)
00079 {
00080   osl_save_topology(s, e, vcl_list<osl_vertex*>());
00081 }