Skip to main content

alice_sdf/types/
operations.rs

1//! Boolean and blending operations for SdfNode
2//!
3//! Author: Moroya Sakamoto
4
5use std::sync::Arc;
6
7use super::SdfNode;
8
9impl SdfNode {
10    // === Operation methods ===
11
12    /// Union with another shape
13    #[must_use]
14    #[inline]
15    pub fn union(self, other: Self) -> Self {
16        Self::Union {
17            a: Arc::new(self),
18            b: Arc::new(other),
19        }
20    }
21
22    /// Intersection with another shape
23    #[must_use]
24    #[inline]
25    pub fn intersection(self, other: Self) -> Self {
26        Self::Intersection {
27            a: Arc::new(self),
28            b: Arc::new(other),
29        }
30    }
31
32    /// Subtract another shape from this one
33    #[must_use]
34    #[inline]
35    pub fn subtract(self, other: Self) -> Self {
36        Self::Subtraction {
37            a: Arc::new(self),
38            b: Arc::new(other),
39        }
40    }
41
42    /// Smooth union with another shape
43    #[must_use]
44    #[inline]
45    pub fn smooth_union(self, other: Self, k: f32) -> Self {
46        Self::SmoothUnion {
47            a: Arc::new(self),
48            b: Arc::new(other),
49            k,
50        }
51    }
52
53    /// Smooth intersection with another shape
54    #[must_use]
55    #[inline]
56    pub fn smooth_intersection(self, other: Self, k: f32) -> Self {
57        Self::SmoothIntersection {
58            a: Arc::new(self),
59            b: Arc::new(other),
60            k,
61        }
62    }
63
64    /// Smooth subtraction of another shape
65    #[must_use]
66    #[inline]
67    pub fn smooth_subtract(self, other: Self, k: f32) -> Self {
68        Self::SmoothSubtraction {
69            a: Arc::new(self),
70            b: Arc::new(other),
71            k,
72        }
73    }
74
75    /// Chamfer union with another shape
76    #[must_use]
77    #[inline]
78    pub fn chamfer_union(self, other: Self, r: f32) -> Self {
79        Self::ChamferUnion {
80            a: Arc::new(self),
81            b: Arc::new(other),
82            r,
83        }
84    }
85
86    /// Chamfer intersection with another shape
87    #[must_use]
88    #[inline]
89    pub fn chamfer_intersection(self, other: Self, r: f32) -> Self {
90        Self::ChamferIntersection {
91            a: Arc::new(self),
92            b: Arc::new(other),
93            r,
94        }
95    }
96
97    /// Chamfer subtraction of another shape
98    #[must_use]
99    #[inline]
100    pub fn chamfer_subtract(self, other: Self, r: f32) -> Self {
101        Self::ChamferSubtraction {
102            a: Arc::new(self),
103            b: Arc::new(other),
104            r,
105        }
106    }
107
108    /// Stairs union with another shape
109    #[must_use]
110    #[inline]
111    pub fn stairs_union(self, other: Self, r: f32, n: f32) -> Self {
112        Self::StairsUnion {
113            a: Arc::new(self),
114            b: Arc::new(other),
115            r,
116            n,
117        }
118    }
119
120    /// Stairs intersection with another shape
121    #[must_use]
122    #[inline]
123    pub fn stairs_intersection(self, other: Self, r: f32, n: f32) -> Self {
124        Self::StairsIntersection {
125            a: Arc::new(self),
126            b: Arc::new(other),
127            r,
128            n,
129        }
130    }
131
132    /// Stairs subtraction of another shape
133    #[must_use]
134    #[inline]
135    pub fn stairs_subtract(self, other: Self, r: f32, n: f32) -> Self {
136        Self::StairsSubtraction {
137            a: Arc::new(self),
138            b: Arc::new(other),
139            r,
140            n,
141        }
142    }
143
144    /// XOR (symmetric difference) with another shape
145    #[must_use]
146    #[inline]
147    pub fn xor(self, other: Self) -> Self {
148        Self::XOR {
149            a: Arc::new(self),
150            b: Arc::new(other),
151        }
152    }
153
154    /// Morph with another shape
155    #[must_use]
156    #[inline]
157    pub fn morph(self, other: Self, t: f32) -> Self {
158        Self::Morph {
159            a: Arc::new(self),
160            b: Arc::new(other),
161            t,
162        }
163    }
164
165    /// Columns union with another shape
166    #[must_use]
167    #[inline]
168    pub fn columns_union(self, other: Self, r: f32, n: f32) -> Self {
169        Self::ColumnsUnion {
170            a: Arc::new(self),
171            b: Arc::new(other),
172            r,
173            n,
174        }
175    }
176
177    /// Columns intersection with another shape
178    #[must_use]
179    #[inline]
180    pub fn columns_intersection(self, other: Self, r: f32, n: f32) -> Self {
181        Self::ColumnsIntersection {
182            a: Arc::new(self),
183            b: Arc::new(other),
184            r,
185            n,
186        }
187    }
188
189    /// Columns subtraction of another shape
190    #[must_use]
191    #[inline]
192    pub fn columns_subtract(self, other: Self, r: f32, n: f32) -> Self {
193        Self::ColumnsSubtraction {
194            a: Arc::new(self),
195            b: Arc::new(other),
196            r,
197            n,
198        }
199    }
200
201    /// Pipe operation with another shape
202    #[must_use]
203    #[inline]
204    pub fn pipe(self, other: Self, r: f32) -> Self {
205        Self::Pipe {
206            a: Arc::new(self),
207            b: Arc::new(other),
208            r,
209        }
210    }
211
212    /// Engrave another shape into this one
213    #[must_use]
214    #[inline]
215    pub fn engrave(self, other: Self, r: f32) -> Self {
216        Self::Engrave {
217            a: Arc::new(self),
218            b: Arc::new(other),
219            r,
220        }
221    }
222
223    /// Cut a groove of another shape into this one
224    #[must_use]
225    #[inline]
226    pub fn groove(self, other: Self, ra: f32, rb: f32) -> Self {
227        Self::Groove {
228            a: Arc::new(self),
229            b: Arc::new(other),
230            ra,
231            rb,
232        }
233    }
234
235    /// Add a tongue protrusion of another shape
236    #[must_use]
237    #[inline]
238    pub fn tongue(self, other: Self, ra: f32, rb: f32) -> Self {
239        Self::Tongue {
240            a: Arc::new(self),
241            b: Arc::new(other),
242            ra,
243            rb,
244        }
245    }
246
247    /// Exponential smooth union with another shape
248    #[must_use]
249    #[inline]
250    pub fn exp_smooth_union(self, other: Self, k: f32) -> Self {
251        Self::ExpSmoothUnion {
252            a: Arc::new(self),
253            b: Arc::new(other),
254            k,
255        }
256    }
257
258    /// Exponential smooth intersection with another shape
259    #[must_use]
260    #[inline]
261    pub fn exp_smooth_intersection(self, other: Self, k: f32) -> Self {
262        Self::ExpSmoothIntersection {
263            a: Arc::new(self),
264            b: Arc::new(other),
265            k,
266        }
267    }
268
269    /// Exponential smooth subtraction of another shape
270    #[must_use]
271    #[inline]
272    pub fn exp_smooth_subtract(self, other: Self, k: f32) -> Self {
273        Self::ExpSmoothSubtraction {
274            a: Arc::new(self),
275            b: Arc::new(other),
276            k,
277        }
278    }
279}