Grasshopper

algorithmic modeling for Rhino

Hi

I am writing some boid class and i tried to introduce some Rtree to speed up attraction functions.

Unfortunately, the documentation is a bit cryptic to me and I would be happy if someone helped me with some simple example of how to use Rtree. For example to search for points (and find ther indexes) that are within some distance

Views: 1617

Replies to This Discussion

Perhaps this helps you? This is a simple example that finds points and their indices. (Example included for a set of 10.000 points)


private void RunScript(List pointList, Point3d startPoint, double radius, ref object A, ref object B)
{
try {
RTree tree = new RTree();
// first insert all points.
for (int i = 0; i < pointList.Count; i++)
{
// add a point, and a number, as a reference for this point.
// this can also be a boundingbox.
tree.Insert(pointList[i], i);
}
// we'll add our matches to this list.
List closestPoints = new List ();
List closestIndices = new List ();
tree.Search(new Sphere(startPoint, radius), (object sender, RTreeEventArgs events) => {
// this will be executed for each point that matches the search criteria
RTreeEventArgs e = events;
// look up which point this is.
closestPoints.Add(pointList[e.Id]);
closestIndices.Add(e.Id);
});
A = closestPoints;
B = closestIndices;
} catch (Exception e)
{
throw new Exception(e.Message + " " + e.StackTrace);
}
}
Attachments:

Interesting. This are the headaches of optimisation - which optimisation technique works best for which problem?

I can't check right now, but in most cases if it's the first time you execute a C# block it's the compilation of the block that takes the most time. Also - most of the time when I use RTree it's for the bounding box cases (might two objects possibly intersect, or not?), it might very well be that the overhead of building of a rtree for a simple one to many proximity search is always higher than a brute force method.

Hi Arend,

Thanks for your above example.  I am also new to C#, and your code has been very useful.

I was wondering, what would be the simplest way to modify this code in order to use it with a list of points rather then a single point.

So, for each point in ListA, find the closest point in List B(within specified radius).
OR, if i can find for each point in ListA, the closest points in ListB(within specified radius) in order of closest distance.  


i know i need to search for each point of ListA (iMeshPoints)... but then i am lost...

for (int i = 0; i < iMeshPoints.Count; i++)
{}

Hope it hasn't been too long since you've posted... Thanks! 

Hi Jon,

I'd be happy to answer this question, but would you mind re-posting your question in the current forum at https://discourse.mcneel.com ? Then it's easier to answer, and also accessible for all other users.

RSS

About

Translate

Search

Photos

  • Add Photos
  • View All

Videos

  • Add Videos
  • View All

© 2024   Created by Scott Davidson.   Powered by

Badges  |  Report an Issue  |  Terms of Service