Looking for the Perfect Crust
In the last post, I had generated a point cloud representing the diffusion between two states of matter. How do I turn this into geometry? I need to form some sort of "crust" on the outside of the points.
This was the original point cloud.

A 3d lipid simulation
There was a Stack Overflow article where someone asks this same question. [1]. There are two ways to do this: to calculate a geometry from the points, or to create an implicit geometry using the marching cubes algorithm, or glorified voxels. Not wanting to create design that looks like it came from Minecraft, I looked at the computational geometry approach. The best-in-class algorithm for this is called powercrust. It finds the median of the structure. This is a good algorithm, but I searched and found a simple algorithm that would do the job: the Point Cloud Crust algorithm AKA the Cheese-maker algorithm. It is very simple, if not efficient.[2]
The Cheese-Maker Algorithm
Given a collection of points, for every combination of three points, that forms a triangle. You draw a circle through the points (actually a big one and a small one, but in this implementation in RhinoCommons I think it only takes the small one). If there are points inside the sphere, it must be an interior point, so the triangle is discarded. But if there are no other points within the circumscribed sphere, it must be the crust.

a diagram explaining the point cloud crust algorithm
What the author of the algorithm failed to mention was that it has a guaranteed complexity of O = n^3! It is a naive, brute force algortithm. Unlike other algorithms like powercrust, Point Cloud Crust does not guarantee watertight geometry. But it is almost trivially simple, so I set out to implement it.
Implementing the Algorithm as a Grasshopper Plugin
I wrote a grasshopper plugin to implement the algorithm. My repo is here [3].
It is brief.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
//Point Cloud Crust "Cheesemaker" Algorithm. //Select three points at random that aren't too far apart. foreach (Point3d i in inputPoints) { foreach (Point3d j in inputPoints) { foreach (Point3d k in inputPoints) { //because it is the order of N^3, we need to reduce the number of points to look at by omitting obviously large triangles that are far apart. if (i != j && j != k && k != i && i.DistanceTo(j) < maxDistance && j.DistanceTo(k) < maxDistance) { var thesePoints = new List<Point3d>(); thesePoints.Add(i); thesePoints.Add(j); thesePoints.Add(k); bool isInside = false; //generate a single sphere formed by the three points, Rhino.Geometry.Brep thisSphere = Rhino.Geometry.Sphere.FitSphereToPoints(thesePoints).ToBrep(); foreach (Point3d p in inputPoints) { //the points in the triangle should not be included, or nothing will come of it. if (p == i || p == j || p == k) { continue; } //if there are points inside the sphere, it is not the crust. Do not add geometry if (thisSphere.IsPointInside(p, 0, true)) { isInside = true; break; } } if (!isInside) //it is a crust. Add the face { Mesh thisTriangle = new Triangle3d(i, j, k).ToMesh(); outputMeshes.Add(thisTriangle); } } } } } |
Easy enough to write.
From Mesh to Model
Let's look at the result.

It looks like a valid mesh, but it isn't watertight, and it is too jagged to be used in jewelry.
I smoothed it in Rhinoceros

result of cheesemaker smoothed
Still not there. So I took it into ZBrush, create a unified skin, and smoothed it manually with a large brush.

The result of the Cheesemaker algorithm after post-processing in Zbrush
Then I rendered it in keyshot.

The result of the cheesemaker-algorithm as silver metal.

cheesemaker algorithm in silver.
That isn't bad. I would put that on my desk at work.
If I scale the model up, and use Rhino to trim the points, to make something like a bracelet, it could be cool.
Revisions
The complexity is horrendous, but let's make it worse! While it uses the method, for a triangle there are two possible spheres, one on each side of the normal, while this method only generates one. To force a triangle on either side of the normal, I find the normal of the triangle's Plane and multiply it by 1.001, to get a slight bias in one direction, and apply this transform to a copy of the vertecies, and add these six verticies to the shape. This controlls which side of the triangle the sphere appears.Rhino.Grasshopper.Sphere.FitSphereThroughPoints()
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
foreach (Point3d i in inputPoints) { foreach (Point3d j in inputPoints) { foreach (Point3d k in inputPoints) { if (i.DistanceTo(j) > maxDistance || j.DistanceTo(k) > maxDistance) { continue; }; // make sure the point being checked is not a corner of the triangle if (i == j || j == k || k == i) { continue; }; { continue; } //if there are points inside the sphere, it is not the crust. Do not add geometry //sometimes it draws the sphere in, sometimes it draws the sphere out. Draw it in and out by adding points in the direction of the normal and the reverse of the normal. rg.Plane thisPlane = new rg.Plane(i, j, k); Vector3d thisNormal = thisPlane.Normal; thisNormal = rg.Vector3d.Multiply(thisNormal, 1.001); var xformIn = Transform.Translation(thisNormal); thisNormal.Reverse(); var xformOut2 = Transform.Translation(thisNormal); bool shouldMakeOut = ShouldIMakeATriangle(inputPoints, i, j, k, xformOut2); bool shouldMakeIn = ShouldIMakeATriangle(inputPoints, i, j, k, xformIn); if (shouldMakeOut || shouldMakeIn) { Mesh thisTriangle = new Triangle3d(i, j, k).ToMesh(); outputMeshes.Add(thisTriangle); } |
I broke-out the triangle sphere operation into its own function that takes an xform as an input to control the flip of the sphere.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
bool ShouldIMakeATriangle(List<Point3d> inputPointsLocal, Point3d firstPoint, Point3d secondPoint, Point3d thirdPoint, Transform xform) { Point3d offsetA = new Point3d(firstPoint); Point3d offsetB = new Point3d(secondPoint); Point3d offsetC = new Point3d(thirdPoint); offsetA.Transform(xform); offsetB.Transform(xform); offsetC.Transform(xform); List<Point3d> thesePoints = new List<Point3d>() { firstPoint, secondPoint, thirdPoint, offsetA, offsetB, offsetC }; bool isInside = false; Brep thisSphere2 = Rhino.Geometry.Sphere.FitSphereToPoints(thesePoints).ToBrep(); if(thisSphere2 == null) { return false; } foreach (Point3d p in inputPointsLocal) {//the points in the triangle should not be included, or nothing will come of it. The offset points are not included in inputPointsLocal so are not counted. if (p.DistanceTo(firstPoint) < 0.1 || p.DistanceTo(secondPoint) < 0.1 || p.DistanceTo(thirdPoint) < 0.1 ) { continue; } //if there are points inside the sphere, it is not the crust. Do not add geometry. { if (thisSphere2.IsPointInside(p, 0, true)) { isInside = true; break; } } } if (!isInside) { //it is a crust. Add the face return true; } else return false; } |
Well now it never completes. It is just too many points for On^3.
Optimizing Point Cloud Crust
How can we reduce the O(n^3) complexity? In the original algorithm, the algorithm explores every possible face that can be made with a collection of points. But the only faces needed are those that connect to each other. This can be found through by finding the Delaunay Triangulation. [[4]][delaunay wiki]. The triangulation of each vertex is the convex hull of the nearest neighbors projected into in a 4-dimensional paraboloid. I implement an off-the-shelf solver from EdgeMesh that implements MIConvexHull by David Sehnal, Matthew Campbell.
Delaunay triangulation scales practically at O(n) in time and space complexity, vastly improving upon O(n^3). It also is capable of looking at all the pertinent faces. I put a maximum distance on the faces to prevent the large planes of the outer edges of the Delaunay triangulation from forming.
The code is the same as before, but instead of three nested for loops, this is how the faces are taken:
|
1 2 3 4 5 6 7 8 9 10 11 12 |
foreach (Mesh t in inputCells) { MeshFaceList faces = t.Faces; var vs = t.Vertices; foreach (var face in t.Faces) { //cast Point3f to Point3d. 'f' is for float (single precision), 'd' is for double precision. // face.A is the index of Corner A of the face. var firstPoint = new Point3d(vs[face.A]); var secondPoint = new Point3d(vs[face.B]); var thirdPoint = new Point3d(vs[face.C]); ... |
Delaunay-Optimized results
The Delaunay optimization and the increase the maximum size of triangles, and the double-sided spheres allow more geometry to come through.



The efficiency allows for even larger simulations. The cross-section certainly resembles the original spinodal decomposition patterns I was aiming for, without the differential equations.
[](Lipid simulation looks like spinodal decomposition "Lipid simulation looks like spinodal decomposition")

The results of the Point Cloud Crust Cheesemaker Algorithm, but larger
Conclusion
In the previous post I created diffusion patterns in NetLogo. In this post I turned a cloud of points into geometry by implementing the Point Cloud Crust aka "Cheesemaker" algorithm. The original Point Cloud Crust "Cheesemaker" algorithm was O(n^3), a naive implementation making it impractical for sets over 1000 points. By applying the cheesemaker algorithm on the Delaunay triangles the time complexity is reduces to O(n).
The next steps are to build out my library to implement other crusting algorithms, such as the marching cubes algorithm to find implicit geometry, and other computational geometries such as PowerCrust. When I have a few more algorithms, I may release it as a public library.
Leave A Reply