import gradio as gr # import pickle # from datasets import load_from_disk from plaid.containers.sample import Sample # import pyvista as pv import numpy as np import pyrender from trimesh import Trimesh import matplotlib as mpl import matplotlib.cm as cm import os # switch to "osmesa" or "egl" before loading pyrender os.environ["PYOPENGL_PLATFORM"] = "egl" os.system("wget https://zenodo.org/records/10124594/files/Tensile2d.tar.gz") os.system("tar -xvf Tensile2d.tar.gz") # FOLDER = "plot" # dataset = load_from_disk("Rotor37") field_names_train = ["sig11", "sig22", "sig12", "U1", "U2", "q"] def sample_info(sample_id_str, fieldn): plaid_sample = Sample.load_from_dir(f"Tensile2d/dataset/samples/sample_"+str(sample_id_str).zfill(9)) nodes = plaid_sample.get_nodes() field = plaid_sample.get_field(fieldn) if nodes.shape[1] == 2: nodes__ = np.zeros((nodes.shape[0],nodes.shape[1]+1)) nodes__[:,:-1] = nodes nodes = nodes__ triangles = plaid_sample.get_elements()['TRI_3'] # generate colormap if np.linalg.norm(field) > 0: norm = mpl.colors.Normalize(vmin=np.min(field), vmax=np.max(field)) cmap = cm.coolwarm m = cm.ScalarMappable(norm=norm, cmap=cmap) vertex_colors = m.to_rgba(field)[:,:3] else: vertex_colors = 1+np.zeros((field.shape[0], 3)) vertex_colors[:,0] = 0.2298057 vertex_colors[:,1] = 0.01555616 vertex_colors[:,2] = 0.15023281 # generate mesh trimesh = Trimesh(vertices = nodes, faces = triangles) trimesh.visual.vertex_colors = vertex_colors mesh = pyrender.Mesh.from_trimesh(trimesh, smooth=False) # compose scene scene = pyrender.Scene(ambient_light=[.1, .1, .3], bg_color=[0, 0, 0]) camera = pyrender.PerspectiveCamera( yfov=np.pi / 3.0) light = pyrender.DirectionalLight(color=[1,1,1], intensity=1000.) scene.add(mesh, pose= np.eye(4)) scene.add(light, pose= np.eye(4)) c = 3**-0.5 scene.add(camera, pose=[[ 1, 0, 0, 0], [ 0, c, -c, -2], [ 0, c, c, 1.2], [ 0, 0, 0, 1]]) # render scene r = pyrender.OffscreenRenderer(1024, 1024) color, _ = r.render(scene) str__ = f"loading sample {sample_id_str}" return str__, color if __name__ == "__main__": with gr.Blocks() as demo: d1 = gr.Slider(0, 499, value=0, label="Training sample id", info="Choose between 0 and 499") d2 = gr.Dropdown(field_names_train, value=field_names_train[0], label="Field name") output1 = gr.Text(label="Training sample info") output2 = gr.Image(label="Training sample visualization") d1.input(sample_info, [d1, d2], [output1, output2]) d2.input(sample_info, [d1, d2], [output1, output2]) demo.launch()