mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-11-28 09:21:55 +01:00
3D: Keep inside bevels within collapsing convex outlines
Part of #7872.
This commit is contained in:
1 parent
0aea8fed95
commit
21679b0635
4 files changed
+105
-24
No files matched your search
@@ -235,6 +235,35 @@ fn flat_triangles_stop_at_their_budget() {
|
||||
assert!(work >= all, "{work} of {all}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn an_inside_bevel_stays_inside_a_collapsing_convex_outline() {
|
||||
for points in [
|
||||
vec![Point::new(30.0, 26.0), Point::new(100.0, 26.0), Point::new(100.0, 28.0), Point::new(30.0, 28.0)],
|
||||
vec![Point::new(0.0, 0.0), Point::new(56.0, 42.0), Point::new(55.6, 44.2), Point::new(-0.4, 2.2)],
|
||||
vec![Point::new(0.0, 0.0), Point::new(6.0, 0.0), Point::new(3.0, 4.0)],
|
||||
] {
|
||||
let mut path = BezPath::new();
|
||||
path.move_to(points[0]);
|
||||
for &point in &points[1..] {
|
||||
path.line_to(point);
|
||||
}
|
||||
path.close_path();
|
||||
let (path, bounds) = shape(&path);
|
||||
let bevel = Bevel { shape: BevelShape::Cove, width: 4.3, height: 10.3, inside: true, ..Bevel::default() };
|
||||
let options = Extrude { depth: 17.2, bevel: Some(bevel), ..Extrude::default() };
|
||||
let solid = extrude_meshed(&path, FillRule::NonZero, bounds, options, front()).unwrap();
|
||||
assert!(!solid.facets.is_empty());
|
||||
for point in solid.facets.iter().flat_map(|facet| facet.contours.iter().flatten()) {
|
||||
for (&a, &b) in points.iter().zip(points.iter().cycle().skip(1)) {
|
||||
assert!((b - a).cross(*point - a) >= -1e-8, "{point:?} escaped {points:?}");
|
||||
}
|
||||
}
|
||||
let mesh = solid.mesh().unwrap();
|
||||
assert!(mesh.parts.contains(&MeshPart::Front), "the front cap has area");
|
||||
assert!(closed(&mesh), "the cap and bands meet for {points:?}");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_beveled_extrusion_stays_closed() {
|
||||
let mut b = BezPath::new();
|
||||
|
||||
@@ -2,8 +2,8 @@ use vectorcraft_geom::kurbo::ParamCurveDeriv;
|
||||
|
||||
use super::outline::{self, Along, Budget, CURVE_TOLERANCE};
|
||||
use super::rings::{
|
||||
CURVE_PIECES, MAX_PIECES, MAX_PLANE_POINTS, MAX_SOLID_POINTS, Ring, Span, TOO_COMPLEX_SURFACE, curve_pieces, inset, left, signed_area,
|
||||
solid_rings_traced, square_corners,
|
||||
CURVE_PIECES, MAX_PIECES, MAX_PLANE_POINTS, MAX_SOLID_POINTS, Ring, Span, TOO_COMPLEX_SURFACE, collapse_width, curve_pieces, inset, left,
|
||||
signed_area, solid_rings_traced, square_corners,
|
||||
};
|
||||
use super::side;
|
||||
use super::{
|
||||
@@ -791,6 +791,35 @@ fn steps(o: &Extrude) -> Vec<Step> {
|
||||
out
|
||||
}
|
||||
|
||||
/// The steps and their shared outline for drawing, surface measurements and mesh export.
|
||||
fn extrude_profile(path: &PathData, rule: FillRule, size: f64, o: &Extrude) -> Result<(Vec<Step>, Vec<Ring>), String> {
|
||||
let mut steps = steps(o);
|
||||
let bands = steps.len().saturating_sub(1);
|
||||
let caps = if o.cap { 2 } else { 0 };
|
||||
let most = curve_pieces(path, size, bands, caps)?;
|
||||
let mut rings = solid_rings_traced(path, rule, size, MAX_SOLID_POINTS, most)?;
|
||||
if let (Some(bevel), [ring]) = (o.bevel, rings.as_slice())
|
||||
&& bevel.inside
|
||||
&& let Some(width) = collapse_width(ring)
|
||||
&& bevel.width > width
|
||||
{
|
||||
// Use one width for the cap and every end of every band. The profile still runs
|
||||
// through the requested depth and meets the wall at the original outline.
|
||||
let scale = width / bevel.width;
|
||||
for step in &mut steps {
|
||||
step.inset *= scale;
|
||||
}
|
||||
steps.dedup_by(|a, b| (a.inset - b.inset).abs() < 1e-9 && (a.t - b.t).abs() < 1e-12);
|
||||
}
|
||||
// The corners the bevel opens are cut square past the miter limit.
|
||||
if let Some(side) = steps.iter().map(|s| s.inset).find(|w| *w != 0.0) {
|
||||
for ring in &mut rings {
|
||||
*ring = square_corners(ring, side);
|
||||
}
|
||||
}
|
||||
Ok((steps, rings))
|
||||
}
|
||||
|
||||
/// The face a band between steps `s0` and `s1` borders: +1 between two front bevel steps, -1 when
|
||||
/// it reaches a back bevel step, and 0 for a wall.
|
||||
fn band_face(s0: &Step, s1: &Step) -> f64 {
|
||||
@@ -957,17 +986,9 @@ pub(crate) fn extrude_faces(path: &PathData, rule: FillRule, bounds: Rect, optio
|
||||
if o.depth <= 0.0 {
|
||||
return plane_faces(path, rule, bounds, view, keep);
|
||||
}
|
||||
let steps = steps(&o);
|
||||
// One face for each edge of the outline in each band between two steps, and the two caps:
|
||||
// counted before any point is sampled, and again on the rings, which can have more points
|
||||
// once contours that meet are merged.
|
||||
let (bands, caps) = (steps.len().saturating_sub(1), if o.cap { 2 } else { 0 });
|
||||
let size = bounds.width().max(bounds.height());
|
||||
let most = curve_pieces(path, size, bands, caps)?;
|
||||
// Every inset of one extrusion goes the same way: into the shape for a bevel inside, out of it
|
||||
// for one outside. The corners that way opens are cut square past the miter limit.
|
||||
let side = steps.iter().map(|s| s.inset).find(|w| *w != 0.0).unwrap_or(0.0);
|
||||
let traced: Vec<Ring> = solid_rings_traced(path, rule, size, MAX_SOLID_POINTS, most)?.iter().map(|r| square_corners(r, side)).collect();
|
||||
let (steps, traced) = extrude_profile(path, rule, size, &o)?;
|
||||
let (bands, caps) = (steps.len().saturating_sub(1), if o.cap { 2 } else { 0 });
|
||||
let rings: Vec<&[Point]> = traced.iter().map(|r| r.points.as_slice()).collect();
|
||||
let edges = rings.iter().map(|r| r.len()).fold(0usize, usize::saturating_add);
|
||||
if edges.saturating_mul(bands).saturating_add(caps) > MAX_SOLID_FACES {
|
||||
@@ -1220,12 +1241,9 @@ pub fn extrude_surfaces(path: &PathData, rule: FillRule, bounds: Rect, options:
|
||||
if o.depth <= 0.0 {
|
||||
return Ok(path.bounds().map(|b| end(PlanarFace::Front, b)).into_iter().collect());
|
||||
}
|
||||
let steps = steps(&o);
|
||||
let (bands, caps) = (steps.len().saturating_sub(1), if o.cap { 2 } else { 0 });
|
||||
let size = bounds.width().max(bounds.height());
|
||||
let most = curve_pieces(path, size, bands, caps)?;
|
||||
let side = steps.iter().map(|s| s.inset).find(|w| *w != 0.0).unwrap_or(0.0);
|
||||
let traced: Vec<Ring> = solid_rings_traced(path, rule, size, MAX_SOLID_POINTS, most)?.iter().map(|r| square_corners(r, side)).collect();
|
||||
let (steps, traced) = extrude_profile(path, rule, size, &o)?;
|
||||
let (bands, caps) = (steps.len().saturating_sub(1), if o.cap { 2 } else { 0 });
|
||||
// A solid past the face limit is refused, as [`extrude_faces`] refuses it.
|
||||
let edges = traced.iter().map(|r| r.points.len()).fold(0usize, usize::saturating_add);
|
||||
if edges.saturating_mul(bands).saturating_add(caps) > MAX_SOLID_FACES {
|
||||
@@ -1264,12 +1282,9 @@ pub fn extrude_side_size(path: &PathData, rule: FillRule, bounds: Rect, options:
|
||||
if o.depth <= 0.0 {
|
||||
return None;
|
||||
}
|
||||
let steps = steps(&o);
|
||||
let (bands, caps) = (steps.len().saturating_sub(1), if o.cap { 2 } else { 0 });
|
||||
let size = bounds.width().max(bounds.height());
|
||||
let most = curve_pieces(path, size, bands, caps).ok()?;
|
||||
let side = steps.iter().map(|s| s.inset).find(|w| *w != 0.0).unwrap_or(0.0);
|
||||
let traced: Vec<Ring> = solid_rings_traced(path, rule, size, MAX_SOLID_POINTS, most).ok()?.iter().map(|r| square_corners(r, side)).collect();
|
||||
let (steps, traced) = extrude_profile(path, rule, size, &o).ok()?;
|
||||
let (bands, caps) = (steps.len().saturating_sub(1), if o.cap { 2 } else { 0 });
|
||||
let rings: Vec<&[Point]> = traced.iter().map(|r| r.points.as_slice()).collect();
|
||||
// A solid past the face limit is refused, as [`extrude_faces`] refuses it.
|
||||
let edges = rings.iter().map(|r| r.len()).fold(0usize, usize::saturating_add);
|
||||
|
||||
@@ -476,6 +476,42 @@ pub(super) fn left(a: Point, b: Point) -> (f64, f64) {
|
||||
if l < 1e-12 { (0.0, 0.0) } else { (-dy / l, dx / l) }
|
||||
}
|
||||
|
||||
/// The safe inward width of a single straight triangle or parallelogram. At the first
|
||||
/// collision the whole ring becomes a point or a segment, so no advancing edges remain.
|
||||
/// This assumes ordinary miters, whose vertices lie on both inset edge lines.
|
||||
pub(super) fn collapse_width(ring: &Ring) -> Option<f64> {
|
||||
let points = ring.points.as_slice();
|
||||
let [a, b, c, ..] = points else { return None };
|
||||
if points.len() > 4 || ring.spans.iter().any(Option::is_some) {
|
||||
return None;
|
||||
}
|
||||
let (u, v) = (*b - *a, *c - *b);
|
||||
let area = u.cross(v);
|
||||
if area <= 0.0 {
|
||||
return None;
|
||||
}
|
||||
// Allow the rounding from a transform of a parallelogram. The final width stays below
|
||||
// the collision by more than this tolerance.
|
||||
let scale = points.iter().map(|p| p.x.abs().max(p.y.abs())).fold(1.0, f64::max);
|
||||
let tolerance = 32.0 * f64::EPSILON * scale;
|
||||
let width = match points {
|
||||
[_, _, _] => area / (u.hypot() + v.hypot() + a.distance(*c)),
|
||||
[_, _, _, d] if (*d - *a - v).hypot() <= tolerance => area / (2.0 * u.hypot().max(v.hypot())),
|
||||
_ => return None,
|
||||
};
|
||||
let cycle = points.iter().copied().cycle();
|
||||
let miters_follow_edges = cycle.clone().zip(cycle.clone().skip(1)).zip(cycle.skip(2)).take(points.len()).all(|((a, b), c)| {
|
||||
let (ax, ay) = left(a, b);
|
||||
let (bx, by) = left(b, c);
|
||||
// The squared cosine of the half angle between the normals is (1 + dot) / 2.
|
||||
ax * bx + ay * by >= 2.0 / (MITER_LIMIT * MITER_LIMIT) - 1.0
|
||||
});
|
||||
// Leave a subpixel cap for the mesh triangulator and room for roundoff in the offset.
|
||||
// The subpixel allowance alone never takes more than half the available width.
|
||||
let margin = 1e-4_f64.min(width / 2.0).max(8.0 * tolerance);
|
||||
miters_follow_edges.then_some((width - margin).max(0.0))
|
||||
}
|
||||
|
||||
/// `ring` moved `w` points towards its filled side (negative: away from it), each corner along
|
||||
/// its bisector, its miter limited to [`MITER_LIMIT`] times `w` so sharp corners don't shoot off.
|
||||
/// A point given twice (a corner [`square_corners`] cut) moves each copy along its one edge's
|
||||
|
||||
+3
-2
@@ -146,8 +146,9 @@ object is where the art was.
|
||||
|
||||
Holes stay holes, with walls of their own. Bevels move each corner along its bisector. A corner the bevel opens (a
|
||||
sharp point with the bevel outside, a narrow notch with it inside) whose miter would reach past four times the width is
|
||||
cut square across instead; at the other corners the miter is limited to four times the width. A bevel wider than a
|
||||
narrow part of the shape folds over itself. Each facet has a seam ([Seams](#seams)).
|
||||
cut square across instead; at the other corners the miter is limited to four times the width. An inside bevel on
|
||||
a single straight triangle or parallelogram with ordinary miters stops before its edges meet, keeping the requested
|
||||
bevel height. Other outlines can still fold where a bevel is wider than a narrow part of the shape. Each facet has a seam ([Seams](#seams)).
|
||||
|
||||
### Custom bevels
|
||||
|
||||
|
||||
Reference in new issue
Block a user