-
Notifications
You must be signed in to change notification settings - Fork 3
/
export.py
342 lines (290 loc) · 11.9 KB
/
export.py
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
"""
Export a html page with each step visualized.
"""
import os
from typing import Optional, Sequence
import numba
import numpy as np
import pytorch3d.structures
import torch
from example import Example, example_config, example_load
from reconstruction import mesh_extraction
from reconstruction.data.chunks import ChunkGrid
from reconstruction.filters.dilate import dilate
from reconstruction.mathlib import Vec3i
from reconstruction.medial_axis_propagating import crust_fix
from reconstruction.mincut import MinCut
from reconstruction.reconstruction import scale_model, crust_dilation, plot_voxels, diffuse, fill_components, \
cleanup_components
from reconstruction.render.cloud_render import CloudRender
from reconstruction.render.voxel_render import VoxelRender
from reconstruction.utils import timed
numba.config.THREADING_LAYER = 'omp'
# Configuration, modify here to change the model
CHUNKSIZE = 16
RESOLUTION_INIT = 64
example = Example.BunnyFixed
STEPS = 3
APPROX_MEDIAL_AXIS = False
if __name__ == '__main__':
# Set initial resolution
resolution = RESOLUTION_INIT
medial_name = "_medial" if APPROX_MEDIAL_AXIS else ""
name = f"{example.name}{medial_name}"
# Export path
path = os.path.join("result", name)
os.makedirs(path, exist_ok=True)
plots = []
print("Loading model")
with timed("\tTime: "):
data = example_load(example)
cfg = example_config[example]
dilations_max = cfg["dilations_max"]
dilations_reverse = cfg["dilations_reverse"]
data_pts, data_offset, data_scale = scale_model(data, resolution=resolution)
model: ChunkGrid[np.bool8] = ChunkGrid(CHUNKSIZE, dtype=np.bool8, fill_value=np.bool8(False))
model[data_pts] = True
model.pad_chunks(2)
model.cleanup()
# Plot only a part of the model points (large models will freeze the browser)
plot_model: Optional[np.ndarray] = data_pts[::5]
crust: ChunkGrid[np.bool8] = model.copy()
crust.cleanup(remove=True)
# Model view
ren = VoxelRender()
fig = ren.make_figure()
fig.add_trace(ren.grid_voxel(crust, opacity=0.1, name='Initial'))
fig.add_trace(CloudRender().make_scatter(plot_model, size=1, name='Model'))
plots.append(os.path.join(path, "model.html"))
fig.write_html(plots[-1], include_plotlyjs='cdn')
print("Dilation")
with timed("\tTime: "):
crust, components, dilation_step = crust_dilation(crust, max_steps=dilations_max,
reverse_steps=dilations_reverse)
# assert components._fill_value == 2
fig = plot_voxels(components == 0, components, title=f"Initial Dilation")
plots.append(os.path.join(path, f"dilation_start.html"))
fig.write_html(plots[-1], include_plotlyjs='cdn')
crust_dilate = dilate(crust)
outer_fill = components == 2
crust_outer = outer_fill & crust_dilate
crust_inner = (components == 3) & crust_dilate
assert crust_dilate._fill_value == False
assert outer_fill._fill_value == True
assert crust_outer._fill_value == False
assert crust_inner._fill_value == False
"""
Increase resolution and make the crust_fixmesh approximation finer
"""
for resolution_step in range(0, STEPS):
print(f"RESOLUTION STEP: {resolution_step}")
path_step = os.path.join(path, str(resolution_step))
os.makedirs(path_step, exist_ok=True)
if APPROX_MEDIAL_AXIS:
"""
Approximate Voxel near Medial Axis, by propagating a Normal field inwards.
Then for each voxel compute a normal cone and mark the voxel as inner component when the cone angle is greater than 90°.
"""
print("Crust-Fix")
with timed("\tTime: "):
medial_axis, medial_figs = crust_fix(
crust, outer_fill, crust_outer, crust_inner,
min_distance=dilation_step,
data_pts=plot_model,
return_figs=True
)
crust_inner |= medial_axis
for fig_name, fig in medial_figs.items():
plots.append(os.path.join(path_step, f"{fig_name}.html"))
fig.write_html(plots[-1], include_plotlyjs='cdn')
# # crust_inner[model] = False # Remove model voxels if they have been added by the crust fix
print("Render Crust")
with timed("\tTime: "):
ren = VoxelRender()
fig = ren.make_figure(title=f"Step-{resolution_step}: Crust")
fig.add_trace(ren.grid_voxel(crust_outer, opacity=0.1, name='Outer'))
fig.add_trace(ren.grid_voxel(crust_inner, opacity=1.0, name='Inner'))
if plot_model is not None:
fig.add_trace(CloudRender().make_scatter(plot_model, size=0.7, name='Model'))
plots.append(os.path.join(path_step, f"crust.html"))
fig.write_html(plots[-1], include_plotlyjs='cdn')
print("Diffusion")
with timed("\tTime: "):
diff = diffuse(model, repeat=3)
print("Plot-Diffusion")
with timed("\tTime: "):
ren = CloudRender()
fig = ren.make_figure()
# Cut in half
diff_mask = (diff != 1.0) & crust
half = (np.max(data_pts, axis=0) + np.min(data_pts, axis=0)).astype(int) // 2
half_chunk = half // diff_mask.chunk_size
half_chunk_split = half[2] % diff_mask.chunk_size
for index in list(diff_mask.chunks.keys()):
if index[2] > half_chunk[2]:
del diff_mask.chunks[index]
elif index[2] == half_chunk[2]:
ch = diff_mask.chunks.get(index)
arr = ch.to_array()
arr[:, :, half_chunk_split:] = False
ch.set_array(arr)
items = list(diff.items(mask=diff_mask))
items.sort(key=lambda e: e[0][2] * 1024 + e[0][1] + e[0][0])
points, values = zip(*items) # type: Sequence[Vec3i], Sequence
pts = np.array(points, dtype=np.float32) + 0.5
fig.add_trace(ren.make_scatter(
pts,
name="Diffusion",
marker=dict(
size=2.0,
opacity=0.7,
colorscale='Viridis',
color=np.array(values)
),
mode="markers",
))
plots.append(os.path.join(path_step, f"diffusion.html"))
fig.write_html(plots[-1], include_plotlyjs='cdn')
print("MinCut")
with timed("\tTime: "):
mincut = MinCut(diff, crust, crust_outer, crust_inner)
segment0, segment1 = mincut.grid_segments()
thincrust = segment0 & segment1
print("Render")
with timed("\tTime: "):
ren = VoxelRender()
fig = ren.make_figure(title=f"Step-{resolution_step}: Segments")
fig.add_trace(ren.grid_voxel(segment0, opacity=0.1, name='Segment 0'))
fig.add_trace(ren.grid_voxel(segment1, opacity=0.1, name='Segment 1'))
fig.add_trace(ren.grid_voxel(thincrust, opacity=1.0, name='Join'))
if plot_model is not None:
fig.add_trace(CloudRender().make_scatter(plot_model, size=1, name='Model'))
plots.append(os.path.join(path_step, f"mincut.html"))
fig.write_html(plots[-1], include_plotlyjs='cdn')
print("Volumetric refinement")
with timed("\tTime: "):
# Rebuild model
resolution *= 2
data_pts, data_offset, data_scale = scale_model(data, resolution=resolution)
model = ChunkGrid(CHUNKSIZE, np.bool8, fill_value=np.bool8(False))
model[data_pts] = np.bool8(True)
plot_model: Optional[np.ndarray] = data_pts[::5]
# Build new crust
crust = dilate(dilate(thincrust.split(2), steps=1) | dilate(model, steps=3))
crust.cleanup(remove=True)
crust.pad_chunks(1)
components, count = fill_components(crust, max_components=5)
cleanup_components(crust, components, count)
outer_fill = (components == 2)
outer_fill.cleanup(remove=True)
crust_dilate = dilate(crust)
crust_outer = outer_fill & crust_dilate
crust_inner = (components == 3) & crust_dilate
crust_outer.cleanup(remove=True)
crust_inner.cleanup(remove=True)
dilation_step = 2
# Validate data
assert crust._fill_value == False
assert outer_fill._fill_value == True
assert crust_outer._fill_value == False
assert crust_inner._fill_value == False
print("Extract mesh")
with timed("\tTime: "):
# Extraction
mesh_extractor = mesh_extraction.MeshExtraction(mincut)
vertices, faces = mesh_extractor.extract_mesh()
ren = VoxelRender()
fig = ren.make_figure()
fig.add_trace(ren.make_mesh(vertices, faces, name='Mesh', flatshading=True))
fig.add_trace(ren.make_wireframe(vertices, faces, name='Wireframe'))
fig.update_layout(showlegend=True)
plots.append(os.path.join(path_step, f"mesh_extraction.html"))
fig.write_html(plots[-1], include_plotlyjs='cdn')
print("Smoothing mesh")
with timed("\tTime: "):
# Smoothing
pytorch_mesh = pytorch3d.structures.Meshes(verts=[torch.FloatTensor(vertices)],
faces=[torch.LongTensor(faces)])
smoothed_vertices = mesh_extraction.Smoothing().smooth(vertices, faces, diff, pytorch_mesh)
verts = smoothed_vertices.cpu().detach().numpy()
faces = torch.cat(pytorch_mesh.faces_list()).cpu().detach().numpy()
ren = VoxelRender()
fig = ren.make_figure()
fig.add_trace(ren.make_mesh(verts, faces, name='Mesh', flatshading=False))
fig.add_trace(ren.make_wireframe(verts, faces, name='Wireframe'))
fig.update_layout(showlegend=True)
plots.append(os.path.join(path_step, f"mesh_final.html"))
fig.write_html(plots[-1], include_plotlyjs='cdn')
links = ""
for p in plots:
rel_p = p[len(path) + 1:]
links += f"<li><a href=\"{rel_p}\" target=\"page\">{rel_p}</a></li>"
html = f"""<html>
<head>
<title>{name}</title>
<style>
html, body {{
height: 100%;
margin: 0;
}}
#iframe_page {{
width:100%;height:100%;
display: inline-block;
padding:0; margin:0;
}}
/* DivTable.com */
.divTable{{
display: table;
width: 100%; height:100%;
}}
.divTableRow {{
display: table-row;
}}
.divTableHeading {{
background-color: #EEE;
display: table-header-group;
}}
.divTableCell, .divTableHead {{
border: 1px solid #999999;
display: table-cell;
padding: 3px 10px;
}}
.divTableHeading {{
background-color: #EEE;
display: table-header-group;
font-weight: bold;
}}
.divTableFoot {{
background-color: #EEE;
display: table-footer-group;
font-weight: bold;
}}
.divTableBody {{
display: table-row-group;
}}
</style>
</head>
<body>
<div class="divTable">
<div class="divTableBody">
<div class="divTableRow">
<div class="divTableCell" style="vertical-align: top; max-width: 40px;">
<div>
<ul>
<li><a href="..">..</a></li>
</ul>
<ul>
{links}
</ul>
</ul>
</div>
</div>
<div class="divTableCell" style="position:relative;padding:0; margin:0;"><iframe src="model.html" name="page" id="iframe_page"></iframe></div>
</div>
</div>
</div>
</body>
</html>
"""
with open(os.path.join(path, "index.html"), 'wt') as fp:
fp.writelines(html)