Commit 606340ec authored by cornimueller's avatar cornimueller
Browse files

[ADD] Added triangulate() function to WGEGeometryUtils which calculates a...

[ADD] Added triangulate() function to WGEGeometryUtils which calculates a Delaunay triangulation of points and returns it as WTriangleMesh
[CHANGE] Moved TriangleMesh from dataHandler to common so it can be used by graphicsEngine
parent 9e36ee4e
......@@ -26,6 +26,7 @@
#include "WTriangleMesh.h"
WTriangleMesh::WTriangleMesh()
: m_fastAddVertId( 0 ),
m_fastAddTriangleId( 0 ),
......
......@@ -27,8 +27,9 @@
#include <vector>
#include "../math/WPosition.h"
#include "../math/WVector3D.h"
#include "../../math/WPosition.h"
#include "../../math/WVector3D.h"
/**
* A triangle consisting of 3 id of vertices that represent its corners
......
......@@ -30,7 +30,8 @@
#include <cxxtest/TestSuite.h>
#include "../WTriangleMesh.h"
#include "../../math/WVector3D.h"
#include "../../../math/WVector3D.h"
/**
* Testing the WTriangleMesh class.
......
......@@ -25,10 +25,13 @@
#include <vector>
#include <osg/Array>
#include <osgUtil/DelaunayTriangulator>
#include "../math/WPosition.h"
#include "exceptions/WGEException.h"
#include "WGEGeometryUtils.h"
#include "WGEUtils.h"
#include "../math/WPosition.h"
osg::ref_ptr< osg::Vec3Array > wge::generateCuboidQuads( const std::vector< wmath::WPosition >& corners )
{
......@@ -90,3 +93,29 @@ osg::ref_ptr< osg::Vec3Array > wge::generateCuboidQuadNormals( const std::vector
vertices->push_back( getQuadNormal( corners[0], corners[1], corners[5] ) );
return vertices;
}
WTriangleMesh wge::triangulate( const std::vector< wmath::WPosition >& points )
{
osg::ref_ptr< osgUtil::DelaunayTriangulator > triangulator( new osgUtil::DelaunayTriangulator( wge::osgVec3Array( points ) ) );
if( triangulator->triangulate() )
{
osg::ref_ptr< const osg::DrawElementsUInt > osgTriangles( triangulator->getTriangles() );
size_t nbTriangles = osgTriangles->size() / 3;
std::vector< Triangle > triangles( nbTriangles );
for( size_t triangleID = 0; triangleID < nbTriangles; ++triangleID )
{
triangles[triangleID].pointID[0] = (*osgTriangles)[3 * triangleID];
triangles[triangleID].pointID[1] = (*osgTriangles)[3 * triangleID + 1];
triangles[triangleID].pointID[2] = (*osgTriangles)[3 * triangleID + 2];
}
WTriangleMesh mesh;
mesh.setVertices( points );
mesh.setTriangles( triangles );
return mesh;
}
else
{
throw WGEException( "Error in triangulation" );
}
}
......@@ -29,8 +29,10 @@
#include <osg/Array>
#include "../common/datastructures/WTriangleMesh.h"
#include "../math/WPosition.h"
namespace wge
{
/**
......@@ -101,6 +103,14 @@ namespace wge
*\return Array of normals in the order as shown above.
*/
osg::ref_ptr< osg::Vec3Array > generateCuboidQuadNormals( const std::vector< wmath::WPosition >& corners );
/**
* Calculate the Delaunay Triangulatation of the given points.
*
* \param points vector of the points to triangulate
* \return triangulation as WTriangleMesh
*/
WTriangleMesh triangulate( const std::vector< wmath::WPosition >& points );
}
#endif // WGEGEOMETRYUTILS_H
//---------------------------------------------------------------------------
//
// Project: OpenWalnut ( http://www.openwalnut.org )
//
// Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
// For more information see http://www.openwalnut.org/copying
//
// This file is part of OpenWalnut.
//
// OpenWalnut is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// OpenWalnut 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
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
//
//---------------------------------------------------------------------------
#ifndef WGEGEOMETRYUTILS_TEST_H
#define WGEGEOMETRYUTILS_TEST_H
#include <vector>
#include <cxxtest/TestSuite.h>
#include "../WGEGeometryUtils.h"
/**
* Tests for the GeometryUtils
*/
class WGEGeometryUtilsTest : public CxxTest::TestSuite
{
public:
/**
* Test triangulate()
*/
void testTriangulate( void )
{
std::vector< wmath::WPosition > points( 4 );
points[0] = wmath::WPosition( 0.0, 0.0, 0.0 );
points[1] = wmath::WPosition( 1.0, 0.0, 0.0 );
points[2] = wmath::WPosition( 1.0, 1.0, 0.0 );
points[3] = wmath::WPosition( 2.0, 1.0, 0.0 );
WTriangleMesh mesh = wge::triangulate( points );
// test number of generated triangles
TS_ASSERT_EQUALS( mesh.getNumTriangles(), 2 );
// test by how many triangles the points are surrounded
TS_ASSERT_EQUALS( mesh.getPositionTriangleNeighborsSlow( 0 ).size(), 1 );
TS_ASSERT_EQUALS( mesh.getPositionTriangleNeighborsSlow( 1 ).size(), 2 );
TS_ASSERT_EQUALS( mesh.getPositionTriangleNeighborsSlow( 2 ).size(), 2 );
TS_ASSERT_EQUALS( mesh.getPositionTriangleNeighborsSlow( 3 ).size(), 1 );
// This is enough for a unique topologie of the triangulation. The IDs
// of the triangles and the order of the vertices at each triangle may
// vary.
}
};
#endif // WGEGEOMETRYUTILS_TEST_H
......@@ -33,7 +33,7 @@
#include "WMMarchingCubes.h"
#include "iso_surface.xpm"
#include "marchingCubesCaseTables.h"
#include "../../dataHandler/WTriangleMesh.h"
#include "../../common/datastructures/WTriangleMesh.h"
#include "../../common/WLimits.h"
#include <osg/Geode>
......
......@@ -33,12 +33,12 @@
#include <osg/Geode>
#include <osg/Uniform>
#include "../../common/datastructures/WTriangleMesh.h"
#include "../../kernel/WModule.h"
#include "../../kernel/WModuleInputData.h"
#include "../../dataHandler/WGridRegular3D.h"
#include "../../graphicsEngine/WShader.h"
#include "../../graphicsEngine/WGEGroupNode.h"
#include "../../dataHandler/WTriangleMesh.h"
/**
* A point consisting of its coordinates and ID
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment