Add Operation::children

This commit is contained in:
Hanno Braun 2024-12-09 20:26:10 +01:00
parent 20025690de
commit a9bcd5df1b
3 changed files with 35 additions and 0 deletions

View File

@ -5,4 +5,5 @@ use super::{Triangle, Vertex};
pub trait Operation: fmt::Display { pub trait Operation: fmt::Display {
fn vertices(&self, vertices: &mut Vec<Vertex>); fn vertices(&self, vertices: &mut Vec<Vertex>);
fn triangles(&self, triangles: &mut Vec<Triangle>); fn triangles(&self, triangles: &mut Vec<Triangle>);
fn children(&self) -> Vec<Box<dyn Operation>>;
} }

View File

@ -73,6 +73,13 @@ impl Operation for OpsLog {
op.triangles(triangles); op.triangles(triangles);
} }
} }
fn children(&self) -> Vec<Box<dyn Operation>> {
self.operations
.iter()
.map(|op| Box::new(op.clone()) as _)
.collect()
}
} }
#[derive(Clone)] #[derive(Clone)]
@ -95,6 +102,10 @@ impl Operation for OperationInSequence {
} }
self.operation.triangles(triangles); self.operation.triangles(triangles);
} }
fn children(&self) -> Vec<Box<dyn Operation>> {
self.operation.children()
}
} }
impl fmt::Display for OperationInSequence { impl fmt::Display for OperationInSequence {
@ -154,6 +165,7 @@ pub struct ClonedOperation {
pub description: String, pub description: String,
pub vertices: Vec<Vertex>, pub vertices: Vec<Vertex>,
pub triangles: Vec<Triangle>, pub triangles: Vec<Triangle>,
pub children: Vec<ClonedOperation>,
} }
impl ClonedOperation { impl ClonedOperation {
@ -164,10 +176,17 @@ impl ClonedOperation {
let mut triangles = Vec::new(); let mut triangles = Vec::new();
op.triangles(&mut triangles); op.triangles(&mut triangles);
let children = op
.children()
.into_iter()
.map(|op| Self::from_op(op.as_ref()))
.collect();
Self { Self {
description: op.to_string(), description: op.to_string(),
vertices, vertices,
triangles, triangles,
children,
} }
} }
} }
@ -186,4 +205,11 @@ impl Operation for ClonedOperation {
fn triangles(&self, triangles: &mut Vec<Triangle>) { fn triangles(&self, triangles: &mut Vec<Triangle>) {
triangles.extend(&self.triangles); triangles.extend(&self.triangles);
} }
fn children(&self) -> Vec<Box<dyn Operation>> {
self.children
.iter()
.map(|op| Box::new(op.clone()) as _)
.collect()
}
} }

View File

@ -33,6 +33,10 @@ impl Operation for Vertex {
} }
fn triangles(&self, _: &mut Vec<Triangle>) {} fn triangles(&self, _: &mut Vec<Triangle>) {}
fn children(&self) -> Vec<Box<dyn Operation>> {
Vec::new()
}
} }
#[derive(Clone, Copy, Debug, Eq, Ord, PartialEq, PartialOrd)] #[derive(Clone, Copy, Debug, Eq, Ord, PartialEq, PartialOrd)]
@ -64,4 +68,8 @@ impl Operation for Triangle {
fn triangles(&self, triangles: &mut Vec<Triangle>) { fn triangles(&self, triangles: &mut Vec<Triangle>) {
triangles.push(*self) triangles.push(*self)
} }
fn children(&self) -> Vec<Box<dyn Operation>> {
Vec::new()
}
} }