3

I have multiple faces in 3D space creating cells. All these faces lie within a predefined cube (e.g. of size 100x100x100). Every face is convex and defined by a set of corner points and a normal vector. Every cell is convex. The cells are result of 3d voronoi tessellation, and I know the initial seed points of the cells.

Now for every integer coordinate I want the smallest distance to any face.

My current solution uses this answer https://math.stackexchange.com/questions/544946/determine-if-projection-of-3d-point-onto-plane-is-within-a-triangle/544947 and calculates for every point for every face for every possible triple of this faces points the projection of the point to the triangle created by the triple, checks if the projection is inside the triangle. If this is the case I return the distance between projection and original point. If not I calculate the distance from the point to every possible line segment defined by two points of a face. Then I choose the smallest distance. I repeat this for every point.

This is quite slow and clumsy. I would much rather calculate all points that lie on (or almost lie on) a face and then with these calculate the smallest distance to all neighbour points and repeat this. I have found this Get all points within a Triangle but am not sure how to apply it to 3D space.

Are there any techniques or algorithms to do this efficiently?

mxcx
  • 75
  • 8
  • Does “Now for every integer coordinate I want the smallest distance to any face” mean that, for each point P with integer coordinates, you want the distance d that is the smallest distance in the set { d is the distance from P to F | F is one of the faces } or that, for each point P with integer coordinates, you want the distance from P to F, where F is given as any one of the faces? Or something else? – Eric Postpischil Jul 04 '22 at 13:27
  • 1
    Re “I have multiple faces in 3D space creating cells”: What is a cell? Do the 3D faces form a closed volume? Is that a cell, a portion of three-dimensional space completely enclosed by faces? Is there more than one cell? Do they intersect? Do the faces share borders and form cells with no parts extending “uselessly” off beyond a cell? (I am not sure these questions are necessary for finding a solution; I am just trying to clarify the situation for analysis.) – Eric Postpischil Jul 04 '22 at 13:31
  • @EricPostpischil I want the smallest distance from point P to any of the existing faces. A cell is completely enclosed by faces. Cells are divided by faces. Any point is ether within a cell or on a face. There are multiple cells. (In my case about 4 to 16, but I think that doesn't really matter) – mxcx Jul 04 '22 at 13:42
  • English is ambiguous. You just answered the question with similar phrasing as before; it has the same ambiguity. I used mathematical notation and language to eliminate ambiguity. “I want the smallest distance from point P to any of the existing faces” could mean “I want, in effect, a function f that takes a point P, iterates over each face F, calculates the distance from P to F, and returns the smallest such distance.” Or it could mean “I want a function f that takes a point P and any face F and returns the distance from P to F.” – Eric Postpischil Jul 04 '22 at 13:53
  • Is the 3D figure convex? The faces all being convex is not a sufficient condition for that, and it may make a difference. – John Bollinger Jul 04 '22 at 13:55
  • Are the "cells" actually relevant to the problem posed? If so, how? – John Bollinger Jul 04 '22 at 13:58
  • @EricPostpischil My bad: "I want, in effect, a function f that takes a point P, iterates over each face F, calculates the distance from P to F, and returns the smallest such distance." is what i meant – mxcx Jul 04 '22 at 14:03
  • @JohnBollinger the cells are convex, but I don't think they are really relevant to the problem – mxcx Jul 04 '22 at 14:04
  • So we'll forget about the cells, but my first question was about whether *the overall figure* is convex. – John Bollinger Jul 04 '22 at 14:19
  • @JohnBollinger No, my data structure is result of a 3d voronoi tessellation inside a given cube – mxcx Jul 04 '22 at 14:39
  • @JohnBollinger: I suspect what they have is the entire volume is partitioned into cells, so each point in the volume is either in the interior of a cell or in a border between multiple cells (there are no points not in some cell or its border nor any points in multiple cells other than a border), and all the faces are the borders between cells (no face has any part that just “sticks out” from cell borders, having interior points of one cell on both sides of it). There is probably some mathematical term for that, but I do not know what it is. – Eric Postpischil Jul 04 '22 at 14:39
  • 2
    @mxcx: If you have a 3D Voronoi tessellation, do you have information about which cell each point is a member of? Because the closest face to a point must be one of the faces of the cell it is inside. – Eric Postpischil Jul 04 '22 at 14:44
  • Ah, now that makes more sense. And in that case, we absolutely *should not* forget the cells. – John Bollinger Jul 04 '22 at 14:44
  • Or do you at least know the points from which the tesselation was generated? Because you can determine the cell of any point in the space by computing the distances from it to the generator points. – John Bollinger Jul 04 '22 at 14:46
  • @JohnBollinger yes I know the initial points. But why are these points or the cells relevant? Doesn't it come down to the faces? – mxcx Jul 04 '22 at 14:51
  • "Now for every integer coordinate": do you mean for every grid point (one million of them) ? –  Jul 04 '22 at 14:51
  • 1
    Ahh ok nvm. I could calculate the closest seed point for the cells and then I know in which cell I must be – mxcx Jul 04 '22 at 14:52
  • @YvesDaoust yes exactly – mxcx Jul 04 '22 at 14:53
  • Then it is not impossible that the problem can be addressed the other way round: for every face, enumerate all grid points that a not farther than one cell away (this is a process similar to raster-scan conversion as done in 2D). And for every grid point, keep the closest distance. –  Jul 04 '22 at 14:56
  • @YvesDaoust I would like to do this very much. But how do I get the initial points (minDist < 1) – mxcx Jul 04 '22 at 15:01
  • 1
    Well, a simple approach is to use the axis-aligned bounding box of every face, plus a unit margin, and consider all grid points inside it. You still have to compute the distance of every point to the face. But you can quickly get rid of a number of them by just checking the distance to the plane of the face. I am not sure that it is worth refining the rejection tests. –  Jul 04 '22 at 15:11

1 Answers1

1

Since we're working with a Voronoi tessellation, we can simplify the current algorithm. Given a grid point p, it belongs to the cell of some site q. Take the minimum over each neighboring site r of the distance from p to the plane that is the perpendicular bisector of qr. We don't need to worry whether the closest point s on the plane belongs to the face between q and r; if not, the segment ps intersects some other face of the cell, which is necessarily closer.

Actually it doesn't even matter if we loop r over some sites that are not neighbors. So if you don't have access to a point location subroutine, or it's slow, we can use a fast nearest neighbors algorithm. Given the grid point p, we know that q is the closest site. Find the second closest site r and compute the distance d(p, bisector(qr)) as above. Now we can prune the sites that are too far away from q (for every other site s, we have d(p, bisector(qs)) ≥ d(q, s)/2 − d(p, q), so we can prune s unless d(q, s) ≤ 2 (d(p, bisector(qr)) + d(p, q))) and keep going until we have either considered or pruned every other site. To do pruning in the best possible way requires access to the guts of the nearest neighbor algorithm; I know that it slots right into the best-first depth-first search of a kd-tree or a cover tree.

David Eisenstat
  • 64,237
  • 7
  • 60
  • 120