MCPcopy Create free account
hub / github.com/Notgnoshi/generative / main

Function main

tools/pack.rs:44–105  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

42}
43
44fn main() -> eyre::Result<()> {
45 color_eyre::install()?;
46 let args = CmdlineOptions::parse();
47
48 let filter = tracing_subscriber::EnvFilter::builder()
49 .with_default_directive(args.log_level.into())
50 .from_env_lossy();
51 tracing_subscriber::fmt()
52 .with_env_filter(filter)
53 .with_ansi(true)
54 .with_writer(std::io::stderr)
55 .init();
56
57 let reader = get_input_reader(&args.input)?;
58 let mut geometries: Vec<_> = read_geometries(reader).collect();
59 let padding = Coord {
60 x: args.padding,
61 y: args.padding,
62 };
63
64 let mut rects = GroupedRectsToPlace::<usize, ()>::new();
65 for (idx, geometry) in geometries.iter().enumerate() {
66 if let Some(mut bbox) = geometry.bounding_rect() {
67 // Apply a little bit of padding around each bounding box. This is not just to generate
68 // a more appealing result; it's so that POINTs don't get "packed" all on top of each
69 // other because they have 0x0 bounding boxes.
70 let min = bbox.min();
71 let new_min = min - padding;
72 bbox.set_min(new_min);
73
74 let max = bbox.max();
75 let new_max = max + padding;
76 bbox.set_max(new_max);
77 let rect =
78 RectToInsert::new(bbox.width().ceil() as u32, bbox.height().ceil() as u32, 1);
79 rects.push_rect(idx, None, rect);
80 }
81 }
82
83 let mut bins = BTreeMap::new();
84 bins.insert(0, TargetBin::new(args.width, args.height, 1));
85
86 let packing = pack_rects(&rects, &mut bins, &volume_heuristic, &contains_smallest_box)
87 .map_err(|e| eyre::eyre!("Failed to pack rectangles: {e:?}"))?;
88 let packing = packing.packed_locations();
89 for (idx, (_bin, location)) in packing.iter() {
90 let geometry = &mut geometries[*idx];
91 let bbox = geometry.bounding_rect().ok_or_else(|| {
92 eyre::eyre!("Geometry at index {idx} has no bounding box, cannot pack")
93 })?;
94 let source = bbox.center();
95 let target = Coord {
96 x: location.x() as f64,
97 y: location.y() as f64,
98 };
99 let offset = target - source;
100 geometry.translate_mut(offset.x, offset.y);
101 }

Callers

nothing calls this directly

Calls 4

get_input_readerFunction · 0.85
read_geometriesFunction · 0.85
get_output_writerFunction · 0.85
write_geometriesFunction · 0.85

Tested by

no test coverage detected