00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012 # ifndef shape_semialgebraic2d_hpp
00013 # define shape_semialgebraic2d_hpp
00014
00015 # include <shape/mesher2d.hpp>
00016
00017
00018 # include <stack>
00019 # define STACK std::stack<Cell2d*>
00020
00021 # define TMPL template<class C, class V>
00022 # define Shape geometric<V>
00023 # define AlgebraicCurve algebraic_curve<C,V>
00024 # define SemiAlgebraicCurve semialgebraic_curve<C,V>
00025 # define ParametricCurve parametric_curve<C,V>
00026 # define Cell2dAlgebraicCurve bcell2d_algebraic_curve<C,V>
00027 # define Cell2dParametricCurve bcell2d_parametric_curve<C,V>
00028 # define Cell2dSemiAlgebraicCurve bcell2d_semialgebraic_curve<C,V>
00029 # define Cell2dInter bcell2d_intersection<C,V>
00030 # define SELF semialgebraic2d<C,V>
00031 # undef Cell
00032
00033 namespace mmx {
00034 namespace shape {
00035
00036
00037
00038 template<class C, class V=default_env>
00039 class semialgebraic2d: public mesher2d<C,V> {
00040
00041 public:
00042
00043 typedef typename mesher2d<C,V>::Point Point;
00044 typedef typename mesher2d<C,V>::Edge Edge;
00045 typedef typename mesher2d<C,V>::Face Face;
00046 typedef typename mesher2d<C,V>::BoundingBox BoundingBox;
00047 typedef typename mesher2d<C,V>::Cell Cell;
00048 typedef typename mesher2d<C,V>::Cell2d Cell2d;
00049 typedef typename mesher2d<C,V>::Curve Curve;
00050 typedef topology<C,V> Topology;
00051
00052 typedef node<Cell*> Node;
00053 typedef kdtree<Cell*> Kdtree;
00054
00055 public:
00056
00057 semialgebraic2d(void): mesher2d<C,V>(){ };
00058
00059 semialgebraic2d(Curve * curve) ;
00060 ~semialgebraic2d(void) { delete this->m_tree ; };
00061
00062 void run(void) ;
00063
00064
00065
00066
00067
00068 virtual void insert_singular(Point*);
00069
00070 protected:
00071 bool is_regular (Cell * bcell) ;
00072 bool insert_regular(Cell * bcell) ;
00073 bool singularity(Cell * bcell) ;
00074 bool subdivide(Cell * bcell, Node * node) ;
00075
00076
00077 private:
00078 Kdtree * m_tree ;
00079 std::list<Node *> m_nodes ;
00080
00081 };
00082
00083
00084
00085 TMPL void
00086 SELF::run() {
00087
00088 std::cout<<"Running subdivision" <<std::endl;
00089
00090 mesher2d<C,V>::run();
00091
00092 std::cout<<"Computing Semi-algebraic set." <<std::endl;
00093
00094 Seq<Cell2d*> nlist;
00095 this->b_leaves.dfs(nlist);
00096 Cell2d* pr;
00097 pr= nlist[nlist.size()-1];
00098
00099 foreach(Cell2d* cl2, nlist)
00100 {
00101 if ( cl2->is_corner() )
00102 pr=cl2;
00103 else {
00104 if ( (cl2->s_neighbors.size()==0 &&
00105 cl2->intersections(0).size()==0 ) ||
00106 (cl2->e_neighbors.size()==0 &&
00107 cl2->intersections(1).size()==0 ) ||
00108 (cl2->n_neighbors.size()==0 &&
00109 cl2->intersections(2).size()==0 ) ||
00110 (cl2->w_neighbors.size()==0 &&
00111 cl2->intersections(3).size()==0 ) )
00112 {
00113 this->b_leaves.push_edge(pr, this->b_leaves.next(cl2) ) ;
00114 this->b_leaves.delete_vertex( cl2 ) ;
00115 }
00116 else
00117 pr=cl2;
00118 }
00119 }
00120 std::cout<<"Border Ok." <<std::endl;
00121
00122
00123 nlist.clear();
00124 this->m_leaves.dfs(nlist);
00125 foreach(Cell2d* cl2, nlist)
00126 {
00127
00128 foreach(Cell2d* nb, cl2->neighbors() )
00129 this->m_leaves.push_edge( cl2,nb ) ;
00130 }
00131
00132 if (0)
00133 foreach(Cell2d* cl, nlist) {
00134 if (!cl->is_active() )
00135 {
00136 this->m_leaves.delete_vertex(cl);
00137 }
00138 }
00139
00140
00141 std::cout<<"Leaf graph Ok." <<std::endl;
00142
00143
00145
00146
00147
00148
00149
00150
00151
00152
00153
00154
00155
00156
00157
00158 Point *p= NULL;
00159 Face * f= NULL;
00160 int aux;
00161 int sgn(1);
00162 assert( nlist.size()>1);
00163
00164 Cell2d *s= NULL,
00165 *t= NULL,
00166 *b= NULL;
00167 STACK stack;
00168
00169
00170 foreach(Cell2d* cl, nlist)
00171 if ( cl->is_active() &&
00172
00173 cl->nb_intersect()==2
00174 && !cl->is_border() )
00175 stack.push(cl);
00176
00177 sgn=1;
00178
00179 while ( !stack.empty() )
00180 {
00181 s= stack.top();
00182 aux=s->m_gnode->aux();
00183
00184
00185
00186 if (aux!=0)
00187 {
00188 stack.pop();
00189 continue;
00190 }
00191
00192
00193 f= new Face();
00194
00195
00196
00197
00198 p= s->starting_point(sgn);
00199
00200
00201 p= s->pair(p,sgn);
00202 f->insert(p);
00203 b=s;
00204
00205
00206 do {
00207
00208
00209
00210
00211
00212 if( this->m_leaves.member(b) ) {
00213 aux=b->m_gnode->aux();
00214 b->m_gnode->aux(aux + (sgn==1 ? 2:-1) ); }
00215
00216 t= b->neighbor(p);
00217
00218 if ( t==NULL)
00219 {
00220
00221
00222
00223
00224 if (b->s_neighbors.size()==0 &&
00225 b->e_neighbors.size()==0 && b->intersections(1).size()==0)
00226 f->insert(new Point(b->xmax(),b->ymin(),0.0) );
00227 if (b->e_neighbors.size()==0 &&
00228 b->n_neighbors.size()==0 && b->intersections(2).size()==0)
00229 f->insert(new Point(b->xmax(),b->ymax(),0.0) );
00230 if (b->n_neighbors.size()==0 &&
00231 b->w_neighbors.size()==0 && b->intersections(3).size()==0)
00232 f->insert(new Point(b->xmin(),b->ymax(),0.0) );
00233 if (b->w_neighbors.size()==0 &&
00234 b->s_neighbors.size()==0 && b->intersections(0).size()==0)
00235 f->insert(new Point(b->xmin(),b->ymin(),0.0) );
00236
00237 b=this->b_leaves.next(b);
00238
00239
00240 if (b->s_neighbors.size()==0 &&
00241 b->e_neighbors.size()==0 && b->intersections(0).size()==0 )
00242 { f->insert(new Point(b->xmax(),b->ymin(),0.0) );
00243 } else if (b->e_neighbors.size()==0 &&
00244 b->n_neighbors.size()==0 && b->intersections(1).size()==0)
00245 { f->insert(new Point(b->xmax(),b->ymax(),0.0) );
00246 } else if (b->n_neighbors.size()==0 &&
00247 b->w_neighbors.size()==0 && b->intersections(2).size()==0)
00248 { f->insert(new Point(b->xmin(),b->ymax(),0.0) );
00249 } else if (b->w_neighbors.size()==0 &&
00250 b->s_neighbors.size()==0 && b->intersections(3).size()==0)
00251 { f->insert(new Point(b->xmin(),b->ymin(),0.0) );
00252 }
00253
00254 if ( this->m_leaves.member(b) )
00255 {
00256
00257 p= b->starting_point(sgn);
00258
00259 f->insert(p);
00260 p= b->pair(p,sgn);
00261
00262 f->insert(p);
00263 }
00264 }
00265 else
00266 {
00267 b=t;
00268 if ( b->m_singular.size()==1 )
00269 f->insert(b->m_singular[0]);
00270 p= b->pair(p,sgn);
00271 f->insert(p);
00272 }
00273
00274 } while (b!=s);
00275
00276
00277
00278 this->get_output()->m_faces<< f;
00279
00280
00281
00282 }
00283
00284 std::cout<<" # faces= "<< this->get_output()->m_faces.size() << std::endl;
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);
00296 }
00297
00298 TMPL bool
00299 SELF::singularity(Cell * cl) {
00300
00301 return cl->insert_singular(this) ;
00302 }
00303
00304 TMPL void
00305 SELF::insert_singular(Point * p) {
00306 this->m_specials<<p;
00307 }
00308
00309 TMPL bool
00310 SELF::subdivide(Cell * cl, Node * node) {
00311 int v=0;
00312
00313 Cell* left, * right;
00314 v = cl->subdivide(left, right) ;
00315
00316 node->m_left = new Node(node, left, Node::LEFT, v);
00317 m_nodes << node->m_left ;
00318 node->m_right = new Node(node, right, Node::RIGHT, v);
00319 m_nodes << node->m_right;
00320
00321 return true ;
00322 }
00323
00324
00325
00326 }
00327 }
00328
00329 # undef TMPL
00330 # undef AlgebraicCurve
00331 # undef SemiAlgebraicCurve
00332 # undef Cell
00333 # undef Cell2dAlgebraicCurve
00334 # undef Cell2dParametricCurve
00335 # undef Cell2dSemiAlgebraicCurve
00336 # undef Cell2dInter
00337 # undef Cell2dFactory
00338 # undef ParametricCurve
00339 # undef Curve
00340 # undef SELF
00341 # undef Shape
00342 # undef STACK
00343 # endif