| 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 | |
|---|
| 14 | #ifndef OSGUTIL_EDGECOLLECTOR |
|---|
| 15 | #define OSGUTIL_EDGECOLLECTOR 1 |
|---|
| 16 | |
|---|
| 17 | |
|---|
| 18 | #include <set> |
|---|
| 19 | #include <map> |
|---|
| 20 | #include <list> |
|---|
| 21 | #include <vector> |
|---|
| 22 | #include <algorithm> |
|---|
| 23 | |
|---|
| 24 | #include <osg/Array> |
|---|
| 25 | #include <osg/Geometry> |
|---|
| 26 | #include <osg/ref_ptr> |
|---|
| 27 | |
|---|
| 28 | #include <osgUtil/Export> |
|---|
| 29 | |
|---|
| 30 | |
|---|
| 31 | namespace osgUtil { |
|---|
| 32 | |
|---|
| 33 | struct dereference_less |
|---|
| 34 | { |
|---|
| 35 | template<class T, class U> |
|---|
| 36 | inline bool operator() (const T& lhs,const U& rhs) const |
|---|
| 37 | { |
|---|
| 38 | return *lhs < *rhs; |
|---|
| 39 | } |
|---|
| 40 | }; |
|---|
| 41 | |
|---|
| 42 | template<class T> |
|---|
| 43 | bool dereference_check_less(const T& lhs,const T& rhs) |
|---|
| 44 | { |
|---|
| 45 | if (lhs==rhs) return false; |
|---|
| 46 | if (!lhs) return true; |
|---|
| 47 | if (!rhs) return false; |
|---|
| 48 | return *lhs < *rhs; |
|---|
| 49 | } |
|---|
| 50 | |
|---|
| 51 | struct dereference_clear |
|---|
| 52 | { |
|---|
| 53 | template<class T> |
|---|
| 54 | inline void operator() (const T& t) |
|---|
| 55 | { |
|---|
| 56 | T& non_const_t = const_cast<T&>(t); |
|---|
| 57 | non_const_t->clear(); |
|---|
| 58 | } |
|---|
| 59 | }; |
|---|
| 60 | |
|---|
| 61 | |
|---|
| 62 | class OSGUTIL_EXPORT EdgeCollector |
|---|
| 63 | { |
|---|
| 64 | public: |
|---|
| 65 | |
|---|
| 66 | struct Triangle; |
|---|
| 67 | struct Edge; |
|---|
| 68 | struct Edgeloop; |
|---|
| 69 | struct Point; |
|---|
| 70 | |
|---|
| 71 | typedef std::list<osg::ref_ptr<osg::UIntArray> > IndexArrayList; |
|---|
| 72 | |
|---|
| 73 | ~EdgeCollector(); |
|---|
| 74 | |
|---|
| 75 | void setGeometry(osg::Geometry* geometry); |
|---|
| 76 | osg::Geometry* getGeometry() { return _geometry; } |
|---|
| 77 | |
|---|
| 78 | unsigned int getNumOfTriangles() { return _triangleSet.size(); } |
|---|
| 79 | |
|---|
| 80 | typedef std::set<osg::ref_ptr<Edge>,dereference_less > EdgeSet; |
|---|
| 81 | typedef std::vector<osg::ref_ptr<Edge> > EdgeList; |
|---|
| 82 | typedef std::list< osg::ref_ptr<Edgeloop> > EdgeloopList; |
|---|
| 83 | typedef std::set< osg::ref_ptr<Point>,dereference_less > PointSet; |
|---|
| 84 | typedef std::vector< osg::ref_ptr<Point> > PointList; |
|---|
| 85 | typedef std::list< osg::ref_ptr<Triangle> > TriangleList; |
|---|
| 86 | typedef std::set< osg::ref_ptr<Triangle> > TriangleSet; |
|---|
| 87 | typedef std::map< osg::ref_ptr<Triangle>, unsigned int, dereference_less > TriangleMap; |
|---|
| 88 | |
|---|
| 89 | struct OSGUTIL_EXPORT Point : public osg::Referenced |
|---|
| 90 | { |
|---|
| 91 | Point(): _protected(false), _index(0) {} |
|---|
| 92 | |
|---|
| 93 | bool _protected; |
|---|
| 94 | |
|---|
| 95 | unsigned int _index; |
|---|
| 96 | |
|---|
| 97 | osg::Vec3d _vertex; |
|---|
| 98 | TriangleSet _triangles; |
|---|
| 99 | |
|---|
| 100 | void clear() { _triangles.clear(); } |
|---|
| 101 | |
|---|
| 102 | bool operator < ( const Point& rhs) const { return _vertex < rhs._vertex; } |
|---|
| 103 | |
|---|
| 104 | bool isBoundaryPoint() const; |
|---|
| 105 | }; |
|---|
| 106 | |
|---|
| 107 | struct OSGUTIL_EXPORT Edge : public osg::Referenced |
|---|
| 108 | { |
|---|
| 109 | void clear(); |
|---|
| 110 | |
|---|
| 111 | osg::ref_ptr<Point> _p1; |
|---|
| 112 | osg::ref_ptr<Point> _p2; |
|---|
| 113 | |
|---|
| 114 | osg::ref_ptr<Point> _op1; |
|---|
| 115 | osg::ref_ptr<Point> _op2; |
|---|
| 116 | |
|---|
| 117 | TriangleSet _triangles; |
|---|
| 118 | |
|---|
| 119 | |
|---|
| 120 | bool operator < ( const Edge& rhs) const; |
|---|
| 121 | |
|---|
| 122 | bool operator == ( const Edge& rhs) const; |
|---|
| 123 | |
|---|
| 124 | bool operator != ( const Edge& rhs) const; |
|---|
| 125 | |
|---|
| 126 | void setOrderedPoints(Point* p1, Point* p2); |
|---|
| 127 | |
|---|
| 128 | void addTriangle(Triangle* triangle) { _triangles.insert(triangle); } |
|---|
| 129 | |
|---|
| 130 | bool isBoundaryEdge() const { return _triangles.size()<=1; } |
|---|
| 131 | |
|---|
| 132 | bool isAdjacentToBoundary() const { return isBoundaryEdge() || _p1->isBoundaryPoint() || _p2->isBoundaryPoint(); } |
|---|
| 133 | |
|---|
| 134 | bool endConnected(const Edge& rhs) const { return (_op2 == rhs._op1); } |
|---|
| 135 | |
|---|
| 136 | bool beginConnected(const Edge& rhs) const { return (_op1 == rhs._op2); } |
|---|
| 137 | }; |
|---|
| 138 | |
|---|
| 139 | struct OSGUTIL_EXPORT Triangle : public osg::Referenced |
|---|
| 140 | { |
|---|
| 141 | Triangle() {} |
|---|
| 142 | |
|---|
| 143 | void clear(); |
|---|
| 144 | |
|---|
| 145 | bool operator < (const Triangle& rhs) const; |
|---|
| 146 | |
|---|
| 147 | void setOrderedPoints(Point* p1, Point* p2, Point* p3); |
|---|
| 148 | |
|---|
| 149 | float distance(const osg::Vec3& vertex) const { return _plane.distance(vertex); } |
|---|
| 150 | |
|---|
| 151 | bool isBoundaryTriangle() const |
|---|
| 152 | { return (_e1->isBoundaryEdge() || _e2->isBoundaryEdge() || _e3->isBoundaryEdge()); } |
|---|
| 153 | |
|---|
| 154 | |
|---|
| 155 | osg::ref_ptr<Point> _p1; |
|---|
| 156 | osg::ref_ptr<Point> _p2; |
|---|
| 157 | osg::ref_ptr<Point> _p3; |
|---|
| 158 | |
|---|
| 159 | osg::ref_ptr<Point> _op1; |
|---|
| 160 | osg::ref_ptr<Point> _op2; |
|---|
| 161 | osg::ref_ptr<Point> _op3; |
|---|
| 162 | |
|---|
| 163 | osg::ref_ptr<Edge> _e1; |
|---|
| 164 | osg::ref_ptr<Edge> _e2; |
|---|
| 165 | osg::ref_ptr<Edge> _e3; |
|---|
| 166 | |
|---|
| 167 | osg::Plane _plane; |
|---|
| 168 | }; |
|---|
| 169 | |
|---|
| 170 | struct OSGUTIL_EXPORT Edgeloop : public osg::Referenced |
|---|
| 171 | { |
|---|
| 172 | typedef std::vector<osg::ref_ptr<Edge> > EdgeList; |
|---|
| 173 | |
|---|
| 174 | bool isClosed() { return (_edgeList.back()->endConnected(*_edgeList.front().get())); } |
|---|
| 175 | |
|---|
| 176 | osg::UIntArray * toIndexArray() const; |
|---|
| 177 | |
|---|
| 178 | EdgeList _edgeList; |
|---|
| 179 | }; |
|---|
| 180 | |
|---|
| 181 | |
|---|
| 182 | |
|---|
| 183 | Triangle* addTriangle(unsigned int p1, unsigned int p2, unsigned int p3); |
|---|
| 184 | Triangle* addTriangle(Point* p1, Point* p2, Point* p3); |
|---|
| 185 | |
|---|
| 186 | Edge* addEdge(Triangle* triangle, Point* p1, Point* p2); |
|---|
| 187 | |
|---|
| 188 | Point* addPoint(Triangle* triangle, unsigned int p1) { return addPoint(triangle,_originalPointList[p1].get()); } |
|---|
| 189 | Point* addPoint(Triangle* triangle, Point* point); |
|---|
| 190 | |
|---|
| 191 | void getBoundaryEdgeList(EdgeList & el); |
|---|
| 192 | bool extractBoundaryEdgeloop(EdgeList & el, Edgeloop & edgeloop); |
|---|
| 193 | bool extractBoundaryEdgeloopList(EdgeList & el, EdgeloopList & edgeloopList); |
|---|
| 194 | |
|---|
| 195 | void getEdgeloopIndexList(IndexArrayList & ial); |
|---|
| 196 | |
|---|
| 197 | //protected: |
|---|
| 198 | |
|---|
| 199 | osg::Geometry* _geometry; |
|---|
| 200 | |
|---|
| 201 | EdgeSet _edgeSet; |
|---|
| 202 | TriangleSet _triangleSet; |
|---|
| 203 | PointSet _pointSet; |
|---|
| 204 | PointList _originalPointList; |
|---|
| 205 | |
|---|
| 206 | }; |
|---|
| 207 | |
|---|
| 208 | } // end of osgUtil namespace |
|---|
| 209 | |
|---|
| 210 | #endif // ** OSGUTIL_EDGECOLLECTOR ** // |
|---|