Jump to content
  • Advertisement
Sign in to follow this  
andershammervald

Subtracting Triangles / Triangle "Cutting"

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi I'm trying to create a new triangle mesh by "cutting" an area out of one triangle using the shape of another triangle (see image below), the thing is I have no idea what is the best way to accomplish this, I've tried googeling a bit but haven't come up with anything useful so far (might be using the wrong keywords though). Does anyone here have any experience doing something similar or know of any resources that might be worth while looking at. Thanks, Anders

Share this post


Link to post
Share on other sites
Advertisement
Hi ander, there are two approaches you might take to this. If you have two triangulated polygons and you're trying to do a subtraction, you would use a sweep-line algorithm for polygon subtraction and to triangulate the result simultaneously (this is relatively straight-forward).

If you're ONLY subtracting one triangle from another here is a sensible approach:

Let's call the first triangle A, and the second triangle B.

We're subtracting A from B.

First determine how many of B's vertices are INSIDE A (note that even if none of them are, you may still have intersections). Second, find every intersection point, and reorder the edges of A such that intersections are either only on the first edge, the first and second, or all three (ie, not only on the second or similar).

You'll be able to construct a case analysis based on how many vertices of B are inside A and where the intersections happen. To illustrate using your pictures:

The first picture is 1 of B's verts inside A, and two intersections on the first edge of A. The second picture is all three vertices of B inside A, and the third picture is two vertices of B inside A, and two intersections on the first edge.

I've done this before (a while ago), iirc the result is about 30 cases.

Keep in mind, if this isn't a single triangle operation, you should look up sweep-line algorithms and use them.

Share this post


Link to post
Share on other sites
David Eberly has free source code to do this using BSP trees. It is under "Boolean operations on 2D polygons" under Applications/Miscellaneous Samples. See his web page here:

Geometric Tools: Applications

If you wish to do it yourself, BSP trees are not the optimum way to do things. I would recomment reading the document "Efficient Clipping of Arbitrary Polygons" by Gunther Greiner and Kai Hormann. Some hints. It is best to use the Bentley-Ottmann algorithm (this is the classic sweep-line algorithm that George suggested) to efficient locate intersection points. Also, if one polygon is contained entirely inside the other, e.g., you're cutting a clean hole out of the middle and you intend to ultimately triangulate this for display, you will need to subdivide the outer polygon before proceeding. You can triangulate the outer polygon, but this might not be sufficient. A single resulting triangle might still totally surround the hole, and so you'd still need to subdivide the triangle containing the hole shape. One more comment. If you will have possibly degenerate cases (coincident vertices, colinear edges, etc.) then you might want to look into John Hobby's paper on snap rounding, which can avoid a whole slew of computational geometry difficulties with computing the intersection points. I only provide all these ideas, since it is nontrivial to build a completely Boolean code, even in 2D.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!