<p>Without debugging the routines with the offending data, it's impossible to say conclusively. However given the examples you have given it sounds like the libraries are falling foul of the coplanar triangle cases when evaluating the meshes. This is certainly an <em>Achilles heel</em> of many CSG implementations out there (far to many in my experience). In many cases, both theory and implementations seem to fail to address these coplanar cases in many CSG libs. </p>

<p>Certainly the fact that it crashes or fails to produce the desired result implies that the libraries you use are buggy or incomplete in this regard.</p>

<blockquote>
  <p>The libraries would operate normally in any other case of Boolean operations between two different meshes. </p>
</blockquote>

<p>When there are no coplanar triangles between manifolds, the result is well defined and since CSG operations use triangle/triangle intersections fundamentally, if the case of coplanar triangle intersections is not accounted for, everything should be fine and dandy when there are no coplanar triangle tests between meshes. Of course this is not what you require, but makes sense that this operates normally in these scenarios.</p>

<blockquote>
  <p>The 2 almost-identical meshes I wanted to use were clean and water-tight. I even used an example shape which I subtracted from itself. It crashed too.</p>
</blockquote>

<p>This is certainly not correct and I would say a bug. A crash implies something is very wrong and either the coplanar triangle support is buggy, limited, or simply not implemented at all.</p>

<p>I suspect the latter unless you can get a result out with something simple such as diff'ing one cube from another... if you can get a result in some sandboxed cases, then it could be any of the former possibilities.</p>

<p>To see if your CSG routines can handle the coplanar cases, subtracting one cube from another should produce nothing (which as you said it fails). You could also attempt to subtract one cube from another which has a single face coplanar. e.g. Two cubes, both of length <em>length</em>. One positioned at <em>x - length</em> should produce a cube which is a closed manifold. i.e. Cube A (1x1x1) positioned at 0,0,0... and cube B (1x1x1) positioned at 1,0,0.</p>

<blockquote>
  <p>These 2 similar meshes are originating from the same mesh, but with a small difference, like a dent or some noise. I tried to introduce stronger noise or bigger changes, but it still would not work if the 2 meshes had their biggest part identical. </p>
</blockquote>

<p>Probably just one triangle in a mesh is coplanar to another triangle in the other mesh which you are operating on, and thus failing. This may be a way to test by creating two meshes with only one triangle from each mesh being coplanar to another in the other mesh. If this fails then It might indicate that this corner case of coplanar intersection is not catered for at all. As said above, if it does work, then this may point to buggy behaviour at much higher level when dealing with coplanar triangles.</p>

<blockquote>
  <p>By enlarging 1 of the 2 meshes by more than 0.1%, or by transforming/moving(see attached picture) it by a similar amount, the algorithm starts working. But by that point, the shapes are too different to be useful.</p>
</blockquote>

<p>I presume these libs work on floating point numbers, in which case this sounds like the FP equality tolerances are kicking in and evaluating 'almost' coplanar triangles as coplanar, and then subsequently failing to result in the CSG operation. Of course this is problematic for you if the tolerances are high since the input geometry is expected to be 'different enough' to not fall within these tolerances.</p>

<p>Unfortunately, there is nothing you can really do which I think you would find satisfactory since you are effectively at the mercy of the accuracy of the implementation. </p>

<p>If you suspect the libs can handle coplanar triangles between meshes, and its just failing in many cases, you might need to find out how you can kludge the input geometry to not fall foul of the shortcomings that may or may not be present for these corner cases in the implementations:</p>

<ul>
<li><p>Remeshing the input geometry may help, although this may result is varying quality of input data in comparison to your measured data, which kind of defeats the purpose and will result in loss of accuracy wrt the original input meshes, but may at least give a result.</p></li>
<li><p>Re-tessellating triangles while retaining the mesh features may help. This involves inserting Steiner points into a given triangle and re-tessellating into more triangles. This will result in larger meshes (mem footprint, not geometrically speaking) but may help the routines to resolve coplanar intersections. Unlike the previous suggestion, It will retain the accuracy of the topology of the original meshes though.</p></li>
<li><p>Change the FP tolerance (if applicable) that the routines use which may allow you to keep meshes a similar size. However the problem is still there, but this may be satisfactory for your needs allowing you to not have to scale as much. Certainly from a visual standpoint this would be satisfactory I would have thought, however from a numerical point of view this may not be. However this does not fix the issue, rather attempt to circumvent it.</p></li>
</ul>

<p>Given that the majority of the meshes will perhaps be similar, if the vertices are what are similar (i.e coplanar triangles have the SAME vertex values, although that screen shot you posted suggests otherwise) you could perhaps simply diff the meshes yourself, finding all the vertices that are not equal when comparing and then tessellating these. This is far more complex though as you would need to keep track of the edges around the different vertices (these edges will use vertices which are equal to others in the other mesh, so not a quick and dirty fix, rather a slightly different approach avoiding the libs you use) so that you can tessellate the different vertices into a meaningful manifold. I wouldn't recommend this as it is quite a specialised case, and may not be generalised enough for your needs.</p>

<p>Ultimately, if you can't circumvent the issue present with the current libs. You may have to find another. I certainly wasn't able to find many that matched my needs with a correct lic that was applicable to work, however I think there are certainly libs out there that would cater for this. Look for CAD libs as this is an important operation in many CAD programs.</p>

<p>A few years ago I spent some time testing and evaluating various different C++ CSG libs for our needs at work as we required a fairly robust implementation for production. While there were various success, they all failed at some point, and coplanar triangles were usually the culprit. It's quite frustrating as the answer is actually quite straight forward and simply requires the Boolean operation to be performed in 2D between the coplanar triangles and then testing if the resulting split triangles are within the 'other' triangle. Why this rather crucial point is missed in a lot of implementations and theory is beyond me.
/rant</p>

CC BY-SA 4.0
2020-03-31T18:27:23.973Z
Because it's tedious and solved by any epsilon offset.
lightxbulb
CC BY-SA 4.0
2020-03-31T22:25:35.813Z
@lightxbulb While this would be an easy fix for a lot of cases, unfortunately not all. Consider two cubes next to each other, not intersecting, with one face touching. If you were to union them and apply an offset to one of the cubes so there is no coplanar check, the resulting mesh would be two manifolds (when it should be one).
lfgtm
CC BY-SA 4.0
2020-03-31T22:25:51.213Z
Also in the case of OP, a decision would have to be made in advanced about which mesh to offset, if the blue mesh was to be offset along it's triangle normals, this would result in an empty space (what is desired), if the blue mesh was offset the other way (normal inverse) then the result would be the orange mesh (not desired, it should be an empty space). Of course if you are well disciplined you could get away with in many cases, however there are always situations which this cannot fix. Also note offsetting would only really work if the two triangles are equal, and not just coplanar.
lfgtm
CC BY-SA 4.0
2020-03-31T22:51:44.097Z
I never said it's foolproof. It explains why you have such failure cases in many libraries - it's a tedious edge case that requires a disproportionate amount of code to deal with robustly.
lightxbulb
CC BY-SA 4.0
2020-03-31T22:59:09.137Z
@lightxbulb Yes it is certainly an extra layer of complexity ontop of what would otherwise be quite an elegant algorithm. I agree in most cases simply offsetting is fine, if I was using it for a hobby project I certainly wouldn't haven't bothered going the extra mile, however unfortunately professionalism sometimes demands that extra bit from me and in this case for CSG I had to comply. Certainly in OP's case, it seems they require this extra complexity due to the nature of required precision of the input data... but yes agree if you can get away with it, best to save oneself the extra pain ;)
lfgtm
CC BY-SA 4.0
2020-03-31T16:50:10.683Z
2020-03-31T17:45:45.027Z
2020-03-31T17:45:45.027Z
lfgtm
<p>I have been trying to detect defects on "before-and-after" meshes, by finding the boolean difference between them, to no avail.</p>

<p>I tried these two Python libraries(algorithms run in C++) but their boolean operations would produce error or never finish:</p>

<ul>
<li>PyVista(VTK wrapper) : <a href="https://docs.pyvista.org" rel="nofollow noreferrer">https://docs.pyvista.org</a></li>
<li>Pymesh : <a href="https://pymesh.readthedocs.io" rel="nofollow noreferrer">https://pymesh.readthedocs.io</a></li>
</ul>

<p>More specifically, some testing and findings that I had:</p>

<ul>
<li>The libraries would operate normally in any other case of boolean operations between two different meshes. </li>
<li>The 2 almost-identical meshes I wanted to use were clean and water-tight. I even used an example shape which I subtracted from itself. It crashed too.</li>
<li>These 2 similar meshes are originating from the same mesh, but with a small difference, like a dent or some noise. I tried to introduce stronger noise or bigger changes, but it still would not work if the 2 meshes had their biggest part identical. </li>
<li>by enlarging 1 of the 2 meshes by more than 0.1%, or by transforming/moving(see attached picture) it by a similar amount, the algorithm starts working. But by that point, the shapes are too different to be useful.</li>
</ul>

<p><a href="https://i.sstatic.net/Av0ng.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/Av0ng.png" alt="meshes apart from each other"></a></p>

<p>To reproduce the error with Pyvista with an example shape from their website:</p>

<pre><code>import pyvista as pv
mesh_knot = pv.read("knot.ply")
mesh_knot2 = pv.read("knot.ply")
mesh =  mesh_knot.boolean_difference(mesh_knot2)
mesh.plot(eye_dome_lighting=True)
</code></pre>

<p>To conclude, has anyone any idea why this would happen? Do you think that there is a way to bypass the problem if no solution is found? Any other ideas of how I should approach my initial problem?</p>

<3d><computational-geometry><mesh><vtk>
What is the reason that a boolean operation between two almost identical 3D triangular meshes fails?
Thanos Sakelliou
# What is the reason that a boolean operation between two almost identical 3D triangular meshes fails?

(Asked by: username_0 on 2020-03-30)

I have been trying to detect defects on "before-and-after" meshes, by finding the boolean difference between them, to no avail.

I tried these two Python libraries(algorithms run in C++) but their boolean operations would produce error or never finish:

* PyVista(VTK wrapper) : <https://docs.pyvista.org>
* Pymesh : <https://pymesh.readthedocs.io>

More specifically, some testing and findings that I had:

* The libraries would operate normally in any other case of boolean operations between two different meshes.
* The 2 almost-identical meshes I wanted to use were clean and water-tight. I even used an example shape which I subtracted from itself. It crashed too.
* These 2 similar meshes are originating from the same mesh, but with a small difference, like a dent or some noise. I tried to introduce stronger noise or bigger changes, but it still would not work if the 2 meshes had their biggest part identical.
* by enlarging 1 of the 2 meshes by more than 0.1%, or by transforming/moving(see attached picture) it by a similar amount, the algorithm starts working. But by that point, the shapes are too different to be useful.

[![meshes apart from each other](https://i.sstatic.net/Av0ng.png)](https://i.sstatic.net/Av0ng.png)

To reproduce the error with Pyvista with an example shape from their website:

```
import pyvista as pv
mesh_knot = pv.read("knot.ply")
mesh_knot2 = pv.read("knot.ply")
mesh =  mesh_knot.boolean_difference(mesh_knot2)
mesh.plot(eye_dome_lighting=True)

```

To conclude, has anyone any idea why this would happen? Do you think that there is a way to bypass the problem if no solution is found? Any other ideas of how I should approach my initial problem?

**Tags:** 3d, computational-geometry, mesh, vtk


---

## 1 answers

### Answer by username_1 on 2020-03-31. Score: 2

Without debugging the routines with the offending data, it's impossible to say conclusively. However given the examples you have given it sounds like the libraries are falling foul of the coplanar triangle cases when evaluating the meshes. This is certainly an *Achilles heel* of many CSG implementations out there (far to many in my experience). In many cases, both theory and implementations seem to fail to address these coplanar cases in many CSG libs.

Certainly the fact that it crashes or fails to produce the desired result implies that the libraries you use are buggy or incomplete in this regard.

> The libraries would operate normally in any other case of Boolean operations between two different meshes.

When there are no coplanar triangles between manifolds, the result is well defined and since CSG operations use triangle/triangle intersections fundamentally, if the case of coplanar triangle intersections is not accounted for, everything should be fine and dandy when there are no coplanar triangle tests between meshes. Of course this is not what you require, but makes sense that this operates normally in these scenarios.

> The 2 almost-identical meshes I wanted to use were clean and water-tight. I even used an example shape which I subtracted from itself. It crashed too.

This is certainly not correct and I would say a bug. A crash implies something is very wrong and either the coplanar triangle support is buggy, limited, or simply not implemented at all.

I suspect the latter unless you can get a result out with something simple such as diff'ing one cube from another... if you can get a result in some sandboxed cases, then it could be any of the former possibilities.

To see if your CSG routines can handle the coplanar cases, subtracting one cube from another should produce nothing (which as you said it fails). You could also attempt to subtract one cube from another which has a single face coplanar. e.g. Two cubes, both of length *length*. One positioned at *x - length* should produce a cube which is a closed manifold. i.e. Cube A (1x1x1) positioned at 0,0,0... and cube B (1x1x1) positioned at 1,0,0.

> These 2 similar meshes are originating from the same mesh, but with a small difference, like a dent or some noise. I tried to introduce stronger noise or bigger changes, but it still would not work if the 2 meshes had their biggest part identical.

Probably just one triangle in a mesh is coplanar to another triangle in the other mesh which you are operating on, and thus failing. This may be a way to test by creating two meshes with only one triangle from each mesh being coplanar to another in the other mesh. If this fails then It might indicate that this corner case of coplanar intersection is not catered for at all. As said above, if it does work, then this may point to buggy behaviour at much higher level when dealing with coplanar triangles.

> By enlarging 1 of the 2 meshes by more than 0.1%, or by transforming/moving(see attached picture) it by a similar amount, the algorithm starts working. But by that point, the shapes are too different to be useful.

I presume these libs work on floating point numbers, in which case this sounds like the FP equality tolerances are kicking in and evaluating 'almost' coplanar triangles as coplanar, and then subsequently failing to result in the CSG operation. Of course this is problematic for you if the tolerances are high since the input geometry is expected to be 'different enough' to not fall within these tolerances.

Unfortunately, there is nothing you can really do which I think you would find satisfactory since you are effectively at the mercy of the accuracy of the implementation.

If you suspect the libs can handle coplanar triangles between meshes, and its just failing in many cases, you might need to find out how you can kludge the input geometry to not fall foul of the shortcomings that may or may not be present for these corner cases in the implementations:

* Remeshing the input geometry may help, although this may result is varying quality of input data in comparison to your measured data, which kind of defeats the purpose and will result in loss of accuracy wrt the original input meshes, but may at least give a result.
* Re-tessellating triangles while retaining the mesh features may help. This involves inserting Steiner points into a given triangle and re-tessellating into more triangles. This will result in larger meshes (mem footprint, not geometrically speaking) but may help the routines to resolve coplanar intersections. Unlike the previous suggestion, It will retain the accuracy of the topology of the original meshes though.
* Change the FP tolerance (if applicable) that the routines use which may allow you to keep meshes a similar size. However the problem is still there, but this may be satisfactory for your needs allowing you to not have to scale as much. Certainly from a visual standpoint this would be satisfactory I would have thought, however from a numerical point of view this may not be. However this does not fix the issue, rather attempt to circumvent it.

Given that the majority of the meshes will perhaps be similar, if the vertices are what are similar (i.e coplanar triangles have the SAME vertex values, although that screen shot you posted suggests otherwise) you could perhaps simply diff the meshes yourself, finding all the vertices that are not equal when comparing and then tessellating these. This is far more complex though as you would need to keep track of the edges around the different vertices (these edges will use vertices which are equal to others in the other mesh, so not a quick and dirty fix, rather a slightly different approach avoiding the libs you use) so that you can tessellate the different vertices into a meaningful manifold. I wouldn't recommend this as it is quite a specialised case, and may not be generalised enough for your needs.

Ultimately, if you can't circumvent the issue present with the current libs. You may have to find another. I certainly wasn't able to find many that matched my needs with a correct lic that was applicable to work, however I think there are certainly libs out there that would cater for this. Look for CAD libs as this is an important operation in many CAD programs.

A few years ago I spent some time testing and evaluating various different C++ CSG libs for our needs at work as we required a fairly robust implementation for production. While there were various success, they all failed at some point, and coplanar triangles were usually the culprit. It's quite frustrating as the answer is actually quite straight forward and simply requires the Boolean operation to be performed in 2D between the coplanar triangles and then testing if the resulting split triangles are within the 'other' triangle. Why this rather crucial point is missed in a lot of implementations and theory is beyond me.
/rant

> **Comments:**
> **username_2**: Because it's tedious and solved by any epsilon offset.  
> **username_1**: @username_2 While this would be an easy fix for a lot of cases, unfortunately not all. Consider two cubes next to each other, not intersecting, with one face touching. If you were to union them and apply an offset to one of the cubes so there is no coplanar check, the resulting mesh would be two manifolds (when it should be one).  
> **username_1**: Also in the case of OP, a decision would have to be made in advanced about which mesh to offset, if the blue mesh was to be offset along it's triangle normals, this would result in an empty space (what is desired), if the blue mesh was offset the other way (normal inverse) then the result would be the orange mesh (not desired, it should be an empty space). Of course if you are well disciplined you could get away with in many cases, however there are always situations which this cannot fix. Also note offsetting would only really work if the two triangles are equal, and not just coplanar.  
> **username_2**: I never said it's foolproof. It explains why you have such failure cases in many libraries - it's a tedious edge case that requires a disproportionate amount of code to deal with robustly.  
> **username_1**: @username_2 Yes it is certainly an extra layer of complexity ontop of what would otherwise be quite an elegant algorithm. I agree in most cases simply offsetting is fine, if I was using it for a hobby project I certainly wouldn't haven't bothered going the extra mile, however unfortunately professionalism sometimes demands that extra bit from me and in this case for CSG I had to comply. Certainly in OP's case, it seems they require this extra complexity due to the nature of required precision of the input data... but yes agree if you can get away with it, best to save oneself the extra pain ;)  

---

