Fabulous @DanielPiker !
Thanks again.
Now… this is obviously very useful for all kinds of applications…
Can McNeel integrate your beautiful code in Rhino, and make a tool out of it, for easy usage?
A tool that works with curves, meshes and surfaces… called FlowAlongMesh
// Grasshopper Script Instance
// By Daniel Piker 250604
#region Usings
#r "Kangaroo2Component.dll"
using Kangaroo2Component.UtilityComponents;
using System;
using System.Linq;
using System.Collections;
using System.Collections.Generic;
using System.Drawing;
using System.Threading.Tasks;
using Rhino;
using Rhino.Geometry;
using Grasshopper;
using Grasshopper.Kernel;
using Grasshopper.Kernel.Data;
using Grasshopper.Kernel.Types;
#endregion
public class Script_Instance : GH_ScriptInstance
{
#region Notes
/*
Members:
RhinoDoc RhinoDocument
GH_Document GrasshopperDocument
IGH_Component Component
int Iteration
Methods (Virtual & overridable):
Print(string text)
Print(string format, params object[] args)
Reflect(object obj)
Reflect(object obj, string method_name)
*/
#endregion
private void RunScript(
Mesh M1,
Mesh M2,
List<Point3d> Pts,
double thickness,
ref object a,
ref object b)
{
Mesh offset = M2.DuplicateMesh();
OffsetMesh(ref offset, thickness);
Point3d[] ptsIn = Pts.ToArray();
Point3d[] ptsOut = new Point3d[ptsIn.Length];
Parallel.For(0, ptsIn.Length,i =>
{
Point3d Pt = ptsIn[i];
MeshPoint mp = M1.ClosestMeshPoint(Pt,0);
Point3f pa,pb,pc,pd;
M1.Faces.GetFaceVertices(mp.FaceIndex, out pa, out pb, out pc, out pd);
Polyline refPoly = new Polyline(new Point3d[4]{pa,pb,pc,pa});
M2.Faces.GetFaceVertices(mp.FaceIndex, out pa, out pb, out pc, out pd);
Polyline inner = new Polyline(new Point3d[4]{pa,pb,pc,pa});
offset.Faces.GetFaceVertices(mp.FaceIndex, out pa, out pb, out pc, out pd);
Polyline outer = new Polyline(new Point3d[4]{pa,pb,pc,pa});
var morph = new MorphToMesh.PolyPrism(refPoly, inner, outer);
ptsOut[i] = morph.MorphPoint(Pt);
});
a= ptsOut;
}
void OffsetMesh(ref Mesh M, double d)
{
M.Normals.ComputeNormals();
var normals = M.Normals.ToFloatArray();
for (int i = 0; i < M.Vertices.Count; i++)
{
double dist = d;
M.Vertices.SetVertex(i,
M.Vertices[i].X + dist * normals[3 * i],
M.Vertices[i].Y + dist * normals[3 * i + 1],
M.Vertices[i].Z + dist * normals[3 * i + 2]);
}
}
}