# Intersection of two 3D Polyhedrons

## Recommended Posts

Hello to all users,
I'm making a project for my university and I need to calculate the intersection of two given 3D polyhedrons. How can I do that?
I know the x,y,z, coordinates of each vertex and also the triangles list that I use to draw each polyhedron.
Any help would be very very appreciated.
With regard,
Dimitris

##### Share on other sites
Hidden
Ray to Triangle intersection: [url="http://www.softsurfer.com/Archive/algorithm_0105/algorithm_0105.htm"]http://www.softsurfer.com/Archive/algorithm_0105/algorithm_0105.htm[/url]

good luck

##### Link to post
This should work:
-Create from all the edges of your polyhedron Rays (make sure that the rays are not infinite, but have the length of the tested edge),
-Intersect them with all triangles of polyhedron.

Ray to Triangle intersection: [url="http://www.softsurfer.com/Archive/algorithm_0105/algorithm_0105.htm"]http://www.softsurfe...orithm_0105.htm[/url]

Perhaps not the best approach, but it should work.

Good luck

##### Share on other sites
Do you want the polyhedron of the intersecting volume of the two original polyhedrons, or just check if they do or do not intersect?

##### Share on other sites
I want the polyhedron, not only check

##### Share on other sites
[quote name='sgdimitris' timestamp='1331560144' post='4921332']
I want the polyhedron, not only check
[/quote]

General polyhedron intersection is a very broad and complex problem, made even more complex when accounting for floating point inaccuracy. There's various libraries around with different characteristics. It gets even more complex if allowing degenerate and self-intersecting inputs.

Such operations are typically performed using CSG and polyhedra are only generated as final step. One disadvantage of such approach is that polyhedra aren't perfect fit, so there might be intersections left as result.

One way is to work on triangle soup. Intersect triangles, then run a post-processing pass to reconstruct final result. Quite a few simplifications can be made if limiting the solution to a well-defined problem, perhaps merely generating non-intersecting objects or similar.

##### Share on other sites
On large scale multiple intersactions, won't that make the code very slow?

##### Share on other sites
[quote name='darksheikh' timestamp='1331738486' post='4921996']
On large scale multiple intersactions, won't that make the code very slow?
[/quote]

Of course, but you are going for general intersection. Usually you do some filtering using convex hulls or bounding boxes/convex polyhedrons, and then you use the detailed mechanism only for the intersecting bodies

##### Share on other sites
A popular way to do this is to generate a bsp tree of of your polyhedrons and then merge the trees in the appropriate way - google for BSP and CSG together to find details on how the two are used together.

If I was going to do this in a quick and dirty way, I would simply take the triangles of one of the polyhedra (the one with the fewest triangles), extend them to planes and split (look for "polyhedral splitting" - it is a well studied problem) the other polyhedron. Then all you have to do is classify the resulting small polyhedra as in or out of the polyhedron used to generate the splitting planes. This can be done with a simple point inside mesh query. Finally you take the "inside" polyhedra and work out the union of them, which isn't to hard since they share sides and don't overlap.

##### Share on other sites
One question: any chance these are convex? That problem is much easier to solve.

##### Share on other sites
Sorry for the late answer, yes the polyhedra are convex

##### Share on other sites
[quote name='sgdimitris' timestamp='1333880145' post='4929271']
Sorry for the late answer, yes the polyhedra are convex
[/quote]

Then it's much easier: there are libraries that convert between vertex representations and hyperplane representations (and back), you just need to convert both polyhedra to hyperplane (just planes actually in 3d), take the union of these sets planes and convert it to vertex representation.

If you want a more direct approach, in 3d, you can take one polyhedron, and for each triangle, generate a plane using the triangle normal and one of it's vertices. Take these planes, and intersect it with the other polyhedron, keeping only the inside half space. Specifically, throw away all triangles whose vertices are all in the outside half space, and keep all the triangles, whose vertices are in the inside half space. Then for all the triangles that cross the plane, clip them to the inside half space. The new vertices that result from the clipped edges will now form a convex polygon embedded within the the intersection plane. Triangulate this. Repeat this for each triangle/plane of the first polyhedron, and you're done!

##### Share on other sites
Thanks for the reply - I will try to implement your algorithm

##### Share on other sites
Hello Crowley99,
do you have some links that can help us implement one of the two given algorithms?
Regards,
Dimitris

##### Share on other sites
Hello Dimitris

If the volumes are convex, it's possible to use the Séparating Axis Theorem (SAT) method. ([url="http://en.wikipedia.org/wiki/Hyperplane_separation_theorem"]http://en.wikipedia.org/wiki/Hyperplane_separation_theorem[/url])
This method is well documented. I can give you my implementation "as an example" if you wish (for what it's worth of course [img]http://public.gamedev.net//public/style_emoticons/default/smile.png[/img] )

What's more with this method you can get the penetration depth if you need it.

Nico [img]http://public.gamedev.net//public/style_emoticons/default/rolleyes.gif[/img]

##### Share on other sites
If it was calculating the intersection volume you meant (I'm not clear whether you meant this or just how far they 'poke' each other) it is probably best to google for CSG of convex hulls.

[url="http://en.wikipedia.org/wiki/Constructive_solid_geometry"]http://en.wikipedia.org/wiki/Constructive_solid_geometry[/url]

'QHull' is a library that may have an implementation to look at I believe, and also probably there is one in Dave Eberly's Geometric Tools [url="http://www.geometrictools.com/"]http://www.geometrictools.com/[/url] (he's a great egghead and has got libraries for everything lol, he wrote the NDL / gamebryo stuff).

In games the convex hulls are often known as 'brushes' and are widely used in quake derived stuff and unreal, if you have a look at dealing with their maps, or gtkradiant source you might get some ideas.

If you can valmorphanize your polygon representation into a set of planes defining the sides of a brush, you can then add planes (or their opposites) from one brush to another to split it up and do CSG jiggery pokery. Then you can use a routine to rebuild the brush from a set of planes back into a set of polygons (using e.g. the intersection of 3 planes to form a vertex) and chuck out any irrelevant planes / polys that have been clipped out by inserted planes. That's how I did it anyway.

##### Share on other sites
GJK (Gilbert Johnson Keerthi) is another well-known algorithm for finding the intersection or closest features of polyhedra.

EDIT: [url="https://mollyrocket.com/849"]this video[/url] has a really good explanation of how it works. [url="https://mollyrocket.com/forums/viewforum.php?f=13"]The forums on that site[/url] are also worth visiting to find enhancements etc.

## Create an account or sign in to comment

You need to be a member in order to leave a comment

## Create an account

Sign up for a new account in our community. It's easy!

Register a new account

• ### Forum Statistics

• Total Topics
628347
• Total Posts
2982204

• 10
• 9
• 24
• 11
• 9