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_SIMPLIFIER
15#define OSGUTIL_SIMPLIFIER 1
17#include <osg/NodeVisitor>
19#include <osg/Geometry>
21#include <osgUtil/Export>
25/** A simplifier for reducing the number of traingles in osg::Geometry.
27class OSGUTIL_EXPORT Simplifier : public osg::NodeVisitor
31 Simplifier(double sampleRatio=1.0, double maximumError=FLT_MAX, double maximumLength=0.0);
33 META_NodeVisitor(osgUtil, Simplifier)
35 void setSampleRatio(float sampleRatio) { _sampleRatio = sampleRatio; }
36 float getSampleRatio() const { return _sampleRatio; }
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; }
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; }
48 void setDoTriStrip(bool on) { _triStrip = on; }
49 bool getDoTriStrip() const { return _triStrip; }
51 void setSmoothing(bool on) { _smoothing = on; }
52 bool getSmoothing() const { return _smoothing; }
54 class ContinueSimplificationCallback : public osg::Referenced
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
60 return simplifier.continueSimplificationImplementation(nextError, numOriginalPrimitives, numRemainingPrimitives);
63 virtual bool requiresDownSampling(const Simplifier& simplifier) const
65 return simplifier.requiresDownSamplingImplementation();
69 virtual ~ContinueSimplificationCallback() {}
72 void setContinueSimplificationCallback(ContinueSimplificationCallback* cb) { _continueSimplificationCallback = cb; }
73 ContinueSimplificationCallback* getContinueSimplificationCallback() { return _continueSimplificationCallback.get(); }
74 const ContinueSimplificationCallback* getContinueSimplificationCallback() const { return _continueSimplificationCallback.get(); }
77 bool continueSimplification(float nextError, unsigned int numOriginalPrimitives, unsigned int numRemainingPrimitives) const
79 if (_continueSimplificationCallback.valid()) return _continueSimplificationCallback->continueSimplification(*this, nextError, numOriginalPrimitives, numRemainingPrimitives);
80 else return continueSimplificationImplementation(nextError, numOriginalPrimitives, numRemainingPrimitives);
83 virtual bool continueSimplificationImplementation(float nextError, unsigned int numOriginalPrimitives, unsigned int numRemainingPrimitives) const
85 if (getSampleRatio()<1.0) return ((float)numRemainingPrimitives > ((float)numOriginalPrimitives) * getSampleRatio()) && nextError<=getMaximumError();
86 else return ((float)numRemainingPrimitives < ((float)numOriginalPrimitives) * getSampleRatio()) && nextError>getMaximumLength();
89 bool requiresDownSampling() const
91 if (_continueSimplificationCallback.valid()) return _continueSimplificationCallback->requiresDownSampling(*this);
92 else return requiresDownSamplingImplementation();
95 virtual bool requiresDownSamplingImplementation() const
97 return getSampleRatio()<1.0;
100 virtual void apply(osg::Geometry& geom)
105 /** simply the geometry.*/
106 void simplify(osg::Geometry& geometry);
108 typedef std::vector<unsigned int> IndexList; /// a list of point indices
110 /** simply the geometry, whilst protecting key points from being modified.*/
111 void simplify(osg::Geometry& geometry, const IndexList& protectedPoints);
117 double _maximumError;
118 double _maximumLength;
122 osg::ref_ptr<ContinueSimplificationCallback> _continueSimplificationCallback;