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

Started by
6 comments, last by Koder4Fun 11 years, 7 months ago
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 and a list of lightmaps visible from it. That's work ok.
The idea is that when all lightmaps in "visible list" 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:

public void GatherEnergy(float accuracy, ref Hemicube h, ref SceneRenderer scene)
{
bool skip = true;
for (int i = 0; i < VisibleLightMaps.Length; ++i)
{
if ((VisibleLightMaps.lightmap.AverageEnergy / VisibleLightMaps.distSquared) > energyThreshold)
{
skip = false;
break;
}
}
if (skip)
return;
if (ForDetailMesh)
GatherEnergyForDetailMesh(accuracy, ref h, ref scene);
else
GatherEnergyForPoly(accuracy, ref h, ref scene);

CalculateMaxAndAverageEnergy();
}


NOTE: the distance I need is the real minimun distance (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.
Thanks in advance for help.
Please vote usefull replies.
Marco Sacchi
Coding is a challenge ... but solving problems is the fun part
My Blog - XNA Italian portal
Advertisement
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.
I'm sorry, it's true....

The polygons are convex and planar and the vertices are ordered counter-clockwise.

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 correct or at least underestimated. 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.

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 is always needed...
Please vote usefull replies.
Marco Sacchi
Coding is a challenge ... but solving problems is the fun part
My Blog - XNA Italian portal
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.

I'm sorry about any spelling or grammar mistakes or any undue brevity, as I'm most likely typing on my phone

"Hell, there's more evidence that we are just living in a frequency wave that flows in harmonic balance creating the universe and all its existence." ~ GDchat

If speed's not a huge concern, perhaps the below would work:
http://cgm.cs.mcgill.ca/~orm/mind2p.html
http://en.wikipedia.org/wiki/Rotating_calipers

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.
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.
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,
Thanks to all guys!
You've got me some differents solutions to the problem, now I test what fit better for me.


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.

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.


If speed's not a huge concern, perhaps the below would work:
http://cgm.cs.mcgill...orm/mind2p.html
http://en.wikipedia....tating_calipers

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.

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.
Please vote usefull replies.
Marco Sacchi
Coding is a challenge ... but solving problems is the fun part
My Blog - XNA Italian portal

This topic is closed to new replies.

Advertisement