EdgeCollector 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. /* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield
  2. *
  3. * This library is open source and may be redistributed and/or modified under
  4. * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
  5. * (at your option) any later version. The full license is in LICENSE file
  6. * included with this distribution, and on the openscenegraph.org website.
  7. *
  8. * This library is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * OpenSceneGraph Public License for more details.
  12. */
  13. #ifndef OSGUTIL_EDGECOLLECTOR
  14. #define OSGUTIL_EDGECOLLECTOR 1
  15. #include <set>
  16. #include <map>
  17. #include <list>
  18. #include <vector>
  19. #include <algorithm>
  20. #include <osg/Array>
  21. #include <osg/Geometry>
  22. #include <osg/ref_ptr>
  23. #include <osgUtil/Export>
  24. namespace osgUtil {
  25. struct dereference_less
  26. {
  27. template<class T, class U>
  28. inline bool operator() (const T& lhs,const U& rhs) const
  29. {
  30. return *lhs < *rhs;
  31. }
  32. };
  33. template<class T>
  34. bool dereference_check_less(const T& lhs,const T& rhs)
  35. {
  36. if (lhs==rhs) return false;
  37. if (!lhs) return true;
  38. if (!rhs) return false;
  39. return *lhs < *rhs;
  40. }
  41. struct dereference_clear
  42. {
  43. template<class T>
  44. inline void operator() (const T& t)
  45. {
  46. T& non_const_t = const_cast<T&>(t);
  47. non_const_t->clear();
  48. }
  49. };
  50. class OSGUTIL_EXPORT EdgeCollector
  51. {
  52. public:
  53. struct Triangle;
  54. struct Edge;
  55. struct Edgeloop;
  56. struct Point;
  57. typedef std::list<osg::ref_ptr<osg::UIntArray> > IndexArrayList;
  58. ~EdgeCollector();
  59. void setGeometry(osg::Geometry* geometry);
  60. osg::Geometry* getGeometry() { return _geometry; }
  61. unsigned int getNumOfTriangles() { return _triangleSet.size(); }
  62. typedef std::set<osg::ref_ptr<Edge>,dereference_less > EdgeSet;
  63. typedef std::vector<osg::ref_ptr<Edge> > EdgeList;
  64. typedef std::list< osg::ref_ptr<Edgeloop> > EdgeloopList;
  65. typedef std::set< osg::ref_ptr<Point>,dereference_less > PointSet;
  66. typedef std::vector< osg::ref_ptr<Point> > PointList;
  67. typedef std::list< osg::ref_ptr<Triangle> > TriangleList;
  68. typedef std::set< osg::ref_ptr<Triangle> > TriangleSet;
  69. typedef std::map< osg::ref_ptr<Triangle>, unsigned int, dereference_less > TriangleMap;
  70. struct OSGUTIL_EXPORT Point : public osg::Referenced
  71. {
  72. Point(): _protected(false), _index(0) {}
  73. bool _protected;
  74. unsigned int _index;
  75. osg::Vec3d _vertex;
  76. TriangleSet _triangles;
  77. void clear() { _triangles.clear(); }
  78. bool operator < ( const Point& rhs) const { return _vertex < rhs._vertex; }
  79. bool isBoundaryPoint() const;
  80. };
  81. struct OSGUTIL_EXPORT Edge : public osg::Referenced
  82. {
  83. void clear();
  84. osg::ref_ptr<Point> _p1;
  85. osg::ref_ptr<Point> _p2;
  86. osg::ref_ptr<Point> _op1;
  87. osg::ref_ptr<Point> _op2;
  88. TriangleSet _triangles;
  89. bool operator < ( const Edge& rhs) const;
  90. bool operator == ( const Edge& rhs) const;
  91. bool operator != ( const Edge& rhs) const;
  92. void setOrderedPoints(Point* p1, Point* p2);
  93. void addTriangle(Triangle* triangle) { _triangles.insert(triangle); }
  94. bool isBoundaryEdge() const { return _triangles.size()<=1; }
  95. bool isAdjacentToBoundary() const { return isBoundaryEdge() || _p1->isBoundaryPoint() || _p2->isBoundaryPoint(); }
  96. bool endConnected(const Edge& rhs) const { return (_op2 == rhs._op1); }
  97. bool beginConnected(const Edge& rhs) const { return (_op1 == rhs._op2); }
  98. };
  99. struct OSGUTIL_EXPORT Triangle : public osg::Referenced
  100. {
  101. Triangle() {}
  102. void clear();
  103. bool operator < (const Triangle& rhs) const;
  104. void setOrderedPoints(Point* p1, Point* p2, Point* p3);
  105. float distance(const osg::Vec3& vertex) const { return _plane.distance(vertex); }
  106. bool isBoundaryTriangle() const
  107. { return (_e1->isBoundaryEdge() || _e2->isBoundaryEdge() || _e3->isBoundaryEdge()); }
  108. osg::ref_ptr<Point> _p1;
  109. osg::ref_ptr<Point> _p2;
  110. osg::ref_ptr<Point> _p3;
  111. osg::ref_ptr<Point> _op1;
  112. osg::ref_ptr<Point> _op2;
  113. osg::ref_ptr<Point> _op3;
  114. osg::ref_ptr<Edge> _e1;
  115. osg::ref_ptr<Edge> _e2;
  116. osg::ref_ptr<Edge> _e3;
  117. osg::Plane _plane;
  118. };
  119. struct OSGUTIL_EXPORT Edgeloop : public osg::Referenced
  120. {
  121. typedef std::vector<osg::ref_ptr<Edge> > EdgeList;
  122. bool isClosed() { return (_edgeList.back()->endConnected(*_edgeList.front().get())); }
  123. osg::UIntArray * toIndexArray() const;
  124. EdgeList _edgeList;
  125. };
  126. Triangle* addTriangle(unsigned int p1, unsigned int p2, unsigned int p3);
  127. Triangle* addTriangle(Point* p1, Point* p2, Point* p3);
  128. Edge* addEdge(Triangle* triangle, Point* p1, Point* p2);
  129. Point* addPoint(Triangle* triangle, unsigned int p1) { return addPoint(triangle,_originalPointList[p1].get()); }
  130. Point* addPoint(Triangle* triangle, Point* point);
  131. void getBoundaryEdgeList(EdgeList & el);
  132. bool extractBoundaryEdgeloop(EdgeList & el, Edgeloop & edgeloop);
  133. bool extractBoundaryEdgeloopList(EdgeList & el, EdgeloopList & edgeloopList);
  134. void getEdgeloopIndexList(IndexArrayList & ial);
  135. //protected:
  136. osg::Geometry* _geometry;
  137. EdgeSet _edgeSet;
  138. TriangleSet _triangleSet;
  139. PointSet _pointSet;
  140. PointList _originalPointList;
  141. };
  142. } // end of osgUtil namespace
  143. #endif // ** OSGUTIL_EDGECOLLECTOR ** //