- Home /
How to get common part of two meshes?
Hi, I am working on cutting meshes. What am I exactly trying to achieve is that:
Input - two meshes(procedurally generated of course)
Output - three meshes(or more, depends on the given meshes)
A - which is a meshA substract mesh B,
B - which is a common part of meshes A and B,
C - which is a mesh B substract mesh A.
I am using the Triangulator that for the given set of vertices creates triangles for me. All I have to do is to give him those vertices, and here goes the question. Is there any algorithm that would help me to do this? Maybe your brilliant idea that came into your mind at the time you saw this picture?
Answer by rageingnonsense · Sep 04, 2015 at 07:38 PM
This is a fairly difficult things to do. I just spent about a month getting my mesh bisector to work. It is far too verbose to go into here, but, here are a few things to remember that may help you:
when you bisect a mesh, you will get two new meshes. Each mesh has triangles from the original that are kept whole, and some that are bisected. In reality, you are bisecting individual triangles.
Each triangle will be bisected into at least 2 new triangles, and at most 3. usually it will be 3, but if the bisection happens on or close to a vertex of a triangle; it effectly splits it into two.
adjacent triangles share edges, but the edges should always be in opposite order to the triangle next to it.
when a triangle is bisected, one mesh will get one triangle, and the other will get one or two (depending on if the triangle was split into 2 or 3).
you WILL have floating point nightmares. keep this in mind for the cases where your bisector cuts along the edge of a triangle almost exactly.
Knowing that, the best way to wind the triangles is to take a known good triangle from the original, and use that as the basis for winding the rest of them.
Here is as method I use to rewind triangles after a bisection:
public static int[] RewindTriangles(int[] triangles) {
int[] processed = new int[triangles.Length];
// the first triangle is considered to be correct for the
// purposes of this routine
processed[0] = triangles[0];
processed[1] = triangles[1];
processed[2] = triangles[2];
// initialize the reference edges using the first triangle
uint re0 = BuildEdge(triangles[0],triangles[1]);
uint re1 = BuildEdge(triangles[1],triangles[2]);
uint re2 = BuildEdge(triangles[2],triangles[0]);
for (int t = 3; t < triangles.Length; t += 3) {
int i0 = triangles[t + 0];
int i1 = triangles[t + 1];
int i2 = triangles[t + 2];
uint e0 = BuildEdge(i0, i1);
uint e1 = BuildEdge(i1, i2);
uint e2 = BuildEdge(i2, i0);
if (e0 == re0 || e0 == re1 || e0 == re2 ||
e1 == re0 || e1 == re1 || e1 == re2 ||
e2 == re0 || e2 == re1 || e2 == re2) {
// this was wound incorrectly (because neighboring edges are the same; no bueno), correct it
i0 = triangles[t + 2];
i1 = triangles[t + 1];
i2 = triangles[t + 0];
}
processed[t + 0] = i0;
processed[t + 1] = i1;
processed[t + 2] = i2;
// rebuild reference edges for the next iteration. Remember, we always check
// the edges from the previous iteration against the edges of the next one
re0 = BuildEdge(i0, i1);
re1 = BuildEdge(i1, i2);
re2 = BuildEdge(i2, i0);
}
return processed;
}
public static uint BuildEdge(int i0, int i1) {
return ((uint)i0 << 16) | (uint)i1;
}
Something to note about this is that I keep track of edges using the fact that triangle indices are 16 bit, but ints can be 32 bit. Using some bitshifting, you can tell if adjacent edges are wound correctly. If the edges are the same int value, then one of them is wound incorrectly.
It is worth noting that I have had trouble getting my mesh bisection to work on spheres. I don't think this has anything to do with the winding of the triangles, but it could be, so take this all as a starting point.
Now, if you want to try a different approach, look up Deluaney Triangulation. I am almost definitely spelling that incorrectly. There are several algorithms you can use for this, like Sweep Line. Since you are bisecting complete meshes already though you already have a basis for triangulation, so the method I posted may be faster.
EDIT: I re-read your question. I realize I made the assumption you are bisecting using a plane, but you are bisecting with another mesh.
I think this means that your triangles could potentially be split into more than 3 triangles. The re-winding of triangles should still be the same though.