Skip to content

Commit 5a1503f

Browse files
committed
New node: Flatten Raster
1 parent ba177c4 commit 5a1503f

File tree

2 files changed

+55
-1
lines changed

2 files changed

+55
-1
lines changed

node-graph/libraries/graphic-types/src/graphic.rs

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,54 @@ pub trait IntoGraphicTable {
156156
flatten_table(&mut output, content);
157157
output
158158
}
159+
160+
/// Deeply flattens any raster content within a graphic table, discarding non-raster content, and returning a table of only raster elements.
161+
fn into_flattened_raster_table(self) -> Table<Raster<CPU>>
162+
where
163+
Self: std::marker::Sized,
164+
{
165+
let content = self.into_graphic_table();
166+
167+
fn flatten_table(output_raster_table: &mut Table<Raster<CPU>>, current_graphic_table: Table<Graphic>) {
168+
for current_graphic_row in current_graphic_table.iter() {
169+
let current_graphic = current_graphic_row.element.clone();
170+
let source_node_id = *current_graphic_row.source_node_id;
171+
172+
match current_graphic {
173+
// If we're allowed to recurse, flatten any tables we encounter
174+
Graphic::Graphic(mut current_graphic_table) => {
175+
// Apply the parent graphic's transform to all child elements
176+
for graphic in current_graphic_table.iter_mut() {
177+
*graphic.transform = *current_graphic_row.transform * *graphic.transform;
178+
}
179+
180+
flatten_table(output_raster_table, current_graphic_table);
181+
}
182+
// Push any leaf RasterCPU elements we encounter
183+
Graphic::RasterCPU(raster_table) => {
184+
for current_raster_row in raster_table.iter() {
185+
output_raster_table.push(TableRow {
186+
element: current_raster_row.element.clone(),
187+
transform: *current_graphic_row.transform * *current_raster_row.transform,
188+
alpha_blending: AlphaBlending {
189+
blend_mode: current_raster_row.alpha_blending.blend_mode,
190+
opacity: current_graphic_row.alpha_blending.opacity * current_raster_row.alpha_blending.opacity,
191+
fill: current_raster_row.alpha_blending.fill,
192+
clip: current_raster_row.alpha_blending.clip,
193+
},
194+
source_node_id,
195+
});
196+
}
197+
}
198+
_ => {}
199+
}
200+
}
201+
}
202+
203+
let mut output = Table::new();
204+
flatten_table(&mut output, content);
205+
output
206+
}
159207
}
160208

161209
impl IntoGraphicTable for Table<Graphic> {

node-graph/nodes/graphic/src/graphic.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,10 +192,16 @@ pub async fn flatten_graphic(_: impl Ctx, content: Table<Graphic>, fully_flatten
192192

193193
/// Converts a graphic table into a vector table by deeply flattening any vector content it contains, and discarding any non-vector content.
194194
#[node_macro::node(category("Vector"))]
195-
pub async fn flatten_vector<I: IntoGraphicTable + 'n + Send + Clone>(_: impl Ctx, #[implementations(Table<Graphic>, Table<Vector>)] content: I) -> Table<Vector> {
195+
pub async fn flatten_vector<T: IntoGraphicTable + 'n + Send + Clone>(_: impl Ctx, #[implementations(Table<Graphic>, Table<Vector>)] content: T) -> Table<Vector> {
196196
content.into_flattened_vector_table()
197197
}
198198

199+
/// Converts a graphic table into a vector table by deeply flattening any vector content it contains, and discarding any non-vector content.
200+
#[node_macro::node(category("Vector"))]
201+
pub async fn flatten_raster<T: IntoGraphicTable + 'n + Send + Clone>(_: impl Ctx, #[implementations(Table<Graphic>, Table<Raster<CPU>>)] content: T) -> Table<Raster<CPU>> {
202+
content.into_flattened_raster_table()
203+
}
204+
199205
/// Returns the value at the specified index in the collection.
200206
/// If no value exists at that index, the type's default value is returned.
201207
#[node_macro::node(category("General"))]

0 commit comments

Comments
 (0)