EdgeCollector 6.13 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210
/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield 
 *
 * This library is open source and may be redistributed and/or modified under  
 * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or 
 * (at your option) any later version.  The full license is in LICENSE file
 * included with this distribution, and on the openscenegraph.org website.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
 * OpenSceneGraph Public License for more details.
*/

#ifndef OSGUTIL_EDGECOLLECTOR
#define OSGUTIL_EDGECOLLECTOR 1


#include <set>
#include <map>
#include <list>
#include <vector>
#include <algorithm>

#include <osg/Array>
#include <osg/Geometry>
#include <osg/ref_ptr>

#include <osgUtil/Export>


namespace osgUtil {

    struct dereference_less
    {
        template<class T, class U>
        inline bool operator() (const T& lhs,const U& rhs) const
        {
            return *lhs < *rhs;
        }
    };

    template<class T>
    bool dereference_check_less(const T& lhs,const T& rhs)
    {
        if (lhs==rhs) return false;
        if (!lhs) return true;
        if (!rhs) return false;
        return *lhs < *rhs;
    }

    struct dereference_clear
    {
        template<class T>
        inline void operator() (const T& t)
        {
            T& non_const_t = const_cast<T&>(t);
            non_const_t->clear();
        }
    };


class OSGUTIL_EXPORT EdgeCollector
{
public:
    
    struct Triangle;
    struct Edge;
    struct Edgeloop;
    struct Point;

    typedef std::list<osg::ref_ptr<osg::UIntArray> > IndexArrayList;
    
    ~EdgeCollector();

    void setGeometry(osg::Geometry* geometry);
    osg::Geometry* getGeometry() { return _geometry; }

    unsigned int getNumOfTriangles() { return _triangleSet.size(); }

    typedef std::set<osg::ref_ptr<Edge>,dereference_less >                      EdgeSet;
    typedef std::vector<osg::ref_ptr<Edge> >                                    EdgeList;
    typedef std::list< osg::ref_ptr<Edgeloop> >                                 EdgeloopList;
    typedef std::set< osg::ref_ptr<Point>,dereference_less >                    PointSet;
    typedef std::vector< osg::ref_ptr<Point> >                                  PointList;
    typedef std::list< osg::ref_ptr<Triangle> >                                 TriangleList;
    typedef std::set< osg::ref_ptr<Triangle> >                                  TriangleSet;
    typedef std::map< osg::ref_ptr<Triangle>, unsigned int, dereference_less >  TriangleMap;

    struct OSGUTIL_EXPORT Point : public osg::Referenced
    {
        Point(): _protected(false), _index(0) {}
        
        bool _protected;

        unsigned int _index;

        osg::Vec3d          _vertex;
        TriangleSet         _triangles;

        void clear() { _triangles.clear(); }

        bool operator < ( const Point& rhs) const { return _vertex < rhs._vertex; }
        
        bool isBoundaryPoint() const;
    };

    struct OSGUTIL_EXPORT Edge : public osg::Referenced
    {
        void clear();

        osg::ref_ptr<Point> _p1;
        osg::ref_ptr<Point> _p2;
        
        osg::ref_ptr<Point> _op1;
        osg::ref_ptr<Point> _op2;
        
        TriangleSet _triangles;

        
        bool operator < ( const Edge& rhs) const;
        
        bool operator == ( const Edge& rhs) const;

        bool operator != ( const Edge& rhs) const;
        
        void setOrderedPoints(Point* p1, Point* p2);
        
        void addTriangle(Triangle* triangle) { _triangles.insert(triangle); }
        
        bool isBoundaryEdge() const { return _triangles.size()<=1; }
        
        bool isAdjacentToBoundary() const { return isBoundaryEdge() || _p1->isBoundaryPoint() || _p2->isBoundaryPoint(); }
        
        bool endConnected(const Edge& rhs) const { return (_op2 == rhs._op1); }
        
        bool beginConnected(const Edge& rhs) const { return (_op1 == rhs._op2); }
    };

    struct OSGUTIL_EXPORT Triangle : public osg::Referenced
    {
        Triangle() {}
        
        void clear();

        bool operator < (const Triangle& rhs) const;

        void setOrderedPoints(Point* p1, Point* p2, Point* p3);
        
        float distance(const osg::Vec3& vertex) const { return _plane.distance(vertex); }
        
        bool isBoundaryTriangle() const
        { return (_e1->isBoundaryEdge() || _e2->isBoundaryEdge() ||  _e3->isBoundaryEdge()); }

       
        osg::ref_ptr<Point> _p1;
        osg::ref_ptr<Point> _p2;
        osg::ref_ptr<Point> _p3;
        
        osg::ref_ptr<Point> _op1;
        osg::ref_ptr<Point> _op2;
        osg::ref_ptr<Point> _op3;
        
        osg::ref_ptr<Edge> _e1;
        osg::ref_ptr<Edge> _e2;
        osg::ref_ptr<Edge> _e3;
        
        osg::Plane _plane;        
    };

    struct OSGUTIL_EXPORT Edgeloop : public osg::Referenced
    {
        typedef std::vector<osg::ref_ptr<Edge> > EdgeList;
        
        bool isClosed() { return (_edgeList.back()->endConnected(*_edgeList.front().get())); }
        
        osg::UIntArray * toIndexArray() const;
        
        EdgeList _edgeList;
    };


    
    Triangle* addTriangle(unsigned int p1, unsigned int p2, unsigned int p3);
    Triangle* addTriangle(Point* p1, Point* p2, Point* p3);

    Edge* addEdge(Triangle* triangle, Point* p1, Point* p2);
    
    Point* addPoint(Triangle* triangle, unsigned int p1) { return addPoint(triangle,_originalPointList[p1].get()); }
    Point* addPoint(Triangle* triangle, Point* point);
    
    void getBoundaryEdgeList(EdgeList & el);
    bool extractBoundaryEdgeloop(EdgeList & el, Edgeloop & edgeloop);
    bool extractBoundaryEdgeloopList(EdgeList & el, EdgeloopList & edgeloopList);
    
    void getEdgeloopIndexList(IndexArrayList & ial);
    
//protected:
    
    osg::Geometry*                  _geometry;
    
    EdgeSet                         _edgeSet;
    TriangleSet                     _triangleSet;
    PointSet                        _pointSet;
    PointList                       _originalPointList;
    
};

} // end of osgUtil namespace

#endif // ** OSGUTIL_EDGECOLLECTOR ** //