| 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | |
|---|
| 14 | #include <osg/TriangleIndexFunctor> |
|---|
| 15 | |
|---|
| 16 | #include <osgUtil/Simplifier> |
|---|
| 17 | |
|---|
| 18 | #include <osgUtil/SmoothingVisitor> |
|---|
| 19 | #include <osgUtil/TriStripVisitor> |
|---|
| 20 | |
|---|
| 21 | #include <set> |
|---|
| 22 | #include <list> |
|---|
| 23 | #include <algorithm> |
|---|
| 24 | |
|---|
| 25 | using namespace osgUtil; |
|---|
| 26 | |
|---|
| 27 | struct dereference_less |
|---|
| 28 | { |
|---|
| 29 | template<class T, class U> |
|---|
| 30 | inline bool operator() (const T& lhs,const U& rhs) const |
|---|
| 31 | { |
|---|
| 32 | return *lhs < *rhs; |
|---|
| 33 | } |
|---|
| 34 | }; |
|---|
| 35 | |
|---|
| 36 | template<class T> |
|---|
| 37 | bool dereference_check_less(const T& lhs,const T& rhs) |
|---|
| 38 | { |
|---|
| 39 | if (lhs==rhs) return false; |
|---|
| 40 | if (!lhs) return true; |
|---|
| 41 | if (!rhs) return false; |
|---|
| 42 | return *lhs < *rhs; |
|---|
| 43 | } |
|---|
| 44 | |
|---|
| 45 | struct dereference_clear |
|---|
| 46 | { |
|---|
| 47 | template<class T> |
|---|
| 48 | inline void operator() (const T& t) |
|---|
| 49 | { |
|---|
| 50 | T& non_const_t = const_cast<T&>(t); |
|---|
| 51 | non_const_t->clear(); |
|---|
| 52 | } |
|---|
| 53 | }; |
|---|
| 54 | |
|---|
| 55 | class EdgeCollapse |
|---|
| 56 | { |
|---|
| 57 | public: |
|---|
| 58 | |
|---|
| 59 | |
|---|
| 60 | #if 1 |
|---|
| 61 | typedef float error_type; |
|---|
| 62 | #else |
|---|
| 63 | typedef double error_type; |
|---|
| 64 | #endif |
|---|
| 65 | |
|---|
| 66 | struct Triangle; |
|---|
| 67 | struct Edge; |
|---|
| 68 | struct Point; |
|---|
| 69 | |
|---|
| 70 | |
|---|
| 71 | EdgeCollapse(): |
|---|
| 72 | _computeErrorMetricUsingLength(false) {} |
|---|
| 73 | |
|---|
| 74 | ~EdgeCollapse(); |
|---|
| 75 | |
|---|
| 76 | void setGeometry(osg::Geometry* geometry, const Simplifier::IndexList& protectedPoints); |
|---|
| 77 | osg::Geometry* getGeometry() { return _geometry; } |
|---|
| 78 | |
|---|
| 79 | void setComputeErrorMetricUsingLength(bool flag) { _computeErrorMetricUsingLength = flag; } |
|---|
| 80 | bool getComputeErrorMetricUsingLength() const { return _computeErrorMetricUsingLength; } |
|---|
| 81 | |
|---|
| 82 | unsigned int getNumOfTriangles() { return _triangleSet.size(); } |
|---|
| 83 | |
|---|
| 84 | Point* computeInterpolatedPoint(Edge* edge,float r) const |
|---|
| 85 | { |
|---|
| 86 | Point* point = new Point; |
|---|
| 87 | float r1 = 1.0f-r; |
|---|
| 88 | float r2 = r; |
|---|
| 89 | Point* p1 = edge->_p1.get(); |
|---|
| 90 | Point* p2 = edge->_p2.get(); |
|---|
| 91 | |
|---|
| 92 | if (p1==0 || p2==0) |
|---|
| 93 | { |
|---|
| 94 | osg::notify(osg::NOTICE)<<"Error computeInterpolatedPoint("<<edge<<",r) p1 and/or p2==0"<<std::endl; |
|---|
| 95 | return 0; |
|---|
| 96 | } |
|---|
| 97 | |
|---|
| 98 | point->_vertex = p1->_vertex * r1 + p2->_vertex * r2; |
|---|
| 99 | unsigned int s = osg::minimum(p1->_attributes.size(),p2->_attributes.size()); |
|---|
| 100 | for(unsigned int i=0;i<s;++i) |
|---|
| 101 | { |
|---|
| 102 | point->_attributes.push_back(p1->_attributes[i]*r1 + p2->_attributes[i]*r2); |
|---|
| 103 | } |
|---|
| 104 | return point; |
|---|
| 105 | } |
|---|
| 106 | |
|---|
| 107 | Point* computeOptimalPoint(Edge* edge) const |
|---|
| 108 | { |
|---|
| 109 | return computeInterpolatedPoint(edge,0.5f); |
|---|
| 110 | } |
|---|
| 111 | |
|---|
| 112 | error_type computeErrorMetric(Edge* edge,Point* point) const |
|---|
| 113 | { |
|---|
| 114 | if (_computeErrorMetricUsingLength) |
|---|
| 115 | { |
|---|
| 116 | error_type dx = error_type(edge->_p1->_vertex.x()) - error_type(edge->_p2->_vertex.x()); |
|---|
| 117 | error_type dy = error_type(edge->_p1->_vertex.y()) - error_type(edge->_p2->_vertex.y()); |
|---|
| 118 | error_type dz = error_type(edge->_p1->_vertex.z()) - error_type(edge->_p2->_vertex.z()); |
|---|
| 119 | return sqrt(dx*dx + dy*dy + dz*dz); |
|---|
| 120 | } |
|---|
| 121 | else if (point) |
|---|
| 122 | { |
|---|
| 123 | typedef std::set< osg::ref_ptr<Triangle> > LocalTriangleSet ; |
|---|
| 124 | LocalTriangleSet triangles; |
|---|
| 125 | std::copy( edge->_p1->_triangles.begin(), edge->_p1->_triangles.end(), std::inserter(triangles,triangles.begin())); |
|---|
| 126 | std::copy( edge->_p2->_triangles.begin(), edge->_p2->_triangles.end(), std::inserter(triangles,triangles.begin())); |
|---|
| 127 | |
|---|
| 128 | const osg::Vec3& vertex = point->_vertex; |
|---|
| 129 | error_type error = 0.0; |
|---|
| 130 | |
|---|
| 131 | if (triangles.empty()) return 0.0; |
|---|
| 132 | |
|---|
| 133 | for(LocalTriangleSet::iterator itr=triangles.begin(); |
|---|
| 134 | itr!=triangles.end(); |
|---|
| 135 | ++itr) |
|---|
| 136 | { |
|---|
| 137 | error += fabs( (*itr)->distance(vertex) ); |
|---|
| 138 | } |
|---|
| 139 | |
|---|
| 140 | |
|---|
| 141 | error /= error_type(triangles.size()); |
|---|
| 142 | |
|---|
| 143 | return error; |
|---|
| 144 | } |
|---|
| 145 | else |
|---|
| 146 | { |
|---|
| 147 | return 0; |
|---|
| 148 | } |
|---|
| 149 | } |
|---|
| 150 | |
|---|
| 151 | void updateErrorMetricForEdge(Edge* edge) |
|---|
| 152 | { |
|---|
| 153 | if (!edge->_p1 || !edge->_p2) |
|---|
| 154 | { |
|---|
| 155 | osg::notify(osg::NOTICE)<<"Error updateErrorMetricForEdge("<<edge<<") p1 and/or p2==0"<<std::endl; |
|---|
| 156 | return; |
|---|
| 157 | } |
|---|
| 158 | |
|---|
| 159 | |
|---|
| 160 | osg::ref_ptr<Edge> keep_local_reference_to_edge(edge); |
|---|
| 161 | |
|---|
| 162 | if (_edgeSet.count(keep_local_reference_to_edge)!=0) |
|---|
| 163 | { |
|---|
| 164 | _edgeSet.erase(keep_local_reference_to_edge); |
|---|
| 165 | } |
|---|
| 166 | |
|---|
| 167 | edge->_proposedPoint = computeOptimalPoint(edge); |
|---|
| 168 | |
|---|
| 169 | if (_computeErrorMetricUsingLength) |
|---|
| 170 | { |
|---|
| 171 | edge->setErrorMetric( computeErrorMetric( edge, edge->_proposedPoint.get())); |
|---|
| 172 | } |
|---|
| 173 | else |
|---|
| 174 | { |
|---|
| 175 | edge->updateMaxNormalDeviationOnEdgeCollapse(); |
|---|
| 176 | |
|---|
| 177 | if (edge->getMaxNormalDeviationOnEdgeCollapse()<=1.0 && !edge->isAdjacentToBoundary()) |
|---|
| 178 | edge->setErrorMetric( computeErrorMetric( edge, edge->_proposedPoint.get())); |
|---|
| 179 | else |
|---|
| 180 | edge->setErrorMetric( FLT_MAX ); |
|---|
| 181 | } |
|---|
| 182 | |
|---|
| 183 | _edgeSet.insert(keep_local_reference_to_edge); |
|---|
| 184 | } |
|---|
| 185 | |
|---|
| 186 | void updateErrorMetricForAllEdges() |
|---|
| 187 | { |
|---|
| 188 | typedef std::vector< osg::ref_ptr<Edge> > LocalEdgeList ; |
|---|
| 189 | LocalEdgeList edges; |
|---|
| 190 | std::copy( _edgeSet.begin(), _edgeSet.end(), std::back_inserter(edges)); |
|---|
| 191 | |
|---|
| 192 | _edgeSet.clear(); |
|---|
| 193 | |
|---|
| 194 | for(LocalEdgeList::iterator itr=edges.begin(); |
|---|
| 195 | itr!=edges.end(); |
|---|
| 196 | ++itr) |
|---|
| 197 | { |
|---|
| 198 | Edge* edge = itr->get(); |
|---|
| 199 | |
|---|
| 200 | if (_computeErrorMetricUsingLength) |
|---|
| 201 | { |
|---|
| 202 | edge->setErrorMetric( computeErrorMetric( edge, edge->_proposedPoint.get())); |
|---|
| 203 | } |
|---|
| 204 | else |
|---|
| 205 | { |
|---|
| 206 | edge->_proposedPoint = computeOptimalPoint(edge); |
|---|
| 207 | edge->updateMaxNormalDeviationOnEdgeCollapse(); |
|---|
| 208 | |
|---|
| 209 | if (edge->getMaxNormalDeviationOnEdgeCollapse()<=1.0 && !edge->isAdjacentToBoundary()) |
|---|
| 210 | edge->setErrorMetric( computeErrorMetric( edge, edge->_proposedPoint.get())); |
|---|
| 211 | else |
|---|
| 212 | edge->setErrorMetric( FLT_MAX ); |
|---|
| 213 | |
|---|
| 214 | } |
|---|
| 215 | _edgeSet.insert(edge); |
|---|
| 216 | } |
|---|
| 217 | } |
|---|
| 218 | |
|---|
| 219 | bool collapseMinimumErrorEdge() |
|---|
| 220 | { |
|---|
| 221 | if (!_edgeSet.empty()) |
|---|
| 222 | { |
|---|
| 223 | Edge* edge = const_cast<Edge*>(_edgeSet.begin()->get()); |
|---|
| 224 | |
|---|
| 225 | if (edge->getErrorMetric()==FLT_MAX) |
|---|
| 226 | { |
|---|
| 227 | osg::notify(osg::INFO)<<"collapseMinimumErrorEdge() return false due to edge->getErrorMetric()==FLT_MAX"<<std::endl; |
|---|
| 228 | return false; |
|---|
| 229 | } |
|---|
| 230 | |
|---|
| 231 | osg::ref_ptr<Point> pNew = edge->_proposedPoint.valid()? edge->_proposedPoint.get() : computeInterpolatedPoint(edge,0.5f); |
|---|
| 232 | return (collapseEdge(edge,pNew.get())); |
|---|
| 233 | } |
|---|
| 234 | osg::notify(osg::INFO)<<"collapseMinimumErrorEdge() return false due to _edgeSet.empty()"<<std::endl; |
|---|
| 235 | return false; |
|---|
| 236 | } |
|---|
| 237 | |
|---|
| 238 | |
|---|
| 239 | bool divideLongestEdge() |
|---|
| 240 | { |
|---|
| 241 | if (!_edgeSet.empty()) |
|---|
| 242 | { |
|---|
| 243 | Edge* edge = const_cast<Edge*>(_edgeSet.rbegin()->get()); |
|---|
| 244 | |
|---|
| 245 | if (edge->getErrorMetric()==FLT_MAX) |
|---|
| 246 | { |
|---|
| 247 | osg::notify(osg::INFO)<<"divideLongestEdge() return false due to edge->getErrorMetric()==FLT_MAX"<<std::endl; |
|---|
| 248 | return false; |
|---|
| 249 | } |
|---|
| 250 | |
|---|
| 251 | osg::ref_ptr<Point> pNew = edge->_proposedPoint.valid()? edge->_proposedPoint.get() : computeInterpolatedPoint(edge,0.5f); |
|---|
| 252 | return (divideEdge(edge,pNew.get())); |
|---|
| 253 | } |
|---|
| 254 | osg::notify(osg::INFO)<<"divideLongestEdge() return false due to _edgeSet.empty()"<<std::endl; |
|---|
| 255 | return false; |
|---|
| 256 | } |
|---|
| 257 | |
|---|
| 258 | void copyBackToGeometry(); |
|---|
| 259 | |
|---|
| 260 | typedef std::vector<float> FloatList; |
|---|
| 261 | typedef std::set<osg::ref_ptr<Edge>,dereference_less > EdgeSet; |
|---|
| 262 | typedef std::set< osg::ref_ptr<Point>,dereference_less > PointSet; |
|---|
| 263 | typedef std::vector< osg::ref_ptr<Point> > PointList; |
|---|
| 264 | typedef std::list< osg::ref_ptr<Triangle> > TriangleList; |
|---|
| 265 | typedef std::set< osg::ref_ptr<Triangle> > TriangleSet; |
|---|
| 266 | typedef std::map< osg::ref_ptr<Triangle>, unsigned int, dereference_less > TriangleMap; |
|---|
| 267 | |
|---|
| 268 | struct Point : public osg::Referenced |
|---|
| 269 | { |
|---|
| 270 | Point(): _protected(false), _index(0) {} |
|---|
| 271 | |
|---|
| 272 | bool _protected; |
|---|
| 273 | |
|---|
| 274 | unsigned int _index; |
|---|
| 275 | |
|---|
| 276 | osg::Vec3 _vertex; |
|---|
| 277 | FloatList _attributes; |
|---|
| 278 | TriangleSet _triangles; |
|---|
| 279 | |
|---|
| 280 | void clear() |
|---|
| 281 | { |
|---|
| 282 | _attributes.clear(); |
|---|
| 283 | _triangles.clear(); |
|---|
| 284 | } |
|---|
| 285 | |
|---|
| 286 | bool operator < ( const Point& rhs) const |
|---|
| 287 | { |
|---|
| 288 | if (_vertex < rhs._vertex) return true; |
|---|
| 289 | if (rhs._vertex < _vertex) return false; |
|---|
| 290 | |
|---|
| 291 | return _attributes < rhs._attributes; |
|---|
| 292 | } |
|---|
| 293 | |
|---|
| 294 | bool isBoundaryPoint() const |
|---|
| 295 | { |
|---|
| 296 | if (_protected) return true; |
|---|
| 297 | |
|---|
| 298 | for(TriangleSet::const_iterator itr=_triangles.begin(); |
|---|
| 299 | itr!=_triangles.end(); |
|---|
| 300 | ++itr) |
|---|
| 301 | { |
|---|
| 302 | const Triangle* triangle = itr->get(); |
|---|
| 303 | if ((triangle->_e1->_p1==this || triangle->_e1->_p2==this) && triangle->_e1->isBoundaryEdge()) return true; |
|---|
| 304 | if ((triangle->_e2->_p1==this || triangle->_e2->_p2==this) && triangle->_e2->isBoundaryEdge()) return true; |
|---|
| 305 | if ((triangle->_e3->_p1==this || triangle->_e3->_p2==this) && triangle->_e3->isBoundaryEdge()) return true; |
|---|
| 306 | |
|---|
| 307 | |
|---|
| 308 | } |
|---|
| 309 | return false; |
|---|
| 310 | } |
|---|
| 311 | |
|---|
| 312 | }; |
|---|
| 313 | |
|---|
| 314 | struct Edge : public osg::Referenced |
|---|
| 315 | { |
|---|
| 316 | Edge(): _errorMetric(0.0), _maximumDeviation(1.0) {} |
|---|
| 317 | |
|---|
| 318 | void clear() |
|---|
| 319 | { |
|---|
| 320 | _p1 = 0; |
|---|
| 321 | _p2 = 0; |
|---|
| 322 | _triangles.clear(); |
|---|
| 323 | } |
|---|
| 324 | |
|---|
| 325 | osg::ref_ptr<Point> _p1; |
|---|
| 326 | osg::ref_ptr<Point> _p2; |
|---|
| 327 | |
|---|
| 328 | TriangleSet _triangles; |
|---|
| 329 | |
|---|
| 330 | error_type _errorMetric; |
|---|
| 331 | error_type _maximumDeviation; |
|---|
| 332 | |
|---|
| 333 | osg::ref_ptr<Point> _proposedPoint; |
|---|
| 334 | |
|---|
| 335 | void setErrorMetric(error_type errorMetric) { _errorMetric = errorMetric; } |
|---|
| 336 | error_type getErrorMetric() const { return _errorMetric; } |
|---|
| 337 | |
|---|
| 338 | bool operator < ( const Edge& rhs) const |
|---|
| 339 | { |
|---|
| 340 | |
|---|
| 341 | if (getErrorMetric()<rhs.getErrorMetric()) return true; |
|---|
| 342 | else if (rhs.getErrorMetric()<getErrorMetric()) return false; |
|---|
| 343 | |
|---|
| 344 | if (dereference_check_less(_p1,rhs._p1)) return true; |
|---|
| 345 | if (dereference_check_less(rhs._p1,_p1)) return false; |
|---|
| 346 | |
|---|
| 347 | return dereference_check_less(_p2,rhs._p2); |
|---|
| 348 | } |
|---|
| 349 | |
|---|
| 350 | bool operator == ( const Edge& rhs) const |
|---|
| 351 | { |
|---|
| 352 | if (&rhs==this) return true; |
|---|
| 353 | if (*this<rhs) return false; |
|---|
| 354 | if (rhs<*this) return false; |
|---|
| 355 | return true; |
|---|
| 356 | } |
|---|
| 357 | |
|---|
| 358 | bool operator != ( const Edge& rhs) const |
|---|
| 359 | { |
|---|
| 360 | if (&rhs==this) return false; |
|---|
| 361 | if (*this<rhs) return true; |
|---|
| 362 | if (rhs<*this) return true; |
|---|
| 363 | return false; |
|---|
| 364 | } |
|---|
| 365 | |
|---|
| 366 | void addTriangle(Triangle* triangle) |
|---|
| 367 | { |
|---|
| 368 | _triangles.insert(triangle); |
|---|
| 369 | |
|---|
| 370 | } |
|---|
| 371 | |
|---|
| 372 | bool isBoundaryEdge() const |
|---|
| 373 | { |
|---|
| 374 | return _triangles.size()<=1; |
|---|
| 375 | } |
|---|
| 376 | |
|---|
| 377 | bool isAdjacentToBoundary() const |
|---|
| 378 | { |
|---|
| 379 | return isBoundaryEdge() || _p1->isBoundaryPoint() || _p2->isBoundaryPoint(); |
|---|
| 380 | } |
|---|
| 381 | |
|---|
| 382 | |
|---|
| 383 | void updateMaxNormalDeviationOnEdgeCollapse() |
|---|
| 384 | { |
|---|
| 385 | |
|---|
| 386 | _maximumDeviation = 0.0f; |
|---|
| 387 | for(TriangleSet::iterator itr1=_p1->_triangles.begin(); |
|---|
| 388 | itr1!=_p1->_triangles.end(); |
|---|
| 389 | ++itr1) |
|---|
| 390 | { |
|---|
| 391 | if (_triangles.count(*itr1)==0) |
|---|
| 392 | { |
|---|
| 393 | _maximumDeviation = osg::maximum(_maximumDeviation, (*itr1)->computeNormalDeviationOnEdgeCollapse(this,_proposedPoint.get())); |
|---|
| 394 | } |
|---|
| 395 | } |
|---|
| 396 | for(TriangleSet::iterator itr2=_p2->_triangles.begin(); |
|---|
| 397 | itr2!=_p2->_triangles.end(); |
|---|
| 398 | ++itr2) |
|---|
| 399 | { |
|---|
| 400 | if (_triangles.count(*itr2)==0) |
|---|
| 401 | { |
|---|
| 402 | _maximumDeviation = osg::maximum(_maximumDeviation, (*itr2)->computeNormalDeviationOnEdgeCollapse(this,_proposedPoint.get())); |
|---|
| 403 | } |
|---|
| 404 | } |
|---|
| 405 | } |
|---|
| 406 | |
|---|
| 407 | error_type getMaxNormalDeviationOnEdgeCollapse() const { return _maximumDeviation; } |
|---|
| 408 | |
|---|
| 409 | }; |
|---|
| 410 | |
|---|
| 411 | struct Triangle : public osg::Referenced |
|---|
| 412 | { |
|---|
| 413 | Triangle() {} |
|---|
| 414 | |
|---|
| 415 | void clear() |
|---|
| 416 | { |
|---|
| 417 | _p1 = 0; |
|---|
| 418 | _p2 = 0; |
|---|
| 419 | _p3 = 0; |
|---|
| 420 | |
|---|
| 421 | _e1 = 0; |
|---|
| 422 | _e2 = 0; |
|---|
| 423 | _e3 = 0; |
|---|
| 424 | } |
|---|
| 425 | |
|---|
| 426 | inline bool operator < (const Triangle& rhs) const |
|---|
| 427 | { |
|---|
| 428 | if (dereference_check_less(_p1,rhs._p1)) return true; |
|---|
| 429 | if (dereference_check_less(rhs._p1,_p1)) return false; |
|---|
| 430 | |
|---|
| 431 | |
|---|
| 432 | const Point* lhs_lower = dereference_check_less(_p2,_p3) ? _p2.get() : _p3.get(); |
|---|
| 433 | const Point* rhs_lower = dereference_check_less(rhs._p2,rhs._p3) ? rhs._p2.get() : rhs._p3.get(); |
|---|
| 434 | |
|---|
| 435 | if (dereference_check_less(lhs_lower,rhs_lower)) return true; |
|---|
| 436 | if (dereference_check_less(rhs_lower,lhs_lower)) return false; |
|---|
| 437 | |
|---|
| 438 | const Point* lhs_upper = dereference_check_less(_p2,_p3) ? _p3.get() : _p2.get(); |
|---|
| 439 | const Point* rhs_upper = dereference_check_less(rhs._p2,rhs._p3) ? rhs._p3.get() : rhs._p2.get(); |
|---|
| 440 | |
|---|
| 441 | return dereference_check_less(lhs_upper,rhs_upper); |
|---|
| 442 | } |
|---|
| 443 | |
|---|
| 444 | |
|---|
| 445 | void setOrderedPoints(Point* p1, Point* p2, Point* p3) |
|---|
| 446 | { |
|---|
| 447 | Point* points[3]; |
|---|
| 448 | points[0] = p1; |
|---|
| 449 | points[1] = p2; |
|---|
| 450 | points[2] = p3; |
|---|
| 451 | |
|---|
| 452 | |
|---|
| 453 | unsigned int lowest = 0; |
|---|
| 454 | if (dereference_check_less(points[1],points[lowest])) lowest = 1; |
|---|
| 455 | if (dereference_check_less(points[2],points[lowest])) lowest = 2; |
|---|
| 456 | |
|---|
| 457 | _p1 = points[lowest]; |
|---|
| 458 | _p2 = points[(lowest+1)%3]; |
|---|
| 459 | _p3 = points[(lowest+2)%3]; |
|---|
| 460 | } |
|---|
| 461 | |
|---|
| 462 | void update() |
|---|
| 463 | { |
|---|
| 464 | _plane.set(_p1->_vertex,_p2->_vertex,_p3->_vertex); |
|---|
| 465 | |
|---|
| 466 | } |
|---|
| 467 | |
|---|
| 468 | osg::Plane computeNewPlaneOnEdgeCollapse(Edge* edge,Point* pNew) const |
|---|
| 469 | { |
|---|
| 470 | const Point* p1 = (_p1==edge->_p1 || _p1==edge->_p2) ? pNew : _p1.get(); |
|---|
| 471 | const Point* p2 = (_p2==edge->_p1 || _p2==edge->_p2) ? pNew : _p2.get(); |
|---|
| 472 | const Point* p3 = (_p3==edge->_p1 || _p3==edge->_p2) ? pNew : _p3.get(); |
|---|
| 473 | |
|---|
| 474 | return osg::Plane(p1->_vertex,p2->_vertex,p3->_vertex); |
|---|
| 475 | } |
|---|
| 476 | |
|---|
| 477 | |
|---|
| 478 | error_type computeNormalDeviationOnEdgeCollapse(Edge* edge,Point* pNew) const |
|---|
| 479 | { |
|---|
| 480 | const Point* p1 = (_p1==edge->_p1 || _p1==edge->_p2) ? pNew : _p1.get(); |
|---|
| 481 | const Point* p2 = (_p2==edge->_p1 || _p2==edge->_p2) ? pNew : _p2.get(); |
|---|
| 482 | const Point* p3 = (_p3==edge->_p1 || _p3==edge->_p2) ? pNew : _p3.get(); |
|---|
| 483 | |
|---|
| 484 | osg::Vec3 new_normal = (p2->_vertex - p1->_vertex) ^ (p3->_vertex - p2->_vertex); |
|---|
| 485 | new_normal.normalize(); |
|---|
| 486 | |
|---|
| 487 | error_type result = 1.0 - (new_normal.x() * _plane[0] + new_normal.y() * _plane[1] + new_normal.z() * _plane[2]); |
|---|
| 488 | return result; |
|---|
| 489 | } |
|---|
| 490 | |
|---|
| 491 | error_type distance(const osg::Vec3& vertex) const |
|---|
| 492 | { |
|---|
| 493 | return error_type(_plane[0])*error_type(vertex.x())+ |
|---|
| 494 | error_type(_plane[1])*error_type(vertex.y())+ |
|---|
| 495 | error_type(_plane[2])*error_type(vertex.z())+ |
|---|
| 496 | error_type(_plane[3]); |
|---|
| 497 | } |
|---|
| 498 | |
|---|
| 499 | bool isBoundaryTriangle() const |
|---|
| 500 | { |
|---|
| 501 | return (_e1->isBoundaryEdge() || _e2->isBoundaryEdge() || _e3->isBoundaryEdge()); |
|---|
| 502 | } |
|---|
| 503 | |
|---|
| 504 | |
|---|
| 505 | osg::ref_ptr<Point> _p1; |
|---|
| 506 | osg::ref_ptr<Point> _p2; |
|---|
| 507 | osg::ref_ptr<Point> _p3; |
|---|
| 508 | |
|---|
| 509 | osg::ref_ptr<Edge> _e1; |
|---|
| 510 | osg::ref_ptr<Edge> _e2; |
|---|
| 511 | osg::ref_ptr<Edge> _e3; |
|---|
| 512 | |
|---|
| 513 | osg::Plane _plane; |
|---|
| 514 | |
|---|
| 515 | }; |
|---|
| 516 | |
|---|
| 517 | |
|---|
| 518 | Triangle* addTriangle(unsigned int p1, unsigned int p2, unsigned int p3) |
|---|
| 519 | { |
|---|
| 520 | |
|---|
| 521 | |
|---|
| 522 | |
|---|
| 523 | if (p1==p2 || p2==p3 || p1==p3) return 0; |
|---|
| 524 | |
|---|
| 525 | Triangle* triangle = new Triangle; |
|---|
| 526 | |
|---|
| 527 | Point* points[3]; |
|---|
| 528 | points[0] = addPoint(triangle, p1); |
|---|
| 529 | points[1] = addPoint(triangle, p2); |
|---|
| 530 | points[2] = addPoint(triangle, p3); |
|---|
| 531 | |
|---|
| 532 | |
|---|
| 533 | unsigned int lowest = 0; |
|---|
| 534 | if (dereference_check_less(points[1],points[lowest])) lowest = 1; |
|---|
| 535 | if (dereference_check_less(points[2],points[lowest])) lowest = 2; |
|---|
| 536 | |
|---|
| 537 | triangle->_p1 = points[lowest]; |
|---|
| 538 | triangle->_p2 = points[(lowest+1)%3]; |
|---|
| 539 | triangle->_p3 = points[(lowest+2)%3]; |
|---|
| 540 | |
|---|
| 541 | triangle->_e1 = addEdge(triangle, triangle->_p1.get(), triangle->_p2.get()); |
|---|
| 542 | triangle->_e2 = addEdge(triangle, triangle->_p2.get(), triangle->_p3.get()); |
|---|
| 543 | triangle->_e3 = addEdge(triangle, triangle->_p3.get(), triangle->_p1.get()); |
|---|
| 544 | |
|---|
| 545 | triangle->update(); |
|---|
| 546 | |
|---|
| 547 | _triangleSet.insert(triangle); |
|---|
| 548 | |
|---|
| 549 | return triangle; |
|---|
| 550 | } |
|---|
| 551 | |
|---|
| 552 | Triangle* addTriangle(Point* p1, Point* p2, Point* p3) |
|---|
| 553 | { |
|---|
| 554 | |
|---|
| 555 | |
|---|
| 556 | |
|---|
| 557 | if (p1==p2 || p2==p3 || p1==p3) |
|---|
| 558 | { |
|---|
| 559 | |
|---|
| 560 | return 0; |
|---|
| 561 | } |
|---|
| 562 | |
|---|
| 563 | Triangle* triangle = new Triangle; |
|---|
| 564 | |
|---|
| 565 | Point* points[3]; |
|---|
| 566 | points[0] = addPoint(triangle, p1); |
|---|
| 567 | points[1] = addPoint(triangle, p2); |
|---|
| 568 | points[2] = addPoint(triangle, p3); |
|---|
| 569 | |
|---|
| 570 | |
|---|
| 571 | unsigned int lowest = 0; |
|---|
| 572 | if (dereference_check_less(points[1],points[lowest])) lowest = 1; |
|---|
| 573 | if (dereference_check_less(points[2],points[lowest])) lowest = 2; |
|---|
| 574 | |
|---|
| 575 | triangle->_p1 = points[lowest]; |
|---|
| 576 | triangle->_p2 = points[(lowest+1)%3]; |
|---|
| 577 | triangle->_p3 = points[(lowest+2)%3]; |
|---|
| 578 | |
|---|
| 579 | triangle->_e1 = addEdge(triangle, triangle->_p1.get(), triangle->_p2.get()); |
|---|
| 580 | triangle->_e2 = addEdge(triangle, triangle->_p2.get(), triangle->_p3.get()); |
|---|
| 581 | triangle->_e3 = addEdge(triangle, triangle->_p3.get(), triangle->_p1.get()); |
|---|
| 582 | |
|---|
| 583 | triangle->update(); |
|---|
| 584 | |
|---|
| 585 | _triangleSet.insert(triangle); |
|---|
| 586 | |
|---|
| 587 | return triangle; |
|---|
| 588 | } |
|---|
| 589 | |
|---|
| 590 | void removeTriangle(Triangle* triangle) |
|---|
| 591 | { |
|---|
| 592 | if (triangle->_p1.valid()) removePoint(triangle,triangle->_p1.get()); |
|---|
| 593 | if (triangle->_p2.valid()) removePoint(triangle,triangle->_p2.get()); |
|---|
| 594 | if (triangle->_p3.valid()) removePoint(triangle,triangle->_p3.get()); |
|---|
| 595 | |
|---|
| 596 | if (triangle->_e1.valid()) removeEdge(triangle,triangle->_e1.get()); |
|---|
| 597 | if (triangle->_e2.valid()) removeEdge(triangle,triangle->_e2.get()); |
|---|
| 598 | if (triangle->_e3.valid()) removeEdge(triangle,triangle->_e3.get()); |
|---|
| 599 | |
|---|
| 600 | _triangleSet.erase(triangle); |
|---|
| 601 | } |
|---|
| 602 | |
|---|
| 603 | void replaceTrianglePoint(Triangle* triangle, Point* pOriginal, Point* pNew) |
|---|
| 604 | { |
|---|
| 605 | if (triangle->_p1==pOriginal || triangle->_p2==pOriginal || triangle->_p3==pOriginal) |
|---|
| 606 | { |
|---|
| 607 | |
|---|
| 608 | if (triangle->_p1==pOriginal) triangle->_p1=pNew; |
|---|
| 609 | if (triangle->_p2==pOriginal) triangle->_p2=pNew; |
|---|
| 610 | if (triangle->_p3==pOriginal) triangle->_p3=pNew; |
|---|
| 611 | |
|---|
| 612 | |
|---|
| 613 | triangle->_e1 = replaceEdgePoint(triangle->_e1.get(),pOriginal,pNew); |
|---|
| 614 | triangle->_e2 = replaceEdgePoint(triangle->_e2.get(),pOriginal,pNew); |
|---|
| 615 | triangle->_e3 = replaceEdgePoint(triangle->_e3.get(),pOriginal,pNew); |
|---|
| 616 | |
|---|
| 617 | |
|---|
| 618 | removePoint(triangle, pOriginal); |
|---|
| 619 | |
|---|
| 620 | |
|---|
| 621 | addPoint(triangle,pNew); |
|---|
| 622 | } |
|---|
| 623 | |
|---|
| 624 | } |
|---|
| 625 | |
|---|
| 626 | unsigned int testTriangle(Triangle* triangle) |
|---|
| 627 | { |
|---|
| 628 | unsigned int result = 0; |
|---|
| 629 | if (!(triangle->_p1)) |
|---|
| 630 | { |
|---|
| 631 | osg::notify(osg::NOTICE)<<"testTriangle("<<triangle<<") _p1==NULL"<<std::endl; |
|---|
| 632 | ++result; |
|---|
| 633 | } |
|---|
| 634 | else if (triangle->_p1->_triangles.count(triangle)==0) |
|---|
| 635 | { |
|---|
| 636 | osg::notify(osg::NOTICE)<<"testTriangle("<<triangle<<") _p1->_triangles does not contain triangle"<<std::endl; |
|---|
| 637 | ++result; |
|---|
| 638 | } |
|---|
| 639 | |
|---|
| 640 | if (!(triangle->_p2)) |
|---|
| 641 | { |
|---|
| 642 | osg::notify(osg::NOTICE)<<"testTriangle("<<triangle<<") _p2==NULL"<<std::endl; |
|---|
| 643 | ++result; |
|---|
| 644 | } |
|---|
| 645 | else if (triangle->_p2->_triangles.count(triangle)==0) |
|---|
| 646 | { |
|---|
| 647 | osg::notify(osg::NOTICE)<<"testTriangle("<<triangle<<") _p2->_triangles does not contain triangle"<<std::endl; |
|---|
| 648 | ++result; |
|---|
| 649 | } |
|---|
| 650 | |
|---|
| 651 | if (!(triangle->_p3)) |
|---|
| 652 | { |
|---|
| 653 | osg::notify(osg::NOTICE)<<"testTriangle("<<triangle<<") _p3==NULL"<<std::endl; |
|---|
| 654 | ++result; |
|---|
| 655 | } |
|---|
| 656 | else if (triangle->_p3->_triangles.count(triangle)==0) |
|---|
| 657 | { |
|---|
| 658 | osg::notify(osg::NOTICE)<<"testTriangle("<<triangle<<") _p3->_triangles does not contain triangle"<<std::endl; |
|---|
| 659 | ++result; |
|---|
| 660 | } |
|---|
| 661 | |
|---|
| 662 | if (testEdge(triangle->_e1.get())) |
|---|
| 663 | { |
|---|
| 664 | ++result; |
|---|
| 665 | osg::notify(osg::NOTICE)<<"testTriangle("<<triangle<<") _e1 test failed"<<std::endl; |
|---|
| 666 | } |
|---|
| 667 | |
|---|
| 668 | if (testEdge(triangle->_e2.get())) |
|---|
| 669 | { |
|---|
| 670 | ++result; |
|---|
| 671 | osg::notify(osg::NOTICE)<<"testTriangle("<<triangle<<") _e2 test failed"<<std::endl; |
|---|
| 672 | } |
|---|
| 673 | |
|---|
| 674 | if (testEdge(triangle->_e3.get())) |
|---|
| 675 | { |
|---|
| 676 | osg::notify(osg::NOTICE)<<"testTriangle("<<triangle<<") _e3 test failed"<<std::endl; |
|---|
| 677 | ++result; |
|---|
| 678 | } |
|---|
| 679 | |
|---|
| 680 | return result; |
|---|
| 681 | } |
|---|
| 682 | |
|---|
| 683 | unsigned int testAllTriangles() |
|---|
| 684 | { |
|---|
| 685 | unsigned int numErrors = 0; |
|---|
| 686 | for(TriangleSet::iterator itr=_triangleSet.begin(); |
|---|
| 687 | itr!=_triangleSet.end(); |
|---|
| 688 | ++itr) |
|---|
| 689 | { |
|---|
| 690 | numErrors += testTriangle(const_cast<Triangle*>(itr->get())); |
|---|
| 691 | } |
|---|
| 692 | return numErrors; |
|---|
| 693 | } |
|---|
| 694 | |
|---|
| 695 | Edge* addEdge(Triangle* triangle, Point* p1, Point* p2) |
|---|
| 696 | { |
|---|
| 697 | |
|---|
| 698 | osg::ref_ptr<Edge> edge = new Edge; |
|---|
| 699 | if (dereference_check_less(p1, p2)) |
|---|
| 700 | { |
|---|
| 701 | edge->_p1 = p1; |
|---|
| 702 | edge->_p2 = p2; |
|---|
| 703 | } |
|---|
| 704 | else |
|---|
| 705 | { |
|---|
| 706 | edge->_p1 = p2; |
|---|
| 707 | edge->_p2 = p1; |
|---|
| 708 | } |
|---|
| 709 | |
|---|
| 710 | edge->setErrorMetric( computeErrorMetric( edge.get(), edge->_proposedPoint.get())); |
|---|
| 711 | |
|---|
| 712 | EdgeSet::iterator itr = _edgeSet.find(edge); |
|---|
| 713 | if (itr==_edgeSet.end()) |
|---|
| 714 | { |
|---|
| 715 | |
|---|
| 716 | _edgeSet.insert(edge); |
|---|
| 717 | } |
|---|
| 718 | else |
|---|
| 719 | { |
|---|
| 720 | |
|---|
| 721 | edge = *itr; |
|---|
| 722 | } |
|---|
| 723 | |
|---|
| 724 | edge->addTriangle(triangle); |
|---|
| 725 | |
|---|
| 726 | return edge.get(); |
|---|
| 727 | } |
|---|
| 728 | |
|---|
| 729 | void removeEdge(Triangle* triangle, Edge* edge) |
|---|
| 730 | { |
|---|
| 731 | EdgeSet::iterator itr = _edgeSet.find(edge); |
|---|
| 732 | if (itr!=_edgeSet.end()) |
|---|
| 733 | { |
|---|
| 734 | edge->_triangles.erase(triangle); |
|---|
| 735 | if (edge->_triangles.empty()) |
|---|
| 736 | { |
|---|
| 737 | edge->_p1 = 0; |
|---|
| 738 | edge->_p2 = 0; |
|---|
| 739 | |
|---|
| 740 | |
|---|
| 741 | _edgeSet.erase(itr); |
|---|
| 742 | } |
|---|
| 743 | } |
|---|
| 744 | } |
|---|
| 745 | |
|---|
| 746 | Edge* replaceEdgePoint(Edge* edge, Point* pOriginal, Point* pNew) |
|---|
| 747 | { |
|---|
| 748 | if (edge->_p1==pOriginal || edge->_p2==pOriginal) |
|---|
| 749 | { |
|---|
| 750 | EdgeSet::iterator itr = _edgeSet.find(edge); |
|---|
| 751 | if (itr!=_edgeSet.end()) |
|---|
| 752 | { |
|---|
| 753 | |
|---|
| 754 | |
|---|
| 755 | _edgeSet.erase(itr); |
|---|
| 756 | } |
|---|
| 757 | |
|---|
| 758 | |
|---|
| 759 | if (edge->_p1==pOriginal) edge->_p1=pNew; |
|---|
| 760 | if (edge->_p2==pOriginal) edge->_p2=pNew; |
|---|
| 761 | |
|---|
| 762 | if (dereference_check_less(edge->_p2,edge->_p1)) |
|---|
| 763 | { |
|---|
| 764 | edge->_p1.swap(edge->_p2); |
|---|
| 765 | } |
|---|
| 766 | |
|---|
| 767 | itr = _edgeSet.find(edge); |
|---|
| 768 | if (itr!=_edgeSet.end()) |
|---|
| 769 | { |
|---|
| 770 | |
|---|
| 771 | edge = const_cast<Edge*>(itr->get()); |
|---|
| 772 | } |
|---|
| 773 | else |
|---|
| 774 | { |
|---|
| 775 | |
|---|
| 776 | _edgeSet.insert(edge); |
|---|
| 777 | } |
|---|
| 778 | return edge; |
|---|
| 779 | } |
|---|
| 780 | else |
|---|
| 781 | { |
|---|
| 782 | return edge; |
|---|
| 783 | } |
|---|
| 784 | |
|---|
| 785 | } |
|---|
| 786 | |
|---|
| 787 | |
|---|
| 788 | bool collapseEdge(Edge* edge, Point* pNew) |
|---|
| 789 | { |
|---|
| 790 | |
|---|
| 791 | |
|---|
| 792 | |
|---|
| 793 | #ifdef ORIGIANAL_CODE |
|---|
| 794 | if (edge->getMaxNormalDeviationOnEdgeCollapse()>1.0) |
|---|
| 795 | { |
|---|
| 796 | osg::notify(osg::NOTICE)<<"collapseEdge("<<edge<<") refused due to edge->getMaxNormalDeviationOnEdgeCollapse() = "<<edge->getMaxNormalDeviationOnEdgeCollapse()<<std::endl; |
|---|
| 797 | return false; |
|---|
| 798 | } |
|---|
| 799 | else |
|---|
| 800 | { |
|---|
| 801 | |
|---|
| 802 | } |
|---|
| 803 | #endif |
|---|
| 804 | |
|---|
| 805 | typedef std::set< osg::ref_ptr<Edge> > LocalEdgeList; |
|---|
| 806 | |
|---|
| 807 | osg::ref_ptr<Edge> keep_edge_locally_referenced_to_prevent_premature_deletion = edge; |
|---|
| 808 | osg::ref_ptr<Point> keep_point_locally_referenced_to_prevent_premature_deletion = pNew; |
|---|
| 809 | osg::ref_ptr<Point> edge_p1 = edge->_p1; |
|---|
| 810 | osg::ref_ptr<Point> edge_p2 = edge->_p2; |
|---|
| 811 | |
|---|
| 812 | TriangleMap triangleMap; |
|---|
| 813 | TriangleList triangles_p1; |
|---|
| 814 | TriangleList triangles_p2; |
|---|
| 815 | LocalEdgeList oldEdges; |
|---|
| 816 | |
|---|
| 817 | |
|---|
| 818 | if (edge_p1 != pNew) |
|---|
| 819 | { |
|---|
| 820 | for(TriangleSet::iterator itr=edge_p1->_triangles.begin(); |
|---|
| 821 | itr!=edge_p1->_triangles.end(); |
|---|
| 822 | ++itr) |
|---|
| 823 | { |
|---|
| 824 | if (edge->_triangles.count(*itr)==0) |
|---|
| 825 | { |
|---|
| 826 | Triangle* triangle = const_cast<Triangle*>(itr->get()); |
|---|
| 827 | triangles_p1.push_back(triangle); |
|---|
| 828 | oldEdges.insert(triangle->_e1); |
|---|
| 829 | oldEdges.insert(triangle->_e2); |
|---|
| 830 | oldEdges.insert(triangle->_e3); |
|---|
| 831 | } |
|---|
| 832 | } |
|---|
| 833 | |
|---|
| 834 | |
|---|
| 835 | } |
|---|
| 836 | |
|---|
| 837 | if (edge_p2 != pNew) |
|---|
| 838 | { |
|---|
| 839 | for(TriangleSet::iterator itr=edge_p2->_triangles.begin(); |
|---|
| 840 | itr!=edge_p2->_triangles.end(); |
|---|
| 841 | ++itr) |
|---|
| 842 | { |
|---|
| 843 | if (edge->_triangles.count(*itr)==0) |
|---|
| 844 | { |
|---|
| 845 | Triangle* triangle = const_cast<Triangle*>(itr->get()); |
|---|
| 846 | triangles_p2.push_back(triangle); |
|---|
| 847 | oldEdges.insert(triangle->_e1); |
|---|
| 848 | oldEdges.insert(triangle->_e2); |
|---|
| 849 | oldEdges.insert(triangle->_e3); |
|---|
| 850 | } |
|---|
| 851 | } |
|---|
| 852 | |
|---|
| 853 | } |
|---|
| 854 | |
|---|
| 855 | for(LocalEdgeList::iterator oeitr=oldEdges.begin(); |
|---|
| 856 | oeitr!=oldEdges.end(); |
|---|
| 857 | ++oeitr) |
|---|
| 858 | { |
|---|
| 859 | _edgeSet.erase(*oeitr); |
|---|
| 860 | |
|---|
| 861 | const_cast<Edge*>(oeitr->get())->setErrorMetric(0.0f); |
|---|
| 862 | |
|---|
| 863 | _edgeSet.insert(*oeitr); |
|---|
| 864 | } |
|---|
| 865 | |
|---|
| 866 | TriangleList::iterator titr_p1, titr_p2; |
|---|
| 867 | |
|---|
| 868 | for(titr_p1 = triangles_p1.begin(); |
|---|
| 869 | titr_p1 != triangles_p1.end(); |
|---|
| 870 | ++titr_p1) |
|---|
| 871 | { |
|---|
| 872 | removeTriangle(const_cast<Triangle*>(titr_p1->get())); |
|---|
| 873 | } |
|---|
| 874 | |
|---|
| 875 | for(titr_p2 = triangles_p2.begin(); |
|---|
| 876 | titr_p2 != triangles_p2.end(); |
|---|
| 877 | ++titr_p2) |
|---|
| 878 | { |
|---|
| 879 | removeTriangle(const_cast<Triangle*>(titr_p2->get())); |
|---|
| 880 | } |
|---|
| 881 | |
|---|
| 882 | |
|---|
| 883 | |
|---|
| 884 | |
|---|
| 885 | TriangleSet trianglesToRemove = edge->_triangles; |
|---|
| 886 | for(TriangleSet::iterator teitr=trianglesToRemove.begin(); |
|---|
| 887 | teitr!=trianglesToRemove.end(); |
|---|
| 888 | ++teitr) |
|---|
| 889 | { |
|---|
| 890 | Triangle* triangle = const_cast<Triangle*>(teitr->get()); |
|---|
| 891 | removeTriangle(triangle); |
|---|
| 892 | } |
|---|
| 893 | |
|---|
| 894 | LocalEdgeList newEdges; |
|---|
| 895 | |
|---|
| 896 | |
|---|
| 897 | for(titr_p1 = triangles_p1.begin(); |
|---|
| 898 | titr_p1 != triangles_p1.end(); |
|---|
| 899 | ++titr_p1) |
|---|
| 900 | { |
|---|
| 901 | Triangle* triangle = const_cast<Triangle*>(titr_p1->get()); |
|---|
| 902 | |
|---|
| 903 | Point* p1 = (triangle->_p1==edge_p1 || triangle->_p1==edge_p2)? pNew : triangle->_p1.get(); |
|---|
| 904 | Point* p2 = (triangle->_p2==edge_p1 || triangle->_p2==edge_p2)? pNew : triangle->_p2.get(); |
|---|
| 905 | Point* p3 = (triangle->_p3==edge_p1 || triangle->_p3==edge_p2)? pNew : triangle->_p3.get(); |
|---|
| 906 | |
|---|
| 907 | Triangle* newTri = addTriangle(p1,p2,p3); |
|---|
| 908 | |
|---|
| 909 | if (newTri) |
|---|
| 910 | { |
|---|
| 911 | newEdges.insert(newTri->_e1); |
|---|
| 912 | newEdges.insert(newTri->_e2); |
|---|
| 913 | newEdges.insert(newTri->_e3); |
|---|
| 914 | } |
|---|
| 915 | } |
|---|
| 916 | |
|---|
| 917 | |
|---|
| 918 | for(titr_p2 = triangles_p2.begin(); |
|---|
| 919 | titr_p2 != triangles_p2.end(); |
|---|
| 920 | ++titr_p2) |
|---|
| 921 | { |
|---|
| 922 | Triangle* triangle = const_cast<Triangle*>(titr_p2->get()); |
|---|
| 923 | |
|---|
| 924 | Point* p1 = (triangle->_p1==edge_p1 || triangle->_p1==edge_p2)? pNew : triangle->_p1.get(); |
|---|
| 925 | Point* p2 = (triangle->_p2==edge_p1 || triangle->_p2==edge_p2)? pNew : triangle->_p2.get(); |
|---|
| 926 | Point* p3 = (triangle->_p3==edge_p1 || triangle->_p3==edge_p2)? pNew : triangle->_p3.get(); |
|---|
| 927 | |
|---|
| 928 | Triangle* newTri = addTriangle(p1,p2,p3); |
|---|
| 929 | |
|---|
| 930 | if (newTri) |
|---|
| 931 | { |
|---|
| 932 | newEdges.insert(newTri->_e1); |
|---|
| 933 | newEdges.insert(newTri->_e2); |
|---|
| 934 | newEdges.insert(newTri->_e3); |
|---|
| 935 | } |
|---|
| 936 | } |
|---|
| 937 | |
|---|
| 938 | LocalEdgeList edges2UpdateErrorMetric; |
|---|
| 939 | |
|---|
| 940 | LocalEdgeList::const_iterator newEdgeIt(newEdges.begin()); |
|---|
| 941 | while (newEdgeIt != newEdges.end()) |
|---|
| 942 | { |
|---|
| 943 | const Point* p = 0; |
|---|
| 944 | if (newEdgeIt->get()->_p1.get() != pNew) |
|---|
| 945 | p = newEdgeIt->get()->_p1.get(); |
|---|
| 946 | else |
|---|
| 947 | p = newEdgeIt->get()->_p2.get(); |
|---|
| 948 | |
|---|
| 949 | TriangleSet::const_iterator triangleIt(p->_triangles.begin()); |
|---|
| 950 | while (triangleIt != p->_triangles.end()) |
|---|
| 951 | { |
|---|
| 952 | const Triangle* triangle = triangleIt->get(); |
|---|
| 953 | if (triangle->_e1->_p1 == p || triangle->_e1->_p2 == p) |
|---|
| 954 | edges2UpdateErrorMetric.insert(triangle->_e1); |
|---|
| 955 | if (triangle->_e2->_p1 == p || triangle->_e2->_p2 == p) |
|---|
| 956 | edges2UpdateErrorMetric.insert(triangle->_e2); |
|---|
| 957 | if (triangle->_e3->_p1 == p || triangle->_e3->_p2 == p) |
|---|
| 958 | edges2UpdateErrorMetric.insert(triangle->_e3); |
|---|
| 959 | |
|---|
| 960 | ++triangleIt; |
|---|
| 961 | } |
|---|
| 962 | |
|---|
| 963 | ++newEdgeIt; |
|---|
| 964 | } |
|---|
| 965 | |
|---|
| 966 | edges2UpdateErrorMetric.insert(newEdges.begin(), newEdges.end()); |
|---|
| 967 | |
|---|
| 968 | |
|---|
| 969 | |
|---|
| 970 | for(LocalEdgeList::iterator itr=edges2UpdateErrorMetric.begin(); |
|---|
| 971 | itr!=edges2UpdateErrorMetric.end(); |
|---|
| 972 | ++itr) |
|---|
| 973 | { |
|---|
| 974 | |
|---|
| 975 | updateErrorMetricForEdge(const_cast<Edge*>(itr->get())); |
|---|
| 976 | } |
|---|
| 977 | |
|---|
| 978 | return true; |
|---|
| 979 | } |
|---|
| 980 | |
|---|
| 981 | |
|---|
| 982 | bool divideEdge(Edge* edge, Point* pNew) |
|---|
| 983 | { |
|---|
| 984 | |
|---|
| 985 | |
|---|
| 986 | |
|---|
| 987 | osg::ref_ptr<Edge> keep_edge_locally_referenced_to_prevent_premature_deletion = edge; |
|---|
| 988 | TriangleSet triangles = edge->_triangles; |
|---|
| 989 | |
|---|
| 990 | |
|---|
| 991 | |
|---|
| 992 | |
|---|
| 993 | |
|---|
| 994 | |
|---|
| 995 | |
|---|
| 996 | typedef std::set< osg::ref_ptr<Edge> > LocalEdgeList; |
|---|
| 997 | LocalEdgeList edges2UpdateErrorMetric; |
|---|
| 998 | TriangleSet::iterator titr; |
|---|
| 999 | |
|---|
| 1000 | |
|---|
| 1001 | |
|---|
| 1002 | for(titr = triangles.begin(); |
|---|
| 1003 | titr != triangles.end(); |
|---|
| 1004 | ++titr) |
|---|
| 1005 | { |
|---|
| 1006 | Triangle* tri = const_cast<Triangle*>(titr->get()); |
|---|
| 1007 | int edgeToReplace = 0; |
|---|
| 1008 | if (edge->_p1 == tri->_p1) |
|---|
| 1009 | { |
|---|
| 1010 | if (edge->_p2 == tri->_p2.get()) edgeToReplace = 1; |
|---|
| 1011 | else if (edge->_p2 == tri->_p3.get()) edgeToReplace = 3; |
|---|
| 1012 | } |
|---|
| 1013 | else if (edge->_p1 == tri->_p2.get()) |
|---|
| 1014 | { |
|---|
| 1015 | if (edge->_p2 == tri->_p3.get()) edgeToReplace = 2; |
|---|
| 1016 | else if (edge->_p2 == tri->_p1.get()) edgeToReplace = 1; |
|---|
| 1017 | } |
|---|
| 1018 | else if (edge->_p1 == tri->_p3.get()) |
|---|
| 1019 | { |
|---|
| 1020 | if (edge->_p2 == tri->_p1.get()) edgeToReplace = 3; |
|---|
| 1021 | else if (edge->_p2 == tri->_p2.get()) edgeToReplace = 2; |
|---|
| 1022 | } |
|---|
| 1023 | |
|---|
| 1024 | Triangle* newTri1 = 0; |
|---|
| 1025 | Triangle* newTri2 = 0; |
|---|
| 1026 | switch(edgeToReplace) |
|---|
| 1027 | { |
|---|
| 1028 | case(0): |
|---|
| 1029 | osg::notify(osg::NOTICE)<<"Error EdgeCollapse::divideEdge(Edge*,Point*) passed invalid edge."<<std::endl; |
|---|
| 1030 | return false; |
|---|
| 1031 | case(1): |
|---|
| 1032 | |
|---|
| 1033 | |
|---|
| 1034 | newTri1 = addTriangle(tri->_p1.get(), pNew, tri->_p3.get()); |
|---|
| 1035 | |
|---|
| 1036 | newTri2 = addTriangle(pNew, tri->_p2.get(), tri->_p3.get()); |
|---|
| 1037 | break; |
|---|
| 1038 | case(2): |
|---|
| 1039 | |
|---|
| 1040 | |
|---|
| 1041 | newTri1 = addTriangle(tri->_p1.get(), tri->_p2.get(), pNew); |
|---|
| 1042 | |
|---|
| 1043 | newTri2 = addTriangle(tri->_p1.get(), pNew, tri->_p3.get()); |
|---|
| 1044 | break; |
|---|
| 1045 | case(3): |
|---|
| 1046 | |
|---|
| 1047 | |
|---|
| 1048 | newTri1 = addTriangle(tri->_p1.get(), tri->_p2.get(), pNew); |
|---|
| 1049 | |
|---|
| 1050 | newTri2 = addTriangle(pNew, tri->_p2.get(), tri->_p3.get()); |
|---|
| 1051 | break; |
|---|
| 1052 | } |
|---|
| 1053 | |
|---|
| 1054 | if (newTri1) |
|---|
| 1055 | { |
|---|
| 1056 | edges2UpdateErrorMetric.insert(newTri1->_e1.get()); |
|---|
| 1057 | edges2UpdateErrorMetric.insert(newTri1->_e2.get()); |
|---|
| 1058 | edges2UpdateErrorMetric.insert(newTri1->_e3.get()); |
|---|
| 1059 | } |
|---|
| 1060 | if (newTri2) |
|---|
| 1061 | { |
|---|
| 1062 | edges2UpdateErrorMetric.insert(newTri2->_e1.get()); |
|---|
| 1063 | edges2UpdateErrorMetric.insert(newTri2->_e2.get()); |
|---|
| 1064 | edges2UpdateErrorMetric.insert(newTri2->_e3.get()); |
|---|
| 1065 | } |
|---|
| 1066 | } |
|---|
| 1067 | |
|---|
| 1068 | |
|---|
| 1069 | |
|---|
| 1070 | |
|---|
| 1071 | |
|---|
| 1072 | |
|---|
| 1073 | for(titr = triangles.begin(); |
|---|
| 1074 | titr != triangles.end(); |
|---|
| 1075 | ++titr) |
|---|
| 1076 | { |
|---|
| 1077 | removeTriangle(const_cast<Triangle*>(titr->get())); |
|---|
| 1078 | } |
|---|
| 1079 | |
|---|
| 1080 | for(LocalEdgeList::iterator itr=edges2UpdateErrorMetric.begin(); |
|---|
| 1081 | itr!=edges2UpdateErrorMetric.end(); |
|---|
| 1082 | ++itr) |
|---|
| 1083 | { |
|---|
| 1084 | |
|---|
| 1085 | if (itr->valid()) updateErrorMetricForEdge(const_cast<Edge*>(itr->get())); |
|---|
| 1086 | } |
|---|
| 1087 | |
|---|
| 1088 | |
|---|
| 1089 | |
|---|
| 1090 | |
|---|
| 1091 | |
|---|
| 1092 | |
|---|
| 1093 | |
|---|
| 1094 | |
|---|
| 1095 | |
|---|
| 1096 | |
|---|
| 1097 | return true; |
|---|
| 1098 | } |
|---|
| 1099 | |
|---|
| 1100 | unsigned int testEdge(Edge* edge) |
|---|
| 1101 | { |
|---|
| 1102 | unsigned int numErrors = 0; |
|---|
| 1103 | for(TriangleSet::iterator teitr=edge->_triangles.begin(); |
|---|
| 1104 | teitr!=edge->_triangles.end(); |
|---|
| 1105 | ++teitr) |
|---|
| 1106 | { |
|---|
| 1107 | Triangle* triangle = const_cast<Triangle*>(teitr->get()); |
|---|
| 1108 | if (!(triangle->_e1 == edge || triangle->_e2 == edge || triangle->_e3 == edge)) |
|---|
| 1109 | { |
|---|
| 1110 | osg::notify(osg::NOTICE)<<"testEdge("<<edge<<"). triangle != point back to this edge"<<std::endl; |
|---|
| 1111 | osg::notify(osg::NOTICE)<<" triangle->_e1=="<<triangle->_e1.get()<<std::endl; |
|---|
| 1112 | osg::notify(osg::NOTICE)<<" triangle->_e2=="<<triangle->_e2.get()<<std::endl; |
|---|
| 1113 | osg::notify(osg::NOTICE)<<" triangle->_e3=="<<triangle->_e3.get()<<std::endl; |
|---|
| 1114 | ++numErrors; |
|---|
| 1115 | } |
|---|
| 1116 | } |
|---|
| 1117 | |
|---|
| 1118 | if (edge->_triangles.empty()) |
|---|
| 1119 | { |
|---|
| 1120 | osg::notify(osg::NOTICE)<<"testEdge("<<edge<<")._triangles is empty"<<std::endl; |
|---|
| 1121 | ++numErrors; |
|---|
| 1122 | } |
|---|
| 1123 | return numErrors; |
|---|
| 1124 | } |
|---|
| 1125 | |
|---|
| 1126 | unsigned int testAllEdges() |
|---|
| 1127 | { |
|---|
| 1128 | unsigned int numErrors = 0; |
|---|
| 1129 | for(EdgeSet::iterator itr = _edgeSet.begin(); |
|---|
| 1130 | itr!=_edgeSet.end(); |
|---|
| 1131 | ++itr) |
|---|
| 1132 | { |
|---|
| 1133 | numErrors += testEdge(const_cast<Edge*>(itr->get())); |
|---|
| 1134 | } |
|---|
| 1135 | return numErrors; |
|---|
| 1136 | } |
|---|
| 1137 | |
|---|
| 1138 | unsigned int computeNumBoundaryEdges() |
|---|
| 1139 | { |
|---|
| 1140 | unsigned int numBoundaryEdges = 0; |
|---|
| 1141 | for(EdgeSet::iterator itr = _edgeSet.begin(); |
|---|
| 1142 | itr!=_edgeSet.end(); |
|---|
| 1143 | ++itr) |
|---|
| 1144 | { |
|---|
| 1145 | if ((*itr)->isBoundaryEdge()) ++numBoundaryEdges; |
|---|
| 1146 | } |
|---|
| 1147 | return numBoundaryEdges; |
|---|
| 1148 | } |
|---|
| 1149 | |
|---|
| 1150 | |
|---|
| 1151 | Point* addPoint(Triangle* triangle, unsigned int p1) |
|---|
| 1152 | { |
|---|
| 1153 | return addPoint(triangle,_originalPointList[p1].get()); |
|---|
| 1154 | } |
|---|
| 1155 | |
|---|
| 1156 | Point* addPoint(Triangle* triangle, Point* point) |
|---|
| 1157 | { |
|---|
| 1158 | |
|---|
| 1159 | PointSet::iterator itr = _pointSet.find(point); |
|---|
| 1160 | if (itr==_pointSet.end()) |
|---|
| 1161 | { |
|---|
| 1162 | |
|---|
| 1163 | _pointSet.insert(point); |
|---|
| 1164 | } |
|---|
| 1165 | else |
|---|
| 1166 | { |
|---|
| 1167 | point = const_cast<Point*>(itr->get()); |
|---|
| 1168 | |
|---|
| 1169 | } |
|---|
| 1170 | |
|---|
| 1171 | point->_triangles.insert(triangle); |
|---|
| 1172 | |
|---|
| 1173 | return point; |
|---|
| 1174 | } |
|---|
| 1175 | |
|---|
| 1176 | void removePoint(Triangle* triangle, Point* point) |
|---|
| 1177 | { |
|---|
| 1178 | PointSet::iterator itr = _pointSet.find(point); |
|---|
| 1179 | if (itr!=_pointSet.end()) |
|---|
| 1180 | { |
|---|
| 1181 | point->_triangles.erase(triangle); |
|---|
| 1182 | |
|---|
| 1183 | if (point->_triangles.empty()) |
|---|
| 1184 | { |
|---|
| 1185 | |
|---|
| 1186 | _pointSet.erase(itr); |
|---|
| 1187 | } |
|---|
| 1188 | } |
|---|
| 1189 | |
|---|
| 1190 | } |
|---|
| 1191 | |
|---|
| 1192 | unsigned int testPoint(Point* point) |
|---|
| 1193 | { |
|---|
| 1194 | unsigned int numErrors = 0; |
|---|
| 1195 | |
|---|
| 1196 | for(TriangleSet::iterator itr=point->_triangles.begin(); |
|---|
| 1197 | itr!=point->_triangles.end(); |
|---|
| 1198 | ++itr) |
|---|
| 1199 | { |
|---|
| 1200 | Triangle* triangle = const_cast<Triangle*>(itr->get()); |
|---|
| 1201 | if (!(triangle->_p1 == point || triangle->_p2 == point || triangle->_p3 == point)) |
|---|
| 1202 | { |
|---|
| 1203 | osg::notify(osg::NOTICE)<<"testPoint("<<point<<") error, triangle "<<triangle<<" does not point back to this point"<<std::endl; |
|---|
| 1204 | osg::notify(osg::NOTICE)<<" triangle->_p1 "<<triangle->_p1.get()<<std::endl; |
|---|
| 1205 | osg::notify(osg::NOTICE)<<" triangle->_p2 "<<triangle->_p2.get()<<std::endl; |
|---|
| 1206 | osg::notify(osg::NOTICE)<<" triangle->_p3 "<<triangle->_p3.get()<<std::endl; |
|---|
| 1207 | ++numErrors; |
|---|
| 1208 | } |
|---|
| 1209 | } |
|---|
| 1210 | |
|---|
| 1211 | return numErrors; |
|---|
| 1212 | } |
|---|
| 1213 | |
|---|
| 1214 | unsigned int testAllPoints() |
|---|
| 1215 | { |
|---|
| 1216 | unsigned int numErrors = 0; |
|---|
| 1217 | for(PointSet::iterator itr = _pointSet.begin(); |
|---|
| 1218 | itr!=_pointSet.end(); |
|---|
| 1219 | ++itr) |
|---|
| 1220 | { |
|---|
| 1221 | numErrors += testPoint(const_cast<Point*>(itr->get())); |
|---|
| 1222 | } |
|---|
| 1223 | return numErrors; |
|---|
| 1224 | } |
|---|
| 1225 | |
|---|
| 1226 | |
|---|
| 1227 | |
|---|
| 1228 | typedef std::vector< osg::ref_ptr<osg::Array> > ArrayList; |
|---|
| 1229 | |
|---|
| 1230 | osg::Geometry* _geometry; |
|---|
| 1231 | |
|---|
| 1232 | bool _computeErrorMetricUsingLength; |
|---|
| 1233 | EdgeSet _edgeSet; |
|---|
| 1234 | TriangleSet _triangleSet; |
|---|
| 1235 | PointSet _pointSet; |
|---|
| 1236 | PointList _originalPointList; |
|---|
| 1237 | |
|---|
| 1238 | }; |
|---|
| 1239 | |
|---|
| 1240 | struct CollectTriangleOperator |
|---|
| 1241 | { |
|---|
| 1242 | |
|---|
| 1243 | CollectTriangleOperator():_ec(0) {} |
|---|
| 1244 | |
|---|
| 1245 | void setEdgeCollapse(EdgeCollapse* ec) { _ec = ec; } |
|---|
| 1246 | |
|---|
| 1247 | EdgeCollapse* _ec; |
|---|
| 1248 | |
|---|
| 1249 | |
|---|
| 1250 | inline void operator()(unsigned int p1, unsigned int p2, unsigned int p3) |
|---|
| 1251 | { |
|---|
| 1252 | _ec->addTriangle(p1,p2,p3); |
|---|
| 1253 | } |
|---|
| 1254 | |
|---|
| 1255 | }; |
|---|
| 1256 | |
|---|
| 1257 | EdgeCollapse::~EdgeCollapse() |
|---|
| 1258 | { |
|---|
| 1259 | std::for_each(_edgeSet.begin(),_edgeSet.end(),dereference_clear()); |
|---|
| 1260 | |
|---|
| 1261 | std::for_each(_triangleSet.begin(),_triangleSet.end(),dereference_clear()); |
|---|
| 1262 | std::for_each(_pointSet.begin(),_pointSet.end(),dereference_clear()); |
|---|
| 1263 | std::for_each(_originalPointList.begin(),_originalPointList.end(),dereference_clear()); |
|---|
| 1264 | } |
|---|
| 1265 | |
|---|
| 1266 | |
|---|
| 1267 | typedef osg::TriangleIndexFunctor<CollectTriangleOperator> CollectTriangleIndexFunctor; |
|---|
| 1268 | |
|---|
| 1269 | class CopyArrayToPointsVisitor : public osg::ArrayVisitor |
|---|
| 1270 | { |
|---|
| 1271 | public: |
|---|
| 1272 | CopyArrayToPointsVisitor(EdgeCollapse::PointList& pointList): |
|---|
| 1273 | _pointList(pointList) {} |
|---|
| 1274 | |
|---|
| 1275 | template<class T> |
|---|
| 1276 | void copy(T& array) |
|---|
| 1277 | { |
|---|
| 1278 | if (_pointList.size()!=array.size()) return; |
|---|
| 1279 | |
|---|
| 1280 | for(unsigned int i=0;i<_pointList.size();++i) |
|---|
| 1281 | _pointList[i]->_attributes.push_back((float)array[i]); |
|---|
| 1282 | } |
|---|
| 1283 | |
|---|
| 1284 | virtual void apply(osg::Array&) {} |
|---|
| 1285 | virtual void apply(osg::ByteArray& array) { copy(array); } |
|---|
| 1286 | virtual void apply(osg::ShortArray& array) { copy(array); } |
|---|
| 1287 | virtual void apply(osg::IntArray& array) { copy(array); } |
|---|
| 1288 | virtual void apply(osg::UByteArray& array) { copy(array); } |
|---|
| 1289 | virtual void apply(osg::UShortArray& array) { copy(array); } |
|---|
| 1290 | virtual void apply(osg::UIntArray& array) { copy(array); } |
|---|
| 1291 | virtual void apply(osg::FloatArray& array) { copy(array); } |
|---|
| 1292 | |
|---|
| 1293 | virtual void apply(osg::Vec4ubArray& array) |
|---|
| 1294 | { |
|---|
| 1295 | if (_pointList.size()!=array.size()) return; |
|---|
| 1296 | |
|---|
| 1297 | for(unsigned int i=0;i<_pointList.size();++i) |
|---|
| 1298 | { |
|---|
| 1299 | osg::Vec4ub& value = array[i]; |
|---|
| 1300 | EdgeCollapse::FloatList& attributes = _pointList[i]->_attributes; |
|---|
| 1301 | attributes.push_back((float)value.r()); |
|---|
| 1302 | attributes.push_back((float)value.g()); |
|---|
| 1303 | attributes.push_back((float)value.b()); |
|---|
| 1304 | attributes.push_back((float)value.a()); |
|---|
| 1305 | } |
|---|
| 1306 | } |
|---|
| 1307 | |
|---|
| 1308 | virtual void apply(osg::Vec2Array& array) |
|---|
| 1309 | { |
|---|
| 1310 | if (_pointList.size()!=array.size()) return; |
|---|
| 1311 | |
|---|
| 1312 | for(unsigned int i=0;i<_pointList.size();++i) |
|---|
| 1313 | { |
|---|
| 1314 | osg::Vec2& value = array[i]; |
|---|
| 1315 | EdgeCollapse::FloatList& attributes = _pointList[i]->_attributes; |
|---|
| 1316 | attributes.push_back(value.x()); |
|---|
| 1317 | attributes.push_back(value.y()); |
|---|
| 1318 | } |
|---|
| 1319 | } |
|---|
| 1320 | |
|---|
| 1321 | virtual void apply(osg::Vec3Array& array) |
|---|
| 1322 | { |
|---|
| 1323 | if (_pointList.size()!=array.size()) return; |
|---|
| 1324 | |
|---|
| 1325 | for(unsigned int i=0;i<_pointList.size();++i) |
|---|
| 1326 | { |
|---|
| 1327 | osg::Vec3& value = array[i]; |
|---|
| 1328 | EdgeCollapse::FloatList& attributes = _pointList[i]->_attributes; |
|---|
| 1329 | attributes.push_back(value.x()); |
|---|
| 1330 | attributes.push_back(value.y()); |
|---|
| 1331 | attributes.push_back(value.z()); |
|---|
| 1332 | } |
|---|
| 1333 | } |
|---|
| 1334 | |
|---|
| 1335 | virtual void apply(osg::Vec4Array& array) |
|---|
| 1336 | { |
|---|
| 1337 | if (_pointList.size()!=array.size()) return; |
|---|
| 1338 | |
|---|
| 1339 | for(unsigned int i=0;i<_pointList.size();++i) |
|---|
| 1340 | { |
|---|
| 1341 | osg::Vec4& value = array[i]; |
|---|
| 1342 | EdgeCollapse::FloatList& attributes = _pointList[i]->_attributes; |
|---|
| 1343 | attributes.push_back(value.x()); |
|---|
| 1344 | attributes.push_back(value.y()); |
|---|
| 1345 | attributes.push_back(value.z()); |
|---|
| 1346 | attributes.push_back(value.w()); |
|---|
| 1347 | } |
|---|
| 1348 | } |
|---|
| 1349 | |
|---|
| 1350 | EdgeCollapse::PointList& _pointList; |
|---|
| 1351 | |
|---|
| 1352 | |
|---|
| 1353 | protected: |
|---|
| 1354 | |
|---|
| 1355 | CopyArrayToPointsVisitor& operator = (const CopyArrayToPointsVisitor&) { return *this; } |
|---|
| 1356 | }; |
|---|
| 1357 | |
|---|
| 1358 | class CopyVertexArrayToPointsVisitor : public osg::ArrayVisitor |
|---|
| 1359 | { |
|---|
| 1360 | public: |
|---|
| 1361 | CopyVertexArrayToPointsVisitor(EdgeCollapse::PointList& pointList): |
|---|
| 1362 | _pointList(pointList) {} |
|---|
| 1363 | |
|---|
| 1364 | virtual void apply(osg::Vec2Array& array) |
|---|
| 1365 | { |
|---|
| 1366 | if (_pointList.size()!=array.size()) return; |
|---|
| 1367 | |
|---|
| 1368 | for(unsigned int i=0;i<_pointList.size();++i) |
|---|
| 1369 | { |
|---|
| 1370 | _pointList[i] = new EdgeCollapse::Point; |
|---|
| 1371 | _pointList[i]->_index = i; |
|---|
| 1372 | |
|---|
| 1373 | osg::Vec2& value = array[i]; |
|---|
| 1374 | osg::Vec3& vertex = _pointList[i]->_vertex; |
|---|
| 1375 | vertex.set(value.x(),value.y(),0.0f); |
|---|
| 1376 | } |
|---|
| 1377 | } |
|---|
| 1378 | |
|---|
| 1379 | virtual void apply(osg::Vec3Array& array) |
|---|
| 1380 | { |
|---|
| 1381 | if (_pointList.size()!=array.size()) return; |
|---|
| 1382 | |
|---|
| 1383 | for(unsigned int i=0;i<_pointList.size();++i) |
|---|
| 1384 | { |
|---|
| 1385 | _pointList[i] = new EdgeCollapse::Point; |
|---|
| 1386 | _pointList[i]->_index = i; |
|---|
| 1387 | |
|---|
| 1388 | _pointList[i]->_vertex = array[i]; |
|---|
| 1389 | } |
|---|
| 1390 | } |
|---|
| 1391 | |
|---|
| 1392 | virtual void apply(osg::Vec4Array& array) |
|---|
| 1393 | { |
|---|
| 1394 | if (_pointList.size()!=array.size()) return; |
|---|
| 1395 | |
|---|
| 1396 | for(unsigned int i=0;i<_pointList.size();++i) |
|---|
| 1397 | { |
|---|
| 1398 | _pointList[i] = new EdgeCollapse::Point; |
|---|
| 1399 | _pointList[i]->_index = i; |
|---|
| 1400 | |
|---|
| 1401 | osg::Vec4& value = array[i]; |
|---|
| 1402 | osg::Vec3& vertex = _pointList[i]->_vertex; |
|---|
| 1403 | vertex.set(value.x()/value.w(),value.y()/value.w(),value.z()/value.w()); |
|---|
| 1404 | } |
|---|
| 1405 | } |
|---|
| 1406 | |
|---|
| 1407 | EdgeCollapse::PointList& _pointList; |
|---|
| 1408 | |
|---|
| 1409 | protected: |
|---|
| 1410 | |
|---|
| 1411 | CopyVertexArrayToPointsVisitor& operator = (const CopyVertexArrayToPointsVisitor&) { return *this; } |
|---|
| 1412 | |
|---|
| 1413 | }; |
|---|
| 1414 | |
|---|
| 1415 | void EdgeCollapse::setGeometry(osg::Geometry* geometry, const Simplifier::IndexList& protectedPoints) |
|---|
| 1416 | { |
|---|
| 1417 | _geometry = geometry; |
|---|
| 1418 | |
|---|
| 1419 | |
|---|
| 1420 | if (_geometry->suitableForOptimization()) |
|---|
| 1421 | { |
|---|
| 1422 | |
|---|
| 1423 | osg::notify(osg::INFO)<<"EdgeCollapse::setGeometry(..): Removing attribute indices"<<std::endl; |
|---|
| 1424 | _geometry->copyToAndOptimize(*_geometry); |
|---|
| 1425 | } |
|---|
| 1426 | |
|---|
| 1427 | |
|---|
| 1428 | if (_geometry->containsSharedArrays()) |
|---|
| 1429 | { |
|---|
| 1430 | |
|---|
| 1431 | osg::notify(osg::INFO)<<"EdgeCollapse::setGeometry(..): Duplicate shared arrays"<<std::endl; |
|---|
| 1432 | _geometry->duplicateSharedArrays(); |
|---|
| 1433 | } |
|---|
| 1434 | |
|---|
| 1435 | unsigned int numVertices = geometry->getVertexArray()->getNumElements(); |
|---|
| 1436 | |
|---|
| 1437 | _originalPointList.resize(numVertices); |
|---|
| 1438 | |
|---|
| 1439 | |
|---|
| 1440 | CopyVertexArrayToPointsVisitor copyVertexArrayToPoints(_originalPointList); |
|---|
| 1441 | _geometry->getVertexArray()->accept(copyVertexArrayToPoints); |
|---|
| 1442 | |
|---|
| 1443 | |
|---|
| 1444 | CopyArrayToPointsVisitor copyArrayToPoints(_originalPointList); |
|---|
| 1445 | |
|---|
| 1446 | for(unsigned int ti=0;ti<_geometry->getNumTexCoordArrays();++ti) |
|---|
| 1447 | { |
|---|
| 1448 | if (_geometry->getTexCoordArray(ti)) |
|---|
| 1449 | geometry->getTexCoordArray(ti)->accept(copyArrayToPoints); |
|---|
| 1450 | } |
|---|
| 1451 | |
|---|
| 1452 | if (_geometry->getNormalArray() && _geometry->getNormalBinding()==osg::Geometry::BIND_PER_VERTEX) |
|---|
| 1453 | geometry->getNormalArray()->accept(copyArrayToPoints); |
|---|
| 1454 | |
|---|
| 1455 | if (_geometry->getColorArray() && _geometry->getColorBinding()==osg::Geometry::BIND_PER_VERTEX) |
|---|
| 1456 | geometry->getColorArray()->accept(copyArrayToPoints); |
|---|
| 1457 | |
|---|
| 1458 | if (_geometry->getSecondaryColorArray() && _geometry->getSecondaryColorBinding()==osg::Geometry::BIND_PER_VERTEX) |
|---|
| 1459 | geometry->getSecondaryColorArray()->accept(copyArrayToPoints); |
|---|
| 1460 | |
|---|
| 1461 | if (_geometry->getFogCoordArray() && _geometry->getFogCoordBinding()==osg::Geometry::BIND_PER_VERTEX) |
|---|
| 1462 | geometry->getFogCoordArray()->accept(copyArrayToPoints); |
|---|
| 1463 | |
|---|
| 1464 | for(unsigned int vi=0;vi<_geometry->getNumVertexAttribArrays();++vi) |
|---|
| 1465 | { |
|---|
| 1466 | if (_geometry->getVertexAttribArray(vi) && _geometry->getVertexAttribBinding(vi)==osg::Geometry::BIND_PER_VERTEX) |
|---|
| 1467 | geometry->getVertexAttribArray(vi)->accept(copyArrayToPoints); |
|---|
| 1468 | } |
|---|
| 1469 | |
|---|
| 1470 | |
|---|
| 1471 | for(Simplifier::IndexList::const_iterator pitr=protectedPoints.begin(); |
|---|
| 1472 | pitr!=protectedPoints.end(); |
|---|
| 1473 | ++pitr) |
|---|
| 1474 | { |
|---|
| 1475 | _originalPointList[*pitr]->_protected = true; |
|---|
| 1476 | } |
|---|
| 1477 | |
|---|
| 1478 | |
|---|
| 1479 | CollectTriangleIndexFunctor collectTriangles; |
|---|
| 1480 | collectTriangles.setEdgeCollapse(this); |
|---|
| 1481 | |
|---|
| 1482 | _geometry->accept(collectTriangles); |
|---|
| 1483 | |
|---|
| 1484 | } |
|---|
| 1485 | |
|---|
| 1486 | |
|---|
| 1487 | |
|---|
| 1488 | class CopyPointsToArrayVisitor : public osg::ArrayVisitor |
|---|
| 1489 | { |
|---|
| 1490 | public: |
|---|
| 1491 | CopyPointsToArrayVisitor(EdgeCollapse::PointList& pointList): |
|---|
| 1492 | _pointList(pointList), |
|---|
| 1493 | _index(0) {} |
|---|
| 1494 | |
|---|
| 1495 | |
|---|
| 1496 | template<typename T,typename R> |
|---|
| 1497 | void copy(T& array, R ) |
|---|
| 1498 | { |
|---|
| 1499 | array.resize(_pointList.size()); |
|---|
| 1500 | |
|---|
| 1501 | for(unsigned int i=0;i<_pointList.size();++i) |
|---|
| 1502 | { |
|---|
| 1503 | if (_index<_pointList[i]->_attributes.size()) |
|---|
| 1504 | { |
|---|
| 1505 | float val = (_pointList[i]->_attributes[_index]); |
|---|
| 1506 | array[i] = R (val); |
|---|
| 1507 | } |
|---|
| 1508 | } |
|---|
| 1509 | |
|---|
| 1510 | ++_index; |
|---|
| 1511 | } |
|---|
| 1512 | |
|---|
| 1513 | |
|---|
| 1514 | typedef unsigned char dummy_uchar; |
|---|
| 1515 | typedef unsigned short dummy_ushort; |
|---|
| 1516 | typedef unsigned int dummy_uint; |
|---|
| 1517 | |
|---|
| 1518 | virtual void apply(osg::Array&) {} |
|---|
| 1519 | virtual void apply(osg::ByteArray& array) { copy(array, char());} |
|---|
| 1520 | virtual void apply(osg::ShortArray& array) { copy(array, short()); } |
|---|
| 1521 | virtual void apply(osg::IntArray& array) { copy(array, int()); } |
|---|
| 1522 | virtual void apply(osg::UByteArray& array) { copy(array, dummy_uchar()); } |
|---|
| 1523 | virtual void apply(osg::UShortArray& array) { copy(array,dummy_ushort()); } |
|---|
| 1524 | virtual void apply(osg::UIntArray& array) { copy(array, dummy_uint()); } |
|---|
| 1525 | virtual void apply(osg::FloatArray& array) { copy(array, float()); } |
|---|
| 1526 | |
|---|
| 1527 | virtual void apply(osg::Vec4ubArray& array) |
|---|
| 1528 | { |
|---|
| 1529 | array.resize(_pointList.size()); |
|---|
| 1530 | |
|---|
| 1531 | for(unsigned int i=0;i<_pointList.size();++i) |
|---|
| 1532 | { |
|---|
| 1533 | EdgeCollapse::FloatList& attributes = _pointList[i]->_attributes; |
|---|
| 1534 | array[i].set((unsigned char)attributes[_index], |
|---|
| 1535 | (unsigned char)attributes[_index+1], |
|---|
| 1536 | (unsigned char)attributes[_index+2], |
|---|
| 1537 | (unsigned char)attributes[_index+3]); |
|---|
| 1538 | } |
|---|
| 1539 | _index += 4; |
|---|
| 1540 | } |
|---|
| 1541 | |
|---|
| 1542 | virtual void apply(osg::Vec2Array& array) |
|---|
| 1543 | { |
|---|
| 1544 | array.resize(_pointList.size()); |
|---|
| 1545 | |
|---|
| 1546 | for(unsigned int i=0;i<_pointList.size();++i) |
|---|
| 1547 | { |
|---|
| 1548 | EdgeCollapse::FloatList& attributes = _pointList[i]->_attributes; |
|---|
| 1549 | if (_index+1<attributes.size()) array[i].set(attributes[_index],attributes[_index+1]); |
|---|
| 1550 | } |
|---|
| 1551 | _index += 2; |
|---|
| 1552 | } |
|---|
| 1553 | |
|---|
| 1554 | virtual void apply(osg::Vec3Array& array) |
|---|
| 1555 | { |
|---|
| 1556 | array.resize(_pointList.size()); |
|---|
| 1557 | |
|---|
| 1558 | for(unsigned int i=0;i<_pointList.size();++i) |
|---|
| 1559 | { |
|---|
| 1560 | EdgeCollapse::FloatList& attributes = _pointList[i]->_attributes; |
|---|
| 1561 | if (_index+2<attributes.size()) array[i].set(attributes[_index],attributes[_index+1],attributes[_index+2]); |
|---|
| 1562 | } |
|---|
| 1563 | _index += 3; |
|---|
| 1564 | } |
|---|
| 1565 | |
|---|
| 1566 | virtual void apply(osg::Vec4Array& array) |
|---|
| 1567 | { |
|---|
| 1568 | array.resize(_pointList.size()); |
|---|
| 1569 | |
|---|
| 1570 | for(unsigned int i=0;i<_pointList.size();++i) |
|---|
| 1571 | { |
|---|
| 1572 | EdgeCollapse::FloatList& attributes = _pointList[i]->_attributes; |
|---|
| 1573 | if (_index+3<attributes.size()) array[i].set(attributes[_index],attributes[_index+1],attributes[_index+2],attributes[_index+3]); |
|---|
| 1574 | } |
|---|
| 1575 | _index += 4; |
|---|
| 1576 | } |
|---|
| 1577 | |
|---|
| 1578 | EdgeCollapse::PointList& _pointList; |
|---|
| 1579 | unsigned int _index; |
|---|
| 1580 | |
|---|
| 1581 | protected: |
|---|
| 1582 | |
|---|
| 1583 | CopyPointsToArrayVisitor& operator = (CopyPointsToArrayVisitor&) { return *this; } |
|---|
| 1584 | }; |
|---|
| 1585 | |
|---|
| 1586 | class NormalizeArrayVisitor : public osg::ArrayVisitor |
|---|
| 1587 | { |
|---|
| 1588 | public: |
|---|
| 1589 | NormalizeArrayVisitor() {} |
|---|
| 1590 | |
|---|
| 1591 | template<typename Itr> |
|---|
| 1592 | void normalize(Itr begin, Itr end) |
|---|
| 1593 | { |
|---|
| 1594 | for(Itr itr = begin; |
|---|
| 1595 | itr != end; |
|---|
| 1596 | ++itr) |
|---|
| 1597 | { |
|---|
| 1598 | itr->normalize(); |
|---|
| 1599 | } |
|---|
| 1600 | } |
|---|
| 1601 | |
|---|
| 1602 | virtual void apply(osg::Vec2Array& array) { normalize(array.begin(),array.end()); } |
|---|
| 1603 | virtual void apply(osg::Vec3Array& array) { normalize(array.begin(),array.end()); } |
|---|
| 1604 | virtual void apply(osg::Vec4Array& array) { normalize(array.begin(),array.end()); } |
|---|
| 1605 | |
|---|
| 1606 | }; |
|---|
| 1607 | |
|---|
| 1608 | class CopyPointsToVertexArrayVisitor : public osg::ArrayVisitor |
|---|
| 1609 | { |
|---|
| 1610 | public: |
|---|
| 1611 | CopyPointsToVertexArrayVisitor(EdgeCollapse::PointList& pointList): |
|---|
| 1612 | _pointList(pointList) {} |
|---|
| 1613 | |
|---|
| 1614 | virtual void apply(osg::Vec2Array& array) |
|---|
| 1615 | { |
|---|
| 1616 | array.resize(_pointList.size()); |
|---|
| 1617 | |
|---|
| 1618 | for(unsigned int i=0;i<_pointList.size();++i) |
|---|
| 1619 | { |
|---|
| 1620 | _pointList[i]->_index = i; |
|---|
| 1621 | osg::Vec3& vertex = _pointList[i]->_vertex; |
|---|
| 1622 | array[i].set(vertex.x(),vertex.y()); |
|---|
| 1623 | } |
|---|
| 1624 | } |
|---|
| 1625 | |
|---|
| 1626 | virtual void apply(osg::Vec3Array& array) |
|---|
| 1627 | { |
|---|
| 1628 | array.resize(_pointList.size()); |
|---|
| 1629 | |
|---|
| 1630 | for(unsigned int i=0;i<_pointList.size();++i) |
|---|
| 1631 | { |
|---|
| 1632 | _pointList[i]->_index = i; |
|---|
| 1633 | array[i] = _pointList[i]->_vertex; |
|---|
| 1634 | } |
|---|
| 1635 | } |
|---|
| 1636 | |
|---|
| 1637 | virtual void apply(osg::Vec4Array& array) |
|---|
| 1638 | { |
|---|
| 1639 | array.resize(_pointList.size()); |
|---|
| 1640 | |
|---|
| 1641 | for(unsigned int i=0;i<_pointList.size();++i) |
|---|
| 1642 | { |
|---|
| 1643 | _pointList[i]->_index = i; |
|---|
| 1644 | osg::Vec3& vertex = _pointList[i]->_vertex; |
|---|
| 1645 | array[i].set(vertex.x(),vertex.y(),vertex.z(),1.0f); |
|---|
| 1646 | } |
|---|
| 1647 | } |
|---|
| 1648 | |
|---|
| 1649 | EdgeCollapse::PointList& _pointList; |
|---|
| 1650 | |
|---|
| 1651 | protected: |
|---|
| 1652 | |
|---|
| 1653 | CopyPointsToVertexArrayVisitor& operator = (const CopyPointsToVertexArrayVisitor&) { return *this; } |
|---|
| 1654 | }; |
|---|
| 1655 | |
|---|
| 1656 | |
|---|
| 1657 | void EdgeCollapse::copyBackToGeometry() |
|---|
| 1658 | { |
|---|
| 1659 | |
|---|
| 1660 | |
|---|
| 1661 | _originalPointList.clear(); |
|---|
| 1662 | std::copy(_pointSet.begin(),_pointSet.end(),std::back_inserter(_originalPointList)); |
|---|
| 1663 | |
|---|
| 1664 | |
|---|
| 1665 | CopyPointsToVertexArrayVisitor copyVertexArrayToPoints(_originalPointList); |
|---|
| 1666 | _geometry->getVertexArray()->accept(copyVertexArrayToPoints); |
|---|
| 1667 | |
|---|
| 1668 | |
|---|
| 1669 | CopyPointsToArrayVisitor copyArrayToPoints(_originalPointList); |
|---|
| 1670 | |
|---|
| 1671 | for(unsigned int ti=0;ti<_geometry->getNumTexCoordArrays();++ti) |
|---|
| 1672 | { |
|---|
| 1673 | if (_geometry->getTexCoordArray(ti)) |
|---|
| 1674 | _geometry->getTexCoordArray(ti)->accept(copyArrayToPoints); |
|---|
| 1675 | } |
|---|
| 1676 | |
|---|
| 1677 | if (_geometry->getNormalArray() && _geometry->getNormalBinding()==osg::Geometry::BIND_PER_VERTEX) |
|---|
| 1678 | { |
|---|
| 1679 | _geometry->getNormalArray()->accept(copyArrayToPoints); |
|---|
| 1680 | |
|---|
| 1681 | |
|---|
| 1682 | NormalizeArrayVisitor nav; |
|---|
| 1683 | _geometry->getNormalArray()->accept(nav); |
|---|
| 1684 | } |
|---|
| 1685 | |
|---|
| 1686 | if (_geometry->getColorArray() && _geometry->getColorBinding()==osg::Geometry::BIND_PER_VERTEX) |
|---|
| 1687 | _geometry->getColorArray()->accept(copyArrayToPoints); |
|---|
| 1688 | |
|---|
| 1689 | if (_geometry->getSecondaryColorArray() && _geometry->getSecondaryColorBinding()==osg::Geometry::BIND_PER_VERTEX) |
|---|
| 1690 | _geometry->getSecondaryColorArray()->accept(copyArrayToPoints); |
|---|
| 1691 | |
|---|
| 1692 | if (_geometry->getFogCoordArray() && _geometry->getFogCoordBinding()==osg::Geometry::BIND_PER_VERTEX) |
|---|
| 1693 | _geometry->getFogCoordArray()->accept(copyArrayToPoints); |
|---|
| 1694 | |
|---|
| 1695 | for(unsigned int vi=0;vi<_geometry->getNumVertexAttribArrays();++vi) |
|---|
| 1696 | { |
|---|
| 1697 | if (_geometry->getVertexAttribArray(vi) && _geometry->getVertexAttribBinding(vi)==osg::Geometry::BIND_PER_VERTEX) |
|---|
| 1698 | _geometry->getVertexAttribArray(vi)->accept(copyArrayToPoints); |
|---|
| 1699 | } |
|---|
| 1700 | |
|---|
| 1701 | typedef std::set< osg::ref_ptr<Triangle>, dereference_less > TrianglesSorted; |
|---|
| 1702 | TrianglesSorted trianglesSorted; |
|---|
| 1703 | for(TriangleSet::iterator itr = _triangleSet.begin(); |
|---|
| 1704 | itr != _triangleSet.end(); |
|---|
| 1705 | ++itr) |
|---|
| 1706 | { |
|---|
| 1707 | trianglesSorted.insert(*itr); |
|---|
| 1708 | } |
|---|
| 1709 | |
|---|
| 1710 | osg::DrawElementsUInt* primitives = new osg::DrawElementsUInt(GL_TRIANGLES,trianglesSorted.size()*3); |
|---|
| 1711 | unsigned int pos = 0; |
|---|
| 1712 | for(TrianglesSorted::iterator titr=trianglesSorted.begin(); |
|---|
| 1713 | titr!=trianglesSorted.end(); |
|---|
| 1714 | ++titr) |
|---|
| 1715 | { |
|---|
| 1716 | const Triangle* triangle = (*titr).get(); |
|---|
| 1717 | (*primitives)[pos++] = triangle->_p1->_index; |
|---|
| 1718 | (*primitives)[pos++] = triangle->_p2->_index; |
|---|
| 1719 | (*primitives)[pos++] = triangle->_p3->_index; |
|---|
| 1720 | } |
|---|
| 1721 | |
|---|
| 1722 | _geometry->getPrimitiveSetList().clear(); |
|---|
| 1723 | _geometry->addPrimitiveSet(primitives); |
|---|
| 1724 | |
|---|
| 1725 | } |
|---|
| 1726 | |
|---|
| 1727 | |
|---|
| 1728 | Simplifier::Simplifier(double sampleRatio, double maximumError, double maximumLength): |
|---|
| 1729 | osg::NodeVisitor(osg::NodeVisitor::TRAVERSE_ALL_CHILDREN), |
|---|
| 1730 | _sampleRatio(sampleRatio), |
|---|
| 1731 | _maximumError(maximumError), |
|---|
| 1732 | _maximumLength(maximumLength), |
|---|
| 1733 | _triStrip(true), |
|---|
| 1734 | _smoothing(true) |
|---|
| 1735 | |
|---|
| 1736 | { |
|---|
| 1737 | } |
|---|
| 1738 | |
|---|
| 1739 | void Simplifier::simplify(osg::Geometry& geometry) |
|---|
| 1740 | { |
|---|
| 1741 | |
|---|
| 1742 | |
|---|
| 1743 | |
|---|
| 1744 | IndexList emptyList; |
|---|
| 1745 | simplify(geometry,emptyList); |
|---|
| 1746 | } |
|---|
| 1747 | |
|---|
| 1748 | void Simplifier::simplify(osg::Geometry& geometry, const IndexList& protectedPoints) |
|---|
| 1749 | { |
|---|
| 1750 | osg::notify(osg::INFO)<<"++++++++++++++simplifier************"<<std::endl; |
|---|
| 1751 | |
|---|
| 1752 | EdgeCollapse ec; |
|---|
| 1753 | ec.setComputeErrorMetricUsingLength(getSampleRatio()>=1.0); |
|---|
| 1754 | ec.setGeometry(&geometry, protectedPoints); |
|---|
| 1755 | ec.updateErrorMetricForAllEdges(); |
|---|
| 1756 | |
|---|
| 1757 | unsigned int numOriginalPrimitives = ec._triangleSet.size(); |
|---|
| 1758 | |
|---|
| 1759 | if (getSampleRatio()<1.0) |
|---|
| 1760 | { |
|---|
| 1761 | while (!ec._edgeSet.empty() && |
|---|
| 1762 | continueSimplification((*ec._edgeSet.begin())->getErrorMetric() , numOriginalPrimitives, ec._triangleSet.size()) && |
|---|
| 1763 | ec.collapseMinimumErrorEdge()) |
|---|
| 1764 | { |
|---|
| 1765 | |
|---|
| 1766 | } |
|---|
| 1767 | |
|---|
| 1768 | osg::notify(osg::INFO)<<"******* AFTER EDGE COLLAPSE *********"<<ec._triangleSet.size()<<std::endl; |
|---|
| 1769 | } |
|---|
| 1770 | else |
|---|
| 1771 | { |
|---|
| 1772 | |
|---|
| 1773 | |
|---|
| 1774 | while (!ec._edgeSet.empty() && |
|---|
| 1775 | continueSimplification((*ec._edgeSet.rbegin())->getErrorMetric() , numOriginalPrimitives, ec._triangleSet.size()) && |
|---|
| 1776 | |
|---|
| 1777 | ec.divideLongestEdge()) |
|---|
| 1778 | { |
|---|
| 1779 | |
|---|
| 1780 | } |
|---|
| 1781 | osg::notify(osg::INFO)<<"******* AFTER EDGE DIVIDE *********"<<ec._triangleSet.size()<<std::endl; |
|---|
| 1782 | } |
|---|
| 1783 | |
|---|
| 1784 | osg::notify(osg::INFO)<<"Number of triangle errors after edge collapse= "<<ec.testAllTriangles()<<std::endl; |
|---|
| 1785 | osg::notify(osg::INFO)<<"Number of edge errors before edge collapse= "<<ec.testAllEdges()<<std::endl; |
|---|
| 1786 | osg::notify(osg::INFO)<<"Number of point errors after edge collapse= "<<ec.testAllPoints()<<std::endl; |
|---|
| 1787 | osg::notify(osg::INFO)<<"Number of triangles= "<<ec._triangleSet.size()<<std::endl; |
|---|
| 1788 | osg::notify(osg::INFO)<<"Number of points= "<<ec._pointSet.size()<<std::endl; |
|---|
| 1789 | osg::notify(osg::INFO)<<"Number of edges= "<<ec._edgeSet.size()<<std::endl; |
|---|
| 1790 | osg::notify(osg::INFO)<<"Number of boundary edges= "<<ec.computeNumBoundaryEdges()<<std::endl; |
|---|
| 1791 | |
|---|
| 1792 | if (!ec._edgeSet.empty()) |
|---|
| 1793 | { |
|---|
| 1794 | osg::notify(osg::INFO)<<std::endl<<"Simplifier, in = "<<numOriginalPrimitives<<"\tout = "<<ec._triangleSet.size()<<"\terror="<<(*ec._edgeSet.begin())->getErrorMetric()<<"\tvs "<<getMaximumError()<<std::endl<<std::endl; |
|---|
| 1795 | osg::notify(osg::INFO)<< " !ec._edgeSet.empty() = "<<!ec._edgeSet.empty()<<std::endl; |
|---|
| 1796 | osg::notify(osg::INFO)<< " continueSimplification(,,) = "<<continueSimplification((*ec._edgeSet.begin())->getErrorMetric() , numOriginalPrimitives, ec._triangleSet.size())<<std::endl; |
|---|
| 1797 | } |
|---|
| 1798 | |
|---|
| 1799 | ec.copyBackToGeometry(); |
|---|
| 1800 | |
|---|
| 1801 | if (_smoothing) |
|---|
| 1802 | { |
|---|
| 1803 | osgUtil::SmoothingVisitor::smooth(geometry); |
|---|
| 1804 | } |
|---|
| 1805 | |
|---|
| 1806 | if (_triStrip) |
|---|
| 1807 | { |
|---|
| 1808 | osgUtil::TriStripVisitor stripper; |
|---|
| 1809 | stripper.stripify(geometry); |
|---|
| 1810 | } |
|---|
| 1811 | |
|---|
| 1812 | } |
|---|