nicklockwood / Euclid

A Swift library for creating and manipulating 3D geometry
MIT License
637 stars 53 forks source link

CSG `subtract` pulling in some polygons from thing being subtracted #14

Closed andygeers closed 4 years ago

andygeers commented 4 years ago

I'm having issues with the CSG subtract method, where some of the polygons from the mesh being subtracted are retained, way beyond the bounds of the mesh they're being subtracted from. I've tried to compare your code with the original JS library that the CSG stuff is inspired by, but the implementation is so different that I can't get my head around it. Any thoughts?

See the really big sticking out polygons in this: IMG_2114

The long sticking out thing is constructed like so:

            // Extrude that outline
            let outlinePath = Path(outline.vertices.reversed().map { PathPoint($0.position, isCurved: false) })
            let outlineHole = Mesh.extrude(outlinePath.closed(), depth: 1.0, material: UIColor.blue)

            let hole = outlineHole.translated(by: centerOffset).rotated(by: rotation).translated(by: position)        

Then subtracted from the building like so:

wallMesh = wallMesh.subtract(hole)
andygeers commented 4 years ago

Even if you were able to explain a little of how your code maps to the explanation of how the geometry works in that Js library would be a real help- e.g. they use the concept of “invert”ing a set where you use “clip” and I just couldn’t quite follow the intent behind your code (sorry! I am sure it is great; I am just a bear of very little brain)

nicklockwood commented 4 years ago

@andygeers the code has evolved quite a bit since the JS version and I'm not sure it exactly correlates anymore. If you could share the code you use to generate the wallMesh then I can try to track this issue down.

andygeers commented 4 years ago

D’oh! I think I figured it out. The clue is in the name: constructive SOLID geometry. My wall mesh had no back plane so all the assumptions were faulty. Haven’t verified this yet but pretty sure that must be the case- I couldn’t reproduce it easily when using the full wall mesh that included a back plane and sides.

nicklockwood commented 4 years ago

@andygeers ah yes, that would explain it. It only works correctly for closed hulls.

PrashantKT commented 2 years ago

@andygeers Do you know how to find the outline from the mesh - and render in 2s