05-CSG with Surface lattice

05-CSG with Surface lattice

"""Example script demonstrating running multiple jobs in parallel.

Note the Metafold API applies rate limiting to job dispatches so running jobs in
parallel will give limited performance improvements.

Usage:
    python examples/surface_lattice_metrics_parallel.py -t <token> -p <project> -n <procs>

For more details on the available job types please refer to the Metafold REST API
documentation.
"""
from argparse import ArgumentParser
from functools import partial
from metafold import MetafoldClient
from multiprocessing import Pool
from numpy.typing import ArrayLike
from pprint import pprint
import pandas as pd
import numpy as np
import os
import csv

import numpy as np
from metafold.func_types import JSONEvaluator
from metafold.func import (
    CSGIntersect,
    EllipsoidPrimitive,
    GenerateSamplePoints,
    LoadVolume,
    Shell,
    PointSource,
    Redistance,
    SampleSurfaceLattice,
    SampleSurfaceLattice_Parameters,
    SampleVolume,
    SampleBox,
    Threshold,
    VolumeAsset
)
from scipy.spatial.transform import Rotation as R
import numpy as np
from numpy.typing import ArrayLike
from metafold.func_types import JSONEvaluator, FuncType, Vec3f, Mat4f
from metafold.func import *


project_id = 5701
access_token = ""
client=MetafoldClient(access_token,project_id)




source = GenerateSamplePoints(       
                              {"offset": [
                                         -2.5,
                                         -2.5,
                                         -2.5
                                        ],
                                        "size": [
                                        5,
                                        5,
                                        5
                                        ],
                                        "resolution": [
                                        128,
                                        128,
                                        128
                                        ]
                              }
                            )

def xform(
    rotation = np.zeros(3),
    translation = np.zeros(3),
    custom: Optional[str] = None,
) -> Mat4f:
    """ Create define shape transformation as you would in the metafold app.

        @param rotation: [x, y, z] euler angles (degrees)
        @param translation: [x, y, z] shift
    """
    if custom == "Cylinder":
        rotation_ = [rotation[0] + 90, rotation[2], rotation[1]]
    else:
        rotation_ = rotation


    rot = R.from_euler("xyz", rotation_,  degrees=True).as_matrix()
    xform_ = np.eye(4)
    xform_[:3, :3] = rot
    xform_[3, :3] = translation
    return xform_

box1_rot = np.array([0.0, 0.0, 0.0])
box1_pos = np.array([0, 0, 0])
box1_params = {
    "shape_type": "Box",
    "size": [3, 3, 3],
    "xform": xform(box1_rot, box1_pos).flatten()
}

sphere_pos = np.array([2.0, 2.0, 2.0])
sphere_rot = np.array([0.0, 0.0, 0.0])
sphere_params = {
    "shape_type": "Ellipsoid",
    "size": [5, 5, 5],
    "xform": xform(sphere_rot, sphere_pos).flatten()
}

shape_func= Threshold(
                Redistance(
                        CSG(
                            CSG(
                                SampleBox(source, parameters=box1_params),
                                SampleSurfaceLattice(
                                    PointSource,
                                    {
                                        "lattice_type": "Gyroid",
                                        "scale": [1, 1 , 1],
                                        "threshold": 0,
                                    },
                                ),
                                parameters={"smoothing": 0.1, "operation": "Intersect"}
                            ),
                        SampleBox(source, parameters=sphere_params), 
                        parameters={"smoothing": 0.1, "operation": "Union"}
                        )
                ),
                {
                    "width": 0.03,
                },
            )    

evaluator = JSONEvaluator(source) 
shape_func(evaluator)
graph_json=evaluator.json()
job= client.jobs.run("evaluate_metrics", {"graph": graph_json, "point_source": 0})
pprint((job.meta["relative_density"]))
export_job = client.jobs.run("export_triangle_mesh", {
        "graph": graph_json,
        "point_source": 0,
    })

export_asset = export_job.assets[0].id
client.assets.download_file(export_asset, "out_iu.stl")