-
Notifications
You must be signed in to change notification settings - Fork 529
/
meshfix.py
234 lines (201 loc) · 8.37 KB
/
meshfix.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
# -*- coding: utf-8 -*-
# emacs: -*- mode: python; py-indent-offset: 4; indent-tabs-mode: nil -*-
# vi: set ft=python sts=4 ts=4 sw=4 et:
"""MeshFix corrects topological errors in polygonal meshes."""
import os.path as op
from ..utils.filemanip import split_filename
from .base import (
CommandLine,
CommandLineInputSpec,
traits,
TraitedSpec,
isdefined,
File,
)
class MeshFixInputSpec(CommandLineInputSpec):
number_of_biggest_shells = traits.Int(
argstr="--shells %d", desc="Only the N biggest shells are kept"
)
epsilon_angle = traits.Range(
argstr="-a %f",
low=0.0,
high=2.0,
desc="Epsilon angle in degrees (must be between 0 and 2)",
)
join_overlapping_largest_components = traits.Bool(
argstr="-j",
xor=["join_closest_components"],
desc="Join 2 biggest components if they overlap, remove the rest.",
)
join_closest_components = traits.Bool(
argstr="-jc",
xor=["join_closest_components"],
desc="Join the closest pair of components.",
)
quiet_mode = traits.Bool(
argstr="-q", desc="Quiet mode, don't write much to stdout."
)
dont_clean = traits.Bool(argstr="--no-clean", desc="Don't Clean")
save_as_stl = traits.Bool(
xor=["save_as_vrml", "save_as_freesurfer_mesh"],
argstr="--stl",
desc="Result is saved in stereolithographic format (.stl)",
)
save_as_vrml = traits.Bool(
argstr="--wrl",
xor=["save_as_stl", "save_as_freesurfer_mesh"],
desc="Result is saved in VRML1.0 format (.wrl)",
)
save_as_freesurfer_mesh = traits.Bool(
argstr="--fsmesh",
xor=["save_as_vrml", "save_as_stl"],
desc="Result is saved in freesurfer mesh format",
)
remove_handles = traits.Bool(argstr="--remove-handles", desc="Remove handles")
uniform_remeshing_steps = traits.Int(
argstr="-u %d",
requires=["uniform_remeshing_vertices"],
desc="Number of steps for uniform remeshing of the whole mesh",
)
uniform_remeshing_vertices = traits.Int(
argstr="--vertices %d",
requires=["uniform_remeshing_steps"],
desc="Constrains the number of vertices."
"Must be used with uniform_remeshing_steps",
)
laplacian_smoothing_steps = traits.Int(
argstr="--smooth %d", desc="The number of laplacian smoothing steps to apply"
)
x_shift = traits.Int(
argstr="--smooth %d",
desc="Shifts the coordinates of the vertices when saving. Output must be in FreeSurfer format",
)
# Cutting, decoupling, dilation
cut_outer = traits.Int(
argstr="--cut-outer %d",
desc="Remove triangles of 1st that are outside of the 2nd shell.",
)
cut_inner = traits.Int(
argstr="--cut-inner %d",
desc="Remove triangles of 1st that are inside of the 2nd shell. Dilate 2nd by N; Fill holes and keep only 1st afterwards.",
)
decouple_inin = traits.Int(
argstr="--decouple-inin %d",
desc="Treat 1st file as inner, 2nd file as outer component."
"Resolve overlaps by moving inners triangles inwards. Constrain the min distance between the components > d.",
)
decouple_outin = traits.Int(
argstr="--decouple-outin %d",
desc="Treat 1st file as outer, 2nd file as inner component."
"Resolve overlaps by moving outers triangles inwards. Constrain the min distance between the components > d.",
)
decouple_outout = traits.Int(
argstr="--decouple-outout %d",
desc="Treat 1st file as outer, 2nd file as inner component."
"Resolve overlaps by moving outers triangles outwards. Constrain the min distance between the components > d.",
)
finetuning_inwards = traits.Bool(
argstr="--fineTuneIn ",
requires=["finetuning_distance", "finetuning_substeps"],
position=-3,
desc="Used to fine-tune the minimal distance between surfaces.",
)
finetuning_outwards = traits.Bool(
argstr="--fineTuneOut ",
requires=["finetuning_distance", "finetuning_substeps"],
position=-3,
xor=["finetuning_inwards"],
desc="Similar to finetuning_inwards, but ensures minimal distance in the other direction",
)
finetuning_distance = traits.Float(
argstr="%f",
requires=["finetuning_substeps"],
position=-2,
desc="Used to fine-tune the minimal distance between surfaces."
"A minimal distance d is ensured, and reached in n substeps. When using the surfaces for subsequent volume meshing by gmsh, this step prevent too flat tetrahedra2)",
)
finetuning_substeps = traits.Int(
argstr="%d",
requires=["finetuning_distance"],
position=-1,
desc="Used to fine-tune the minimal distance between surfaces."
"A minimal distance d is ensured, and reached in n substeps. When using the surfaces for subsequent volume meshing by gmsh, this step prevent too flat tetrahedra2)",
)
dilation = traits.Int(
argstr="--dilate %d", desc="Dilate the surface by d. d < 0 means shrinking."
)
set_intersections_to_one = traits.Bool(
argstr="--intersect",
desc="If the mesh contains intersections, return value = 1."
"If saved in gmsh format, intersections will be highlighted.",
)
in_file1 = File(exists=True, argstr="%s", position=1, mandatory=True)
in_file2 = File(exists=True, argstr="%s", position=2)
output_type = traits.Enum(
"off",
["stl", "msh", "wrl", "vrml", "fs", "off"],
usedefault=True,
desc="The output type to save the file as.",
)
out_filename = File(
genfile=True, argstr="-o %s", desc="The output filename for the fixed mesh file"
)
class MeshFixOutputSpec(TraitedSpec):
mesh_file = File(exists=True, desc="The output mesh file")
class MeshFix(CommandLine):
"""
MeshFix v1.2-alpha - by Marco Attene, Mirko Windhoff, Axel Thielscher.
.. seealso::
http://jmeshlib.sourceforge.net
Sourceforge page
http://simnibs.de/installation/meshfixandgetfem
Ubuntu installation instructions
If MeshFix is used for research purposes, please cite the following paper:
M. Attene - A lightweight approach to repairing digitized polygon meshes.
The Visual Computer, 2010. (c) Springer.
Accepted input formats are OFF, PLY and STL.
Other formats (like .msh for gmsh) are supported only partially.
Example
-------
>>> import nipype.interfaces.meshfix as mf
>>> fix = mf.MeshFix()
>>> fix.inputs.in_file1 = 'lh-pial.stl'
>>> fix.inputs.in_file2 = 'rh-pial.stl'
>>> fix.run() # doctest: +SKIP
>>> fix.cmdline
'meshfix lh-pial.stl rh-pial.stl -o lh-pial_fixed.off'
"""
_cmd = "meshfix"
input_spec = MeshFixInputSpec
output_spec = MeshFixOutputSpec
def _list_outputs(self):
outputs = self.output_spec().get()
if isdefined(self.inputs.out_filename):
path, name, ext = split_filename(self.inputs.out_filename)
ext = ext.replace(".", "")
out_types = ["stl", "msh", "wrl", "vrml", "fs", "off"]
# Make sure that the output filename uses one of the possible file types
if any(ext == out_type.lower() for out_type in out_types):
outputs["mesh_file"] = op.abspath(self.inputs.out_filename)
else:
outputs["mesh_file"] = op.abspath(name + "." + self.inputs.output_type)
else:
outputs["mesh_file"] = op.abspath(self._gen_outfilename())
return outputs
def _gen_filename(self, name):
if name == "out_filename":
return self._gen_outfilename()
else:
return None
def _gen_outfilename(self):
_, name, _ = split_filename(self.inputs.in_file1)
if self.inputs.save_as_freesurfer_mesh or self.inputs.output_type == "fs":
self.inputs.output_type = "fs"
self.inputs.save_as_freesurfer_mesh = True
if self.inputs.save_as_stl or self.inputs.output_type == "stl":
self.inputs.output_type = "stl"
self.inputs.save_as_stl = True
if self.inputs.save_as_vrml or self.inputs.output_type == "vrml":
self.inputs.output_type = "vrml"
self.inputs.save_as_vrml = True
return name + "_fixed." + self.inputs.output_type