File size: 3,112 Bytes
5ea1b6f
5eb7f8a
 
5ea1b6f
5eb7f8a
ede25fc
2e0c5aa
5ea1b6f
2e121c3
2e0c5aa
 
ede25fc
5ea1b6f
 
 
ede25fc
2e0c5aa
5eb7f8a
 
ede25fc
6f3ba92
ede25fc
6902d7b
ede25fc
e2ad235
ede25fc
 
 
 
5ea1b6f
 
5eb7f8a
 
 
 
2e0c5aa
 
 
 
 
 
 
 
 
 
 
d9403e1
 
 
 
 
 
 
27ec183
e7895e8
 
 
 
65555e4
2e0c5aa
 
 
 
65555e4
 
 
2e0c5aa
 
65555e4
 
2e0c5aa
65555e4
2495238
 
65555e4
2e0c5aa
65555e4
2e0c5aa
1036858
5ea1b6f
 
764e17a
c257e9e
764e17a
 
5ea1b6f
2e0c5aa
5ea1b6f
 
 
 
 
6902d7b
5ea1b6f
 
 
 
4629515
5ea1b6f
 
 
 
ede25fc
 
5ea1b6f
ede25fc
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
import gradio as gr
import pickle
from datasets import load_dataset
from plaid.containers.sample import Sample


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")

hf_dataset = load_dataset("PLAID-datasets/Tensile2d", split="all_samples")

nb_samples = 500

field_names_train = ["sig11", "sig22", "sig12", "U1", "U2", "q"]




def sample_info(sample_id_str, fieldn):

    sample_ = hf_dataset[int(sample_id_str)]["sample"]
    plaid_sample = Sample.model_validate(pickle.loads(sample_))
    # 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))

    scene.add(camera, pose=[[ 1,  0,  0,  0],
                            [ 0,  1,  0,  0],
                            [ 0,  0,  1,  3],
                            [ 0,  0,  0,  1]])

    # render scene
    r = pyrender.OffscreenRenderer(1024, 1024)
    color, _ = r.render(scene)
    

    
    str__ = f"Training sample {sample_id_str}\n"
    str__ += str(plaid_sample)+"\n"
    str__ += f"number of nodes: {nodes.shape[0]}"

    return str__, color


if __name__ == "__main__":

    with gr.Blocks() as demo:
        d1 = gr.Slider(0, nb_samples-1, value=0, label="Training sample id", info="Choose between 0 and "+str(nb_samples-1))
        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()