shape_doc 0.1
voronoi2d_test.cpp
# include <iostream>
//#include <time.h>

# include <shape/voronoi2d.hpp>

# include <shape/graph.hpp>
# include <shape/face.hpp>
# include <shape/point.hpp>

using namespace std ;
using namespace mmx ;
using namespace shape ;

# define AXEL        viewer< axel >
# define BoundingBox bounding_box< double >
# define Voronoi2d   voronoi2d< double >
# define Point       point< double >
# define VoronoiSite2d voronoi_site2d< double >

int main(int argc, char** argv) {

  std::srand((unsigned)time(0));

/*
  //usual diagram test
  BoundingBox bx(-3, 3, -3, 3);
  Voronoi2d * vd= new Voronoi2d(bx);
  for( int i=0; i<10; i++ )
  {
    vd->push_back( new VoronoiSite2d( 
                     Point( rand() * bx.xsize() / RAND_MAX + bx.xmin(),
                            rand() * bx.ysize() / RAND_MAX + bx.ymin(),
                            0 )));
  }
*/


/*
  BoundingBox bx(-1.1, 1, -1, 1.1 );
  Voronoi2d * vd= new Voronoi2d(bx);
  vd->push_back( new VoronoiSite2d( Point(0,1.01,0)  ));
  vd->push_back( new VoronoiSite2d( Point(0,-1,0)  ));
  vd->push_back( new VoronoiSite2d( Point(1.1,0,0)  ));
*/



  //Anisotropic diagram
  //point, a11,a22,a12

  Voronoi2d * vd= new Voronoi2d( BoundingBox(-2,2,-2,2) );
  vd->push_back( new VoronoiSite2d( Point(0,.3),1,11,2 ));
  vd->push_back( new VoronoiSite2d( Point(0,-.6),4,3,2 ));
  vd->push_back( new VoronoiSite2d( Point(.7,0),2,5,1 ));
  vd->push_back( new VoronoiSite2d( Point(-1,-.8), 2,10,0 ));
  vd->push_back( new VoronoiSite2d( Point(.5,1.5), 1,1,1.1 ));
  vd->push_back( new VoronoiSite2d( Point(-.4,-.4) ));
  vd->push_back( new VoronoiSite2d( Point(1,1.6) ));
  vd->push_back( new VoronoiSite2d( Point(1.34,.6) ));
//*/

/*  
  //Anisotropic diagram

  BoundingBox bx(-3, 3, -3, 3);
  Voronoi2d * vd= new Voronoi2d(bx);
  for( int i=0; i<5; i++ )
  {
    vd->push_back( new VoronoiSite2d( 
                     Point( rand() * bx.xsize() / RAND_MAX + bx.xmin(),
                            rand() * bx.ysize() / RAND_MAX + bx.ymin()  ),
                     1 ,
                     1 ,
                     2 ) );
  }
*/


/*  
   // Voronoi of Disks (power diagram)

  BoundingBox bx(0, 6.1, 0, 5.2);
  Voronoi2d * vd= new Voronoi2d(bx);
   // radius, point
  vd->push_back( new VoronoiSite2d( .5, Point(1,1,0)   ));
  vd->push_back( new VoronoiSite2d( .3, Point(2,1,0)     ));
  vd->push_back( new VoronoiSite2d( .4,  Point(3,2,0)   ));
  vd->push_back( new VoronoiSite2d( .9, Point(2.8,4.2,0)   ));
  vd->push_back( new VoronoiSite2d(   Point(2.8,0.8,0)   ));
  vd->push_back( new VoronoiSite2d(   Point(4.7,2.4,0)   ));
  vd->push_back( new VoronoiSite2d( .1, Point(.5,3.4,0)    ));
*/


  // Compute
  vd->set_smoothness(0.2); //maxsize
  vd->set_precision(0.01);  //minsize
  vd->run();


  //Output
  AXEL os((char *)"VoronoiDiag.axl");
  //print_boxes(os,vd->m_leaves) ;// output subdivision leaves(boxes)
  //print_boxes(os,vd->b_leaves) ;// output subdivision border(boxes)
  os << vd->m_graph ;  
 

  // Assign colors to sites
  int colors[3*vd->size()];
  for (unsigned i=0;i< 3*vd->size(); i++ )
  {
    colors[i]=  (int)(std::rand()*255.0 / RAND_MAX);
  }

  typedef Voronoi2d::Face Face;
  foreach(Face * f,vd->m_faces)
   {
     os.set_color(colors[3* f->get_index()],
                  colors[3* f->get_index()+1],
                  colors[3* f->get_index()+2] );
     os << f;
   }

  // Output the sites (as points)
   os<<"<pointset size=\""<<vd->size()<<"\" color=\"255 255 255\">\n";
  foreach(VoronoiSite2d * vs,vd->m_sites)
   {
     os<<
       vs->x() << " "<<
       vs->y() << " "<<
       vs->z() << "\n";
   }
  os<<"</pointset>\n";

  os.view();

  return EXIT_SUCCESS ;

}

# undef Kernel
# undef AXEL
# undef Point
# undef BoundingBox
# undef Voronoi2d
# undef VoronoiSite2d