30 #include <osgUtil/DelaunayTriangulator>
32 #include "WGEGeometryUtils.h"
34 #include "WTriangleMesh.h"
35 #include "exceptions/WGEException.h"
39 osg::ref_ptr< osg::Vec3Array > vertices = osg::ref_ptr< osg::Vec3Array >(
new osg::Vec3Array );
42 vertices->push_back( corners[0] );
43 vertices->push_back( corners[1] );
44 vertices->push_back( corners[2] );
45 vertices->push_back( corners[3] );
47 vertices->push_back( corners[1] );
48 vertices->push_back( corners[5] );
49 vertices->push_back( corners[6] );
50 vertices->push_back( corners[2] );
52 vertices->push_back( corners[5] );
53 vertices->push_back( corners[4] );
54 vertices->push_back( corners[7] );
55 vertices->push_back( corners[6] );
57 vertices->push_back( corners[4] );
58 vertices->push_back( corners[0] );
59 vertices->push_back( corners[3] );
60 vertices->push_back( corners[7] );
62 vertices->push_back( corners[3] );
63 vertices->push_back( corners[2] );
64 vertices->push_back( corners[6] );
65 vertices->push_back( corners[7] );
67 vertices->push_back( corners[0] );
68 vertices->push_back( corners[1] );
69 vertices->push_back( corners[5] );
70 vertices->push_back( corners[4] );
81 return normalize( normal );
86 osg::ref_ptr< osg::Vec3Array > vertices = osg::ref_ptr< osg::Vec3Array >(
new osg::Vec3Array );
88 vertices->push_back(
getQuadNormal( corners[0], corners[1], corners[2] ) );
89 vertices->push_back(
getQuadNormal( corners[1], corners[5], corners[6] ) );
90 vertices->push_back(
getQuadNormal( corners[5], corners[4], corners[7] ) );
91 vertices->push_back(
getQuadNormal( corners[4], corners[0], corners[3] ) );
92 vertices->push_back(
getQuadNormal( corners[3], corners[2], corners[6] ) );
93 vertices->push_back(
getQuadNormal( corners[0], corners[1], corners[5] ) );
99 WAssert( points.size() > 2,
"The Delaunay triangulation needs at least 3 vertices!" );
103 if( transformationFactor != 0.0 )
108 for( std::size_t pointID = 0; pointID < osgPoints->size(); ++pointID )
110 centroid += (*osgPoints)[pointID];
112 centroid /= osgPoints->size();
114 for( std::size_t pointID = 0; pointID < osgPoints->size(); ++pointID )
116 const double factor = ( (*osgPoints)[pointID].z() - centroid.z() ) * transformationFactor + 1.0;
117 (*osgPoints)[pointID].x() = ( (*osgPoints)[pointID].x() - centroid.x() ) * factor + centroid.x();
118 (*osgPoints)[pointID].y() = ( (*osgPoints)[pointID].y() - centroid.y() ) * factor + centroid.y();
126 std::map< osg::Vec3, size_t > map;
127 for(
size_t index = 0; index < osgPoints->size(); ++index )
129 map[ (*osgPoints)[index] ] = index;
132 osg::ref_ptr< osgUtil::DelaunayTriangulator > triangulator(
new osgUtil::DelaunayTriangulator( osgPoints ) );
134 bool triangulationResult = triangulator->triangulate();
136 WAssert( triangulationResult,
"Something went wrong in triangulation." );
138 osg::ref_ptr< const osg::DrawElementsUInt > osgTriangles( triangulator->getTriangles() );
139 size_t nbTriangles = osgTriangles->size() / 3;
140 std::vector< size_t > triangles( osgTriangles->size() );
141 for(
size_t triangleID = 0; triangleID < nbTriangles; ++triangleID )
145 size_t vertID = triangleID * 3;
146 triangles[vertID + 0] = map[ (*osgPoints)[ (*osgTriangles)[vertID + 0] ] ];
147 triangles[vertID + 1] = map[ (*osgPoints)[ (*osgTriangles)[vertID + 1] ] ];
148 triangles[vertID + 2] = map[ (*osgPoints)[ (*osgTriangles)[vertID + 2] ] ];
This only is a 3d double vector.
Triangle mesh data structure allowing for convenient access of the elements.
std::shared_ptr< WTriangleMesh > SPtr
Shared pointer.
osg::ref_ptr< osg::Vec3Array > generateCuboidQuadNormals(const std::vector< WPosition > &corners)
Generates for all QUADS of the Cuboid the normals in the following order:
WTriangleMesh::SPtr triangulate(const std::vector< WPosition > &points, double transformationFactor=0.0)
Calculate the Delaunay Triangulation of the given points.
osg::Vec3 getQuadNormal(const WPosition &a, const WPosition &b, const WPosition &c)
Generates for a QUAD given via 3 three points ( the fourth is not needed ) the normal.
osg::ref_ptr< osg::Vec3Array > generateCuboidQuads(const std::vector< WPosition > &corners)
Creates out of eight corner vertices QUAD vertices.
osg::ref_ptr< osg::Vec3Array > osgVec3Array(const std::vector< WPosition > &posArray)
Converts a whole vector of WPositions into an osg::Vec3Array.