OSG  3.4.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Simplifier.h
Go to the documentation of this file.
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 
28 {
29  public:
30 
31  Simplifier(double sampleRatio=1.0, double maximumError=FLT_MAX, double maximumLength=0.0);
32 
34 
35  void setSampleRatio(float sampleRatio) { _sampleRatio = sampleRatio; }
36  float getSampleRatio() const { return _sampleRatio; }
37 
40  void setMaximumError(float error) { _maximumError = error; }
41  float getMaximumError() const { return _maximumError; }
42 
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 
55  {
56  public:
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:
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 
98  void simplify(osg::Geometry& geometry);
99 
100  typedef std::vector<unsigned int> IndexList;
101 
103  void simplify(osg::Geometry& geometry, const IndexList& protectedPoints);
104 
105 
106  protected:
107 
108  double _sampleRatio;
111  bool _triStrip;
113 
115 
116 };
117 
118 
119 }
120 
121 #endif
void setMaximumLength(float length)
Definition: Simplifier.h:45
void setContinueSimplificationCallback(ContinueSimplificationCallback *cb)
Definition: Simplifier.h:67
float getSampleRatio() const
Definition: Simplifier.h:36
const ContinueSimplificationCallback * getContinueSimplificationCallback() const
Definition: Simplifier.h:69
META_NodeVisitor(osgUtil, Simplifier) void setSampleRatio(float sampleRatio)
Definition: Simplifier.h:33
bool getDoTriStrip() const
Definition: Simplifier.h:49
virtual bool continueSimplificationImplementation(float nextError, unsigned int numOriginalPrimitives, unsigned int numRemainingPrimitives) const
Definition: Simplifier.h:78
std::vector< unsigned int > IndexList
Definition: Simplifier.h:100
ContinueSimplificationCallback * getContinueSimplificationCallback()
Definition: Simplifier.h:68
typedef void(GL_APIENTRY *GLTexImage3DProc)(GLenum target
void setSmoothing(bool on)
Definition: Simplifier.h:51
osg::ref_ptr< ContinueSimplificationCallback > _continueSimplificationCallback
Definition: Simplifier.h:114
float getMaximumLength() const
Definition: Simplifier.h:46
virtual bool continueSimplification(const Simplifier &simplifier, float nextError, unsigned int numOriginalPrimitives, unsigned int numRemainingPrimitives) const
Definition: Simplifier.h:58
virtual void apply(osg::Geode &geode)
Definition: Simplifier.h:85
#define OSGUTIL_EXPORT
Definition: Export.h:40
virtual Geometry * asGeometry()
Definition: Node.h:115
Drawable * getDrawable(unsigned int i)
Definition: Geode.h:94
void setDoTriStrip(bool on)
Definition: Simplifier.h:48
unsigned int getNumDrawables() const
Definition: Geode.h:91
void setMaximumError(float error)
Definition: Simplifier.h:40
float getMaximumError() const
Definition: Simplifier.h:41
Shader generator framework.
Definition: RenderInfo.h:20
bool continueSimplification(float nextError, unsigned int numOriginalPrimitives, unsigned int numRemainingPrimitives) const
Definition: Simplifier.h:72
bool getSmoothing() const
Definition: Simplifier.h:52