Repository: Genesis-Embodied-AI/Genesis
File: genesis/utils/mesh.py (Python)

Code before:
        mesh.visual = trimesh.visual.TextureVisuals(
            uv=np.array(
                [
                    [0, 0],
                    [0, 0],
                    [0, size],
                    [0, size],
                    [size, 0],
                    [size, 0],
                    [size, size],
                    [size, size],
                ],
                dtype=np.float32,
            ),
            material=trimesh.visual.material.SimpleMaterial(
                image=Image.open(os.path.join(get_assets_dir(), "textures/checker.png")),
            ),
        )
    else:
        mesh.visual = trimesh.visual.ColorVisuals(vertex_colors=np.tile(color, [len(mesh.vertices), 1]).astype(float))
    return mesh


def generate_tetgen_config_from_morph(morph):
    return dict(
        order=getattr(morph, "order", 1),
        mindihedral=getattr(morph, "mindihedral", 10),
        minratio=getattr(morph, "minratio", 1.1),
        nobisect=getattr(morph, "nobisect", True),
        quality=getattr(morph, "quality", True),
        maxvolume=getattr(morph, "maxvolume", -1.0),
        verbose=getattr(morph, "verbose", 0),
    )


def make_tetgen_switches(cfg):
    """Build a TetGen switches string from a config dict."""
    flags = ["p"]

    if cfg.get("quality", True):
        r = cfg.get("minratio", 1.1)
        di = cfg.get("mindihedral", 10)
        flags.append(f"q{r}/{di}")

    a = cfg.get("maxvolume", -1.0)
    if a > 0:
        flags.append(f"a{a}")

    o = cfg.get("order", 1)
    if o != 1:
        flags.append(f"o{o}")

Review comment (suggestion):
`getattr` is not a good approach. `morph` must have these attributes, already set with the right default value. Add some `isinstance` check for the morph and raise an exception if its type is not appropriate.

Code after:
        mesh.visual = trimesh.visual.TextureVisuals(
            uv=np.array(
                [
                    [0, 0],
                    [0, 0],
                    [0, size],
                    [0, size],
                    [size, 0],
                    [size, 0],
                    [size, size],
                    [size, size],
                ],
                dtype=np.float32,
            ),
            material=trimesh.visual.material.SimpleMaterial(
                image=Image.open(os.path.join(get_assets_dir(), "textures/checker.png")),
            ),
        )
    else:
        mesh.visual = trimesh.visual.ColorVisuals(vertex_colors=np.tile(color, [len(mesh.vertices), 1]).astype(float))
    return mesh


def generate_tetgen_config_from_morph(morph):
    if not isinstance(morph, gs.options.morphs.TetGenMixin):
        raise TypeError(
            f"Expected an instance of a class that inherits from TetGenMixin, but got an instance of {type(morph).name}."
        )
    return dict(
        order=morph.order,
        mindihedral=morph.mindihedral,
        minratio=morph.minratio,
        nobisect=morph.nobisect,
        quality=morph.quality,
        maxvolume=morph.maxvolume,
        verbose=morph.verbose,
    )


def make_tetgen_switches(cfg):
    """Build a TetGen switches string from a config dict."""
    flags = ["p"]

    if cfg.get("quality", True):
        r = cfg.get("minratio", 1.1)
        di = cfg.get("mindihedral", 10)
        flags.append(f"q{r}/{di}")

    a = cfg.get("maxvolume", -1.0)
    if a > 0:
        flags.append(f"a{a}")