A C++ library for slicing 3D triangle meshes into 2D layers with offsetting, perimeters, and infill generation.
- Triangle–plane intersection
- Multi‑layer slicing
- Polyline construction from segments
- Winding detection and normalization
- Island construction with outer contours and holes
- Polyline offsetting
- Perimeter generation
- Infill patterns: line, grid, hex
- STL loading (ASCII and binary)
- Handling of degenerate geometry
- C++20
- CMake 3.16+
- Clipper2 (fetched automatically)
mkdir build
cd build
cmake ..
make -jInclude headers from include/ and link against the mesh_slicing library.
See examples/example.cpp for usage.
cd build
ctestVerbose:
ctest --output-on-failureSee the LICENSE file.
include/
v3.h
Plane.h
triangle.h
lineSegment.h
sliceLayer.h
triangleMesh.h
multiSlice.h
winding.h
winding_normalize.h
islands.h
offset.h
perimeters.h
infill.h
src/
triangle.cpp
triangleMesh.cpp
sliceLayer.cpp
multiSlice.cpp
winding.cpp
winding_normalize.cpp
islands.cpp
offset.cpp
perimeters.cpp
infill.cpp
examples/
example.cpp
tests/
test_slicing.cpp
Each triangle is intersected with a horizontal plane using robust classification:
- Vertices above, below, or exactly on the plane are detected
- Edges that cross the plane produce intersection points through linear interpolation
- Edges with both endpoints on the plane produce a coplanar segment
- Duplicate intersection points are collapsed
- Fully coplanar triangles are ignored
This ensures stable slicing when vertices lie exactly on layer boundaries.
All triangles are processed at each Z height.
Valid intersections form unordered line segments.
Segments are stitched by endpoint matching to form closed loops or open polylines.
Contours are classified by orientation.
Outer loops are normalized to CCW.
Holes are normalized to CW.
Holes are assigned to the smallest containing outer loop.
Contours are offset inward or outward.
Perimeters are produced by repeated inward offsets.
Infill is generated by producing parallel lines, rotating them, and clipping them against islands.
Supported patterns: line, grid, hex.
The debug module converts internal geometry into simple SVG and JSON files.
It supports islands, perimeters, infill segments, and generic polylines.
It is useful for inspecting layer geometry and verifying algorithm output.
Example:
auto a = debug::from_islands(islands);
auto b = debug::from_perimeters(perimeters);
auto c = debug::from_infill_segments(infill);
std::vector<TaggedPolyline> all;
all.insert(all.end(), a.begin(), a.end());
all.insert(all.end(), b.begin(), b.end());
all.insert(all.end(), c.begin(), c.end());
debug::export_svg("layer.svg", all);
debug::export_json("layer.json", all);- Triangle slicing
- Square reconstruction
- Disjoint shapes
- Donut shapes
- Degenerate triangles
- Randomized meshes
- On‑plane vertex handling
- Coplanar edge detection
- Duplicate intersection collapse
Run:
ctest --output-on-failureThis repository demonstrates geometric reasoning, slicing algorithms, contour reconstruction, offsetting, infill generation, modular C++ design, and test‑driven development.
It serves as a reference for building slicing engines or toolpath planners.