fabiencasenave commited on
Commit
78fdcf8
·
verified ·
1 Parent(s): f4c6266

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +58 -40
app.py CHANGED
@@ -40,48 +40,65 @@ def sample_info(sample_id_str, fieldn):
40
 
41
  nodes = plaid_sample.get_nodes()
42
  field = plaid_sample.get_field(fieldn)
43
- if nodes.shape[1] == 2:
44
- nodes__ = np.zeros((nodes.shape[0],nodes.shape[1]+1))
45
- nodes__[:,:-1] = nodes
46
- nodes = nodes__
47
-
 
 
 
 
 
48
 
49
  quads = plaid_sample.get_elements()['QUAD_4']
 
 
 
 
 
 
 
 
 
 
 
 
50
 
51
- # generate colormap
52
- if np.linalg.norm(field) > 0:
53
- norm = mpl.colors.Normalize(vmin=np.min(field), vmax=np.max(field))
54
- cmap = cm.nipy_spectral#cm.coolwarm
55
- m = cm.ScalarMappable(norm=norm, cmap=cmap)
56
 
57
- vertex_colors = m.to_rgba(field)[:,:3]
58
- else:
59
- vertex_colors = 1+np.zeros((field.shape[0], 3))
60
- vertex_colors[:,0] = 0.2298057
61
- vertex_colors[:,1] = 0.01555616
62
- vertex_colors[:,2] = 0.15023281
63
-
64
- # generate mesh
65
- trimesh = Trimesh(vertices = nodes, faces = quads)
66
- trimesh.visual.vertex_colors = vertex_colors
67
- mesh = pyrender.Mesh.from_trimesh(trimesh, smooth=False)
68
-
69
- # compose scene
70
- scene = pyrender.Scene(ambient_light=[.1, .1, .3], bg_color=[0, 0, 0])
71
- camera = pyrender.PerspectiveCamera( yfov=np.pi / 6.0)
72
- light = pyrender.DirectionalLight(color=[1,1,1], intensity=1000.)
73
-
74
- scene.add(mesh, pose= np.eye(4))
75
- scene.add(light, pose= np.eye(4))
76
-
77
- scene.add(camera, pose=[[ 1, 0, 0, 0.02],
78
- [ 0, 1, 0, 0.21],
79
- [ 0, 0, 1, 0.19],
80
- [ 0, 0, 0, 1]])
81
-
82
- # render scene
83
- r = pyrender.OffscreenRenderer(1024, 1024)
84
- color, _ = r.render(scene)
85
 
86
 
87
  str__ = f"Training sample {sample_id_str}\n"
@@ -105,7 +122,7 @@ def sample_info(sample_id_str, fieldn):
105
  for fname in hf_dataset.description['out_fields_names']:
106
  str__ += f"- {fname}\n"
107
 
108
- return str__, color
109
 
110
 
111
  if __name__ == "__main__":
@@ -118,7 +135,8 @@ if __name__ == "__main__":
118
  output1 = gr.Text(label="Training sample info")
119
  with gr.Column():
120
  d2 = gr.Dropdown(field_names_train, value=field_names_train[0], label="Field name")
121
- output2 = gr.Image(label="Training sample visualization")
 
122
 
123
  d1.input(sample_info, [d1, d2], [output1, output2])
124
  d2.input(sample_info, [d1, d2], [output1, output2])
 
40
 
41
  nodes = plaid_sample.get_nodes()
42
  field = plaid_sample.get_field(fieldn)
43
+ # if nodes.shape[1] == 2:
44
+ # nodes__ = np.zeros((nodes.shape[0],nodes.shape[1]+1))
45
+ # nodes__[:,:-1] = nodes
46
+ # nodes = nodes__
47
+
48
+ norm = (field - field.min()) / (field.max() - field.min())
49
+ colormap_func = mpl.pyplot.get_cmap('viridis')
50
+ rgb_colors = colormap_func(norm)[:, :3]
51
+
52
+ nb_nodes = nodes.shape[0]
53
 
54
  quads = plaid_sample.get_elements()['QUAD_4']
55
+ nb_quads = quads.shape[0]
56
+
57
+ assert field.shape[0] == nb_nodes
58
+
59
+ with open("visu.obj", 'w') as f:
60
+ for i in range(nb_nodes):
61
+ f.write(f"v {nodes[i,0]} {nodes[i,1]} {nodes[i,2]} {rgb_colors[i,0]} {rgb_colors[i,1]} {rgb_colors[i,2]}\n")
62
+
63
+ for i in range(nb_quads):
64
+ f.write(f"f {quads[i,0] + 1} {quads[i,1] + 1} {quads[i,2] + 1} {quads[i,3] + 1}\n")
65
+
66
+ # quads = plaid_sample.get_elements()['QUAD_4']
67
 
68
+ # # generate colormap
69
+ # if np.linalg.norm(field) > 0:
70
+ # norm = mpl.colors.Normalize(vmin=np.min(field), vmax=np.max(field))
71
+ # cmap = cm.nipy_spectral#cm.coolwarm
72
+ # m = cm.ScalarMappable(norm=norm, cmap=cmap)
73
 
74
+ # vertex_colors = m.to_rgba(field)[:,:3]
75
+ # else:
76
+ # vertex_colors = 1+np.zeros((field.shape[0], 3))
77
+ # vertex_colors[:,0] = 0.2298057
78
+ # vertex_colors[:,1] = 0.01555616
79
+ # vertex_colors[:,2] = 0.15023281
80
+
81
+ # # generate mesh
82
+ # trimesh = Trimesh(vertices = nodes, faces = quads)
83
+ # trimesh.visual.vertex_colors = vertex_colors
84
+ # mesh = pyrender.Mesh.from_trimesh(trimesh, smooth=False)
85
+
86
+ # # compose scene
87
+ # scene = pyrender.Scene(ambient_light=[.1, .1, .3], bg_color=[0, 0, 0])
88
+ # camera = pyrender.PerspectiveCamera( yfov=np.pi / 6.0)
89
+ # light = pyrender.DirectionalLight(color=[1,1,1], intensity=1000.)
90
+
91
+ # scene.add(mesh, pose= np.eye(4))
92
+ # scene.add(light, pose= np.eye(4))
93
+
94
+ # scene.add(camera, pose=[[ 1, 0, 0, 0.02],
95
+ # [ 0, 1, 0, 0.21],
96
+ # [ 0, 0, 1, 0.19],
97
+ # [ 0, 0, 0, 1]])
98
+
99
+ # # render scene
100
+ # r = pyrender.OffscreenRenderer(1024, 1024)
101
+ # color, _ = r.render(scene)
102
 
103
 
104
  str__ = f"Training sample {sample_id_str}\n"
 
122
  for fname in hf_dataset.description['out_fields_names']:
123
  str__ += f"- {fname}\n"
124
 
125
+ return str__, "./visu.obj"
126
 
127
 
128
  if __name__ == "__main__":
 
135
  output1 = gr.Text(label="Training sample info")
136
  with gr.Column():
137
  d2 = gr.Dropdown(field_names_train, value=field_names_train[0], label="Field name")
138
+ # output2 = gr.Image(label="Training sample visualization")
139
+ output2 = gr.Model3D(label="Training sample visualization")
140
 
141
  d1.input(sample_info, [d1, d2], [output1, output2])
142
  d2.input(sample_info, [d1, d2], [output1, output2])