[5328] | 1 | |
---|
[4611] | 2 | |
---|
| 3 | |
---|
| 4 | |
---|
| 5 | |
---|
| 6 | |
---|
| 7 | |
---|
| 8 | |
---|
| 9 | |
---|
| 10 | |
---|
| 11 | |
---|
| 12 | |
---|
| 13 | |
---|
[1889] | 14 | #include <osgUtil/DelaunayTriangulator> |
---|
[5928] | 15 | |
---|
[4611] | 16 | |
---|
[1889] | 17 | |
---|
| 18 | #include <osg/GL> |
---|
| 19 | #include <osg/Vec3> |
---|
| 20 | #include <osg/Array> |
---|
| 21 | #include <osg/Notify> |
---|
| 22 | |
---|
| 23 | #include <algorithm> |
---|
| 24 | #include <set> |
---|
[4611] | 25 | #include <map> //GWM July 2005 map is used in constraints. |
---|
[5985] | 26 | #include <osgUtil/Tessellator> // tessellator triangulates the constrained triangles |
---|
[10764] | 27 | #include <stdlib.h> |
---|
| 28 | #include <iterator> |
---|
[1889] | 29 | |
---|
| 30 | namespace osgUtil |
---|
| 31 | { |
---|
| 32 | |
---|
| 33 | |
---|
| 34 | |
---|
| 35 | |
---|
| 36 | |
---|
| 37 | |
---|
| 38 | |
---|
| 39 | inline osg::Vec3 compute_circumcircle( |
---|
| 40 | const osg::Vec3 &a, |
---|
| 41 | const osg::Vec3 &b, |
---|
| 42 | const osg::Vec3 &c) |
---|
| 43 | { |
---|
| 44 | float D = |
---|
| 45 | (a.x() - c.x()) * (b.y() - c.y()) - |
---|
| 46 | (b.x() - c.x()) * (a.y() - c.y()); |
---|
| 47 | |
---|
[4358] | 48 | float cx, cy, r2; |
---|
| 49 | |
---|
| 50 | if(D==0.0) |
---|
| 51 | { |
---|
| 52 | |
---|
| 53 | |
---|
| 54 | |
---|
| 55 | |
---|
| 56 | cx = (a.x()+b.x()+c.x())/3.0; |
---|
| 57 | cy = (a.y()+b.y()+c.y())/3.0; |
---|
| 58 | r2 = 0.0; |
---|
| 59 | } |
---|
| 60 | else |
---|
| 61 | { |
---|
| 62 | cx = |
---|
[1889] | 63 | (((a.x() - c.x()) * (a.x() + c.x()) + |
---|
| 64 | (a.y() - c.y()) * (a.y() + c.y())) / 2 * (b.y() - c.y()) - |
---|
| 65 | ((b.x() - c.x()) * (b.x() + c.x()) + |
---|
| 66 | (b.y() - c.y()) * (b.y() + c.y())) / 2 * (a.y() - c.y())) / D; |
---|
| 67 | |
---|
[4358] | 68 | cy = |
---|
[1889] | 69 | (((b.x() - c.x()) * (b.x() + c.x()) + |
---|
| 70 | (b.y() - c.y()) * (b.y() + c.y())) / 2 * (a.x() - c.x()) - |
---|
| 71 | ((a.x() - c.x()) * (a.x() + c.x()) + |
---|
| 72 | (a.y() - c.y()) * (a.y() + c.y())) / 2 * (b.x() - c.x())) / D; |
---|
| 73 | |
---|
[4611] | 74 | |
---|
| 75 | |
---|
| 76 | |
---|
| 77 | r2 = sqrt((c.x() - cx) * (c.x() - cx) + (c.y() - cy) * (c.y() - cy)); |
---|
[4358] | 78 | } |
---|
[1889] | 79 | return osg::Vec3(cx, cy, r2); |
---|
| 80 | } |
---|
| 81 | |
---|
| 82 | |
---|
[4611] | 83 | |
---|
[1889] | 84 | |
---|
| 85 | inline bool point_in_circle(const osg::Vec3 &point, const osg::Vec3 &circle) |
---|
| 86 | { |
---|
| 87 | float r2 = |
---|
| 88 | (point.x() - circle.x()) * (point.x() - circle.x()) + |
---|
| 89 | (point.y() - circle.y()) * (point.y() - circle.y()); |
---|
[4611] | 90 | return r2 <= circle.z()*circle.z(); |
---|
| 91 | |
---|
[1889] | 92 | } |
---|
| 93 | |
---|
| 94 | |
---|
| 95 | |
---|
| 96 | |
---|
| 97 | |
---|
| 98 | |
---|
| 99 | |
---|
| 100 | typedef GLuint Vertex_index; |
---|
| 101 | |
---|
| 102 | |
---|
| 103 | |
---|
| 104 | |
---|
| 105 | |
---|
| 106 | |
---|
| 107 | class Edge { |
---|
| 108 | public: |
---|
| 109 | |
---|
| 110 | |
---|
[4611] | 111 | struct Less |
---|
| 112 | { |
---|
[1889] | 113 | inline bool operator()(const Edge &e1, const Edge &e2) const |
---|
| 114 | { |
---|
| 115 | if (e1.ibs() < e2.ibs()) return true; |
---|
| 116 | if (e1.ibs() > e2.ibs()) return false; |
---|
| 117 | if (e1.ies() < e2.ies()) return true; |
---|
| 118 | return false; |
---|
| 119 | } |
---|
| 120 | }; |
---|
| 121 | |
---|
[9630] | 122 | Edge(): ib_(0), ie_(0), ibs_(0), ies_(0), duplicate_(false) {} |
---|
[1950] | 123 | Edge(Vertex_index ib, Vertex_index ie) : ib_(ib), ie_(ie), ibs_(osg::minimum(ib, ie)), ies_(osg::maximum(ib, ie)), duplicate_(false) {} |
---|
[1889] | 124 | |
---|
| 125 | |
---|
| 126 | inline Vertex_index ib() const { return ib_; } |
---|
| 127 | |
---|
| 128 | |
---|
| 129 | inline Vertex_index ie() const { return ie_; } |
---|
| 130 | |
---|
| 131 | |
---|
| 132 | inline Vertex_index ibs() const { return ibs_; } |
---|
| 133 | |
---|
| 134 | |
---|
| 135 | inline Vertex_index ies() const { return ies_; } |
---|
| 136 | |
---|
| 137 | |
---|
| 138 | inline bool get_duplicate() const { return duplicate_; } |
---|
| 139 | |
---|
| 140 | |
---|
| 141 | inline void set_duplicate(bool v) { duplicate_ = v; } |
---|
| 142 | |
---|
| 143 | private: |
---|
| 144 | Vertex_index ib_, ie_; |
---|
| 145 | Vertex_index ibs_, ies_; |
---|
| 146 | bool duplicate_; |
---|
| 147 | }; |
---|
| 148 | |
---|
| 149 | |
---|
| 150 | |
---|
| 151 | |
---|
[4611] | 152 | class Triangle |
---|
| 153 | { |
---|
[1889] | 154 | public: |
---|
[9630] | 155 | |
---|
| 156 | Triangle(): |
---|
| 157 | a_(0), |
---|
| 158 | b_(0), |
---|
| 159 | c_(0) {} |
---|
| 160 | |
---|
| 161 | |
---|
[1889] | 162 | Triangle(Vertex_index a, Vertex_index b, Vertex_index c, osg::Vec3Array *points) |
---|
| 163 | : a_(a), |
---|
| 164 | b_(b), |
---|
| 165 | c_(c), |
---|
| 166 | cc_(compute_circumcircle((*points)[a_], (*points)[b_], (*points)[c_])) |
---|
| 167 | { |
---|
| 168 | edge_[0] = Edge(a_, b_); |
---|
| 169 | edge_[1] = Edge(b_, c_); |
---|
| 170 | edge_[2] = Edge(c_, a_); |
---|
| 171 | } |
---|
| 172 | |
---|
[9630] | 173 | Triangle& operator = (const Triangle& rhs) |
---|
| 174 | { |
---|
| 175 | if (&rhs==this) return *this; |
---|
| 176 | |
---|
| 177 | a_ = rhs.a_; |
---|
| 178 | b_ = rhs.b_; |
---|
| 179 | c_ = rhs.c_; |
---|
| 180 | cc_ = rhs.cc_; |
---|
| 181 | edge_[0] = rhs.edge_[0]; |
---|
| 182 | edge_[1] = rhs.edge_[1]; |
---|
| 183 | edge_[2] = rhs.edge_[2]; |
---|
| 184 | |
---|
| 185 | return *this; |
---|
| 186 | } |
---|
| 187 | |
---|
[1889] | 188 | inline Vertex_index a() const { return a_; } |
---|
| 189 | inline Vertex_index b() const { return b_; } |
---|
| 190 | inline Vertex_index c() const { return c_; } |
---|
[5810] | 191 | inline void incrementa(const int delta) { a_+=delta; } |
---|
| 192 | inline void incrementb(const int delta) { b_+=delta; } |
---|
| 193 | inline void incrementc(const int delta) { c_+=delta; } |
---|
[1889] | 194 | |
---|
| 195 | inline const Edge &get_edge(int idx) const { return edge_[idx]; } |
---|
| 196 | inline const osg::Vec3 &get_circumcircle() const { return cc_; } |
---|
| 197 | |
---|
[4611] | 198 | inline osg::Vec3 compute_centroid(const osg::Vec3Array *points) const |
---|
| 199 | { |
---|
| 200 | return ((*points)[a_] +(*points)[b_] + (*points)[c_])/3; |
---|
| 201 | } |
---|
| 202 | |
---|
[1889] | 203 | inline osg::Vec3 compute_normal(osg::Vec3Array *points) const |
---|
| 204 | { |
---|
| 205 | osg::Vec3 N = ((*points)[b_] - (*points)[a_]) ^ ((*points)[c_] - (*points)[a_]); |
---|
| 206 | return N / N.length(); |
---|
| 207 | } |
---|
| 208 | |
---|
[4611] | 209 | bool isedge(const unsigned int ip1,const unsigned int ip2) const |
---|
| 210 | { |
---|
| 211 | bool isedge=ip1==a() && ip2==b(); |
---|
| 212 | if (!isedge) |
---|
| 213 | { |
---|
| 214 | isedge=ip1==b() && ip2==c(); |
---|
| 215 | if (!isedge) |
---|
| 216 | { |
---|
| 217 | isedge=ip1==c() && ip2==a(); |
---|
| 218 | } |
---|
| 219 | } |
---|
| 220 | return isedge; |
---|
| 221 | } |
---|
| 222 | |
---|
| 223 | |
---|
[1889] | 224 | |
---|
[4611] | 225 | const bool intersected(const unsigned int ip1,const unsigned int ip2,const osg::Vec2 p1 ,const osg::Vec2 p2,const int iedge, osg::Vec3Array *points) const |
---|
| 226 | { |
---|
| 227 | |
---|
| 228 | Vertex_index ie1,ie2; |
---|
| 229 | if (iedge==0) |
---|
| 230 | { |
---|
| 231 | ie1=a(); |
---|
| 232 | ie2=b(); |
---|
| 233 | } |
---|
| 234 | else if (iedge==1) |
---|
| 235 | { |
---|
| 236 | ie1=b(); |
---|
| 237 | ie2=c(); |
---|
| 238 | } |
---|
| 239 | else if (iedge==2) |
---|
| 240 | { |
---|
| 241 | ie1=c(); |
---|
| 242 | ie2=a(); |
---|
| 243 | } |
---|
| 244 | if (ip1==ie1 || ip2==ie1) return false; |
---|
| 245 | if (ip1==ie2 || ip2==ie2) return false; |
---|
| 246 | |
---|
| 247 | osg::Vec2 tp1((*points)[ie1].x(),(*points)[ie1].y()); |
---|
| 248 | osg::Vec2 tp2((*points)[ie2].x(),(*points)[ie2].y()); |
---|
| 249 | return intersect(tp1,tp2,p1,p2); |
---|
| 250 | } |
---|
| 251 | |
---|
| 252 | bool intersectedby(const osg::Vec2 p1,const osg::Vec2 p2,osg::Vec3Array *points) const { |
---|
| 253 | |
---|
| 254 | osg::Vec2 tp1((*points)[a()].x(),(*points)[a()].y()); |
---|
| 255 | osg::Vec2 tp2((*points)[b()].x(),(*points)[b()].y()); |
---|
| 256 | osg::Vec2 tp3((*points)[c()].x(),(*points)[c()].y()); |
---|
| 257 | bool ip=intersect(tp1,tp2,p1,p2); |
---|
| 258 | if (!ip) |
---|
| 259 | { |
---|
| 260 | ip=intersect(tp2,tp3,p1,p2); |
---|
| 261 | if (!ip) |
---|
| 262 | { |
---|
| 263 | ip=intersect(tp3,tp1,p1,p2); |
---|
| 264 | } |
---|
| 265 | } |
---|
| 266 | return ip; |
---|
| 267 | } |
---|
| 268 | int whichEdge(osg::Vec3Array *points,const osg::Vec2 p1, const osg::Vec2 p2, |
---|
| 269 | const unsigned int e1,const unsigned int e2) const |
---|
| 270 | { |
---|
| 271 | int icut=0; |
---|
| 272 | |
---|
| 273 | |
---|
| 274 | osg::Vec2 tp1((*points)[a()].x(),(*points)[a()].y()); |
---|
| 275 | osg::Vec2 tp2((*points)[b()].x(),(*points)[b()].y()); |
---|
| 276 | osg::Vec2 tp3((*points)[c()].x(),(*points)[c()].y()); |
---|
| 277 | bool ip=intersect(tp2,tp3,p1,p2); |
---|
| 278 | if (ip && (a()==e1 || a()==e2)) { return 1;} |
---|
| 279 | ip=intersect(tp3,tp1,p1,p2); |
---|
| 280 | if (ip && (b()==e1 || b()==e2)) { return 2;} |
---|
| 281 | ip=intersect(tp1,tp2,p1,p2); |
---|
| 282 | if (ip && (c()==e1 || c()==e2)) { return 3;} |
---|
| 283 | return icut; |
---|
| 284 | } |
---|
| 285 | |
---|
| 286 | bool usesVertex(const unsigned int ip) const |
---|
| 287 | { |
---|
| 288 | return ip==a_ || ip==b_ || ip==c_; |
---|
| 289 | } |
---|
| 290 | |
---|
[4616] | 291 | int lineBisectTest(const osg::Vec3 apt,const osg::Vec3 bpt,const osg::Vec3 cpt, const osg::Vec2 p2) const |
---|
[4611] | 292 | { |
---|
| 293 | osg::Vec2 vp2tp=p2-osg::Vec2(apt.x(), apt.y()); |
---|
| 294 | |
---|
| 295 | |
---|
| 296 | osg::Vec2 vecba=osg::Vec2(bpt.x(), bpt.y())-osg::Vec2(apt.x(), apt.y()); |
---|
| 297 | osg::Vec2 vecca=osg::Vec2(cpt.x(), cpt.y())-osg::Vec2(apt.x(), apt.y()); |
---|
| 298 | float cprodzba=vp2tp.x()*vecba.y() - vp2tp.y()*vecba.x(); |
---|
| 299 | float cprodzca=vp2tp.x()*vecca.y() - vp2tp.y()*vecca.x(); |
---|
[4616] | 300 | |
---|
[4611] | 301 | if (cprodzba*cprodzca<0) |
---|
| 302 | { |
---|
| 303 | |
---|
| 304 | osg::Vec2 tp1(bpt.x(),bpt.y()); |
---|
| 305 | osg::Vec2 tp2(cpt.x(),cpt.y()); |
---|
| 306 | osg::Vec2 tp3(apt.x(),apt.y()); |
---|
| 307 | bool ip=intersect(tp1,tp2,tp3,p2); |
---|
| 308 | if (ip) return 1; |
---|
| 309 | } |
---|
| 310 | return 0; |
---|
| 311 | } |
---|
| 312 | |
---|
| 313 | int lineBisects(osg::Vec3Array *points, const unsigned int ip1, const osg::Vec2 p2) const |
---|
| 314 | { |
---|
| 315 | |
---|
| 316 | |
---|
| 317 | |
---|
| 318 | |
---|
| 319 | if (a_==ip1) |
---|
| 320 | { |
---|
| 321 | |
---|
| 322 | osg::Vec3 apt=(*points)[a_]; |
---|
| 323 | osg::Vec3 bpt=(*points)[b_]; |
---|
| 324 | osg::Vec3 cpt=(*points)[c_]; |
---|
[4616] | 325 | return lineBisectTest(apt,bpt,cpt,p2)?1:0; |
---|
[4611] | 326 | } |
---|
| 327 | else if (b_==ip1) |
---|
| 328 | { |
---|
| 329 | |
---|
| 330 | osg::Vec3 apt=(*points)[b_]; |
---|
| 331 | osg::Vec3 bpt=(*points)[c_]; |
---|
| 332 | osg::Vec3 cpt=(*points)[a_]; |
---|
[4616] | 333 | return lineBisectTest(apt,bpt,cpt,p2)?2:0; |
---|
[4611] | 334 | } |
---|
| 335 | else if (c_==ip1) |
---|
| 336 | { |
---|
| 337 | |
---|
| 338 | osg::Vec3 apt=(*points)[c_]; |
---|
| 339 | osg::Vec3 bpt=(*points)[a_]; |
---|
| 340 | osg::Vec3 cpt=(*points)[b_]; |
---|
[4616] | 341 | return lineBisectTest(apt,bpt,cpt,p2)?3:0; |
---|
[4611] | 342 | } |
---|
| 343 | return 0; |
---|
| 344 | } |
---|
| 345 | |
---|
[1889] | 346 | private: |
---|
[4611] | 347 | |
---|
[9630] | 348 | |
---|
[4611] | 349 | bool intersect(const osg::Vec2 p1,const osg::Vec2 p2,const osg::Vec2 p3,const osg::Vec2 p4) const |
---|
| 350 | { |
---|
| 351 | |
---|
| 352 | |
---|
| 353 | |
---|
| 354 | float det=((p4.y()-p3.y())*(p2.x()-p1.x())-(p4.x()-p3.x())*(p2.y()-p1.y())); |
---|
| 355 | if (det!=0) |
---|
| 356 | { |
---|
| 357 | |
---|
| 358 | float ua=((p4.x()-p3.x())*(p1.y()-p3.y())-(p4.y()-p3.y())*(p1.x()-p3.x()))/det; |
---|
| 359 | float ub=((p2.x()-p1.x())*(p1.y()-p3.y())-(p2.y()-p1.y())*(p1.x()-p3.x()))/det; |
---|
| 360 | if (ua> 0.00 && ua< 1 && ub> 0.0000 && ub< 1) |
---|
| 361 | { |
---|
| 362 | return true; |
---|
| 363 | } |
---|
| 364 | } |
---|
| 365 | return false; |
---|
| 366 | } |
---|
| 367 | |
---|
[1889] | 368 | Vertex_index a_; |
---|
| 369 | Vertex_index b_; |
---|
| 370 | Vertex_index c_; |
---|
| 371 | osg::Vec3 cc_; |
---|
| 372 | Edge edge_[3]; |
---|
| 373 | }; |
---|
| 374 | |
---|
[4611] | 375 | typedef std::list<Triangle> Triangle_list; |
---|
[1889] | 376 | |
---|
| 377 | |
---|
| 378 | bool Sample_point_compare(const osg::Vec3 &p1, const osg::Vec3 &p2) |
---|
| 379 | { |
---|
[4611] | 380 | |
---|
| 381 | |
---|
| 382 | if (p1.x() != p2.x()) return p1.x() < p2.x(); |
---|
| 383 | if (p1.y() != p2.y()) return p1.y() < p2.y(); |
---|
| 384 | osg::notify(osg::INFO) << "Two points are coincident at "<<p1.x() <<","<<p1.y() << std::endl; |
---|
| 385 | return p1.z() < p2.z(); |
---|
[1889] | 386 | } |
---|
| 387 | |
---|
| 388 | |
---|
| 389 | |
---|
| 390 | typedef std::set<Edge, Edge::Less> Edge_set; |
---|
| 391 | |
---|
| 392 | |
---|
| 393 | DelaunayTriangulator::DelaunayTriangulator(): |
---|
| 394 | osg::Referenced() |
---|
| 395 | { |
---|
| 396 | } |
---|
| 397 | |
---|
| 398 | DelaunayTriangulator::DelaunayTriangulator(osg::Vec3Array *points, osg::Vec3Array *normals): |
---|
| 399 | osg::Referenced(), |
---|
| 400 | points_(points), |
---|
| 401 | normals_(normals) |
---|
| 402 | { |
---|
| 403 | } |
---|
| 404 | |
---|
| 405 | DelaunayTriangulator::DelaunayTriangulator(const DelaunayTriangulator ©, const osg::CopyOp ©op): |
---|
| 406 | osg::Referenced(copy), |
---|
| 407 | points_(static_cast<osg::Vec3Array *>(copyop(copy.points_.get()))), |
---|
| 408 | normals_(static_cast<osg::Vec3Array *>(copyop(copy.normals_.get()))), |
---|
| 409 | prim_tris_(static_cast<osg::DrawElementsUInt *>(copyop(copy.prim_tris_.get()))) |
---|
| 410 | { |
---|
| 411 | } |
---|
| 412 | |
---|
| 413 | DelaunayTriangulator::~DelaunayTriangulator() |
---|
| 414 | { |
---|
| 415 | } |
---|
| 416 | |
---|
[4611] | 417 | const Triangle * getTriangleWithEdge(const unsigned int ip1,const unsigned int ip2, const Triangle_list *triangles) |
---|
| 418 | { |
---|
| 419 | Triangle_list::const_iterator trconnitr; |
---|
| 420 | int idx=0; |
---|
| 421 | for (trconnitr=triangles->begin(); trconnitr!=triangles->end(); ) |
---|
| 422 | { |
---|
| 423 | if (trconnitr->isedge (ip1,ip2)) |
---|
| 424 | { |
---|
| 425 | |
---|
| 426 | return &(*trconnitr); |
---|
| 427 | } |
---|
| 428 | ++trconnitr; |
---|
| 429 | idx++; |
---|
| 430 | } |
---|
| 431 | return NULL; |
---|
| 432 | } |
---|
| 433 | |
---|
[6446] | 434 | int DelaunayTriangulator::getindex(const osg::Vec3 &pt,const osg::Vec3Array *points) |
---|
[4611] | 435 | { |
---|
| 436 | |
---|
| 437 | for (unsigned int i=0; i<points->size(); i++) |
---|
| 438 | { |
---|
| 439 | if (pt.x()==(*points)[i].x() &&pt.y()==(*points)[i].y() ) |
---|
| 440 | { |
---|
| 441 | return i; |
---|
| 442 | } |
---|
| 443 | } |
---|
| 444 | return -1; |
---|
| 445 | } |
---|
| 446 | |
---|
| 447 | Triangle_list fillHole(osg::Vec3Array *points, std::vector<unsigned int> vindexlist) |
---|
| 448 | { |
---|
| 449 | |
---|
| 450 | Triangle_list triangles; |
---|
| 451 | osg::ref_ptr<osg::Geometry> gtess=new osg::Geometry; |
---|
| 452 | osg::ref_ptr<osg::Vec3Array> constraintverts=new osg::Vec3Array; |
---|
[5928] | 453 | osg::ref_ptr<osgUtil::Tessellator> tscx=new osgUtil::Tessellator; |
---|
[4611] | 454 | |
---|
| 455 | for (std::vector<unsigned int>::iterator itint=vindexlist.begin(); itint!=vindexlist.end(); itint++) |
---|
| 456 | { |
---|
| 457 | |
---|
| 458 | constraintverts->push_back((*points)[*itint]); |
---|
| 459 | } |
---|
| 460 | |
---|
| 461 | unsigned int npts=vindexlist.size(); |
---|
| 462 | |
---|
| 463 | gtess->setVertexArray(constraintverts.get()); |
---|
| 464 | gtess->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::POLYGON,0,npts)); |
---|
[5928] | 465 | tscx->setTessellationNormal(osg::Vec3(0.0,0.0,1.0)); |
---|
| 466 | tscx->setTessellationType(osgUtil::Tessellator::TESS_TYPE_GEOMETRY); |
---|
[4611] | 467 | tscx->setBoundaryOnly(false); |
---|
[5940] | 468 | tscx->setWindingType( osgUtil::Tessellator::TESS_WINDING_ODD); |
---|
[5928] | 469 | tscx->retessellatePolygons(*(gtess.get())); |
---|
[4611] | 470 | |
---|
| 471 | |
---|
| 472 | |
---|
| 473 | unsigned int ipr; |
---|
| 474 | for (ipr=0; ipr<gtess->getNumPrimitiveSets(); ipr++) |
---|
| 475 | { |
---|
| 476 | unsigned int ic; |
---|
| 477 | osg::PrimitiveSet* prset=gtess->getPrimitiveSet(ipr); |
---|
| 478 | |
---|
| 479 | |
---|
| 480 | unsigned int pidx,pidx1,pidx2; |
---|
| 481 | switch (prset->getMode()) { |
---|
| 482 | case osg::PrimitiveSet::TRIANGLES: |
---|
| 483 | for (ic=0; ic<prset->getNumIndices()-2; ic+=3) |
---|
| 484 | { |
---|
| 485 | if (prset->index(ic)>=npts) |
---|
| 486 | { |
---|
| 487 | |
---|
| 488 | points->push_back((*constraintverts)[prset->index(ic)]); |
---|
[5810] | 489 | pidx=points->size()-1; |
---|
[4611] | 490 | } |
---|
| 491 | else |
---|
| 492 | { |
---|
| 493 | pidx=vindexlist[prset->index(ic)]; |
---|
| 494 | } |
---|
| 495 | |
---|
| 496 | if (prset->index(ic+1)>=npts) |
---|
| 497 | { |
---|
| 498 | |
---|
| 499 | points->push_back((*constraintverts)[prset->index(ic+1)]); |
---|
[5810] | 500 | pidx1=points->size()-1; |
---|
[4611] | 501 | } |
---|
| 502 | else |
---|
| 503 | { |
---|
| 504 | pidx1=vindexlist[prset->index(ic+1)]; |
---|
| 505 | } |
---|
| 506 | |
---|
| 507 | if (prset->index(ic+2)>=npts) |
---|
| 508 | { |
---|
| 509 | |
---|
| 510 | points->push_back((*constraintverts)[prset->index(ic+2)]); |
---|
[5810] | 511 | pidx2=points->size()-1; |
---|
[4611] | 512 | } |
---|
| 513 | else |
---|
| 514 | { |
---|
| 515 | pidx2=vindexlist[prset->index(ic+2)]; |
---|
| 516 | } |
---|
| 517 | triangles.push_back(Triangle(pidx, pidx1, pidx2, points)); |
---|
| 518 | |
---|
| 519 | } |
---|
| 520 | break; |
---|
| 521 | case osg::PrimitiveSet::TRIANGLE_STRIP: |
---|
| 522 | |
---|
| 523 | for (ic=0; ic<prset->getNumIndices()-2; ic++) |
---|
| 524 | { |
---|
| 525 | if (prset->index(ic)>=npts) |
---|
| 526 | { |
---|
| 527 | |
---|
| 528 | points->push_back((*constraintverts)[prset->index(ic)]); |
---|
[5810] | 529 | pidx=points->size()-1; |
---|
[4611] | 530 | } else { |
---|
| 531 | pidx=vindexlist[prset->index(ic)]; |
---|
| 532 | } |
---|
| 533 | if (prset->index(ic+1)>=npts) |
---|
| 534 | { |
---|
| 535 | |
---|
| 536 | points->push_back((*constraintverts)[prset->index(ic+1)]); |
---|
[5810] | 537 | pidx1=points->size()-1; |
---|
[4611] | 538 | } |
---|
| 539 | else |
---|
| 540 | { |
---|
| 541 | pidx1=vindexlist[prset->index(ic+1)]; |
---|
| 542 | } |
---|
| 543 | |
---|
| 544 | if (prset->index(ic+2)>=npts) |
---|
| 545 | { |
---|
| 546 | |
---|
| 547 | points->push_back((*constraintverts)[prset->index(ic+2)]); |
---|
[5810] | 548 | pidx2=points->size()-1; |
---|
[4611] | 549 | } |
---|
| 550 | else |
---|
| 551 | { |
---|
| 552 | pidx2=vindexlist[prset->index(ic+2)]; |
---|
| 553 | } |
---|
| 554 | |
---|
| 555 | if (ic%2==0) |
---|
| 556 | { |
---|
| 557 | triangles.push_back(Triangle(pidx, pidx1, pidx2, points)); |
---|
| 558 | } |
---|
| 559 | else |
---|
| 560 | { |
---|
| 561 | triangles.push_back(Triangle(pidx1, pidx, pidx2, points)); |
---|
| 562 | } |
---|
| 563 | |
---|
| 564 | } |
---|
| 565 | break; |
---|
| 566 | |
---|
| 567 | case osg::PrimitiveSet::TRIANGLE_FAN: |
---|
| 568 | { |
---|
| 569 | osg::Vec3 ptest=(*constraintverts)[prset->index(0)]; |
---|
| 570 | if (prset->index(0)>=npts) |
---|
| 571 | { |
---|
| 572 | |
---|
| 573 | points->push_back((*constraintverts)[prset->index(0)]); |
---|
[5810] | 574 | pidx=points->size()-1; |
---|
[4611] | 575 | } |
---|
| 576 | else |
---|
| 577 | { |
---|
| 578 | pidx=vindexlist[prset->index(0)]; |
---|
| 579 | } |
---|
| 580 | |
---|
| 581 | for (ic=1; ic<prset->getNumIndices()-1; ic++) |
---|
| 582 | { |
---|
| 583 | if (prset->index(ic)>=npts) |
---|
| 584 | { |
---|
| 585 | |
---|
| 586 | points->push_back((*constraintverts)[prset->index(ic)]); |
---|
[5810] | 587 | pidx1=points->size()-1; |
---|
[4611] | 588 | } |
---|
| 589 | else |
---|
| 590 | { |
---|
| 591 | pidx1=vindexlist[prset->index(ic)]; |
---|
| 592 | } |
---|
| 593 | |
---|
| 594 | if (prset->index(ic+1)>=npts) |
---|
| 595 | { |
---|
| 596 | points->push_back((*constraintverts)[prset->index(ic+1)]); |
---|
[5810] | 597 | pidx2=points->size()-1; |
---|
[4611] | 598 | } |
---|
| 599 | else |
---|
| 600 | { |
---|
| 601 | pidx2=vindexlist[prset->index(ic+1)]; |
---|
| 602 | } |
---|
| 603 | triangles.push_back(Triangle(pidx, pidx1, pidx2, points)); |
---|
| 604 | } |
---|
| 605 | } |
---|
| 606 | break; |
---|
| 607 | default: |
---|
| 608 | osg::notify(osg::WARN)<< "WARNING set " << ipr << " nprims " << prset->getNumPrimitives() << |
---|
| 609 | " type " << prset->getMode() << " Type not triangle, tfan or strip"<< std::endl; |
---|
| 610 | break; |
---|
| 611 | } |
---|
| 612 | } |
---|
| 613 | return triangles; |
---|
| 614 | } |
---|
| 615 | |
---|
| 616 | void DelaunayConstraint::removeVerticesInside(const DelaunayConstraint *dco) |
---|
| 617 | { |
---|
| 618 | |
---|
| 619 | |
---|
| 620 | |
---|
| 621 | int nrem=0; |
---|
| 622 | osg::Vec3Array *vertices= dynamic_cast< osg::Vec3Array*>(getVertexArray()); |
---|
[4728] | 623 | if (vertices) |
---|
[4611] | 624 | { |
---|
[4728] | 625 | for (osg::Vec3Array::iterator vitr=vertices->begin(); vitr!=vertices->end(); ) |
---|
[4611] | 626 | { |
---|
[4728] | 627 | if (dco->contains(*vitr)) |
---|
[4611] | 628 | { |
---|
[4728] | 629 | unsigned int idx=vitr-vertices->begin(); |
---|
| 630 | |
---|
| 631 | for (unsigned int ipr=0; ipr<getNumPrimitiveSets(); ipr++) |
---|
| 632 | { |
---|
| 633 | osg::PrimitiveSet* prset=getPrimitiveSet(ipr); |
---|
| 634 | osg::DrawElementsUShort *dsup=dynamic_cast<osg::DrawElementsUShort *>(prset); |
---|
| 635 | if (dsup) { |
---|
| 636 | for (osg::DrawElementsUShort::iterator usitr=dsup->begin(); usitr!=dsup->end(); ) |
---|
[4611] | 637 | { |
---|
[4728] | 638 | if ((*usitr)==idx) |
---|
| 639 | { |
---|
| 640 | usitr=dsup->erase(usitr); |
---|
| 641 | } |
---|
| 642 | else |
---|
| 643 | { |
---|
| 644 | if ((*usitr)>idx) (*usitr)--; |
---|
| 645 | usitr++; |
---|
| 646 | } |
---|
[4611] | 647 | } |
---|
| 648 | } |
---|
[4728] | 649 | else |
---|
| 650 | { |
---|
| 651 | osg::notify(osg::WARN) << "Invalid prset " <<ipr<< " tp " << prset->getType() << " types PrimitiveType,DrawArraysPrimitiveType=1 etc" << std::endl; |
---|
| 652 | } |
---|
[4611] | 653 | } |
---|
[4728] | 654 | vitr=vertices->erase(vitr); |
---|
| 655 | nrem++; |
---|
| 656 | |
---|
[4611] | 657 | } |
---|
[4728] | 658 | else |
---|
| 659 | { |
---|
| 660 | vitr++; |
---|
| 661 | } |
---|
[4611] | 662 | } |
---|
| 663 | } |
---|
| 664 | } |
---|
| 665 | |
---|
| 666 | void DelaunayConstraint::merge(DelaunayConstraint *dco) |
---|
| 667 | { |
---|
| 668 | unsigned int ipr; |
---|
[5949] | 669 | if (dco) { |
---|
| 670 | osg::Vec3Array* vmerge=dynamic_cast<osg::Vec3Array*>(getVertexArray()); |
---|
| 671 | if (!vmerge) vmerge=new osg::Vec3Array; |
---|
| 672 | setVertexArray(vmerge); |
---|
| 673 | for ( ipr=0; ipr<dco->getNumPrimitiveSets(); ipr++) |
---|
[4611] | 674 | { |
---|
[5949] | 675 | osg::PrimitiveSet* prset=dco->getPrimitiveSet(ipr); |
---|
| 676 | osg::DrawArrays *drarr=dynamic_cast<osg::DrawArrays *> (prset); |
---|
| 677 | if (drarr) |
---|
| 678 | { |
---|
| 679 | |
---|
| 680 | unsigned int noff=vmerge->size(); |
---|
| 681 | unsigned int n1=drarr->getFirst(); |
---|
| 682 | unsigned int numv=drarr->getCount(); |
---|
| 683 | addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::LINE_LOOP,n1+noff,numv)); |
---|
| 684 | } |
---|
[4611] | 685 | } |
---|
[5949] | 686 | osg::Vec3Array* varr=dynamic_cast<osg::Vec3Array*>(dco->getVertexArray()); |
---|
| 687 | if (varr) vmerge->insert(vmerge->end(),varr->begin(),varr->end()); |
---|
[4611] | 688 | } |
---|
| 689 | } |
---|
| 690 | |
---|
[4984] | 691 | void DelaunayTriangulator::_uniqueifyPoints() |
---|
| 692 | { |
---|
| 693 | std::sort( points_->begin(), points_->end() ); |
---|
[4611] | 694 | |
---|
[4984] | 695 | osg::ref_ptr<osg::Vec3Array> temppts = new osg::Vec3Array; |
---|
| 696 | |
---|
| 697 | |
---|
| 698 | |
---|
| 699 | |
---|
| 700 | |
---|
| 701 | osg::Vec3Array::iterator p = points_->begin(); |
---|
| 702 | osg::Vec3 v = *p; |
---|
[5075] | 703 | |
---|
| 704 | temppts->push_back( (v = *p)); |
---|
[4984] | 705 | for( ; p != points_->end(); p++ ) |
---|
| 706 | { |
---|
| 707 | if( v[0] == (*p)[0] && v[1] == (*p)[1] ) |
---|
| 708 | continue; |
---|
| 709 | |
---|
| 710 | temppts->push_back( (v = *p)); |
---|
| 711 | } |
---|
| 712 | |
---|
| 713 | points_->clear(); |
---|
| 714 | std::insert_iterator< osg::Vec3Array > ci(*(points_.get()),points_->begin()); |
---|
| 715 | std::copy( temppts->begin(), temppts->end(), ci ); |
---|
| 716 | } |
---|
| 717 | |
---|
[5985] | 718 | osgUtil::DelaunayConstraint *getconvexhull(osg::Vec3Array *points) |
---|
| 719 | { |
---|
| 720 | osg::ref_ptr<osgUtil::DelaunayConstraint> dcconvexhull=new osgUtil::DelaunayConstraint; |
---|
| 721 | |
---|
| 722 | |
---|
| 723 | osg::Vec3Array *verts=new osg::Vec3Array; |
---|
| 724 | verts->push_back(*(points->begin()) ); |
---|
| 725 | verts->push_back(*(points->begin()+1) ); |
---|
| 726 | for (osg::Vec3Array::iterator vit=(points->begin()+2); vit!=points->end(); vit++) { |
---|
| 727 | |
---|
| 728 | bool ok=1; |
---|
| 729 | while (ok && verts->size()>1) { |
---|
| 730 | osg::Vec3 lastseg=*(verts->end()-2)-*(verts->end()-1); |
---|
| 731 | osg::Vec3 thisseg=(*vit)-*(verts->end()-1); |
---|
| 732 | float cosang=(lastseg^thisseg).z(); |
---|
| 733 | if (cosang <0.0) { |
---|
| 734 | verts->pop_back(); |
---|
| 735 | } else { ok=0;} |
---|
| 736 | } |
---|
| 737 | verts->push_back(*vit ); |
---|
| 738 | |
---|
| 739 | } |
---|
| 740 | for (osg::Vec3Array::reverse_iterator rvit=points->rbegin()+1; rvit!=points->rend(); rvit++) { |
---|
| 741 | |
---|
| 742 | bool ok=1; |
---|
| 743 | while (ok && verts->size()>1) { |
---|
| 744 | osg::Vec3 lastseg=*(verts->end()-2)-*(verts->end()-1); |
---|
| 745 | osg::Vec3 thisseg=(*rvit)-*(verts->end()-1); |
---|
| 746 | float cosang=(lastseg^thisseg).z(); |
---|
| 747 | if (cosang <0.0) { |
---|
| 748 | verts->pop_back(); |
---|
| 749 | } else { ok=0;} |
---|
| 750 | } |
---|
| 751 | if ((*rvit)!=*(verts->begin())) verts->push_back(*rvit ); |
---|
| 752 | |
---|
| 753 | } |
---|
| 754 | dcconvexhull->setVertexArray(verts); |
---|
| 755 | dcconvexhull->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::LINE_LOOP,0,verts->size()) ); |
---|
| 756 | return dcconvexhull.release(); |
---|
| 757 | } |
---|
[4984] | 758 | |
---|
[1889] | 759 | bool DelaunayTriangulator::triangulate() |
---|
| 760 | { |
---|
| 761 | |
---|
[4611] | 762 | if (!points_.valid()) |
---|
| 763 | { |
---|
[1889] | 764 | osg::notify(osg::WARN) << "Warning: DelaunayTriangulator::triangulate(): invalid sample point array" << std::endl; |
---|
| 765 | return false; |
---|
| 766 | } |
---|
| 767 | |
---|
| 768 | osg::Vec3Array *points = points_.get(); |
---|
| 769 | |
---|
[4611] | 770 | if (points->size() < 1) |
---|
| 771 | { |
---|
[1889] | 772 | osg::notify(osg::WARN) << "Warning: DelaunayTriangulator::triangulate(): too few sample points" << std::endl; |
---|
| 773 | return false; |
---|
| 774 | } |
---|
| 775 | |
---|
[4984] | 776 | |
---|
| 777 | _uniqueifyPoints(); |
---|
| 778 | |
---|
| 779 | |
---|
[1889] | 780 | |
---|
| 781 | Triangle_list triangles; |
---|
| 782 | Triangle_list discarded_tris; |
---|
| 783 | |
---|
[4611] | 784 | |
---|
| 785 | linelist::iterator linitr; |
---|
| 786 | for (linitr=constraint_lines.begin();linitr!=constraint_lines.end();linitr++) |
---|
| 787 | { |
---|
[4728] | 788 | DelaunayConstraint* dc=(*linitr).get(); |
---|
| 789 | const osg::Vec3Array* vercon= dynamic_cast<const osg::Vec3Array*>(dc->getVertexArray()); |
---|
| 790 | if (vercon) |
---|
[4611] | 791 | { |
---|
[4728] | 792 | int nadded=0; |
---|
| 793 | for (unsigned int icon=0;icon<vercon->size();icon++) |
---|
[4611] | 794 | { |
---|
[4728] | 795 | osg::Vec3 p1=(*vercon)[icon]; |
---|
| 796 | int idx=getindex(p1,points_.get()); |
---|
| 797 | if (idx<0) |
---|
| 798 | { |
---|
| 799 | points_->push_back(p1); |
---|
| 800 | nadded++; |
---|
| 801 | } |
---|
| 802 | else |
---|
| 803 | { |
---|
| 804 | osg::notify(osg::WARN) << "DelaunayTriangulator: ignore a duplicate point at "<< p1.x()<< " " << p1.y() << std::endl;; |
---|
| 805 | } |
---|
[4611] | 806 | } |
---|
| 807 | } |
---|
| 808 | |
---|
| 809 | } |
---|
| 810 | |
---|
| 811 | |
---|
[1889] | 812 | |
---|
| 813 | osg::notify(osg::INFO) << "DelaunayTriangulator: pre-sorting sample points\n"; |
---|
| 814 | std::sort(points->begin(), points->end(), Sample_point_compare); |
---|
[5985] | 815 | |
---|
| 816 | osg::ref_ptr<osgUtil::DelaunayConstraint> dcconvexhull=getconvexhull(points); |
---|
| 817 | addInputConstraint(dcconvexhull.get()); |
---|
[1889] | 818 | |
---|
| 819 | |
---|
| 820 | GLuint last_valid_index = points->size() - 1; |
---|
| 821 | |
---|
| 822 | |
---|
| 823 | float minx = (*points)[0].x(); |
---|
[1895] | 824 | float maxx = (*points)[last_valid_index].x(); |
---|
[1889] | 825 | |
---|
[5985] | 826 | |
---|
[1889] | 827 | float miny = (*points)[0].y(); |
---|
| 828 | float maxy = miny; |
---|
[1895] | 829 | |
---|
[1889] | 830 | osg::notify(osg::INFO) << "DelaunayTriangulator: finding minimum and maximum Y values\n"; |
---|
| 831 | osg::Vec3Array::const_iterator mmi; |
---|
[4611] | 832 | for (mmi=points->begin(); mmi!=points->end(); ++mmi) |
---|
| 833 | { |
---|
[1889] | 834 | if (mmi->y() < miny) miny = mmi->y(); |
---|
| 835 | if (mmi->y() > maxy) maxy = mmi->y(); |
---|
| 836 | } |
---|
| 837 | |
---|
| 838 | |
---|
[4611] | 839 | |
---|
| 840 | |
---|
| 841 | |
---|
[5949] | 842 | |
---|
| 843 | |
---|
| 844 | |
---|
[5985] | 845 | points_->push_back(osg::Vec3(minx - .10*(maxx - minx), miny - .10*(maxy - miny), 0)); |
---|
| 846 | points_->push_back(osg::Vec3(maxx + .10*(maxx - minx), miny - .10*(maxy - miny), 0)); |
---|
| 847 | points_->push_back(osg::Vec3(maxx + .10*(maxx - minx), maxy + .10*(maxy - miny), 0)); |
---|
| 848 | points_->push_back(osg::Vec3(minx - .10*(maxx - minx), maxy + .10*(maxy - miny), 0)); |
---|
| 849 | |
---|
| 850 | |
---|
[1889] | 851 | triangles.push_back(Triangle(last_valid_index+1, last_valid_index+2, last_valid_index+3, points)); |
---|
[5985] | 852 | triangles.push_back(Triangle(last_valid_index+4, last_valid_index+1, last_valid_index+3, points)); |
---|
[1889] | 853 | |
---|
| 854 | |
---|
| 855 | |
---|
| 856 | GLuint pidx = 0; |
---|
| 857 | osg::Vec3Array::const_iterator i; |
---|
| 858 | |
---|
| 859 | osg::notify(osg::INFO) << "DelaunayTriangulator: triangulating vertex grid (" << (points->size()-3) <<" points)\n"; |
---|
| 860 | |
---|
[4611] | 861 | for (i=points->begin(); i!=points->end(); ++i, ++pidx) |
---|
| 862 | { |
---|
[1889] | 863 | |
---|
| 864 | |
---|
| 865 | if (pidx > last_valid_index) break; |
---|
| 866 | |
---|
| 867 | Edge_set edges; |
---|
| 868 | |
---|
| 869 | |
---|
| 870 | Triangle_list::iterator j, next_j; |
---|
[4611] | 871 | for (j=triangles.begin(); j!=triangles.end(); j = next_j) |
---|
| 872 | { |
---|
[1889] | 873 | |
---|
| 874 | next_j = j; |
---|
| 875 | ++next_j; |
---|
| 876 | |
---|
[5985] | 877 | |
---|
[1889] | 878 | osg::Vec3 cc = j->get_circumcircle(); |
---|
| 879 | |
---|
| 880 | |
---|
| 881 | |
---|
| 882 | float xdist = i->x() - cc.x(); |
---|
[4611] | 883 | |
---|
| 884 | |
---|
| 885 | if ((xdist ) > cc.z() ) |
---|
| 886 | { |
---|
[5985] | 887 | discarded_tris.push_back(*j); |
---|
| 888 | |
---|
[1889] | 889 | triangles.erase(j); |
---|
[4611] | 890 | } |
---|
| 891 | else |
---|
| 892 | { |
---|
[1889] | 893 | |
---|
| 894 | |
---|
| 895 | |
---|
| 896 | if (point_in_circle(*i, cc)) |
---|
| 897 | { |
---|
| 898 | for (int ei=0; ei<3; ++ei) |
---|
| 899 | { |
---|
| 900 | std::pair<Edge_set::iterator, bool> result = edges.insert(j->get_edge(ei)); |
---|
| 901 | if (!result.second) |
---|
| 902 | { |
---|
| 903 | |
---|
| 904 | |
---|
| 905 | |
---|
| 906 | Edge& edge = const_cast<Edge&>(*(result.first)); |
---|
[4611] | 907 | |
---|
| 908 | |
---|
| 909 | edge.set_duplicate(!edge.get_duplicate()); |
---|
[1889] | 910 | } |
---|
| 911 | } |
---|
| 912 | triangles.erase(j); |
---|
| 913 | } |
---|
| 914 | } |
---|
| 915 | } |
---|
| 916 | |
---|
| 917 | |
---|
| 918 | Edge_set::iterator ci; |
---|
[4611] | 919 | for (ci=edges.begin(); ci!=edges.end(); ++ci) |
---|
| 920 | { |
---|
| 921 | if (!ci->get_duplicate()) |
---|
| 922 | { |
---|
[1889] | 923 | triangles.push_back(Triangle(pidx, ci->ib(), ci->ie(), points)); |
---|
| 924 | } |
---|
| 925 | } |
---|
| 926 | } |
---|
[5810] | 927 | |
---|
| 928 | |
---|
[1889] | 929 | |
---|
| 930 | osg::notify(osg::INFO) << "DelaunayTriangulator: finalizing and cleaning up structures\n"; |
---|
| 931 | |
---|
[5810] | 932 | |
---|
[1889] | 933 | triangles.insert(triangles.begin(), discarded_tris.begin(), discarded_tris.end()); |
---|
| 934 | |
---|
[4611] | 935 | |
---|
| 936 | |
---|
| 937 | |
---|
| 938 | |
---|
[5940] | 939 | |
---|
[4611] | 940 | for (linelist::iterator dcitr=constraint_lines.begin();dcitr!=constraint_lines.end();dcitr++) |
---|
| 941 | { |
---|
| 942 | |
---|
[4728] | 943 | const osg::Vec3Array* vercon = dynamic_cast<const osg::Vec3Array*>((*dcitr)->getVertexArray()); |
---|
| 944 | if (vercon) |
---|
[4611] | 945 | { |
---|
[4728] | 946 | for (unsigned int ipr=0; ipr<(*dcitr)->getNumPrimitiveSets(); ipr++) |
---|
[4611] | 947 | { |
---|
[4728] | 948 | const osg::PrimitiveSet* prset=(*dcitr)->getPrimitiveSet(ipr); |
---|
| 949 | if (prset->getMode()==osg::PrimitiveSet::LINE_LOOP || |
---|
| 950 | prset->getMode()==osg::PrimitiveSet::LINE_STRIP) |
---|
[4611] | 951 | { |
---|
[4728] | 952 | |
---|
| 953 | |
---|
| 954 | unsigned int ip1=getindex((*vercon)[prset->index (prset->getNumIndices()-1)],points_.get()); |
---|
| 955 | for (unsigned int i=0; i<prset->getNumIndices(); i++) |
---|
[4611] | 956 | { |
---|
[4728] | 957 | unsigned int ip2=getindex((*vercon)[prset->index(i)],points_.get()); |
---|
| 958 | if (i>0 || prset->getMode()==osg::PrimitiveSet::LINE_LOOP) |
---|
[4611] | 959 | { |
---|
[4728] | 960 | |
---|
| 961 | |
---|
| 962 | |
---|
| 963 | bool edgused=false; |
---|
| 964 | Triangle_list::iterator titr; |
---|
| 965 | const osg::Vec3 curp=(*vercon)[prset->index(i)]; |
---|
| 966 | int it=0; |
---|
| 967 | for (titr=triangles.begin(); titr!=triangles.end() && !edgused; ++titr) |
---|
[4611] | 968 | { |
---|
[4728] | 969 | |
---|
| 970 | if (titr->isedge(ip1,ip2)) edgused=true; |
---|
| 971 | if (titr->isedge(ip2,ip1)) edgused=true; |
---|
| 972 | |
---|
| 973 | |
---|
| 974 | it++; |
---|
| 975 | } |
---|
| 976 | if (!edgused) |
---|
| 977 | { |
---|
| 978 | |
---|
| 979 | |
---|
| 980 | osg::Vec2 p1((*points_)[ip1].x(),(*points_)[ip1].y()); |
---|
| 981 | osg::Vec2 p2((*points_)[ip2].x(),(*points_)[ip2].y()); |
---|
| 982 | int ntr=0; |
---|
| 983 | std::vector<const Triangle *> trisToDelete; |
---|
| 984 | |
---|
| 985 | |
---|
| 986 | |
---|
| 987 | |
---|
[5940] | 988 | |
---|
[4728] | 989 | for (titr=triangles.begin(); titr!=triangles.end(); ) |
---|
[4611] | 990 | { |
---|
[4728] | 991 | int icut=titr->lineBisects(points_.get(),ip1,p2); |
---|
| 992 | |
---|
| 993 | |
---|
| 994 | if (icut>0) |
---|
[4611] | 995 | { |
---|
[4728] | 996 | |
---|
| 997 | std::vector<unsigned int> edgeRight, edgeLeft; |
---|
| 998 | edgeRight.push_back(ip1); |
---|
| 999 | edgeLeft.push_back(ip1); |
---|
| 1000 | |
---|
| 1001 | trisToDelete.push_back(&(*titr)); |
---|
| 1002 | |
---|
| 1003 | unsigned int e1, e2; |
---|
| 1004 | if (icut==1) |
---|
[4611] | 1005 | { |
---|
[4728] | 1006 | |
---|
| 1007 | e1=titr->b(); e2=titr->c(); |
---|
| 1008 | } |
---|
| 1009 | else if (icut==2) |
---|
| 1010 | { |
---|
| 1011 | e1=titr->c(); e2=titr->a(); |
---|
| 1012 | } |
---|
| 1013 | else if (icut==3) |
---|
| 1014 | { |
---|
| 1015 | e1=titr->a(); e2=titr->b(); |
---|
| 1016 | } |
---|
| 1017 | edgeRight.push_back(e2); |
---|
| 1018 | edgeLeft.push_back(e1); |
---|
| 1019 | |
---|
| 1020 | const Triangle *tradj=getTriangleWithEdge(e2,e1, &triangles); |
---|
| 1021 | if (tradj) |
---|
| 1022 | { |
---|
| 1023 | while (tradj && !tradj->usesVertex(ip2) && trisToDelete.size()<999) |
---|
| 1024 | { |
---|
| 1025 | trisToDelete.push_back(tradj); |
---|
| 1026 | icut=tradj->whichEdge(points_.get(),p1,p2,e1,e2); |
---|
| 1027 | |
---|
| 1028 | |
---|
| 1029 | |
---|
| 1030 | if (icut==1) {e1=tradj->b(); e2=tradj->c();} |
---|
| 1031 | else if (icut==2) {e1=tradj->c(); e2=tradj->a();} |
---|
| 1032 | else if (icut==3) {e1=tradj->a(); e2=tradj->b();} |
---|
| 1033 | if (edgeLeft.back()!=e1 && edgeRight.back()==e2 && e1!=ip2) { |
---|
| 1034 | edgeLeft.push_back(e1); |
---|
| 1035 | } else if(edgeRight.back()!=e2 && edgeLeft.back()==e1 && e2!=ip2) { |
---|
| 1036 | edgeRight.push_back(e2); |
---|
| 1037 | } else { |
---|
| 1038 | if (!tradj->usesVertex(ip2)) osg::notify(osg::WARN) << "tradj error " << tradj->a()<< " , " << tradj->b()<< " , " << tradj->c()<< std::endl; |
---|
| 1039 | } |
---|
| 1040 | tradj=getTriangleWithEdge(e2,e1, &triangles); |
---|
[4611] | 1041 | } |
---|
[4728] | 1042 | if (trisToDelete.size()>=900) { |
---|
| 1043 | osg::notify(osg::WARN) << " found " << trisToDelete.size() << " adjacent tris " <<std::endl; |
---|
| 1044 | } |
---|
[4611] | 1045 | } |
---|
[4728] | 1046 | |
---|
| 1047 | |
---|
| 1048 | edgeLeft.push_back(ip2); |
---|
| 1049 | edgeRight.push_back(ip2); |
---|
| 1050 | if (tradj) trisToDelete.push_back(tradj); |
---|
| 1051 | |
---|
| 1052 | Triangle_list constrainedtris=fillHole(points_.get(),edgeLeft); |
---|
| 1053 | triangles.insert(triangles.begin(), constrainedtris.begin(), constrainedtris.end()); |
---|
| 1054 | constrainedtris=fillHole(points_.get(),edgeRight); |
---|
| 1055 | triangles.insert(triangles.begin(), constrainedtris.begin(), constrainedtris.end()); |
---|
| 1056 | |
---|
[4611] | 1057 | } |
---|
[4728] | 1058 | ++titr; |
---|
| 1059 | ntr++; |
---|
[4611] | 1060 | } |
---|
[4728] | 1061 | |
---|
| 1062 | Triangle_list::iterator tri; |
---|
| 1063 | for (tri=triangles.begin(); tri!=triangles.end(); ) |
---|
[4611] | 1064 | { |
---|
[4728] | 1065 | bool deleted=false; |
---|
[5985] | 1066 | for (std::vector<const Triangle *>::iterator deleteTri=trisToDelete.begin(); |
---|
| 1067 | deleteTri!=trisToDelete.end(); ) |
---|
[4611] | 1068 | { |
---|
[4728] | 1069 | if (&(*tri)==*deleteTri) |
---|
| 1070 | { |
---|
| 1071 | deleted=true; |
---|
| 1072 | tri=triangles.erase(tri); |
---|
[5985] | 1073 | deleteTri=trisToDelete.erase(deleteTri); |
---|
| 1074 | } else {deleteTri++; } |
---|
[4611] | 1075 | } |
---|
[4728] | 1076 | if (!deleted) ++tri; |
---|
[4611] | 1077 | } |
---|
| 1078 | } |
---|
[4728] | 1079 | } |
---|
| 1080 | |
---|
| 1081 | ip1=ip2; |
---|
| 1082 | } |
---|
[4611] | 1083 | } |
---|
| 1084 | } |
---|
| 1085 | } |
---|
| 1086 | } |
---|
| 1087 | |
---|
[5810] | 1088 | |
---|
| 1089 | |
---|
| 1090 | |
---|
| 1091 | Triangle_list::iterator tri; |
---|
[5985] | 1092 | GLuint supertriend = last_valid_index+4; |
---|
[5810] | 1093 | for (tri=triangles.begin(); tri!=triangles.end();) |
---|
| 1094 | { |
---|
| 1095 | if ((tri->a() > last_valid_index && tri->a() <= supertriend) || |
---|
| 1096 | (tri->b() > last_valid_index && tri->b() <= supertriend ) || |
---|
| 1097 | (tri->c() > last_valid_index && tri->c() <= supertriend )) { |
---|
| 1098 | tri=triangles.erase(tri); |
---|
| 1099 | } else { |
---|
[5985] | 1100 | if (tri->a() > last_valid_index) { |
---|
| 1101 | tri->incrementa(-4); |
---|
[5810] | 1102 | } |
---|
[5985] | 1103 | if (tri->b() > last_valid_index) { |
---|
| 1104 | tri->incrementb(-4); |
---|
[5810] | 1105 | } |
---|
[5985] | 1106 | if (tri->c() > last_valid_index) { |
---|
| 1107 | tri->incrementc(-4); |
---|
[5810] | 1108 | } |
---|
[5985] | 1109 | ++tri; |
---|
[5810] | 1110 | } |
---|
| 1111 | } |
---|
| 1112 | |
---|
| 1113 | |
---|
[5985] | 1114 | points->erase(points->begin()+last_valid_index+1,points->begin()+last_valid_index+5); |
---|
[5810] | 1115 | |
---|
| 1116 | |
---|
[4611] | 1117 | |
---|
[1889] | 1118 | |
---|
| 1119 | std::vector<GLuint> pt_indices; |
---|
| 1120 | pt_indices.reserve(triangles.size() * 3); |
---|
| 1121 | |
---|
| 1122 | |
---|
| 1123 | osg::notify(osg::INFO) << "DelaunayTriangulator: building primitive(s)\n"; |
---|
| 1124 | Triangle_list::const_iterator ti; |
---|
[4611] | 1125 | for (ti=triangles.begin(); ti!=triangles.end(); ++ti) |
---|
| 1126 | { |
---|
[1889] | 1127 | |
---|
| 1128 | |
---|
[5949] | 1129 | |
---|
| 1130 | |
---|
| 1131 | |
---|
| 1132 | if ( ti->get_circumcircle().z()>0.0) |
---|
[4611] | 1133 | { |
---|
[1889] | 1134 | |
---|
[4611] | 1135 | if (normals_.valid()) |
---|
| 1136 | { |
---|
[1889] | 1137 | (normals_.get())->push_back(ti->compute_normal(points)); |
---|
| 1138 | } |
---|
| 1139 | |
---|
| 1140 | pt_indices.push_back(ti->a()); |
---|
| 1141 | pt_indices.push_back(ti->b()); |
---|
| 1142 | pt_indices.push_back(ti->c()); |
---|
| 1143 | } |
---|
| 1144 | } |
---|
| 1145 | |
---|
| 1146 | prim_tris_ = new osg::DrawElementsUInt(GL_TRIANGLES, pt_indices.size(), &(pt_indices.front())); |
---|
| 1147 | |
---|
[6135] | 1148 | osg::notify(osg::INFO) << "DelaunayTriangulator: process done, " << prim_tris_->getNumPrimitives() << " triangles remain\n"; |
---|
[1889] | 1149 | |
---|
| 1150 | return true; |
---|
| 1151 | } |
---|
| 1152 | |
---|
[4611] | 1153 | void DelaunayTriangulator::removeInternalTriangles(DelaunayConstraint *dc ) |
---|
| 1154 | { |
---|
[5949] | 1155 | if (dc) { |
---|
| 1156 | |
---|
| 1157 | |
---|
| 1158 | |
---|
| 1159 | |
---|
| 1160 | int ndel=0; |
---|
| 1161 | osg::Vec3Array::iterator normitr; |
---|
| 1162 | if( normals_.valid() ) |
---|
| 1163 | normitr = normals_->begin(); |
---|
| 1164 | |
---|
| 1165 | |
---|
| 1166 | for (osg::DrawElementsUInt::iterator triit=prim_tris_->begin(); triit!=prim_tris_->end(); ) |
---|
[4611] | 1167 | { |
---|
[5949] | 1168 | |
---|
| 1169 | Triangle tritest((*triit), *(triit+1), *(triit+2), points_.get()); |
---|
| 1170 | if ( dc->contains(tritest.compute_centroid( points_.get()) ) ) |
---|
[4611] | 1171 | { |
---|
[5949] | 1172 | |
---|
| 1173 | |
---|
| 1174 | dc->addtriangle((*triit), *(triit+1), *(triit+2)); |
---|
| 1175 | triit=prim_tris_->erase(triit); |
---|
| 1176 | triit=prim_tris_->erase(triit); |
---|
| 1177 | triit=prim_tris_->erase(triit); |
---|
| 1178 | if (normals_.valid()) |
---|
| 1179 | { |
---|
| 1180 | |
---|
| 1181 | normitr=normals_->erase(normitr); |
---|
| 1182 | } |
---|
| 1183 | ndel++; |
---|
[4611] | 1184 | } |
---|
[5949] | 1185 | else |
---|
[4611] | 1186 | { |
---|
[5949] | 1187 | if (normals_.valid()) |
---|
| 1188 | { |
---|
| 1189 | normitr++; |
---|
| 1190 | } |
---|
| 1191 | |
---|
| 1192 | triit+=3; |
---|
[4611] | 1193 | } |
---|
| 1194 | } |
---|
[1889] | 1195 | |
---|
[5949] | 1196 | osg::notify(osg::INFO) << "end of test dc, deleted " << ndel << std::endl; |
---|
| 1197 | } |
---|
[1889] | 1198 | } |
---|
[4611] | 1199 | |
---|
| 1200 | |
---|
[6446] | 1201 | float DelaunayConstraint::windingNumber(const osg::Vec3 &testpoint) const |
---|
[4611] | 1202 | { |
---|
| 1203 | |
---|
| 1204 | float theta=0; |
---|
| 1205 | const osg::Vec3Array *vertices= dynamic_cast<const osg::Vec3Array*>(getVertexArray()); |
---|
[4728] | 1206 | if (vertices) |
---|
[4611] | 1207 | { |
---|
[4728] | 1208 | for (unsigned int ipr=0; ipr<getNumPrimitiveSets(); ipr++) |
---|
[4611] | 1209 | { |
---|
[4728] | 1210 | const osg::PrimitiveSet* prset=getPrimitiveSet(ipr); |
---|
| 1211 | if (prset->getMode()==osg::PrimitiveSet::LINE_LOOP) |
---|
[4611] | 1212 | { |
---|
[4728] | 1213 | |
---|
| 1214 | |
---|
| 1215 | const osg::Vec3 prev=(*vertices)[prset->index (prset->getNumIndices()-1)]; |
---|
| 1216 | osg::Vec3 pi(prev.x()-testpoint.x(),prev.y()-testpoint.y(),0); |
---|
| 1217 | pi.normalize(); |
---|
| 1218 | for (unsigned int i=0; i<prset->getNumIndices(); i++) |
---|
[4611] | 1219 | { |
---|
[4728] | 1220 | const osg::Vec3 curp=(*vertices)[prset->index (i)]; |
---|
| 1221 | osg::Vec3 edge(curp.x()-testpoint.x(),curp.y()-testpoint.y(),0); |
---|
| 1222 | edge.normalize(); |
---|
| 1223 | double cth=edge*pi; |
---|
| 1224 | if (cth<=-0.99999 ) |
---|
[4611] | 1225 | { |
---|
[4728] | 1226 | |
---|
| 1227 | return 0; |
---|
[4611] | 1228 | } |
---|
[4728] | 1229 | else |
---|
| 1230 | { |
---|
| 1231 | if (cth<0.99999) |
---|
| 1232 | { |
---|
| 1233 | float dang=(cth<1 && cth>-1)?acos(edge*pi):0; |
---|
| 1234 | float zsign=edge.x()*pi.y()-pi.x()*edge.y(); |
---|
| 1235 | if (zsign>0) theta+=dang; |
---|
| 1236 | else if (zsign<0) theta-=dang; |
---|
| 1237 | } |
---|
| 1238 | } |
---|
| 1239 | pi=edge; |
---|
[4611] | 1240 | } |
---|
| 1241 | } |
---|
| 1242 | } |
---|
| 1243 | } |
---|
[4728] | 1244 | |
---|
[4611] | 1245 | return theta/osg::PI/2.0; |
---|
| 1246 | } |
---|
| 1247 | osg::DrawElementsUInt *DelaunayConstraint::makeDrawable() |
---|
| 1248 | { |
---|
| 1249 | |
---|
| 1250 | std::vector<GLuint> pt_indices; |
---|
| 1251 | pt_indices.reserve(_interiorTris.size() * 3); |
---|
| 1252 | trilist::const_iterator ti; |
---|
| 1253 | for (ti=_interiorTris.begin(); ti!=_interiorTris.end(); ++ti) |
---|
| 1254 | { |
---|
| 1255 | |
---|
| 1256 | |
---|
| 1257 | |
---|
| 1258 | |
---|
| 1259 | |
---|
| 1260 | pt_indices.push_back((*ti)[0]); |
---|
| 1261 | pt_indices.push_back((*ti)[1]); |
---|
| 1262 | pt_indices.push_back((*ti)[2]); |
---|
| 1263 | } |
---|
| 1264 | prim_tris_ = new osg::DrawElementsUInt(GL_TRIANGLES, pt_indices.size(), &(pt_indices.front())); |
---|
| 1265 | |
---|
| 1266 | return prim_tris_.get(); |
---|
| 1267 | } |
---|
[6446] | 1268 | bool DelaunayConstraint::contains(const osg::Vec3 &testpoint) const |
---|
[4611] | 1269 | { |
---|
| 1270 | |
---|
| 1271 | float theta=windingNumber(testpoint); |
---|
| 1272 | return fabs(theta)>0.9; |
---|
| 1273 | } |
---|
[6446] | 1274 | bool DelaunayConstraint::outside(const osg::Vec3 &testpoint) const |
---|
[4611] | 1275 | { |
---|
| 1276 | |
---|
| 1277 | float theta=windingNumber(testpoint); |
---|
| 1278 | return fabs(theta)<.05; |
---|
| 1279 | } |
---|
| 1280 | |
---|
| 1281 | |
---|
[6446] | 1282 | void DelaunayConstraint::addtriangle(int i1, int i2, int i3) |
---|
[4611] | 1283 | { |
---|
| 1284 | |
---|
| 1285 | |
---|
| 1286 | |
---|
| 1287 | int *ip=new int[3]; |
---|
| 1288 | ip[0]=i1; |
---|
| 1289 | ip[1]=i2; |
---|
| 1290 | ip[2]=i3; |
---|
| 1291 | _interiorTris.push_back(ip); |
---|
| 1292 | } |
---|
| 1293 | osg::Vec3Array* DelaunayConstraint::getPoints(const osg::Vec3Array *points) |
---|
| 1294 | { |
---|
| 1295 | |
---|
| 1296 | osg::ref_ptr<osg::Vec3Array> points_=new osg::Vec3Array; |
---|
| 1297 | trilist::iterator ti; |
---|
| 1298 | for (ti=_interiorTris.begin(); ti!=_interiorTris.end(); ++ti) { |
---|
| 1299 | int idx=0; |
---|
| 1300 | int ip[3]={-1,-1,-1}; |
---|
| 1301 | |
---|
| 1302 | for (osg::Vec3Array::iterator ivert=points_->begin(); ivert!=points_->end(); ivert++) |
---|
| 1303 | { |
---|
| 1304 | if (ip[0]<0 && *ivert==(*points)[(*ti)[0]]) |
---|
| 1305 | { |
---|
| 1306 | (*ti)[0]=ip[0]=idx; |
---|
| 1307 | } |
---|
| 1308 | if (ip[1]<0 && *ivert==(*points)[(*ti)[1]]) |
---|
| 1309 | { |
---|
| 1310 | (*ti)[1]=ip[1]=idx; |
---|
| 1311 | } |
---|
| 1312 | if (ip[2]<0 && *ivert==(*points)[(*ti)[2]]) |
---|
| 1313 | { |
---|
| 1314 | (*ti)[2]=ip[2]=idx; |
---|
| 1315 | } |
---|
| 1316 | idx++; |
---|
| 1317 | } |
---|
| 1318 | if (ip[0]<0) |
---|
| 1319 | { |
---|
| 1320 | points_->push_back((*points)[(*ti)[0]]); |
---|
| 1321 | (*ti)[0]=ip[0]=points_->size()-1; |
---|
| 1322 | } |
---|
| 1323 | if (ip[1]<0) |
---|
| 1324 | { |
---|
| 1325 | points_->push_back((*points)[(*ti)[1]]); |
---|
| 1326 | (*ti)[1]=ip[1]=points_->size()-1; |
---|
| 1327 | } |
---|
| 1328 | if (ip[2]<0) |
---|
| 1329 | { |
---|
| 1330 | points_->push_back((*points)[(*ti)[2]]); |
---|
| 1331 | (*ti)[2]=ip[2]=points_->size()-1; |
---|
| 1332 | } |
---|
| 1333 | } |
---|
| 1334 | makeDrawable(); |
---|
| 1335 | return points_.release(); |
---|
| 1336 | } |
---|
| 1337 | |
---|
| 1338 | void DelaunayConstraint::handleOverlaps(void) |
---|
| 1339 | { |
---|
[5985] | 1340 | |
---|
[5928] | 1341 | osg::ref_ptr<osgUtil::Tessellator> tscx=new osgUtil::Tessellator; |
---|
| 1342 | tscx->setTessellationType(osgUtil::Tessellator::TESS_TYPE_GEOMETRY); |
---|
[4611] | 1343 | tscx->setBoundaryOnly(true); |
---|
[5928] | 1344 | tscx->setWindingType( osgUtil::Tessellator::TESS_WINDING_ODD); |
---|
[4611] | 1345 | |
---|
| 1346 | |
---|
| 1347 | |
---|
| 1348 | |
---|
[5928] | 1349 | tscx->retessellatePolygons(*this); |
---|
[4611] | 1350 | } |
---|
| 1351 | |
---|
| 1352 | } |
---|