Added beginning of new osgUtil::Simplifier

This commit is contained in:
Robert Osfield 2004-04-08 11:27:01 +00:00
parent db9c192a37
commit 51c8655630
5 changed files with 227 additions and 7 deletions

View File

@ -161,6 +161,10 @@ SOURCE=..\..\src\osgUtil\SceneView.cpp
# End Source File
# Begin Source File
SOURCE=..\..\src\osgUtil\Simplifier.cpp
# End Source File
# Begin Source File
SOURCE=..\..\src\osgUtil\SmoothingVisitor.cpp
# End Source File
# Begin Source File
@ -277,6 +281,10 @@ SOURCE=..\..\Include\osgUtil\SceneView
# End Source File
# Begin Source File
SOURCE=..\..\include\osgUtil\Simplifier
# End Source File
# Begin Source File
SOURCE=..\..\include\osgUtil\SmoothingVisitor
# End Source File
# Begin Source File

View File

@ -0,0 +1,45 @@
/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2003 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_SIMPLIYFIER
#define OSGUTIL_SIMPLIYFIER 1
#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.
*/
class OSGUTIL_EXPORT Simplifier
{
public:
Simplifier();
/** simply the geometry to defined ratio of original size.*/
void simplify(osg::Geometry& geometry, float sampleRatio);
void simplify(osg::Geometry& geometry, unsigned int targetNumberOfTriangles);
protected:
};
}
#endif

View File

@ -21,6 +21,7 @@
#include <osgUtil/SmoothingVisitor>
#include <osgUtil/TriStripVisitor>
#include <osgUtil/Simplifier>
#include <osgDB/ReadFile>
#include <osgDB/WriteFile>
@ -2177,6 +2178,10 @@ osg::Node* DataSet::DestinationTile::createPolygonal()
geometry->setColorBinding(osg::Geometry::BIND_OVERALL);
}
osgUtil::Simplifier simplifier;
simplifier.simplify(*geometry,0.5f); // this will replace the normal vector with a new one
osg::Geode* geode = new osg::Geode;
geode->addDrawable(geometry);

View File

@ -3,11 +3,13 @@ include $(TOPDIR)/Make/makedefs
CXXFILES = \
UpdateVisitor.cpp\
CubeMapGenerator.cpp\
CullVisitor.cpp\
DelaunayTriangulator.cpp\
DisplayListVisitor.cpp\
DisplayRequirementsVisitor.cpp\
DelaunayTriangulator.cpp\
HalfWayMapGenerator.cpp\
HighlightMapGenerator.cpp\
InsertImpostorsVisitor.cpp\
IntersectVisitor.cpp\
Optimizer.cpp\
@ -18,16 +20,15 @@ CXXFILES = \
RenderStageLighting.cpp\
RenderToTextureStage.cpp\
SceneView.cpp\
Simplifier.cpp\
SmoothingVisitor.cpp\
TangentSpaceGenerator.cpp\
Tesselator.cpp\
TransformCallback.cpp\
TransformAttributeFunctor.cpp\
TriStripVisitor.cpp\
TransformCallback.cpp\
TriStrip_tri_stripper.cpp\
CubeMapGenerator.cpp\
HalfWayMapGenerator.cpp\
HighlightMapGenerator.cpp\
TriStripVisitor.cpp\
UpdateVisitor.cpp\
Version.cpp\
DEF += -DOSGUTIL_LIBRARY

161
src/osgUtil/Simplifier.cpp Normal file
View File

@ -0,0 +1,161 @@
/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2003 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.
*/
#include <osg/TriangleIndexFunctor>
#include <osgUtil/Simplifier>
#include <set>
#include <iostream>
using namespace osgUtil;
class EdgeCollapse
{
public:
EdgeCollapse():
_targetNumTriangles(0) {}
~EdgeCollapse() {}
void setGeometry(osg::Geometry* geometry);
osg::Geometry* getGeometry() { return _geometry; }
void setTargetNumOfTriangles(unsigned int num) { _targetNumTriangles = num; }
unsigned int getNumOfTriangles() { return 0; }
bool collapseMinimumErrorEdge() { return false; }
void copyBackToGeometry() {}
class Triangle;
class Edge;
struct Point : public osg::Referenced
{
Point() {}
unsigned int _index;
};
struct Edge : public osg::Referenced
{
Edge() {}
osg::ref_ptr<Point> _p1;
osg::ref_ptr<Point> _p2;
osg::ref_ptr<Triangle> _t1;
osg::ref_ptr<Triangle> _t2;
void setErrorMetric(float errorMetric) { _errorMetric = errorMetric; }
float getErrorMetric() const { return _errorMetric; }
float _errorMetric;
};
struct Triangle : public osg::Referenced
{
Triangle() {}
osg::ref_ptr<Point> _p1;
osg::ref_ptr<Point> _p2;
osg::ref_ptr<Point> _p3;
osg::ref_ptr<Edge> _e1;
osg::ref_ptr<Edge> _e2;
osg::ref_ptr<Edge> _e3;
};
struct LessPtr
{
inline bool operator() (const osg::ref_ptr<Edge>& lhs,const osg::ref_ptr<Edge>& rhs) const
{
return lhs->getErrorMetric()<rhs->getErrorMetric();
}
};
typedef std::set<osg::ref_ptr<Edge>,LessPtr> EdgeSet;
void addTriangle(unsigned int p1, unsigned int p2, unsigned int p3)
{
std::cout<<"addTriangle("<<p1<<","<<p2<<","<<p3<<")"<<std::endl;
}
protected:
osg::Geometry* _geometry;
unsigned int _targetNumTriangles;
EdgeSet _edgeSet;
};
struct MyTriangleOperator
{
MyTriangleOperator():_ec(0) {}
void setEdgeCollapse(EdgeCollapse* ec) { _ec = ec; }
EdgeCollapse* _ec;
// for use in the triangle functor.
inline void operator()(unsigned int p1, unsigned int p2, unsigned int p3)
{
_ec->addTriangle(p1,p2,p3);
}
};
typedef osg::TriangleIndexFunctor<MyTriangleOperator> MyTriangleIndexFunctor;
void EdgeCollapse::setGeometry(osg::Geometry* geometry)
{
_geometry = geometry;
MyTriangleIndexFunctor collectTriangles;
collectTriangles.setEdgeCollapse(this);
}
Simplifier::Simplifier()
{
}
void Simplifier::simplify(osg::Geometry& geometry, float sampleRatio)
{
EdgeCollapse ec;
ec.setGeometry(&geometry);
ec.setTargetNumOfTriangles((unsigned int)(sampleRatio*(float)ec.getNumOfTriangles()));
while (ec.collapseMinimumErrorEdge()) {}
ec.copyBackToGeometry();
}
void Simplifier::simplify(osg::Geometry& geometry, unsigned int targetNumberOfTriangles)
{
EdgeCollapse ec;
ec.setGeometry(&geometry);
ec.setTargetNumOfTriangles(targetNumberOfTriangles);
while (ec.collapseMinimumErrorEdge()) {}
ec.copyBackToGeometry();
}