1use super::SdfNode;
17use std::sync::{Arc, OnceLock};
18
19fn leaf_sentinel() -> Arc<SdfNode> {
22 static LEAF: OnceLock<Arc<SdfNode>> = OnceLock::new();
23 Arc::clone(LEAF.get_or_init(|| Arc::new(SdfNode::Sphere { radius: 0.0 })))
24}
25
26impl SdfNode {
27 pub(crate) fn take_children_into(&mut self, out: &mut Vec<Arc<Self>>) {
33 match self {
34 Self::Union { a, b, .. }
36 | Self::Intersection { a, b, .. }
37 | Self::Subtraction { a, b, .. }
38 | Self::SmoothUnion { a, b, .. }
39 | Self::SmoothIntersection { a, b, .. }
40 | Self::SmoothSubtraction { a, b, .. }
41 | Self::ChamferUnion { a, b, .. }
42 | Self::ChamferIntersection { a, b, .. }
43 | Self::ChamferSubtraction { a, b, .. }
44 | Self::StairsUnion { a, b, .. }
45 | Self::StairsIntersection { a, b, .. }
46 | Self::StairsSubtraction { a, b, .. }
47 | Self::ColumnsUnion { a, b, .. }
48 | Self::ColumnsIntersection { a, b, .. }
49 | Self::ColumnsSubtraction { a, b, .. }
50 | Self::ExpSmoothUnion { a, b, .. }
51 | Self::ExpSmoothIntersection { a, b, .. }
52 | Self::ExpSmoothSubtraction { a, b, .. }
53 | Self::XOR { a, b, .. }
54 | Self::Pipe { a, b, .. }
55 | Self::Engrave { a, b, .. }
56 | Self::Groove { a, b, .. }
57 | Self::Tongue { a, b, .. }
58 | Self::Morph { a, b, .. } => {
59 out.push(std::mem::replace(a, leaf_sentinel()));
60 out.push(std::mem::replace(b, leaf_sentinel()));
61 }
62
63 Self::Translate { child, .. }
65 | Self::Rotate { child, .. }
66 | Self::Scale { child, .. }
67 | Self::ScaleNonUniform { child, .. }
68 | Self::Shear { child, .. }
69 | Self::ProjectiveTransform { child, .. }
70 | Self::SdfSkinning { child, .. }
71 | Self::Round { child, .. }
72 | Self::Onion { child, .. }
73 | Self::Elongate { child, .. }
74 | Self::Twist { child, .. }
75 | Self::Bend { child, .. }
76 | Self::Taper { child, .. }
77 | Self::Mirror { child, .. }
78 | Self::OctantMirror { child, .. }
79 | Self::IcosahedralSymmetry { child, .. }
80 | Self::RepeatInfinite { child, .. }
81 | Self::RepeatFinite { child, .. }
82 | Self::PolarRepeat { child, .. }
83 | Self::Displacement { child, .. }
84 | Self::SineDisplacement { child, .. }
85 | Self::HeightmapDisplacement { child, .. }
86 | Self::Noise { child, .. }
87 | Self::SurfaceRoughness { child, .. }
88 | Self::Revolution { child, .. }
89 | Self::Extrude { child, .. }
90 | Self::SweepBezier { child, .. }
91 | Self::LatticeDeform { child, .. }
92 | Self::IFS { child, .. }
93 | Self::WithMaterial { child, .. }
94 | Self::Animated { child, .. } => {
95 out.push(std::mem::replace(child, leaf_sentinel()));
96 }
97
98 _ => {}
100 }
101 }
102}
103
104impl Drop for SdfNode {
105 fn drop(&mut self) {
106 let mut stack: Vec<Arc<Self>> = Vec::new();
107 self.take_children_into(&mut stack);
108 if stack.is_empty() {
109 return;
110 }
111 while let Some(child) = stack.pop() {
112 if let Ok(mut node) = Arc::try_unwrap(child) {
116 node.take_children_into(&mut stack);
117 }
118 }
119 }
120}