0

I have a list of triangles in 3D space and a point described with (x,y,z) coordinates. I am writing a method for returning the closest triangle to that point.

The naive implementation I wrote initially was to loop through all the triangles, check the distance from that point and then return the one with the minimum distance. In most cases though the list of triangles I am working with consists of thousands or tens of thousands of elements, so I am looking at ways of optimising it.

I have been trying to make it work using an octree structure, so I have created an octree that stores all the triangles. I thought that a possible approach would be to find the closest cell of the octree from that point by calculating the distance between the point and the center of each cell, and then just comparing with the triangles inside that cell.

I am not sure though of how to retrieve the closest cell from the octree (it's the first time I'm using an octree). This is the method I have written so far:

public Octree getClosestCell(final Vec3D point) {
    if (children != null) {
        float minDist = Float.MAX_VALUE;
        Octree closestCell = null;
        for (int i = 0; i < 8; i++) {
            final float dist = point.distanceTo(children[i].getCentroid());
            if (dist < minDist) {
                minDist = dist;
                closestCell = children[i];
            }
        }
        return closestCell.getClosestCell(point);
    } else {
        return this;
    }
}

So to sum up, I have 2 questions:

  1. Does the suggested approach sound like a good solution for optimising this problem?
  2. Does the method above seem correct or there is a better way of retrieving the closest cell?
leonidasarch
  • 85
  • 1
  • 9
  • How exactly do you store the triangles in the octree ? –  Nov 19 '21 at 14:00
  • It seems that you don't use the distances to the triangles but the distances to the centroids. This is a quite different question. –  Nov 19 '21 at 14:02
  • Yes, the idea is that I get the closest cell of the octree and then I call another method for getting the closest triangle from all the triangles inside that cell. I should probably update the title to something like 'finding closest octree cell from a point', but I thought it would be interesting to get any suggestions on how else this problem could be approached. – leonidasarch Nov 19 '21 at 16:05
  • How exactly do you store the triangles in the octree ? –  Nov 19 '21 at 16:51

0 Answers0