File size: 1,413 Bytes
a2f76ca
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
from __future__ import annotations

import numpy as np
from PIL import Image
from shapely.geometry import Polygon
import trimesh

from stl_slicer import _compose_even_odd_polygons, calculate_z_levels, slice_stl_to_tiffs


def test_calculate_z_levels_creates_single_layer_for_thin_mesh() -> None:
    z_values = calculate_z_levels(0.0, 0.01, 0.1)

    assert len(z_values) == 1
    assert 0.0 <= z_values[0] < 0.01


def test_slice_stl_to_tiffs_creates_non_empty_tiffs(tmp_path) -> None:
    mesh = trimesh.creation.box(extents=(2.0, 2.0, 2.0))
    stl_path = tmp_path / "cube.stl"
    mesh.export(stl_path)

    stack = slice_stl_to_tiffs(
        stl_path,
        layer_height=0.5,
        pixel_size=0.25,
        output_root=tmp_path / "generated",
    )

    assert len(stack.tiff_paths) == 4
    assert stack.zip_path.exists()
    assert all(path.exists() for path in stack.tiff_paths)

    with Image.open(stack.tiff_paths[0]) as first_image:
        pixels = np.array(first_image)

    assert pixels.max() == 255
    assert pixels.sum() > 0


def test_compose_even_odd_polygons_preserves_holes() -> None:
    outer = Polygon([(0, 0), (10, 0), (10, 10), (0, 10)])
    inner = Polygon([(3, 3), (7, 3), (7, 7), (3, 7)])

    composed = _compose_even_odd_polygons([outer, inner])

    assert len(composed) == 1
    assert composed[0].area == outer.area - inner.area
    assert len(composed[0].interiors) == 1