What is CSG?
Constructive Solid Geometry is a modeling technique where complex objects are built by combining simple primitives:- Union (A ∪ B): Combine two shapes into one
- Subtraction (A − B): Remove B from A
- Intersection (A ∩ B): Keep only overlapping volume
Manifold WASM Library
Gridfinity Builder uses Manifold, a high-performance CSG library compiled to WebAssembly:useManifold.ts:7-19
Bin Building Process
Each Gridfinity bin is constructed through a series of boolean operations. The process varies between preview and export modes, but follows the same core algorithm.Preview Geometry (Simplified)
FrombinGeometry.ts:388-420, the preview bin is built in 5 steps:
1. Outer Shell: Rounded Box
Create a solid rounded box with configurable corner radiusThe
binGeometry.ts:399-400
roundedBox helper (binGeometry.ts:40-59) extrudes a 2D rounded rectangle polygon to create a 3D solid.2. Inner Cavity: Subtract Hollow Body
Create walls and floor by subtracting an inner cavitySee
binGeometry.ts:402-406
binGeometry.ts:250-279 for the hollowing algorithm.4. Subtract Features: Magnet & Screw Holes
Remove cylindrical holes for magnets and screws from the baseHoles are positioned at bin corners with deduplication for multi-cell bins (
binGeometry.ts:413-414
binGeometry.ts:88-113).Export Geometry (Full Fidelity)
Export geometry (binGeometry.ts:495-537) includes the precise Gridfinity Z-profile base:
Per-Cell Base Profile
Each cell gets a 5-layer stepped base profile for baseplate interlockingEach section uses tapered extrusion to create chamfered steps.
binGeometry.ts:435-441
Hollowing Algorithm
ThecreateHollowBody function (binGeometry.ts:250-279) demonstrates CSG subtraction:
binGeometry.ts:261-277
cavityH — this ensures a clean open top by extending the cavity slightly above the body.
Memory Management
Manifold objects are manually memory-managed. Every created object must be deleted after use:Example Pattern
unionAll (binGeometry.ts:62-72) encapsulate this pattern:
binGeometry.ts:62-72
Rounded Corners
ThecreateRoundedRectPolygon function (binGeometry.ts:8-38) generates a 2D polygon with circular arc corners:
binGeometry.ts:23-35
SEGMENTS_PER_CORNER:4).
Feature Application
Features like dividers and label shelves are applied as final add/subtract operations:binGeometry.ts:311-362
Performance Considerations
- CSG is expensive: Boolean operations scale with mesh complexity. Preview mode uses simpler geometry to stay fast.
- Web Workers: All CSG runs in a background thread to avoid blocking the UI.
- Caching: Identical configurations reuse cached meshes.
- Memory leaks: Forgetting to
.delete()Manifold objects causes WASM heap growth.
