Merge pull request #8003 from dotCooCoo/fix/inflate-colored-light-seam-fill

3D: colored Inflate fills each facet and its seam as one path, covering each edge pixel once
This commit is contained in:
BFlat
2026-11-16 09:31:14 +07:00
committed by GitHub
5 changed files with 179 additions and 5 deletions
+133 -2
View File
@@ -168,6 +168,62 @@ pub(crate) fn footprint(path: &vectorcraft_geom::BezPath, seam: f64) -> vectorcr
if sign < 0.0 { footprint.reverse_subpaths() } else { footprint }
}
/// The most edges a facet may have for [`seamed_fill`] to test it for crossing edges.
const SEAMED_FILL_EDGES: usize = 256;
/// Facet `path` and its round seam `seam` wide as one path to fill ([`footprint`]), so that at an
/// edge pixel the facet and its seam cover it once between them, not a fill's coverage and then a
/// stroke's over it. `None` unless `path` is one closed polygon of up to [`SEAMED_FILL_EDGES`]
/// finite edges, with an area, and with no two edges that are not neighbors touching: only there
/// does the footprint's nonzero fill cover the facet and its seam and nothing more. `None` too for
/// a seam that is not a positive finite width, or a footprint with no finite bounds or less area
/// than the facet.
pub(crate) fn seamed_fill(path: &vectorcraft_geom::PathData, seam: f64) -> Option<vectorcraft_geom::PathData> {
use kurbo::{PathEl, Point, Shape};
if !(seam.is_finite() && seam > 0.0) {
return None;
}
let bez = path.to_bezpath();
let mut elements = bez.elements().iter();
let Some(PathEl::MoveTo(first)) = elements.next() else { return None };
let mut points = vec![*first];
let mut closed = false;
for el in elements {
match el {
PathEl::LineTo(p) if !closed => points.push(*p),
PathEl::ClosePath if !closed => closed = true,
_ => return None,
}
}
if points.len() > 1 && points.first() == points.last() {
points.pop();
}
let area = bez.area().abs();
if !closed || !(3..=SEAMED_FILL_EDGES).contains(&points.len()) || points.iter().any(|p| !p.is_finite()) || !area.is_finite() || area <= 1e-9 {
return None;
}
let side = |a: Point, b: Point, c: Point| (b - a).cross(c - a);
let touch = |a: Point, b: Point, c: Point, d: Point| {
let boxes = a.x.min(b.x) <= c.x.max(d.x) && c.x.min(d.x) <= a.x.max(b.x) && a.y.min(b.y) <= c.y.max(d.y) && c.y.min(d.y) <= a.y.max(b.y);
boxes && side(a, b, c) * side(a, b, d) <= 0.0 && side(c, d, a) * side(c, d, b) <= 0.0
};
let n = points.len();
let edge = |i: usize| (points[i], points[(i + 1) % n]);
for i in 0..n {
for j in i + 2..n {
if i == 0 && j == n - 1 {
continue;
}
let ((a, b), (c, d)) = (edge(i), edge(j));
if touch(a, b, c, d) {
return None;
}
}
}
let fill = footprint(&bez, seam);
(fill.bounding_box().is_finite() && fill.area().abs() >= area).then(|| vectorcraft_geom::PathData::from_bezpath(&fill))
}
/// Colored-light facets `faces` (in drawing order) drawn over an underlay of themselves, the same
/// facets grown to seams [`UNDERLAY_SEAM`] wide, all clipped once to the union of the facets'
/// footprints. Where two facets' antialiased edges meet, the underlay fills what they leave
@@ -227,7 +283,9 @@ pub(crate) fn underlay(
/// Composite smooth solid-fill facets with one clip and three channel groups per surface. In each
/// channel, an underlay of the facets with wide seams lies under the facets themselves, so a
/// shared edge is opaque in that channel's own light before the channels are screened together;
/// the black backdrop shows only outside the surface, which the clip removes.
/// the black backdrop shows only outside the surface, which the clip removes. Each facet over the
/// underlay is one fill of the facet and its seam together ([`seamed_fill`]), or a fill and a seam
/// stroke where that is `None`.
pub(crate) fn solid_surface(id: vectorcraft_doc::NodeId, facets: &[vectorcraft_three_d::Facet], paint: &Paint) -> vectorcraft_doc::Node {
use kurbo::Shape;
use std::sync::Arc;
@@ -251,6 +309,8 @@ pub(crate) fn solid_surface(id: vectorcraft_doc::NodeId, facets: &[vectorcraft_t
let clip = Node::path(id, PathData::from_bezpath(&boundary), Appearance::default());
let bounds = bounds.unwrap_or(Rect::ZERO).inflate(1.0, 1.0);
let cover = |paint| Node::path(id, shapes::rectangle(bounds), Appearance::basic(paint, Paint::None, 0.0));
let seam = |face: &vectorcraft_three_d::Facet| (face.thickness() / 2.0).clamp(0.05, 0.75);
let exact: Vec<Option<PathData>> = facets.iter().map(|face| seamed_fill(&face.path(), seam(face))).collect();
let channels = (0..3)
.map(|channel| {
let mut color = [0.0; 3];
@@ -286,7 +346,17 @@ pub(crate) fn solid_surface(id: vectorcraft_doc::NodeId, facets: &[vectorcraft_t
};
let mut children = vec![Arc::new(cover(Paint::solid(Color::BLACK)))];
children.extend(draw(|_| UNDERLAY_SEAM));
children.extend(draw(|face| (face.thickness() / 2.0).clamp(0.05, 0.75)));
children.extend(facets.iter().zip(&exact).zip(&paints).map(|((face, fill), paint)| {
let mut items = vec![AppearanceItem::Fill(FillLayer::new(paint.clone()))];
let path = match fill {
Some(fill) => fill.clone(),
None => {
items.push(AppearanceItem::Stroke(seam_stroke(paint.clone(), seam(face))));
face.path()
}
};
Arc::new(Node::path(id, path, Appearance { items, ..Default::default() }))
}));
let mut group = Node::group(id, children);
group.isolate = true;
group.blend = BlendMode::Screen;
@@ -529,4 +599,65 @@ mod tests {
}
}
}
/// A simple polygon facet and its 0.75 pt round seam are one nonzero-filled path: it covers the
/// facet and the 0.375 pt the seam reaches past each edge and corner, and nothing more, wound
/// either way and in a concave notch. A facet whose footprint would gain or lose area under a
/// nonzero fill (no area, crossing edges, a hole, curves) or a seam with no width has none.
#[test]
fn seamed_fill_covers_a_polygon_and_its_seam_once() {
use kurbo::Shape;
use vectorcraft_geom::BezPath;
let polygon = |points: &[(f64, f64)]| {
let mut bp = BezPath::new();
bp.move_to(points[0]);
for p in &points[1..] {
bp.line_to(*p);
}
bp.close_path();
bp
};
let covers = |fill: &vectorcraft_geom::PathData, x: f64, y: f64| fill.to_bezpath().winding(Point::new(x, y)) != 0;
let notched = [(0.0, 0.0), (10.0, 0.0), (10.0, 10.0), (6.0, 10.0), (6.0, 4.0), (4.0, 4.0), (4.0, 10.0), (0.0, 10.0)];
let mut reversed = notched;
reversed.reverse();
for points in [&notched[..], &reversed[..], &[(0.0, 0.0), (10.0, 0.0), (0.0, 10.0)][..]] {
let fill = seamed_fill(&vectorcraft_geom::PathData::from_bezpath(&polygon(points)), 0.75).expect("a simple polygon");
let contours = fill.to_bezpath().elements().iter().filter(|el| matches!(el, kurbo::PathEl::MoveTo(_))).count();
assert!(contours >= 2, "{points:?}: the seam's outline and the facet, {contours} contours");
for (x, y) in [(1.0, 1.0), (2.0, 0.5), (5.0, -0.3), (-0.3, 5.0), (-0.2, -0.2)] {
assert!(covers(&fill, x, y), "{points:?} covers ({x},{y})");
}
for (x, y) in [(5.0, -0.5), (-0.5, 5.0), (-0.3, -0.3), (12.0, 12.0)] {
assert!(!covers(&fill, x, y), "{points:?} leaves ({x},{y})");
}
}
let fill = seamed_fill(&vectorcraft_geom::PathData::from_bezpath(&polygon(&notched)), 0.75).unwrap();
assert!(covers(&fill, 4.3, 8.0) && covers(&fill, 5.0, 4.3), "the notch's edges carry the seam");
assert!(!covers(&fill, 5.0, 8.0), "the notch stays open");
let square = polygon(&[(0.0, 0.0), (10.0, 0.0), (10.0, 10.0), (0.0, 10.0)]);
let mut holed = square.clone();
holed.extend(polygon(&[(3.0, 3.0), (3.0, 7.0), (7.0, 7.0), (7.0, 3.0)]).iter());
let mut curved = BezPath::new();
curved.move_to((0.0, 0.0));
curved.quad_to((5.0, -5.0), (10.0, 0.0));
curved.line_to((5.0, 10.0));
curved.close_path();
let mut open = BezPath::new();
open.move_to((0.0, 0.0));
open.line_to((10.0, 0.0));
open.line_to((10.0, 10.0));
for (name, path, seam) in [
("sliver", polygon(&[(0.0, 0.0), (10.0, 0.0), (5.0, 0.0)]), 0.75),
("bow tie", polygon(&[(0.0, 0.0), (10.0, 10.0), (10.0, 0.0), (0.0, 10.0)]), 0.75),
("a vertex visited twice", polygon(&[(0.0, 0.0), (10.0, 0.0), (5.0, 5.0), (10.0, 10.0), (0.0, 10.0), (5.0, 5.0)]), 0.75),
("hole", holed, 0.75),
("curve", curved, 0.75),
("open", open, 0.75),
("no seam", square.clone(), 0.0),
("endless seam", square, f64::INFINITY),
] {
assert!(seamed_fill(&vectorcraft_geom::PathData::from_bezpath(&path), seam).is_none(), "{name}");
}
}
}
+16 -3
View File
@@ -1766,11 +1766,24 @@ fn a_colored_solid_face_on_its_own_keeps_the_composites_colors() {
assert_eq!(ramps, one.iter().skip(1).collect::<Vec<_>>(), "the same light ramps");
// The single face's clip holds its seam: the clip reaches past the facet by half the seam.
let seam = (face.thickness() / 2.0).clamp(0.05, 0.75);
let widths: Vec<f64> = paints(&batch).into_iter().filter_map(|(_, w)| w).filter(|w| *w > 0.0 && *w != crate::lighting::UNDERLAY_SEAM).collect();
assert!(widths.len() == 3 && widths.iter().all(|w| (w - seam).abs() < 1e-12), "{widths:?}");
let NodeKind::Group { children, .. } = &single.kind else { panic!("a clip group") };
let (clip, facet) = (children[0].path_data().and_then(PathData::bounds).unwrap(), face.path().bounds().unwrap());
assert!((clip.x0 - (facet.x0 - seam / 2.0)).abs() < 0.1 && (clip.x1 - (facet.x1 + seam / 2.0)).abs() < 0.1, "{clip:?} {facet:?}");
let reaches = |b: Rect| (b.x0 - (facet.x0 - seam / 2.0)).abs() < 0.1 && (b.x1 - (facet.x1 + seam / 2.0)).abs() < 0.1;
assert!(reaches(clip), "{clip:?} {facet:?}");
// In the composite, the facet over the underlay is one fill of the facet and its seam in each
// channel: no seam stroke of its own, and a path that reaches past the facet by half the seam.
let widths: Vec<f64> = paints(&batch).into_iter().filter_map(|(_, w)| w).filter(|w| *w > 0.0 && *w != crate::lighting::UNDERLAY_SEAM).collect();
assert!(widths.is_empty(), "{widths:?}");
let mut seamed = vec![];
batch.walk(&mut |n| {
if let [AppearanceItem::Fill(f)] = n.appearance.items.as_slice()
&& f.paint != Paint::solid(Color::BLACK)
&& f.paint != paint
{
seamed.extend(n.path_data().and_then(PathData::bounds));
}
});
assert!(seamed.len() == 3 && seamed.iter().all(|b| reaches(*b)), "{seamed:?} {facet:?}");
}
/// `square_node` moved right by `dx`, with id `id` and fill `fill`.
@@ -579,6 +579,29 @@ fn colored_light_surfaces_match_supersampling_at_internal_facet_edges() {
assert!(failures.is_empty(), "{failures:#?}");
}
/// Inflate turned so its facets narrow to slivers at the edges, in an opaque solid fill under two
/// colored lights, against 3x averaged down ([`seams_against_supersampled`]): each facet and its
/// seam cover an edge pixel once together, so at most one RGB channel is more than 3 levels off, by
/// no more than 4, and no alpha is off.
#[test]
fn turned_colored_inflate_facets_and_seams_cover_their_edges_once() {
let lights = json!([
{"color":"#ff0000","rotation":135,"height":35,"intensity":70},
{"color":"#0000ff","rotation":-35,"height":25,"intensity":65}
]);
let (mut s, _) = setup("threeD.inflate");
let change = json!({"index":0,"item":null,"params":{"ambient":10,"lights":lights,"rotationX":50,"rotationY":35}});
s.execute("effect.setParams", &change).unwrap();
s.execute("paint.setFill", &json!({"color":"#ffcc66"})).unwrap();
let image = render(doc(&s));
let (measured, color, max, holes, worst) = seams_against_supersampled(doc(&s), &image);
assert!(measured > 2_000, "{measured} pixels inside");
assert!(
holes == 0 && color <= 1 && max <= 4,
"of {measured} pixels, {color} channels off by more than 3 (at most {max}), {holes} alphas off: {worst:?}"
);
}
/// The closed facet edges of colored-light surfaces survive SVG and PDF, read back and drawn at
/// 1:1, and Expand Appearance: inside the art, every pixel stays opaque and within 6 levels of the
/// live art.
+2
View File
@@ -2225,6 +2225,8 @@ Moved from the ROADMAP's "Shipped so far" on 2026-10-10. Update it when a featur
Evidence for the shared light Color: geometry tests in `tests_lighting`, effect paint tests, command/save/undo/expand and SVG/PDF checks in `three_d_light_color`, and headless picker/reset/preview tests in `tests_revolve`. Revolve keeps smooth colored vector ramps and continuous flat-lit caps. Inflate geometry tests check spherical lighting, shared vertex/edge continuity, concave cell coverage, puffed backs at perspective and legacy single-light/list equivalence; rendered command/export regressions cover smooth shading and editable expansion. Four legacy white-light PNGs matched main byte for byte when this was measured (2026-10-17); since every 3D seam took round joins (2026-11-12), white-light output is expected to differ from them at facet corners ([Seams](three-d.md#seams)); the four scenes were not exported again to measure it; [release measurements](3d-lighting-performance.md) record the colored solid-fill batch and the remaining source-gradient cost. Colored Inflate's facets lie over an underlay of themselves under one clip, so their shared edges show neither the black channel backdrop nor the background ([Seams](three-d.md#seams)); `three_d_light_color::colored_light_surfaces_match_supersampling_at_internal_facet_edges` compares 1:1 with 3x averaged down, and `colored_light_seams_stay_closed_in_svg_pdf_and_expansion` checks SVG, PDF and Expand Appearance. Colored Revolve's faces lie over an underlay of themselves the same way, run by run in a group and with mapped graphics kept after their faces; `colored_revolve_faces_leave_no_alpha_holes_at_one_to_one` checks that no pixel inside the art lets the background through at 1:1 where 3x averaged down is opaque, live, read back from SVG and PDF, and expanded.
Colored Inflate's composited facets that are simple polygons are each one filled path of the facet and its round seam, so a facet edge pixel is covered once instead of by a fill and then a same-color stroke ([Seams](three-d.md#seams)); `three_d_light_color::turned_colored_inflate_facets_and_seams_cover_their_edges_once` bounds the turned (X 50°, Y 35°) solid fill against 3x averaged down, and `lighting::tests::seamed_fill_covers_a_polygon_and_its_seam_once` checks the path's coverage and the facets that keep a fill and a stroke.
## Eyedropper character dimensions (2026-11-11)
`appearance.copyFrom` reads the source's effective character dimensions and converts them into each target's local text space, preserving target placement, rotation, shear and reflection (#6404). Point and area text, grouped text, highlighted ranges, reverse and remembered sampling, attribute opt-outs, Undo/Redo and native-file round trips are regression-tested. Picks with unrepresentable dimensions or text that would extend beyond the canvas leave the document, paint defaults and remembered sample intact. Paint-only copying remains available. Tests: `crates/engine/src/tests/eyedropper.rs` (`scaled_type_pick_*`). Scores and estimates unchanged.
+5
View File
@@ -157,6 +157,11 @@ How a facet carries its seam depends on the lights and the paint:
and from places where the light changes sharply, a pixel drawn at 1:1 is opaque where the 3x art is opaque, and
otherwise its alpha is within 3 levels. Inflate seen straight on with an opaque fill also has each color channel
within 3 levels (VectorCraft's own measure).
In the composite, a facet that is one closed polygon whose edges do not cross is drawn over the underlay as one
filled path, the facet and the outline of its round seam together, so a pixel at its edge is covered once and not
by a fill and then by a stroke of the same color. Other facets keep a fill and a seam stroke. Turned to X 50°,
Y 35° in an opaque fill, at most one color channel sample is more than 3 levels off the 3x art, and by at most 4
(VectorCraft's own measure).
- Extrude & Bevel (Classic), Rotate (Classic) and Revolve (Classic): a path and a seam for every face; gradient and
pattern faces seam only the base paint, and the shading and highlight layers cover the face alone. The Wireframe
surface draws edges only, with no seam.