1/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield
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.
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.
14#ifndef OSGUTIL_EDGECOLLECTOR
15#define OSGUTIL_EDGECOLLECTOR 1
25#include <osg/Geometry>
28#include <osgUtil/Export>
33 struct dereference_less
35 template<class T, class U>
36 inline bool operator() (const T& lhs,const U& rhs) const
43 bool dereference_check_less(const T& lhs,const T& rhs)
45 if (lhs==rhs) return false;
46 if (!lhs) return true;
47 if (!rhs) return false;
51 struct dereference_clear
54 inline void operator() (const T& t)
56 T& non_const_t = const_cast<T&>(t);
62class OSGUTIL_EXPORT EdgeCollector
71 typedef std::list<osg::ref_ptr<osg::UIntArray> > IndexArrayList;
75 void setGeometry(osg::Geometry* geometry);
76 osg::Geometry* getGeometry() { return _geometry; }
78 unsigned int getNumOfTriangles() { return _triangleSet.size(); }
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;
89 struct OSGUTIL_EXPORT Point : public osg::Referenced
91 Point(): _protected(false), _index(0) {}
98 TriangleSet _triangles;
100 void clear() { _triangles.clear(); }
102 bool operator < ( const Point& rhs) const { return _vertex < rhs._vertex; }
104 bool isBoundaryPoint() const;
107 struct OSGUTIL_EXPORT Edge : public osg::Referenced
111 osg::ref_ptr<Point> _p1;
112 osg::ref_ptr<Point> _p2;
114 osg::ref_ptr<Point> _op1;
115 osg::ref_ptr<Point> _op2;
117 TriangleSet _triangles;
120 bool operator < ( const Edge& rhs) const;
122 bool operator == ( const Edge& rhs) const;
124 bool operator != ( const Edge& rhs) const;
126 void setOrderedPoints(Point* p1, Point* p2);
128 void addTriangle(Triangle* triangle) { _triangles.insert(triangle); }
130 bool isBoundaryEdge() const { return _triangles.size()<=1; }
132 bool isAdjacentToBoundary() const { return isBoundaryEdge() || _p1->isBoundaryPoint() || _p2->isBoundaryPoint(); }
134 bool endConnected(const Edge& rhs) const { return (_op2 == rhs._op1); }
136 bool beginConnected(const Edge& rhs) const { return (_op1 == rhs._op2); }
139 struct OSGUTIL_EXPORT Triangle : public osg::Referenced
145 bool operator < (const Triangle& rhs) const;
147 void setOrderedPoints(Point* p1, Point* p2, Point* p3);
149 float distance(const osg::Vec3& vertex) const { return _plane.distance(vertex); }
151 bool isBoundaryTriangle() const
152 { return (_e1->isBoundaryEdge() || _e2->isBoundaryEdge() || _e3->isBoundaryEdge()); }
155 osg::ref_ptr<Point> _p1;
156 osg::ref_ptr<Point> _p2;
157 osg::ref_ptr<Point> _p3;
159 osg::ref_ptr<Point> _op1;
160 osg::ref_ptr<Point> _op2;
161 osg::ref_ptr<Point> _op3;
163 osg::ref_ptr<Edge> _e1;
164 osg::ref_ptr<Edge> _e2;
165 osg::ref_ptr<Edge> _e3;
170 struct OSGUTIL_EXPORT Edgeloop : public osg::Referenced
172 typedef std::vector<osg::ref_ptr<Edge> > EdgeList;
174 bool isClosed() { return (_edgeList.back()->endConnected(*_edgeList.front().get())); }
176 osg::UIntArray * toIndexArray() const;
183 Triangle* addTriangle(unsigned int p1, unsigned int p2, unsigned int p3);
184 Triangle* addTriangle(Point* p1, Point* p2, Point* p3);
186 Edge* addEdge(Triangle* triangle, Point* p1, Point* p2);
188 Point* addPoint(Triangle* triangle, unsigned int p1) { return addPoint(triangle,_originalPointList[p1].get()); }
189 Point* addPoint(Triangle* triangle, Point* point);
191 void getBoundaryEdgeList(EdgeList & el);
192 bool extractBoundaryEdgeloop(EdgeList & el, Edgeloop & edgeloop);
193 bool extractBoundaryEdgeloopList(EdgeList & el, EdgeloopList & edgeloopList);
195 void getEdgeloopIndexList(IndexArrayList & ial);
199 osg::Geometry* _geometry;
202 TriangleSet _triangleSet;
204 PointList _originalPointList;
208} // end of osgUtil namespace
210#endif // ** OSGUTIL_EDGECOLLECTOR ** //