File size: 2,597 Bytes
a83127a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""DSAFD DSL Parser — trimesh valós 3D mesh generálás"""
import re
import numpy as np
import trimesh

def to_vec(raw, default):
    try:
        parts = [float(x.strip()) for x in raw.strip('[]').split(',')[:3]]
        while len(parts) < 3:
            parts.append(0.0)
        return np.array(parts, dtype=np.float64)
    except Exception:
        return np.array(default, dtype=np.float64)

def parse_dsl_to_mesh(dsl: str) -> dict:
    lower = dsl.lower()
    meshes = []
    primitives = re.findall(r'primitive\s+(\w+)\s*\(([^)]*)\)', lower)

    for ptype, params_str in primitives:
        params = {}
        for part in params_str.split(','):
            part = part.strip()
            if ':' in part:
                k, v = part.split(':', 1)
                params[k.strip()] = v.strip()

        center = to_vec(params.get('center', '0,0,0'), [0,0,0])
        m = None

        try:
            if ptype == 'box':
                size = to_vec(params.get('size', '10,10,10'), [10,10,10])
                m = trimesh.creation.box(extents=size.tolist())
            elif ptype == 'sphere':
                radius = float(params.get('radius', '5'))
                m = trimesh.creation.icosphere(radius=radius, subdivisions=3)
            elif ptype == 'cylinder':
                radius = float(params.get('radius', '3'))
                height = float(params.get('height', '15'))
                m = trimesh.creation.cylinder(radius=radius, height=height, sections=32)
            elif ptype == 'cone':
                radius = float(params.get('radius_bottom', params.get('radius', '5')))
                height = float(params.get('height', '10'))
                m = trimesh.creation.cone(radius=radius, height=height, sections=32)
            elif ptype == 'torus':
                major = float(params.get('major_radius', '10'))
                minor = float(params.get('minor_radius', '2'))
                m = trimesh.creation.torus(major_radius=major, minor_radius=minor)

            if m is not None:
                m.vertices += center
                meshes.append(m)
        except Exception:
            continue

    if not meshes:
        mesh = trimesh.creation.box(extents=[2, 2, 2])
    else:
        mesh = meshes[0]
        for m in meshes[1:]:
            try:
                mesh = mesh + m
            except Exception:
                pass

    vertices = mesh.vertices.flatten().tolist()
    indices = mesh.faces.flatten().tolist()
    return {
        "positions": vertices,
        "indices": indices,
        "vertex_count": len(mesh.vertices),
    }