jagua_rs/io/
export.rs

1use crate::entities::{Instance, LayoutSnapshot};
2use crate::geometry::{DTransformation, Transformation};
3use crate::io::ext_repr::{ExtLayout, ExtPlacedItem};
4
5/// Exports a layout to an external representation.
6pub fn export_layout_snapshot(layout: &LayoutSnapshot, instance: &impl Instance) -> ExtLayout {
7    let ext_placed_items = layout
8        .placed_items
9        .values()
10        .map(|pi| {
11            let item = instance.item(pi.item_id);
12
13            let abs_transf =
14                int_to_ext_transformation(&pi.d_transf, &item.shape_orig.pre_transform);
15
16            ExtPlacedItem {
17                item_id: pi.item_id as u64,
18                transformation: abs_transf.into(),
19            }
20        })
21        .collect();
22
23    ExtLayout {
24        container_id: layout.container.id as u64,
25        placed_items: ext_placed_items,
26        density: layout.density(instance),
27    }
28}
29
30/// Converts an internal (used within `jagua-rs`) transformation to an external transformation (applicable to the original shapes).
31///
32/// * `int_transf` - The internal transformation.
33/// * `pre_transf` - The transformation that was applied to the original shape to derive the internal representation.
34pub fn int_to_ext_transformation(
35    int_transf: &DTransformation,
36    pre_transf: &DTransformation,
37) -> DTransformation {
38    //1. apply the pre-transform
39    //2. apply the internal transformation
40
41    Transformation::empty()
42        .transform_from_decomposed(pre_transf)
43        .transform_from_decomposed(int_transf)
44        .decompose()
45}