13
13
import glob
14
14
import shapeworks as sw
15
15
import AnalyzeUtils
16
- import numpy as np
16
+ import numpy as np
17
17
import subprocess
18
+
19
+
18
20
def Run_Pipeline (args ):
19
21
print ("\n Step 1. Acquire Data\n " )
20
22
"""
@@ -33,16 +35,16 @@ def Run_Pipeline(args):
33
35
sw .download_dataset (dataset_name , output_directory )
34
36
dataset_name = "ellipsoid_joint_rotation"
35
37
mesh_files = sorted (glob .glob (output_directory +
36
- dataset_name + "/meshes/*.vtk" ))[:6 ]
38
+ dataset_name + "/meshes/*.vtk" ))[:6 ]
37
39
# Else download the entire dataset
38
40
else :
39
41
dataset_name = "ellipsoid_multiple_domain_mesh"
40
42
sw .download_dataset (dataset_name , output_directory )
41
43
mesh_files = sorted (glob .glob (output_directory + "/meshes/*.vtk" ))
42
44
43
45
if args .use_subsample :
44
- inputMeshes = [sw .Mesh (filename ) for filename in mesh_files ]
45
- sample_idx = sw .data .sample_meshes (inputMeshes , int (args .num_subsample ),domains_per_shape = 2 )
46
+ inputMeshes = [sw .Mesh (filename ) for filename in mesh_files ]
47
+ sample_idx = sw .data .sample_meshes (inputMeshes , int (args .num_subsample ), domains_per_shape = 2 )
46
48
mesh_files = [mesh_files [i ] for i in sample_idx ]
47
49
48
50
print ("\n Step 2. Groom - Data Pre-processing\n " )
@@ -76,7 +78,7 @@ def Run_Pipeline(args):
76
78
mesh_names .append (mesh_name )
77
79
# get domain identifiers
78
80
domain_ids .append (mesh_name .split ("." )[0 ].split ("_" )[- 1 ])
79
-
81
+
80
82
# load mesh
81
83
mesh = sw .Mesh (mesh_file )
82
84
# do initial grooming steps
@@ -85,12 +87,11 @@ def Run_Pipeline(args):
85
87
# append to the mesh list
86
88
mesh_list .append (mesh )
87
89
88
-
89
- #domain identifiers for all shapes
90
+ # domain identifiers for all shapes
90
91
domain_ids = np .array (domain_ids )
91
- #shape index for all shapes in domain 1
92
+ # shape index for all shapes in domain 1
92
93
domain1_indx = list (np .where (domain_ids == 'd1' )[0 ])
93
- #shape index for all shapes in domain 2
94
+ # shape index for all shapes in domain 2
94
95
domain2_indx = list (np .where (domain_ids == 'd2' )[0 ])
95
96
"""
96
97
Grooming Step 2: Select a reference
@@ -99,7 +100,7 @@ def Run_Pipeline(args):
99
100
"""
100
101
domains_per_shape = 2
101
102
domain_1_meshes = []
102
- # get domain 1 shapes
103
+ # get domain 1 shapes
103
104
for i in range (int (len (mesh_list )/ domains_per_shape )):
104
105
domain_1_meshes .append (mesh_list [i * domains_per_shape ])
105
106
@@ -108,8 +109,13 @@ def Run_Pipeline(args):
108
109
domain2_reference = mesh_list [ref_index * domains_per_shape + 1 ].copy ()
109
110
domain1_ref_name = mesh_names [ref_index * domains_per_shape ]
110
111
domain2_ref_name = mesh_names [ref_index * domains_per_shape + 1 ]
111
- reference = [domain1_reference ,domain2_reference ]
112
- ref_name = [domain1_ref_name ,domain2_ref_name ]
112
+ reference = [domain1_reference , domain2_reference ]
113
+ ref_name = [domain1_ref_name , domain2_ref_name ]
114
+
115
+ # Create a combined mesh for the global alignment
116
+ combined_reference = domain1_reference .copy ()
117
+ combined_reference += domain2_reference
118
+
113
119
"""
114
120
Grooming Step 3: Rigid alignment
115
121
Now we can loop over all of the meshes again to find the rigid
@@ -118,26 +124,35 @@ def Run_Pipeline(args):
118
124
119
125
transforms = []
120
126
for i in range (len (domain_1_meshes )):
121
-
122
- # calculate the transformation
127
+
128
+ # calculate the transformation
123
129
for d in range (domains_per_shape ):
124
130
# compute rigid transformation
125
- rigidTransform = mesh_list [i * domains_per_shape + d ].createTransform (reference [d ],sw .Mesh .AlignmentType .Rigid ,100 )
131
+ rigidTransform = mesh_list [i * domains_per_shape +
132
+ d ].createTransform (reference [d ], sw .Mesh .AlignmentType .Rigid , 100 )
126
133
name = mesh_names [i * domains_per_shape + d ]
127
- print ('Aligning ' + name + ' to ' + ref_name [d ])
134
+ print ('Aligning ' + name + ' to ' + ref_name [d ])
128
135
transforms .append (rigidTransform )
129
136
137
+ combined_mesh = mesh_list [i * domains_per_shape ].copy ()
138
+ for d in range (domains_per_shape ):
139
+ # skip the first domain
140
+ if d == 0 :
141
+ continue
142
+ combined_mesh += mesh_list [i * domains_per_shape + d ]
143
+ transform = combined_mesh .createTransform (combined_reference , sw .Mesh .AlignmentType .Rigid , 100 )
144
+ transforms .append (transform )
145
+
130
146
# Save groomed meshes
131
147
groomed_mesh_files = sw .utils .save_meshes (groom_dir + 'meshes/' , mesh_list , mesh_names , extension = 'vtk' )
132
148
133
-
134
149
print ("\n Step 3. Optimize - Particle Based Optimization\n " )
135
150
"""
136
151
Step 3: OPTIMIZE - Particle Based Optimization
137
152
138
153
Now we can run optimization directly on the meshes.
139
154
For more details on the plethora of parameters for shapeworks please refer
140
- to docs/workflow/optimze .md
155
+ to docs/workflow/optimize .md
141
156
http://sciinstitute.github.io/ShapeWorks/workflow/optimize.html
142
157
"""
143
158
@@ -147,17 +162,21 @@ def Run_Pipeline(args):
147
162
os .makedirs (project_location )
148
163
# Set subjects
149
164
subjects = []
150
-
165
+
151
166
for i in range (len (domain_1_meshes )):
152
167
subject = sw .Subject ()
153
168
subject .set_number_of_domains (domains_per_shape )
154
169
rel_mesh_files = []
155
170
rel_groom_files = []
156
171
transform = []
157
172
for d in range (domains_per_shape ):
158
- rel_mesh_files += sw .utils .get_relative_paths ([os .getcwd () + '/' + mesh_files [i * domains_per_shape + d ]], project_location )
159
- rel_groom_files += sw .utils .get_relative_paths ([os .getcwd () + '/' + groomed_mesh_files [i * domains_per_shape + d ]], project_location )
160
- transform .append (transforms [i * domains_per_shape + d ].flatten ())
173
+ rel_mesh_files += sw .utils .get_relative_paths ([os .getcwd () +
174
+ '/' + mesh_files [i * domains_per_shape + d ]], project_location )
175
+ rel_groom_files += sw .utils .get_relative_paths ([os .getcwd () + '/' +
176
+ groomed_mesh_files [i * domains_per_shape + d ]], project_location )
177
+ transform .append (transforms [i * (domains_per_shape + 1 )+ d ].flatten ())
178
+ # add the global alignment transform
179
+ transform .append (transforms [i * (domains_per_shape + 1 )+ domains_per_shape ].flatten ())
161
180
subject .set_groomed_transforms (transform )
162
181
subject .set_groomed_filenames (rel_groom_files )
163
182
subject .set_original_filenames (rel_mesh_files )
@@ -168,36 +187,36 @@ def Run_Pipeline(args):
168
187
parameters = sw .Parameters ()
169
188
170
189
parameter_dictionary = {
171
- "checkpointing_interval" : 200 ,
172
- "keep_checkpoints" : 0 ,
173
- "iterations_per_split" : 200 ,
174
- "optimization_iterations" : 200 ,
175
- "starting_regularization" : 1000 ,
176
- "ending_regularization" : 0.1 ,
177
- "relative_weighting" : 10 ,
178
- "initial_relative_weighting" : 0.1 ,
179
- "procrustes_interval" : 0 ,
180
- "procrustes_scaling" : 0 ,
181
- "save_init_splits" : 0 ,
182
- "verbosity" : 0
183
-
184
- }
185
- num_particles = [128 ,128 ]
190
+ "checkpointing_interval" : 200 ,
191
+ "keep_checkpoints" : 0 ,
192
+ "iterations_per_split" : 200 ,
193
+ "optimization_iterations" : 200 ,
194
+ "starting_regularization" : 1000 ,
195
+ "ending_regularization" : 0.1 ,
196
+ "relative_weighting" : 10 ,
197
+ "initial_relative_weighting" : 0.1 ,
198
+ "procrustes_interval" : 0 ,
199
+ "procrustes_scaling" : 0 ,
200
+ "save_init_splits" : 0 ,
201
+ "verbosity" : 0
202
+
203
+ }
204
+ num_particles = [128 , 128 ]
186
205
187
206
# If running a tiny test, reduce some parameters
188
207
if args .tiny_test :
189
- num_particles = [32 ,32 ]
208
+ num_particles = [32 , 32 ]
190
209
parameter_dictionary ["optimization_iterations" ] = 30
191
210
192
- #setting the argument to singlescale for the output filename
211
+ # setting the argument to singlescale for the output filename
193
212
args .use_single_scale = True
194
- args .option_set = args .option_set .replace ("multiscale" ,"singlescale" )
213
+ args .option_set = args .option_set .replace ("multiscale" , "singlescale" )
195
214
# Add param dictionary to spreadsheet
196
215
for key in parameter_dictionary :
197
216
parameters .set (key , sw .Variant ([parameter_dictionary [key ]]))
198
- parameters .set ("number_of_particles" , sw .Variant (num_particles ))
217
+ parameters .set ("number_of_particles" , sw .Variant (num_particles ))
199
218
project .set_parameters ("optimize" , parameters )
200
-
219
+
201
220
spreadsheet_file = output_directory + "ellipsoid_multiple_domain_mesh_" + args .option_set + ".swproj"
202
221
project .save (spreadsheet_file )
203
222
0 commit comments