Skip to main content

lib3mf_core/model/
slice.rs

1use crate::model::ResourceId;
2use serde::{Deserialize, Serialize};
3
4/// A stack of 2D slices defining a geometry (Slice Extension).
5#[derive(Debug, Clone, Default, Serialize, Deserialize)]
6pub struct SliceStack {
7    /// Unique resource ID for this slice stack.
8    pub id: ResourceId,
9    /// Z-coordinate of the bottom of the first slice.
10    #[serde(default)]
11    pub z_bottom: f32,
12    /// Inline slices contained in this stack.
13    pub slices: Vec<Slice>,
14    /// References to external slice stacks in other model parts.
15    pub refs: Vec<SliceRef>,
16}
17
18/// A single 2D slice at a specific Z height.
19#[derive(Debug, Clone, Default, Serialize, Deserialize)]
20pub struct Slice {
21    /// Z-coordinate of the top of this slice.
22    pub z_top: f32,
23    /// 2D vertex positions used by polygons in this slice.
24    pub vertices: Vec<Vertex2D>,
25    /// Contour polygons defining the cross-section at this Z height.
26    pub polygons: Vec<Polygon>,
27}
28
29/// A 2D vertex with X and Y coordinates.
30#[derive(Debug, Clone, Copy, Default, Serialize, Deserialize)]
31pub struct Vertex2D {
32    /// X coordinate in the slice plane.
33    pub x: f32,
34    /// Y coordinate in the slice plane.
35    pub y: f32,
36}
37
38/// A closed polygon contour within a slice, defined by a sequence of segments.
39#[derive(Debug, Clone, Default, Serialize, Deserialize)]
40pub struct Polygon {
41    /// Index of the start vertex.
42    pub start_segment: u32,
43    /// Segments connecting vertices to form the closed contour.
44    pub segments: Vec<Segment>,
45}
46
47/// A single segment within a polygon contour.
48#[derive(Debug, Clone, Default, Serialize, Deserialize)]
49pub struct Segment {
50    /// Index of the vertex to connect to.
51    pub v2: u32,
52    /// Property index for the start vertex (for material interpolation).
53    #[serde(skip_serializing_if = "Option::is_none")]
54    pub p1: Option<u32>,
55    /// Property index for the end vertex.
56    #[serde(skip_serializing_if = "Option::is_none")]
57    pub p2: Option<u32>,
58    /// Property resource ID for this segment (overrides object-level pid).
59    #[serde(skip_serializing_if = "Option::is_none")]
60    pub pid: Option<ResourceId>,
61}
62
63/// A reference to a slice stack in an external model part.
64#[derive(Debug, Clone, Default, Serialize, Deserialize)]
65pub struct SliceRef {
66    /// ID of the referenced slice stack.
67    pub slice_stack_id: ResourceId,
68    /// Package path to the model part containing the referenced slice stack.
69    pub slice_path: String,
70}