04-CSG

04-CSG

"""Example script demonstrating a CSG operation

For more details on the available job types please refer to the Metafold REST API
documentation.
"""
import pandas as pd
import numpy as np
from matplotlib import pyplot
import os
import csv
from numpy.typing import ArrayLike
from pprint import pprint
from scipy.spatial.transform import Rotation as R
from metafold import MetafoldClient
from metafold.func_types import JSONEvaluator, FuncType, Vec3f, Mat4f
from metafold.func import *


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




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

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([-1.0, -1.0, -1.0])
box1_params = {
    "shape_type": "Box",
    "size": [2, 2, 2],
    "xform": xform(box1_rot, box1_pos).flatten()
}

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

shape_func= Threshold(
                Redistance(
                        CSG(
                            SampleBox(source, parameters=box1_params),
                            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_u.stl")
"""e=GraphEvaluator()
shape_func(e)

draw_graph(e.graph)"""