| 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | |
|---|
| 14 | #include <osgUtil/DelaunayTriangulator> |
|---|
| 15 | |
|---|
| 16 | |
|---|
| 17 | |
|---|
| 18 | #include <osg/GL> |
|---|
| 19 | #include <osg/Vec3> |
|---|
| 20 | #include <osg/Array> |
|---|
| 21 | #include <osg/Notify> |
|---|
| 22 | |
|---|
| 23 | #include <algorithm> |
|---|
| 24 | #include <set> |
|---|
| 25 | #include <map> //GWM July 2005 map is used in constraints. |
|---|
| 26 | #include <osgUtil/Tessellator> // tessellator triangulates the constrained triangles |
|---|
| 27 | #include <stdlib.h> |
|---|
| 28 | #include <iterator> |
|---|
| 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 | |
|---|
| 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 = |
|---|
| 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 | |
|---|
| 68 | cy = |
|---|
| 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 | |
|---|
| 74 | |
|---|
| 75 | |
|---|
| 76 | |
|---|
| 77 | r2 = sqrt((c.x() - cx) * (c.x() - cx) + (c.y() - cy) * (c.y() - cy)); |
|---|
| 78 | } |
|---|
| 79 | return osg::Vec3(cx, cy, r2); |
|---|
| 80 | } |
|---|
| 81 | |
|---|
| 82 | |
|---|
| 83 | |
|---|
| 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()); |
|---|
| 90 | return r2 <= circle.z()*circle.z(); |
|---|
| 91 | |
|---|
| 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 | |
|---|
| 111 | struct Less |
|---|
| 112 | { |
|---|
| 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 | |
|---|
| 122 | Edge(): ib_(0), ie_(0), ibs_(0), ies_(0), duplicate_(false) {} |
|---|
| 123 | Edge(Vertex_index ib, Vertex_index ie) : ib_(ib), ie_(ie), ibs_(osg::minimum(ib, ie)), ies_(osg::maximum(ib, ie)), duplicate_(false) {} |
|---|
| 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 | |
|---|
| 152 | class Triangle |
|---|
| 153 | { |
|---|
| 154 | public: |
|---|
| 155 | |
|---|
| 156 | Triangle(): |
|---|
| 157 | a_(0), |
|---|
| 158 | b_(0), |
|---|
| 159 | c_(0) {} |
|---|
| 160 | |
|---|
| 161 | |
|---|
| 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 | |
|---|
| 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 | |
|---|
| 188 | inline Vertex_index a() const { return a_; } |
|---|
| 189 | inline Vertex_index b() const { return b_; } |
|---|
| 190 | inline Vertex_index c() const { return c_; } |
|---|
| 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; } |
|---|
| 194 | |
|---|
| 195 | inline const Edge &get_edge(int idx) const { return edge_[idx]; } |
|---|
| 196 | inline const osg::Vec3 &get_circumcircle() const { return cc_; } |
|---|
| 197 | |
|---|
| 198 | inline osg::Vec3 compute_centroid(const osg::Vec3Array *points) const |
|---|
| 199 | { |
|---|
| 200 | return ((*points)[a_] +(*points)[b_] + (*points)[c_])/3; |
|---|
| 201 | } |
|---|
| 202 | |
|---|
| 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 | |
|---|
| 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 | |
|---|
| 224 | |
|---|
| 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 | |
|---|
| 291 | int lineBisectTest(const osg::Vec3 apt,const osg::Vec3 bpt,const osg::Vec3 cpt, const osg::Vec2 p2) const |
|---|
| 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(); |
|---|
| 300 | |
|---|
| 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_]; |
|---|
| 325 | return lineBisectTest(apt,bpt,cpt,p2)?1:0; |
|---|
| 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_]; |
|---|
| 333 | return lineBisectTest(apt,bpt,cpt,p2)?2:0; |
|---|
| 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_]; |
|---|
| 341 | return lineBisectTest(apt,bpt,cpt,p2)?3:0; |
|---|
| 342 | } |
|---|
| 343 | return 0; |
|---|
| 344 | } |
|---|
| 345 | |
|---|
| 346 | private: |
|---|
| 347 | |
|---|
| 348 | |
|---|
| 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 | |
|---|
| 368 | Vertex_index a_; |
|---|
| 369 | Vertex_index b_; |
|---|
| 370 | Vertex_index c_; |
|---|
| 371 | osg::Vec3 cc_; |
|---|
| 372 | Edge edge_[3]; |
|---|
| 373 | }; |
|---|
| 374 | |
|---|
| 375 | typedef std::list<Triangle> Triangle_list; |
|---|
| 376 | |
|---|
| 377 | |
|---|
| 378 | bool Sample_point_compare(const osg::Vec3 &p1, const osg::Vec3 &p2) |
|---|
| 379 | { |
|---|
| 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_INFO << "Two points are coincident at "<<p1.x() <<","<<p1.y() << std::endl; |
|---|
| 385 | return p1.z() < p2.z(); |
|---|
| 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 | |
|---|
| 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 | |
|---|
| 434 | int DelaunayTriangulator::getindex(const osg::Vec3 &pt,const osg::Vec3Array *points) |
|---|
| 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; |
|---|
| 453 | osg::ref_ptr<osgUtil::Tessellator> tscx=new osgUtil::Tessellator; |
|---|
| 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)); |
|---|
| 465 | tscx->setTessellationNormal(osg::Vec3(0.0,0.0,1.0)); |
|---|
| 466 | tscx->setTessellationType(osgUtil::Tessellator::TESS_TYPE_GEOMETRY); |
|---|
| 467 | tscx->setBoundaryOnly(false); |
|---|
| 468 | tscx->setWindingType( osgUtil::Tessellator::TESS_WINDING_ODD); |
|---|
| 469 | tscx->retessellatePolygons(*(gtess.get())); |
|---|
| 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)]); |
|---|
| 489 | pidx=points->size()-1; |
|---|
| 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)]); |
|---|
| 500 | pidx1=points->size()-1; |
|---|
| 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)]); |
|---|
| 511 | pidx2=points->size()-1; |
|---|
| 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)]); |
|---|
| 529 | pidx=points->size()-1; |
|---|
| 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)]); |
|---|
| 537 | pidx1=points->size()-1; |
|---|
| 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)]); |
|---|
| 548 | pidx2=points->size()-1; |
|---|
| 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)]); |
|---|
| 574 | pidx=points->size()-1; |
|---|
| 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)]); |
|---|
| 587 | pidx1=points->size()-1; |
|---|
| 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)]); |
|---|
| 597 | pidx2=points->size()-1; |
|---|
| 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_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 | template <typename TVector> |
|---|
| 617 | void removeIndices( TVector& elements, unsigned int index ) |
|---|
| 618 | { |
|---|
| 619 | typename TVector::iterator itr = elements.begin(); |
|---|
| 620 | while ( itr != elements.end() ) |
|---|
| 621 | { |
|---|
| 622 | if ( (*itr)==index ) |
|---|
| 623 | { |
|---|
| 624 | itr = elements.erase(itr); |
|---|
| 625 | } |
|---|
| 626 | else |
|---|
| 627 | { |
|---|
| 628 | if ((*itr)>index) --(*itr); |
|---|
| 629 | ++itr; |
|---|
| 630 | } |
|---|
| 631 | } |
|---|
| 632 | } |
|---|
| 633 | |
|---|
| 634 | void DelaunayConstraint::removeVerticesInside(const DelaunayConstraint *dco) |
|---|
| 635 | { |
|---|
| 636 | |
|---|
| 637 | |
|---|
| 638 | |
|---|
| 639 | int nrem=0; |
|---|
| 640 | osg::Vec3Array *vertices= dynamic_cast< osg::Vec3Array*>(getVertexArray()); |
|---|
| 641 | if (vertices) |
|---|
| 642 | { |
|---|
| 643 | for (osg::Vec3Array::iterator vitr=vertices->begin(); vitr!=vertices->end(); ) |
|---|
| 644 | { |
|---|
| 645 | if (dco->contains(*vitr)) |
|---|
| 646 | { |
|---|
| 647 | unsigned int idx=vitr-vertices->begin(); |
|---|
| 648 | |
|---|
| 649 | for (unsigned int ipr=0; ipr<getNumPrimitiveSets(); ipr++) |
|---|
| 650 | { |
|---|
| 651 | osg::PrimitiveSet* prset=getPrimitiveSet(ipr); |
|---|
| 652 | switch (prset->getType()) |
|---|
| 653 | { |
|---|
| 654 | case osg::PrimitiveSet::DrawElementsUBytePrimitiveType: |
|---|
| 655 | removeIndices( *static_cast<osg::DrawElementsUByte *>(prset), idx ); |
|---|
| 656 | break; |
|---|
| 657 | case osg::PrimitiveSet::DrawElementsUShortPrimitiveType: |
|---|
| 658 | removeIndices( *static_cast<osg::DrawElementsUShort *>(prset), idx ); |
|---|
| 659 | break; |
|---|
| 660 | case osg::PrimitiveSet::DrawElementsUIntPrimitiveType: |
|---|
| 661 | removeIndices( *static_cast<osg::DrawElementsUInt *>(prset), idx ); |
|---|
| 662 | break; |
|---|
| 663 | default: |
|---|
| 664 | OSG_WARN << "Invalid prset " <<ipr<< " tp " << prset->getType() << " types PrimitiveType,DrawArraysPrimitiveType=1 etc" << std::endl; |
|---|
| 665 | break; |
|---|
| 666 | } |
|---|
| 667 | } |
|---|
| 668 | vitr=vertices->erase(vitr); |
|---|
| 669 | nrem++; |
|---|
| 670 | |
|---|
| 671 | } |
|---|
| 672 | else |
|---|
| 673 | { |
|---|
| 674 | vitr++; |
|---|
| 675 | } |
|---|
| 676 | } |
|---|
| 677 | } |
|---|
| 678 | } |
|---|
| 679 | |
|---|
| 680 | void DelaunayConstraint::merge(DelaunayConstraint *dco) |
|---|
| 681 | { |
|---|
| 682 | unsigned int ipr; |
|---|
| 683 | if (dco) { |
|---|
| 684 | osg::Vec3Array* vmerge=dynamic_cast<osg::Vec3Array*>(getVertexArray()); |
|---|
| 685 | if (!vmerge) vmerge=new osg::Vec3Array; |
|---|
| 686 | setVertexArray(vmerge); |
|---|
| 687 | for ( ipr=0; ipr<dco->getNumPrimitiveSets(); ipr++) |
|---|
| 688 | { |
|---|
| 689 | osg::PrimitiveSet* prset=dco->getPrimitiveSet(ipr); |
|---|
| 690 | osg::DrawArrays *drarr=dynamic_cast<osg::DrawArrays *> (prset); |
|---|
| 691 | if (drarr) |
|---|
| 692 | { |
|---|
| 693 | |
|---|
| 694 | unsigned int noff=vmerge->size(); |
|---|
| 695 | unsigned int n1=drarr->getFirst(); |
|---|
| 696 | unsigned int numv=drarr->getCount(); |
|---|
| 697 | addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::LINE_LOOP,n1+noff,numv)); |
|---|
| 698 | } |
|---|
| 699 | } |
|---|
| 700 | osg::Vec3Array* varr=dynamic_cast<osg::Vec3Array*>(dco->getVertexArray()); |
|---|
| 701 | if (varr) vmerge->insert(vmerge->end(),varr->begin(),varr->end()); |
|---|
| 702 | } |
|---|
| 703 | } |
|---|
| 704 | |
|---|
| 705 | void DelaunayTriangulator::_uniqueifyPoints() |
|---|
| 706 | { |
|---|
| 707 | std::sort( points_->begin(), points_->end() ); |
|---|
| 708 | |
|---|
| 709 | osg::ref_ptr<osg::Vec3Array> temppts = new osg::Vec3Array; |
|---|
| 710 | |
|---|
| 711 | |
|---|
| 712 | |
|---|
| 713 | |
|---|
| 714 | |
|---|
| 715 | osg::Vec3Array::iterator p = points_->begin(); |
|---|
| 716 | osg::Vec3 v = *p; |
|---|
| 717 | |
|---|
| 718 | temppts->push_back( (v = *p)); |
|---|
| 719 | for( ; p != points_->end(); p++ ) |
|---|
| 720 | { |
|---|
| 721 | if( v[0] == (*p)[0] && v[1] == (*p)[1] ) |
|---|
| 722 | continue; |
|---|
| 723 | |
|---|
| 724 | temppts->push_back( (v = *p)); |
|---|
| 725 | } |
|---|
| 726 | |
|---|
| 727 | points_->clear(); |
|---|
| 728 | std::insert_iterator< osg::Vec3Array > ci(*(points_.get()),points_->begin()); |
|---|
| 729 | std::copy( temppts->begin(), temppts->end(), ci ); |
|---|
| 730 | } |
|---|
| 731 | |
|---|
| 732 | osgUtil::DelaunayConstraint *getconvexhull(osg::Vec3Array *points) |
|---|
| 733 | { |
|---|
| 734 | osg::ref_ptr<osgUtil::DelaunayConstraint> dcconvexhull=new osgUtil::DelaunayConstraint; |
|---|
| 735 | |
|---|
| 736 | |
|---|
| 737 | osg::Vec3Array *verts=new osg::Vec3Array; |
|---|
| 738 | verts->push_back(*(points->begin()) ); |
|---|
| 739 | verts->push_back(*(points->begin()+1) ); |
|---|
| 740 | for (osg::Vec3Array::iterator vit=(points->begin()+2); vit!=points->end(); vit++) { |
|---|
| 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=(*vit)-*(verts->end()-1); |
|---|
| 746 | float cosang=(lastseg^thisseg).z(); |
|---|
| 747 | if (cosang <0.0) { |
|---|
| 748 | verts->pop_back(); |
|---|
| 749 | } else { ok=0;} |
|---|
| 750 | } |
|---|
| 751 | verts->push_back(*vit ); |
|---|
| 752 | |
|---|
| 753 | } |
|---|
| 754 | for (osg::Vec3Array::reverse_iterator rvit=points->rbegin()+1; rvit!=points->rend(); rvit++) { |
|---|
| 755 | |
|---|
| 756 | bool ok=1; |
|---|
| 757 | while (ok && verts->size()>1) { |
|---|
| 758 | osg::Vec3 lastseg=*(verts->end()-2)-*(verts->end()-1); |
|---|
| 759 | osg::Vec3 thisseg=(*rvit)-*(verts->end()-1); |
|---|
| 760 | float cosang=(lastseg^thisseg).z(); |
|---|
| 761 | if (cosang <0.0) { |
|---|
| 762 | verts->pop_back(); |
|---|
| 763 | } else { ok=0;} |
|---|
| 764 | } |
|---|
| 765 | if ((*rvit)!=*(verts->begin())) verts->push_back(*rvit ); |
|---|
| 766 | |
|---|
| 767 | } |
|---|
| 768 | dcconvexhull->setVertexArray(verts); |
|---|
| 769 | dcconvexhull->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::LINE_LOOP,0,verts->size()) ); |
|---|
| 770 | return dcconvexhull.release(); |
|---|
| 771 | } |
|---|
| 772 | |
|---|
| 773 | bool DelaunayTriangulator::triangulate() |
|---|
| 774 | { |
|---|
| 775 | |
|---|
| 776 | if (!points_.valid()) |
|---|
| 777 | { |
|---|
| 778 | OSG_WARN << "Warning: DelaunayTriangulator::triangulate(): invalid sample point array" << std::endl; |
|---|
| 779 | return false; |
|---|
| 780 | } |
|---|
| 781 | |
|---|
| 782 | osg::Vec3Array *points = points_.get(); |
|---|
| 783 | |
|---|
| 784 | if (points->size() < 1) |
|---|
| 785 | { |
|---|
| 786 | OSG_WARN << "Warning: DelaunayTriangulator::triangulate(): too few sample points" << std::endl; |
|---|
| 787 | return false; |
|---|
| 788 | } |
|---|
| 789 | |
|---|
| 790 | |
|---|
| 791 | _uniqueifyPoints(); |
|---|
| 792 | |
|---|
| 793 | |
|---|
| 794 | |
|---|
| 795 | Triangle_list triangles; |
|---|
| 796 | Triangle_list discarded_tris; |
|---|
| 797 | |
|---|
| 798 | |
|---|
| 799 | linelist::iterator linitr; |
|---|
| 800 | for (linitr=constraint_lines.begin();linitr!=constraint_lines.end();linitr++) |
|---|
| 801 | { |
|---|
| 802 | DelaunayConstraint* dc=(*linitr).get(); |
|---|
| 803 | const osg::Vec3Array* vercon= dynamic_cast<const osg::Vec3Array*>(dc->getVertexArray()); |
|---|
| 804 | if (vercon) |
|---|
| 805 | { |
|---|
| 806 | int nadded=0; |
|---|
| 807 | for (unsigned int icon=0;icon<vercon->size();icon++) |
|---|
| 808 | { |
|---|
| 809 | osg::Vec3 p1=(*vercon)[icon]; |
|---|
| 810 | int idx=getindex(p1,points_.get()); |
|---|
| 811 | if (idx<0) |
|---|
| 812 | { |
|---|
| 813 | points_->push_back(p1); |
|---|
| 814 | nadded++; |
|---|
| 815 | } |
|---|
| 816 | else |
|---|
| 817 | { |
|---|
| 818 | OSG_WARN << "DelaunayTriangulator: ignore a duplicate point at "<< p1.x()<< " " << p1.y() << std::endl;; |
|---|
| 819 | } |
|---|
| 820 | } |
|---|
| 821 | } |
|---|
| 822 | |
|---|
| 823 | } |
|---|
| 824 | |
|---|
| 825 | |
|---|
| 826 | |
|---|
| 827 | OSG_INFO << "DelaunayTriangulator: pre-sorting sample points\n"; |
|---|
| 828 | std::sort(points->begin(), points->end(), Sample_point_compare); |
|---|
| 829 | |
|---|
| 830 | osg::ref_ptr<osgUtil::DelaunayConstraint> dcconvexhull=getconvexhull(points); |
|---|
| 831 | addInputConstraint(dcconvexhull.get()); |
|---|
| 832 | |
|---|
| 833 | |
|---|
| 834 | GLuint last_valid_index = points->size() - 1; |
|---|
| 835 | |
|---|
| 836 | |
|---|
| 837 | float minx = (*points)[0].x(); |
|---|
| 838 | float maxx = (*points)[last_valid_index].x(); |
|---|
| 839 | |
|---|
| 840 | |
|---|
| 841 | float miny = (*points)[0].y(); |
|---|
| 842 | float maxy = miny; |
|---|
| 843 | |
|---|
| 844 | OSG_INFO << "DelaunayTriangulator: finding minimum and maximum Y values\n"; |
|---|
| 845 | osg::Vec3Array::const_iterator mmi; |
|---|
| 846 | for (mmi=points->begin(); mmi!=points->end(); ++mmi) |
|---|
| 847 | { |
|---|
| 848 | if (mmi->y() < miny) miny = mmi->y(); |
|---|
| 849 | if (mmi->y() > maxy) maxy = mmi->y(); |
|---|
| 850 | } |
|---|
| 851 | |
|---|
| 852 | |
|---|
| 853 | |
|---|
| 854 | |
|---|
| 855 | |
|---|
| 856 | |
|---|
| 857 | |
|---|
| 858 | |
|---|
| 859 | points_->push_back(osg::Vec3(minx - .10*(maxx - minx), miny - .10*(maxy - miny), 0)); |
|---|
| 860 | points_->push_back(osg::Vec3(maxx + .10*(maxx - minx), miny - .10*(maxy - miny), 0)); |
|---|
| 861 | points_->push_back(osg::Vec3(maxx + .10*(maxx - minx), maxy + .10*(maxy - miny), 0)); |
|---|
| 862 | points_->push_back(osg::Vec3(minx - .10*(maxx - minx), maxy + .10*(maxy - miny), 0)); |
|---|
| 863 | |
|---|
| 864 | |
|---|
| 865 | triangles.push_back(Triangle(last_valid_index+1, last_valid_index+2, last_valid_index+3, points)); |
|---|
| 866 | triangles.push_back(Triangle(last_valid_index+4, last_valid_index+1, last_valid_index+3, points)); |
|---|
| 867 | |
|---|
| 868 | |
|---|
| 869 | |
|---|
| 870 | GLuint pidx = 0; |
|---|
| 871 | osg::Vec3Array::const_iterator i; |
|---|
| 872 | |
|---|
| 873 | OSG_INFO << "DelaunayTriangulator: triangulating vertex grid (" << (points->size()-3) <<" points)\n"; |
|---|
| 874 | |
|---|
| 875 | for (i=points->begin(); i!=points->end(); ++i, ++pidx) |
|---|
| 876 | { |
|---|
| 877 | |
|---|
| 878 | |
|---|
| 879 | if (pidx > last_valid_index) break; |
|---|
| 880 | |
|---|
| 881 | Edge_set edges; |
|---|
| 882 | |
|---|
| 883 | |
|---|
| 884 | Triangle_list::iterator j, next_j; |
|---|
| 885 | for (j=triangles.begin(); j!=triangles.end(); j = next_j) |
|---|
| 886 | { |
|---|
| 887 | |
|---|
| 888 | next_j = j; |
|---|
| 889 | ++next_j; |
|---|
| 890 | |
|---|
| 891 | |
|---|
| 892 | osg::Vec3 cc = j->get_circumcircle(); |
|---|
| 893 | |
|---|
| 894 | |
|---|
| 895 | |
|---|
| 896 | float xdist = i->x() - cc.x(); |
|---|
| 897 | |
|---|
| 898 | |
|---|
| 899 | if ((xdist ) > cc.z() ) |
|---|
| 900 | { |
|---|
| 901 | discarded_tris.push_back(*j); |
|---|
| 902 | |
|---|
| 903 | triangles.erase(j); |
|---|
| 904 | } |
|---|
| 905 | else |
|---|
| 906 | { |
|---|
| 907 | |
|---|
| 908 | |
|---|
| 909 | |
|---|
| 910 | if (point_in_circle(*i, cc)) |
|---|
| 911 | { |
|---|
| 912 | for (int ei=0; ei<3; ++ei) |
|---|
| 913 | { |
|---|
| 914 | std::pair<Edge_set::iterator, bool> result = edges.insert(j->get_edge(ei)); |
|---|
| 915 | if (!result.second) |
|---|
| 916 | { |
|---|
| 917 | |
|---|
| 918 | |
|---|
| 919 | |
|---|
| 920 | Edge& edge = const_cast<Edge&>(*(result.first)); |
|---|
| 921 | |
|---|
| 922 | |
|---|
| 923 | edge.set_duplicate(!edge.get_duplicate()); |
|---|
| 924 | } |
|---|
| 925 | } |
|---|
| 926 | triangles.erase(j); |
|---|
| 927 | } |
|---|
| 928 | } |
|---|
| 929 | } |
|---|
| 930 | |
|---|
| 931 | |
|---|
| 932 | Edge_set::iterator ci; |
|---|
| 933 | for (ci=edges.begin(); ci!=edges.end(); ++ci) |
|---|
| 934 | { |
|---|
| 935 | if (!ci->get_duplicate()) |
|---|
| 936 | { |
|---|
| 937 | triangles.push_back(Triangle(pidx, ci->ib(), ci->ie(), points)); |
|---|
| 938 | } |
|---|
| 939 | } |
|---|
| 940 | } |
|---|
| 941 | |
|---|
| 942 | |
|---|
| 943 | |
|---|
| 944 | OSG_INFO << "DelaunayTriangulator: finalizing and cleaning up structures\n"; |
|---|
| 945 | |
|---|
| 946 | |
|---|
| 947 | triangles.insert(triangles.begin(), discarded_tris.begin(), discarded_tris.end()); |
|---|
| 948 | |
|---|
| 949 | |
|---|
| 950 | |
|---|
| 951 | |
|---|
| 952 | |
|---|
| 953 | |
|---|
| 954 | for (linelist::iterator dcitr=constraint_lines.begin();dcitr!=constraint_lines.end();dcitr++) |
|---|
| 955 | { |
|---|
| 956 | |
|---|
| 957 | const osg::Vec3Array* vercon = dynamic_cast<const osg::Vec3Array*>((*dcitr)->getVertexArray()); |
|---|
| 958 | if (vercon) |
|---|
| 959 | { |
|---|
| 960 | for (unsigned int ipr=0; ipr<(*dcitr)->getNumPrimitiveSets(); ipr++) |
|---|
| 961 | { |
|---|
| 962 | const osg::PrimitiveSet* prset=(*dcitr)->getPrimitiveSet(ipr); |
|---|
| 963 | if (prset->getMode()==osg::PrimitiveSet::LINE_LOOP || |
|---|
| 964 | prset->getMode()==osg::PrimitiveSet::LINE_STRIP) |
|---|
| 965 | { |
|---|
| 966 | |
|---|
| 967 | |
|---|
| 968 | unsigned int ip1=getindex((*vercon)[prset->index (prset->getNumIndices()-1)],points_.get()); |
|---|
| 969 | for (unsigned int i=0; i<prset->getNumIndices(); i++) |
|---|
| 970 | { |
|---|
| 971 | unsigned int ip2=getindex((*vercon)[prset->index(i)],points_.get()); |
|---|
| 972 | if (i>0 || prset->getMode()==osg::PrimitiveSet::LINE_LOOP) |
|---|
| 973 | { |
|---|
| 974 | |
|---|
| 975 | |
|---|
| 976 | |
|---|
| 977 | bool edgused=false; |
|---|
| 978 | Triangle_list::iterator titr; |
|---|
| 979 | const osg::Vec3 curp=(*vercon)[prset->index(i)]; |
|---|
| 980 | int it=0; |
|---|
| 981 | for (titr=triangles.begin(); titr!=triangles.end() && !edgused; ++titr) |
|---|
| 982 | { |
|---|
| 983 | |
|---|
| 984 | if (titr->isedge(ip1,ip2)) edgused=true; |
|---|
| 985 | if (titr->isedge(ip2,ip1)) edgused=true; |
|---|
| 986 | |
|---|
| 987 | |
|---|
| 988 | it++; |
|---|
| 989 | } |
|---|
| 990 | if (!edgused) |
|---|
| 991 | { |
|---|
| 992 | |
|---|
| 993 | |
|---|
| 994 | osg::Vec2 p1((*points_)[ip1].x(),(*points_)[ip1].y()); |
|---|
| 995 | osg::Vec2 p2((*points_)[ip2].x(),(*points_)[ip2].y()); |
|---|
| 996 | int ntr=0; |
|---|
| 997 | std::vector<const Triangle *> trisToDelete; |
|---|
| 998 | |
|---|
| 999 | |
|---|
| 1000 | |
|---|
| 1001 | |
|---|
| 1002 | |
|---|
| 1003 | for (titr=triangles.begin(); titr!=triangles.end(); ) |
|---|
| 1004 | { |
|---|
| 1005 | int icut=titr->lineBisects(points_.get(),ip1,p2); |
|---|
| 1006 | |
|---|
| 1007 | |
|---|
| 1008 | if (icut>0) |
|---|
| 1009 | { |
|---|
| 1010 | |
|---|
| 1011 | std::vector<unsigned int> edgeRight, edgeLeft; |
|---|
| 1012 | edgeRight.push_back(ip1); |
|---|
| 1013 | edgeLeft.push_back(ip1); |
|---|
| 1014 | |
|---|
| 1015 | trisToDelete.push_back(&(*titr)); |
|---|
| 1016 | |
|---|
| 1017 | unsigned int e1, e2; |
|---|
| 1018 | if (icut==1) |
|---|
| 1019 | { |
|---|
| 1020 | |
|---|
| 1021 | e1=titr->b(); e2=titr->c(); |
|---|
| 1022 | } |
|---|
| 1023 | else if (icut==2) |
|---|
| 1024 | { |
|---|
| 1025 | e1=titr->c(); e2=titr->a(); |
|---|
| 1026 | } |
|---|
| 1027 | else if (icut==3) |
|---|
| 1028 | { |
|---|
| 1029 | e1=titr->a(); e2=titr->b(); |
|---|
| 1030 | } |
|---|
| 1031 | edgeRight.push_back(e2); |
|---|
| 1032 | edgeLeft.push_back(e1); |
|---|
| 1033 | |
|---|
| 1034 | const Triangle *tradj=getTriangleWithEdge(e2,e1, &triangles); |
|---|
| 1035 | if (tradj) |
|---|
| 1036 | { |
|---|
| 1037 | while (tradj && !tradj->usesVertex(ip2) && trisToDelete.size()<999) |
|---|
| 1038 | { |
|---|
| 1039 | trisToDelete.push_back(tradj); |
|---|
| 1040 | icut=tradj->whichEdge(points_.get(),p1,p2,e1,e2); |
|---|
| 1041 | |
|---|
| 1042 | |
|---|
| 1043 | |
|---|
| 1044 | if (icut==1) {e1=tradj->b(); e2=tradj->c();} |
|---|
| 1045 | else if (icut==2) {e1=tradj->c(); e2=tradj->a();} |
|---|
| 1046 | else if (icut==3) {e1=tradj->a(); e2=tradj->b();} |
|---|
| 1047 | if (edgeLeft.back()!=e1 && edgeRight.back()==e2 && e1!=ip2) { |
|---|
| 1048 | edgeLeft.push_back(e1); |
|---|
| 1049 | } else if(edgeRight.back()!=e2 && edgeLeft.back()==e1 && e2!=ip2) { |
|---|
| 1050 | edgeRight.push_back(e2); |
|---|
| 1051 | } else { |
|---|
| 1052 | if (!tradj->usesVertex(ip2)) OSG_WARN << "tradj error " << tradj->a()<< " , " << tradj->b()<< " , " << tradj->c()<< std::endl; |
|---|
| 1053 | } |
|---|
| 1054 | const Triangle *previousTradj = tradj; |
|---|
| 1055 | tradj=getTriangleWithEdge(e2,e1, &triangles); |
|---|
| 1056 | if (tradj == previousTradj) { |
|---|
| 1057 | tradj = 0; |
|---|
| 1058 | } |
|---|
| 1059 | } |
|---|
| 1060 | if (trisToDelete.size()>=900) { |
|---|
| 1061 | OSG_WARN << " found " << trisToDelete.size() << " adjacent tris " <<std::endl; |
|---|
| 1062 | } |
|---|
| 1063 | } |
|---|
| 1064 | |
|---|
| 1065 | |
|---|
| 1066 | edgeLeft.push_back(ip2); |
|---|
| 1067 | edgeRight.push_back(ip2); |
|---|
| 1068 | if (tradj) trisToDelete.push_back(tradj); |
|---|
| 1069 | |
|---|
| 1070 | Triangle_list constrainedtris=fillHole(points_.get(),edgeLeft); |
|---|
| 1071 | triangles.insert(triangles.begin(), constrainedtris.begin(), constrainedtris.end()); |
|---|
| 1072 | constrainedtris=fillHole(points_.get(),edgeRight); |
|---|
| 1073 | triangles.insert(triangles.begin(), constrainedtris.begin(), constrainedtris.end()); |
|---|
| 1074 | |
|---|
| 1075 | } |
|---|
| 1076 | ++titr; |
|---|
| 1077 | ntr++; |
|---|
| 1078 | } |
|---|
| 1079 | |
|---|
| 1080 | Triangle_list::iterator tri; |
|---|
| 1081 | for (tri=triangles.begin(); tri!=triangles.end(); ) |
|---|
| 1082 | { |
|---|
| 1083 | bool deleted=false; |
|---|
| 1084 | for (std::vector<const Triangle *>::iterator deleteTri=trisToDelete.begin(); |
|---|
| 1085 | deleteTri!=trisToDelete.end(); ) |
|---|
| 1086 | { |
|---|
| 1087 | if (&(*tri)==*deleteTri) |
|---|
| 1088 | { |
|---|
| 1089 | deleted=true; |
|---|
| 1090 | tri=triangles.erase(tri); |
|---|
| 1091 | deleteTri=trisToDelete.erase(deleteTri); |
|---|
| 1092 | } else {deleteTri++; } |
|---|
| 1093 | } |
|---|
| 1094 | if (!deleted) ++tri; |
|---|
| 1095 | } |
|---|
| 1096 | } |
|---|
| 1097 | } |
|---|
| 1098 | |
|---|
| 1099 | ip1=ip2; |
|---|
| 1100 | } |
|---|
| 1101 | } |
|---|
| 1102 | } |
|---|
| 1103 | } |
|---|
| 1104 | } |
|---|
| 1105 | |
|---|
| 1106 | |
|---|
| 1107 | |
|---|
| 1108 | |
|---|
| 1109 | Triangle_list::iterator tri; |
|---|
| 1110 | GLuint supertriend = last_valid_index+4; |
|---|
| 1111 | for (tri=triangles.begin(); tri!=triangles.end();) |
|---|
| 1112 | { |
|---|
| 1113 | if ((tri->a() > last_valid_index && tri->a() <= supertriend) || |
|---|
| 1114 | (tri->b() > last_valid_index && tri->b() <= supertriend ) || |
|---|
| 1115 | (tri->c() > last_valid_index && tri->c() <= supertriend )) { |
|---|
| 1116 | tri=triangles.erase(tri); |
|---|
| 1117 | } else { |
|---|
| 1118 | if (tri->a() > last_valid_index) { |
|---|
| 1119 | tri->incrementa(-4); |
|---|
| 1120 | } |
|---|
| 1121 | if (tri->b() > last_valid_index) { |
|---|
| 1122 | tri->incrementb(-4); |
|---|
| 1123 | } |
|---|
| 1124 | if (tri->c() > last_valid_index) { |
|---|
| 1125 | tri->incrementc(-4); |
|---|
| 1126 | } |
|---|
| 1127 | ++tri; |
|---|
| 1128 | } |
|---|
| 1129 | } |
|---|
| 1130 | |
|---|
| 1131 | |
|---|
| 1132 | points->erase(points->begin()+last_valid_index+1,points->begin()+last_valid_index+5); |
|---|
| 1133 | |
|---|
| 1134 | |
|---|
| 1135 | |
|---|
| 1136 | |
|---|
| 1137 | std::vector<GLuint> pt_indices; |
|---|
| 1138 | pt_indices.reserve(triangles.size() * 3); |
|---|
| 1139 | |
|---|
| 1140 | |
|---|
| 1141 | OSG_INFO << "DelaunayTriangulator: building primitive(s)\n"; |
|---|
| 1142 | Triangle_list::const_iterator ti; |
|---|
| 1143 | for (ti=triangles.begin(); ti!=triangles.end(); ++ti) |
|---|
| 1144 | { |
|---|
| 1145 | |
|---|
| 1146 | |
|---|
| 1147 | |
|---|
| 1148 | |
|---|
| 1149 | |
|---|
| 1150 | if ( ti->get_circumcircle().z()>0.0) |
|---|
| 1151 | { |
|---|
| 1152 | |
|---|
| 1153 | if (normals_.valid()) |
|---|
| 1154 | { |
|---|
| 1155 | (normals_.get())->push_back(ti->compute_normal(points)); |
|---|
| 1156 | } |
|---|
| 1157 | |
|---|
| 1158 | pt_indices.push_back(ti->a()); |
|---|
| 1159 | pt_indices.push_back(ti->b()); |
|---|
| 1160 | pt_indices.push_back(ti->c()); |
|---|
| 1161 | } |
|---|
| 1162 | } |
|---|
| 1163 | |
|---|
| 1164 | prim_tris_ = new osg::DrawElementsUInt(GL_TRIANGLES, pt_indices.size(), &(pt_indices.front())); |
|---|
| 1165 | |
|---|
| 1166 | OSG_INFO << "DelaunayTriangulator: process done, " << prim_tris_->getNumPrimitives() << " triangles remain\n"; |
|---|
| 1167 | |
|---|
| 1168 | return true; |
|---|
| 1169 | } |
|---|
| 1170 | |
|---|
| 1171 | void DelaunayTriangulator::removeInternalTriangles(DelaunayConstraint *dc ) |
|---|
| 1172 | { |
|---|
| 1173 | if (dc) { |
|---|
| 1174 | |
|---|
| 1175 | |
|---|
| 1176 | |
|---|
| 1177 | |
|---|
| 1178 | int ndel=0; |
|---|
| 1179 | osg::Vec3Array::iterator normitr; |
|---|
| 1180 | if( normals_.valid() ) |
|---|
| 1181 | normitr = normals_->begin(); |
|---|
| 1182 | |
|---|
| 1183 | |
|---|
| 1184 | for (osg::DrawElementsUInt::iterator triit=prim_tris_->begin(); triit!=prim_tris_->end(); ) |
|---|
| 1185 | { |
|---|
| 1186 | |
|---|
| 1187 | Triangle tritest((*triit), *(triit+1), *(triit+2), points_.get()); |
|---|
| 1188 | if ( dc->contains(tritest.compute_centroid( points_.get()) ) ) |
|---|
| 1189 | { |
|---|
| 1190 | |
|---|
| 1191 | |
|---|
| 1192 | dc->addtriangle((*triit), *(triit+1), *(triit+2)); |
|---|
| 1193 | triit=prim_tris_->erase(triit); |
|---|
| 1194 | triit=prim_tris_->erase(triit); |
|---|
| 1195 | triit=prim_tris_->erase(triit); |
|---|
| 1196 | if (normals_.valid()) |
|---|
| 1197 | { |
|---|
| 1198 | |
|---|
| 1199 | normitr=normals_->erase(normitr); |
|---|
| 1200 | } |
|---|
| 1201 | ndel++; |
|---|
| 1202 | } |
|---|
| 1203 | else |
|---|
| 1204 | { |
|---|
| 1205 | if (normals_.valid()) |
|---|
| 1206 | { |
|---|
| 1207 | normitr++; |
|---|
| 1208 | } |
|---|
| 1209 | |
|---|
| 1210 | triit+=3; |
|---|
| 1211 | } |
|---|
| 1212 | } |
|---|
| 1213 | |
|---|
| 1214 | OSG_INFO << "end of test dc, deleted " << ndel << std::endl; |
|---|
| 1215 | } |
|---|
| 1216 | } |
|---|
| 1217 | |
|---|
| 1218 | |
|---|
| 1219 | float DelaunayConstraint::windingNumber(const osg::Vec3 &testpoint) const |
|---|
| 1220 | { |
|---|
| 1221 | |
|---|
| 1222 | float theta=0; |
|---|
| 1223 | const osg::Vec3Array *vertices= dynamic_cast<const osg::Vec3Array*>(getVertexArray()); |
|---|
| 1224 | if (vertices) |
|---|
| 1225 | { |
|---|
| 1226 | for (unsigned int ipr=0; ipr<getNumPrimitiveSets(); ipr++) |
|---|
| 1227 | { |
|---|
| 1228 | const osg::PrimitiveSet* prset=getPrimitiveSet(ipr); |
|---|
| 1229 | if (prset->getMode()==osg::PrimitiveSet::LINE_LOOP) |
|---|
| 1230 | { |
|---|
| 1231 | |
|---|
| 1232 | |
|---|
| 1233 | const osg::Vec3 prev=(*vertices)[prset->index (prset->getNumIndices()-1)]; |
|---|
| 1234 | osg::Vec3 pi(prev.x()-testpoint.x(),prev.y()-testpoint.y(),0); |
|---|
| 1235 | pi.normalize(); |
|---|
| 1236 | for (unsigned int i=0; i<prset->getNumIndices(); i++) |
|---|
| 1237 | { |
|---|
| 1238 | const osg::Vec3 curp=(*vertices)[prset->index (i)]; |
|---|
| 1239 | osg::Vec3 edge(curp.x()-testpoint.x(),curp.y()-testpoint.y(),0); |
|---|
| 1240 | edge.normalize(); |
|---|
| 1241 | double cth=edge*pi; |
|---|
| 1242 | if (cth<=-0.99999 ) |
|---|
| 1243 | { |
|---|
| 1244 | |
|---|
| 1245 | return 0; |
|---|
| 1246 | } |
|---|
| 1247 | else |
|---|
| 1248 | { |
|---|
| 1249 | if (cth<0.99999) |
|---|
| 1250 | { |
|---|
| 1251 | float dang=(cth<1 && cth>-1)?acos(edge*pi):0; |
|---|
| 1252 | float zsign=edge.x()*pi.y()-pi.x()*edge.y(); |
|---|
| 1253 | if (zsign>0) theta+=dang; |
|---|
| 1254 | else if (zsign<0) theta-=dang; |
|---|
| 1255 | } |
|---|
| 1256 | } |
|---|
| 1257 | pi=edge; |
|---|
| 1258 | } |
|---|
| 1259 | } |
|---|
| 1260 | } |
|---|
| 1261 | } |
|---|
| 1262 | |
|---|
| 1263 | return theta/osg::PI/2.0; |
|---|
| 1264 | } |
|---|
| 1265 | osg::DrawElementsUInt *DelaunayConstraint::makeDrawable() |
|---|
| 1266 | { |
|---|
| 1267 | |
|---|
| 1268 | std::vector<GLuint> pt_indices; |
|---|
| 1269 | pt_indices.reserve(_interiorTris.size() * 3); |
|---|
| 1270 | trilist::const_iterator ti; |
|---|
| 1271 | for (ti=_interiorTris.begin(); ti!=_interiorTris.end(); ++ti) |
|---|
| 1272 | { |
|---|
| 1273 | |
|---|
| 1274 | |
|---|
| 1275 | |
|---|
| 1276 | |
|---|
| 1277 | |
|---|
| 1278 | pt_indices.push_back((*ti)[0]); |
|---|
| 1279 | pt_indices.push_back((*ti)[1]); |
|---|
| 1280 | pt_indices.push_back((*ti)[2]); |
|---|
| 1281 | } |
|---|
| 1282 | prim_tris_ = new osg::DrawElementsUInt(GL_TRIANGLES, pt_indices.size(), &(pt_indices.front())); |
|---|
| 1283 | |
|---|
| 1284 | return prim_tris_.get(); |
|---|
| 1285 | } |
|---|
| 1286 | bool DelaunayConstraint::contains(const osg::Vec3 &testpoint) const |
|---|
| 1287 | { |
|---|
| 1288 | |
|---|
| 1289 | float theta=windingNumber(testpoint); |
|---|
| 1290 | return fabs(theta)>0.9; |
|---|
| 1291 | } |
|---|
| 1292 | bool DelaunayConstraint::outside(const osg::Vec3 &testpoint) const |
|---|
| 1293 | { |
|---|
| 1294 | |
|---|
| 1295 | float theta=windingNumber(testpoint); |
|---|
| 1296 | return fabs(theta)<.05; |
|---|
| 1297 | } |
|---|
| 1298 | |
|---|
| 1299 | |
|---|
| 1300 | void DelaunayConstraint::addtriangle(int i1, int i2, int i3) |
|---|
| 1301 | { |
|---|
| 1302 | |
|---|
| 1303 | |
|---|
| 1304 | |
|---|
| 1305 | int *ip=new int[3]; |
|---|
| 1306 | ip[0]=i1; |
|---|
| 1307 | ip[1]=i2; |
|---|
| 1308 | ip[2]=i3; |
|---|
| 1309 | _interiorTris.push_back(ip); |
|---|
| 1310 | } |
|---|
| 1311 | osg::Vec3Array* DelaunayConstraint::getPoints(const osg::Vec3Array *points) |
|---|
| 1312 | { |
|---|
| 1313 | |
|---|
| 1314 | osg::ref_ptr<osg::Vec3Array> points_=new osg::Vec3Array; |
|---|
| 1315 | trilist::iterator ti; |
|---|
| 1316 | for (ti=_interiorTris.begin(); ti!=_interiorTris.end(); ++ti) { |
|---|
| 1317 | int idx=0; |
|---|
| 1318 | int ip[3]={-1,-1,-1}; |
|---|
| 1319 | |
|---|
| 1320 | for (osg::Vec3Array::iterator ivert=points_->begin(); ivert!=points_->end(); ivert++) |
|---|
| 1321 | { |
|---|
| 1322 | if (ip[0]<0 && *ivert==(*points)[(*ti)[0]]) |
|---|
| 1323 | { |
|---|
| 1324 | (*ti)[0]=ip[0]=idx; |
|---|
| 1325 | } |
|---|
| 1326 | if (ip[1]<0 && *ivert==(*points)[(*ti)[1]]) |
|---|
| 1327 | { |
|---|
| 1328 | (*ti)[1]=ip[1]=idx; |
|---|
| 1329 | } |
|---|
| 1330 | if (ip[2]<0 && *ivert==(*points)[(*ti)[2]]) |
|---|
| 1331 | { |
|---|
| 1332 | (*ti)[2]=ip[2]=idx; |
|---|
| 1333 | } |
|---|
| 1334 | idx++; |
|---|
| 1335 | } |
|---|
| 1336 | if (ip[0]<0) |
|---|
| 1337 | { |
|---|
| 1338 | points_->push_back((*points)[(*ti)[0]]); |
|---|
| 1339 | (*ti)[0]=ip[0]=points_->size()-1; |
|---|
| 1340 | } |
|---|
| 1341 | if (ip[1]<0) |
|---|
| 1342 | { |
|---|
| 1343 | points_->push_back((*points)[(*ti)[1]]); |
|---|
| 1344 | (*ti)[1]=ip[1]=points_->size()-1; |
|---|
| 1345 | } |
|---|
| 1346 | if (ip[2]<0) |
|---|
| 1347 | { |
|---|
| 1348 | points_->push_back((*points)[(*ti)[2]]); |
|---|
| 1349 | (*ti)[2]=ip[2]=points_->size()-1; |
|---|
| 1350 | } |
|---|
| 1351 | } |
|---|
| 1352 | makeDrawable(); |
|---|
| 1353 | return points_.release(); |
|---|
| 1354 | } |
|---|
| 1355 | |
|---|
| 1356 | void DelaunayConstraint::handleOverlaps(void) |
|---|
| 1357 | { |
|---|
| 1358 | |
|---|
| 1359 | osg::ref_ptr<osgUtil::Tessellator> tscx=new osgUtil::Tessellator; |
|---|
| 1360 | tscx->setTessellationType(osgUtil::Tessellator::TESS_TYPE_GEOMETRY); |
|---|
| 1361 | tscx->setBoundaryOnly(true); |
|---|
| 1362 | tscx->setWindingType( osgUtil::Tessellator::TESS_WINDING_ODD); |
|---|
| 1363 | |
|---|
| 1364 | |
|---|
| 1365 | |
|---|
| 1366 | |
|---|
| 1367 | tscx->retessellatePolygons(*this); |
|---|
| 1368 | } |
|---|
| 1369 | |
|---|
| 1370 | } |
|---|