RTree search Overlaps

Don’t know if I should post this here or another group, if this is the wrong group feel free to move the topic. I am scripting some stuff in Grasshopper, but basically the questions refers rather to Rhino.Common objects.

I have the following code to find the overlap between two RTrees:

// class level variables
private List<int> index1;
private List<int> index2;

// Callback Function
void SearchCallback(object sender, RTreeEventArgs e)

// ... this happens inside a method
this.index1 = new List<int>();
this.index2 = new List<int>();
double tol = 1.0E-3;
RTree tree1 = new RTree();  
RTree tree2 = new RTree();

// ...
// some code to fill the trees

RTree.SearchOverlaps(tree1, tree2, tol, SearchCallback);

So this all seems to works OK, sometimes many intersections are found, sometimes none, but still two questions:

  1. Why does the SearchOverlaps function always return false ? This would mean not the entire tree had been searched, but doesn’t seem to be the case
  2. For now I get my results using class level variables which are assigned in the callback function, is this the way this is supposed to work? The other search functions have an object argument in the function header, which allows to pass around search data.


I do not know if this helps but this is how I use RTree. Do not know much about overlaps though. Wondering what is the application goal to use two rtrees instead of one?

 private void RunScript(List<Line> lineList, double radius, int item, ref object A, ref object B)

Component.Message = "RTee";

lineList2 = lineList;

RTree tree = new RTree();

for(int i = 0; i < lineList.Count; i++)
  tree.Insert(lineList[i].PointAt(0.5), i);

tree.Search(new Sphere(lineList[item].PointAt(0.5), radius), method);

A = cl;
B = ci;


// <Custom additional code> 
List<Line> cl = new List<Line>();
 List<int> ci = new List<int>();
List<Line> lineList2 = new List<Line>();

private void method(object sender, RTreeEventArgs e){

I have two lists of custom node objects, want to know if and how many items in both lists are at the same location (i.e. within overlap tolerance as per definition of the SearchOverlap method).

The method which i described above seems to work fine, just don’t understand the return value of the Search function (a bug maybe?) and why there is no better way to pass around data, don’t like this approach with class level variables much in this case.

Hi @dsonntag,

Without some source we can run here, there isn’t much we can do.

However, this example (see attached) seems to function as expected.

TestDaniel.cs (4.8 KB)

– Dale

OK, well thanks already, I’ll have a more detailed look into the example you sent next week, this already answers the second question that filling out class level variables is the way to go :wink:

For the first question, I tried to create a reduced version of this in a script component, my question is actually: why does the SearchOverlaps method return false (and when would it return true?). It seems to have run through both trees (as the result yields multiple items), shouldn’t then the return value be true?

testTree.gh (10.8 KB)

OK, also in your example

   var rc = check.BoxCheck(doc.ModelAbsoluteTolerance);

always returns false, independently if there are 0, 1 or multiple interference events. So my question remains why this is so and when it would return true?

Hi @dsonntag,

I my simple tests, my 'BoxCheck` function returns true. Can you provide a sample (code and geometry) and step-by-step instructions need to repeat what you are seeing?

– Dale

Well for example the .gh-Definition attached in my previous post produces this:

Alternatively I take your example file, compile it (I have to replace the Parallel.For with a standard for loop, and a RhinoApp.WriteLine(rc.ToString()) ) and execute it with this bogus file as attached.

test.3dm (32.4 KB)

Or if move it to a .gh-File again, which I am more familiar with, I still get false as a result

testMeshInterference.gh (5.9 KB)

Could this be a version issue or something which has been fixed with RH6 already ?

Nobody ?