14
14
from utils .tetmesh import marching_tetrahedra
15
15
16
16
@torch .no_grad ()
17
- def evaluage_alpha (points , views , gaussians , pipeline , background , kernel_size ):
17
+ def evaluage_alpha (points , views , gaussians , pipeline , background , kernel_size , return_color = False ):
18
18
final_alpha = torch .ones ((points .shape [0 ]), dtype = torch .float32 , device = "cuda" )
19
+ if return_color :
20
+ final_color = torch .ones ((points .shape [0 ], 3 ), dtype = torch .float32 , device = "cuda" )
19
21
20
22
with torch .no_grad ():
21
23
for _ , view in enumerate (tqdm (views , desc = "Rendering progress" )):
22
24
ret = integrate (points , view , gaussians , pipeline , background , kernel_size = kernel_size )
23
25
alpha_integrated = ret ["alpha_integrated" ]
26
+ if return_color :
27
+ color_integrated = ret ["color_integrated" ]
28
+ final_color = torch .where ((alpha_integrated < final_alpha ).reshape (- 1 , 1 ), color_integrated , final_color )
24
29
final_alpha = torch .min (final_alpha , alpha_integrated )
30
+
25
31
alpha = 1 - final_alpha
32
+ if return_color :
33
+ return alpha , final_color
26
34
return alpha
27
35
28
36
@torch .no_grad ()
29
- def marching_tetrahedra_with_binary_search (model_path , name , iteration , views , gaussians , pipeline , background , kernel_size ):
37
+ def marching_tetrahedra_with_binary_search (model_path , name , iteration , views , gaussians , pipeline , background , kernel_size , filter_mesh : bool , texture_mesh : bool ):
30
38
render_path = os .path .join (model_path , name , "ours_{}" .format (iteration ), "fusion" )
31
39
32
40
makedirs (render_path , exist_ok = True )
@@ -95,13 +103,19 @@ def alpha_to_sdf(alpha):
95
103
if step not in [7 ]:
96
104
continue
97
105
98
- mesh = trimesh .Trimesh (vertices = points .cpu ().numpy (), faces = faces , process = False )
106
+ if texture_mesh :
107
+ _ , color = evaluage_alpha (points , views , gaussians , pipeline , background , kernel_size , return_color = True )
108
+ vertex_colors = (color .cpu ().numpy () * 255 ).astype (np .uint8 )
109
+ else :
110
+ vertex_colors = None
111
+ mesh = trimesh .Trimesh (vertices = points .cpu ().numpy (), faces = faces , vertex_colors = vertex_colors , process = False )
99
112
100
113
# filter
101
- mask = (distance <= scale ).cpu ().numpy ()
102
- face_mask = mask [faces ].all (axis = 1 )
103
- mesh .update_vertices (mask )
104
- mesh .update_faces (face_mask )
114
+ if filter_mesh :
115
+ mask = (distance <= scale ).cpu ().numpy ()
116
+ face_mask = mask [faces ].all (axis = 1 )
117
+ mesh .update_vertices (mask )
118
+ mesh .update_faces (face_mask )
105
119
106
120
mesh .export (os .path .join (render_path , f"mesh_binary_search_{ step } .ply" ))
107
121
@@ -112,7 +126,7 @@ def alpha_to_sdf(alpha):
112
126
# mesh.export(os.path.join(render_path, f"mesh_binary_search_interp.ply"))
113
127
114
128
115
- def extract_mesh (dataset : ModelParams , iteration : int , pipeline : PipelineParams ):
129
+ def extract_mesh (dataset : ModelParams , iteration : int , pipeline : PipelineParams , filter_mesh : bool , texture_mesh : bool ):
116
130
with torch .no_grad ():
117
131
gaussians = GaussianModel (dataset .sh_degree )
118
132
scene = Scene (dataset , gaussians , load_iteration = iteration , shuffle = False )
@@ -124,7 +138,7 @@ def extract_mesh(dataset : ModelParams, iteration : int, pipeline : PipelinePara
124
138
kernel_size = dataset .kernel_size
125
139
126
140
cams = scene .getTrainCameras ()
127
- marching_tetrahedra_with_binary_search (dataset .model_path , "test" , iteration , cams , gaussians , pipeline , background , kernel_size )
141
+ marching_tetrahedra_with_binary_search (dataset .model_path , "test" , iteration , cams , gaussians , pipeline , background , kernel_size , filter_mesh , texture_mesh )
128
142
129
143
if __name__ == "__main__" :
130
144
# Set up command line argument parser
@@ -133,6 +147,8 @@ def extract_mesh(dataset : ModelParams, iteration : int, pipeline : PipelinePara
133
147
pipeline = PipelineParams (parser )
134
148
parser .add_argument ("--iteration" , default = 30000 , type = int )
135
149
parser .add_argument ("--quiet" , action = "store_true" )
150
+ parser .add_argument ("--filter_mesh" , action = "store_true" )
151
+ parser .add_argument ("--texture_mesh" , action = "store_true" )
136
152
args = get_combined_args (parser )
137
153
print ("Rendering " + args .model_path )
138
154
@@ -141,4 +157,4 @@ def extract_mesh(dataset : ModelParams, iteration : int, pipeline : PipelinePara
141
157
torch .manual_seed (0 )
142
158
torch .cuda .set_device (torch .device ("cuda:0" ))
143
159
144
- extract_mesh (model .extract (args ), args .iteration , pipeline .extract (args ))
160
+ extract_mesh (model .extract (args ), args .iteration , pipeline .extract (args ), args . filter_mesh , args . texture_mesh )
0 commit comments