alice-sdf 6.0.0

ALICE-SDF: 3D/Spatial Data Specialist — Store laws, not polygons. GLSL/WGSL/HLSL transpile, SVO, GPU marching cubes, texture-fit, procedural CSG. Optional bridges to alice-physics / alice-codec / libasp / alice-cache behind features.
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
//! Simulation Bridge: ALICE-SDF ↔ Simulation Modifiers
//!
//! Connects ALICE-SDF's `CompiledSdf` with ALICE-Physics simulation
//! modifiers (thermal, pressure, erosion, fracture, phase change).
//!
//! # Architecture
//!
//! ```text
//! CompiledSdf ──→ CompiledSdfField ──→ ModifiedSdf
//!                  (physics_bridge)      ├── ThermalModifier
//!                                        ├── PressureModifier
//!                                        ├── ErosionModifier
//!                                        ├── FractureModifier
//!                                        └── PhaseChangeModifier
//! ```
//!
//! The `SimulatedSdf` wrapper provides a convenient API for
//! combining an ALICE-SDF shape with physics simulation modifiers.
//!
//! # Example
//!
//! ```ignore
//! use alice_sdf::prelude::*;
//! use alice_sdf::sim_bridge::SimulatedSdf;
//! use alice_physics::thermal::{ThermalModifier, ThermalConfig};
//!
//! // Create SDF shape
//! let node = SdfNode::sphere(1.0);
//! let compiled = CompiledSdf::compile(&node);
//!
//! // Wrap with simulation (bounds auto-estimated or user-provided)
//! let mut sim = SimulatedSdf::new(compiled);
//! sim.add_thermal(ThermalConfig::default(), 16);
//!
//! // Query modified distance
//! let d = sim.distance(1.0, 0.0, 0.0);
//! ```
//!
//! Author: Moroya Sakamoto

use crate::compiled::CompiledSdf;
use crate::physics_bridge::CompiledSdfField;
use alice_physics::sdf_collider::SdfField;
use alice_physics::sim_modifier::{ModifiedSdf, PhysicsModifier};
use std::sync::Arc;

/// Default simulation bounds (half-extent)
const DEFAULT_HALF_EXTENT: f32 = 5.0;

// ============================================================================
// SimulatedSdf
// ============================================================================

/// SDF with physics simulation modifiers attached.
///
/// Wraps a `CompiledSdf` and chains physics modifiers that modify
/// the distance field based on simulation state (heat, pressure, etc.).
pub struct SimulatedSdf {
    /// The underlying modified SDF (CompiledSdf + modifiers)
    inner: ModifiedSdf,
    /// Reference to the compiled SDF (for re-use)
    compiled: Arc<CompiledSdf>,
    /// Simulation bounds (min corner)
    bounds_min: (f32, f32, f32),
    /// Simulation bounds (max corner)
    bounds_max: (f32, f32, f32),
}

impl SimulatedSdf {
    /// Create a new simulated SDF from a compiled SDF.
    ///
    /// Uses default bounds (-5..5). For accurate bounds, use `with_bounds`.
    pub fn new(compiled: CompiledSdf) -> Self {
        Self::from_arc(Arc::new(compiled))
    }

    /// Create from an existing `Arc<CompiledSdf>`.
    pub fn from_arc(arc: Arc<CompiledSdf>) -> Self {
        let field = CompiledSdfField::from_arc(Arc::clone(&arc));
        let modified = ModifiedSdf::new(Box::new(field));
        let e = DEFAULT_HALF_EXTENT;
        Self {
            inner: modified,
            compiled: arc,
            bounds_min: (-e, -e, -e),
            bounds_max: (e, e, e),
        }
    }

    /// Set custom simulation bounds.
    pub const fn with_bounds(mut self, min: (f32, f32, f32), max: (f32, f32, f32)) -> Self {
        self.bounds_min = min;
        self.bounds_max = max;
        self
    }

    /// Get the underlying compiled SDF.
    pub fn compiled(&self) -> &CompiledSdf {
        &self.compiled
    }

    /// Get simulation bounds.
    pub const fn bounds(&self) -> ((f32, f32, f32), (f32, f32, f32)) {
        (self.bounds_min, self.bounds_max)
    }

    /// Add a generic physics modifier.
    pub fn add_modifier(&mut self, modifier: Box<dyn PhysicsModifier>) {
        self.inner.add_modifier(modifier);
    }

    /// Number of active modifiers.
    pub fn modifier_count(&self) -> usize {
        self.inner.modifier_count()
    }

    /// Remove all modifiers.
    pub fn clear_modifiers(&mut self) {
        self.inner.clear_modifiers();
    }

    /// Update all modifier simulations by dt seconds.
    pub fn update(&mut self, dt: f32) {
        self.inner.update(dt);
    }

    /// Evaluate the simulated distance at a point.
    pub fn distance(&self, x: f32, y: f32, z: f32) -> f32 {
        self.inner.distance(x, y, z)
    }

    /// Evaluate the simulated normal at a point.
    pub fn normal(&self, x: f32, y: f32, z: f32) -> (f32, f32, f32) {
        self.inner.normal(x, y, z)
    }

    /// Get mutable access to a modifier by index.
    pub fn modifier_mut(&mut self, index: usize) -> Option<&mut dyn PhysicsModifier> {
        self.inner.modifier_mut(index)
    }

    // ========================================================================
    // Convenience methods for common modifiers
    // ========================================================================

    /// Add a thermal modifier with given config and field resolution.
    ///
    /// Returns the modifier index for later access.
    pub fn add_thermal(
        &mut self,
        config: alice_physics::thermal::ThermalConfig,
        resolution: usize,
    ) -> usize {
        let modifier = alice_physics::thermal::ThermalModifier::new(
            config,
            resolution,
            self.bounds_min,
            self.bounds_max,
        );
        let idx = self.modifier_count();
        self.add_modifier(Box::new(modifier));
        idx
    }

    /// Add a pressure modifier.
    pub fn add_pressure(
        &mut self,
        config: alice_physics::pressure::PressureConfig,
        resolution: usize,
    ) -> usize {
        let modifier = alice_physics::pressure::PressureModifier::new(
            config,
            resolution,
            self.bounds_min,
            self.bounds_max,
        );
        let idx = self.modifier_count();
        self.add_modifier(Box::new(modifier));
        idx
    }

    /// Add an erosion modifier.
    pub fn add_erosion(
        &mut self,
        config: alice_physics::erosion::ErosionConfig,
        resolution: usize,
    ) -> usize {
        let modifier = alice_physics::erosion::ErosionModifier::new(
            config,
            resolution,
            self.bounds_min,
            self.bounds_max,
        );
        let idx = self.modifier_count();
        self.add_modifier(Box::new(modifier));
        idx
    }

    /// Add a fracture modifier.
    pub fn add_fracture(
        &mut self,
        config: alice_physics::fracture::FractureConfig,
        resolution: usize,
    ) -> usize {
        let modifier = alice_physics::fracture::FractureModifier::new(
            config,
            resolution,
            self.bounds_min,
            self.bounds_max,
        );
        let idx = self.modifier_count();
        self.add_modifier(Box::new(modifier));
        idx
    }

    /// Add a phase change modifier.
    pub fn add_phase_change(
        &mut self,
        config: alice_physics::phase_change::PhaseChangeConfig,
        resolution: usize,
    ) -> usize {
        let modifier = alice_physics::phase_change::PhaseChangeModifier::new(
            config,
            resolution,
            self.bounds_min,
            self.bounds_max,
        );
        let idx = self.modifier_count();
        self.add_modifier(Box::new(modifier));
        idx
    }
}

/// Convenience function: create a simulated SDF from an SDF node.
pub fn simulate_sdf(node: &crate::types::SdfNode) -> SimulatedSdf {
    let compiled = CompiledSdf::compile(node);
    SimulatedSdf::new(compiled)
}

// ============================================================================
// GPU Mesh + Physics Pipeline
// ============================================================================

/// GPU メッシュ生成と物理パイプラインの統合結果。
#[cfg(feature = "gpu")]
pub struct GpuPhysicsBundle {
    /// GPU MC で生成されたビジュアルメッシュ。
    pub mesh: crate::mesh::Mesh,
    /// 物理シミュレーション用 SDF。
    pub sim: SimulatedSdf,
}

/// GPU Marching Cubes でメッシュを生成し、同時に物理パイプラインを構築する。
///
/// ワンコールで:
/// 1. `SdfNode` → `CompiledSdf` にコンパイル
/// 2. GPU MC でビジュアルメッシュを生成
/// 3. `SimulatedSdf` を構築(物理モディファイア追加可能な状態)
///
/// # Arguments
/// * `node` - メッシュ化する SDF ノード
/// * `bounds_min` - ワールド空間の最小バウンド
/// * `bounds_max` - ワールド空間の最大バウンド
/// * `mc_config` - GPU MC 設定
///
/// # Errors
/// GPU MC が失敗した場合にエラーを返す。
#[cfg(feature = "gpu")]
pub fn gpu_mesh_with_physics(
    node: &crate::types::SdfNode,
    bounds_min: glam::Vec3,
    bounds_max: glam::Vec3,
    mc_config: &crate::mesh::gpu_marching_cubes::GpuMarchingCubesConfig,
) -> Result<GpuPhysicsBundle, crate::compiled::GpuError> {
    use crate::mesh::gpu_marching_cubes::gpu_marching_cubes;

    // GPU MC でメッシュ生成
    let mesh = gpu_marching_cubes(node, bounds_min, bounds_max, mc_config)?;

    // 物理用 CompiledSdf + SimulatedSdf を構築
    let compiled = CompiledSdf::compile(node);
    let sim = SimulatedSdf::new(compiled).with_bounds(
        (bounds_min.x, bounds_min.y, bounds_min.z),
        (bounds_max.x, bounds_max.y, bounds_max.z),
    );

    Ok(GpuPhysicsBundle { mesh, sim })
}

/// 既存メッシュから物理パイプラインを構築する。
///
/// GPU MC 等で生成済みのメッシュに対して、
/// 元の SDF ノードから `SimulatedSdf` を後付けで構築する。
pub fn attach_physics(
    node: &crate::types::SdfNode,
    bounds_min: (f32, f32, f32),
    bounds_max: (f32, f32, f32),
) -> SimulatedSdf {
    let compiled = CompiledSdf::compile(node);
    SimulatedSdf::new(compiled).with_bounds(bounds_min, bounds_max)
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::types::SdfNode;
    use alice_physics::pressure::PressureConfig;
    use alice_physics::thermal::ThermalConfig;

    #[test]
    fn test_simulated_sdf_no_modifiers() {
        let node = SdfNode::sphere(1.0);
        let sim = simulate_sdf(&node);

        let d = sim.distance(2.0, 0.0, 0.0);
        assert!((d - 1.0).abs() < 0.01, "No modifiers: d={}", d);
    }

    #[test]
    fn test_simulated_sdf_with_thermal() {
        let node = SdfNode::sphere(1.0);
        let mut sim = simulate_sdf(&node);

        let _idx = sim.add_thermal(ThermalConfig::default(), 8);
        assert_eq!(sim.modifier_count(), 1);

        // Without heat, distance unchanged
        let d = sim.distance(2.0, 0.0, 0.0);
        assert!((d - 1.0).abs() < 0.1, "No heat: d={}", d);
    }

    #[test]
    fn test_simulated_sdf_with_pressure() {
        let node = SdfNode::sphere(1.0);
        let mut sim = simulate_sdf(&node);

        sim.add_pressure(PressureConfig::default(), 8);
        assert_eq!(sim.modifier_count(), 1);

        // Step simulation
        sim.update(0.016);

        let d = sim.distance(2.0, 0.0, 0.0);
        assert!((d - 1.0).abs() < 0.1, "No pressure: d={}", d);
    }

    #[test]
    fn test_simulated_sdf_multiple_modifiers() {
        let node = SdfNode::sphere(1.0);
        let mut sim = simulate_sdf(&node);

        sim.add_thermal(ThermalConfig::default(), 8);
        sim.add_pressure(PressureConfig::default(), 8);
        assert_eq!(sim.modifier_count(), 2);

        // Step several times
        for _ in 0..10 {
            sim.update(0.016);
        }

        // Distance should still be reasonable
        let d = sim.distance(2.0, 0.0, 0.0);
        assert!(d > 0.0, "Should still be outside: d={}", d);
    }

    #[test]
    fn test_attach_physics() {
        let node = SdfNode::sphere(1.0);
        let sim = attach_physics(&node, (-3.0, -3.0, -3.0), (3.0, 3.0, 3.0));

        let (bmin, bmax) = sim.bounds();
        assert_eq!(bmin, (-3.0, -3.0, -3.0));
        assert_eq!(bmax, (3.0, 3.0, 3.0));

        // 距離評価が正常に動作すること
        let d = sim.distance(2.0, 0.0, 0.0);
        assert!((d - 1.0).abs() < 0.01, "attach_physics d={}", d);
    }

    #[test]
    fn test_attach_physics_with_modifiers() {
        let node = SdfNode::sphere(1.0);
        let mut sim = attach_physics(&node, (-2.0, -2.0, -2.0), (2.0, 2.0, 2.0));

        sim.add_thermal(ThermalConfig::default(), 8);
        assert_eq!(sim.modifier_count(), 1);

        sim.update(0.016);
        let d = sim.distance(2.0, 0.0, 0.0);
        assert!(d > 0.0, "Should still be outside: d={}", d);
    }

    #[test]
    fn test_simulated_sdf_custom_bounds() {
        let node = SdfNode::sphere(1.0);
        let sim = SimulatedSdf::new(CompiledSdf::compile(&node))
            .with_bounds((-2.0, -2.0, -2.0), (2.0, 2.0, 2.0));

        let (bmin, bmax) = sim.bounds();
        assert_eq!(bmin, (-2.0, -2.0, -2.0));
        assert_eq!(bmax, (2.0, 2.0, 2.0));
    }
}