Render arbitrary CSG solid given boolean function?

1.2k Views Asked by At

I'm looking to implement my own CSG classes for a Robotics project, and I'm thinking to implement each solid as a function that returns a boolean value, given a 3D point; this function will return true if the 3D point is contained within the solid. I figured by doing things this way, I can easily perform union, intersection and subtraction of solids.

This will be sufficient for performing collision detection .etc. by itself, but I'll want to actually render the solids, so my question is this; are there any methods of rendering a solid given its boolean function as described above? I'm more than happy to implement this myself as I want to ideally know exactly what's going on so I can streamline and add to the code as required. I'm also open to suggestions for representing solids in a different way if it will make things easier!

An interesting thing to note is that it would be useful if I could derive things such as the center-of-mass of a solid.

Thanks in advance! Lee.

2

There are 2 best solutions below

0
On BEST ANSWER

Constructive Solid Geometry looks simple, but like most geometry problems, there enough subtleties that you really don't want to implement this for yourself unless it's the core of your work / research.

My suggestion is that you instead look for a high quality computational geometry library (ideally supported by good academic pedigree and published as open source). CGAL is a good option.

If speed isn't a huge priority (ie/ you can solve the problems offline in a separate tool), the problem is common enough problem that other people have done a lot of the hard work already. Check out OpenSCAD "The Programmers Solid 3D CAD Modeller", which uses CGAL to do the boolean operations.

If you need a compromise between the very low, and very high level interfaces, SolidCode might not be a bad intermediate api that lets you call OpenSCAD like commands from Python code.

For rendering, I would advise that you consider treating everything as a (triangulated) surface mesh or volumetric mesh as appropriate, and using CSG or any other tool (eg/ blender) as just a mechanism for data entry. You may also find that collision libraries are readily available for triangulated meshes - and while you may gain some runtime performance improvement for using CSG, it's likely to take longer to develop the whole project.

0
On

The general way to implement Constructive Solid Geometry (CSG) is via a Binary Space Partitioning (BSP) tree on the tessellated polygons of the solids. The CSG operations (union, intersection, subtraction) are done by performing operations on the BSP trees, yielding another BSP tree as a result. The BSP result contains the polygons that are a result of the operation.

Since you're working in C# check out this library, its open source and free to use.

https://github.com/johnmott59/CGSinCSharp