00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012 # ifndef shape_mesher2d_hpp
00013 # define shape_mesher2d_hpp
00014
00015 # include <shape/vertex.hpp>
00016 # include <shape/bcell.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/bcell2d_factory.hpp>
00023 # include <shape/topology.hpp>
00024 # include <shape/tpl3d.hpp>
00025
00026 # include <stack>
00027 # define STACK std::stack<Cell2d*>
00028 # define Cell2dFactory bcell2d_factory<C,V>
00029
00030
00031 # define TMPL template<class C, class V>
00032 # define TMPL1 template<class V>
00033 # define REF REF_OF(V)
00034
00035
00036 # define AlgebraicCurve algebraic_curve<C,V>
00037
00038
00039
00040 # define Cell2dAlgebraicCurve bcell2d_algebraic_curve<C,REF>
00041
00042
00043
00044
00045
00046
00047 # define SELF mesher2d<C,V>
00048 # define Viewer viewer<axel,V>
00049 # undef Cell
00050
00051
00052 namespace mmx { namespace shape {
00053
00054
00055 TMPL1 class curve ;
00056 TMPL class bcell2d_parametric_curve ;
00057 TMPL class bcell2d_algebraic_curve;
00058 TMPL class bcell2d_list;
00059
00060 template<class C, class V=default_env>
00061 class mesher2d
00062 {
00063 public:
00064 typedef typename SHAPE_OF(V) Shape;
00065 typedef topology<C,V> Topology;
00066 typedef typename topology<C,V>::Point Point;
00067 typedef typename topology<C,V>::Edge Edge;
00068 typedef typename topology<C,V>::Face Face;
00069 typedef typename topology<C,V>::BoundingBox BoundingBox;
00070 typedef bcell<C,V> Cell;
00071 typedef bcell2d<C,REF> Cell2d;
00072 typedef curve<REF> Curve;
00073 typedef tpl3d<C,V> Output;
00074 typedef node<Cell*> Node;
00075 typedef kdtree<Cell*> Kdtree;
00076
00077
00078 public:
00079 mesher2d(void);
00080 virtual ~mesher2d(void) ;
00081
00082 void set_smoothness(double e) { m_smooth = e; }
00083 void set_precision (double e) { m_prec = e; }
00084
00085 double get_smoothness(void) { return m_smooth; }
00086 double get_precision (void) { return m_prec; }
00087
00088
00089 void set_input_bbox(const BoundingBox& bx);
00090 void add_input(Shape* s);
00091
00092 Cell* get_input_cell();
00093
00094 Output* get_output() {return m_output;}
00095
00096 void run(void) ;
00097
00098
00099
00100
00101
00102 virtual void insert_singular(Point*);
00103
00104 protected:
00105 bool is_regular (Cell * bcell) ;
00106 bool insert_regular(Cell * bcell) ;
00107 bool insert_singular(Cell * bcell) ;
00108 bool subdivide(Cell * bcell, Node * node) ;
00109
00110 std::list<Node *> m_nodes ;
00111
00112 private:
00113 Kdtree * m_tree ;
00114
00116 static bool active_cell(gNode<Cell2d*>* v)
00117 {
00118 return (v->get_aux()>0 );
00119 }
00120
00121 static void ccw_walk(gNode<Cell2d*>* v)
00122 {
00123
00124 }
00125
00126 public:
00127
00128 Graph<Point*> m_graph;
00129 Graph<Cell2d*> m_leaves;
00130 Graph<Cell2d*> b_leaves;
00131
00132
00133
00134
00135
00136
00137
00138
00139
00140 Seq<Point*> m_specials;
00141
00142
00143
00144
00145
00146 private:
00147 double m_smooth ;
00148 double m_prec ;
00149
00150 Seq<Shape*> m_input_objects;
00151 BoundingBox m_input_bbx;
00152
00153 Output* m_output;
00154 };
00155
00156
00157 TMPL
00158 SELF::mesher2d (void): m_smooth(0.1), m_prec(0.01) {
00159 m_tree = new Kdtree ;
00160 m_input_bbx = BoundingBox(0, 1, 0, 1);
00161 m_output = new Output;
00162 }
00163
00164 TMPL
00165 SELF::~mesher2d(void) {
00166 delete m_tree ;
00167 }
00168
00169 TMPL void
00170 SELF::set_input_bbox(const BoundingBox& box) {
00171 m_input_bbx=box;
00172 m_input_bbx.set_zmin(0);
00173 m_input_bbx.set_zmax(0);
00174 }
00175
00176 TMPL void
00177 SELF::add_input(Shape * object) {
00178 this->m_input_objects.push_back(object);
00179 }
00180
00181
00182 TMPL bcell<C,V>*
00183 SELF::get_input_cell() {
00184 if(this->m_input_objects.size() == 1)
00185 return Cell2dFactory::instance()->create(this->m_input_objects[0],this->m_input_bbx);
00186 else
00187 return Cell2dFactory::instance()->create(this->m_input_objects,this->m_input_bbx);
00188 }
00189 TMPL void
00190 SELF::run() {
00191
00192 Node* root = m_tree->root() ;
00193
00194
00195
00196 Cell* cl = this->get_input_cell();
00197
00198
00199 root->set_cell( (Cell*)cl ) ;
00200 this->m_nodes.push_back(root) ;
00201
00202
00203
00204
00205
00206 double maxsz = this->get_smoothness();
00207 double minsz = this->get_precision();
00208
00209 while(!m_nodes.empty() ) {
00210 Node * node = m_nodes.front() ;
00211 Cell * cl = node->get_cell() ;
00212 Cell2d * cl2= dynamic_cast<Cell2d*>(cl);
00213
00214
00215
00216 if(cl->is_active()) {
00217
00218
00219
00220 if(cl->size() > maxsz)
00221 {
00222
00223 this->subdivide(cl, node) ;
00224 }
00225 else if(this->is_regular(cl) )
00226 {
00227
00228 this->get_output()->insert_regular(cl) ;
00229
00230 cl2->m_gnode=
00231 this->m_leaves.push_vertex(cl2);
00232 if ( cl2->is_border() )
00233 this->b_leaves.push_vertex(cl2);
00234 }
00235 else if(cl->size() > minsz)
00236 {
00237
00238 this->subdivide(cl, node) ;
00239 }
00240 else
00241 {
00242 this->insert_singular(cl) ;
00243 cl2->m_gnode=
00244 this->m_leaves.push_vertex(cl2);
00245 if ( cl2->is_border() )
00246 this->b_leaves.push_vertex(cl2);
00247 }
00248 }
00249 else
00250 {
00251
00252
00253 if ( cl2->is_border() )
00254 this->b_leaves.push_vertex(cl2);
00255 }
00256 this->m_nodes.pop_front() ;
00257 }
00258
00259
00260 Seq<Cell2d*> nlist;
00261
00262
00263
00264 this->b_leaves.dfs(nlist);
00265 foreach(Cell2d* cl2, nlist)
00266 {
00267 if (cl2->s_neighbors.size()==0)
00268 foreach(Cell2d* nb, cl2->e_neighbors )
00269 if (nlist.member(nb) && nb->s_neighbors.size()==0)
00270 this->b_leaves.push_edge( cl2,nb ) ;
00271 if (cl2->e_neighbors.size()==0)
00272 foreach(Cell2d* nb, cl2->n_neighbors )
00273 if (nlist.member(nb) && nb->e_neighbors.size()==0)
00274 this->b_leaves.push_edge( cl2,nb ) ;
00275 if (cl2->n_neighbors.size()==0)
00276 foreach(Cell2d* nb, cl2->w_neighbors )
00277 if (nlist.member(nb)&& nb->n_neighbors.size()==0)
00278 this->b_leaves.push_edge( cl2,nb ) ;
00279 if (cl2->w_neighbors.size()==0)
00280 foreach(Cell2d* nb, cl2->s_neighbors )
00281 if (nlist.member(nb)&& nb->w_neighbors.size()==0)
00282 this->b_leaves.push_edge( cl2,nb ) ;
00283 }
00284 nlist.clear();
00285 }
00286
00287
00288 TMPL bool
00289 SELF::is_regular(Cell * cl) {
00290 return cl->is_regular();
00291 }
00292
00293 TMPL bool
00294 SELF::insert_regular(Cell * cl) {
00295 return cl->insert_regular(this->get_output());
00296 }
00297
00298 TMPL bool
00299 SELF::insert_singular(Cell * cl) {
00300 return cl->insert_singular(this->get_output()) ;
00301 }
00302
00303 TMPL void
00304 SELF::insert_singular(Point * p) {
00305 m_specials<<p;
00306 }
00307
00308 TMPL bool
00309 SELF::subdivide(Cell * cl, Node * node) {
00310 int v=0;
00311
00312 Cell* left, * right;
00313 v = cl->subdivide(left, right) ;
00314
00315 node->m_left = new Node(node, left, Node::LEFT, v);
00316 m_nodes << node->m_left ;
00317 node->m_right = new Node(node, right, Node::RIGHT, v);
00318 m_nodes << node->m_right;
00319
00320 return true ;
00321 }
00322
00323
00324
00325
00326
00327
00328
00329
00330
00331
00332
00333
00334
00335
00336
00337
00338
00339
00340
00341
00342
00343
00344
00345
00346
00347
00348
00349
00350
00351
00352
00353
00354
00355
00356
00357
00358
00359
00360
00361
00362
00363
00364
00365
00366
00367
00368
00369
00370
00371 }
00372 }
00373
00374 # undef TMPL
00375 # undef TMPL1
00376 # undef AlgebraicCurve
00377 # undef SemiAlgebraicCurve
00378 # undef Cell
00379 # undef Cell2dAlgebraicCurve
00380 # undef Cell2dParametricCurve
00381 # undef Cell2dSemiAlgebraicCurve
00382 # undef Cell2dList
00383 # undef Cell2dInter
00384 # undef Cell2dFactory
00385 # undef Curve
00386 # undef SELF
00387 # undef Shape
00388 # undef STACK
00389 # undef Viewer
00390
00391 # undef ParametricCurve
00392
00393 # undef Cell2dAlgebraicCurve
00394 #endif
00395
00396
00397
00398
00399