# How to find minimum distance between two polygons in 3D space

This topic is 1977 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

After some hours of search and some implementations that doesn't work I hope on help of people that know more about this topic.

I need this on a iterative radiosity renderer, using gathering method, writed in C# using GPU (with XNA), to speed up the rendering.

For each polygon I have a lightmap [b]and a list of lightmaps visible from it[/b]. That's work ok.
The idea is that when[b] all lightmaps in "visible list" [/b]have an average energy less than a thresold I can skip the light gathering for the entire polygon's lightmap.

In a first time I've considered only the average energy, but I find that accounting for distance (or better the square of the distance, that is the physical decay of light) i can rescale the average energy and check against the threshold to do a better filtering:
[code]
public void GatherEnergy(float accuracy, ref Hemicube h, ref SceneRenderer scene)
{
bool skip = true;
for (int i = 0; i < VisibleLightMaps.Length; ++i)
{
if ((VisibleLightMaps[i].lightmap.AverageEnergy / VisibleLightMaps[i].distSquared) > energyThreshold)
{
skip = false;
break;
}
}
if (skip)
return;
if (ForDetailMesh)
GatherEnergyForDetailMesh(accuracy, ref h, ref scene);
else
GatherEnergyForPoly(accuracy, ref h, ref scene);

CalculateMaxAndAverageEnergy();
}
[/code]

NOTE: the distance I need is the [b]real minimun distance[/b] (or distance squared) between the polygons. The points that define the distance segment can be on a vertex, on edge, or inside the polygon.

I hope to be clear.

##### Share on other sites
I have to ask, are you sure you need the exact minimum distance? This could turn out to be zero if they intersect, which would cause problems for the division and comparison. This would happen frequently at the corners of rooms for example. Another question is what kind of polygons we're talking about. Triangles may have shortcuts not applicable to polygons made from arbitrary vertices, which may not even be co-planar due to rounding errors or malformation. And finally, for performance is it more important to have an exact answer or just a heuristic which always errs on a known side? This code will be run quite frequently, so you wouldn't want it to be slow. For arbitrary polygons an estimate based on an axis-aligned bounding box or a centroid-to-plane test would likely be much faster than an exact test.

##### Share on other sites
I'm sorry, it's true....

The [b]polygons are convex and planar and the vertices are ordered counter-clockwise[/b].

I already have an algo to check if two convex polys intersects so I can filter it out and return zero distance, otherwise I will use the algorithm I search for.

The distance must be [b]correct or at least underestimated[/b]. If the distance returned if bigger than real the contribution of lightmap is skipped making illuminations artifacts (darker regions).

I not search for speed, I precalculate these distances only once during radiosity setup.

[quote]I have another question now, a radiosity solution based on hemicubes is automatically unit-less?
I've thinked that the inverse square law is due to the area that a polygon fill onto the hemicube faces, so the perspective automatically reduce this.... and so the units of the scene are not important.
But now, misuring the distance directly from polys, i think i need to convert units it to meters to work well?[/quote]
I've partially solved, the assertions made on the box above are wrong i need to check for solid angle and thus accounting for the square of distance between hemicube origin and patch center.
I have also set an UnitsToMeters float to define the scene scale. Now it's all more correct.

The distance algorithm [b]is always needed[/b]... Edited by Koder4Fun

##### Share on other sites
I second jefferytitan. You should (if I understand the algorithm correctly) be able to get away with an approximation.

But if you *really* need the _exact_ distance between two polygons you should look into Minkowski Differences.

##### Share on other sites
If speed's not a huge concern, perhaps the below would work:
[url="http://cgm.cs.mcgill.ca/~orm/mind2p.html"]http://cgm.cs.mcgill.ca/~orm/mind2p.html[/url]
[url="http://en.wikipedia.org/wiki/Rotating_calipers"]http://en.wikipedia.org/wiki/Rotating_calipers[/url]

Personally I'd just go the AABB way for simplicity and speed, and then optimise if it's needed. Assuming you don't use single huge polygons, the chances of AABBs overlapping for arbitrary polygons should be pretty low.

##### Share on other sites
As an estimation (max bound), you can compute the minimum of all nine distances between the vertices of the triangles. Then, filtered by a threshold, you can choose candidates and compute the real distance between each vertex of one triangle and the plane that represents the other triangle.

##### Share on other sites
Would not the best way be to use standard quadratic programming tools for the job?
I mean there should be some fast algorithms to solve the problem,

##### Share on other sites
Thanks to all guys!
You've got me some differents solutions to the problem, now I test what fit better for me.

[quote name='pacobarter' timestamp='1345415093' post='4971244']
As an estimation (max bound), you can compute the minimum of all nine distances between the vertices of the triangles. Then, filtered by a threshold, you can choose candidates and compute the real distance between each vertex of one triangle and the plane that represents the other triangle.
[/quote]
I do this (I work with polys not triangles, but basically is the same).
Also I check for distance from center to center of the two polygons.
The only distance that I need to check is when the minimum distance is made by a segment with endpoints inside the polygons.

[quote name='jefferytitan' timestamp='1345406263' post='4971194']
If speed's not a huge concern, perhaps the below would work:
[url="http://cgm.cs.mcgill.ca/~orm/mind2p.html"]http://cgm.cs.mcgill...orm/mind2p.html[/url]
[url="http://en.wikipedia.org/wiki/Rotating_calipers"]http://en.wikipedia....tating_calipers[/url]

Personally I'd just go the AABB way for simplicity and speed, and then optimise if it's needed. Assuming you don't use single huge polygons, the chances of AABBs overlapping for arbitrary polygons should be pretty low.
[/quote]
I've already seen this approach but is for 2D.
If I'm correct, when I translate to 3D, support lines become planes parallels to polygon's normal...

Thanks again.