alice-lol 0.4.2

ALICE-LOL: Law-Oriented Language — proc_macro + runtime DSL compiling to SdfNode (GLSL/WGSL/HLSL transpile), print export (STL/3MF/OBJ/FBX), law checker, laser pattern generator, and optional LLM Guided Generation bridge.
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
//! 空間枝刈りコンパイラ
//!
//! AABB を N×N×N グリッドに分割し、セルごとに `eval_interval` で
//! 不要なサブツリーを除去した軽量 GLSL を生成する。

use std::sync::Arc;

use alice_sdf::interval::{eval_interval, Interval, Vec3Interval};
use alice_sdf::SdfNode;
use glam::Vec3;

/// グリッドセルの分類
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum CellKind {
    /// `lo > 0` — 完全に外側(描画不要)
    Outside,
    /// `hi < 0` — 完全に内側
    Inside,
    /// 表面が通過する可能性あり
    Crossing,
}

/// セルごとの解析結果
#[derive(Debug, Clone)]
pub struct CellInfo {
    /// グリッド座標
    pub ix: usize,
    pub iy: usize,
    pub iz: usize,
    /// 空間的な範囲
    pub bounds: Vec3Interval,
    /// セル分類
    pub kind: CellKind,
    /// 枝刈り済み `SdfNode`(Crossing セルのみ `Some`)
    pub pruned_node: Option<SdfNode>,
}

/// 空間枝刈りの設定
#[derive(Debug, Clone)]
pub struct PruneConfig {
    /// AABB 最小点
    pub aabb_min: Vec3,
    /// AABB 最大点
    pub aabb_max: Vec3,
    /// 各軸の分割数
    pub grid_resolution: usize,
}

impl Default for PruneConfig {
    fn default() -> Self {
        Self {
            aabb_min: Vec3::splat(-5.0),
            aabb_max: Vec3::splat(5.0),
            grid_resolution: 4,
        }
    }
}

/// 空間枝刈り結果
#[derive(Debug, Clone)]
pub struct PruneResult {
    /// セル情報の 3D 配列(row-major: iz * ny * nx + iy * nx + ix)
    pub cells: Vec<CellInfo>,
    /// 設定
    pub config: PruneConfig,
    /// 統計: 外側セル数
    pub outside_count: usize,
    /// 統計: 内側セル数
    pub inside_count: usize,
    /// 統計: 交差セル数
    pub crossing_count: usize,
}

/// AABB を N×N×N に分割してセルごとに解析
#[must_use]
pub fn analyze_grid(node: &SdfNode, config: &PruneConfig) -> PruneResult {
    let n = config.grid_resolution;
    let total = n * n * n;
    let mut cells = Vec::with_capacity(total);
    let mut outside_count = 0usize;
    let mut inside_count = 0usize;
    let mut crossing_count = 0usize;

    let extent = config.aabb_max - config.aabb_min;
    #[allow(clippy::cast_precision_loss)]
    let inv_n = 1.0 / n as f32;

    for iz in 0..n {
        for iy in 0..n {
            for ix in 0..n {
                #[allow(clippy::cast_precision_loss)]
                let lo =
                    config.aabb_min + extent * Vec3::new(ix as f32, iy as f32, iz as f32) * inv_n;
                #[allow(clippy::cast_precision_loss)]
                let hi = config.aabb_min
                    + extent * Vec3::new((ix + 1) as f32, (iy + 1) as f32, (iz + 1) as f32) * inv_n;

                let bounds = Vec3Interval {
                    x: Interval { lo: lo.x, hi: hi.x },
                    y: Interval { lo: lo.y, hi: hi.y },
                    z: Interval { lo: lo.z, hi: hi.z },
                };

                let iv = eval_interval(node, bounds);

                let kind = if iv.lo > 0.0 {
                    outside_count += 1;
                    CellKind::Outside
                } else if iv.hi < 0.0 {
                    inside_count += 1;
                    CellKind::Inside
                } else {
                    crossing_count += 1;
                    CellKind::Crossing
                };

                let pruned_node = if kind == CellKind::Crossing {
                    Some(prune_subtree(node, bounds))
                } else {
                    None
                };

                cells.push(CellInfo {
                    ix,
                    iy,
                    iz,
                    bounds,
                    kind,
                    pruned_node,
                });
            }
        }
    }

    PruneResult {
        cells,
        config: config.clone(),
        outside_count,
        inside_count,
        crossing_count,
    }
}

/// サブツリー枝刈り: Union / `SmoothUnion` で片方が確実に遠ければ除去
#[must_use]
#[allow(clippy::too_many_lines)]
fn prune_subtree(node: &SdfNode, bounds: Vec3Interval) -> SdfNode {
    match node {
        // ── Union: min(a, b) → a が確実に遠ければ b のみ ──
        SdfNode::Union { a, b } => {
            let iv_a = eval_interval(a, bounds);
            let iv_b = eval_interval(b, bounds);
            if iv_a.lo >= iv_b.hi {
                // a は b より確実に遠い → b だけで十分
                prune_subtree(b, bounds)
            } else if iv_b.lo >= iv_a.hi {
                prune_subtree(a, bounds)
            } else {
                SdfNode::Union {
                    a: Arc::new(prune_subtree(a, bounds)),
                    b: Arc::new(prune_subtree(b, bounds)),
                }
            }
        }

        // ── SmoothUnion: k の影響範囲を考慮 ──
        SdfNode::SmoothUnion { a, b, k } => {
            let iv_a = eval_interval(a, bounds);
            let iv_b = eval_interval(b, bounds);
            if iv_a.lo >= iv_b.hi + k {
                prune_subtree(b, bounds)
            } else if iv_b.lo >= iv_a.hi + k {
                prune_subtree(a, bounds)
            } else {
                SdfNode::SmoothUnion {
                    a: Arc::new(prune_subtree(a, bounds)),
                    b: Arc::new(prune_subtree(b, bounds)),
                    k: *k,
                }
            }
        }

        // ── Intersection / SmoothIntersection: 両方必要なのでそのまま再帰 ──
        SdfNode::Intersection { a, b } => SdfNode::Intersection {
            a: Arc::new(prune_subtree(a, bounds)),
            b: Arc::new(prune_subtree(b, bounds)),
        },
        SdfNode::SmoothIntersection { a, b, k } => SdfNode::SmoothIntersection {
            a: Arc::new(prune_subtree(a, bounds)),
            b: Arc::new(prune_subtree(b, bounds)),
            k: *k,
        },

        // ── Subtraction / SmoothSubtraction: 両方必要 ──
        SdfNode::Subtraction { a, b } => SdfNode::Subtraction {
            a: Arc::new(prune_subtree(a, bounds)),
            b: Arc::new(prune_subtree(b, bounds)),
        },
        SdfNode::SmoothSubtraction { a, b, k } => SdfNode::SmoothSubtraction {
            a: Arc::new(prune_subtree(a, bounds)),
            b: Arc::new(prune_subtree(b, bounds)),
            k: *k,
        },

        // ── Translate: bounds をシフトして子を枝刈り ──
        SdfNode::Translate { child, offset } => {
            let shifted = Vec3Interval {
                x: Interval {
                    lo: bounds.x.lo - offset.x,
                    hi: bounds.x.hi - offset.x,
                },
                y: Interval {
                    lo: bounds.y.lo - offset.y,
                    hi: bounds.y.hi - offset.y,
                },
                z: Interval {
                    lo: bounds.z.lo - offset.z,
                    hi: bounds.z.hi - offset.z,
                },
            };
            SdfNode::Translate {
                child: Arc::new(prune_subtree(child, shifted)),
                offset: *offset,
            }
        }

        // ── その他のトランスフォーム・モディファイア: 子を再帰枝刈り ──
        SdfNode::Rotate { child, rotation } => SdfNode::Rotate {
            child: Arc::new(prune_subtree(child, bounds)),
            rotation: *rotation,
        },
        SdfNode::Scale { child, factor } => SdfNode::Scale {
            child: Arc::new(prune_subtree(child, bounds)),
            factor: *factor,
        },
        SdfNode::Twist { child, strength } => SdfNode::Twist {
            child: Arc::new(prune_subtree(child, bounds)),
            strength: *strength,
        },
        SdfNode::Bend { child, curvature } => SdfNode::Bend {
            child: Arc::new(prune_subtree(child, bounds)),
            curvature: *curvature,
        },
        SdfNode::Round { child, radius } => SdfNode::Round {
            child: Arc::new(prune_subtree(child, bounds)),
            radius: *radius,
        },
        SdfNode::Onion { child, thickness } => SdfNode::Onion {
            child: Arc::new(prune_subtree(child, bounds)),
            thickness: *thickness,
        },
        SdfNode::Mirror { child, axes } => SdfNode::Mirror {
            child: Arc::new(prune_subtree(child, bounds)),
            axes: *axes,
        },
        SdfNode::RepeatInfinite { child, spacing } => SdfNode::RepeatInfinite {
            child: Arc::new(prune_subtree(child, bounds)),
            spacing: *spacing,
        },

        // ── Time ──
        SdfNode::Animated {
            child,
            speed,
            amplitude,
        } => SdfNode::Animated {
            child: Arc::new(prune_subtree(child, bounds)),
            speed: *speed,
            amplitude: *amplitude,
        },
        SdfNode::Morph { a, b, t } => SdfNode::Morph {
            a: Arc::new(prune_subtree(a, bounds)),
            b: Arc::new(prune_subtree(b, bounds)),
            t: *t,
        },

        // ── プリミティブ / 未対応バリアント: そのまま返す ──
        other => other.clone(),
    }
}

/// `SdfNode` ツリーのノード数を数える
#[must_use]
pub fn count_nodes(node: &SdfNode) -> usize {
    match node {
        SdfNode::Union { a, b }
        | SdfNode::Intersection { a, b }
        | SdfNode::Subtraction { a, b }
        | SdfNode::SmoothUnion { a, b, .. }
        | SdfNode::SmoothIntersection { a, b, .. }
        | SdfNode::SmoothSubtraction { a, b, .. }
        | SdfNode::Morph { a, b, .. } => 1 + count_nodes(a) + count_nodes(b),
        SdfNode::Translate { child, .. }
        | SdfNode::Rotate { child, .. }
        | SdfNode::Scale { child, .. }
        | SdfNode::Twist { child, .. }
        | SdfNode::Bend { child, .. }
        | SdfNode::Round { child, .. }
        | SdfNode::Onion { child, .. }
        | SdfNode::Mirror { child, .. }
        | SdfNode::RepeatInfinite { child, .. }
        | SdfNode::Animated { child, .. } => 1 + count_nodes(child),
        _ => 1,
    }
}

/// 枝刈り結果から GLSL コードを生成
///
/// - Crossing セルごとに `float sdf_cell_X_Y_Z(vec3 p)` を生成
/// - Outside/Inside セルはスキップ
/// - ディスパッチャ `float sdf_pruned(vec3 p)` で座標→セル→関数呼び出し
#[must_use]
#[cfg(feature = "glsl")]
pub fn to_pruned_glsl(node: &SdfNode, result: &PruneResult) -> String {
    use std::fmt::Write;

    use alice_sdf::compiled::glsl::{GlslShader, GlslTranspileMode};

    let mut out = String::with_capacity(4096);

    // フルシーン関数(Outside/Inside セル用フォールバック)
    let full = GlslShader::transpile(node, GlslTranspileMode::Hardcoded);
    out.push_str("// ── Full scene (fallback) ──\n");
    out.push_str(&full.source);
    out.push('\n');

    // セルごとの枝刈り関数
    for cell in &result.cells {
        if cell.kind != CellKind::Crossing {
            continue;
        }
        let Some(ref pruned) = cell.pruned_node else {
            continue;
        };
        let cell_glsl = GlslShader::transpile(pruned, GlslTranspileMode::Hardcoded);

        let _ = write!(
            out,
            "\n// ── Cell ({}, {}, {}) ──\n",
            cell.ix, cell.iy, cell.iz
        );

        // 関数名をリネーム: sdf( → sdf_cell_X_Y_Z(
        let cell_fn = format!("sdf_cell_{}_{}_{}", cell.ix, cell.iy, cell.iz);
        let renamed = cell_glsl
            .source
            .replace("float sdf(", &format!("float {cell_fn}("));
        out.push_str(&renamed);
        out.push('\n');
    }

    // ディスパッチャ
    let n = result.config.grid_resolution;
    let mn = result.config.aabb_min;
    let mx = result.config.aabb_max;

    out.push_str("\n// ── Dispatcher ──\n");
    out.push_str("float sdf_pruned(vec3 p) {\n");
    let _ = writeln!(
        out,
        "  vec3 rel = (p - vec3({:.6}, {:.6}, {:.6})) / vec3({:.6}, {:.6}, {:.6});",
        mn.x,
        mn.y,
        mn.z,
        mx.x - mn.x,
        mx.y - mn.y,
        mx.z - mn.z,
    );
    #[allow(clippy::cast_precision_loss)]
    let _ = writeln!(
        out,
        "  ivec3 idx = clamp(ivec3(rel * {:.1}), ivec3(0), ivec3({}));",
        n as f32,
        n - 1,
    );
    let _ = writeln!(
        out,
        "  int cell = idx.z * {} + idx.y * {} + idx.x;",
        n * n,
        n
    );
    out.push_str("  switch(cell) {\n");

    for cell in &result.cells {
        if cell.kind != CellKind::Crossing {
            continue;
        }
        let idx = cell.iz * n * n + cell.iy * n + cell.ix;
        let _ = writeln!(
            out,
            "    case {idx}: return sdf_cell_{}_{}_{vz}(p);",
            cell.ix,
            cell.iy,
            vz = cell.iz,
        );
    }

    out.push_str("    default: return sdf(p);\n");
    out.push_str("  }\n");
    out.push_str("}\n");

    out
}

/// 枝刈り統計のサマリ文字列
#[must_use]
pub fn summary(result: &PruneResult) -> String {
    let total = result.cells.len();
    format!(
        "Grid {}^3 = {} cells: {} outside, {} inside, {} crossing",
        result.config.grid_resolution,
        total,
        result.outside_count,
        result.inside_count,
        result.crossing_count,
    )
}