Simplifier 5.2 KB

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