|
| 1 | +import os |
| 2 | +import random |
| 3 | +import argparse |
| 4 | +import numpy as np |
| 5 | +import colorsys |
| 6 | +from skimage import measure |
| 7 | +import tqdm |
| 8 | + |
| 9 | + |
| 10 | +import torch |
| 11 | +import torch.nn as nn |
| 12 | +import torch.nn.functional as F |
| 13 | + |
| 14 | +from utils import build_network |
| 15 | + |
| 16 | +# For extracting mesh through marching cubes: |
| 17 | +# python marching_cubes.py --name output --epochs 2000 |
| 18 | + |
| 19 | + |
| 20 | +######################## |
| 21 | +# this function you need to complete |
| 22 | +def compute_SDFs(net, device, nb_grid): |
| 23 | + x = np.linspace(-1.5, 1.5, nb_grid) |
| 24 | + y = np.linspace(-1.5, 1.5, nb_grid) |
| 25 | + z = np.linspace(-1.5, 1.5, nb_grid) |
| 26 | + X, Y, Z = np.meshgrid(x, y, z) |
| 27 | + |
| 28 | + ... |
| 29 | + |
| 30 | + return val |
| 31 | + |
| 32 | + |
| 33 | +if __name__ == '__main__': |
| 34 | + parser = argparse.ArgumentParser(description='Process some integers.') |
| 35 | + parser.add_argument('--name', '-n', type=str, default='output', help='output model name') |
| 36 | + |
| 37 | + args = parser.parse_args() |
| 38 | + name = args.name |
| 39 | + |
| 40 | + use_cuda = torch.cuda.is_available() |
| 41 | + device = torch.device("cuda" if use_cuda else "cpu") |
| 42 | + |
| 43 | + net = build_network(input_dim=3) |
| 44 | + net.to(device) |
| 45 | + net.load_state_dict(torch.load('./models/{}_model.pth'.format(name), map_location=device)) |
| 46 | + |
| 47 | + nb_grid = 128 |
| 48 | + val = compute_SDFs(net, device, nb_grid) |
| 49 | + volume = val.reshape(nb_grid, nb_grid, nb_grid) |
| 50 | + |
| 51 | + verts, faces, normals, values = measure.marching_cubes(volume, 0.0, spacing=(1.0, -1.0, 1.0), gradient_direction='ascent') |
| 52 | + |
| 53 | + import open3d as o3d |
| 54 | + |
| 55 | + mesh = o3d.geometry.TriangleMesh() |
| 56 | + |
| 57 | + mesh.vertices = o3d.utility.Vector3dVector(verts) |
| 58 | + mesh.triangles = o3d.utility.Vector3iVector(faces) |
| 59 | + mesh.triangle_normals = o3d.utility.Vector3dVector(normals) |
| 60 | + |
| 61 | + os.makedirs('output', exist_ok=True) |
| 62 | + o3d.io.write_triangle_mesh("output/{}_mesh.ply".format(name), mesh) |
0 commit comments