chiark / gitweb /
wip print-opt-orient
authorIan Jackson <ijackson@chiark.greenend.org.uk>
Sat, 29 Oct 2022 21:26:02 +0000 (22:26 +0100)
committerIan Jackson <ijackson@chiark.greenend.org.uk>
Sat, 29 Oct 2022 21:26:02 +0000 (22:26 +0100)
Signed-off-by: Ian Jackson <ijackson@chiark.greenend.org.uk>
src/bin/print-opt-orient.rs
src/lib.rs

index 70cc60cc912c94ff3d12fadd77a17f3797930987..8b578a4f86d7dd36b5cf6dc2ea8955b10d578bf8 100644 (file)
@@ -1,19 +1,32 @@
+// Tries rotating it about and sees which one has smallest
+// total Z coordinate for in-Z-layer edges.
+// That is least support when printing.
 
 use z3_treefoil::*;
 
 fn main() -> io::Result<()> {
   for shape1 in read_vertices() {
-    let rots = shape_all_rotations(&shape1)
+    let mut rots = shape_all_rotations(&shape1)
       .enumerate()
       .map(|(i, shape)| {
-        let weight = 0;
+        let weight: i32 = shape_edges(&shape)
+          .filter_map(|edge| {
+            let z = edge
+              .iter()
+              .map(|p| p[DIM-1])
+              .dedup()
+              .exactly_one().ok()?;
+            Some(i32::from(z))
+          })
+          .sum();
         (weight, i, shape)
       })
       .collect_vec();
 
+    rots.sort();
+
     for (weight, _, shape) in rots {
-      print!("{} ", weight);
-      print_shape(&shape)?;
+      println!("{} {}", weight, Xyzzy(&shape));
     }
   }
 
index f911cd9381ee2e5d6ce573c844a1b29c1cc06b0c..5cf9d5f8de6aed99eba605c60962a9c7d3faa6ac 100644 (file)
@@ -1,5 +1,6 @@
 //
 
+pub use std::fmt::{self, Display};
 pub use std::io;
 pub use std::io::Write as _;
 pub use std::iter;
@@ -93,6 +94,26 @@ pub fn print_shape(shape: &Shape) -> io::Result<()> {
   Ok(())
 }
 
+pub struct Xyzzy<'s>(pub &'s Shape);
+
+impl Display for Xyzzy<'_> {
+  fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+    for edge in shape_edges(self.0) {
+      let mut done = None;
+      for (i, c) in izip!(0..DIM, "xyz".chars()) {
+        let d = edge[1][i] - edge[0][i];
+        if d == 0 { continue }
+        assert_eq!(done, None);
+        let c = if d > 0 { c.to_ascii_uppercase() } else { c };
+        write!(f, "{}", c)?;
+        done = Some(c);
+      }
+      assert!(done.is_some());
+    }
+    Ok(())
+  }
+}
+
 pub fn point_middle(point: &Point) -> bool {
   point.iter().all(|&c| c == 1 || c == 2)
 }