Geometry.DistanceTo incorrect behaviour

Hi All ,

I have very interesting issue here and would like to get some help. Been spinning on this for few days now but cant seam to get solution.

I have 86 BBoxes and 1044 points that scattered around these boxes.

All im trying to do is to find Points closest to BB centre point and its partially working & not working at the same time.

but results i get if select points “From other” using “indexof” and draw a line to visuelize i have noticed that 50% are correct and there is issue where it seams checking against same point and results that 50% seams to get allocated to same point.

How can i fix this what do i do wrong here ?

i have used python as well but results are smae regardless if i use OTTB notes.

import math
from itertools import product

# Inputs: IN = Targets (List A), IN = Candidates (List B)
targets = IN[0]
candidates = IN[1]

def get_coords_safe(item):
    """Safely extracts X,Y,Z coordinates and original object, returns None if invalid."""
    if item is None: return None
    try:
        # Return coordinates and the original item reference as a tuple
        return (item.X, item.Y, item.Z, item) 
    except AttributeError:
        return None
    except Exception:
        return None

# Prepare Input B data once for performance
b_data = [get_coords_safe(c) for c in candidates]
b_data = [d for d in b_data if d is not None] # Filter all invalid candidates

results = []

# --- EXPLICIT CROSS-PRODUCT LOGIC ---

# Use itertools.product to generate every possible pair (Target, Candidate)
# We also use enumerate to keep track of the original list indices (idx_a and idx_b)
for idx_a, t_data in enumerate(targets):
    a_data = get_coords_safe(t_data)
    if not a_data: continue # Skip if Target A is invalid

    ax, ay, az, a_obj = a_data
    min_dist_sq = float('inf')
    best_idx_b = -1 # This tracks the original index of the winner in List B
    best_point_b = None
    
    # Iterate through every item in B again (the 'inner loop' of the cross product)
    for idx_b, b_data_tuple in enumerate(b_data):
        bx, by, bz, b_obj = b_data_tuple
        
        # SQUARED DISTANCE: Avoids expensive math.sqrt() during comparison
        d_sq = (bx - ax)**2 + (by - ay)**2 + (bz - az)**2
        
        if d_sq < min_dist_sq:
            min_dist_sq = d_sq
            best_idx_b = idx_b # Update the index of the winner
            best_point_b = b_obj
            
    # Finalise result for this target A item
    if best_idx_b != -1:
        dist = math.sqrt(min_dist_sq) # Only perform sqrt once
        # Result format: [Index A, Index B, Closest Point, Distance]
        results.append([idx_a, best_idx_b, best_point_b, dist])
    else:
        results.append([idx_a, -1, None, None])

OUT = results

Thanks In advance

Regards

Here’s another way of doing this


1 Like

Thanks for your time @Mike.Buttery

I have tried this method as you suggested but. I do require to get all closest points and this only seams to pick one per BB.

How can you check all closest points against BB?

Thanks Again

Regards

All closest points doesn’t really make sense.
You might ask the 5 closest points for example. Then you would sort the list and then get the 5 first item.

1 Like

@Darozas your question isn’t clear here.

Do you want to find the point nearest the center of the bounding box? Or the N points nearest the center of each bounding box?

Or do you want to find the bounding box nearest each point?

All are achievable without issue, but we need to know what to help guide you along. A sketch and dataset would go a LONG way in that respect.

Hi @jacob.small ,

Im trying to get Number of points closest to BB centre point. Number of point varies on each BB.

Im partially there but not there at the same time as i have this weird behaviour.

This is how intended result should look like Marked in red it works 50% but seams like some points just reference to same BB center hence i cant figure out why its doing it or its me doing wrong here.

Thanks for your time

Regards

Question still isn’t clear.

Do you want to group the points by the bounding box they are closest to?

Hi @jacob.small ,

yes i want to have points on nested list per each BB please.

Regards

  1. Centroids will lead to false positives; use cuboids instead.
  2. Get the distance from each point to every bounding box.
  3. Build a range from 0 to one less than the number of bounding boxes.
  4. Sort the range by each collection of distances.
  5. Get the first item in each sorted list of ranges.
  6. Group the points by the associated first item from the list of ranges.
  7. Sort the lists of grouped points by the unique keys.

All together it looks something like this (with some coloring thrown in to help illustrate the result):

Note you will still want to deal with edge cases (i.e. a point on the limit of bounding box 1 and bounding box 2; a point N units from bounding box 2 and bounding box 3, etc.).

3 Likes

Thanks @jacob.small

Thanks for your time just tried your method and got same results not sure why its happening like this.

its seams that it getting stuck on one point as i have list of 200 items and i dont have BB with that many points around it.

Regards

Specific dataset problems require access to the dataset or we can’t build the solution.

Post your RVT and DYN?

1 Like

Can i send you files on DM as i cant share revit & IFC files publicly.

dyn is fine no problem to share publicly

Sadly private support requires a consulting project via my employer (Autodesk), and I’m assuming you’d have reached out via those channels if you were working on such a project. General community knowledge contribution in the open forum is doable though.

To get around this, how about you pack the points into a Data.Remember node, then pack the bounding boxes into another Data.Remember node. Then save your graph as a new file, delete EVERYTHING leading into the Data.Remember nodes, and post that file. It’ll put points and bounding boxes into the public domain, but keep anything project specific out of circulation.

1 Like

Hi @jacob.small ,

Thanks again please see attached

Regards

DistancetoNotWorking.dyn (207.1 KB)

I think i have fund a problem .Ill be back when i narrow it down