shape_doc 0.1
/Users/mourrain/Devel/mmx/shape/include/shape/mesher3d_curve_algebraic.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  * Julien Wintz & Bernard Mourrain
00007  *****************************************************************************
00008  *               Copyright (C) 2006 INRIA Sophia-Antipolis
00009  *****************************************************************************
00010  * Comments :
00011  ****************************************************************************/
00012 
00013 # ifndef shape_mesher3d_curve_algebraic_hpp
00014 # define shape_mesher3d_curve_algebraic_hpp
00015 
00016 # include <shape/vertex.hpp>
00017 # include <shape/edge.hpp>
00018 # include <shape/face.hpp>
00019 # include <shape/kdtree.hpp>
00020 # include <shape/list.hpp>
00021 # include <shape/graphic.hpp>
00022 //# include <shape/algebraic_curve.hpp>
00023 # include <shape/surface_algebraic.hpp>
00024 //# include <shape/cell.hpp>
00025 # include <shape/cell3d_algebraic_curve.hpp>
00026 //# include <shape/cell3d_surface_algebraic.hpp>
00027 //# include <shape/cell3d_list.hpp>
00028 //# include <shape/tpl3d.hpp>
00029 //# include <algorithm>
00030 //# include <shape/with_vertex.hpp>
00031 
00032 # undef mesher3d_curve_algebraic
00033 
00034 # define TMPL template<class C, class V>
00035 # define TMPL1 template<class V>
00036 
00037 # define AlgebraicCurve         algebraic_curve<C,V>
00038 # define AlgebraicSurface       surface_algebraic<C,V>
00039 # define Cell3dAlgebraicCurve   cell3d_algebraic_curve<C,V>
00040 # define Cell3dAlgebraicSurface cell3d_surface_algebraic<C,V>
00041 # define Cell3dParametricCurve  cell3d_parametric_curve<C,V>
00042 # define Cell3dList             cell3d_list<C,V>
00043 # define SELF                   mesher3d_curve_algebraic<C,V> 
00044 
00045 //====================================================================
00046 namespace mmx { namespace shape {
00047 //====================================================================
00048 TMPL struct mesher3d_curve_algebraic;
00049 
00050 struct mesher3d_curve_algebraic_def {};
00051 
00052 template<class V>
00053 struct use<mesher3d_curve_algebraic_def,V>
00054   : public use<vertex_def,V> 
00055   , public use<cell3d_def,V> 
00056   , public use<surface_def,V>
00057 { 
00058   //typedef typename use<tpl3d_def,V>::Point Point;
00059   typedef mesher3d_curve_algebraic<typename use<scalar_def,V>::Scalar,V> Topology3d; 
00060 
00061 };
00062 
00063 //--------------------------------------------------------------------
00064 TMPL class cell_list ;
00065 TMPL1 class surface ;
00066 template <class Object, class CELL> class node;
00067 template <class Object, class CELL> class kdtree;
00068 
00069 //====================================================================
00070 template<class C,class V=default_env>
00071 class mesher3d_curve_algebraic : public SHAPE_OF(V) {
00072 public:
00073   typedef typename SHAPE_OF(V)        Shape;
00074   typedef vertex<C,V>                 Point;  
00075   typedef edge<C,V, Point>            Edge;
00076   typedef face<C,V, Point>            Face;
00077   typedef cell<C,V>                   CellBase;
00078   typedef cell3d_algebraic_curve<C,V> Cell;
00079   typedef surface<V>                  Surface;
00080 
00081   typedef node<Surface *, Cell *>     Node;
00082   typedef kdtree<Surface *, Cell *>   Kdtree;
00083   //  typedef topology<C,V>                   Topology;
00084   typedef typename Cell::BoundingBox BoundingBox;
00085   
00086   mesher3d_curve_algebraic(const BoundingBox & bx)
00087     : m_maxprec(0.1), m_minprec(0.01), m_bbx(bx)  
00088   {
00089     m_tree = new Kdtree;
00090   }
00091 
00092   ~mesher3d_curve_algebraic(void) {
00093     delete m_tree ;
00094   }
00095 
00096   void set_precision (double eps) { m_minprec = eps; }
00097   void set_smoothness(double eps) { m_maxprec = eps; }
00098 
00099   void push_back(Shape * s)  { m_objects.push_back(s); }
00100   void operator<<(Shape * s) { m_objects.push_back(s); }
00101   unsigned count(void)       { return m_objects.size(); }
00102 
00103   int nbv() const { return m_vertices.size();}
00104   int nbe() const { return m_edges.size();}
00105   int nbf() const { return m_faces.size();}
00106 
00107   inline Seq<Point *>&       vertices(void)     { return m_vertices ; }
00108   inline const Seq<Point *>& vertices(void)const{ return m_vertices ; }
00109   inline Seq<Edge *>&        edges(void)        { return m_edges ; }
00110   inline const Seq<Edge *>&  edges(void) const  { return m_edges ; }
00111   inline Seq<Face *>&        faces(void)        { return m_faces ; }
00112   inline const Seq<Face *>&  faces(void) const  { return m_faces ; }
00113   
00114   virtual Point* vertices(int i)     { return m_vertices[i] ; }
00115   virtual Face*  faces(int i)        { return m_faces[i] ; }
00116   
00117   void run(void) ;
00118 
00119   virtual void insert(Point*);
00120   virtual void insert(Edge *);
00121   virtual void insert(Face *);
00122   virtual void insert(BoundingBox*, bool);
00123 
00125   virtual void clear();
00126 
00127           bool insert_regular (Cell * cell);
00128   virtual void insert_singular(Point*); ;
00129   virtual void insert_singular(BoundingBox* bx);
00130 
00131   const Kdtree* tree(void) const                 { return m_tree; }
00132 
00133   //  protected:
00134   bool is_regular  (Cell * cell);
00135 
00136 
00137   bool post_process(Cell * cell);
00138   bool sing_process(Cell * cell);
00139   bool subdivide   (Cell * cell, Node * node) ;
00140 
00141   int stopFlag;
00142 
00143 private:
00144   Kdtree*             m_tree ;
00145   std::list<Node *>   m_nodes ;
00146 
00147   Seq<BoundingBox*>   m_specials;
00148   Seq<Point *>        m_vertices ;
00149   Seq<Edge  *>        m_edges ;
00150   Seq<Face  *>        m_faces ;
00151 
00152   double              m_maxprec ;
00153   double              m_minprec ;
00154   
00155   BoundingBox         m_bbx;
00156   Seq<Shape*>         m_objects;
00157 
00158 };
00159 //--------------------------------------------------------------------
00160 TMPL void SELF::insert(BoundingBox *bx, bool cross=false) {
00161   Point 
00162     *p0= new Point(bx->xmin(),bx->ymin(),bx->zmin()),
00163     *p1= new Point(bx->xmin(),bx->ymax(),bx->zmin()),
00164     *p2= new Point(bx->xmax(),bx->ymax(),bx->zmin()),
00165     *p3= new Point(bx->xmax(),bx->ymin(),bx->zmin());
00166   
00167   this->insert(p0);this->insert(p1); this->insert(new Edge(p0,p1));
00168   this->insert(p1);this->insert(p2); this->insert(new Edge(p1,p2));
00169   this->insert(p2);this->insert(p3); this->insert(new Edge(p2,p3));
00170   this->insert(p3);this->insert(p0); this->insert(new Edge(p3,p0));
00171       
00172   Point 
00173     *q0= new Point(bx->xmin(),bx->ymin(),bx->zmax()),
00174     *q1= new Point(bx->xmin(),bx->ymax(),bx->zmax()),
00175     *q2= new Point(bx->xmax(),bx->ymax(),bx->zmax()),
00176     *q3= new Point(bx->xmax(),bx->ymin(),bx->zmax());
00177   
00178   this->insert(q0);this->insert(q1); this->insert(new Edge(q0,q1));
00179   this->insert(q1);this->insert(q2); this->insert(new Edge(q1,q2));
00180   this->insert(q2);this->insert(q3); this->insert(new Edge(q2,q3));
00181   this->insert(q3);this->insert(q0); this->insert(new Edge(q3,q0));
00182   
00183   this->insert(p0);this->insert(q0);this->insert(new Edge(p0,q0));
00184   this->insert(p1);this->insert(q1);this->insert(new Edge(p1,q1));
00185   this->insert(p2);this->insert(q2);this->insert(new Edge(p2,q2));
00186   this->insert(p3);this->insert(q3);this->insert(new Edge(p3,q3));
00187 
00188   if(cross) {
00189     Point 
00190       *r0= new Point(bx->xmin(),bx->ymin(),bx->zmax()),
00191       *r1= new Point(bx->xmin(),bx->ymax(),bx->zmax()),
00192       *r2= new Point(bx->xmax(),bx->ymax(),bx->zmax()),
00193       *r3= new Point(bx->xmax(),bx->ymin(),bx->zmax());
00194 
00195     this->insert(r0);this->insert(r2);this->insert(new Edge(r0,r2));
00196     this->insert(r1);this->insert(r3);this->insert(new Edge(r1,r3));
00197   }
00198 }
00199 //======================================================================
00200 TMPL void 
00201 SELF::run(void) {
00202   //no singularities are detected in the topology of the space algebraic curve. stopFlag is set to false(0)
00203   stopFlag=0;
00204   Node* root = m_tree->root();
00205 
00206   if(this->m_objects.size()==0) return;
00207 
00208   AlgebraicCurve* ic = dynamic_cast<AlgebraicCurve *>(*(this->m_objects.begin()));
00209   Cell* cl = new Cell3dAlgebraicCurve(ic, this->m_bbx);
00210 
00211 //   foreach(Shape* s, m_objects) 
00212 //     cl->push_back(SurfaceCellFactory::instance()->create(s,m_bbx)) ;
00213   
00214   root->set_cell(cl);
00215   m_nodes.push_back(root) ;
00216      
00217   double maxsz = this->m_maxprec*m_tree->root()->get_cell()->size();
00218   double minsz = this->m_minprec*m_tree->root()->get_cell()->size();
00219 
00220   while(!m_nodes.empty()) {
00221     Node* node = m_nodes.front() ;
00222     Cell* cl = node->get_cell() ;
00223 
00224     if(cl->is_active()) {
00225       if(cl->size() > maxsz) 
00226         { 
00227           this->subdivide(cl, node) ;
00228         } 
00229       else if(this->is_regular(cl)) 
00230         { 
00231           this->insert_regular(cl) ;
00232         } 
00233       else if(cl->size() > minsz ) 
00234         { 
00235           this->subdivide(cl, node) ;
00236         } 
00237       else { 
00238                   this->sing_process(cl);
00239                   //singularities are detected in the topology of the space algebraic curve. stopFlag is set to true(1)
00240                   stopFlag=1;
00241       }
00242     }
00243     m_nodes.pop_front() ;
00244   }
00245 }
00246 
00247 TMPL bool 
00248 SELF::is_regular(Cell* cl) {
00249   return cl->is_regular();
00250 }
00251 
00252 TMPL bool 
00253 SELF::insert_regular(Cell* cl) {
00254   return cl->inserted_regular_in(this);
00255 }
00256 
00257 TMPL bool 
00258 SELF::post_process(Cell* cl) {
00259   return true;
00260 }
00261 
00262 TMPL bool 
00263 SELF::sing_process(Cell* cl) {
00264   //the method insert_singular(Topology* t) from cell3d_algebraic_curve.hpp is used here!!
00265   return cl->inserted_singular_in(this) ;
00266 }
00267 
00268 TMPL bool 
00269 SELF::subdivide(Cell* cl, Node * node) {
00270 
00271   int v = 0;
00272   Cell * left, * right;
00273   v = cl->subdivide(left, right) ;
00274   node->m_left = new Node(node, left,  Node::LEFT , v) ; m_nodes << node->m_left ;
00275   node->m_right= new Node(node, right, Node::RIGHT, v) ; m_nodes << node->m_right;
00276 
00277   return true ;
00278 }
00279 
00280 TMPL void 
00281 SELF::insert(Point * p) {
00282   use<mesher3d_curve_algebraic_def,V>::point_insertor(this->m_vertices, p); 
00283 }
00284 
00285 TMPL void 
00286 SELF::insert(Edge* e) {
00287   //  std::cout<<m_edges.size()<<"| "<<e->source()->index()<<" "<<e->destination()->index()<<std::endl;
00288   this->m_edges<<e;
00289 }
00290 
00291 TMPL void 
00292 SELF::insert(Face * f) {
00293   this->m_faces<<f;
00294 }
00295 
00296 TMPL void 
00297 SELF::insert_singular(BoundingBox * bx) {
00298         std::cout<<"Begin insert_singular"<<endl;
00299         this->m_specials<<bx;
00300         std::cout<<"End insert_singular"<<endl;
00301 }
00302 
00303 TMPL void 
00304 SELF::insert_singular(Point * ) {
00305   std::cout<<"Begin insert_singular Uff"<<endl;
00306   //  m_specials<<p;
00307 }
00308 
00309 TMPL void
00310 SELF::clear() {
00311         // Should we clean up these points?
00312         this->m_faces.resize( 0 );
00313         this->m_edges.resize( 0 );
00314         this->m_vertices.resize( 0 );
00315 }
00316 //--------------------------------------------------------------------
00317 TMPL
00318 graphic<C,V>* as_graphic(const SELF& tp) {
00319 
00320   typedef typename SELF::Point  Point; 
00321   typedef typename SELF::Edge   Edge;
00322   typedef graphic<C,V>          Graphic;
00323 
00324   int nbi=0;
00325   
00326   if( tp.nbe()>0){
00327     nbi=2*tp.nbe();
00328     int c=0;
00329     Graphic* res = new Graphic( Graphic::E_LINE, nbi, nbi);
00330     foreach(Edge* e, tp.edges()) { 
00331       res->vertices[3*c]  =e->source()->x();
00332       res->vertices[3*c+1]=e->source()->y();
00333       res->vertices[3*c+2]=e->source()->z();
00334       c++;
00335       res->vertices[3*c]  =e->destination()->x();
00336       res->vertices[3*c+1]=e->destination()->y();
00337       res->vertices[3*c+2]=e->destination()->z();
00338       c++;
00339       res->indices[c-2]=c-1;
00340       res->indices[c-1]=c-2;
00341     }
00342     return res;
00343   }
00344   return NULL;
00345 }
00346 
00347 //====================================================================
00348 } ; // namespace shape
00349 } ; // namespace mmx
00350 # undef TMPL
00351 # undef TMPL1
00352 # undef AlgebraicCurve 
00353 # undef AlgebraicSurface
00354 # undef Cell
00355 # undef Cell3dAlgebraicCurve
00356 # undef Cell3dAlgebraicSurface
00357 # undef Cell3dParametricCurve
00358 # undef Cell3dList
00359 # undef Cell3dFactory
00360 # undef SELF 
00361 # undef Shape
00362 # endif