Implement the Brush tool (#1099)

* Implement Brush Node

* Add color Input

* Add VectorPointsNode

* Add Erase Node

* Adapt compilation infrastructure to allow non Image Frame inputs

* Remove debug output from TransformNode

* Fix transform calculation

* Fix Blending by making the brush texture use associated alpha

* Code improvements and UX polish

* Rename Opacity to Flow

* Add erase option to brush node + fix freehand tool

* Fix crash

* Revert erase implementation

* Fix flattening id calculation

* Fix some transformation issues

* Fix changing the pivot location

* Fix vector data modify bounds

* Minor fn name cleanup

* Fix some tests

* Fix tests

---------

Co-authored-by: Keavon Chambers <keavon@keavon.com>
Co-authored-by: hypercube <0hypercube@gmail.com>
This commit is contained in:
Dennis Kobert 2023-04-11 10:35:21 +02:00 committed by Keavon Chambers
parent 758f757775
commit 589ff9a2d3
36 changed files with 1527 additions and 406 deletions

View file

@ -93,7 +93,7 @@ impl<N: Clone, O: StaticType> Clone for DowncastNode<O, N> {
impl<N: Copy, O: StaticType> Copy for DowncastNode<O, N> {}
#[node_macro::node_fn(DowncastNode<_O>)]
fn downcast<N, _O: StaticType>(input: Any<'input>, node: &'input N) -> _O
fn downcast<N: 'input, _O: StaticType>(input: Any<'input>, node: &'input N) -> _O
where
N: for<'any_input> Node<'any_input, Any<'any_input>, Output = Any<'any_input>> + 'input,
{

View file

@ -0,0 +1,215 @@
use std::marker::PhantomData;
use glam::{DAffine2, DVec2};
use graphene_core::raster::{Color, Image, ImageFrame};
use graphene_core::transform::TransformMut;
use graphene_core::vector::VectorData;
use graphene_core::Node;
use node_macro::node_fn;
// Spacing is a consistent 0.2 apart, even when tiled across pixels (from 0.9 to the neighboring 0.1), to avoid bias
const MULTISAMPLE_GRID: [(f64, f64); 25] = [
// Row 1
(0.1, 0.1),
(0.1, 0.3),
(0.1, 0.5),
(0.1, 0.7),
(0.1, 0.9),
// Row 2
(0.3, 0.1),
(0.3, 0.3),
(0.3, 0.5),
(0.3, 0.7),
(0.3, 0.9),
// Row 3
(0.5, 0.1),
(0.5, 0.3),
(0.5, 0.5),
(0.5, 0.7),
(0.5, 0.9),
// Row 4
(0.7, 0.1),
(0.7, 0.3),
(0.7, 0.5),
(0.7, 0.7),
(0.7, 0.9),
// Row 5
(0.9, 0.1),
(0.9, 0.3),
(0.9, 0.5),
(0.9, 0.7),
(0.9, 0.9),
];
#[derive(Clone, Debug, PartialEq)]
pub struct ReduceNode<Initial, Lambda> {
pub initial: Initial,
pub lambda: Lambda,
}
#[node_fn(ReduceNode)]
fn reduce<I: Iterator, Lambda, T>(iter: I, initial: T, lambda: &'any_input Lambda) -> T
where
Lambda: for<'a> Node<'a, (T, I::Item), Output = T>,
{
iter.fold(initial, |a, x| lambda.eval((a, x)))
}
#[derive(Clone, Debug, PartialEq)]
pub struct IntoIterNode<T> {
_t: PhantomData<T>,
}
#[node_fn(IntoIterNode<_T>)]
fn into_iter<'i: 'input, _T: Send + Sync>(vec: &'i Vec<_T>) -> Box<dyn Iterator<Item = &'i _T> + Send + Sync + 'i> {
Box::new(vec.iter())
}
#[derive(Clone, Debug, PartialEq)]
pub struct VectorPointsNode;
#[node_fn(VectorPointsNode)]
fn vector_points(vector: VectorData) -> Vec<DVec2> {
vector.subpaths.iter().flat_map(|subpath| subpath.manipulator_groups().iter().map(|group| group.anchor)).collect()
}
#[derive(Clone, Debug, PartialEq)]
pub struct BrushTextureNode<ColorNode, Hardness, Flow> {
pub color: ColorNode,
pub hardness: Hardness,
pub flow: Flow,
}
#[derive(Clone, Debug, PartialEq)]
pub struct EraseNode<Flow> {
flow: Flow,
}
#[node_fn(EraseNode)]
fn erase(input: (Color, Color), flow: f64) -> Color {
let (input, brush) = input;
let alpha = input.a() * (1.0 - flow as f32 * brush.a());
Color::from_unassociated_alpha(input.r(), input.g(), input.b(), alpha)
}
#[node_fn(BrushTextureNode)]
fn brush_texture(diameter: f64, color: Color, hardness: f64, flow: f64) -> ImageFrame {
// Diameter
let radius = diameter / 2.;
// TODO: Remove the 4px padding after figuring out why the brush stamp gets randomly offset by 1px up/down/left/right when clicking with the Brush tool
let dimension = diameter.ceil() as u32 + 4;
let center = DVec2::splat(radius + (dimension as f64 - diameter) / 2.);
// Hardness
let hardness = hardness / 100.;
let feather_exponent = 1. / (1. - hardness);
// Flow
let flow = flow / 100.;
// Color
let color = color.apply_opacity(flow as f32);
// Initial transparent image
let mut image = Image::new(dimension, dimension, Color::TRANSPARENT);
for y in 0..dimension {
for x in 0..dimension {
let summation = MULTISAMPLE_GRID.iter().fold(0., |acc, (offset_x, offset_y)| {
let position = DVec2::new(x as f64 + offset_x, y as f64 + offset_y);
let distance = (position - center).length();
if distance < radius {
acc + (1. - (distance / radius).powf(feather_exponent)).clamp(0., 1.)
} else {
acc
}
});
let pixel_fill = summation / MULTISAMPLE_GRID.len() as f64;
let pixel = image.get_mut(x, y).unwrap();
*pixel = color.apply_opacity(pixel_fill as f32);
}
}
ImageFrame {
image,
transform: DAffine2::from_scale_angle_translation(DVec2::splat(dimension as f64), 0., -DVec2::splat(radius)),
}
}
#[derive(Clone, Debug, PartialEq)]
pub struct TranslateNode<Translatable> {
translatable: Translatable,
}
#[node_fn(TranslateNode)]
fn translate_node<Data: TransformMut>(offset: DVec2, mut translatable: Data) -> Data {
translatable.translate(offset);
translatable
}
#[cfg(test)]
mod test {
use super::*;
use crate::raster::*;
use glam::DAffine2;
use graphene_core::ops::{AddNode, CloneNode};
use graphene_core::raster::*;
use graphene_core::structural::Then;
use graphene_core::transform::{Transform, TransformMut};
use graphene_core::value::{ClonedNode, ValueNode};
#[test]
fn test_translate_node() {
let image = Image::new(10, 10, Color::TRANSPARENT);
let mut image = ImageFrame { image, transform: DAffine2::IDENTITY };
image.translate(DVec2::new(1.0, 2.0));
let translate_node = TranslateNode::new(ClonedNode::new(image));
let image = translate_node.eval(DVec2::new(1.0, 2.0));
assert_eq!(image.transform(), DAffine2::from_translation(DVec2::new(2.0, 4.0)));
}
#[test]
fn test_reduce() {
let reduce_node = ReduceNode::new(ClonedNode::new(0u32), ValueNode::new(AddNode));
let sum = reduce_node.eval(vec![1, 2, 3, 4, 5].into_iter());
assert_eq!(sum, 15);
}
#[test]
fn test_brush_texture() {
let brush_texture_node = BrushTextureNode::new(ClonedNode::new(Color::BLACK), ClonedNode::new(100.), ClonedNode::new(100.));
let size = 20.;
let image = brush_texture_node.eval(size);
assert_eq!(image.image.width, size.ceil() as u32 + 4);
assert_eq!(image.image.height, size.ceil() as u32 + 4);
assert_eq!(image.transform, DAffine2::from_scale_angle_translation(DVec2::splat(size.ceil() + 4.), 0., -DVec2::splat(size / 2.)));
// center pixel should be BLACK
assert_eq!(image.image.get(11, 11), Some(&Color::BLACK));
}
#[test]
fn test_brush() {
let brush_texture_node = BrushTextureNode::new(ClonedNode::new(Color::BLACK), ClonedNode::new(1.0), ClonedNode::new(1.0));
let image = brush_texture_node.eval(20.);
let trace = vec![DVec2::new(0.0, 0.0), DVec2::new(10.0, 0.0)];
let trace = ClonedNode::new(trace.into_iter());
let translate_node = TranslateNode::new(ClonedNode::new(image));
let frames = MapNode::new(ValueNode::new(translate_node));
let frames = trace.then(frames).eval(()).collect::<Vec<_>>();
assert_eq!(frames.len(), 2);
assert_eq!(frames[0].image.width, 24);
let background_bounds = ReduceNode::new(ClonedNode::new(None), ValueNode::new(MergeBoundingBoxNode::new()));
let background_bounds = background_bounds.eval(frames.clone().into_iter());
let background_bounds = ClonedNode::new(background_bounds.unwrap().to_transform());
let background_image = background_bounds.then(EmptyImageNode::new(ClonedNode::new(Color::TRANSPARENT)));
let blend_node = graphene_core::raster::BlendNode::new(ClonedNode::new(BlendMode::Normal), ClonedNode::new(1.0));
let final_image = ReduceNode::new(background_image, ValueNode::new(BlendImageTupleNode::new(ValueNode::new(blend_node))));
let final_image = final_image.eval(frames.into_iter());
assert_eq!(final_image.image.height, 24);
assert_eq!(final_image.image.width, 34);
drop(final_image);
}
}

View file

@ -19,3 +19,5 @@ pub mod executor;
pub mod quantization;
pub use graphene_core::*;
pub mod brush;

View file

@ -1,9 +1,12 @@
use dyn_any::{DynAny, StaticType};
use dyn_any::{DynAny, StaticType, StaticTypeSized};
use glam::{BVec2, DAffine2, DVec2};
use graphene_core::raster::{Color, Image, ImageFrame};
use graphene_core::transform::Transform;
use graphene_core::value::{ClonedNode, ValueNode};
use graphene_core::Node;
use std::marker::PhantomData;
use std::path::Path;
#[derive(Debug, DynAny)]
@ -139,6 +142,10 @@ pub struct MapImageFrameNode<MapFn> {
map_fn: MapFn,
}
impl<MapFn: dyn_any::StaticTypeSized> StaticType for MapImageFrameNode<MapFn> {
type Static = MapImageFrameNode<MapFn::Static>;
}
#[node_macro::node_fn(MapImageFrameNode)]
fn map_image<MapFn>(mut image_frame: ImageFrame, map_fn: &'any_input MapFn) -> ImageFrame
where
@ -151,12 +158,37 @@ where
image_frame
}
#[derive(Debug, Clone)]
struct AxisAlignedBbox {
#[derive(Debug, Clone, DynAny)]
pub struct AxisAlignedBbox {
start: DVec2,
end: DVec2,
}
impl AxisAlignedBbox {
pub fn size(&self) -> DVec2 {
self.end - self.start
}
pub fn to_transform(&self) -> DAffine2 {
DAffine2::from_translation(self.start) * DAffine2::from_scale(self.size())
}
pub fn contains(&self, point: DVec2) -> bool {
point.x >= self.start.x && point.x <= self.end.x && point.y >= self.start.y && point.y <= self.end.y
}
pub fn intersects(&self, other: &AxisAlignedBbox) -> bool {
other.start.x <= self.end.x && other.end.x >= self.start.x && other.start.y <= self.end.y && other.end.y >= self.start.y
}
pub fn union(&self, other: &AxisAlignedBbox) -> AxisAlignedBbox {
AxisAlignedBbox {
start: DVec2::new(self.start.x.min(other.start.x), self.start.y.min(other.start.y)),
end: DVec2::new(self.end.x.max(other.end.x), self.end.y.max(other.end.y)),
}
}
}
#[derive(Debug, Clone)]
struct Bbox {
top_left: DVec2,
@ -228,18 +260,42 @@ fn mask_image(mut image: ImageFrame, mask: ImageFrame) -> ImageFrame {
image
}
#[derive(Debug, Clone, Copy)]
pub struct BlendImageTupleNode<MapFn> {
map_fn: MapFn,
}
impl<MapFn: StaticTypeSized> StaticType for BlendImageTupleNode<MapFn> {
type Static = BlendImageTupleNode<MapFn::Static>;
}
#[node_macro::node_fn(BlendImageTupleNode)]
fn blend_image_tuple<MapFn>(images: (ImageFrame, ImageFrame), map_fn: &'any_input MapFn) -> ImageFrame
where
MapFn: for<'any_input> Node<'any_input, (Color, Color), Output = Color> + 'input + Clone,
{
let (mut background, foreground) = images;
let node = BlendImageNode::new(ClonedNode::new(background), ValueNode::new(map_fn.clone()));
node.eval(foreground)
}
#[derive(Debug, Clone, Copy)]
pub struct BlendImageNode<Background, MapFn> {
background: Background,
map_fn: MapFn,
}
impl<Background: StaticTypeSized, MapFn: StaticTypeSized> StaticType for BlendImageNode<Background, MapFn> {
type Static = BlendImageNode<Background::Static, MapFn::Static>;
}
// TODO: Implement proper blending
#[node_macro::node_fn(BlendImageNode)]
fn blend_image<MapFn>(foreground: ImageFrame, mut background: ImageFrame, map_fn: &'any_input MapFn) -> ImageFrame
fn blend_image<MapFn, Frame: AsRef<ImageFrame>>(foreground: Frame, mut background: ImageFrame, map_fn: &'any_input MapFn) -> ImageFrame
where
MapFn: for<'any_input> Node<'any_input, (Color, Color), Output = Color> + 'input,
{
let foreground = foreground.as_ref();
let foreground_size = DVec2::new(foreground.image.width as f64, foreground.image.height as f64);
let background_size = DVec2::new(background.image.width as f64, background.image.height as f64);
@ -271,6 +327,38 @@ where
background
}
#[derive(Clone, Debug, PartialEq)]
pub struct MergeBoundingBoxNode<Data> {
_data: PhantomData<Data>,
}
#[node_macro::node_fn(MergeBoundingBoxNode<_Data>)]
fn merge_bounding_box_node<_Data: Transform>(input: (Option<AxisAlignedBbox>, _Data)) -> Option<AxisAlignedBbox> {
let (initial_aabb, data) = input;
let snd_aabb = compute_transformed_bounding_box(data.transform()).axis_aligned_bbox();
if let Some(fst_aabb) = initial_aabb {
Some(fst_aabb.union(&snd_aabb))
} else {
Some(snd_aabb)
}
}
#[derive(Clone, Debug, PartialEq)]
pub struct EmptyImageNode<FillColor> {
pub color: FillColor,
}
#[node_macro::node_fn(EmptyImageNode)]
fn empty_image(transform: DAffine2, color: Color) -> ImageFrame {
let width = transform.transform_vector2(DVec2::new(1., 0.)).length() as u32;
let height = transform.transform_vector2(DVec2::new(0., 1.)).length() as u32;
let image = Image::new(width, height, color);
ImageFrame { image, transform }
}
#[derive(Debug, Clone, Copy)]
pub struct ImaginateNode<E> {
cached: E,