How To: Create a Mesh of a Convex Hull |
This section will demonstrate how to construct the convex hull of a cloud of points and convert the convex hull to a triangle mesh which can be rendered.
This topic contains the following sections:
The following example will use data types and helper methods from DigitalRune Geometry and Mathematics. The following namespaces must be imported for this example at the beginning of the source code file:
using DigitalRune.Geometry; using DigitalRune.Geometry.Meshes; using DigitalRune.Mathematics.Algebra; using DigitalRune.Mathematics.Statistics;
For the purpose of this example, we will create a cloud consisting of randomly placed points. The points will lie between (-100, -100, -100) and (100, 100, 100).
List<Vector3F> points = new List<Vector3F>(); for (int i = 0; i < 100; i++) points.Add(RandomHelper.Random.NextVector3F(-100, 100));
Next, we will build the convex hull of all points. GeometryHelper is an auxiliary class which can be used to create a convex hull from any collection of points (Vector3F).
DcelMesh convexHull = GeometryHelper.CreateConvexHull(points);
The DcelMesh is a doubly-connected edge list. This is a data structure that is used to build or manipulate meshes. It defines the mesh's vertices, edges, and faces (not necessarily triangles).
The returned DcelMesh can be converted to a TriangleMesh which consists only of triangles - a representation suitable for rendering.
TriangleMesh triangleMesh = convexHull.ToTriangleMesh();