Isovist optimization

@gankeyu and @RIL

Thank you so much for your help. :pray:
with your help my code went from 34ms to just 3ms.

here is the code:

private void RunScript(List spaces, Polyline boundary, int nOfRays, double rad, List spaceStrings, string desiredView, string streetView, ref object IsoVist, ref object CrvTree)
{
// var crvTree = new DataTree();
//add boundary to test
var obstacleCrvs = new List();
obstacleCrvs.AddRange(spaces);
obstacleCrvs.Add(boundary);

//duplicating tree while removing test point
Line[][][] crvarr = new Line[obstacleCrvs.Count - 1][][];

for (int i = 0; i < obstacleCrvs.Count - 1 ; i++)
{
  var newCrvs = new List<Polyline>(obstacleCrvs.Count);
  newCrvs.AddRange(obstacleCrvs);

  newCrvs.RemoveAt(i);
  var polylineArr = new Line[newCrvs.Count][];

  for(int j = 0; j < newCrvs.Count ; j++)
    polylineArr[j] = newCrvs[j].GetSegments();

  crvarr[i] = polylineArr;
}
//-----------------
//creating isovist
//-----------------
var centers = spaces.Select(x => x.CenterPoint()).ToList();
var angle = (Math.PI * 2) / nOfRays;
Point3d[][] isoVistArr = new Point3d[centers.Count][];
var xVector = new Vector3d(1, 0, 0);
var rotatedVectors = new List<Vector3d>();
//creating lines for each center
var lineTree = new DataTree<Line>();
var lineArr = new Line[centers.Count][];
for(int u = 0; u < centers.Count;u++)
{
  var lines = new Line[nOfRays];
  for(int i = 0;i < nOfRays;i++)
  {
    xVector.Rotate((Math.PI * 2) / nOfRays, new Vector3d(0, 0, 1));
    rotatedVectors.Add(xVector);
    var line = new Line(centers[u], xVector, rad);
    lineTree.Add(line, new GH_Path(u));
    lines[i] = line;
  }
  lineArr[u] = lines;
}
//intersect rays with curves
for(int u = 0; u < centers.Count;u++)
{
  Point3d[] isoVistArrBranch = new Point3d[nOfRays];
  for(int i = 0;i < nOfRays;i++)
  {
    var orderPoints = new List<Point3d>();
    for(int j = 0; j < crvarr[u].Length ;j++)
    {
      for(int k = 0; k < crvarr[u][j].Length ;k++)
      {
        double a;
        double b;
        bool curveLinIntersects =
          Rhino.Geometry.Intersect.Intersection.LineLine(crvarr[u][j][k], lineArr[u][i],
          out a, out b, RhinoDocument.ModelAbsoluteTolerance, true);
        if(curveLinIntersects)
          orderPoints.Add(crvarr[u][j][k].PointAt(a));
      }
    }
    Print(orderPoints.Count.ToString());
    //        var ptToAdd = orderPoints.OrderBy(x => x.DistanceTo(centers[u])).FirstOrDefault();
    var ptToAdd = Rhino.Collections.Point3dList.ClosestPointInList(orderPoints, centers[u]);
    isoVistArrBranch[i] = ptToAdd;
  }
  isoVistArr[u] = isoVistArrBranch;
}
//display isovist
var isoVist = new DataTree<Point3d>();
for (int i = 0; i < isoVistArr.Length; i++)
  isoVist.AddRange(isoVistArr[i], new GH_Path(i));
//    //display crvTree
var crvTree = new DataTree<Line>();
for (int i = 0; i < crvarr.Length; i++)
{
  for (int j = 0; j < crvarr.Length; j++)
  {
    crvTree.AddRange(crvarr[i][j], new GH_Path(i, j));
  }
}
//
CrvTree = crvTree;
IsoVist = isoVist;

}

I hope it helps you. If you have any improvments please don’t hesitate to add them.
Thanks again.

2 Likes