Simplifier
Go to the documentation of this file.
/* -*-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_SIMPLIFIER
#define OSGUTIL_SIMPLIFIER 1
#include <osg/NodeVisitor>
#include <osg/Geode>
#include <osg/Geometry>
#include <osgUtil/Export>
namespace osgUtil {
class OSGUTIL_EXPORT Simplifier : public osg::NodeVisitor
{
public:
Simplifier(double sampleRatio=1.0, double maximumError=FLT_MAX, double maximumLength=0.0);
META_NodeVisitor(osgUtil, Simplifier)
void setSampleRatio(float sampleRatio) { _sampleRatio = sampleRatio; }
float getSampleRatio() const { return _sampleRatio; }
void setMaximumError(float error) { _maximumError = error; }
float getMaximumError() const { return _maximumError; }
void setMaximumLength(float length) { _maximumLength = length; }
float getMaximumLength() const { return _maximumLength; }
void setDoTriStrip(bool on) { _triStrip = on; }
bool getDoTriStrip() const { return _triStrip; }
void setSmoothing(bool on) { _smoothing = on; }
bool getSmoothing() const { return _smoothing; }
class ContinueSimplificationCallback : public osg::Referenced
{
public:
virtual bool continueSimplification(const Simplifier& simplifier, float nextError, unsigned int numOriginalPrimitives, unsigned int numRemainingPrimitives) const
{
return simplifier.continueSimplificationImplementation(nextError, numOriginalPrimitives, numRemainingPrimitives);
}
virtual bool requiresDownSampling(const Simplifier& simplifier) const
{
return simplifier.requiresDownSamplingImplementation();
}
protected:
virtual ~ContinueSimplificationCallback() {}
};
void setContinueSimplificationCallback(ContinueSimplificationCallback* cb) { _continueSimplificationCallback = cb; }
ContinueSimplificationCallback* getContinueSimplificationCallback() { return _continueSimplificationCallback.get(); }
const ContinueSimplificationCallback* getContinueSimplificationCallback() const { return _continueSimplificationCallback.get(); }
bool continueSimplification(float nextError, unsigned int numOriginalPrimitives, unsigned int numRemainingPrimitives) const
{
if (_continueSimplificationCallback.valid()) return _continueSimplificationCallback->continueSimplification(*this, nextError, numOriginalPrimitives, numRemainingPrimitives);
else return continueSimplificationImplementation(nextError, numOriginalPrimitives, numRemainingPrimitives);
}
virtual bool continueSimplificationImplementation(float nextError, unsigned int numOriginalPrimitives, unsigned int numRemainingPrimitives) const
{
if (getSampleRatio()<1.0) return ((float)numRemainingPrimitives > ((float)numOriginalPrimitives) * getSampleRatio()) && nextError<=getMaximumError();
else return ((float)numRemainingPrimitives < ((float)numOriginalPrimitives) * getSampleRatio()) && nextError>getMaximumLength();
}
bool requiresDownSampling() const
{
if (_continueSimplificationCallback.valid()) return _continueSimplificationCallback->requiresDownSampling(*this);
else return requiresDownSamplingImplementation();
}
virtual bool requiresDownSamplingImplementation() const
{
return getSampleRatio()<1.0;
}
virtual void apply(osg::Geometry& geom)
{
simplify(geom);
}
void simplify(osg::Geometry& geometry);
typedef std::vector<unsigned int> IndexList;
void simplify(osg::Geometry& geometry, const IndexList& protectedPoints);
protected:
double _sampleRatio;
double _maximumError;
double _maximumLength;
bool _triStrip;
bool _smoothing;
osg::ref_ptr<ContinueSimplificationCallback> _continueSimplificationCallback;
};
}
#endif
Geode
Geometry
NodeVisitor
osgUtil::Simplifier::ContinueSimplificationCallback
Definition Simplifier:55
osgUtil::Simplifier::ContinueSimplificationCallback::continueSimplification
virtual bool continueSimplification(const Simplifier &simplifier, float nextError, unsigned int numOriginalPrimitives, unsigned int numRemainingPrimitives) const
Definition Simplifier:58
osgUtil::Simplifier::ContinueSimplificationCallback::requiresDownSampling
virtual bool requiresDownSampling(const Simplifier &simplifier) const
Definition Simplifier:63
osgUtil::Simplifier::ContinueSimplificationCallback::~ContinueSimplificationCallback
virtual ~ContinueSimplificationCallback()
Definition Simplifier:69
osgUtil::Simplifier::simplify
void simplify(osg::Geometry &geometry, const IndexList &protectedPoints)
a list of point indices
osgUtil::Simplifier::getContinueSimplificationCallback
ContinueSimplificationCallback * getContinueSimplificationCallback()
Definition Simplifier:73
osgUtil::Simplifier::getSmoothing
bool getSmoothing() const
Definition Simplifier:52
osgUtil::Simplifier::_maximumError
double _maximumError
Definition Simplifier:117
osgUtil::Simplifier::setDoTriStrip
void setDoTriStrip(bool on)
Definition Simplifier:48
osgUtil::Simplifier::_maximumLength
double _maximumLength
Definition Simplifier:118
osgUtil::Simplifier::getSampleRatio
float getSampleRatio() const
Definition Simplifier:36
osgUtil::Simplifier::META_NodeVisitor
META_NodeVisitor(osgUtil, Simplifier) void setSampleRatio(float sampleRatio)
Definition Simplifier:33
osgUtil::Simplifier::getMaximumLength
float getMaximumLength() const
Definition Simplifier:46
osgUtil::Simplifier::setMaximumLength
void setMaximumLength(float length)
Definition Simplifier:45
osgUtil::Simplifier::_triStrip
bool _triStrip
Definition Simplifier:119
osgUtil::Simplifier::setSmoothing
void setSmoothing(bool on)
Definition Simplifier:51
osgUtil::Simplifier::continueSimplificationImplementation
virtual bool continueSimplificationImplementation(float nextError, unsigned int numOriginalPrimitives, unsigned int numRemainingPrimitives) const
Definition Simplifier:83
osgUtil::Simplifier::getContinueSimplificationCallback
const ContinueSimplificationCallback * getContinueSimplificationCallback() const
Definition Simplifier:74
osgUtil::Simplifier::continueSimplification
bool continueSimplification(float nextError, unsigned int numOriginalPrimitives, unsigned int numRemainingPrimitives) const
Definition Simplifier:77
osgUtil::Simplifier::getDoTriStrip
bool getDoTriStrip() const
Definition Simplifier:49
osgUtil::Simplifier::simplify
void simplify(osg::Geometry &geometry)
osgUtil::Simplifier::IndexList
std::vector< unsigned int > IndexList
Definition Simplifier:108
osgUtil::Simplifier::setMaximumError
void setMaximumError(float error)
Definition Simplifier:40
osgUtil::Simplifier::requiresDownSampling
bool requiresDownSampling() const
Definition Simplifier:89
osgUtil::Simplifier::requiresDownSamplingImplementation
virtual bool requiresDownSamplingImplementation() const
Definition Simplifier:95
osgUtil::Simplifier::_continueSimplificationCallback
osg::ref_ptr< ContinueSimplificationCallback > _continueSimplificationCallback
Definition Simplifier:122
osgUtil::Simplifier::getMaximumError
float getMaximumError() const
Definition Simplifier:41
osgUtil::Simplifier::_smoothing
bool _smoothing
Definition Simplifier:120
osgUtil::Simplifier::_sampleRatio
double _sampleRatio
Definition Simplifier:116
osgUtil::Simplifier::apply
virtual void apply(osg::Geometry &geom)
Definition Simplifier:100
osgUtil::Simplifier::setContinueSimplificationCallback
void setContinueSimplificationCallback(ContinueSimplificationCallback *cb)
Definition Simplifier:72
osgUtil::Simplifier::Simplifier
Simplifier(double sampleRatio=1.0, double maximumError=FLT_MAX, double maximumLength=0.0)
osg::Geometry
Definition Geometry:31
osg::NodeVisitor
Definition NodeVisitor:82
osg::Referenced
Definition Referenced:44
osg::ref_ptr
Definition ref_ptr:32
osgUtil
Definition NodeVisitor:25
Export
OSGUTIL_EXPORT
#define OSGUTIL_EXPORT
Definition osgUtil/Export:40