Here is the code : http://www.3rdm.biz/files/OSG_DelaunayTest.cpp

Or cut and paste :


Code:

#include <osgViewer/Viewer>
#include <osgViewer/ViewerEventHandlers>
#include <osgUtil/SmoothingVisitor>
#include <osgUtil/DelaunayTriangulator>
#include <osgDB/WriteFile>

#include <iostream>
#include <algorithm>

using namespace std;

// 
________________________________________________________________________________________________
// Generate a gaussian pseudo-random number

double FastAndDirtyGaussianRandomNumber( double sequenceMean, double 
sequenceSigma )
{
    double u = ( static_cast<double>( rand() ) / 
static_cast<double>(RAND_MAX))*2.0 - 1.0;
    double v = ( static_cast<double>( rand() ) / 
static_cast<double>(RAND_MAX))*2.0 - 1.0;
    double r = u*u + v*v;
    if ( r==0 || r>1 )
    {
       return FastAndDirtyGaussianRandomNumber( sequenceMean, sequenceSigma );
    }
    double c = sqrt( -2.0*log(r)/r );
    return sequenceMean + sequenceSigma*( u*c );
}

struct GaussianRandomSeq
{
   double mMean, mSigma;
   GaussianRandomSeq( double sequenceMean, double sequenceSigma, unsigned int 
randomSeed)
   : mMean( sequenceMean ), mSigma( sequenceSigma )
   {
      srand( randomSeed );
   }
   double operator() () { return FastAndDirtyGaussianRandomNumber( mMean, 
mSigma ); }
};

// 
________________________________________________________________________________________________
// Main

int main( int argc, const char** pArgv )
{
// Smoothing has the effect of softening the abberation (Linux). Turn smoothing 
off to see the
// abberation even better
   bool doSmoothing = false;

// Define surface sizes
   size_t dimension = 256;
   unsigned int rows=dimension, cols=dimension;
   unsigned int size = rows * cols;

// Create elevation map as a gaussian random surface
        cout << "Create points ...... " << endl;
   osg::ref_ptr<osg::Vec3Array> rPoints = new osg::Vec3Array( size );
   unsigned int seed = 7993;
   double mean = 0.0, sigma=0.5;
   GaussianRandomSeq randSeq( mean, sigma, seed );

   std::vector<double> elevationData( rows*cols );
   std::generate( elevationData.begin(), elevationData.end(), randSeq );

// Create 3D points using the elevation map
   unsigned pointIndex = 0;
   for( unsigned i=0; i<rows; i++ )
   {
      float y = static_cast<double>( i );
      for( unsigned j=0; j<cols; j++ )
      {
         float x = static_cast<double>( j );
         float z = elevationData[pointIndex];
         (*rPoints).at( pointIndex++ ) = osg::Vec3( x, y, z );
      }
   }

// Create geometry with a triangulator
        cout << "Triangulate  ...... " << endl;
   osg::ref_ptr<osg::Geometry> rGeom = new osg::Geometry;
   osg::ref_ptr<osgUtil::DelaunayTriangulator> triangulator =
                new osgUtil::DelaunayTriangulator( rPoints );
   triangulator->setOutputNormalArray( new osg::Vec3Array() );
   triangulator->triangulate();

   rGeom->addPrimitiveSet( triangulator->getTriangles() );
   rGeom->setVertexArray( triangulator->getInputPointArray() );
   rGeom->setNormalArray( triangulator->getOutputNormalArray() );
   rGeom->setNormalBinding( osg::Geometry::BIND_PER_PRIMITIVE );
   rGeom->setTexCoordArray( 0, new osg::Vec2Array( size ) );

// Colorize uniformly
        cout << "Post prep  ...... " << endl;
   osg::ref_ptr<osg::Vec4Array> rColors = new osg::Vec4Array();
   osg::Vec4 color( 1, 1, 1, 1 );
   rColors->push_back( color );
   rGeom->setColorArray( rColors );
   rGeom->setColorBinding( osg::Geometry::BIND_OVERALL );

// Smooth the geometry
   if ( doSmoothing )
   {
      osgUtil::SmoothingVisitor smoother;
      smoother.smooth( *rGeom );
   }

// Create geode and save to disk
   osg::ref_ptr<osg::Geode> rGeode = new osg::Geode();
   rGeode->addDrawable( rGeom );
        cout << "Write 'Triangulated.osg'  ...... " << endl;
   osgDB::writeNodeFile( *rGeode.get(), "Triangulated.osg" );

// Create scene graph and add geometry
   osg::ref_ptr<osg::Group> rSceneGraph = new osg::Group();
   rSceneGraph->addChild( rGeode );

// Setup viewer and run
   osgViewer::Viewer viewer;
   viewer.setSceneData( rSceneGraph );
   viewer.addEventHandler( new osgViewer::StatsHandler() );
        return viewer.run();
}





------------------------
things are more like they are now than they have ever been before

------------------
Read this topic online here:
http://forum.openscenegraph.org/viewtopic.php?p=58085#58085





_______________________________________________
osg-users mailing list
[email protected]
http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org

Reply via email to