alice-lol 0.3.0

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
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
//! Roblox 向けメッシュエクスポートモジュール
//!
//! LOL → `SdfNode` → `Mesh` → OBJ/FBX (Roblox `MeshPart` 用) のパイプライン。
//! アクセサリー (帽子・武器・装飾品) の静的メッシュ出力に特化。
//!
//! # 使い方
//!
//! ```ignore
//! use alice_lol::roblox_export::{RobloxConfig, lol_to_obj_roblox};
//!
//! let stats = lol_to_obj_roblox(
//!     "smooth_union(0.3, sphere(1.0), translate(0.0, 1.5, 0.0, scale(0.6, sphere(1.0))))",
//!     "hat.obj",
//!     &RobloxConfig::accessory(),
//! ).unwrap();
//! println!("{stats}");
//! ```

use crate::print_export::{ExportError, MarchingCubesConfig, Mesh, MeshRepair};
use crate::SdfNode;
use alice_sdf::io::{export_fbx, export_obj, FbxConfig, FbxUpAxis, ObjConfig};
use alice_sdf::mesh::sdf_to_mesh;
use glam::Vec3;
use std::path::Path;

// ── Roblox 制約定数 ──

/// UGC アクセサリー三角形上限
const ROBLOX_ACCESSORY_MAX_TRIS: usize = 4_000;

/// 汎用 `MeshPart` 三角形上限
const ROBLOX_MESHPART_MAX_TRIS: usize = 10_000;

/// デフォルトアクセサリーサイズ上限 (studs)
const ROBLOX_DEFAULT_MAX_SIZE: Vec3 = Vec3::new(10.0, 10.0, 10.0);

/// デジェネレート面判定 epsilon
const DEGENERATE_EPSILON: f32 = 1e-8;

// ── 設定 ──

/// Roblox エクスポート設定
#[derive(Debug, Clone)]
pub struct RobloxConfig {
    /// メッシュ解像度 (Marching Cubes グリッド数)
    pub resolution: usize,

    /// SDF バウンディングボックス最小点
    pub bounds_min: Vec3,

    /// SDF バウンディングボックス最大点
    pub bounds_max: Vec3,

    /// SDF 単位 → stud 変換スケール (1.0 SDF unit = `scale_studs` studs)
    pub scale_studs: f32,

    /// 三角形数上限
    pub max_triangles: usize,

    /// バウンディングボックス上限 (studs)
    pub max_size_studs: Vec3,
}

impl Default for RobloxConfig {
    fn default() -> Self {
        Self {
            resolution: 128,
            bounds_min: Vec3::splat(-2.0),
            bounds_max: Vec3::splat(2.0),
            scale_studs: 2.0,
            max_triangles: ROBLOX_ACCESSORY_MAX_TRIS,
            max_size_studs: ROBLOX_DEFAULT_MAX_SIZE,
        }
    }
}

impl RobloxConfig {
    /// UGC アクセサリー向けプリセット (4,000 三角形上限)
    #[must_use]
    pub const fn accessory() -> Self {
        Self {
            resolution: 128,
            bounds_min: Vec3::splat(-2.0),
            bounds_max: Vec3::splat(2.0),
            scale_studs: 2.0,
            max_triangles: ROBLOX_ACCESSORY_MAX_TRIS,
            max_size_studs: ROBLOX_DEFAULT_MAX_SIZE,
        }
    }

    /// 汎用 `MeshPart` 向けプリセット (10,000 三角形上限)
    #[must_use]
    pub const fn meshpart() -> Self {
        Self {
            resolution: 192,
            bounds_min: Vec3::splat(-2.0),
            bounds_max: Vec3::splat(2.0),
            scale_studs: 2.0,
            max_triangles: ROBLOX_MESHPART_MAX_TRIS,
            max_size_studs: ROBLOX_DEFAULT_MAX_SIZE,
        }
    }

    /// 高速プレビュー向けプリセット
    #[must_use]
    pub const fn preview() -> Self {
        Self {
            resolution: 64,
            bounds_min: Vec3::splat(-2.0),
            bounds_max: Vec3::splat(2.0),
            scale_studs: 2.0,
            max_triangles: ROBLOX_ACCESSORY_MAX_TRIS,
            max_size_studs: ROBLOX_DEFAULT_MAX_SIZE,
        }
    }

    /// カスタムバウンディングボックス設定
    #[must_use]
    pub const fn with_bounds(mut self, min: Vec3, max: Vec3) -> Self {
        self.bounds_min = min;
        self.bounds_max = max;
        self
    }

    /// スケール設定
    #[must_use]
    pub const fn with_scale_studs(mut self, scale: f32) -> Self {
        self.scale_studs = scale;
        self
    }

    /// 三角形上限設定
    #[must_use]
    pub const fn with_max_triangles(mut self, max: usize) -> Self {
        self.max_triangles = max;
        self
    }

    /// サイズ上限設定 (studs)
    #[must_use]
    pub const fn with_max_size_studs(mut self, max: Vec3) -> Self {
        self.max_size_studs = max;
        self
    }
}

// ── バリデーション ──

/// Roblox 向けメッシュバリデーション結果
#[derive(Debug, Clone)]
pub struct RobloxValidation {
    /// 三角形数
    pub triangle_count: usize,
    /// 頂点数
    pub vertex_count: usize,
    /// stud 単位のバウンディングボックスサイズ
    pub bounds_studs: Vec3,
    /// 三角形上限以内か
    pub is_within_triangle_limit: bool,
    /// サイズ上限以内か
    pub is_within_size_limit: bool,
    /// デジェネレート面があるか
    pub has_degenerate_faces: bool,
}

impl RobloxValidation {
    /// 全チェック合格か
    #[must_use]
    pub const fn is_valid(&self) -> bool {
        self.is_within_triangle_limit && self.is_within_size_limit && !self.has_degenerate_faces
    }
}

impl std::fmt::Display for RobloxValidation {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let status = if self.is_valid() { "PASS" } else { "FAIL" };
        write!(
            f,
            "[{}] {} tris, {} verts, bounds: {:.1} x {:.1} x {:.1} studs",
            status,
            self.triangle_count,
            self.vertex_count,
            self.bounds_studs.x,
            self.bounds_studs.y,
            self.bounds_studs.z,
        )?;
        if !self.is_within_triangle_limit {
            write!(f, " [OVER TRI LIMIT]")?;
        }
        if !self.is_within_size_limit {
            write!(f, " [OVER SIZE LIMIT]")?;
        }
        if self.has_degenerate_faces {
            write!(f, " [DEGENERATE FACES]")?;
        }
        Ok(())
    }
}

/// メッシュを Roblox 制約に対して検証
#[must_use]
pub fn validate_for_roblox(mesh: &Mesh, config: &RobloxConfig) -> RobloxValidation {
    let tri_count = mesh.indices.len() / 3;
    let vert_count = mesh.vertices.len();

    // バウンディングボックス計算 (stud 単位)
    let bounds = compute_bounds_studs(mesh);

    // デジェネレート面チェック
    let has_degenerate = check_degenerate_faces(mesh);

    RobloxValidation {
        triangle_count: tri_count,
        vertex_count: vert_count,
        bounds_studs: bounds,
        is_within_triangle_limit: tri_count <= config.max_triangles,
        is_within_size_limit: bounds.x <= config.max_size_studs.x
            && bounds.y <= config.max_size_studs.y
            && bounds.z <= config.max_size_studs.z,
        has_degenerate_faces: has_degenerate,
    }
}

/// バウンディングボックスサイズ (頂点座標から、スケーリング適用後)
fn compute_bounds_studs(mesh: &Mesh) -> Vec3 {
    if mesh.vertices.is_empty() {
        return Vec3::ZERO;
    }
    let mut min = Vec3::splat(f32::MAX);
    let mut max = Vec3::splat(f32::MIN);
    for v in &mesh.vertices {
        min = min.min(v.position);
        max = max.max(v.position);
    }
    max - min
}

/// デジェネレート面 (面積ゼロ) の有無チェック
fn check_degenerate_faces(mesh: &Mesh) -> bool {
    let indices = &mesh.indices;
    let verts = &mesh.vertices;
    let tri_count = indices.len() / 3;
    for i in 0..tri_count {
        let a = verts[indices[i * 3] as usize].position;
        let b = verts[indices[i * 3 + 1] as usize].position;
        let c = verts[indices[i * 3 + 2] as usize].position;
        let cross = (b - a).cross(c - a);
        if cross.length_squared() < DEGENERATE_EPSILON {
            return true;
        }
    }
    false
}

// ── メッシュ生成 ──

/// `SdfNode` → Roblox 用メッシュ生成 (スケーリング + 修復済み)
#[must_use]
pub fn node_to_mesh_roblox(node: &SdfNode, config: &RobloxConfig) -> Mesh {
    // 目標三角形数から解像度を推定
    // Marching Cubes は resolution³ の ~2% 程度が三角形数になる経験則
    let resolution = estimate_resolution(config.resolution, config.max_triangles);

    let mc_config = MarchingCubesConfig {
        resolution,
        compute_normals: true,
        ..MarchingCubesConfig::default()
    };
    let mesh = sdf_to_mesh(node, config.bounds_min, config.bounds_max, &mc_config);

    // 修復
    let mut mesh = MeshRepair::repair_all(&mesh, 5e-3);

    // SDF 座標 → stud スケーリング
    if (config.scale_studs - 1.0).abs() > f32::EPSILON {
        for v in &mut mesh.vertices {
            v.position *= config.scale_studs;
        }
    }

    mesh
}

/// 三角形上限から適切な解像度を推定
///
/// Marching Cubes の三角形数は resolution に対して O(resolution²) に比例。
/// ユーザー指定 resolution を上限として、三角形数が超過しそうなら下げる。
fn estimate_resolution(user_resolution: usize, max_triangles: usize) -> usize {
    // 経験則: resolution=128 → ~20,000 tri (球体の場合)
    // resolution=64 → ~5,000 tri
    // resolution=48 → ~2,500 tri
    // 最終的に解像度はユーザー指定を尊重し、上限でクランプ
    let estimated_tris_at_128: u16 = 20_000;
    #[allow(clippy::cast_precision_loss)]
    let ratio = max_triangles as f32 / f32::from(estimated_tris_at_128);
    #[allow(clippy::cast_possible_truncation, clippy::cast_sign_loss)]
    let auto_res = (128.0_f32 * ratio.sqrt()).round() as usize;
    // ユーザー指定と自動推定の小さい方を採用
    user_resolution.min(auto_res).max(16)
}

// ── エクスポート関数 ──

/// Roblox エクスポート統計
#[derive(Debug, Clone)]
pub struct RobloxExportStats {
    /// 頂点数
    pub vertex_count: usize,
    /// 三角形数
    pub triangle_count: usize,
    /// stud 単位のバウンディングボックスサイズ
    pub bounds_studs: Vec3,
    /// 出力ファイルパス
    pub path: String,
    /// バリデーション結果
    pub validation: RobloxValidation,
}

impl std::fmt::Display for RobloxExportStats {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(
            f,
            "{}: {} vertices, {} triangles (bounds: {:.1} x {:.1} x {:.1} studs) {}",
            self.path,
            self.vertex_count,
            self.triangle_count,
            self.bounds_studs.x,
            self.bounds_studs.y,
            self.bounds_studs.z,
            self.validation,
        )
    }
}

/// `SdfNode` → OBJ ファイル出力 (Roblox 制約適用)
///
/// # Errors
///
/// メッシュが空の場合 `EmptyMesh`、ファイル書き込み失敗時 `Io`、
/// Roblox 制約違反時に警告をログ出力 (エラーにはしない)。
pub fn node_to_obj_roblox(
    node: &SdfNode,
    path: impl AsRef<Path>,
    config: &RobloxConfig,
) -> Result<RobloxExportStats, ExportError> {
    let mesh = node_to_mesh_roblox(node, config);
    if mesh.indices.is_empty() {
        return Err(ExportError::EmptyMesh);
    }

    let validation = validate_for_roblox(&mesh, config);

    let obj_config = ObjConfig {
        export_normals: true,
        export_uvs: true,
        export_materials: false,
        flip_uv_v: false,
    };
    export_obj(&mesh, &path, &obj_config, None)?;

    Ok(build_stats(&mesh, &path, validation))
}

/// `SdfNode` → FBX ファイル出力 (Roblox 制約適用)
///
/// # Errors
///
/// メッシュが空の場合 `EmptyMesh`、ファイル書き込み失敗時 `Io`。
pub fn node_to_fbx_roblox(
    node: &SdfNode,
    path: impl AsRef<Path>,
    config: &RobloxConfig,
) -> Result<RobloxExportStats, ExportError> {
    let mesh = node_to_mesh_roblox(node, config);
    if mesh.indices.is_empty() {
        return Err(ExportError::EmptyMesh);
    }

    let validation = validate_for_roblox(&mesh, config);

    let fbx_config = FbxConfig {
        export_normals: true,
        export_uvs: true,
        export_materials: false,
        up_axis: FbxUpAxis::Y,
        ..FbxConfig::default()
    };
    export_fbx(&mesh, &path, &fbx_config, None)?;

    Ok(build_stats(&mesh, &path, validation))
}

/// LOL テキスト → OBJ ファイル出力 (Roblox 制約適用, LLM 連携用)
///
/// # Errors
///
/// LOLパースエラー、メッシュ空、ファイル書き込み失敗時にエラーを返す。
pub fn lol_to_obj_roblox(
    lol_text: &str,
    path: impl AsRef<Path>,
    config: &RobloxConfig,
) -> Result<RobloxExportStats, ExportError> {
    let node = crate::runtime_parser::parse_lol(lol_text)?;
    node_to_obj_roblox(&node, path, config)
}

/// LOL テキスト → FBX ファイル出力 (Roblox 制約適用, LLM 連携用)
///
/// # Errors
///
/// LOLパースエラー、メッシュ空、ファイル書き込み失敗時にエラーを返す。
pub fn lol_to_fbx_roblox(
    lol_text: &str,
    path: impl AsRef<Path>,
    config: &RobloxConfig,
) -> Result<RobloxExportStats, ExportError> {
    let node = crate::runtime_parser::parse_lol(lol_text)?;
    node_to_fbx_roblox(&node, path, config)
}

fn build_stats(
    mesh: &Mesh,
    path: &impl AsRef<Path>,
    validation: RobloxValidation,
) -> RobloxExportStats {
    RobloxExportStats {
        vertex_count: mesh.vertices.len(),
        triangle_count: mesh.indices.len() / 3,
        bounds_studs: validation.bounds_studs,
        path: path.as_ref().display().to_string(),
        validation,
    }
}