shape_doc 0.1
/Users/mourrain/Devel/mmx/shape/include/shape/cell2d_voronoi_diagram.hpp
Go to the documentation of this file.
00001 /*****************************************************************************
00002  * M a t h e m a  g i x
00003  *****************************************************************************
00004  * cell2d_voronoi_diagram.hpp
00005  * 2010-04-10
00006  * Angelos Mantzaflaris
00007  *****************************************************************************
00008  *               Copyright (C) 2010 INRIA Sophia-Antipolis
00009  *****************************************************************************
00010  * Comments :
00011  ****************************************************************************/
00012 # ifndef shape_cell2d_voronoi_diagram
00013 # define shape_cell2d_voronoi_diagram
00014 
00015 # include <shape/topology.hpp>
00016 # include <shape/cell_list.hpp>
00017 # include <shape/cell2d_algebraic_curve.hpp>
00018 
00019 # include <shape/cell2d_voronoi_site2d.hpp>
00020 # include <shape/solver_implicit.hpp>
00021 
00022 # define EPSILON .000001
00023 
00024 # define TMPL template<class C, class V>
00025 # define Shape geometric<V>
00026 # define Cell2dAlgebraicCurve cell2d_algebraic_curve<C,V>
00027 # define Intersection2dFactory intersection2d_factory<C,V>
00028 # define Solver     solver_implicit<C,V>
00029 
00030 # define Cell       cell<C,V>
00031 # define VSite      cell2d_voronoi_site2d<C,V>
00032 # define Cell2d     cell2d<C,V>
00033 # define CellList   cell2d_list<C,V>
00034 # define SELF cell2d_voronoi_diagram<C,V>
00035 namespace mmx {
00036 namespace shape {
00037 
00038 TMPL struct voronoi2d;
00039 
00040 template<class C, class V=default_env>
00041 class cell2d_voronoi_diagram
00042 //: VSite
00043 //: public bounding_box<C,V>
00044 : public cell2d<C,V>
00045    {
00046 public:
00047      typedef typename Cell::BoundingBox      BoundingBox;
00048      typedef typename cell2d<C,V>::Point     Point;
00049      typedef typename topology2d<C,V>::Edge  Edge;
00050      typedef topology<C,V>                   Topology;
00051      //typedef voronoi2d<C,V>               Voronoi2d;
00052      typedef polynomial< Interval<double>, with<Bernstein> > Polynomial;
00053      typedef Interval<double> coeff;
00054 
00055   cell2d_voronoi_diagram(void) ;
00056   cell2d_voronoi_diagram(double xmin, double xmax) ;
00057   cell2d_voronoi_diagram(double xmin, double xmax, double ymin, double ymax) ;
00058   cell2d_voronoi_diagram(double xmin, double xmax, double ymin, double ymax, bool itr) ;
00059   cell2d_voronoi_diagram(const BoundingBox& bx);
00060   virtual ~cell2d_voronoi_diagram(void) ;
00061 
00062   virtual bool is_regular (void) ;// = 0 ;
00063   virtual bool is_active  (void) ;// = 0 ;
00064   virtual bool is_intersected(void) ;// = 0 ;
00065 
00066   virtual bool insert_regular(Topology*);// = 0 ;
00067   virtual bool process_singular();// = 0 ;
00068 
00069   virtual bool insert_singular(Topology*) {return false;};// not used
00070 
00071   virtual void subdivide     (Cell*& left, Cell*& right, int v, double s);
00072 
00073   virtual void split_position(int& v, double& t); 
00074 
00075   virtual Point* pair(Point * p, int & sgn);
00076   virtual Point* starting_point( int sgn);
00077 
00078   virtual bool  is_touching (void) {return true; };
00079 
00080   virtual unsigned nb_intersect(void) const;
00081 
00082     //Seq<Cell2d *>  neighbors();
00083     Cell2d * neighbor(Point * p);
00084   Seq<Point *> intersections() const;
00085 
00086   virtual Seq<Point *> intersections(int i) const{
00087       Seq<Point*> l;
00088       Cell2dAlgebraicCurve* c;
00089       
00090       foreach(Cell*m, this->m_objects)
00091       {
00092           c = dynamic_cast<Cell2dAlgebraicCurve*>(m);
00093           l<< c->intersections(i);
00094       }
00095       return l;
00096   }
00097 
00098      void push_back(Cell * cv) 
00099        {
00100          m_sites<< m_objects.size();
00101          m_objects.push_back(cv); 
00102        }; 
00103 
00104      void push_back(Cell * cv, unsigned k) {
00105        m_sites << k ; //object cv corresponds to site labeled k
00106        m_objects.push_back(cv); 
00107      }; 
00108 
00109      void push_bisector(Cell2dAlgebraicCurve * cv, Seq<unsigned> k) {
00110        m_sites << k ; 
00111        m_objects.push_back(cv); 
00112      }; 
00113 
00114 
00115      int  count(void) { return m_objects.size() ; }
00116      inline Polynomial func(const int i) const{
00117        return ((VSite*)(this->m_objects[0]))->m_polynomial;
00118      };
00119 
00120      inline int signature(int & i, int & j) {
00121        int c(0);
00122        for ( int u=0; u<i; u++)
00123          for ( int v=u+1; v<m_sites.size(); v++)
00124            c++;
00125        return c + j-i-1; }
00126 
00127      unsigned site(int sgn, unsigned bs=0) { 
00128        unsigned n=this->count();
00129        unsigned i,k(bs+1);
00130        
00131        for (i=1;i<n;i++)
00132          if ( k>n-i )
00133            k-= n-i;
00134          else
00135            break;
00136        
00137        //return( sgn>0 ? m_sites[i-1+k]: m_sites[i-1])  ; 
00138        return ( m_sites[i-1 + (sgn>0?k:0)] )  ; 
00139      }
00140 
00141      int signof(unsigned st, unsigned bs=0) {
00142 
00143        if (this->m_objects.size()==1 )
00144          return ( st==m_sites[0]? -1 :1 ); 
00145 
00146        //std::cout<<"Sgnof " <<st<<"(bs="<<bs<<") on "<< *this<<"objs="<<this->m_objects.size()<<std::endl;
00147 
00148        unsigned n=this->count() - 1;
00149        unsigned i,j(0),k(0);
00150 
00151        for (i=0;i<=bs;i++)
00152          if ( k<n )
00153            k++ ;
00154          else
00155            j++ ;
00156 
00157        if (st==this->m_sites[j])   return (-1);
00158        if (st==this->m_sites[k])   return (1);
00159 
00160        std::cout<<"problem at \"signof\" "<<i <<", "<< *this <<"sites= "<<m_sites<<std::endl;
00161        return 0;
00162      };
00163 
00164      Seq<unsigned> sites() const { return m_sites;};
00165      void addsite(unsigned i){m_sites<<i;};
00166 
00167   Seq<Cell *>        m_objects  ;
00168 
00169 protected:
00170 
00171 
00172   Seq<unsigned>      m_sites;
00173   bool               m_intersected;
00174   bool               m_bisector;
00175   bool               m_treated;
00176 
00177 private:
00178 
00179  template<int i> static bool 
00180  coord(Point*u,Point*v) { return ( (*u)[i]< (*v)[i]); }
00181   
00182   static bool comp_up( VSite* a, VSite* b)
00183     {
00184       return ( a->upper() < b->upper() );
00185     }
00186 
00187   static bool intersect( VSite* a, VSite* b)
00188     {
00189       return has_sign_variation( a->equation() - b->equation() );
00190     }
00191 
00192 
00193   inline bool over( VSite* a, double bound)
00194     {
00195       return ( a->lower() > bound );
00196     }
00197 
00198      bool is_bisector()
00199        {
00200          if ( this->m_objects.size() > 2)
00201            return false;
00202 //         if ( this->m_bisector==true)
00203 //           return true;
00204          
00205          if ( !this->m_bisector  &&  this->m_objects.size() == 2 )
00207          {// first call: compute f_1-f_2 and intersections
00208            Polynomial p=  ((VSite*)(this->m_objects[0]))->m_polynomial ;
00209            p -= ((VSite*)(this->m_objects[1]))->m_polynomial;
00210            
00211            Cell2dAlgebraicCurve* cc= 
00212              new Cell2dAlgebraicCurve( p, (BoundingBox)(*this), false);
00213 
00214            //std::cout<<"bisector: treating "<< *this <<"\n";           
00215 
00217 
00218            //0
00219              if ( this->s_neighbors.size()==0)
00220              {
00221                Seq<Point *> ip;
00222                Solver::edge_point(ip,
00223                cc->m_polynomial,
00224                Solver::south_edge, 
00225                *cc);
00226                cc->s_intersections << ip;
00227              }
00228              else
00229            foreach( Cell2d *c, this->s_neighbors )
00230            if  ( ((SELF*)c)->m_bisector ) 
00231              {
00232                foreach(Point * p, ((SELF*)c)->intersections(2) ) 
00233                  if (this->xmin()<p->x() && this->xmax()>p->x())
00234                    cc->s_intersections << p;
00235              }
00236              else
00237              {
00238                Seq<Point *> ip;
00239                Solver::edge_point(ip,
00240                cc->m_polynomial,
00241                Solver::south_edge, 
00242                *cc  );
00243                foreach(Point * p, ip ) 
00244                  if (c->xmin()<p->x() && c->xmax()>p->x())
00245                    cc->s_intersections << p;
00246                //std::cout<<"ip: "<< ip.size() <<"\n";
00247              }
00248 
00249            //1
00250              if (this->e_neighbors.size()==0)
00251              {
00252                Seq<Point *> ip;
00253                Solver::edge_point(ip,
00254                cc->m_polynomial,
00255                Solver::east_edge, 
00256                *cc);
00257                cc->e_intersections << ip;
00258              }
00259              else
00260              foreach( Cell2d *c, this->e_neighbors )
00261              if ( ((SELF*)c)->m_bisector ) 
00262              {
00263                //Cell2d *c;
00264                //foreach(Point * p, ((VSite*)( ((SELF*)c)->m_objects[0]))->w_intersections ) 
00265                foreach(Point * p, ((SELF*)c)->intersections(3) ) 
00266                  if (this->ymin()<p->y() && this->ymax()>p->y())
00267                    cc->e_intersections << p;
00268              }
00269              else
00270              {
00271                Seq<Point *> ip;
00272                Solver::edge_point(ip,
00273                cc->m_polynomial,
00274                Solver::east_edge, 
00275                *cc  );
00276                foreach(Point * p, ip ) 
00277                  if (c->ymin()<p->y() && c->ymax()>p->y())
00278                    cc->e_intersections << p;
00279              }
00280 
00281            //2
00282              if (this->n_neighbors.size()==0 )
00283              {
00284                Seq<Point *> ip;
00285                Solver::edge_point(ip,
00286                cc->m_polynomial,
00287                Solver::north_edge, 
00288                *cc);
00289                cc->n_intersections << ip;
00290              }
00291              else
00292            foreach( Cell2d *c, this->n_neighbors )
00293              if ( ((SELF*)c)->m_bisector ) 
00294              {
00295                //Cell2d *c;
00296                //foreach(Point * p, ((VSite*)( ((SELF*)c)->m_objects[0]))->s_intersections ) 
00297                foreach(Point * p, ((SELF*)c)->intersections(0) ) 
00298                  if (this->xmin()<p->x() && this->xmax()>p->x())
00299                    cc->n_intersections << p;
00300              }
00301              else
00302              {
00303                Seq<Point *> ip;
00304                Solver::edge_point(ip,
00305                cc->m_polynomial,
00306                Solver::north_edge, 
00307                *cc );
00308                foreach(Point * p, ip ) 
00309                  if (c->xmin()<p->x() && c->xmax()>p->x())
00310                    cc->n_intersections << p;
00311              }
00312 
00313            //3
00314              if (this->w_neighbors.size()==0)
00315              {
00316                Seq<Point *> ip;
00317                Solver::edge_point(ip,
00318                cc->m_polynomial,
00319                Solver::west_edge, 
00320                *cc);
00321                cc->w_intersections << ip;
00322              }
00323              else
00324            foreach( Cell2d *c, this->w_neighbors )
00325              if ( ((SELF*)c)->m_bisector ) 
00326              {
00327                //Cell2d *c;
00328                //foreach(Point * p, ((VSite*)( ((SELF*)c)->m_objects[0]))->e_intersections ) 
00329                foreach(Point * p, ((SELF*)c)->intersections(1) ) 
00330                  if (this->ymin()<p->y() && this->ymax()>p->y())
00331                    cc->w_intersections << p;
00332 
00333              }
00334              else
00335              {
00336                Seq<Point *> ip;
00337                Solver::edge_point(ip,
00338                cc->m_polynomial,
00339                Solver::west_edge, 
00340                *cc  );
00341                foreach(Point * p, ip ) 
00342                  if (c->ymin()<p->y() && c->ymax()>p->y())
00343                    cc->w_intersections << p;
00344 
00345                //std::cout<<"west: "<< *ip[0] <<"\n";
00346              }
00347 
00348 
00350 
00351 
00352            //foreach (Cell* m, this->m_objects) delete m; //segfault
00353            this->m_objects.clear();
00354            
00355            this->m_objects<< (Cell*)(cc);
00356            //std::cout<<"bisector found "<< this<<std::endl;
00357 
00358            //std::cout<<"Intersections: "<< std::endl;
00359            //foreach(Point* p, this->intersections() )
00360            //  std::cout<< *p<<"\n";
00361            
00362          }
00363 
00364          this->m_bisector=true;
00365          return true;
00366 
00367        }
00368      
00369 public:
00370 
00371 bool compute_boundary()
00372 {
00373   int i(0),j(0), r(m_sites.size()-1);
00374   foreach ( Cell* obj, this->m_objects)
00375   {
00376     Cell2dAlgebraicCurve* cc= dynamic_cast<Cell2dAlgebraicCurve*>(obj);
00377     if (j<r) // bisector signature (i,j)
00378       j++;
00379     else
00380       { i++; j=i+1;}
00381 
00382     //0
00383     if ( this->s_neighbors.size()==0)
00384     {
00385       Seq<Point *> ip;
00386         Solver::edge_point(ip,
00387                            cc->m_polynomial,
00388                            Solver::south_edge, 
00389                            *cc);
00390         cc->s_intersections << ip;
00391       }
00392     else
00393       foreach( Cell2d *c, this->s_neighbors )
00394         if  ( ((SELF*)c)->m_bisector ) 
00395         {
00396           Seq<unsigned> a= ((SELF*)c)->sites();
00397             if ( a.member( m_sites[i]) && a.member(m_sites[j]) )
00398             {
00399               foreach(Point * p, ((SELF*)c)->intersections(2) ) 
00400                 if (this->xmin()<p->x() && this->xmax()>p->x())
00401                   cc->s_intersections << p;
00402             }
00403           }
00404         else if ( ((SELF*)c)->m_treated )
00405         {
00406               int u(0),v(0);
00407               foreach( Cell *nc, ((SELF*)c)->m_objects )
00408               {
00409                 if (v<r) // bisector signature (u,v)
00410                   v++;
00411                 else
00412                 { u++; v=u+1;}
00413                   if ( ((SELF*)c)->m_sites[u]==m_sites[i] && 
00414                        ((SELF*)c)->m_sites[v]==m_sites[j] )
00415               foreach(Point * p, ((SELF*)nc)->intersections(2) ) 
00416                 if (this->xmin()<p->x() && this->xmax()>p->x())
00417                   cc->s_intersections << p;
00418           }
00419        }
00420         else
00421         {
00422           Seq<Point *> ip;
00423             Solver::edge_point(ip,
00424                                cc->m_polynomial,
00425                                Solver::south_edge, 
00426                                *cc  );
00427             foreach(Point * p, ip ) 
00428               if (c->xmin()<p->x() && c->xmax()>p->x())
00429                 cc->s_intersections << p;
00430             //std::cout<<"ip: "<< ip.size() <<"\n";
00431           }
00432         
00433         //1
00434         if (this->e_neighbors.size()==0)
00435         {
00436           Seq<Point *> ip;
00437             Solver::edge_point(ip,
00438                                cc->m_polynomial,
00439                                Solver::east_edge, 
00440                                *cc);
00441                cc->e_intersections << ip;
00442           }
00443         else
00444           foreach( Cell2d *c, this->e_neighbors )
00445             if ( ((SELF*)c)->m_bisector ) 
00446             {
00447               Seq<unsigned> a= ((SELF*)c)->sites();
00448               if ( a.member( m_sites[i]) && a.member(m_sites[j]) )
00449               {
00450                 foreach(Point * p, ((SELF*)c)->intersections(3) ) 
00451                   if (this->ymin()<p->y() && this->ymax()>p->y())
00452                     cc->e_intersections << p;
00453               }
00454             }
00455             else if ( ((SELF*)c)->m_treated )
00456             {
00457               int u(0),v(0);
00458               foreach( Cell *nc, ((SELF*)c)->m_objects )
00459               {
00460                 if (v<r) // bisector signature (u,v)
00461                   v++;
00462                 else
00463                 { u++; v=u+1;}
00464                   if ( ((SELF*)c)->m_sites[u]==m_sites[i] && 
00465                        ((SELF*)c)->m_sites[v]==m_sites[j] )
00466                     foreach(Point * p, ((SELF*)nc)->intersections(3) ) 
00467                       if (this->ymin()<p->y() && this->ymax()>p->y())
00468                         cc->e_intersections << p;                    
00469                     }
00470               }
00471             else
00472             {
00473               Seq<Point *> ip;
00474                 Solver::edge_point(ip,
00475                                    cc->m_polynomial,
00476                                    Solver::east_edge, 
00477                                    *cc  );
00478                  foreach(Point * p, ip ) 
00479                    if (c->ymin()<p->y() && c->ymax()>p->y())
00480                      cc->e_intersections << p;
00481               }
00482             
00483             //2
00484              if (this->n_neighbors.size()==0 )
00485              {
00486                Seq<Point *> ip;
00487                  Solver::edge_point(ip,
00488                                     cc->m_polynomial,
00489                                     Solver::north_edge, 
00490                                     *cc);
00491                  cc->n_intersections << ip;
00492                }
00493              else
00494                foreach( Cell2d *c, this->n_neighbors )
00495                  if ( ((SELF*)c)->m_bisector ) 
00496                  {
00497                    Seq<unsigned> a= ((SELF*)c)->sites();
00498                      if ( a.member( m_sites[i]) && a.member(m_sites[j]) )
00499                      {
00500                    foreach(Point * p, ((SELF*)c)->intersections(0) ) 
00501                      if (this->xmin()<p->x() && this->xmax()>p->x())
00502                        cc->n_intersections << p;
00503                      }
00504                    }
00505             else if ( ((SELF*)c)->m_treated )
00506             {
00507               int u(0),v(0);
00508               foreach( Cell *nc, ((SELF*)c)->m_objects )
00509               {
00510                 if (v<r) // bisector signature (u,v)
00511                   v++;
00512                 else
00513                 { u++; v=u+1;}
00514                   if ( ((SELF*)c)->m_sites[u]==m_sites[i] && 
00515                        ((SELF*)c)->m_sites[v]==m_sites[j] )
00516                    foreach(Point * p, ((SELF*)nc)->intersections(0) ) 
00517                       if (this->xmin()<p->x() && this->xmax()>p->x())
00518                         cc->n_intersections << p;                    
00519                 }
00520               }
00521                  else
00522                  {
00523                    Seq<Point *> ip;
00524                      Solver::edge_point(ip,
00525                                         cc->m_polynomial,
00526                                         Solver::north_edge, 
00527                                         *cc );
00528                      foreach(Point * p, ip ) 
00529                        if (c->xmin()<p->x() && c->xmax()>p->x())
00530                          cc->n_intersections << p;
00531                    }
00532                  
00533                  //3
00534                  if (this->w_neighbors.size()==0)
00535                  {
00536                    Seq<Point *> ip;
00537                  Solver::edge_point(ip,
00538                                     cc->m_polynomial,
00539                                     Solver::west_edge, 
00540                                     *cc);
00541                  cc->w_intersections << ip;
00542                }
00543                  else
00544                foreach( Cell2d *c, this->w_neighbors )
00545                  if ( ((SELF*)c)->m_bisector ) 
00546                  {
00547                    Seq<unsigned> a= ((SELF*)c)->sites();
00548                      if ( a.member( m_sites[i]) && a.member(m_sites[j]) )
00549                      {
00550                    //Cell2d *c;
00551                    //foreach(Point * p, ((VSite*)( ((SELF*)c)->m_objects[0]))->e_intersections ) 
00552                    foreach(Point * p, ((SELF*)c)->intersections(1) ) 
00553                      if (this->ymin()<p->y() && this->ymax()>p->y())
00554                        cc->w_intersections << p;
00555                      }
00556                    }
00557             else if ( ((SELF*)c)->m_treated )
00558             {
00559               int u(0),v(0);
00560               foreach( Cell *nc, ((SELF*)c)->m_objects )
00561               {
00562                 if (v<r) // bisector signature (u,v)
00563                   v++;
00564                 else
00565                 { u++; v=u+1;}
00566                   if ( ((SELF*)c)->m_sites[u]==m_sites[i] && 
00567                        ((SELF*)c)->m_sites[v]==m_sites[j] )
00568                    foreach(Point * p, ((SELF*)nc)->intersections(1) ) 
00569                      if (this->ymin()<p->y() && this->ymax()>p->y())
00570                        cc->w_intersections << p;                    
00571                     }
00572               }
00573                  else
00574                  {
00575                    Seq<Point *> ip;
00576                      Solver::edge_point(ip,
00577                                         cc->m_polynomial,
00578                                         Solver::west_edge, 
00579                                         *cc  );
00580                      foreach(Point * p, ip ) 
00581                        if (c->ymin()<p->y() && c->ymax()>p->y())
00582                          cc->w_intersections << p;
00583                      
00584                      //std::cout<<"west: "<< *ip[0] <<"\n";
00585                    }
00586                  
00587         }
00588   this->m_treated=true;
00589   return true;
00590 }//compute_boundary
00591                
00592 } ;
00593 
00594 
00595 //--------------------------------------------------------------------
00596 
00597 /*
00598 TMPL
00599 class intersection2d_factory {
00600 public:
00601   typedef bounding_box<C,V>      BoundingBox;
00602   typedef typename cell2d<C,V>::Point Point;
00603   static Intersection2dFactory * instance(void) {
00604 
00605         if(!m_instance)
00606             m_instance = new Intersection2dFactory ;
00607 
00608         return m_instance ;
00609     }
00610 
00611   void compute(Seq<Point*>& res, Shape * c1, Shape * c2, const BoundingBox& bx) {
00612     
00613     if(Cell2dAlgebraicCurve * ca1 = dynamic_cast<Cell2dAlgebraicCurve *>(c1)) {
00614       if(Cell2dAlgebraicCurve * ca2 = dynamic_cast<Cell2dAlgebraicCurve *>(c2)) {
00615 
00616         typedef polynomial< double, with<Bernstein> > MultivariateDenseBernstein;
00617         MultivariateDenseBernstein p1, p2;
00618         let::assign(p1.rep(), ca1->m_polynomial.rep() );
00619         let::assign(p2.rep(), ca2->m_polynomial.rep() );
00620         Solver::intersection(res, p1, p2, bx);
00621       }
00622     }
00623   }
00624 
00625 private:
00626     intersection2d_factory(void) {} ;
00627 
00628 private:
00629     static Intersection2dFactory * m_instance;
00630 } ;
00631 
00632 TMPL Intersection2dFactory*  Intersection2dFactory::m_instance = NULL;
00633 
00634 */
00635 
00636 TMPL
00637 SELF::cell2d_voronoi_diagram(void): m_intersected(false), m_treated(false) {}
00638 
00639 TMPL
00640 SELF::cell2d_voronoi_diagram(double xmin, double xmax, double ymin, double ymax) :  cell2d<C,V>(xmin, xmax, ymin, ymax), m_intersected(false), m_bisector(false), m_treated(false) {}
00641 
00642 TMPL
00643 SELF::cell2d_voronoi_diagram(double xmin, double xmax, double ymin, double ymax, bool itr) : cell2d<C,V>(xmin, xmax, ymin, ymax), m_intersected(itr), m_bisector(false), m_treated(false) {}
00644 
00645 TMPL
00646 SELF::cell2d_voronoi_diagram(const BoundingBox& bx): cell2d<C,V>(bx), m_intersected(false), m_bisector(false), m_treated(false)  {};
00647 
00648 TMPL
00649 SELF::~cell2d_voronoi_diagram(void) {
00650     foreach (Cell* m, this->m_objects) delete m;
00651 }
00652   
00653 TMPL bool 
00654 SELF::is_regular()
00655 {
00656   //std::cout<<"bisector? "<< this->is_bisector()<<std::endl;
00657   if ( this->is_bisector() ) 
00658   {
00659     return (this->m_objects[0])->is_regular();
00660   }
00661   else
00662   {
00663     return false;
00664   }
00665 }
00666 
00667 TMPL bool 
00668 SELF::is_intersected() {
00669   
00670   if(this->m_objects.size() >1 && !m_intersected) {
00671 //      std::cout<<"Intersecting inside box "<< this <<std::endl;
00672       for(unsigned i=0; i<this->m_objects.size();i++)
00673           for(unsigned j=i+1; j<this->m_objects.size(); j++)
00674             Intersection2dFactory::instance()->compute(this->m_singular, (Shape*)this->m_objects[i], (Shape*)this->m_objects[j], (BoundingBox)*this);
00675       m_intersected = true;      
00676   }
00677   
00678   if (this->m_singular.size() > 0) return true;
00679   return false;
00680 }
00681 
00682 
00683 TMPL bool 
00684 SELF::insert_regular(Topology* s) {
00685 
00686   Seq<Point*> l;
00687   l= this->intersections();
00688 
00689   
00690   //std::cout<<"VD, regular: "<< *this<<", #="<<l.size() <<std::endl;
00691   // foreach( Point* e, l)
00692   //    std::cout<<*e <<", at "<< e <<std::endl;
00693 
00694   int * sz;
00695   int * st;
00696   Point *q;
00697 
00698   if ( l.size()==2 )
00699   {
00700     s->insert( l[0] );
00701     s->insert( l[1] );
00702     s->insert( new Edge(l[0],l[1]) );
00703     return true;
00704   }
00705 
00706   if ( l.size()==4 ) // two dublicated
00707   {
00708     s->insert( l[0] );
00709     s->insert( l[1] );
00710     s->insert( new Edge(l[0],l[1]) );
00711     return true;
00712   }
00713   
00714   if ( l.size()==1)
00715   {
00716     std::cout<< "SIZE ONE, "<< *this<<std::endl;
00717 
00718     s->insert( l[0] );
00719     foreach( Cell* c, this->m_objects )
00720       if ( ((Cell2d*)c)->nb_intersect()==1 )
00721       {
00722         sz= ((VSite*)c)->m_polynomial.rep().szs();
00723         st= ((VSite*)c)->m_polynomial.rep().str();
00724         if ( ((VSite*)c)->m_polynomial[0]<EPSILON )
00725         {
00726           q= new Point(this->xmin(),this->ymin(),0); 
00727           std::cout<< "1.add ("<< *q <<")->("<< *l[0] << ") in "<< *this<<std::endl;
00728           s->insert(q);
00729           s->insert( new Edge(l[0],q) );
00730           ((VSite*)c)->n_intersections<< q;
00731 
00732           foreach( Cell2d *nb, this->s_neighbors )
00733             foreach( Cell* cc, ((SELF*)nb)->m_objects )
00734             if ( cc->is_active() )
00735             {
00736               ((VSite*)cc)->n_intersections<< q;
00737               std::cout<<"This Intersections: "<< this->intersections().size() << std::endl;
00738               std::cout<<"Neib Intersections: "<< nb->intersections().size() << std::endl;
00739               return true;
00740             }
00741           foreach( Cell2d *nb, this->w_neighbors )
00742             foreach( Cell* cc, ((SELF*)nb)->m_objects )
00743             if ( cc->is_active() )
00744             {
00745               ((VSite*)cc)->e_intersections<< q;
00746               std::cout<<"This Intersections: "<< this->intersections().size() << std::endl;
00747               std::cout<<"Neib Intersections: "<< nb->intersections().size() << std::endl;
00748               return true;
00749             }
00750 
00751         } else if ( ((VSite*)c)->m_polynomial[(sz[0]-1)*st[0]]<EPSILON )
00752         {
00753           q= new Point(this->xmax(),this->ymax(),0);
00754           std::cout<< "2.add ("<< *q <<")->("<< *l[0] << ") in "<< *this<<std::endl;
00755           s->insert(q);
00756           s->insert( new Edge(l[0],q) );
00757           ((VSite*)c)->s_intersections<< q;
00758 
00759 
00760           foreach( Cell2d *nb, this->e_neighbors )
00761             foreach( Cell* cc, ((SELF*)nb)->m_objects )
00762             if ( cc->is_active() )
00763             {
00764               ((VSite*)cc)->w_intersections<< q;
00765               std::cout<<"This Intersections: "<< this->intersections().size() << std::endl;
00766               std::cout<<"Neib Intersections: "<< nb->intersections().size() << std::endl;
00767               return true;
00768             }
00769           foreach( Cell2d *nb, this->n_neighbors )
00770             foreach( Cell* cc, ((SELF*)nb)->m_objects )
00771             if ( cc->is_active() )
00772             {
00773               ((VSite*)cc)->s_intersections<< q;
00774               std::cout<<"This Intersections: "<< this->intersections().size() << std::endl;
00775               std::cout<<"Neib Intersections: "<< nb->intersections().size() << std::endl;
00776               return true;
00777             }
00778 
00779         } else if ( ((VSite*)c)->m_polynomial[sz[0]*sz[1]-1]<EPSILON )
00780         {
00781           q= new Point(this->xmin(),this->ymax(),0);
00782           std::cout<< "3.add ("<< *q <<")->("<< *l[0] << ") in "<< *this<<std::endl;
00783           s->insert(q);
00784           s->insert( new Edge(l[0],q) );
00785           ((VSite*)c)->w_intersections<< q;
00786           return true;
00787         } else if ( ((VSite*)c)->m_polynomial[(sz[1]-1)*st[1]]<EPSILON )
00788         {
00789           q= new Point(this->xmax(),this->ymin(),0);
00790           std::cout<< "4.add ("<< *q <<")->("<< *l[0] << ") in "<< *this<<std::endl;
00791           s->insert(q);
00792           s->insert( new Edge(l[0],q) );
00793           ((VSite*)c)->e_intersections<< q;
00794           return true;
00795         }  
00796       }
00797   }
00798 
00799   if ( l.size()==0)
00800   {
00801     std::cout<< "SIZE ZERO, "<< *this<<std::endl;
00802     return true;
00803 
00804     foreach( Cell* c, this->m_objects )
00805       {
00806         sz= ((VSite*)c)->m_polynomial.rep().szs();
00807         st= ((VSite*)c)->m_polynomial.rep().str();
00808         if ( ((VSite*)c)->m_polynomial[0]<EPSILON )
00809         {
00810           q= new Point(this->xmin(),this->ymin(),0); 
00811           std::cout<< "1.add ("<< *q <<") in "<< *this<<std::endl;
00812           s->insert(q);
00813           ((VSite*)c)->n_intersections<< q;
00814           return true;
00815         } else if ( ((VSite*)c)->m_polynomial[(sz[0]-1)*st[0]]<EPSILON )
00816         {
00817           q= new Point(this->xmax(),this->ymax(),0);
00818           std::cout<< "1.add ("<< *q <<") in "<< *this<<std::endl;
00819           s->insert(q);
00820           ((VSite*)c)->s_intersections<< q;
00821           return true;
00822         } else if ( ((VSite*)c)->m_polynomial[sz[0]*sz[1]-1]<EPSILON )
00823         {
00824           q= new Point(this->xmin(),this->ymax(),0);
00825           std::cout<< "1.add ("<< *q <<") in "<< *this<<std::endl;
00826           s->insert( new Edge(l[0],q) );
00827           ((VSite*)c)->w_intersections<< q;
00828           return true;
00829         } else if ( ((VSite*)c)->m_polynomial[(sz[1]-1)*st[1]]<EPSILON )
00830         {
00831           q= new Point(this->xmax(),this->ymin(),0);
00832           std::cout<< "1.add ("<< *q <<") in "<< *this<<std::endl;
00833           s->insert(q);
00834           s->insert( new Edge(l[0],q) );
00835           ((VSite*)c)->e_intersections<< q;
00836           return true;
00837         }  
00838       }
00839   }
00840 
00841     std::cout<< "nb_in= "<<  this->nb_intersect() <<std::endl;
00842     //std::cout<< "box  = "<< *this <<std::endl;
00843         //foreach(Point* q, this->intersections() ) 
00844         //std::cout<< " "<< *q <<std::endl;
00845     print((Cell2dAlgebraicCurve*)m_objects[0]);
00846     return true;
00847 }
00848   
00849 TMPL bool 
00850 SELF::process_singular() {
00851 
00852 //  std::cout<<"VD, Inserting singular"<<*this<<std::endl;  
00853 //  ((voronoi2d<C,V>*)s)->m_singular_cells<< this; // does not work with forward declaration of voronoi2d
00854  
00855 //  foreach( Point* q, this->intersections() )
00856 //    std::cout<< *q<<"   adr  "<<q<<std::endl; 
00857 
00858 
00859      //CellList* l= new CellList( (BoundingBox)(*this) );
00860      Seq<Cell2dAlgebraicCurve*> l;
00861      Cell2dAlgebraicCurve* cc;
00862 
00863      //std::cout<<"compute arrangement of bisectors"<<std::endl;
00864      //More than one bisector in the cell: Compute arrangement
00865      Polynomial p;
00866      for ( unsigned i=0; i< this->m_objects.size(); i++ )
00867        for ( unsigned j=i+1; j< this->m_objects.size(); j++ )
00868        {
00869          p  = ((VSite*)(this->m_objects[i]))->m_polynomial ;
00870          p -= ((VSite*)(this->m_objects[j]))->m_polynomial ;
00871          
00872          cc= new Cell2dAlgebraicCurve( p,  (BoundingBox)(*this), false  );
00873          //if ( cc->is_active() ) //commented:Put all curves, even inactive!
00874            l<< cc;
00875        }
00876 
00877      this->m_objects.clear();
00878      this->m_objects<< l;
00879      this->is_intersected();
00880 //     foreach(Cell * m, this->m_objects) m->insert_singular(s);
00881 
00882      return true;
00883 
00884      this->m_bisector=true;// .. 
00885 
00886      //foreach(Cell * m, l ) m->insert_singular(s);
00887 
00888      //if (this->m_singular.size()>0 ) 
00889      //  std::cout<<"VORONOI VERTEX COMPUTED in"<< *this <<std::endl;
00890    
00891 }
00892   
00893 TMPL void
00894 SELF::split_position(int& v, double& s) {
00895   double sx = (this->xmax()-this->xmin());
00896   double sy = (this->ymax()-this->ymin());
00897   if(sx<sy) {
00898     v=1;
00899     s=(this->ymax()+this->ymin())/2;
00900   } else {
00901     v=0;
00902     s=(this->xmax()+this->xmin())/2;
00903   }
00904 }
00905 
00906 TMPL void
00907 SELF::subdivide(Cell *& left, Cell *& right, int  v, double c) {
00908 
00909   //std::cout<<"Subdividing "<< this << "sites="<<this->m_sites <<std::endl;
00910 
00911   typedef SELF Cell_t;
00912 
00913   if(v==1) {
00914     left =(Cell*)new Cell_t(this->xmin(), this->xmax(), this->ymin(), c, m_intersected) ;
00915     right=(Cell*)new Cell_t(this->xmin(), this->xmax(), c, this->ymax(), m_intersected) ;
00916     
00917     foreach(Point * p, this->m_singular) {
00918       if(p->y() <=  c) 
00919         ((Cell_t*) left)->m_singular << p ;
00920       else
00921         ((Cell_t*)right)->m_singular << p ;
00922     }
00923 
00924     /*  Update neighbors  */
00925     this->connect1( (Cell_t*)left, (Cell_t*)right);
00926     ((Cell_t*)left)->join1((Cell_t*)right);
00927 
00928   } else {//v==0
00929     left = (Cell*)new Cell_t(this->xmin(), c, this->ymin(), this->ymax(), m_intersected) ;
00930     right= (Cell*)new Cell_t(c, this->xmax(), this->ymin(), this->ymax(), m_intersected) ;
00931 
00932     foreach(Point * p, this->m_singular) {
00933       if(p->x() <= c ) 
00934         ((Cell_t*)left)->m_singular << p ;
00935       else
00936         ((Cell_t*)right)->m_singular << p ;
00937     }
00938 
00939     /*  Update neighbors  */
00940     this->connect0((Cell_t*)left, (Cell_t*)right);
00941     ((Cell_t*)left)->join0((Cell_t*)right);
00942 
00943   }
00944 
00945   /* disconnect parent */
00946   this->disconnect( );
00947 
00948   if (!this->is_bisector() ) 
00949   {
00950 
00951   Seq<VSite*> ll, rr;
00952   Cell * cv_left, * cv_right;
00953 
00954   foreach(Cell* cv, this->m_objects) {
00955     cv->subdivide( cv_left, cv_right);
00956       ll<< (VSite*)cv_left;
00957       rr<< (VSite*)cv_right;
00958   }
00959 
00960   /* Filtering sites that are "far away" */
00961   //1. find minimum upper bound 
00962   //2. remove all cells whose lower bound is bigger than that
00963   //std::cout<<"Filtering Cells "<< cv_left<<" , "<<cv_right<<std::endl;
00964 
00965   /* Left cell */
00966   double mm;
00967   unsigned cnt;
00968   mm=  ( (VSite*)ll.min(this->comp_up) )->upper();
00969   
00970   cnt=0;
00971   foreach(VSite* vs, ll)
00972   {
00973     //std::cout<<"check "<< m_sites[cnt]<<std::endl;
00974     if ( !this->over(vs,mm) )
00975     {
00976       ((Cell_t*)left)->push_back( vs, m_sites[cnt] );
00977       //std::cout<<"added "<< m_sites[cnt]<<std::endl;
00978     }
00979     cnt++;
00980     //else
00981     //{
00982     //delete vs;
00983     //ll.erase( ll.search(vs) ) ;
00984     //}
00985   }
00986 
00987   /* Right cell */
00988   mm=  ( (VSite*)rr.min(this->comp_up) )->upper();
00989   
00990   cnt=0;
00991   foreach(VSite* vs, rr)
00992   {
00993     if ( !this->over(vs,mm) )
00994     {
00995       ((Cell_t*)right)->push_back( vs, m_sites[cnt] );
00996       //((Cell_t*)right)->m_objects<< (Cell*)vs;
00997     }
00998     //else
00999     //{
01000     //delete vs;
01001     //ll.erase( ll.search(vs) ) ;
01002     //}
01003     cnt++;
01004   }
01005   }
01006   else
01007   {//bisector box
01008 
01009     Cell * cv_left, * cv_right;
01010     //std::cout<<"bisector subdiv " <<std::endl;
01011     this->m_objects[0]->subdivide( cv_left, cv_right);
01012     ((Cell_t*)left)->push_bisector( (Cell2dAlgebraicCurve*)cv_left,  m_sites );
01013     ((Cell_t*)right)->push_bisector((Cell2dAlgebraicCurve*)cv_right, m_sites );
01014 
01015     ((Cell_t*)left)->m_bisector=true;
01016     ((Cell_t*)right)->m_bisector=true;
01017     
01018   }
01019 
01020 //    std::cout<<"ok " <<std::endl;  
01021 }
01022 
01023 //----------
01024 
01025 TMPL  bool 
01026 SELF::is_active()  {
01027   if ( this->is_bisector() ) 
01028   {
01029     return (this->m_objects[0])->is_active();
01030   }
01031   else
01032   {
01033     return true;
01034   }
01035 
01036 }
01037 
01038 TMPL unsigned 
01039 SELF::nb_intersect(void) const {
01040     unsigned sum(0);
01041     Cell2d* c;
01042     foreach (Cell* m, m_objects)
01043     { 
01044         c = dynamic_cast<Cell2d*>(m);
01045         sum+= c->nb_intersect();
01046     }
01047     return sum;
01048   }
01049 
01050 
01051 TMPL typename SELF::Point* 
01052 SELF::pair(Point * p, int & sgn)
01053 {
01054 
01055 //  std::cout<<(sgn>0?"+": "-")<<" pair of "<< *this<<"("<< p->x()<<","<<p->y()<<")"<<std::endl;
01056 
01057   if ( this->is_intersected() )  
01058   {
01059     //std::cout<<"Reached Vertex cell "<< *this << " ("<<sgn<<")"<<std::endl;
01060     //std::cout<<"sites are "<< this->m_sites <<std::endl;
01061     //foreach( Point* q, this->intersections() )
01062     //std::cout<< *q<<std::endl; 
01063   
01064     //Find bisector containing p
01065     
01066     unsigned st, cnt(0);
01067     foreach (Cell* mm, this->m_objects)
01068     { 
01069       Cell2d* m = dynamic_cast<Cell2d*>(mm);
01070 
01071       if ( m->intersections().member(p) )
01072 //      foreach( Point* q, m->intersections() )
01073 //        if ( abs(p->x()-q->x())<EPSILON && abs(p->y()-q->y())<EPSILON )
01074         { 
01075           // p found in bisector m.
01076           //std::cout<< "Current bisector: "<< cnt <<std::endl;
01077           // deduce site from sgn
01078           st=this->site(sgn,cnt);
01079           //std::cout<< "Site is: "<< st <<std::endl;
01080           // jump to a different bisector containing st
01081           unsigned cnt2(0);
01082           foreach (Cell* cc, this->m_objects)
01083           { 
01084             Cell2d* c = dynamic_cast<Cell2d*>(cc);
01085             if ( c!=m                   &&
01086                (st==this->site(1,cnt2)  ||
01087                 st==this->site(-1,cnt2) ))
01088             {
01089               sgn= this->signof(st,cnt2);
01090 //              std::cout<< "New bisector: "<< cnt2 <<std::endl;
01091 //              std::cout<< "New sign: "<< sgn <<std::endl;
01092 //              std::cout<< "start="<< *(c->starting_point(sgn))<<std::endl;
01093 //              std::cout<< "end  ="<< *(c->pair(c->starting_point(sgn), sgn )) <<std::endl;
01094 
01095               return c->pair(c->starting_point(sgn), sgn );
01096             } 
01097             cnt2++;
01098           }
01099         }
01100       cnt++;
01101     }
01102 
01103   } else {
01104     
01105     Cell2d* c;
01106     foreach (Cell* m, m_objects)
01107     { 
01108       c = dynamic_cast<Cell2d*>(m);
01109       if ( c->intersections().member(p) )
01110 //    foreach( Point* q, c->intersections() )
01111 //      if ( abs(p->x()-q->x())<EPSILON && abs(p->y()-q->y())<EPSILON )
01112       { 
01113         //return c->pair(q,sgn);
01114         return c->pair(p,sgn);
01115       }
01116     }
01117   }
01118   std::cout<<"... Cell list pair trouble"<<std::endl;
01119   return NULL;
01120 } 
01121 
01122 TMPL typename SELF::Point* 
01123 SELF::starting_point( int sgn)
01124 {
01125 //  std::cout<<"starting point(voronoi diagram)"<<std::endl;
01126 
01127    Cell2d* c;
01128    foreach (Cell* m, m_objects)
01129     { 
01130         if ( m->is_active() )
01131         {
01132             c = dynamic_cast<Cell2d*>(m);           
01133             return ((VSite*)c)->starting_point(sgn);
01134         }
01135     }
01136     return NULL; 
01137 }
01138 
01139 TMPL Cell2d*
01140 SELF::neighbor( Point* p)
01141 {
01142   foreach( Cell2d *c, this->s_neighbors     )
01143     if ( c->intersections(2).member(p) )
01144 //    foreach( Point* q, c->intersections(2) )
01145 //    if ( abs(p->x()-q->x())<EPSILON && abs(p->y()-q->y())<EPSILON )
01146       return c;
01147 
01148   foreach( Cell2d *c, this->e_neighbors     )
01149     if ( c->intersections(3).member(p) )
01150 //    foreach( Point* q, c->intersections(3) )
01151 //    if ( abs(p->x()-q->x())<EPSILON && abs(p->y()-q->y())<EPSILON )
01152       return c;
01153 
01154   foreach( Cell2d *c, this->n_neighbors     )
01155     if ( c->intersections(0).member(p) )
01156 //    foreach( Point* q, c->intersections(0) )
01157 //    if ( abs(p->x()-q->x())<EPSILON && abs(p->y()-q->y())<EPSILON )
01158       return c;
01159 
01160   foreach( Cell2d *c, this->w_neighbors     )
01161     if ( c->intersections(1).member(p) )
01162 //    foreach( Point* q, c->intersections(1) )
01163 //    if ( abs(p->x()-q->x())<EPSILON && abs(p->y()-q->y())<EPSILON )
01164       return c;
01165   
01166 /*
01167 
01168   std::cout<<"Point "<<"("<<*p<<") not found in neighbs of "<< *this<<"( #neibs="<<this->neighbors().size() <<")"<<std::endl;
01169 
01170       foreach( Cell2d* c, this->neighbors() )
01171       {
01172         std::cout<< *c <<" ints:"<< ((SELF*)c)->intersections().size()  <<std::endl;
01173         foreach(Point* q, c->intersections() )
01174           std::cout<<"Point "<<q<<" ("<<*q<<")"<<std::endl;
01175       }
01176 */
01177 
01178     return NULL;
01179 }
01180 
01181 TMPL Seq<typename mmx::shape::cell2d<C,V>::Point*>
01182 SELF::intersections() const{
01183     Seq<Point *> s,e,n,w,r;
01184     //std::cout<<"intersections, "<<*this<<std::endl;
01185     Cell2d* cl;
01186     foreach (Cell* m, m_objects)
01187     {
01188         cl = dynamic_cast<Cell2d*>(m);           
01189         s<< cl->s_intersections;
01190         e<< cl->e_intersections;
01191         n<< cl->n_intersections;
01192         w<< cl->w_intersections;
01193     }
01194     s.sort(this->coord<0>);
01195     e.sort(this->coord<1>);
01196     n.sort(this->coord<0>);
01197     w.sort(this->coord<1>);
01198 
01199     r<<s;
01200     r<<e;
01201     r<<n.reversed();
01202     r<<w.reversed();
01203 
01204 
01205     return ( r ); 
01206 }
01207 
01208 
01209 } ; // namespace shape
01210 } ; // namespace mmx
01211 
01212 # undef TMPL
01213 # undef Solver
01214 # undef Cell
01215 # undef Cell2d
01216 # undef CellList
01217 # undef SELF
01218 # undef AlgebraicCurve
01219 # undef Intersection2dFactory
01220 # undef Cell2dAlgebraicCurve
01221 # undef Shape
01222 # undef VSite
01223 
01224 # endif