Skip to content

JaskRendix/slicer-core

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

15 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Mesh Slicing Engine

A C++ library for slicing 3D triangle meshes into 2D layers with offsetting, perimeters, and infill generation.

Features

  • 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

Dependencies

  • C++20
  • CMake 3.16+
  • Clipper2 (fetched automatically)

Build

mkdir build
cd build
cmake ..
make -j

Usage

Include headers from include/ and link against the mesh_slicing library.
See examples/example.cpp for usage.

Testing

cd build
ctest

Verbose:

ctest --output-on-failure

License

See the LICENSE file.


Project Structure

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

Core Concepts

Triangle–Plane Intersection

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.

Layer Slicing

All triangles are processed at each Z height.
Valid intersections form unordered line segments.

Polyline Reconstruction

Segments are stitched by endpoint matching to form closed loops or open polylines.

Winding and Islands

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.

Offsetting and Perimeters

Contours are offset inward or outward.
Perimeters are produced by repeated inward offsets.

Infill

Infill is generated by producing parallel lines, rotating them, and clipping them against islands.
Supported patterns: line, grid, hex.

Debug Export

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);

Test Coverage

  • 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-failure

Purpose

This 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.

About

Core slicing algorithms for 3D printing: slicing, polylines, offsets, perimeters, infill.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors