diff --git a/crates/three-d/src/solid/face_map.rs b/crates/three-d/src/solid/face_map.rs index cca6e6971a..ac99348ba3 100644 --- a/crates/three-d/src/solid/face_map.rs +++ b/crates/three-d/src/solid/face_map.rs @@ -178,10 +178,12 @@ pub fn plane_face_map(bounds: Rect, view: View) -> Option { /// before projection, in the mesh's coordinates (y up), and the face's outward normal there. #[derive(Clone, Copy, Debug, PartialEq)] pub struct MeshFace { - /// An art point `p` lies at `x · p.x + y · p.y + c`. + /// An art point `p` lies at `x · (p.x - centre.x) + y · (p.y - centre.y) + at`, with the art's + /// center at `at`. x: V3, y: V3, - c: V3, + centre: Point, + at: V3, normal: V3, /// A plane's mesh turns its normal toward the viewer, so its triangles face either way. either_side: bool, @@ -193,10 +195,11 @@ fn mesh_space(v: V3) -> V3 { } impl MeshFace { - fn new(camera: &Camera, placing: &Placing, side: f64, either_side: bool) -> Self { - let (x, y, c) = placing.frame(); + fn new(camera: &Camera, placing: &Placing, centre: Point, side: f64, either_side: bool) -> Self { + let (x, y, _) = placing.frame(); + let at = mesh_space(placing.place(centre)); let normal = mesh_space(camera.rotate(V3(0.0, 0.0, side))).unit(); - Self { x: mesh_space(x), y: mesh_space(y), c: mesh_space(c), normal, either_side } + Self { x: mesh_space(x), y: mesh_space(y), centre, at, normal, either_side } } /// The art point that mesh point `m` shows, when `m` lies on the face's plane and a triangle @@ -210,7 +213,9 @@ impl MeshFace { if !faces { return None; } - let d = V3(m[0], m[1], m[2]).sub(self.c); + // Measured from the art's center, so the tolerance follows the solid's size and not where + // the art lies on the page. + let d = V3(m[0], m[1], m[2]).sub(self.at); let (xx, xy, yy) = (self.x.dot(self.x), self.x.dot(self.y), self.y.dot(self.y)); let det = xx * yy - xy * xy; let spans = xx > 1e-24 && yy > 1e-24 && det >= 1e-12 * xx * yy; @@ -218,10 +223,11 @@ impl MeshFace { return None; } let (bx, by) = (self.x.dot(d), self.y.dot(d)); - let p = Point::new((bx * yy - by * xy) / det, (xx * by - xy * bx) / det); - let on = V3(self.x.0 * p.x + self.y.0 * p.y, self.x.1 * p.x + self.y.1 * p.y, self.x.2 * p.x + self.y.2 * p.y); + let (px, py) = ((bx * yy - by * xy) / det, (xx * by - xy * bx) / det); + let on = V3(self.x.0 * px + self.y.0 * py, self.x.1 * px + self.y.1 * py, self.x.2 * px + self.y.2 * py); let off = d.sub(on); - let size = 1.0 + d.dot(d).sqrt() + self.c.dot(self.c).sqrt(); + let size = 1.0 + d.dot(d).sqrt(); + let p = Point::new(self.centre.x + px, self.centre.y + py); (p.x.is_finite() && p.y.is_finite() && off.dot(off).sqrt() <= 1e-6 * size).then_some(p) } } @@ -245,7 +251,7 @@ pub fn extrude_mesh_face(bounds: Rect, options: Extrude, view: View, face: Plana PlanarFace::Back => (1.0, -1.0), }; let placing = Placing::new(&camera, bounds.center(), Turn::new(bounds, o.twist, o.taper, t), o.depth / 2.0 - o.depth * t); - Some(MeshFace::new(&camera, &placing, side, false)) + Some(MeshFace::new(&camera, &placing, bounds.center(), side, false)) } /// The plane [`super::plane`] turns art with `bounds` into, with `view`, in its mesh @@ -254,7 +260,7 @@ pub fn plane_mesh_face(bounds: Rect, view: View) -> Option { check_bounds(bounds).ok()?; let camera = Camera::new(view, bounds, 0.0); let placing = Placing::new(&camera, bounds.center(), Turn::new(bounds, 0.0, 1.0, 0.0), 0.0); - Some(MeshFace::new(&camera, &placing, 1.0, true)) + Some(MeshFace::new(&camera, &placing, bounds.center(), 1.0, true)) } #[cfg(test)] diff --git a/crates/three-d/src/solid/tests_face_map.rs b/crates/three-d/src/solid/tests_face_map.rs index 3cf10e7c70..ebbe95448a 100644 --- a/crates/three-d/src/solid/tests_face_map.rs +++ b/crates/three-d/src/solid/tests_face_map.rs @@ -186,6 +186,27 @@ fn a_caps_mesh_corners_go_back_to_the_art_they_were_made_from() { assert_eq!(extrude_mesh_face(bounds, Extrude { depth: 0.0, ..options }, view, PlanarFace::Back), None); } +#[test] +fn a_cap_far_from_the_page_origin_takes_none_of_a_step_bevels_treads() { + let (path, bounds) = square(); + let far = vectorcraft_geom::kurbo::Affine::translate((1e7, 1e7)); + let path = PathData::from_bezpath(&(far * path.to_bezpath())); + let bounds = far.transform_rect_bbox(bounds); + let bevel = crate::Bevel { width: 4.0, height: 4.0, shape: crate::BevelShape::Step, ..Default::default() }; + let options = Extrude { bevel: Some(bevel), ..Extrude::default() }; + let view = turned(0.0); + let mesh = crate::extrude_mesh(&path, FillRule::NonZero, bounds, options, view).unwrap(); + let mesh_face = extrude_mesh_face(bounds, options, view, PlanarFace::Front).unwrap(); + let (points, taken) = mesh_art(&mesh, &mesh_face); + // The treads face the same way as the cap, a few points behind it: only the cap's two + // triangles are taken, wherever the art lies on the page. + assert_eq!(taken, 2); + for p in points { + let near = corners(bounds).iter().map(|c| c.distance(p)).fold(f64::INFINITY, f64::min); + assert!(near < 1e-6, "{p:?} is {near} from the cap's corners"); + } +} + #[test] fn the_mapped_part_of_a_rectangle_stops_before_the_eye() { let (_, bounds) = square();