//! From rectangle + cut depths to a vertex list, and independent checks. use crate::tree::{rot, Tree, NONE, V2}; const AXES: [V2; 4] = [(1, 0), (0, 1), (-1, 0), (0, -1)]; /// `depth(global_id)` gives the depth of every node (0 for most). /// Returns the vertices in counterclockwise order (duplicates removed). pub fn vertices(tree: &Tree, a: i64, b: i64, depth: &dyn Fn(usize) -> i64) -> Vec { let nq = tree.nq(); let axis_h = [a, b, 0i64, 0i64]; let mut normals: Vec<(V2, i64)> = vec![]; let mut h = vec![0i64; nq]; let mut marked = vec![false; nq]; for r in 0..4 { let hp = axis_h[r]; let hq = axis_h[(r + 1) % 4]; let get = |h: &Vec, g: u32| -> i64 { match g { 0 => hp, 1 => hq, _ => h[(g - 2) as usize], } }; for i in 0..nq { let val = get(&h, tree.gen_p[i]) + get(&h, tree.gen_q[i]) - depth(r * nq + i); h[i] = val; marked[i] = false; } for i in (0..nq).rev() { if depth(r * nq + i) > 0 { marked[i] = true; } if marked[i] && tree.parent[i] != NONE { marked[tree.parent[i] as usize] = true; } } normals.push((AXES[r], axis_h[r])); // in-order traversal of the marked part if nq > 0 && marked[0] { let mut stack: Vec<(u32, bool)> = vec![(0, false)]; while let Some((v, expanded)) = stack.pop() { let vi = v as usize; if expanded { normals.push((rot(tree.vecs[vi], r), h[vi])); continue; } let rc = tree.right[vi]; if rc != NONE && marked[rc as usize] { stack.push((rc, false)); } stack.push((v, true)); let lc = tree.left[vi]; if lc != NONE && marked[lc as usize] { stack.push((lc, false)); } } } } let t = normals.len(); let mut verts: Vec = Vec::with_capacity(t); for i in 0..t { let (u, hu) = normals[i]; let (v, hv) = normals[(i + 1) % t]; let det = u.0 * v.1 - u.1 * v.0; assert_eq!(det, 1, "consecutive normals must form a Z-basis"); let x = (hu * v.1 - hv * u.1, hv * u.0 - hu * v.0); if verts.last() != Some(&x) { verts.push(x); } } while verts.len() > 1 && verts.first() == verts.last() { verts.pop(); } verts } fn gcd(mut a: i64, mut b: i64) -> i64 { a = a.abs(); b = b.abs(); while b != 0 { let t = a % b; a = b; b = t; } a } #[allow(dead_code)] pub struct Check { pub convex: bool, pub count: i128, pub perimeter: f64, pub twice_area: i128, pub boundary: i128, /// longest edge (Euclidean) and its number of lattice steps pub longest_edge: f64, pub longest_steps: i64, } /// Convexity (strict, counterclockwise), lattice count by Pick's theorem, /// and the perimeter recomputed from the vertices. pub fn check(verts: &[V2]) -> Check { let t = verts.len(); let mut a2: i128 = 0; let mut bd: i128 = 0; let mut per = 0.0f64; let mut longest = 0.0f64; let mut lsteps = 0i64; let mut convex = true; for i in 0..t { let p = verts[i]; let q = verts[(i + 1) % t]; a2 += p.0 as i128 * q.1 as i128 - q.0 as i128 * p.1 as i128; let dx = q.0 - p.0; let dy = q.1 - p.1; bd += gcd(dx, dy) as i128; let len = ((dx as f64) * (dx as f64) + (dy as f64) * (dy as f64)).sqrt(); per += len; if len > longest { longest = len; lsteps = gcd(dx, dy); } if t >= 3 { let r = verts[(i + 2) % t]; let cr = (q.0 - p.0) as i128 * (r.1 - q.1) as i128 - (q.1 - p.1) as i128 * (r.0 - q.0) as i128; if cr <= 0 { convex = false; } } } if t == 1 { bd = 0; } // Pick: N = I + B, 2A = 2I + B - 2 => N = (2A + B)/2 + 1 let count = if t == 1 { 1 } else { (a2 + bd) / 2 + 1 }; Check { convex, count, perimeter: per, twice_area: a2, boundary: bd, longest_edge: longest, longest_steps: lsteps } }