use super::Fabric;
use crate::atoms::Point2;
use crate::ops::mesh::contour::{point_in_polygon, Domain};
use std::collections::{HashMap, HashSet};
const MERGE_FLOOR: f64 = 0.5;
const FEATURE_SPAN: f64 = 1.0;
const ALIGN_SLOPE: f64 = 0.25;
const SAME_NODE: f64 = 0.0025;
const MAX_SNAP_LINES: usize = 4096;
const DOMINANT_SHARE: f64 = 0.2;
const TURN_MARGIN: f64 = 0.02;
const ALIGN_WINDOW: f64 = 0.017;
#[derive(Clone, Copy)]
pub struct Frame2 {
cos: f64,
sin: f64,
}
impl Frame2 {
pub(super) fn at(angle: f64) -> Frame2 {
if angle == 0.0 {
Frame2 { cos: 1.0, sin: 0.0 }
} else {
Frame2 {
cos: angle.cos(),
sin: angle.sin(),
}
}
}
fn identity(self) -> bool {
self.sin == 0.0 && self.cos == 1.0
}
pub(super) fn to_grid(self, p: Point2) -> Point2 {
if self.identity() {
return p;
}
Point2::new(
self.cos * p.x + self.sin * p.y,
-self.sin * p.x + self.cos * p.y,
)
}
pub(super) fn to_local(self, p: Point2) -> Point2 {
if self.identity() {
return p;
}
Point2::new(
self.cos * p.x - self.sin * p.y,
self.sin * p.x + self.cos * p.y,
)
}
}
pub fn preferred_angle(domain: &Domain) -> f64 {
let quarter = std::f64::consts::FRAC_PI_2;
let mut edges: Vec<(f64, f64)> = Vec::new();
let mut perimeter = 0.0;
for l in std::iter::once(&domain.outer).chain(&domain.holes) {
let n = l.pts.len();
for i in 0..n {
let d = l.pts[(i + 1) % n] - l.pts[i];
let len = d.norm();
if len <= 0.0 {
continue;
}
perimeter += len;
edges.push((d.y.atan2(d.x).rem_euclid(quarter), len));
}
}
if edges.is_empty() {
return 0.0;
}
edges.sort_by(|a, b| a.0.partial_cmp(&b.0).unwrap());
let aligned_at = |theta: f64| -> f64 {
edges
.iter()
.filter(|(a, _)| {
let d = (a - theta).rem_euclid(quarter);
d <= ALIGN_WINDOW || d >= quarter - ALIGN_WINDOW
})
.map(|(_, w)| w)
.sum()
};
let axes = aligned_at(0.0);
let mut best = (axes, 0.0);
for &(a, _) in &edges {
let w = aligned_at(a);
if w > best.0 {
best = (w, a);
}
}
if best.0 < DOMINANT_SHARE * perimeter || best.0 <= axes + TURN_MARGIN * perimeter {
return 0.0;
}
best.1
}
pub struct Grid {
pub xs: Vec<f64>,
pub ys: Vec<f64>,
}
impl Grid {
pub fn over(domain: &Domain, target: f64) -> Grid {
let pts = &domain.outer.pts;
let (mut x0, mut x1) = (f64::INFINITY, f64::NEG_INFINITY);
let (mut y0, mut y1) = (f64::INFINITY, f64::NEG_INFINITY);
for p in pts {
x0 = x0.min(p.x);
x1 = x1.max(p.x);
y0 = y0.min(p.y);
y1 = y1.max(p.y);
}
let (mut sx, mut sy) = (vec![x0, x1], vec![y0, y1]);
let slope = ALIGN_SLOPE;
let (mut across_x, mut across_y): (Vec<Run>, Vec<Run>) = (Vec::new(), Vec::new());
for l in std::iter::once(&domain.outer).chain(&domain.holes) {
for run in runs(l, slope) {
let span = run.along[run.along.len() - 1] - run.along[0];
if span >= FEATURE_SPAN * target {
if run.horizontal {
sy.push(run.mean);
} else {
sx.push(run.mean);
}
}
if run.horizontal {
across_x.push(run);
} else {
across_y.push(run);
}
}
}
Grid {
xs: weave(cluster(sx, MERGE_FLOOR * target), &across_x, target),
ys: weave(cluster(sy, MERGE_FLOOR * target), &across_y, target),
}
}
pub fn nx(&self) -> usize {
self.xs.len() - 1
}
pub fn ny(&self) -> usize {
self.ys.len() - 1
}
fn node(&self, i: usize, j: usize) -> Point2 {
Point2::new(self.xs[i], self.ys[j])
}
fn centre(&self, i: usize, j: usize) -> Point2 {
Point2::new(
0.5 * (self.xs[i] + self.xs[i + 1]),
0.5 * (self.ys[j] + self.ys[j + 1]),
)
}
}
struct Run {
horizontal: bool,
mean: f64,
along: Vec<f64>,
}
fn runs(l: &crate::ops::mesh::contour::Loop2D, slope: f64) -> Vec<Run> {
let n = l.pts.len();
if n < 2 {
return Vec::new();
}
let class = |i: usize| -> Option<bool> {
let (a, b) = (l.pts[i], l.pts[(i + 1) % n]);
let (dx, dy) = ((b.x - a.x).abs(), (b.y - a.y).abs());
if dy <= slope * dx && dx > 0.0 {
Some(true)
} else if dx <= slope * dy && dy > 0.0 {
Some(false)
} else {
None
}
};
let start = (0..n)
.find(|&i| class(i) != class((i + n - 1) % n))
.unwrap_or(0);
let mut out = Vec::new();
let mut i = 0;
while i < n {
let Some(horizontal) = class((start + i) % n) else {
i += 1;
continue;
};
let mut j = i + 1;
while j < n && class((start + j) % n) == Some(horizontal) {
j += 1;
}
let (mut across, mut along) = (0.0, Vec::with_capacity(j - i + 1));
for t in i..=j {
let p = l.pts[(start + t) % n];
let (a, b) = if horizontal { (p.y, p.x) } else { (p.x, p.y) };
across += a;
along.push(b);
}
if along[0] > along[along.len() - 1] {
along.reverse();
}
out.push(Run {
horizontal,
mean: across / along.len() as f64,
along,
});
i = j;
}
out
}
fn cluster(mut v: Vec<f64>, tol: f64) -> Vec<f64> {
v.sort_by(|a, b| a.partial_cmp(b).unwrap());
let mut out: Vec<f64> = Vec::with_capacity(v.len());
let (mut sum, mut count) = (0.0, 0usize);
for x in v {
if count > 0 && x - sum / count as f64 > tol {
out.push(sum / count as f64);
(sum, count) = (0.0, 0);
}
sum += x;
count += 1;
}
if count > 0 {
out.push(sum / count as f64);
}
out
}
fn weave(lines: Vec<f64>, across: &[Run], target: f64) -> Vec<f64> {
if lines.len() < 2 {
return lines;
}
if lines.len() > MAX_SNAP_LINES {
let (a, b) = (lines[0], lines[lines.len() - 1]);
let k = (((b - a) / target).round() as usize).max(1);
return (0..=k).map(|i| a + (b - a) * i as f64 / k as f64).collect();
}
let mut out = vec![lines[0]];
for w in lines.windows(2) {
let (a, b) = (w[0], w[1]);
match dictated(a, b, across, target) {
Some(run) => {
let (lo, hi) = (run[0], run[run.len() - 1]);
let scale = (b - a) / (hi - lo);
for &v in &run[1..run.len() - 1] {
out.push(a + (v - lo) * scale);
}
}
None => {
let k = (((b - a) / target).round() as usize).max(1);
for i in 1..k {
out.push(a + (b - a) * i as f64 / k as f64);
}
}
}
out.push(b);
}
out
}
fn dictated(a: f64, b: f64, across: &[Run], target: f64) -> Option<&[f64]> {
let reach = MERGE_FLOOR * target * 0.5;
let mut best: Option<(f64, &[f64])> = None;
for run in across {
let v = &run.along[..];
if (v[0] - a).abs() > reach || (v[v.len() - 1] - b).abs() > reach {
continue;
}
if v.windows(2).any(|w| {
let d = w[1] - w[0];
d < MERGE_FLOOR * target || d > target / MERGE_FLOOR
}) {
continue;
}
let err = (((b - a) / (v.len() - 1) as f64) / target).ln().abs();
if best.is_none_or(|(e, _)| err < e) {
best = Some((err, v));
}
}
best.map(|(_, v)| v)
}
pub struct Core {
pub band: Vec<(Vec<u32>, bool)>,
}
pub fn build(
fab: &mut Fabric,
domain: &Domain,
contour_loops: &[Vec<u32>],
target: f64,
band: usize,
) -> Core {
let frame = Frame2::at(preferred_angle(domain));
let turned = Domain {
outer: turn(&domain.outer, frame),
holes: domain.holes.iter().map(|h| turn(h, frame)).collect(),
};
let domain = &turned;
let grid = Grid::over(domain, target);
let (nx, ny) = (grid.nx(), grid.ny());
if nx == 0 || ny == 0 {
return Core {
band: contour_loops.iter().map(|l| (l.clone(), false)).collect(),
};
}
let mut cut = vec![false; nx * ny];
for l in std::iter::once(&domain.outer).chain(&domain.holes) {
let n = l.pts.len();
for i in 0..n {
let (a, b) = (l.pts[i], l.pts[(i + 1) % n]);
for (ci, cj) in cells_touching(&grid, a, b) {
cut[cj * nx + ci] = true;
}
}
}
let mut solid = vec![false; nx * ny];
for j in 0..ny {
for i in 0..nx {
if cut[j * nx + i] {
continue;
}
let c = grid.centre(i, j);
solid[j * nx + i] = point_in_polygon(c, &domain.outer.pts)
&& !domain.holes.iter().any(|h| point_in_polygon(c, &h.pts));
}
}
let known = ContourNodes::build(fab, contour_loops, target, frame);
let mut keep = clear_of_unmet(&grid, &solid, nx, ny, &known, contour_loops, band);
tidy(&mut keep, nx, ny);
let mut vert: HashMap<(usize, usize), u32> = HashMap::new();
for j in 0..ny {
for i in 0..nx {
if !keep[j * nx + i] {
continue;
}
let mut q = [0u32; 4];
for (k, (di, dj)) in [(0, 0), (1, 0), (1, 1), (0, 1)].into_iter().enumerate() {
q[k] = *vert.entry((i + di, j + dj)).or_insert_with(|| {
let p = grid.node(i + di, j + dj);
known
.at(p)
.unwrap_or_else(|| fab.add(frame.to_local(p), false, false))
});
}
fab.push_quad(q);
}
}
let core_loops = boundary_loops(&keep, nx, ny, &vert);
Core {
band: band_loops(contour_loops, &core_loops),
}
}
pub(super) fn turn(
l: &crate::ops::mesh::contour::Loop2D,
frame: Frame2,
) -> crate::ops::mesh::contour::Loop2D {
crate::ops::mesh::contour::Loop2D {
node_ids: l.node_ids.clone(),
pts: l.pts.iter().map(|&p| frame.to_grid(p)).collect(),
}
}
struct ContourNodes {
grid: super::proximity::PointGrid,
pts: Vec<Point2>,
ids: Vec<u32>,
tol: f64,
}
impl ContourNodes {
fn build(fab: &Fabric, loops: &[Vec<u32>], target: f64, frame: Frame2) -> ContourNodes {
let ids: Vec<u32> = loops.iter().flatten().copied().collect();
let pts: Vec<Point2> = ids
.iter()
.map(|&v| frame.to_grid(fab.pts[v as usize]))
.collect();
ContourNodes {
tol: SAME_NODE * target,
grid: super::proximity::PointGrid::build(&pts, target),
pts,
ids,
}
}
fn at(&self, p: Point2) -> Option<u32> {
let k = self.grid.nearest_index_within(&self.pts, p, self.tol)?;
Some(self.ids[k])
}
}
pub(super) fn band_loops(contour: &[Vec<u32>], core: &[Vec<u32>]) -> Vec<(Vec<u32>, bool)> {
let mut edges: HashMap<(u32, u32), bool> = HashMap::new();
for (l, from_core) in contour
.iter()
.map(|l| (l, false))
.chain(core.iter().map(|l| (l, true)))
{
let n = l.len();
for i in 0..n {
let (a, b) = (l[i], l[(i + 1) % n]);
if edges.remove(&(b, a)).is_none() {
edges.insert((a, b), from_core);
}
}
}
let mut out_of: HashMap<u32, Vec<(u32, bool)>> = HashMap::new();
let mut keys: Vec<(u32, u32)> = edges.keys().copied().collect();
keys.sort_unstable();
for k in keys {
out_of.entry(k.0).or_default().push((k.1, edges[&k]));
}
let mut out = Vec::new();
let mut starts: Vec<u32> = out_of.keys().copied().collect();
starts.sort_unstable();
for s in starts {
while out_of.get(&s).is_some_and(|v| !v.is_empty()) {
let mut ring = vec![s];
let mut all_core = true;
let mut cur = s;
while let Some((nxt, from_core)) = out_of.get_mut(&cur).and_then(|v| v.pop()) {
all_core &= from_core;
if nxt == s {
break;
}
ring.push(nxt);
cur = nxt;
}
if ring.len() >= 3 {
out.push((ring, all_core));
}
}
}
out
}
fn cells_touching(grid: &Grid, a: Point2, b: Point2) -> Vec<(usize, usize)> {
let i0 = column(&grid.xs, a.x.min(b.x));
let i1 = column(&grid.xs, a.x.max(b.x));
let j0 = column(&grid.ys, a.y.min(b.y));
let j1 = column(&grid.ys, a.y.max(b.y));
let mut out = Vec::new();
for j in j0..=j1 {
for i in i0..=i1 {
let eps = 1e-9 * (grid.xs[i + 1] - grid.xs[i]).min(grid.ys[j + 1] - grid.ys[j]);
if enters(
a,
b,
grid.xs[i] + eps,
grid.xs[i + 1] - eps,
grid.ys[j] + eps,
grid.ys[j + 1] - eps,
) {
out.push((i, j));
}
}
}
out
}
fn enters(a: Point2, b: Point2, x0: f64, x1: f64, y0: f64, y1: f64) -> bool {
let (dx, dy) = (b.x - a.x, b.y - a.y);
let (mut t0, mut t1) = (0.0f64, 1.0f64);
for (p, q) in [
(-dx, a.x - x0),
(dx, x1 - a.x),
(-dy, a.y - y0),
(dy, y1 - a.y),
] {
if p == 0.0 {
if q < 0.0 {
return false; }
} else {
let r = q / p;
if p < 0.0 {
t0 = t0.max(r);
} else {
t1 = t1.min(r);
}
if t0 > t1 {
return false;
}
}
}
true
}
pub(super) fn column(lines: &[f64], v: f64) -> usize {
match lines.binary_search_by(|x| x.partial_cmp(&v).unwrap()) {
Ok(i) => i.min(lines.len() - 2),
Err(0) => 0,
Err(i) => (i - 1).min(lines.len() - 2),
}
}
fn clear_of_unmet(
grid: &Grid,
solid: &[bool],
nx: usize,
ny: usize,
known: &ContourNodes,
contour_loops: &[Vec<u32>],
band: usize,
) -> Vec<bool> {
let mut on_contour: HashSet<(u32, u32)> = HashSet::new();
for l in contour_loops {
let n = l.len();
for i in 0..n {
let (a, b) = (l[i], l[(i + 1) % n]);
on_contour.insert((a.min(b), a.max(b)));
}
}
let met = |a: Point2, b: Point2| match (known.at(a), known.at(b)) {
(Some(u), Some(v)) => on_contour.contains(&(u.min(v), u.max(v))),
_ => false,
};
let at = |i: i64, j: i64| -> bool {
i >= 0
&& j >= 0
&& (i as usize) < nx
&& (j as usize) < ny
&& solid[j as usize * nx + i as usize]
};
let mut unmet = vec![false; nx * ny];
for j in 0..ny as i64 {
for i in 0..nx as i64 {
if !at(i, j) {
continue;
}
let (u, v) = (i as usize, j as usize);
let faces = [
((1, 0), (u + 1, v), (u + 1, v + 1)),
((-1, 0), (u, v + 1), (u, v)),
((0, 1), (u + 1, v + 1), (u, v + 1)),
((0, -1), (u, v), (u + 1, v)),
];
for ((di, dj), a, b) in faces {
if at(i + di, j + dj) {
continue;
}
if !met(grid.node(a.0, a.1), grid.node(b.0, b.1)) {
unmet[v * nx + u] = true;
break;
}
}
if !unmet[v * nx + u] {
for (di, dj) in [(1, 1), (1, -1), (-1, 1), (-1, -1)] {
if !at(i + di, j + dj) && at(i + di, j) && at(i, j + dj) {
unmet[v * nx + u] = true;
break;
}
}
}
}
}
let mut out = solid.to_vec();
for j in 0..ny as i64 {
for i in 0..nx as i64 {
let r = band as i64;
let near = (-r..=r).any(|dj| {
(-r..=r).any(|di| {
let (ii, jj) = (i + di, j + dj);
ii >= 0
&& jj >= 0
&& (ii as usize) < nx
&& (jj as usize) < ny
&& unmet[jj as usize * nx + ii as usize]
})
});
if near {
out[j as usize * nx + i as usize] = false;
}
}
}
out
}
pub(super) fn tidy(keep: &mut [bool], nx: usize, ny: usize) {
loop {
let mut changed = false;
let at = |k: &[bool], i: i64, j: i64| -> bool {
i >= 0
&& j >= 0
&& (i as usize) < nx
&& (j as usize) < ny
&& k[j as usize * nx + i as usize]
};
for j in 0..ny as i64 {
for i in 0..nx as i64 {
if !at(keep, i, j) {
continue;
}
let n = [(1, 0), (-1, 0), (0, 1), (0, -1)]
.into_iter()
.filter(|&(di, dj)| at(keep, i + di, j + dj))
.count();
if n < 2 {
keep[j as usize * nx + i as usize] = false;
changed = true;
}
}
}
for j in 0..ny as i64 - 1 {
for i in 0..nx as i64 - 1 {
let (a, b) = (at(keep, i, j), at(keep, i + 1, j + 1));
let (c, d) = (at(keep, i + 1, j), at(keep, i, j + 1));
if a && b && !c && !d {
keep[j as usize * nx + i as usize] = false;
changed = true;
} else if c && d && !a && !b {
keep[j as usize * nx + i as usize + 1] = false;
changed = true;
}
}
}
if !changed {
return;
}
}
}
pub(super) fn boundary_loops(
keep: &[bool],
nx: usize,
ny: usize,
vert: &HashMap<(usize, usize), u32>,
) -> Vec<Vec<u32>> {
let at = |i: i64, j: i64| -> bool {
i >= 0
&& j >= 0
&& (i as usize) < nx
&& (j as usize) < ny
&& keep[j as usize * nx + i as usize]
};
let mut next: HashMap<(usize, usize), (usize, usize)> = HashMap::new();
for j in 0..ny as i64 {
for i in 0..nx as i64 {
if !at(i, j) {
continue;
}
let (i, j) = (i as usize, j as usize);
if !at(i as i64, j as i64 - 1) {
next.insert((i + 1, j), (i, j)); }
if !at(i as i64 + 1, j as i64) {
next.insert((i + 1, j + 1), (i + 1, j)); }
if !at(i as i64, j as i64 + 1) {
next.insert((i, j + 1), (i + 1, j + 1)); }
if !at(i as i64 - 1, j as i64) {
next.insert((i, j), (i, j + 1)); }
}
}
let mut out = Vec::new();
let mut seen: HashMap<(usize, usize), bool> = HashMap::new();
let starts: Vec<(usize, usize)> = {
let mut k: Vec<(usize, usize)> = next.keys().copied().collect();
k.sort_unstable();
k
};
for s in starts {
if seen.contains_key(&s) {
continue;
}
let mut ring = Vec::new();
let mut cur = s;
loop {
seen.insert(cur, true);
ring.push(vert[&cur]);
let Some(&nxt) = next.get(&cur) else { break };
cur = nxt;
if cur == s || seen.contains_key(&cur) {
break;
}
}
if ring.len() >= 4 {
out.push(ring);
}
}
out
}
#[cfg(test)]
mod tests {
use super::*;
use crate::atoms::NodeId;
use crate::ops::mesh::contour::Loop2D;
fn loop2d(pts: &[(f64, f64)]) -> Loop2D {
Loop2D {
node_ids: (0..pts.len() as u32).map(NodeId).collect(),
pts: pts.iter().map(|&(x, y)| Point2::new(x, y)).collect(),
}
}
fn spin(pts: &[(f64, f64)], deg: f64) -> Vec<(f64, f64)> {
let t = deg.to_radians();
pts.iter()
.map(|&(x, y)| (x * t.cos() - y * t.sin(), x * t.sin() + y * t.cos()))
.collect()
}
fn only(outer: Loop2D) -> Domain {
Domain {
outer,
holes: Vec::new(),
}
}
fn cut(corners: &[(f64, f64)], h: f64) -> Loop2D {
let n = corners.len();
let mut pts = Vec::new();
for i in 0..n {
let (a, b) = (corners[i], corners[(i + 1) % n]);
let len = ((b.0 - a.0).powi(2) + (b.1 - a.1).powi(2)).sqrt();
let k = ((len / h).round() as usize).max(1);
for j in 0..k {
let t = j as f64 / k as f64;
pts.push((a.0 + t * (b.0 - a.0), a.1 + t * (b.1 - a.1)));
}
}
loop2d(&pts)
}
#[test]
fn a_wall_places_its_line_whatever_its_pieces_measure() {
let h = 0.1;
for tenths in 2..9u32 {
let step = tenths as f64 * 0.1 + 0.01;
let d = only(cut(
&[
(0.0, 0.0),
(1.0, 0.0),
(1.0, 1.0),
(0.53, 1.0),
(0.53, step),
(0.0, step),
],
h,
));
let g = Grid::over(&d, h);
assert!(
g.xs.iter().any(|x| (x - 0.53).abs() < 1e-9),
"the wall at x=0.53 was missed with the step at y={step}: {:?}",
g.xs
);
}
}
#[test]
fn the_lines_follow_the_contour_rather_than_recompute_it() {
let mut pts = Vec::new();
for i in 0..10 {
pts.push((i as f64 * 0.1, 0.0));
}
for i in 0..5 {
pts.push((1.0, i as f64 * 0.11));
}
for i in 0..10 {
pts.push((1.0 - i as f64 * 0.1, 0.55));
}
for i in 0..5 {
pts.push((0.0, 0.55 - i as f64 * 0.11));
}
let g = Grid::over(&only(loop2d(&pts)), 0.1);
let want = [0.0, 0.11, 0.22, 0.33, 0.44, 0.55];
assert_eq!(g.ys.len(), want.len(), "ys = {:?}", g.ys);
for (got, w) in g.ys.iter().zip(want) {
assert!((got - w).abs() < 1e-9, "ys = {:?}", g.ys);
}
}
#[test]
fn a_square_on_shape_is_left_exactly_where_it_is() {
let d = only(loop2d(&[(0.0, 0.0), (3.0, 0.0), (3.0, 1.0), (0.0, 1.0)]));
assert_eq!(preferred_angle(&d), 0.0);
}
#[test]
fn a_turned_shape_gives_back_its_own_angle() {
for deg in [5.0, 23.7, 30.0, 60.0, 88.0] {
let d = only(loop2d(&spin(
&[(0.0, 0.0), (3.0, 0.0), (3.0, 1.0), (0.0, 1.0)],
deg,
)));
let want = deg.to_radians().rem_euclid(std::f64::consts::FRAC_PI_2);
let got = preferred_angle(&d);
let d = (got - want).rem_euclid(std::f64::consts::FRAC_PI_2);
let err = d.min(std::f64::consts::FRAC_PI_2 - d);
assert!(err < 1e-9, "{deg}°: got {got}, want {want}");
}
}
#[test]
fn a_circle_has_no_direction_and_keeps_the_axes() {
let pts: Vec<(f64, f64)> = (0..64)
.map(|i| {
let t = i as f64 / 64.0 * std::f64::consts::TAU;
(t.cos(), t.sin())
})
.collect();
assert_eq!(preferred_angle(&only(loop2d(&pts))), 0.0);
}
#[test]
fn the_axes_win_a_tie_against_a_marginal_rival() {
let d = only(loop2d(&[
(0.0, 0.0),
(3.0, 0.0),
(3.0, 1.0),
(0.1, 1.0),
(0.0, 0.94),
]));
assert_eq!(preferred_angle(&d), 0.0);
}
#[test]
fn the_frame_is_a_round_trip() {
let f = Frame2::at(0.4);
let p = Point2::new(1.25, -3.5);
let back = f.to_local(f.to_grid(p));
assert!((back - p).norm() < 1e-12, "{back:?} for {p:?}");
let id = Frame2::at(0.0);
assert_eq!(id.to_grid(p), p);
assert_eq!(id.to_local(p), p);
}
}