• Create Account

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

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

7 replies to this topic

### #1Koder4Fun  Members

146
Like
0Likes
Like

Posted 19 August 2012 - 03:32 AM

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[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();
}


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

### #2jefferytitan  Members

2508
Like
0Likes
Like

Posted 19 August 2012 - 04:27 AM

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.

### #3Koder4Fun  Members

146
Like
0Likes
Like

Posted 19 August 2012 - 04:59 AM

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?

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...

Edited by Koder4Fun, 19 August 2012 - 09:51 AM.

Please vote usefull replies.
Marco Sacchi
Coding is a challenge ... but solving problems is the fun part
My Blog - XNA Italian portal

### #4CulDeVu  Members

2859
Like
0Likes
Like

Posted 19 August 2012 - 11:55 AM

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

### #5jefferytitan  Members

2508
Like
1Likes
Like

Posted 19 August 2012 - 01:57 PM

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.

### #6pacobarter  Members

115
Like
1Likes
Like

Posted 19 August 2012 - 04:24 PM

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.

### #7Inferiarum  Members

739
Like
0Likes
Like

Posted 20 August 2012 - 05:25 PM

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,

### #8Koder4Fun  Members

146
Like
0Likes
Like

Posted 21 August 2012 - 02:27 AM

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

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.