Copy Code
// Grasshopper Script Instance
using System;
using System.Collections;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
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(
List<double> in_params,
Curve in_curve,
bool wrap,
double weight,
int iterations,
ref object A,
ref object B)
{
List<double> parameters = new List<double>();
parameters = in_params.ToList();
if (wrap) parameters.Add(1.0);
List<Point3d> pts = new List<Point3d>();
pts = FindPoints(parameters, in_curve);
for (int i = 0; i < iterations; i++)
{
parameters = UpdateParameters(parameters, in_curve, weight);
}
pts = FindPoints(parameters, in_curve);
A = pts;
B = parameters;
}
public List<Point3d> FindPoints(List<double> p, Curve c)
{
List<Point3d> points = new List<Point3d>();
foreach (double par in p)
{
Point3d new_p = new Point3d(c.PointAtNormalizedLength(par));
points.Add(new_p);
}
return points;
}
public List<double> FindDistances(List<Point3d> p_list)
{
List<double> distances = new List<double>();
for (int i = 1; i < p_list.Count; i++)
{
double newDistance = p_list[i].DistanceTo(p_list[i - 1]);
distances.Add(newDistance);
}
return distances;
}
public List<double> UpdateParameters(List<double> pars, Curve in_c, double w)
{
List<double> new_pars = new List<double>();
List<double> dists = new List<double>();
List<double> norm_dists = new List<double>();
List<Point3d> new_pts = new List<Point3d>();
double crvLength = in_c.GetLength();
new_pars = pars;
new_pts = FindPoints(new_pars, in_c);
dists = FindDistances(new_pts);
foreach (double dis in dists)
{
double norm_dis = dis / crvLength;
norm_dists.Add(norm_dis);
}
for (int i = 1; i < new_pars.Count - 1; i++)
{
double difference = norm_dists[i] - norm_dists[i - 1];
new_pars[i] = new_pars[i] + (difference * w);
}
return new_pars;
}
}