00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013 # ifndef shape_mesher3d_algebraic_curve_hpp
00014 # define shape_mesher3d_algebraic_curve_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
00023 # include <shape/algebraic_surface.hpp>
00024
00025 # include <shape/bcell3d_algebraic_curve.hpp>
00026
00027
00028
00029
00030
00031
00032 # undef mesher3d_algebraic_curve
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 algebraic_surface<C,V>
00039 # define Cell3dAlgebraicCurve bcell3d_algebraic_curve<C,V>
00040 # define Cell3dAlgebraicSurface bcell3d_algebraic_surface<C,V>
00041 # define Cell3dParametricCurve bcell3d_parametric_curve<C,V>
00042 # define Cell3dList bcell3d_list<C,V>
00043 # define SELF mesher3d_algebraic_curve<C,V>
00044
00045
00046 namespace mmx { namespace shape {
00047
00048 TMPL struct mesher3d_algebraic_curve;
00049
00050 struct mesher3d_algebraic_curve_def {};
00051
00052 template<class C, class V>
00053 struct use<mesher3d_algebraic_curve_def,C, V>
00054 : public use<vertex_def,C,V>
00055 , public use<bcell3d_def,C,V>
00056 , public use<surface_def,C,V>
00057 {
00058
00059 typedef mesher3d_algebraic_curve<C, V> Topology3d;
00060
00061 };
00062
00063
00064 TMPL class bcell_list ;
00065 TMPL1 class surface ;
00066 template <class CELL> class node;
00067 template <class CELL> class kdtree;
00068
00069
00070 template<class C,class V=default_env>
00071 class mesher3d_algebraic_curve : public SHAPE_OF(V) {
00072 public:
00073 typedef typename SHAPE_OF(V) Shape;
00074 typedef vertex<C, 3, V> Point;
00075 typedef edge<C,V, Point> Edge;
00076 typedef face<C,V, Point> Face;
00077 typedef bcell<C,V> CellBase;
00078 typedef bcell3d_algebraic_curve<C,V> Cell;
00079 typedef surface<V> Surface;
00080
00081 typedef node<Cell *> Node;
00082 typedef kdtree<Cell *> Kdtree;
00083
00084 typedef typename Cell::BoundingBox BoundingBox;
00085
00086 mesher3d_algebraic_curve(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_algebraic_curve(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 * bcell);
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
00134 bool is_regular (Cell * bcell);
00135
00136
00137 bool post_process(Cell * bcell);
00138 bool sing_process(Cell * bcell);
00139 bool subdivide (Cell * bcell, 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
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
00212
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
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
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_algebraic_curve_def,C,V>::point_insertor(this->m_vertices, p);
00283 }
00284
00285 TMPL void
00286 SELF::insert(Edge* e) {
00287
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
00307 }
00308
00309 TMPL void
00310 SELF::clear() {
00311
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 Graphic* res = new Graphic (tp.nbv(), tp.nbe(), 0);
00325
00326 int c=0;
00327 foreach(Point* p, tp.vertices()) {
00328 res->vertices[3*c] =p->x();
00329 res->vertices[3*c+1]=p->y();
00330 res->vertices[3*c+2]=p->z();
00331 c++;
00332 }
00333
00334 if( tp.nbe()>0){
00335 int c=0;
00336 foreach(Edge* e, tp.edges()) {
00337 if(e->source()->index()<0) std::cerr<<"Pb in as_graphic"<<std::endl;
00338 res->edges[2*c] = e->source()->index();
00339 if(e->destination()->index()<0) std::cerr<<"Pb in as_graphic"<<std::endl;
00340 res->edges[2*c+1] = e->destination()->index();
00341 c++;
00342 }
00343 return res;
00344 }
00345 return NULL;
00346 }
00347
00348
00349 } ;
00350 } ;
00351 # undef TMPL
00352 # undef TMPL1
00353 # undef AlgebraicCurve
00354 # undef AlgebraicSurface
00355 # undef Cell
00356 # undef Cell3dAlgebraicCurve
00357 # undef Cell3dAlgebraicSurface
00358 # undef Cell3dParametricCurve
00359 # undef Cell3dList
00360 # undef Cell3dFactory
00361 # undef SELF
00362 # undef Shape
00363 # endif