Some experiments with bvh-csg-0.0.10

The nature of floating point arithmetic makes these kinds of problems extremely difficult. It’s not so surprising that there are cases that have some odd or unexpected results. With testing and example cases some of the scenarios can be addressed but it must be recognized that achieving perfect results is not going to be attainable. Something like using integers for these kinds of operations (ie scaling to large values and dropping the decimal) can help make the math more stable but there’d be a performance hit and a rewrite and complexity would increase.

To your questions:

(1) Why does the result of the subtraction (A2, B2) have degenerate triangles. Is that OK?

Depending on how degenerate they are (literally a triangle with exactly the same 3 vertices?) then they can of course be removed. They should not cause issues, though.

(2) Why does the volume of the result of a subtraction (A2, B2) exceed that of the parent sometimes?

Again - floating point precision can make these kinds of things difficult. The code for computing mesh volume is in this file and adapted from this stackoverflow answer. Feel free to recommend or contribute improvements if you see them.

(3) Why does the bounding box of the A2,B2 model sometimes exceed that of the A1,B1 model?

There’s no guarantees about combing bounding box sizes when merging or computing sibling objects. Consider the bounding box of two merged small but far part spheres. It also looks like three.js’ compute bounding box function does not account for “drawRange” which means some vertices not being rendered could be being included in the BB generation.

(4) In one experiment, the number of triangles in the resulting mesh increased quite drastically (order of magnitude). Is that OK?

Without seeing a reproduction of the case or how the numbers in your chart are computed it’s hard to say whether or not the increase is reasonable.


If you can provide minimal repro cases of some of these issues - ideally with simple generated geometry if possible - then I’m happy to add them to the list for myself or someone else to look at when availability affords and maybe some improvements can be made the project.

(I can’t guarantee that my very messy code is bug-free, but I have looked it over quite carefully :):slight_smile:

I will ask that code be cleaned up and simplified so I can more easily follow what’s going on, though, to save time and effort.