Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork7.9k
Description
Bug summary
I am trying to convert a graphics-based tetrahedral-mesh voxelizer Ipreviously wrote for MATLAB to Python.
This voxelizer converts a tetrahedral mesh to a 3-D array with each voxel carrying the index of the enclosing tetrahedron (nan
if outside of any element). This is done slice-by-slide along the z-axis. For each slice, I compute the intersections to each tetrahedron - either a triangle or quad - and plot all patches (as aPatchCollection
) in a 2-D plot and setfacecolors
so that each patch has a unique RGB value based on the colormap. Once rendered, I usefig.canvas.renderer.buffer_rgba()
to read the RGB values of the rasterized plane. Then I post-process it to map the RGB value to its original element ID.
BecausePatchCollection
by default is rendered usingantialiased=True
,. to avoid the dithering of edge colors, I have to setantialiased
to False so every voxel inside the mesh has a label.
What I found is that the output patch/element label appears to be dependent on the order of the patch being added to thePatchCollection
Code for reproduction
importnumpyasnpimportmatplotlib.pyplotaspltimportmatplotlib.cmascmfrommatplotlib.collectionsimportPatchCollectionfrommatplotlib.patchesimportPolygonnode=np.array([[0,0], [2,0], [2,1], [0,1], [1,0.5]])face=np.array([[1,2,5], [2,3,5], [3,4,5], [4,1,5]])xi=np.arange(-0.1,2.2,0.1)yi=np.arange(-0.1,1.2,0.1)mn= [np.min(xi),np.min(yi)]mx= [np.max(xi),np.max(yi)]# figure size is set so that it produces the x/y pixel counts matches xi/yifig=plt.figure(figsize=(xi.size*0.01,yi.size*0.01),dpi=100)ax=fig.add_subplot(111)ax.set_position([0,0,1,1])ax.set_xlim(mn[0],mx[0])ax.set_ylim(mn[1],mx[1])ax.set_axis_off()colors=cm.jet(np.linspace(0,1,len(face)))# draw the patches in the collectionpatches= []fori,finenumerate(face[:, :3]):polygon=Polygon(node[f-1, :2],zorder=1)patches.append(polygon)collection=PatchCollection(patches,facecolors=colors,linewidths=0,edgecolors="none",edgecolor="face",antialiased=False)ax.add_collection(collection)plt.draw()fig.canvas.draw()img=np.array(fig.canvas.renderer.buffer_rgba())plt.close(fig)mask_raw=img[:, :,0]plt.imshow(img)plt.show()mask=np.zeros(mask_raw.shape,dtype=np.int32)*np.nancolor_vals=np.floor(colors[:, :3]*255+0.5).astype(np.uint8)print(color_vals.shape)print(color_vals)foridx,cvalinenumerate(color_vals):match=np.all(img[:, :, :3]==cval,axis=-1)mask[match]=idx+1print(mask)plt.imshow(mask)plt.colorbar()plt.show()
Actual outcome
When running the above code, the rasterized image shows a bias towards the later-added triangles - triangle#4 has the largest area

however, if one changes the line
for i, f in enumerate(face[:, :3]):
to
for i, f in enumerate(np.flipud(face[:, :3])):
then the rendered result shows that the triangle at the bottom (1st triangle in the original order) has the largest area.

Expected outcome
Using the MATLAB function linked above, the output is symmetrical regardless of the order of the triangles.

Additional information
I even addedzorder=1
to thePatchCollection
, but this behavior still does not change.
is there a setting to produce an output that is not sensitive to the patch orders?
Operating system
Ubuntu
Matplotlib Version
3.10.3
Matplotlib Backend
tkagg
Python version
3.10
Jupyter version
No response
Installation
pip