oxiphysics-geometry 0.1.0

Geometric shape types for the OxiPhysics engine
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
//! Auto-generated module
//!
//! 🤖 Generated with [SplitRS](https://github.com/cool-japan/splitrs)

use super::functions::ImplicitSurface;
#[allow(unused_imports)]
use super::functions::*;
#[allow(unused_imports)]
use super::functions_2::*;

/// An infinite plane: `dot(p, normal) - d = 0`.
pub struct SdfPlane {
    /// Unit outward normal.
    pub normal: [f64; 3],
    /// Plane offset: points with `dot(p, normal) > d` are outside.
    pub d: f64,
}
impl SdfPlane {
    /// Create a new plane.
    pub fn new(normal: [f64; 3], d: f64) -> Self {
        Self {
            normal: normalize(normal),
            d,
        }
    }
}
/// Smooth CSG difference.
///
/// SDF = smooth_max(sdf_a, -sdf_b, k).
pub struct CsgSmoothDifference {
    /// The base shape.
    pub a: Box<dyn ImplicitSurface>,
    /// The shape to subtract.
    pub b: Box<dyn ImplicitSurface>,
    /// Smoothing factor.
    pub k: f64,
}
impl CsgSmoothDifference {
    /// Create a new smooth difference.
    pub fn new(a: Box<dyn ImplicitSurface>, b: Box<dyn ImplicitSurface>, k: f64) -> Self {
        Self { a, b, k }
    }
}
/// A cone (infinite half-cone) opening upward from the origin.
///
/// Defined by its half-angle in radians. SDF is exact on the surface.
pub struct SdfCone {
    /// Apex position.
    pub apex: [f64; 3],
    /// Half-angle in radians.
    pub half_angle: f64,
    /// Height of the finite cone.
    pub height: f64,
}
impl SdfCone {
    /// Create a new cone.
    pub fn new(apex: [f64; 3], half_angle: f64, height: f64) -> Self {
        Self {
            apex,
            half_angle,
            height,
        }
    }
}
/// A sphere defined by centre and radius.
pub struct SdfSphere {
    /// World-space centre.
    pub center: [f64; 3],
    /// Sphere radius (must be positive).
    pub radius: f64,
}
impl SdfSphere {
    /// Create a new sphere.
    pub fn new(center: [f64; 3], radius: f64) -> Self {
        Self { center, radius }
    }
}
/// CSG intersection: the region inside both `a` and `b`.
///
/// SDF = max(sdf_a, sdf_b).
pub struct CsgIntersection {
    /// First operand.
    pub a: Box<dyn ImplicitSurface>,
    /// Second operand.
    pub b: Box<dyn ImplicitSurface>,
}
impl CsgIntersection {
    /// Create a new intersection.
    pub fn new(a: Box<dyn ImplicitSurface>, b: Box<dyn ImplicitSurface>) -> Self {
        Self { a, b }
    }
}
/// A node in a CSG tree.
///
/// Each node is either a leaf (wrapping an `ImplicitSurface`) or an interior
/// node combining two subtrees with a `CsgOp`.
pub enum CsgTree {
    /// A leaf primitive.
    Leaf(Box<dyn ImplicitSurface>),
    /// An interior combination node.
    Node {
        /// The boolean operation to apply.
        op: CsgOp,
        /// Left operand.
        left: Box<CsgTree>,
        /// Right operand.
        right: Box<CsgTree>,
    },
}
impl CsgTree {
    /// Construct a leaf node from any `ImplicitSurface`.
    pub fn leaf(s: impl ImplicitSurface + 'static) -> Self {
        CsgTree::Leaf(Box::new(s))
    }
    /// Construct a union node.
    pub fn union(left: CsgTree, right: CsgTree) -> Self {
        CsgTree::Node {
            op: CsgOp::Union,
            left: Box::new(left),
            right: Box::new(right),
        }
    }
    /// Construct an intersection node.
    pub fn intersection(left: CsgTree, right: CsgTree) -> Self {
        CsgTree::Node {
            op: CsgOp::Intersection,
            left: Box::new(left),
            right: Box::new(right),
        }
    }
    /// Construct a difference node (left minus right).
    pub fn difference(left: CsgTree, right: CsgTree) -> Self {
        CsgTree::Node {
            op: CsgOp::Difference,
            left: Box::new(left),
            right: Box::new(right),
        }
    }
}
/// CSG union: the region inside either `a` or `b`.
///
/// SDF = min(sdf_a, sdf_b).
pub struct CsgUnion {
    /// First operand.
    pub a: Box<dyn ImplicitSurface>,
    /// Second operand.
    pub b: Box<dyn ImplicitSurface>,
}
impl CsgUnion {
    /// Create a new union.
    pub fn new(a: Box<dyn ImplicitSurface>, b: Box<dyn ImplicitSurface>) -> Self {
        Self { a, b }
    }
}
/// A torus centred at the origin lying in the XZ plane.
pub struct SdfTorus {
    /// Major radius (distance from centre to tube centre).
    pub major_radius: f64,
    /// Minor radius (tube radius).
    pub minor_radius: f64,
}
impl SdfTorus {
    /// Create a new torus.
    pub fn new(major_radius: f64, minor_radius: f64) -> Self {
        Self {
            major_radius,
            minor_radius,
        }
    }
}
/// CSG boolean operation type.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum CsgOp {
    /// Union: region inside either child.
    Union,
    /// Intersection: region inside both children.
    Intersection,
    /// Difference: region inside the left child but outside the right child.
    Difference,
}
/// A capsule aligned with the Y axis.
pub struct SdfCapsule {
    /// Center of the capsule.
    pub center: [f64; 3],
    /// Capsule radius.
    pub radius: f64,
    /// Half-height of the cylindrical section.
    pub half_height: f64,
}
impl SdfCapsule {
    /// Create a new capsule.
    pub fn new(center: [f64; 3], radius: f64, half_height: f64) -> Self {
        Self {
            center,
            radius,
            half_height,
        }
    }
}
/// Offset a `CsgTree` surface inward (negative `offset`) or outward (positive).
///
/// Returns a wrapper `SDF f(p) = original_sdf(p) - offset` so that the
/// zero-level set is shifted by `offset` in the normal direction.
///
/// The returned object implements `ImplicitSurface`.
pub struct CsgOffsetSurface {
    /// The original SDF tree (as an evaluated function table over a grid is
    /// impractical, so we store the sampler function as a closure via callback).
    pub(super) inner_sdf: f64,
    /// Offset value (positive = expand outward, negative = shrink inward).
    pub offset: f64,
    /// Bounding box for the original shape (used for gradient estimation).
    pub bbox_min: [f64; 3],
    /// Maximum bounding extent for gradient computation.
    pub bbox_max: [f64; 3],
}
impl CsgOffsetSurface {
    /// Create an offset wrapper (placeholder; use `csg_offset_sdf` for actual evaluation).
    pub fn new(offset: f64, bbox_min: [f64; 3], bbox_max: [f64; 3]) -> Self {
        Self {
            inner_sdf: 0.0,
            offset,
            bbox_min,
            bbox_max,
        }
    }
}
/// A cell in a marching-cubes grid.
#[derive(Debug, Clone)]
pub struct MarchingCell {
    /// Min corner of the cell.
    pub min: [f64; 3],
    /// Cell side length.
    pub step: f64,
    /// SDF values at the 8 corners (index = ix*4 + iy*2 + iz).
    pub corner_values: [f64; 8],
}
impl MarchingCell {
    /// Return `true` if any corner is inside (negative SDF) and any is outside.
    pub fn has_surface(&self) -> bool {
        let has_inside = self.corner_values.iter().any(|&v| v < 0.0);
        let has_outside = self.corner_values.iter().any(|&v| v >= 0.0);
        has_inside && has_outside
    }
    /// Linear interpolation of position along edge between corner `i0` and `i1`.
    fn edge_vertex(&self, i0: usize, i1: usize) -> [f64; 3] {
        let offsets: [[f64; 3]; 8] = [
            [0.0, 0.0, 0.0],
            [self.step, 0.0, 0.0],
            [self.step, self.step, 0.0],
            [0.0, self.step, 0.0],
            [0.0, 0.0, self.step],
            [self.step, 0.0, self.step],
            [self.step, self.step, self.step],
            [0.0, self.step, self.step],
        ];
        let v0 = self.corner_values[i0];
        let v1 = self.corner_values[i1];
        let t = if (v1 - v0).abs() > 1e-14 {
            v0 / (v0 - v1)
        } else {
            0.5
        };
        let p0 = add(self.min, offsets[i0]);
        let p1 = add(self.min, offsets[i1]);
        lerp3(p0, p1, t)
    }
    /// Extract surface vertices from this cell (one per crossing edge).
    pub fn extract_vertices(&self) -> Vec<[f64; 3]> {
        if !self.has_surface() {
            return vec![];
        }
        let edges: [(usize, usize); 12] = [
            (0, 1),
            (1, 2),
            (2, 3),
            (3, 0),
            (4, 5),
            (5, 6),
            (6, 7),
            (7, 4),
            (0, 4),
            (1, 5),
            (2, 6),
            (3, 7),
        ];
        edges
            .iter()
            .filter_map(|&(i0, i1)| {
                let v0 = self.corner_values[i0];
                let v1 = self.corner_values[i1];
                if v0.signum() != v1.signum() {
                    Some(self.edge_vertex(i0, i1))
                } else {
                    None
                }
            })
            .collect()
    }
}
/// An axis-aligned box defined by centre and half-extents.
pub struct SdfBox {
    /// World-space centre.
    pub center: [f64; 3],
    /// Half-extents along each axis.
    pub half_extents: [f64; 3],
}
impl SdfBox {
    /// Create a new box.
    pub fn new(center: [f64; 3], half_extents: [f64; 3]) -> Self {
        Self {
            center,
            half_extents,
        }
    }
}
/// An infinite cylinder along the Y axis.
pub struct SdfCylinder {
    /// World-space centre (on the axis).
    pub center: [f64; 3],
    /// Cylinder radius.
    pub radius: f64,
}
impl SdfCylinder {
    /// Create a new cylinder.
    pub fn new(center: [f64; 3], radius: f64) -> Self {
        Self { center, radius }
    }
}
/// CSG difference: region inside `a` but outside `b`.
///
/// SDF = max(sdf_a, -sdf_b).
pub struct CsgDifference {
    /// The base shape.
    pub a: Box<dyn ImplicitSurface>,
    /// The shape to subtract.
    pub b: Box<dyn ImplicitSurface>,
}
impl CsgDifference {
    /// Create a new difference.
    pub fn new(a: Box<dyn ImplicitSurface>, b: Box<dyn ImplicitSurface>) -> Self {
        Self { a, b }
    }
}
/// Smooth CSG union using the polynomial smooth-min kernel.
///
/// SDF = smooth_min(sdf_a, sdf_b, k).
pub struct CsgSmoothUnion {
    /// First operand.
    pub a: Box<dyn ImplicitSurface>,
    /// Second operand.
    pub b: Box<dyn ImplicitSurface>,
    /// Smoothing factor (larger -> smoother blend).
    pub k: f64,
}
impl CsgSmoothUnion {
    /// Create a new smooth union.
    pub fn new(a: Box<dyn ImplicitSurface>, b: Box<dyn ImplicitSurface>, k: f64) -> Self {
        Self { a, b, k }
    }
}
/// Smooth CSG intersection.
///
/// SDF = smooth_max(sdf_a, sdf_b, k).
pub struct CsgSmoothIntersection {
    /// First operand.
    pub a: Box<dyn ImplicitSurface>,
    /// Second operand.
    pub b: Box<dyn ImplicitSurface>,
    /// Smoothing factor.
    pub k: f64,
}
impl CsgSmoothIntersection {
    /// Create a new smooth intersection.
    pub fn new(a: Box<dyn ImplicitSurface>, b: Box<dyn ImplicitSurface>, k: f64) -> Self {
        Self { a, b, k }
    }
}
/// Classification of a point relative to a plane.
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum PlaneSide {
    /// Point is on the front (positive) side.
    Front,
    /// Point is on the back (negative) side.
    Back,
    /// Point is on the plane (within tolerance).
    OnPlane,
}
/// A finite capped cylinder aligned with the Y axis.
pub struct SdfCappedCylinder {
    /// World-space center.
    pub center: [f64; 3],
    /// Cylinder radius.
    pub radius: f64,
    /// Half-height (distance from centre to cap).
    pub half_height: f64,
}
impl SdfCappedCylinder {
    /// Create a new capped cylinder.
    pub fn new(center: [f64; 3], radius: f64, half_height: f64) -> Self {
        Self {
            center,
            radius,
            half_height,
        }
    }
}
/// Rounded box: axis-aligned box with rounded edges.
///
/// SDF = SdfBox.sdf(p) - rounding_radius
pub struct SdfRoundedBox {
    /// World-space centre.
    pub center: [f64; 3],
    /// Half-extents (before rounding).
    pub half_extents: [f64; 3],
    /// Corner rounding radius.
    pub radius: f64,
}
impl SdfRoundedBox {
    /// Create a new rounded box.
    pub fn new(center: [f64; 3], half_extents: [f64; 3], radius: f64) -> Self {
        Self {
            center,
            half_extents,
            radius,
        }
    }
}