Project 7: Simple CSG Engine (Constructive Solid Geometry)
Build a CSG tree evaluator that combines primitives using boolean operations.
Project Overview
| Attribute | Value |
|---|---|
| Difficulty | Level 3: Advanced |
| Time Estimate | 2-3 weeks |
| Main Language | C++ |
| Alternative Languages | Rust, C, Python |
| Knowledge Area | Boolean modeling and tree evaluation |
| Tools | Mesh viewer or ray-marcher |
| Main Book | “Geometric Modeling” by Michael E. Mortenson |
What you’ll build: A CSG evaluator that applies union, intersection, and difference to primitives like boxes and cylinders.
Why it teaches computational geometry: CSG is a logical representation of geometry that reveals how modeling operations compose.
Core challenges you’ll face:
- Representing boolean trees
- Evaluating set operations consistently
- Handling ambiguous boundaries
Real World Outcome
You will describe a shape as a tree of primitive operations and output a mesh or signed-distance field. The resulting solid should match the expected boolean composition.
Example Output:
$ ./csg_eval --input scene.json --output result.obj
Primitives: 3
Operations: 2
Wrote solid to result.obj
Verification steps:
- Visualize the result and check expected boolean effects
- Evaluate inside/outside queries at sample points
The Core Question You’re Answering
“How do you build complex solids by combining simpler ones with boolean logic?”
This is the conceptual heart of many CAD modeling systems.
Concepts You Must Understand First
Stop and research these before coding:
- Set operations on solids
- What is union, intersection, and difference geometrically?
- Book Reference: “Geometric Modeling” by Michael E. Mortenson, Ch. 11
- Implicit surfaces
- How do you test whether a point is inside a primitive?
- Book Reference: “Geometric Tools for Computer Graphics” by Schneider & Eberly, Ch. 6
- Boundary ambiguity
- What happens when surfaces overlap exactly?
- Book Reference: “Computational Geometry: Algorithms and Applications” by de Berg et al., Ch. 3
Questions to Guide Your Design
- Representation
- Will you represent primitives implicitly or as meshes?
- How will you store the CSG tree?
- Evaluation strategy
- Will you use point membership tests or mesh booleans?
- How will you handle precision at boundaries?
Thinking Exercise
Boolean Logic Table
Construct the truth table for inside/outside for union, intersection, and difference.
Questions while working:
- How does difference invert the second operand?
- Why does CSG align naturally with logic gates?
The Interview Questions They’ll Ask
Prepare to answer these:
- “What is CSG and why is it useful?”
- “How does a CSG tree evaluate?”
- “What is the difference between implicit and explicit geometry?”
- “How do you handle overlapping boundaries?”
- “Why is CSG popular in CAD and not in real-time rendering?”
Hints in Layers
Hint 1: Starting Point Start with inside/outside tests for primitives.
Hint 2: Next Level Combine tests with boolean logic for each tree node.
Hint 3: Technical Details Use a consistent epsilon for boundary handling.
Hint 4: Tools/Debugging Sample points across space and visualize inside/outside classification.
Books That Will Help
| Topic | Book | Chapter |
|---|---|---|
| CSG basics | “Geometric Modeling” by Michael E. Mortenson | Ch. 11 |
| Implicit surfaces | “Geometric Tools for Computer Graphics” by Schneider & Eberly | Ch. 6 |
| Set operations | “Computational Geometry: Algorithms and Applications” by de Berg et al. | Ch. 3 |
Implementation Hints
- Begin with a tiny set of primitives to keep evaluation clear.
- Separate tree parsing from geometry evaluation.
- Provide a debug mode that prints boolean evaluation paths.
Learning Milestones
- First milestone: You can evaluate union and intersection of two primitives.
- Second milestone: You can build multi-level CSG trees.
- Final milestone: You can explain how set theory drives CAD modeling.