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_SIMPLIFIER |
---|
15 | #define OSGUTIL_SIMPLIFIER 1 |
---|
16 | |
---|
17 | #include <osg/NodeVisitor> |
---|
18 | #include <osg/Geode> |
---|
19 | #include <osg/Geometry> |
---|
20 | |
---|
21 | #include <osgUtil/Export> |
---|
22 | |
---|
23 | namespace osgUtil { |
---|
24 | |
---|
25 | /** A simplifier for reducing the number of traingles in osg::Geometry. |
---|
26 | */ |
---|
27 | class OSGUTIL_EXPORT Simplifier : public osg::NodeVisitor |
---|
28 | { |
---|
29 | public: |
---|
30 | |
---|
31 | Simplifier(double sampleRatio=1.0, double maximumError=FLT_MAX, double maximumLength=0.0); |
---|
32 | |
---|
33 | META_NodeVisitor("osgUtil","Simplifier") |
---|
34 | |
---|
35 | void setSampleRatio(float sampleRatio) { _sampleRatio = sampleRatio; } |
---|
36 | float getSampleRatio() const { return _sampleRatio; } |
---|
37 | |
---|
38 | /** Set the maximum point error that all point removals must be less than to permit removal of a point. |
---|
39 | * Note, Only used when down sampling. i.e. sampleRatio < 1.0*/ |
---|
40 | void setMaximumError(float error) { _maximumError = error; } |
---|
41 | float getMaximumError() const { return _maximumError; } |
---|
42 | |
---|
43 | /** Set the maximum length target that all edges must be shorted than. |
---|
44 | * Note, Only used when up sampling i.e. sampleRatio > 1.0.*/ |
---|
45 | void setMaximumLength(float length) { _maximumLength = length; } |
---|
46 | float getMaximumLength() const { return _maximumLength; } |
---|
47 | |
---|
48 | void setDoTriStrip(bool on) { _triStrip = on; } |
---|
49 | bool getDoTriStrip() const { return _triStrip; } |
---|
50 | |
---|
51 | void setSmoothing(bool on) { _smoothing = on; } |
---|
52 | bool getSmoothing() const { return _smoothing; } |
---|
53 | |
---|
54 | class ContinueSimplificationCallback : public osg::Referenced |
---|
55 | { |
---|
56 | public: |
---|
57 | /** return true if mesh should be continued to be simplified, return false to stop simplification.*/ |
---|
58 | virtual bool continueSimplification(const Simplifier& simplifier, float nextError, unsigned int numOriginalPrimitives, unsigned int numRemainingPrimitives) const |
---|
59 | { |
---|
60 | return simplifier.continueSimplificationImplementation(nextError, numOriginalPrimitives, numRemainingPrimitives); |
---|
61 | } |
---|
62 | |
---|
63 | protected: |
---|
64 | virtual ~ContinueSimplificationCallback() {} |
---|
65 | }; |
---|
66 | |
---|
67 | void setContinueSimplificationCallback(ContinueSimplificationCallback* cb) { _continueSimplificationCallback = cb; } |
---|
68 | ContinueSimplificationCallback* getContinueSimplificationCallback() { return _continueSimplificationCallback.get(); } |
---|
69 | const ContinueSimplificationCallback* getContinueSimplificationCallback() const { return _continueSimplificationCallback.get(); } |
---|
70 | |
---|
71 | |
---|
72 | bool continueSimplification(float nextError, unsigned int numOriginalPrimitives, unsigned int numRemainingPrimitives) const |
---|
73 | { |
---|
74 | if (_continueSimplificationCallback.valid()) return _continueSimplificationCallback->continueSimplification(*this, nextError, numOriginalPrimitives, numRemainingPrimitives); |
---|
75 | else return continueSimplificationImplementation(nextError, numOriginalPrimitives, numRemainingPrimitives); |
---|
76 | } |
---|
77 | |
---|
78 | virtual bool continueSimplificationImplementation(float nextError, unsigned int numOriginalPrimitives, unsigned int numRemainingPrimitives) const |
---|
79 | { |
---|
80 | if (getSampleRatio()<1.0) return ((float)numRemainingPrimitives > ((float)numOriginalPrimitives) * getSampleRatio()) && nextError<=getMaximumError(); |
---|
81 | else return ((float)numRemainingPrimitives < ((float)numOriginalPrimitives) * getSampleRatio()) && nextError>getMaximumLength(); |
---|
82 | } |
---|
83 | |
---|
84 | |
---|
85 | virtual void apply(osg::Geode& geode) |
---|
86 | { |
---|
87 | for(unsigned int i=0;i<geode.getNumDrawables();++i) |
---|
88 | { |
---|
89 | osg::Geometry* geometry = geode.getDrawable(i)->asGeometry(); |
---|
90 | if (geometry) |
---|
91 | { |
---|
92 | simplify(*geometry); |
---|
93 | } |
---|
94 | } |
---|
95 | } |
---|
96 | |
---|
97 | /** simply the geometry.*/ |
---|
98 | void simplify(osg::Geometry& geometry); |
---|
99 | |
---|
100 | typedef std::vector<unsigned int> IndexList; /// a list of point indices |
---|
101 | |
---|
102 | /** simply the geometry, whilst protecting key points from being modified.*/ |
---|
103 | void simplify(osg::Geometry& geometry, const IndexList& protectedPoints); |
---|
104 | |
---|
105 | |
---|
106 | protected: |
---|
107 | |
---|
108 | double _sampleRatio; |
---|
109 | double _maximumError; |
---|
110 | double _maximumLength; |
---|
111 | bool _triStrip; |
---|
112 | bool _smoothing; |
---|
113 | |
---|
114 | osg::ref_ptr<ContinueSimplificationCallback> _continueSimplificationCallback; |
---|
115 | |
---|
116 | }; |
---|
117 | |
---|
118 | |
---|
119 | } |
---|
120 | |
---|
121 | #endif |
---|