Yes, the MIConvexHull library provides a Delaunay triangulation algorithm that works for 2D, 3D, and higher dimensions.
The DelaunayTriangulation.cs file shows the Create method which takes a list of vertices and a plane distance tolerance as input and returns a DelaunayTriangulation object. The GetDelaunayTriangulation method in DelaunayTriangulationInternal.cs performs the actual computation, which internally uses a convex hull algorithm and then removes the "upper" faces of the convex hull to obtain the Delaunay triangulation. This method is generic, using TVertex and TCell, so it can be used for 2D, 3D, and higher dimensions.
User
What if I want to use this with RhinoCommon Points and Meshes?
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Okay, let's discuss how you can use MIConvexHull with RhinoCommon Points and Meshes.
Understanding the Integration
MIConvexHull works with its own IVertex interface for points and generic TriangulationCell for the faces of the triangulation. To use it with RhinoCommon, you'll need to bridge the gap between these structures:
Rhino Point to IVertex: You'll need to create a custom class that implements the IVertex interface and stores a Rhino.Geometry.Point3d. This class will be used to pass points to the MIConvexHull algorithm.
TriangulationCell to Rhino Mesh: After the Delaunay triangulation is created, you'll get a set of TriangulationCell objects. You'll need to convert these into a Rhino.Geometry.Mesh object.
Example Implementation
Here's a conceptual example demonstrating how you can do this:
[Redacted
using System;
using System.Collections.Generic;
using System.Linq;
using MIConvexHull;
using Rhino.Geometry;
// 1. Create a class that implements IVertex for Rhino points
public class RhinoVertex : IVertex
{
public double[] Position { get; set; }
public Point3d Point { get; set; }
public RhinoVertex(Point3d point)
{
Point = point;
Position = new double[] { point.X, point.Y, point.Z };
}
}
public class RhinoTriangulationCell : TriangulationCell<RhinoVertex, RhinoTriangulationCell>
{
}
public static class MIConvexHullRhinoExtensions
{
public static Mesh ToRhinoMesh(this IEnumerable<RhinoTriangulationCell> cells)
{
var mesh = new Mesh();
var vertices = new Dictionary<RhinoVertex, int>();
var faceIndex = 0;
foreach (var cell in cells)
{
var faceVertices = new int[cell.Vertices.Length];
for (int i = 0; i < cell.Vertices.Length; i++)
{
var vertex = cell.Vertices[i];
int vertexIndex;
if (!vertices.TryGetValue(vertex, out vertexIndex))
{
vertexIndex = mesh.Vertices.Add(vertex.Point);
vertices.Add(vertex, vertexIndex);
}
faceVertices[i] = vertexIndex;
}
if (faceVertices.Length == 3)
{
mesh.Faces.AddFace(faceVertices[0], faceVertices[1], faceVertices[2]);
}
else if (faceVertices.Length == 4)
{
mesh.Faces.AddFace(faceVertices[0], faceVertices[1], faceVertices[2], faceVertices[3]);
}
}
return mesh;
}
}
public class DelaunayTriangulationRhino
{
public static Mesh CreateDelaunayMesh(List<Point3d> rhinoPoints, double tolerance = Constants.DefaultPlaneDistanceTolerance)
{
if(rhinoPoints == null || rhinoPoints.Count < 4)
{
return new Mesh(); // Or throw exception as you see fit.
}
// 1. Convert Rhino Points to MIConvexHull vertices
var vertices = rhinoPoints.Select(point => new RhinoVertex(point)).ToList();
// 2. Create the Delaunay Triangulation
var triangulation = Triangulation.CreateDelaunay<RhinoVertex, RhinoTriangulationCell>(vertices, tolerance);
// 3. Convert Triangulation Cells to a Rhino Mesh
return triangulation.Cells.ToRhinoMesh();
}
}
Explanation
RhinoVertex Class:
Implements IVertex.
Stores a Rhino.Geometry.Point3d called Point for the actual geometry.
The Position property (required by IVertex) returns a double array that is derived from the Rhino Point3d.
RhinoTriangulationCell Class
Extends the TriangulationCell<RhinoVertex, RhinoTriangulationCell> abstract class from MIConvexHull
CreateDelaunayMesh Method:
Takes a list of Rhino.Geometry.Point3d points and a tolerance as input.
Converts the Rhino.Geometry.Point3d list into a list of RhinoVertex.
Calls Triangulation.CreateDelaunay to compute the Delaunay triangulation using the RhinoVertex objects.
Calls ToRhinoMesh which converts the TriangulationCell to a Rhino.Geometry.Mesh.
ToRhinoMesh extension method
Iterates through each cell in the triangulation
Adds each unique vertex to the mesh vertex list.
Creates a face by index
How to Use
[Redacted
// Example usage within your Rhino command/script:
List<Point3d> myPoints = new List<Point3d>
{
new Point3d(0, 0, 0),
new Point3d(10, 0, 0),
new Point3d(0, 10, 0),
new Point3d(5, 5, 10),
new Point3d(1, 1, 1),
new Point3d(9, 1, 1),
new Point3d(1, 9, 1),
};
Mesh delaunayMesh = DelaunayTriangulationRhino.CreateDelaunayMesh(myPoints);
if (delaunayMesh.IsValid)
{
// Do something with the mesh like adding it to the Rhino document
Rhino.RhinoDoc.ActiveDoc.Objects.AddMesh(delaunayMesh);
Rhino.RhinoDoc.ActiveDoc.Views.Redraw();
}
Important Considerations
Tolerance: The PlaneDistanceTolerance is critical. A small value is usually good. If your points are too close to the same plane, you may get errors.
Error Handling: Add more robust error handling (e.g., checks for null lists, invalid input).
Performance: MIConvexHull is generally very fast. However, if you're dealing with a huge number of points, you might want to investigate ways to optimize data structures and access.
Mesh Validation: The resulting mesh may not be perfect if the inputs are edge cases. Be sure to check for validity and repair if necessary.
Further Enhancements
Generalization: You could generalize the RhinoVertex class to accept any kind of Rhino.Geometry object (e.g. Point3f).
Voronoi Diagrams: You can extend these concepts to build Voronoi diagrams using the VoronoiMesh class in MIConvexHull.
Let me know if you'd like a more detailed example or have any more specific use cases in mind!