shape_doc 0.1
/Users/mourrain/Devel/mmx/shape/include/shape/topology2d.hpp
Go to the documentation of this file.
00001 /*****************************************************************************
00002  * M a t h e m a g i x
00003  *****************************************************************************
00004  * TopologyCurve
00005  * 2008-05-16
00006  * Bernard Mourrain & Julien Wintz
00007  *****************************************************************************
00008  *               Copyright (C) 2008 INRIA Sophia-Antipolis
00009  *****************************************************************************
00010  * Comments :
00011  ****************************************************************************/
00012 # ifndef shape_topology2d_hpp
00013 # define shape_topology2d_hpp
00014 
00015 # include <shape/vertex.hpp>
00016 # include <shape/cell.hpp>
00017 # include <shape/kdtree.hpp>
00018 # include <shape/graph.hpp>
00019 # include <shape/list.hpp>
00020 # include <shape/curve.hpp>
00021 # include <shape/algebraic_curve.hpp>
00022 # include <shape/parametric_curve.hpp>
00023 # include <shape/semialgebraic_curve.hpp>
00024 # include <shape/cell2d_list.hpp>
00025 # include <shape/cell2d_intersection.hpp>
00026 # include <shape/cell2d_algebraic_curve.hpp>
00027 # include <shape/cell2d_parametric_curve.hpp>
00028 # include <shape/cell2d_semialgebraic_curve.hpp>
00029 //# include <shape/cell2d_voronoi_site2d.hpp>
00030 //# include <shape/cell2d_voronoi_diagram.hpp>
00031 # include <shape/topology.hpp>
00032 
00033 # include <stack>
00034 # define  STACK std::stack<Cell2d*>
00035 
00036 
00037 # define TMPL template<class C, class V>
00038 # define TMPL1 template<class V>
00039 # define REF REF_OF(V)
00040 
00041 //# define Shape SHAPE_OF(V)
00042 # define AlgebraicCurve algebraic_curve<C,V>
00043 # define SemiAlgebraicCurve semialgebraic_curve<C,REF>
00044 # define ParametricCurve parametric_curve<C,REF>
00045 //# define VoronoiSite2d voronoi_site2d<C,REF>
00046 # define Cell2dAlgebraicCurve cell2d_algebraic_curve<C,REF>
00047 # define Cell2dParametricCurve cell2d_parametric_curve<C,REF>
00048 # define Cell2dSemiAlgebraicCurve cell2d_semialgebraic_curve<C,REF>
00049 //# define Cell2dVoronoiSite cell2d_voronoi_site2d<C,REF>
00050 //# define Cell2dVoronoiDiagram cell2d_voronoi_diagram<C,REF>
00051 # define Cell2dList cell2d_list<C,REF>
00052 # define Cell2dInter cell2d_intersection<C,REF>
00053 # define SELF   topology2d<C,V>
00054 # define Viewer viewer<axel,V>
00055 # undef Cell
00056 //====================================================================
00057 namespace mmx {
00058 namespace shape {
00059 
00060 
00061 // TMPL struct topology2d;
00062 
00063 // TMPL struct with_topology2d { 
00064 //   typedef topology2d<K> Topology2d; 
00065 // };
00066 
00067 // TMPL struct topology2d_def 
00068 //   :public topology_def<K>
00069 //   ,public with_cell2d<K>
00070 //   ,public with_curve<K>
00071 //   ,public with_topology2d<K>
00072 // {};
00073 
00074 TMPL1 class curve ;
00075 TMPL class cell2d_parametric_curve ;
00076 TMPL class cell2d_algebraic_curve;
00077 TMPL class cell2d_list;
00078 
00079                 //template <class Object, class CELL> class octree_node;
00080                 //template <class Object, class CELL> class quadtree;
00081  
00082 template<class C, class V=default_env>
00083 class topology2d: public topology<C,V> {
00084 public:
00085   typedef topology<C,V> Topology;
00086   typedef typename topology<C,V>::Point       Point;  
00087   typedef typename topology<C,V>::Edge        Edge;
00088   typedef typename topology<C,V>::Face        Face;
00089   typedef typename topology<C,V>::BoundingBox BoundingBox;
00090   typedef cell<C,V>         Cell;
00091   typedef cell2d<C,REF>     Cell2d;
00092   typedef curve<REF>       Curve;
00093   
00094   typedef node<Curve*, Cell*>               Node;
00095   typedef kdtree<Curve*, Cell*>             Kdtree;
00096 
00097   
00098 public:
00099   topology2d(const BoundingBox& bx);
00100   topology2d(Curve * curve) ;
00101   ~topology2d(void) ;
00102 
00103   void run(void) ;
00104   
00105   virtual void insert(Point*);
00106   virtual void insert(Edge *);
00107   virtual void insert(Point * p1, Point * p2);
00108 
00109   //    virtual void insert(BoundingBox*, bool);
00110   virtual void insert_singular(Point*);
00111 
00112 protected:
00113   bool is_regular (Cell * cell) ;
00114   bool insert_regular(Cell * cell) ;
00115   bool singularity(Cell * cell) ;
00116   bool   subdivide(Cell * cell, Node * node) ;
00117   
00118   std::list<Node *> m_nodes ;
00119 
00120 private:
00121   Kdtree         *  m_tree ;
00122   
00124   static bool active_cell(gNode<Cell2d*>* v)
00125   {
00126     return (v->get_aux()>0 );
00127   }
00128 
00129   static void ccw_walk(gNode<Cell2d*>* v)
00130   {
00131     //
00132   }
00133 
00134 public:
00135 
00136   Seq<Point*>         m_vertices;
00137   Seq<Edge *>         m_edges;
00138   Seq<Face *>         m_faces;
00139   
00140   Graph<Point*>       m_graph;  // topological graph
00141   
00142   Graph<Cell2d*>      m_leaves; // active leaves graph
00143   Graph<Cell2d*>      b_leaves; // border leaves CCW loop
00144   
00145   int nbv() const { return m_vertices.size();}
00146   int nbe() const { return m_edges.size();}
00147   
00148   Point*  vertices(int i) const { return m_vertices[i] ; }
00149   Seq<Edge*> edges(void)  const { return m_edges ; }
00150   Edge*      edges(int i) const { return m_edges[i] ; }
00151   
00152   Seq<Point*>         m_specials;
00153 };
00154 
00155 //--------------------------------------------------------------------
00156 // TMPL struct cell2d_factory ;
00157 // TMPL struct cell2d_factory_def
00158 //   :public with_algebraic_curve<K>
00159 //   ,public with_cell2d_algebraic_curve<K>
00160 //   ,public with_cell2d_semialgebraic_curve<K>
00161 // //  ,public with_cell2d_voronoi_site2d<K>
00162 //   ,public with_parametric_curve<K>
00163 //   ,public with_cell2d_parametric_curve<K>
00164 // {};
00165 
00166 # define Cell2dFactory cell2d_factory<C,V>
00167 TMPL 
00168 struct cell2d_factory {
00169 public:
00170   typedef typename SHAPE_OF(V)     Shape;  
00171   typedef bounding_box<C,REF>      BoundingBox;
00172   typedef cell<C,REF>                    Cell ;
00173 
00174   static Cell2dFactory * instance(void) {
00175 
00176         if(!m_instance)
00177             m_instance = new Cell2dFactory ;
00178 
00179         return m_instance ;
00180     }
00181 
00182   Cell * create(Shape * curve, const BoundingBox& bx) {
00183     // std::cout<<"create"<<std::endl;
00184         
00185     Cell* cell = NULL ;
00186     if(SemiAlgebraicCurve * isemialg = dynamic_cast<SemiAlgebraicCurve *>(curve))
00187       {
00188         cell = new Cell2dSemiAlgebraicCurve(isemialg,bx);
00189         //std::cout<<"semialg"<<std::endl;
00190       }
00191     //else if(VoronoiSite2d * vcurve = dynamic_cast<VoronoiSite2d *>(curve))
00192     //  {
00193     //  cell = new Cell2dVoronoiSite(vcurve,bx);
00194     //  //std::cout<<"voronoi site"<<std::endl;
00195     //}
00196     else if(AlgebraicCurve * icurve = dynamic_cast<AlgebraicCurve *>(curve))
00197       {
00198         cell = new Cell2dAlgebraicCurve(icurve,bx);
00199         //std::cout<<"algcurve"<<std::endl;
00200       }
00201     else if(ParametricCurve * pcurve = dynamic_cast<ParametricCurve *>(curve))
00202       {
00203         cell = new Cell2dParametricCurve(pcurve,bx);
00204         //std::cout<<"parametric curve"<<std::endl;
00205       }
00206 
00207     if(cell==NULL) std::cout<<"no dynamic cast cell2d_factory"<<std::endl;
00208     return cell ;
00209 
00210   }
00211 
00212   Cell * create(const Seq<Shape*>& s, const BoundingBox& bx) {
00213     Cell * cell = NULL ;
00214   
00215     if (s.size()==0) return NULL;
00216     if (s.size()==1)  return create(s[0],bx);
00217 
00218     if( dynamic_cast<SemiAlgebraicCurve *>(s[0]) )
00219     {
00220       Cell2dInter* l= new Cell2dInter(bx);
00221       //std::cout<<"semialg intersection"<<std::endl;
00222       foreach(Shape* o, s) l->push_back(create(o,bx)) ;
00223       cell = l;
00224     } 
00225     //else if ( dynamic_cast<VoronoiSite2d *>(s[0]) ) {
00226     //  Cell2dVoronoiDiagram* l = new Cell2dVoronoiDiagram(bx);
00227     //  //std::cout<<"voronoi diagram"<<std::endl;
00228     // foreach(Shape* o, s) l->push_back(create(o,bx)) ;
00229     //  cell = l;
00230     //}
00231     else {
00232       Cell2dList* l= new Cell2dList(bx);
00233       // std::cout<<"cell-list"<<std::endl;
00234       foreach(Shape* o, s) l->push_back(create(o,bx)) ;
00235       cell = l;
00236     }
00237     
00238     return cell;
00239   }
00240   
00241 private:
00242     cell2d_factory(void) {} ;
00243 
00244 private:
00245     static Cell2dFactory * m_instance;
00246 } ;
00247 
00248 TMPL Cell2dFactory*  Cell2dFactory::m_instance = NULL;
00249 
00250 //--------------------------------------------------------------------
00251 
00252 TMPL 
00253 SELF::topology2d(const BoundingBox& bx) : Topology(bx) {
00254   m_tree = new Kdtree ;
00255 }
00256 
00257 TMPL
00258 SELF::topology2d(Curve * object) : Topology(object->boundingBox()) {
00259   this->m_objects.push_back(object);
00260   m_tree = new Kdtree ;
00261 }
00262 
00263 TMPL
00264 SELF::~topology2d(void) {
00265   delete m_tree ;
00266 }
00267 
00268 TMPL void 
00269 SELF::run() {
00270   
00271   Node* root = m_tree->root() ;
00272 
00273   std::cout<<"Running, objs="<<this->m_objects.size() << std::endl;
00274 
00275   Cell* cl = Cell2dFactory::instance()->create(this->m_objects,this->m_bbx);
00276 
00277   root->set_cell( (Cell*)cl ) ;
00278   this->m_nodes.push_back(root) ;
00279 
00280   //std::cout<<"RUNing "<< cl <<std::endl;
00281 
00282   // double maxsz = this->m_maxprec*m_tree->root()->get_cell()->size();
00283   // double minsz = this->m_minprec*m_tree->root()->get_cell()->size();
00284   double maxsz = this->m_maxprec;
00285   double minsz = this->m_minprec;
00286 
00287   while(!m_nodes.empty()  ) {   //      
00288     Node * node = m_nodes.front() ;
00289     Cell   * cl = node->get_cell() ;
00290     Cell2d * cl2= dynamic_cast<Cell2d*>(cl);
00291 
00292     //std::cout<<"node "<<cl <<std::endl;
00293 
00294     if(cl->is_active()) {
00295 
00296       //std::cout<<"active.. "<<std::endl;
00297 
00298       if(cl->size() > maxsz) 
00299         { 
00300           this->subdivide(cl, node) ;
00301         }
00302       else if(this->is_regular(cl) )// && cl->is_intersected())
00303       { 
00304         //std::cout<<"ins regular "<<std::endl;
00305         this->insert_regular(cl) ;
00306         //std::cout<<"ok "<<std::endl;
00307         cl2->m_gnode= 
00308           this->m_leaves.push_vertex(cl2);
00309         if (  cl2->is_border() )
00310           this->b_leaves.push_vertex(cl2);       
00311       }
00312       else if(cl->size() > minsz)
00313       { 
00314         //std::cout<<"subdivide "<<std::endl;
00315         this->subdivide(cl, node) ;
00316       }
00317       else
00318       { 
00319         //std::cout<<"singularity "<<std::endl;
00320         this->singularity(cl) ;
00321         cl2->m_gnode= 
00322           this->m_leaves.push_vertex(cl2);
00323         if (  cl2->is_border() )
00324           this->b_leaves.push_vertex(cl2);       
00325       }
00326     } 
00327     else    //inactive leaf -- end if
00328     {
00329       // cl2->m_gnode= 
00330       // this->m_leaves.push_vertex(cl2);
00331         if (  cl2->is_border() )
00332             this->b_leaves.push_vertex(cl2);       
00333     }
00334     this->m_nodes.pop_front() ;
00335   }
00336 
00337   /* add edges */
00338   Seq<Cell2d*> nlist;
00339 
00340   std::cout<<"border " << std::endl;
00341   // Border graph
00342   this->b_leaves.dfs(nlist);
00343   foreach(Cell2d* cl2, nlist)
00344   {
00345     if (cl2->s_neighbors.size()==0)
00346       foreach(Cell2d* nb, cl2->e_neighbors )
00347         if (nlist.member(nb) && nb->s_neighbors.size()==0)
00348           this->b_leaves.push_edge( cl2,nb ) ;
00349     if (cl2->e_neighbors.size()==0)
00350       foreach(Cell2d* nb, cl2->n_neighbors )
00351         if (nlist.member(nb) && nb->e_neighbors.size()==0)
00352           this->b_leaves.push_edge( cl2,nb ) ;
00353     if (cl2->n_neighbors.size()==0)
00354       foreach(Cell2d* nb, cl2->w_neighbors )
00355         if (nlist.member(nb)&& nb->n_neighbors.size()==0)
00356           this->b_leaves.push_edge( cl2,nb ) ;
00357     if (cl2->w_neighbors.size()==0)
00358              foreach(Cell2d* nb, cl2->s_neighbors )
00359                if (nlist.member(nb)&& nb->w_neighbors.size()==0)
00360                  this->b_leaves.push_edge( cl2,nb ) ;
00361   }
00362   nlist.clear();
00363 }
00364 
00365 
00366 TMPL bool 
00367 SELF::is_regular(Cell * cl) {
00368   return cl->is_regular();
00369 }
00370 
00371 TMPL bool 
00372 SELF::insert_regular(Cell * cl) {
00373   return cl->insert_regular(this);
00374 }
00375 
00376 TMPL bool 
00377 SELF::singularity(Cell * cl) {
00378   return cl->insert_singular(this) ;
00379 }
00380 
00381 TMPL void 
00382 SELF::insert_singular(Point * p) {
00383   m_specials<<p;
00384 }
00385 
00386 TMPL bool 
00387 SELF::subdivide(Cell * cl, Node * node) {
00388   int v=0;
00389   
00390   Cell* left, * right;
00391   v = cl->subdivide(left, right) ;
00392   
00393   node->m_left  = new Node(node, left,  Node::LEFT,  v); 
00394   m_nodes << node->m_left ;
00395   node->m_right = new Node(node, right, Node::RIGHT, v); 
00396   m_nodes << node->m_right;
00397 
00398   return true ;
00399 }
00400 
00401   TMPL void SELF::insert(Point * p) { 
00402     if(p->index()<0) {
00403       p->set_index(this->m_vertices.size());
00404       this->m_vertices << p; 
00405       this->m_graph.push_vertex(p);
00406     }
00407   }
00408 
00409 TMPL void SELF::insert(Edge  * e) { 
00410   this->m_edges    << e;
00411   this->m_graph.push_uedge(e->source(), e->destination() ); 
00412 }
00413 
00414 TMPL void SELF::insert(Point * p1,Point * p2){ 
00415   this->m_edges << new Edge(p1,p2); 
00416   this->m_graph.push_uedge(p1,p2); 
00417 }
00418 
00419 //--------------------------------------------------------------------
00420 TMPL struct viewer; struct axel;
00421 TMPL Viewer&
00422 operator<<(Viewer& out, const SELF& tp) {
00423   typedef SELF                        Topology2d;
00424   typedef typename Topology2d::Point  Point;
00425   typedef typename Topology2d::Edge   Edge; 
00426   //shape::edge<K>     Edge;
00427   //
00428   
00429   foreach(Point* p, tp.m_specials) out<<(*p)<<"\n";
00430   
00431   out<<" <curve type=\"mesh\">\n<vect>\nVECT\n";
00432   out<<tp.nbe()<<" "
00433      <<2*tp.nbe()<<" "
00434      <<tp.nbe()<<"\n";
00435   
00436   for(int i=0; i<tp.nbe();i++) out<<"2 ";
00437   out<<"\n";
00438   for(int i=0; i<tp.nbe();i++) out<<"1 ";
00439   out<<"\n";
00440   foreach(Edge* e, tp.edges()) {
00441     out <<e->source()->x()<<" "<<e->source()->y()<<" 0 "
00442         <<e->destination()->x()<<" "<<e->destination()->y()<<" 0 "
00443         <<"\n";
00444   }
00445   for(int i=0; i<tp.nbe();i++) 
00446     out<< "0.314 0.979 1 1\n";
00447   
00448   out<<" </vect>\n </curve>\n";
00449   
00450   return out;
00451 }
00452 //====================================================================
00453 } // namespace shape
00454 } // namespace mmx
00455 //====================================================================
00456 # undef TMPL
00457 # undef AlgebraicCurve
00458 # undef SemiAlgebraicCurve
00459 # undef Cell
00460 # undef Cell2dAlgebraicCurve
00461 # undef Cell2dParametricCurve
00462 # undef Cell2dSemiAlgebraicCurve
00463 # undef Cell2dList
00464 # undef Cell2dInter
00465 # undef Cell2dFactory
00466 # undef Curve
00467 # undef SELF 
00468 # undef Shape
00469 # undef STACK
00470 # undef Viewer
00471 //# undef VoronoiSite2d
00472 # undef ParametricCurve
00473 //# undef Cell2dVoronoiDiagram
00474 # undef Cell2dAlgebraicCurve
00475 #endif
00476 
00477 
00478 
00479 
00480