2012-03-22 01:36:20 +08:00
|
|
|
/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield
|
2004-04-08 19:27:01 +08:00
|
|
|
*
|
2012-03-22 01:36:20 +08:00
|
|
|
* 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
|
2004-04-08 19:27:01 +08:00
|
|
|
* (at your option) any later version. The full license is in LICENSE file
|
|
|
|
* included with this distribution, and on the openscenegraph.org website.
|
2012-03-22 01:36:20 +08:00
|
|
|
*
|
2004-04-08 19:27:01 +08:00
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2012-03-22 01:36:20 +08:00
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2004-04-08 19:27:01 +08:00
|
|
|
* OpenSceneGraph Public License for more details.
|
|
|
|
*/
|
|
|
|
|
2004-09-28 17:14:04 +08:00
|
|
|
#ifndef OSGUTIL_SIMPLIFIER
|
|
|
|
#define OSGUTIL_SIMPLIFIER 1
|
2004-04-08 19:27:01 +08:00
|
|
|
|
|
|
|
#include <osg/NodeVisitor>
|
|
|
|
#include <osg/Geode>
|
|
|
|
#include <osg/Geometry>
|
|
|
|
|
|
|
|
#include <osgUtil/Export>
|
|
|
|
|
|
|
|
namespace osgUtil {
|
|
|
|
|
|
|
|
/** A simplifier for reducing the number of traingles in osg::Geometry.
|
|
|
|
*/
|
2004-04-13 05:29:31 +08:00
|
|
|
class OSGUTIL_EXPORT Simplifier : public osg::NodeVisitor
|
2004-04-08 19:27:01 +08:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
2007-12-20 23:50:07 +08:00
|
|
|
Simplifier(double sampleRatio=1.0, double maximumError=FLT_MAX, double maximumLength=0.0);
|
2004-04-20 07:08:30 +08:00
|
|
|
|
2013-01-25 02:48:34 +08:00
|
|
|
META_NodeVisitor(osgUtil, Simplifier)
|
2004-04-19 00:15:49 +08:00
|
|
|
|
|
|
|
void setSampleRatio(float sampleRatio) { _sampleRatio = sampleRatio; }
|
|
|
|
float getSampleRatio() const { return _sampleRatio; }
|
2004-04-20 07:08:30 +08:00
|
|
|
|
2006-02-07 03:16:04 +08:00
|
|
|
/** Set the maximum point error that all point removals must be less than to permit removal of a point.
|
|
|
|
* Note, Only used when down sampling. i.e. sampleRatio < 1.0*/
|
2004-04-20 07:08:30 +08:00
|
|
|
void setMaximumError(float error) { _maximumError = error; }
|
|
|
|
float getMaximumError() const { return _maximumError; }
|
2006-02-07 03:16:04 +08:00
|
|
|
|
|
|
|
/** Set the maximum length target that all edges must be shorted than.
|
|
|
|
* Note, Only used when up sampling i.e. sampleRatio > 1.0.*/
|
|
|
|
void setMaximumLength(float length) { _maximumLength = length; }
|
|
|
|
float getMaximumLength() const { return _maximumLength; }
|
2006-02-07 04:36:09 +08:00
|
|
|
|
|
|
|
void setDoTriStrip(bool on) { _triStrip = on; }
|
|
|
|
bool getDoTriStrip() const { return _triStrip; }
|
|
|
|
|
|
|
|
void setSmoothing(bool on) { _smoothing = on; }
|
|
|
|
bool getSmoothing() const { return _smoothing; }
|
2012-03-22 01:36:20 +08:00
|
|
|
|
2004-04-20 07:08:30 +08:00
|
|
|
class ContinueSimplificationCallback : public osg::Referenced
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
/** return true if mesh should be continued to be simplified, return false to stop simplification.*/
|
|
|
|
virtual bool continueSimplification(const Simplifier& simplifier, float nextError, unsigned int numOriginalPrimitives, unsigned int numRemainingPrimitives) const
|
|
|
|
{
|
|
|
|
return simplifier.continueSimplificationImplementation(nextError, numOriginalPrimitives, numRemainingPrimitives);
|
|
|
|
}
|
2016-01-18 19:50:07 +08:00
|
|
|
|
|
|
|
virtual bool requiresDownSampling(const Simplifier& simplifier) const
|
|
|
|
{
|
|
|
|
return simplifier.requiresDownSamplingImplementation();
|
|
|
|
}
|
2012-03-22 01:36:20 +08:00
|
|
|
|
2004-04-20 07:08:30 +08:00
|
|
|
protected:
|
|
|
|
virtual ~ContinueSimplificationCallback() {}
|
|
|
|
};
|
2012-03-22 01:36:20 +08:00
|
|
|
|
2004-04-20 07:08:30 +08:00
|
|
|
void setContinueSimplificationCallback(ContinueSimplificationCallback* cb) { _continueSimplificationCallback = cb; }
|
|
|
|
ContinueSimplificationCallback* getContinueSimplificationCallback() { return _continueSimplificationCallback.get(); }
|
|
|
|
const ContinueSimplificationCallback* getContinueSimplificationCallback() const { return _continueSimplificationCallback.get(); }
|
2012-03-22 01:36:20 +08:00
|
|
|
|
|
|
|
|
2004-04-20 07:08:30 +08:00
|
|
|
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
|
|
|
|
{
|
2006-02-07 01:12:35 +08:00
|
|
|
if (getSampleRatio()<1.0) return ((float)numRemainingPrimitives > ((float)numOriginalPrimitives) * getSampleRatio()) && nextError<=getMaximumError();
|
2006-02-07 03:16:04 +08:00
|
|
|
else return ((float)numRemainingPrimitives < ((float)numOriginalPrimitives) * getSampleRatio()) && nextError>getMaximumLength();
|
2004-04-20 07:08:30 +08:00
|
|
|
}
|
2016-01-18 19:50:07 +08:00
|
|
|
|
|
|
|
bool requiresDownSampling() const
|
|
|
|
{
|
|
|
|
if (_continueSimplificationCallback.valid()) return _continueSimplificationCallback->requiresDownSampling(*this);
|
|
|
|
else return requiresDownSamplingImplementation();
|
|
|
|
}
|
2004-04-20 07:08:30 +08:00
|
|
|
|
2016-01-18 19:50:07 +08:00
|
|
|
virtual bool requiresDownSamplingImplementation() const
|
|
|
|
{
|
|
|
|
return getSampleRatio()<1.0;
|
|
|
|
}
|
2004-04-20 07:08:30 +08:00
|
|
|
|
2004-04-13 05:29:31 +08:00
|
|
|
virtual void apply(osg::Geode& geode)
|
|
|
|
{
|
|
|
|
for(unsigned int i=0;i<geode.getNumDrawables();++i)
|
|
|
|
{
|
|
|
|
osg::Geometry* geometry = geode.getDrawable(i)->asGeometry();
|
|
|
|
if (geometry)
|
|
|
|
{
|
2004-04-20 07:08:30 +08:00
|
|
|
simplify(*geometry);
|
2004-04-13 05:29:31 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2004-04-08 19:27:01 +08:00
|
|
|
|
2004-04-22 21:36:30 +08:00
|
|
|
/** simply the geometry.*/
|
2004-04-20 07:08:30 +08:00
|
|
|
void simplify(osg::Geometry& geometry);
|
2012-03-22 01:36:20 +08:00
|
|
|
|
2004-04-22 21:36:30 +08:00
|
|
|
typedef std::vector<unsigned int> IndexList; /// a list of point indices
|
2004-04-08 19:27:01 +08:00
|
|
|
|
2004-04-22 21:36:30 +08:00
|
|
|
/** simply the geometry, whilst protecting key points from being modified.*/
|
|
|
|
void simplify(osg::Geometry& geometry, const IndexList& protectedPoints);
|
2004-04-08 19:27:01 +08:00
|
|
|
|
2006-02-07 01:12:35 +08:00
|
|
|
|
2004-04-08 19:27:01 +08:00
|
|
|
protected:
|
2012-03-22 01:36:20 +08:00
|
|
|
|
2007-12-20 23:50:07 +08:00
|
|
|
double _sampleRatio;
|
|
|
|
double _maximumError;
|
|
|
|
double _maximumLength;
|
2006-02-07 04:36:09 +08:00
|
|
|
bool _triStrip;
|
|
|
|
bool _smoothing;
|
2012-03-22 01:36:20 +08:00
|
|
|
|
2004-04-20 07:08:30 +08:00
|
|
|
osg::ref_ptr<ContinueSimplificationCallback> _continueSimplificationCallback;
|
2012-03-22 01:36:20 +08:00
|
|
|
|
2004-04-08 19:27:01 +08:00
|
|
|
};
|
|
|
|
|
2004-04-13 05:29:31 +08:00
|
|
|
|
2004-04-08 19:27:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|