alice-sdf 1.9.0

ALICE-SDF: 3D/Spatial Data Specialist — Store laws, not polygons. GLSL/WGSL/HLSL transpile, SVO, GPU marching cubes, texture-fit, procedural CSG. Bridges to alice-codec/physics/asp/cache/font are available via GitHub path dep only in 1.7.7 (scheduled restoration in 1.8.0 once transitive deps publish).
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
//! OpCode definitions for compiled SDF
//!
//! Author: Moroya Sakamoto

/// Operation codes for the SDF virtual machine
///
/// Each opcode represents either a primitive (pushes to stack),
/// an operation (pops operands, pushes result), or a transform
/// (modifies the evaluation point).
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[repr(u8)]
pub enum OpCode {
    // === Primitives (push distance to stack) ===
    /// Sphere: `params[0]` = radius
    Sphere = 0,
    /// Box3d: `params[0..3]` = half_extents (x, y, z)
    Box3d = 1,
    /// Cylinder: `params[0]` = radius, `params[1]` = half_height
    Cylinder = 2,
    /// Torus: `params[0]` = major_radius, `params[1]` = minor_radius
    Torus = 3,
    /// Plane: `params[0..3]` = normal (x, y, z), `params[3]` = distance
    Plane = 4,
    /// Capsule: `params[0..3]` = point_a, `params[3..6]` = point_b (radius in params via separate field)
    Capsule = 5,
    /// Cone: `params[0]` = radius, `params[1]` = half_height
    Cone = 6,
    /// Ellipsoid: `params[0..3]` = radii (x, y, z)
    Ellipsoid = 7,
    /// RoundedCone: `params[0]` = r1, `params[1]` = r2, `params[2]` = half_height
    RoundedCone = 8,
    /// Pyramid: `params[0]` = half_height
    Pyramid = 9,
    /// Octahedron: `params[0]` = size
    Octahedron = 10,
    /// HexPrism: `params[0]` = hex_radius, `params[1]` = half_height
    HexPrism = 11,
    /// Link: `params[0]` = half_length, `params[1]` = r1, `params[2]` = r2
    Link = 12,

    // === Extended Primitives (64-101) ===
    /// RoundedBox: `params[0..3]` = half_extents, `params[3]` = round_radius
    RoundedBox = 64,
    /// CappedCone: `params[0]` = half_height, `params[1]` = r1, `params[2]` = r2
    CappedCone = 65,
    /// CappedTorus: `params[0]` = major_radius, `params[1]` = minor_radius, `params[2]` = cap_angle
    CappedTorus = 66,
    /// RoundedCylinder: `params[0]` = radius, `params[1]` = round_radius, `params[2]` = half_height
    RoundedCylinder = 67,
    /// TriangularPrism: `params[0]` = width, `params[1]` = half_depth
    TriangularPrism = 68,
    /// CutSphere: `params[0]` = radius, `params[1]` = cut_height
    CutSphere = 69,
    /// CutHollowSphere: `params[0]` = radius, `params[1]` = cut_height, `params[2]` = thickness
    CutHollowSphere = 70,
    /// DeathStar: `params[0]` = ra, `params[1]` = rb, `params[2]` = d
    DeathStar = 71,
    /// SolidAngle: `params[0]` = angle, `params[1]` = radius
    SolidAngle = 72,
    /// Rhombus: `params[0]` = la, `params[1]` = lb, `params[2]` = half_height, `params[3]` = round_radius
    Rhombus = 73,
    /// Horseshoe: `params[0]` = angle, `params[1]` = radius, `params[2]` = half_length, `params[3]` = width, `params[4]` = thickness
    Horseshoe = 74,
    /// Vesica: `params[0]` = radius, `params[1]` = half_dist
    Vesica = 75,
    /// InfiniteCylinder: `params[0]` = radius
    InfiniteCylinder = 76,
    /// InfiniteCone: `params[0]` = angle
    InfiniteCone = 77,
    /// Gyroid: `params[0]` = scale, `params[1]` = thickness
    Gyroid = 78,
    /// Heart: `params[0]` = size
    Heart = 79,
    /// Tube: `params[0]` = outer_radius, `params[1]` = thickness, `params[2]` = half_height
    Tube = 80,
    /// Barrel: `params[0]` = radius, `params[1]` = half_height, `params[2]` = bulge
    Barrel = 81,
    /// Diamond: `params[0]` = radius, `params[1]` = half_height
    Diamond = 82,
    /// ChamferedCube: `params[0..3]` = half_extents, `params[3]` = chamfer
    ChamferedCube = 83,
    /// SchwarzP: `params[0]` = scale, `params[1]` = thickness
    SchwarzP = 84,
    /// Superellipsoid: `params[0..3]` = half_extents, `params[3]` = e1, `params[4]` = e2
    Superellipsoid = 85,
    /// RoundedX: `params[0]` = width, `params[1]` = round_radius, `params[2]` = half_height
    RoundedX = 86,
    /// Pie: `params[0]` = angle, `params[1]` = radius, `params[2]` = half_height
    Pie = 87,
    /// Trapezoid: `params[0]` = r1, `params[1]` = r2, `params[2]` = trap_height, `params[3]` = half_depth
    Trapezoid = 88,
    /// Parallelogram: `params[0]` = width, `params[1]` = para_height, `params[2]` = skew, `params[3]` = half_depth
    Parallelogram = 89,
    /// Tunnel: `params[0]` = width, `params[1]` = height_2d, `params[2]` = half_depth
    Tunnel = 90,
    /// UnevenCapsule: `params[0]` = r1, `params[1]` = r2, `params[2]` = cap_height, `params[3]` = half_depth
    UnevenCapsule = 91,
    /// Egg: `params[0]` = ra, `params[1]` = rb
    Egg = 92,
    /// ArcShape: `params[0]` = aperture, `params[1]` = radius, `params[2]` = thickness, `params[3]` = half_height
    ArcShape = 93,
    /// Moon: `params[0]` = d, `params[1]` = ra, `params[2]` = rb, `params[3]` = half_height
    Moon = 94,
    /// CrossShape: `params[0]` = length, `params[1]` = thickness, `params[2]` = round_radius, `params[3]` = half_height
    CrossShape = 95,
    /// BlobbyCross: `params[0]` = size, `params[1]` = half_height
    BlobbyCross = 96,
    /// ParabolaSegment: `params[0]` = width, `params[1]` = para_height, `params[2]` = half_depth
    ParabolaSegment = 97,
    /// RegularPolygon: `params[0]` = radius, `params[1]` = n_sides, `params[2]` = half_height
    RegularPolygon = 98,
    /// StarPolygon: `params[0]` = radius, `params[1]` = n_points, `params[2]` = m, `params[3]` = half_height
    StarPolygon = 99,
    /// Stairs: `params[0]` = step_width, `params[1]` = step_height, `params[2]` = n_steps, `params[3]` = half_depth
    Stairs = 100,
    /// Helix: `params[0]` = major_r, `params[1]` = minor_r, `params[2]` = pitch, `params[3]` = half_height
    Helix = 101,
    /// Tetrahedron: `params[0]` = size
    Tetrahedron = 102,
    /// Dodecahedron: `params[0]` = radius
    Dodecahedron = 103,
    /// Icosahedron: `params[0]` = radius
    Icosahedron = 104,
    /// TruncatedOctahedron: `params[0]` = radius
    TruncatedOctahedron = 105,
    /// TruncatedIcosahedron: `params[0]` = radius
    TruncatedIcosahedron = 106,
    /// BoxFrame: `params[0..3]` = half_extents, `params[3]` = edge
    BoxFrame = 107,
    /// DiamondSurface: `params[0]` = scale, `params[1]` = thickness
    DiamondSurface = 108,
    /// Neovius: `params[0]` = scale, `params[1]` = thickness
    Neovius = 109,
    /// Lidinoid: `params[0]` = scale, `params[1]` = thickness
    Lidinoid = 110,
    /// IWP: `params[0]` = scale, `params[1]` = thickness
    IWP = 111,
    /// FRD: `params[0]` = scale, `params[1]` = thickness
    FRD = 112,
    /// FischerKochS: `params[0]` = scale, `params[1]` = thickness
    FischerKochS = 113,
    /// PMY: `params[0]` = scale, `params[1]` = thickness
    PMY = 114,
    /// Circle2D: `params[0]` = radius, `params[1]` = half_height
    Circle2D = 115,
    /// Rect2D: `params[0..2]` = half_extents, `params[2]` = half_height
    Rect2D = 116,
    /// Segment2D: `params[0..4]` = a,b, `params[4]` = thickness, `params[5]` = half_height
    Segment2D = 117,
    /// Polygon2D: special handling
    Polygon2D = 118,
    /// RoundedRect2D: `params[0..2]` = half_extents, `params[2]` = round_radius, `params[3]` = half_height
    RoundedRect2D = 119,
    /// Annular2D: `params[0]` = outer_radius, `params[1]` = thickness, `params[2]` = half_height
    Annular2D = 120,

    // === Binary Operations (pop 2, push 1) ===
    /// Union: min(a, b)
    Union = 16,
    /// Intersection: max(a, b)
    Intersection = 17,
    /// Subtraction: max(a, -b)
    Subtraction = 18,
    /// SmoothUnion: `params[0]` = k
    SmoothUnion = 19,
    /// SmoothIntersection: `params[0]` = k
    SmoothIntersection = 20,
    /// SmoothSubtraction: `params[0]` = k
    SmoothSubtraction = 21,
    /// ChamferUnion: `params[0]` = r (chamfer radius)
    ChamferUnion = 22,
    /// ChamferIntersection: `params[0]` = r
    ChamferIntersection = 23,
    /// ChamferSubtraction: `params[0]` = r
    ChamferSubtraction = 24,
    /// StairsUnion: `params[0]` = r, `params[1]` = n
    StairsUnion = 25,
    /// StairsIntersection: `params[0]` = r, `params[1]` = n
    StairsIntersection = 26,
    /// StairsSubtraction: `params[0]` = r, `params[1]` = n
    StairsSubtraction = 27,

    // === Extended Binary Operations (128-136) ===
    /// XOR: symmetric difference
    XOR = 128,
    /// Morph: `params[0]` = t (blend factor)
    Morph = 129,
    /// ColumnsUnion: `params[0]` = r, `params[1]` = n
    ColumnsUnion = 130,
    /// ColumnsIntersection: `params[0]` = r, `params[1]` = n
    ColumnsIntersection = 131,
    /// ColumnsSubtraction: `params[0]` = r, `params[1]` = n
    ColumnsSubtraction = 132,
    /// Pipe: `params[0]` = r
    Pipe = 133,
    /// Engrave: `params[0]` = r
    Engrave = 134,
    /// Groove: `params[0]` = ra, `params[1]` = rb
    Groove = 135,
    /// Tongue: `params[0]` = ra, `params[1]` = rb
    Tongue = 136,
    /// ExpSmoothUnion: `params[0]` = k
    ExpSmoothUnion = 137,
    /// ExpSmoothIntersection: `params[0]` = k
    ExpSmoothIntersection = 138,
    /// ExpSmoothSubtraction: `params[0]` = k
    ExpSmoothSubtraction = 139,

    // === Transforms (modify point, then evaluate child) ===
    /// Translate: `params[0..3]` = offset (x, y, z)
    Translate = 32,
    /// Rotate: `params[0..4]` = quaternion (x, y, z, w)
    Rotate = 33,
    /// Scale: `params[0]` = uniform scale factor
    Scale = 34,
    /// ScaleNonUniform: `params[0..3]` = scale factors (x, y, z)
    ScaleNonUniform = 35,

    // === Extended Transforms (200-202) ===
    /// ProjectiveTransform: requires auxiliary data (inv_matrix, lipschitz_bound)
    ProjectiveTransform = 200,
    /// LatticeDeform: requires auxiliary data (control_points, lattice params)
    LatticeDeform = 201,
    /// SdfSkinning: requires auxiliary data (bones)
    SdfSkinning = 202,

    // === Modifiers ===
    /// Twist: `params[0]` = strength
    Twist = 48,
    /// Bend: `params[0]` = curvature
    Bend = 49,
    /// RepeatInfinite: `params[0..3]` = spacing (x, y, z)
    RepeatInfinite = 50,
    /// RepeatFinite: `params[0..3]` = count, `params[3..6]` = spacing
    RepeatFinite = 51,
    /// Round: `params[0]` = radius (post-process: d - radius)
    Round = 52,
    /// Onion: `params[0]` = thickness (post-process: |d| - thickness)
    Onion = 53,
    /// Elongate: `params[0..3]` = amount (x, y, z)
    Elongate = 54,
    /// Noise: `params[0]` = amplitude, `params[1]` = frequency, `params[2]` = seed
    /// Post-processes distance: d += noise(p * frequency) * amplitude
    Noise = 55,
    /// Mirror: `params[0..3]` = axes (non-zero = mirror that axis)
    Mirror = 56,
    /// Revolution: `params[0]` = offset from Y-axis
    Revolution = 57,
    /// Extrude: `params[0]` = half_height
    Extrude = 58,
    /// Taper: `params[0]` = factor
    Taper = 59,
    /// Displacement: `params[0]` = strength (post-process)
    Displacement = 60,
    /// PolarRepeat: `params[0]` = count (as f32)
    PolarRepeat = 61,
    /// SweepBezier: `params[0..6]` = p0.x, p0.z, p1.x, p1.z, p2.x, p2.z
    SweepBezier = 62,
    /// OctantMirror: abs + sort (x >= y >= z)
    OctantMirror = 63,
    /// Shear: `params[0..3]` = shear factors (xy, xz, yz)
    Shear = 144,
    /// Animated: `params[0]` = speed, `params[1]` = amplitude
    Animated = 145,

    // === Extended Modifiers (160-163) ===
    /// IcosahedralSymmetry: no params (maps to fundamental domain)
    IcosahedralSymmetry = 160,
    /// IFS: requires auxiliary data (transforms, iterations)
    IFS = 161,
    /// HeightmapDisplacement: requires auxiliary data (heightmap, dimensions)
    HeightmapDisplacement = 162,
    /// SurfaceRoughness: `params[0]` = frequency, `params[1]` = amplitude, `params[2]` = octaves
    SurfaceRoughness = 163,

    // === Control ===
    /// Pop transform from coordinate stack
    PopTransform = 240,
    /// End of program
    End = 255,
}

impl OpCode {
    /// Returns true if this opcode is a primitive (pushes to value stack)
    #[inline]
    pub fn is_primitive(self) -> bool {
        let v = self as u8;
        v < 16 || (64..=120).contains(&v)
    }

    /// Returns true if this opcode is a binary operation
    #[inline]
    pub fn is_binary_op(self) -> bool {
        let v = self as u8;
        (16..32).contains(&v) || (128..144).contains(&v)
    }

    /// Returns true if this opcode is a transform
    #[inline]
    pub fn is_transform(self) -> bool {
        let v = self as u8;
        (32..48).contains(&v) || (200..=202).contains(&v)
    }

    /// Returns true if this opcode is a modifier
    #[inline]
    pub fn is_modifier(self) -> bool {
        let v = self as u8;
        (48..64).contains(&v) || (144..164).contains(&v)
    }

    /// Returns true if this opcode modifies the evaluation point
    #[inline]
    pub fn modifies_point(self) -> bool {
        self.is_transform()
            || matches!(
                self,
                Self::Twist
                    | Self::Bend
                    | Self::RepeatInfinite
                    | Self::RepeatFinite
                    | Self::Elongate
                    | Self::Mirror
                    | Self::Revolution
                    | Self::Extrude
                    | Self::SweepBezier
                    | Self::Taper
                    | Self::PolarRepeat
                    | Self::OctantMirror
                    | Self::Shear
            )
    }

    /// Returns true if this opcode post-processes the distance value
    #[inline]
    pub const fn is_post_process(self) -> bool {
        matches!(
            self,
            Self::Round
                | Self::Onion
                | Self::Scale
                | Self::Noise
                | Self::Extrude
                | Self::Displacement
        )
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_opcode_categories() {
        assert!(OpCode::Sphere.is_primitive());
        assert!(OpCode::Box3d.is_primitive());
        assert!(!OpCode::Union.is_primitive());

        assert!(OpCode::Union.is_binary_op());
        assert!(OpCode::SmoothUnion.is_binary_op());
        assert!(!OpCode::Sphere.is_binary_op());

        assert!(OpCode::Translate.is_transform());
        assert!(OpCode::Rotate.is_transform());
        assert!(!OpCode::Sphere.is_transform());

        assert!(OpCode::Twist.is_modifier());
        assert!(OpCode::Round.is_modifier());
        assert!(!OpCode::Union.is_modifier());
    }

    #[test]
    fn test_opcode_values() {
        // Ensure opcodes have expected values for serialization
        assert_eq!(OpCode::Sphere as u8, 0);
        assert_eq!(OpCode::Union as u8, 16);
        assert_eq!(OpCode::Translate as u8, 32);
        assert_eq!(OpCode::Twist as u8, 48);
        assert_eq!(OpCode::RoundedBox as u8, 64);
        assert_eq!(OpCode::Helix as u8, 101);
        assert_eq!(OpCode::End as u8, 255);
    }

    #[test]
    fn test_extended_primitives_are_primitives() {
        assert!(OpCode::RoundedBox.is_primitive());
        assert!(OpCode::Stairs.is_primitive());
        assert!(OpCode::Helix.is_primitive());
        assert!(OpCode::Heart.is_primitive());
        assert!(OpCode::Gyroid.is_primitive());
        assert!(!OpCode::RoundedBox.is_binary_op());
        assert!(!OpCode::RoundedBox.is_transform());
        assert!(!OpCode::RoundedBox.is_modifier());
    }

    #[test]
    fn test_new_transforms() {
        // Test the 3 new transform opcodes
        assert!(OpCode::ProjectiveTransform.is_transform());
        assert!(OpCode::LatticeDeform.is_transform());
        assert!(OpCode::SdfSkinning.is_transform());

        assert!(!OpCode::ProjectiveTransform.is_primitive());
        assert!(!OpCode::ProjectiveTransform.is_binary_op());
        assert!(!OpCode::ProjectiveTransform.is_modifier());

        assert_eq!(OpCode::ProjectiveTransform as u8, 200);
        assert_eq!(OpCode::LatticeDeform as u8, 201);
        assert_eq!(OpCode::SdfSkinning as u8, 202);
    }

    #[test]
    fn test_new_modifiers() {
        // Test the 4 new modifier opcodes
        assert!(OpCode::IcosahedralSymmetry.is_modifier());
        assert!(OpCode::IFS.is_modifier());
        assert!(OpCode::HeightmapDisplacement.is_modifier());
        assert!(OpCode::SurfaceRoughness.is_modifier());

        assert!(!OpCode::IcosahedralSymmetry.is_primitive());
        assert!(!OpCode::IcosahedralSymmetry.is_binary_op());
        assert!(!OpCode::IcosahedralSymmetry.is_transform());

        assert_eq!(OpCode::IcosahedralSymmetry as u8, 160);
        assert_eq!(OpCode::IFS as u8, 161);
        assert_eq!(OpCode::HeightmapDisplacement as u8, 162);
        assert_eq!(OpCode::SurfaceRoughness as u8, 163);
    }
}