Video thumbnail
Make Pasta
This tutorial explores one way equations can be used to make surfaces (and, as in our case here, meshes) in 3D modelling. To do this, we study a page in the unique and beautiful book Pasta by Design by George L. Legendre. In the first video of this series we make 2 ranges of integers and we pair them. We can think of these as similar to discrete pairs of 2 dimensional u, v coordinates falling on a surface. We then write 3 equations (created by George L. Legendre) that accept these 2 dimensional coordinates (one for X, one for Y, and one for Z). When we plug in our coordinate pairs we get points in 3 dimensional space that would fall on the surface of Legendre’s cavatappi.

Our next step is to make a mesh out of these points. We partition the list of points into branches and plug the resulting data tree into the C# script below.

Back to Pasta Mesh
Code
Below is the C# script we used to make a mesh from a DataTree of points. This is all of the code from our scripting component.
  
// Grasshopper Script Instance
using System;
using System.Collections;
using System.Collections.Generic;
using System.Drawing;

using Rhino;
using Rhino.Geometry;

using Grasshopper;
using Grasshopper.Kernel;
using Grasshopper.Kernel.Data;
using Grasshopper.Kernel.Types;

public class Script_Instance : GH_ScriptInstance
{
  private void RunScript(DataTree<Point3d> inputPts, ref object a)
  {
    // Write your logic here
    a = CreateMeshFromTree(inputPts);
  }

  Mesh CreateMeshFromTree(DataTree<Point3d> inputPts)
  {
    Mesh mesh = new Mesh();

    for (int i = 0; i < inputPts.BranchCount - 1; i++)
    {
      List<Point3d> currentBranch = inputPts.Branch(i);
      List<Point3d> nextBranch = inputPts.Branch(i+1);

      if (currentBranch.Count != nextBranch.Count) continue;

      for (int j = 0; j < currentBranch.Count - 1; j++)
      {
        Point3d pt1 = currentBranch[j];
        Point3d pt2 = currentBranch[j+1];
        Point3d pt3 = nextBranch[j+1];
        Point3d pt4 = nextBranch[j];

        int v1 = mesh.Vertices.Add(pt1);
        int v2 = mesh.Vertices.Add(pt2);
        int v3 = mesh.Vertices.Add(pt3);
        int v4 = mesh.Vertices.Add(pt4);

        mesh.Faces.AddFace(v1, v2, v3, v4);
      }
    }

    mesh.Vertices.CombineIdentical(true, true);

    return mesh;
  }
}
  
And, here is the code for the CreateMeshFromTree function only:
  
  Mesh CreateMeshFromTree(DataTree<Point3d> inputPts)
  {
    Mesh mesh = new Mesh();

    for (int i = 0; i < inputPts.BranchCount - 1; i++)
    {
      List<Point3d> currentBranch = inputPts.Branch(i);
      List<Point3d> nextBranch = inputPts.Branch(i+1);

      if (currentBranch.Count != nextBranch.Count) continue;

      for (int j = 0; j < currentBranch.Count - 1; j++)
      {
        Point3d pt1 = currentBranch[j];
        Point3d pt2 = currentBranch[j+1];
        Point3d pt3 = nextBranch[j+1];
        Point3d pt4 = nextBranch[j];

        int v1 = mesh.Vertices.Add(pt1);
        int v2 = mesh.Vertices.Add(pt2);
        int v3 = mesh.Vertices.Add(pt3);
        int v4 = mesh.Vertices.Add(pt4);

        mesh.Faces.AddFace(v1, v2, v3, v4);
      }
    }

    mesh.Vertices.CombineIdentical(true, true);

    return mesh;
  }
  
Resources
Legendre, G. L. (2011). Pasta by design. Thames & Hudson.