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