19
19
##====================================================================================
20
20
21
21
22
- def m2v (* args ):
22
+ def m2v (* args , ** kwargs ):
23
23
"""
24
24
Shortcut for mesh2vol, rasterizing a tetrahedral mesh to a volume.
25
25
@@ -29,10 +29,10 @@ def m2v(*args):
29
29
Returns:
30
30
Volumetric representation of the mesh.
31
31
"""
32
- return mesh2vol (* args )
32
+ return mesh2vol (* args , ** kwargs )
33
33
34
34
35
- def mesh2vol (node , elem , xi , yi = None , zi = None ):
35
+ def mesh2vol (node , elem , xi , yi = None , zi = None , ** kwargs ):
36
36
"""
37
37
mesh2vol(node, elem, xi, yi=None, zi=None)
38
38
@@ -108,10 +108,10 @@ def mesh2vol(node, elem, xi, yi=None, zi=None):
108
108
continue
109
109
110
110
if weight is not None :
111
- maskz , weightz = mesh2mask (cutpos , facedata , xi , yi )
111
+ maskz , weightz = mesh2mask (cutpos , facedata , xi , yi , ** kwargs )
112
112
weight [:, :, :, i ] = weightz
113
113
else :
114
- maskz = mesh2mask (cutpos , facedata , xi , yi )[0 ]
114
+ maskz = mesh2mask (cutpos , facedata , xi , yi , ** kwargs )[0 ]
115
115
116
116
idx = ~ np .isnan (maskz )
117
117
if nodeval is not None :
@@ -131,7 +131,7 @@ def mesh2vol(node, elem, xi, yi=None, zi=None):
131
131
return mask , weight
132
132
133
133
134
- def mesh2mask (node , face , xi , yi = None , hf = None ):
134
+ def mesh2mask (node , face , xi , yi = None , hf = None , ** kwargs ):
135
135
"""
136
136
Fast rasterization of a 2D mesh to an image with triangle index labels.
137
137
@@ -173,9 +173,7 @@ def mesh2mask(node, face, xi, yi=None, hf=None):
173
173
)
174
174
175
175
fig = (
176
- plt .figure (
177
- figsize = (xi .size * 0.01 , yi .size * 0.01 ), dpi = 100 , layout = "compressed"
178
- )
176
+ plt .figure (figsize = (xi .size * 0.01 , yi .size * 0.01 ), dpi = 100 )
179
177
if hf is None
180
178
else hf
181
179
)
@@ -185,30 +183,28 @@ def mesh2mask(node, face, xi, yi=None, hf=None):
185
183
ax .set_ylim (mn [1 ], mx [1 ])
186
184
ax .set_axis_off ()
187
185
188
- colors = cm .gray (np .linspace (0 , 1 , len (face )))
186
+ colors = cm .jet (np .linspace (0 , 1 , len (face )))
189
187
190
188
patches = []
191
189
for i , f in enumerate (face [:, :3 ]):
192
- polygon = Polygon (
193
- node [f - 1 , :2 ],
194
- closed = True ,
195
- edgecolor = "none" ,
196
- linewidth = 0 ,
197
- linestyle = "none" ,
198
- )
190
+ polygon = Polygon (node [f - 1 , :2 ], closed = True , zorder = 1 )
199
191
patches .append (polygon )
200
192
201
193
collection = PatchCollection (
202
- patches , facecolors = colors , linewidths = 0.01 , edgecolors = "none" , edgecolor = "face"
194
+ patches ,
195
+ facecolors = colors ,
196
+ linewidths = 0 ,
197
+ edgecolor = "face" ,
198
+ antialiased = (not kwargs .get ("edge" , True )),
203
199
)
204
200
ax .add_collection (collection )
205
201
206
202
plt .draw ()
207
203
fig .canvas .draw ()
208
204
img = np .array (fig .canvas .renderer .buffer_rgba ())
209
- mask_raw = img [:, :, 0 ]
210
- mask = np .zeros (mask_raw .shape , dtype = np .int32 )
211
- color_vals = (colors [:, :3 ] * 255 ).astype (np .uint8 )
205
+
206
+ mask = np .zeros (img .shape [: 2 ] , dtype = np .int32 ) * np . nan
207
+ color_vals = np . floor (colors [:, :3 ] * 255 + 0.5 ).astype (np .uint8 )
212
208
213
209
for idx , cval in enumerate (color_vals ):
214
210
match = np .all (img [:, :, :3 ] == cval , axis = - 1 )
0 commit comments