The latest Rhino 9 WIP contains a new method in RhinoCommon -
Mesh.CreateFromIsosurface
for creating meshes from isosurfaces, also known as implicit surfaces or level sets.
note - this post is only relevant for those using scripting or Grasshopper and is not about any new Rhino command.
An implicit surface is defined by a field or function which returns a number (a scalar value) for any point in 3d space, and an iso-value (typically 0). The surface is where the field crosses the given value (like contouring a heightfield, but in one dimension higher).
These scalar fields can be defined using distances, mathematical equations, or any other method you want to write which returns a value for any given point. This allows the creation of some types of surfaces which would be tricky to define directly using NURBS/SubD/Mesh.
While an implicit form fully defines a surface, and we can use functions of one implicit surface to define new implicit surfaces, to actually add it to the Rhino document and interact with it, we still need to convert it into an explicit form, in this case a mesh, and that is what this function does.
This is similar functionality to that found in the Isopod plugin, but now available as part of Rhino, so it can be used in scripting.
Here is a basic example of how to use this function in a C# script.
It asks the user to select a mesh, an inset distance and a resolution, and creates a new mesh which shells that mesh towards the inside, while leaving the outer surface unchanged.
IsoMeshExampleScript.cs (1.7 KB)
(You can run this using the Rhino Script Editor, found under Tools>Script>Edit)
using Rhino.Input;
using Rhino.Geometry;
using Rhino.DocObjects;
var doc = Rhino.RhinoDoc.ActiveDoc;
ObjectType filter = Rhino.DocObjects.ObjectType.Mesh;
ObjRef objref = null;
Rhino.Commands.Result rc = Rhino.Input.RhinoGet.GetOneObject("Select mesh to shell", false, filter, out objref);
double thickness = 1.0;
Rhino.Input.RhinoGet.GetNumber("Input thickness",true,ref thickness,0.1,100);
int resolution = 10;
Rhino.Input.RhinoGet.GetInteger("Input resolution",true,ref resolution,5,50);
if (rc == Rhino.Commands.Result.Success && objref != null)
{
Rhino.Geometry.Mesh mesh = objref.Mesh();
if (null != mesh)
{
Point3d check = mesh.ClosestPoint(Point3d.Origin);
//This is where the field is defined.
//It is a function which takes a Point3d as input and returns a double
double myField(Point3d p)
{
double d = p.DistanceTo(mesh.ClosestPoint(p)); //this gives us an unsigned distance field from the mesh
if(mesh.IsPointInside(p,0.001,false))
{
d=-d; //this turns it into a *signed* distance field, or SDF, where the value is negative inside the shape and positive outside
}
d+= thickness; //adding a constant scalar value to the field effectively insets the surface
return d;
}
BoundingBox bbox = mesh.GetBoundingBox(false);
Mesh isoMesh = Mesh.CreateFromIsosurface(myField,bbox,resolution,1);
//If we flip our new inset mesh and join it with the original input mesh, we get a solid shell with the same outer surface as the original
isoMesh.Flip(true,true,true);
isoMesh.Append(mesh);
doc.Objects.AddMesh(isoMesh);
doc.Views.Redraw();
}
}
Syntax:
Mesh CreateFromIsosurface(Func<Point3d, double> scalarFieldEvaluator, BoundingBox box, int resolution, int RootFindingMaxSteps)
Parameters:
scalarFieldEvaluator (type Func<Point3d, double>)
This is where the field you want to mesh is defined. It should return a double for any point in space.
box (type BoundingBox)
The volume within which the mesh will be generated. Note that the mesher does not currently create faces right at the boundary which would actually touch or cross the faces of this box, so it is sometimes useful to slightly expand the box using Inflate to ensure it contains your object with a bit of margin.
resolution (type int)
The resolution of the grid used when computing the isosurface. The number input is used as the number of divisions along the smallest dimension of the box, then the other 2 dimensions are divided to the same size. Higher numbers will capture finer details, but take longer to compute.
RootFindingMaxSteps (type int)
The maximum number of root finding iterations used when positioning each vertex of the mesh. A value of 1 gives linear interpolation, and is a good value to use most of the time, as it is typically sufficient for fairly ‘well behaved’ smoothly varying fields. Higher values can be useful for some more exotic field types with sharp variations in gradient or discontinuities, at the cost of slightly higher meshing time. There is rarely any benefit to going higher than 10.
There is also a new component for this in Grasshopper 2 in Rhino 9, compatible with the Grasshopper 2 field type:
