mirror of
https://github.com/GraphiteEditor/Graphite.git
synced 2026-12-06 08:13:40 -10:00
* Borrow the input in LazyInput::eval, as Node::eval does * Allow wrapping a network in scope without the render node * Implement CacheHash for BTreeMap * Replace the CanvasFrame render output with canvas info on the response * Add OwnedContextImpl::with_varargs for a context with several varargs
628 lines
23 KiB
Rust
628 lines
23 KiB
Rust
use crate::gcore::Context;
|
|
use core::f64::consts::TAU;
|
|
use core_types::list::{Item, List};
|
|
use core_types::transform::{ApplyTransform, Footprint};
|
|
use core_types::{ATTR_TRANSFORM, CloneVarArgs, Color, Ctx, ExtractAll, ExtractFootprint, InjectFootprint, InjectVarArgs, Interrupt, NodeError, OwnedContextImpl, Poll};
|
|
use glam::{DAffine2, DVec2, I64Vec2};
|
|
use graphic_types::Generator;
|
|
use graphic_types::{Artboard, Graphic, Vector};
|
|
use raster_types::{CPU, GPU, Raster};
|
|
use vector_types::Gradient;
|
|
use vector_types::vector::{no_paths, paths_from_list_row_attributes};
|
|
|
|
#[node_macro::node(category("Graphic"), icon("NodeRepeat"))]
|
|
fn repeat<T: Send + Clone + 'static>(
|
|
ctx: impl ExtractAll + CloneVarArgs + Ctx,
|
|
#[implementations(
|
|
Context -> List<String>,
|
|
Context -> List<bool>,
|
|
Context -> List<f64>,
|
|
Context -> List<i64>,
|
|
Context -> List<DVec2>,
|
|
Context -> List<DAffine2>,
|
|
Context -> List<Vector>,
|
|
Context -> List<Graphic>,
|
|
Context -> List<Raster<CPU>>,
|
|
Context -> List<Raster<GPU>>,
|
|
Context -> List<Color>,
|
|
Context -> List<Gradient>,
|
|
Context -> List<Generator>,
|
|
Context -> List<Artboard>,
|
|
)]
|
|
content: impl Node<Context<'static>, Output = List<T>>,
|
|
#[default(1)]
|
|
#[hard(1..)]
|
|
count: Item<i64>,
|
|
reverse: Item<bool>,
|
|
) -> Result<List<T>, Interrupt> {
|
|
// Someday this node can have the option to generate infinitely instead of a fixed count (basically `std::iter::repeat`).
|
|
|
|
let (count, reverse) = (count.into_element(), reverse.into_element());
|
|
let count = count as usize;
|
|
|
|
let mut result_list = List::new();
|
|
|
|
for index in 0..count {
|
|
let index = if reverse { count - index - 1 } else { index };
|
|
|
|
let new_ctx = OwnedContextImpl::from(ctx.clone()).with_index(index);
|
|
let generated_content = content.eval(&new_ctx.into_context())?;
|
|
|
|
for generated_row in generated_content.into_iter() {
|
|
result_list.push(generated_row);
|
|
}
|
|
}
|
|
|
|
Ok(result_list)
|
|
}
|
|
|
|
#[node_macro::node(category("Graphic"), icon("NodeRepeatArray"))]
|
|
fn repeat_array<T: Send + Clone + 'static>(
|
|
ctx: impl ExtractAll + CloneVarArgs + Ctx,
|
|
#[implementations(
|
|
Context -> List<String>,
|
|
Context -> List<bool>,
|
|
Context -> List<f64>,
|
|
Context -> List<i64>,
|
|
Context -> List<DVec2>,
|
|
Context -> List<DAffine2>,
|
|
Context -> List<Vector>,
|
|
Context -> List<Graphic>,
|
|
Context -> List<Raster<CPU>>,
|
|
Context -> List<Raster<GPU>>,
|
|
Context -> List<Color>,
|
|
Context -> List<Gradient>,
|
|
Context -> List<Generator>,
|
|
Context -> List<Artboard>,
|
|
)]
|
|
content: impl Node<Context<'static>, Output = List<T>>,
|
|
#[unit(" px")]
|
|
#[default(100., 100.)]
|
|
// TODO: When using a custom Inspector layout in document_node_definitions.rs and this default is set, the widget weirdly doesn't show up in the Inspector. Investigation is needed.
|
|
direction: Item<DVec2>,
|
|
#[unit("°")]
|
|
#[range]
|
|
#[soft(-360..360)]
|
|
angle: Item<f64>,
|
|
#[default(5)]
|
|
#[hard(1..)]
|
|
count: Item<i64>,
|
|
) -> Result<List<T>, Interrupt> {
|
|
let (direction, angle, count) = (direction.into_element(), angle.into_element(), count.into_element());
|
|
let angle = angle.to_radians();
|
|
// A single copy has no steps between copies, so the denominator is kept at 1 to avoid `0. / 0.` producing a NaN transform
|
|
let total = (count - 1).max(1) as f64;
|
|
|
|
let mut result_list = List::new();
|
|
|
|
for index in 0..count {
|
|
let angle = index as f64 * angle / total;
|
|
let translation = index as f64 * direction / total;
|
|
let transform = DAffine2::from_angle(angle) * DAffine2::from_translation(translation);
|
|
|
|
let new_ctx = OwnedContextImpl::from(ctx.clone()).with_index(index as usize);
|
|
let generated_content = content.eval(&new_ctx.into_context())?;
|
|
|
|
for row_index in 0..generated_content.len() {
|
|
let Some(mut row) = generated_content.clone_item(row_index) else { continue };
|
|
|
|
let local_transform: DAffine2 = row.attribute_cloned_or_default(ATTR_TRANSFORM);
|
|
let local_translation = DAffine2::from_translation(local_transform.translation);
|
|
let local_matrix = DAffine2::from_mat2(local_transform.matrix2);
|
|
*row.attribute_mut_or_insert_default(ATTR_TRANSFORM) = local_translation * transform * local_matrix;
|
|
|
|
result_list.push(row);
|
|
}
|
|
}
|
|
|
|
Ok(result_list)
|
|
}
|
|
|
|
const MAX_TILE_COUNT: f64 = 10_000.;
|
|
const DEGENERATE_VIEWPORT_CELL_COUNT: f64 = 4.;
|
|
|
|
/// The range of cells covering the viewport, or a fixed block of cells at the origin when the footprint can't map the viewport into local space.
|
|
fn visible_cell_range(footprint: &Footprint, spacing: DVec2) -> (DVec2, DVec2) {
|
|
let viewport_bounds = footprint.viewport_bounds_in_local_space();
|
|
let viewport_size = viewport_bounds.size();
|
|
let is_degenerate = !(viewport_size.x > 0. && viewport_size.y > 0. && viewport_size.is_finite() && viewport_bounds.start.is_finite());
|
|
if is_degenerate {
|
|
return (DVec2::ZERO, DVec2::splat(DEGENERATE_VIEWPORT_CELL_COUNT));
|
|
}
|
|
|
|
// One cell of padding on each side keeps content reaching past its own cell from being culled at the viewport edges
|
|
let first_cell = (viewport_bounds.start / spacing).floor() - DVec2::ONE;
|
|
let last_cell = (viewport_bounds.end / spacing).ceil() + DVec2::ONE;
|
|
(first_cell, last_cell)
|
|
}
|
|
|
|
/// Shrinks the cell range to a window of at most `max_count` cells centered on the original range.
|
|
fn centered_cell_window(first_cell: DVec2, last_cell: DVec2, max_count: f64) -> (I64Vec2, I64Vec2) {
|
|
let cell_count = last_cell - first_cell;
|
|
let scale = (max_count / (cell_count.x * cell_count.y)).sqrt();
|
|
|
|
let width = (cell_count.x * scale).floor().clamp(1., cell_count.x.min(max_count));
|
|
let height = (max_count / width).floor().clamp(1., cell_count.y);
|
|
let window = DVec2::new(width, height);
|
|
|
|
let start = (first_cell + (cell_count - window) / 2.).floor();
|
|
(start.as_i64vec2(), (start + window).as_i64vec2())
|
|
}
|
|
|
|
/// Repeats the content on a grid that covers the visible viewport.
|
|
#[node_macro::node(category(""), icon("NodeRepeatArray"))]
|
|
fn tile<T: Send + Clone + 'static>(
|
|
ctx: impl ExtractAll + CloneVarArgs + Ctx + ExtractFootprint + InjectFootprint,
|
|
#[implementations(
|
|
Context -> List<Vector>,
|
|
Context -> List<Graphic>,
|
|
Context -> List<Raster<CPU>>,
|
|
Context -> List<Raster<GPU>>,
|
|
Context -> List<Color>,
|
|
Context -> List<Gradient>,
|
|
Context -> List<Generator>,
|
|
)]
|
|
content: impl Node<Context<'static>, Output = List<T>>,
|
|
#[unit(" px")]
|
|
#[default(100., 100.)]
|
|
spacing: Item<DVec2>,
|
|
) -> Poll<List<T>> {
|
|
let spacing = spacing.into_element();
|
|
if spacing.x <= 0. || spacing.y <= 0. || !spacing.is_finite() {
|
|
return Poll::Final(List::new());
|
|
}
|
|
|
|
let footprint = *ctx.footprint();
|
|
let (first_cell, last_cell) = visible_cell_range(&footprint, spacing);
|
|
|
|
let cell_count = last_cell - first_cell;
|
|
let visible_tile_count = cell_count.x * cell_count.y;
|
|
let exceeds_limit = visible_tile_count > MAX_TILE_COUNT;
|
|
let (first_cell, last_cell) = if exceeds_limit {
|
|
centered_cell_window(first_cell, last_cell, MAX_TILE_COUNT)
|
|
} else {
|
|
(first_cell.as_i64vec2(), last_cell.as_i64vec2())
|
|
};
|
|
|
|
let mut result_list = List::new();
|
|
let mut partial = false;
|
|
let mut upstream_error = None;
|
|
|
|
for y in first_cell.y..last_cell.y {
|
|
for x in first_cell.x..last_cell.x {
|
|
let tile_origin = DVec2::new(x as f64, y as f64) * spacing;
|
|
let offset = DAffine2::from_translation(tile_origin);
|
|
|
|
let mut tile_footprint = footprint;
|
|
tile_footprint.apply_transform(&offset);
|
|
|
|
let tile_ctx = OwnedContextImpl::from(ctx.clone()).with_footprint(tile_footprint).with_position(tile_origin);
|
|
let generated_content = match content.eval(&tile_ctx.into_context()) {
|
|
Poll::Final(value) => value,
|
|
Poll::Partial(value) => {
|
|
partial = true;
|
|
value
|
|
}
|
|
Poll::Fallback(boxed) => {
|
|
let (value, error) = *boxed;
|
|
upstream_error.get_or_insert(error);
|
|
value
|
|
}
|
|
Poll::Pending => return Poll::Pending,
|
|
// Like `Status`, an error after a partial result waits for the final inputs before it is reported
|
|
Poll::Error(error) => return if partial { Poll::Pending } else { Poll::Error(error) },
|
|
};
|
|
|
|
for mut generated_row in generated_content.into_iter() {
|
|
let local_transform: DAffine2 = generated_row.attribute_cloned_or_default(ATTR_TRANSFORM);
|
|
*generated_row.attribute_mut_or_insert_default(ATTR_TRANSFORM) = offset * local_transform;
|
|
|
|
result_list.push(generated_row);
|
|
}
|
|
}
|
|
}
|
|
|
|
if partial {
|
|
Poll::Partial(result_list)
|
|
} else if exceeds_limit {
|
|
let message = format!("The view needs {visible_tile_count:.0} tiles but at most {MAX_TILE_COUNT} are drawn. Zoom in or increase the spacing.");
|
|
let error = NodeError::new(message).with_upstream(upstream_error.into_iter().collect());
|
|
Poll::Fallback(Box::new((result_list, error)))
|
|
} else if let Some(error) = upstream_error {
|
|
Poll::Fallback(Box::new((result_list, error)))
|
|
} else {
|
|
Poll::Final(result_list)
|
|
}
|
|
}
|
|
|
|
#[node_macro::node(category("Graphic"), icon("NodeRepeatRadial"))]
|
|
fn repeat_radial<T: Send + Clone + 'static>(
|
|
ctx: impl ExtractAll + CloneVarArgs + Ctx,
|
|
#[implementations(
|
|
Context -> List<String>,
|
|
Context -> List<bool>,
|
|
Context -> List<f64>,
|
|
Context -> List<i64>,
|
|
Context -> List<DVec2>,
|
|
Context -> List<DAffine2>,
|
|
Context -> List<Vector>,
|
|
Context -> List<Graphic>,
|
|
Context -> List<Raster<CPU>>,
|
|
Context -> List<Raster<GPU>>,
|
|
Context -> List<Color>,
|
|
Context -> List<Gradient>,
|
|
Context -> List<Generator>,
|
|
Context -> List<Artboard>,
|
|
)]
|
|
content: impl Node<Context<'static>, Output = List<T>>,
|
|
#[unit("°")]
|
|
#[range]
|
|
#[soft(-180..180)]
|
|
start_angle: Item<f64>,
|
|
#[unit(" px")]
|
|
#[default(5)]
|
|
radius: Item<f64>,
|
|
#[default(5)]
|
|
#[hard(1..)]
|
|
count: Item<i64>,
|
|
) -> Result<List<T>, Interrupt> {
|
|
let (start_angle, radius, count) = (start_angle.into_element(), radius.into_element(), count.into_element());
|
|
|
|
let mut result_list = List::new();
|
|
|
|
for index in 0..count {
|
|
let angle = DAffine2::from_angle((TAU / count as f64) * index as f64 + start_angle.to_radians());
|
|
let translation = DAffine2::from_translation(radius * DVec2::Y);
|
|
let transform = angle * translation;
|
|
|
|
let new_ctx = OwnedContextImpl::from(ctx.clone()).with_index(index as usize);
|
|
let generated_content = content.eval(&new_ctx.into_context())?;
|
|
|
|
for row_index in 0..generated_content.len() {
|
|
let Some(mut row) = generated_content.clone_item(row_index) else { continue };
|
|
|
|
let local_transform: DAffine2 = row.attribute_cloned_or_default(ATTR_TRANSFORM);
|
|
let local_translation = DAffine2::from_translation(local_transform.translation);
|
|
let local_matrix = DAffine2::from_mat2(local_transform.matrix2);
|
|
*row.attribute_mut_or_insert_default(ATTR_TRANSFORM) = local_translation * transform * local_matrix;
|
|
|
|
result_list.push(row);
|
|
}
|
|
}
|
|
|
|
Ok(result_list)
|
|
}
|
|
|
|
#[node_macro::node(category("Graphic"), icon("NodeRepeatOnPoints"), name("Repeat on Points"))]
|
|
fn repeat_on_points<T: Send + Clone + 'static>(
|
|
ctx: impl ExtractAll + CloneVarArgs + Sync + Ctx + InjectVarArgs,
|
|
points: List<Vector>,
|
|
#[implementations(
|
|
Context -> List<String>,
|
|
Context -> List<bool>,
|
|
Context -> List<f64>,
|
|
Context -> List<i64>,
|
|
Context -> List<DVec2>,
|
|
Context -> List<DAffine2>,
|
|
Context -> List<Vector>,
|
|
Context -> List<Graphic>,
|
|
Context -> List<Raster<CPU>>,
|
|
Context -> List<Raster<GPU>>,
|
|
Context -> List<Color>,
|
|
Context -> List<Gradient>,
|
|
Context -> List<Generator>,
|
|
Context -> List<Artboard>,
|
|
)]
|
|
content: impl Node<Context<'static>, Output = List<T>>,
|
|
reverse: Item<bool>,
|
|
) -> Result<List<T>, Interrupt> {
|
|
let reverse = reverse.into_element();
|
|
|
|
let mut result_list = List::new();
|
|
|
|
for points_index in 0..points.len() {
|
|
let Some(points_element) = points.element(points_index) else { continue };
|
|
let transform: DAffine2 = points.attribute_cloned_or_default(ATTR_TRANSFORM, points_index);
|
|
|
|
let mut iteration = |index, point| -> Result<(), Interrupt> {
|
|
let transformed_point = transform.transform_point2(point);
|
|
|
|
let new_ctx = OwnedContextImpl::from(ctx.clone()).with_index(index).with_position(transformed_point);
|
|
let generated_content = content.eval(&new_ctx.into_context())?;
|
|
|
|
for mut generated_row in generated_content.into_iter() {
|
|
generated_row.attribute_mut_or_insert_default::<DAffine2>(ATTR_TRANSFORM).translation = transformed_point;
|
|
result_list.push(generated_row);
|
|
}
|
|
Ok(())
|
|
};
|
|
|
|
let paths = paths_from_list_row_attributes(&points, points_index).unwrap_or_else(|| no_paths());
|
|
let positions = points_element.anchor_positions(paths);
|
|
let range = positions.iter().enumerate();
|
|
if reverse {
|
|
for (index, &point) in range.rev() {
|
|
iteration(index, point)?;
|
|
}
|
|
} else {
|
|
for (index, &point) in range {
|
|
iteration(index, point)?;
|
|
}
|
|
}
|
|
}
|
|
|
|
Ok(result_list)
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod test {
|
|
use super::*;
|
|
use core_types::list::Item;
|
|
use core_types::transform::Footprint;
|
|
use core_types::{LazyInput, Node, Poll, Status};
|
|
use glam::DVec2;
|
|
use graphene_core::ReadPositionNode;
|
|
use graphic_types::Vector;
|
|
use kurbo::Shape;
|
|
use kurbo::{BezPath, DEFAULT_ACCURACY, Rect};
|
|
use vector_nodes::generator_nodes::RectangleNode;
|
|
use vector_types::vector::Path;
|
|
use vector_types::vector::misc::BoxCorners;
|
|
|
|
fn vector_node_from_bezpath(bezpath: BezPath) -> List<Vector> {
|
|
List::new_from_item(Vector::item_from_bezpath(&bezpath))
|
|
}
|
|
|
|
/// The paths riding the row, so geometry queries can resolve against them.
|
|
fn row_paths(list: &List<Vector>, index: usize) -> &List<Path> {
|
|
paths_from_list_row_attributes(list, index).unwrap_or_else(|| no_paths())
|
|
}
|
|
|
|
#[derive(Clone)]
|
|
pub struct ValueNode<T: Clone>(T);
|
|
|
|
impl<I, T: Clone> Node<I> for ValueNode<T> {
|
|
type Output = T;
|
|
fn eval(&self, _input: &I) -> Poll<T> {
|
|
Poll::Final(self.0.clone())
|
|
}
|
|
}
|
|
|
|
// Raises a generator's rank-0 `Item<Vector>` output to a singleton `List<Vector>` for the still-list-typed Repeat on Points content connector
|
|
#[derive(Clone)]
|
|
pub struct RaiseToListNode<N>(N);
|
|
|
|
impl<I, N: Node<I, Output = Item<Vector>>> Node<I> for RaiseToListNode<N> {
|
|
type Output = List<Vector>;
|
|
fn eval(&self, input: &I) -> Poll<List<Vector>> {
|
|
self.0.eval(input).map(Into::into)
|
|
}
|
|
}
|
|
|
|
/// Test helper that extracts the Y component of an upstream node's `Item<DVec2>` output.
|
|
#[derive(Clone)]
|
|
struct ExtractYNode<Position>(Position);
|
|
|
|
impl<I: Ctx, Position> Node<I> for ExtractYNode<Position>
|
|
where
|
|
Position: Node<I, Output = Item<DVec2>>,
|
|
{
|
|
type Output = Item<f64>;
|
|
fn eval(&self, input: &I) -> Poll<Item<f64>> {
|
|
self.0.eval(input).map(|position| Item::new_from_element(position.element().y))
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn repeat_on_points_test() {
|
|
let context = OwnedContextImpl::default().into_context();
|
|
let rect = RectangleNode::new(
|
|
ValueNode(()),
|
|
ExtractYNode(ReadPositionNode::new(ValueNode(()), ValueNode(Item::new_from_element(0_i64)))),
|
|
ValueNode(Item::new_from_element(2_f64)),
|
|
ValueNode(Item::new_from_element(BoxCorners::default())),
|
|
ValueNode(Item::new_from_element(false)),
|
|
ValueNode(Item::new_from_element(false)),
|
|
);
|
|
|
|
let positions = [DVec2::new(40., 20.), DVec2::ONE, DVec2::new(-42., 9.), DVec2::new(10., 345.)];
|
|
let mut polyline = BezPath::new();
|
|
polyline.move_to((positions[0].x, positions[0].y));
|
|
for position in &positions[1..] {
|
|
polyline.line_to((position.x, position.y));
|
|
}
|
|
let points = vector_node_from_bezpath(polyline);
|
|
let status = Status::new();
|
|
let generated = super::repeat_on_points(context, points, LazyInput::new(&RaiseToListNode(rect), &status, 1), Item::new_from_element(false)).unwrap();
|
|
assert_eq!(generated.len(), positions.len());
|
|
for (position, index) in positions.into_iter().zip(0..generated.len()) {
|
|
let bounds = generated
|
|
.element(index)
|
|
.unwrap()
|
|
.bounding_box_with_transform(row_paths(&generated, index), generated.attribute_cloned_or_default(ATTR_TRANSFORM, index))
|
|
.unwrap();
|
|
assert!(position.abs_diff_eq((bounds[0] + bounds[1]) / 2., 1e-10));
|
|
assert_eq!((bounds[1] - bounds[0]).x, position.y);
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn repeat() {
|
|
let direction = DVec2::X * 1.5;
|
|
let count = 3;
|
|
let context = OwnedContextImpl::default().into_context();
|
|
let status = Status::new();
|
|
let repeated = super::repeat_array(
|
|
context,
|
|
LazyInput::new(&ValueNode(vector_node_from_bezpath(Rect::new(0., 0., 1., 1.).to_path(DEFAULT_ACCURACY))), &status, 0),
|
|
Item::new_from_element(direction),
|
|
Item::new_from_element(0.),
|
|
Item::new_from_element(count),
|
|
)
|
|
.unwrap();
|
|
let vector_list = List::new_from_item(vector_nodes::combine_paths(Footprint::default(), List::new_from_element(Graphic::VectorList(repeated))));
|
|
let vector = vector_list.element(0).unwrap();
|
|
let paths = row_paths(&vector_list, 0);
|
|
assert_eq!(vector.stroke_manipulator_groups(paths).count(), 3);
|
|
for (index, (manipulator_groups, _)) in vector.stroke_manipulator_groups(paths).enumerate() {
|
|
assert!((manipulator_groups[0].anchor - direction * index as f64 / (count - 1) as f64).length() < 1e-5);
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn repeat_single_copy() {
|
|
let context = OwnedContextImpl::default().into_context();
|
|
let status = Status::new();
|
|
let repeated = super::repeat_array(
|
|
context,
|
|
LazyInput::new(&ValueNode(vector_node_from_bezpath(Rect::new(0., 0., 1., 1.).to_path(DEFAULT_ACCURACY))), &status, 0),
|
|
Item::new_from_element(DVec2::new(12., 10.)),
|
|
Item::new_from_element(45.),
|
|
Item::new_from_element(1),
|
|
)
|
|
.unwrap();
|
|
let vector_list = List::new_from_item(vector_nodes::combine_paths(Footprint::default(), List::new_from_element(Graphic::VectorList(repeated))));
|
|
let vector = vector_list.element(0).unwrap();
|
|
let paths = row_paths(&vector_list, 0);
|
|
assert_eq!(vector.stroke_manipulator_groups(paths).count(), 1);
|
|
|
|
let (manipulator_groups, _) = vector.stroke_manipulator_groups(paths).next().unwrap();
|
|
let anchor = manipulator_groups[0].anchor;
|
|
assert!(anchor.length() < 1e-5, "Expected the single copy to be untransformed, found anchor {anchor}");
|
|
}
|
|
|
|
#[test]
|
|
fn repeat_transform_position() {
|
|
let direction = DVec2::new(12., 10.);
|
|
let count = 8;
|
|
let context = OwnedContextImpl::default().into_context();
|
|
let status = Status::new();
|
|
let repeated = super::repeat_array(
|
|
context,
|
|
LazyInput::new(&ValueNode(vector_node_from_bezpath(Rect::new(0., 0., 1., 1.).to_path(DEFAULT_ACCURACY))), &status, 0),
|
|
Item::new_from_element(direction),
|
|
Item::new_from_element(0.),
|
|
Item::new_from_element(count),
|
|
)
|
|
.unwrap();
|
|
let vector_list = List::new_from_item(vector_nodes::combine_paths(Footprint::default(), List::new_from_element(Graphic::VectorList(repeated))));
|
|
let vector = vector_list.element(0).unwrap();
|
|
let paths = row_paths(&vector_list, 0);
|
|
assert_eq!(vector.stroke_manipulator_groups(paths).count(), 8);
|
|
for (index, (manipulator_groups, _)) in vector.stroke_manipulator_groups(paths).enumerate() {
|
|
assert!((manipulator_groups[0].anchor - direction * index as f64 / (count - 1) as f64).length() < 1e-5);
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn tile_covers_viewport() {
|
|
let footprint = Footprint {
|
|
transform: DAffine2::from_translation(DVec2::new(50., 0.)),
|
|
resolution: glam::UVec2::new(250, 100),
|
|
..Default::default()
|
|
};
|
|
let context = OwnedContextImpl::default().with_footprint(footprint).into_context();
|
|
let content = vector_node_from_bezpath(Rect::new(0., 0., 1., 1.).to_path(DEFAULT_ACCURACY));
|
|
|
|
let Poll::Final(tiled) = super::tile(context, &ValueNode(content), Item::new_from_element(DVec2::splat(100.))) else {
|
|
panic!("Expected a final result");
|
|
};
|
|
|
|
let offsets: Vec<DVec2> = (0..tiled.len()).map(|index| tiled.attribute_cloned_or_default::<DAffine2>(ATTR_TRANSFORM, index).translation).collect();
|
|
let expected: Vec<DVec2> = (-1..2).flat_map(|y| (-2..3).map(move |x| DVec2::new(x as f64, y as f64) * 100.)).collect();
|
|
assert_eq!(offsets, expected);
|
|
}
|
|
|
|
#[test]
|
|
fn tile_falls_back_to_centered_window_above_limit() {
|
|
let footprint = Footprint {
|
|
transform: DAffine2::IDENTITY,
|
|
resolution: glam::UVec2::new(1000, 500),
|
|
..Default::default()
|
|
};
|
|
let context = OwnedContextImpl::default().with_footprint(footprint).into_context();
|
|
let content = vector_node_from_bezpath(Rect::new(0., 0., 1., 1.).to_path(DEFAULT_ACCURACY));
|
|
|
|
let Poll::Fallback(boxed) = super::tile(context, &ValueNode(content), Item::new_from_element(DVec2::ONE)) else {
|
|
panic!("Expected a fallback result");
|
|
};
|
|
let (tiled, _) = *boxed;
|
|
assert!(tiled.len() as f64 <= MAX_TILE_COUNT);
|
|
assert!(tiled.len() as f64 > MAX_TILE_COUNT * 0.9);
|
|
|
|
let center_offsets = (0..tiled.len()).map(|index| tiled.attribute_cloned_or_default::<DAffine2>(ATTR_TRANSFORM, index).translation);
|
|
let mean_offset = center_offsets.fold(DVec2::ZERO, |sum, offset| sum + offset) / tiled.len() as f64;
|
|
assert!(
|
|
mean_offset.abs_diff_eq(DVec2::new(499.5, 249.5), 1.),
|
|
"Expected the window to be centered, found mean offset {mean_offset}"
|
|
);
|
|
}
|
|
|
|
fn tile_count(footprint: Footprint, spacing: DVec2) -> usize {
|
|
let context = OwnedContextImpl::default().with_footprint(footprint).into_context();
|
|
let content = vector_node_from_bezpath(Rect::new(0., 0., 1., 1.).to_path(DEFAULT_ACCURACY));
|
|
|
|
match super::tile(context, &ValueNode(content), Item::new_from_element(spacing)) {
|
|
Poll::Final(tiled) => tiled.len(),
|
|
Poll::Fallback(boxed) => boxed.0.len(),
|
|
_ => panic!("Expected a final or fallback result"),
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn tile_window_stays_within_limit_for_uneven_cell_counts() {
|
|
let footprint = Footprint {
|
|
transform: DAffine2::IDENTITY,
|
|
resolution: glam::UVec2::new(1_000_000, 3),
|
|
..Default::default()
|
|
};
|
|
|
|
assert!(tile_count(footprint, DVec2::ONE) as f64 <= MAX_TILE_COUNT);
|
|
}
|
|
|
|
#[test]
|
|
fn tile_survives_extreme_spacing() {
|
|
let footprint = Footprint {
|
|
transform: DAffine2::IDENTITY,
|
|
resolution: glam::UVec2::new(1000, 1000),
|
|
..Default::default()
|
|
};
|
|
|
|
assert!(tile_count(footprint, DVec2::splat(1e-17)) as f64 <= MAX_TILE_COUNT);
|
|
}
|
|
|
|
#[test]
|
|
fn tile_draws_fixed_area_for_degenerate_footprint() {
|
|
let expected = (DEGENERATE_VIEWPORT_CELL_COUNT * DEGENERATE_VIEWPORT_CELL_COUNT) as usize;
|
|
|
|
assert_eq!(tile_count(Footprint::BOUNDLESS, DVec2::splat(100.)), expected);
|
|
}
|
|
|
|
#[test]
|
|
fn repeat_radial() {
|
|
let context = OwnedContextImpl::default().into_context();
|
|
let status = Status::new();
|
|
let repeated = super::repeat_radial(
|
|
context,
|
|
LazyInput::new(&ValueNode(vector_node_from_bezpath(Rect::new(-1., -1., 1., 1.).to_path(DEFAULT_ACCURACY))), &status, 0),
|
|
Item::new_from_element(45.),
|
|
Item::new_from_element(4.),
|
|
Item::new_from_element(8),
|
|
)
|
|
.unwrap();
|
|
let vector_list = List::new_from_item(vector_nodes::combine_paths(Footprint::default(), List::new_from_element(Graphic::VectorList(repeated))));
|
|
let vector = vector_list.element(0).unwrap();
|
|
let paths = row_paths(&vector_list, 0);
|
|
assert_eq!(vector.stroke_manipulator_groups(paths).count(), 8);
|
|
|
|
for (index, (manipulator_groups, _)) in vector.stroke_manipulator_groups(paths).enumerate() {
|
|
let expected_angle = (index as f64 + 1.) * 45.;
|
|
|
|
let center = (manipulator_groups[0].anchor + manipulator_groups[2].anchor) / 2.;
|
|
let actual_angle = DVec2::Y.angle_to(center).to_degrees();
|
|
|
|
assert!((actual_angle - expected_angle).abs() % 360. < 1e-5, "Expected {expected_angle} found {actual_angle}");
|
|
}
|
|
}
|
|
}
|