Skip to content

WIP: internal_faces option for axes3d voxels #9927

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions lib/mpl_toolkits/mplot3d/axes3d.py
Original file line number Diff line number Diff line change
Expand Up @@ -2753,6 +2753,12 @@ def voxels(self, *args, **kwargs):
- A 4D ndarray of rgb/rgba data, with the components along the
last axis.

internal_faces : boolean
By default, only exposed faces of a voxel are rendered,
but you can force the rendering of all faces of the voxels
by setting this keyword argument to True.
.. versionadded:: 2.1
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

whoops, should be 2.2, not 2.1.


**kwargs
Additional keyword arguments to pass onto
:func:`~mpl_toolkits.mplot3d.art3d.Poly3DCollection`
Expand Down Expand Up @@ -2823,6 +2829,9 @@ def _broadcast_color_arg(color, name):
edgecolors = kwargs.pop('edgecolors', None)
edgecolors = _broadcast_color_arg(edgecolors, 'edgecolors')

# include possibly occluded internal faces or not
internal_faces = kwargs.pop('internal_faces', False)

# always scale to the full array, even if the data is only in the center
self.auto_scale_xyz(x, y, z)

Expand Down Expand Up @@ -2875,9 +2884,9 @@ def permutation_matrices(n):
i1 = tuple(p1)
i2 = tuple(p2)

if filled[i1] and not filled[i2]:
if filled[i1] and (internal_faces or not filled[i2]):
voxel_faces[i1].append(p2 + square_rot)
elif not filled[i1] and filled[i2]:
elif (internal_faces or not filled[i1]) and filled[i2]:
voxel_faces[i2].append(p2 + square_rot)
Copy link
Contributor

@eric-wieser eric-wieser Dec 11, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this will fix your bug, since right now the if and else are mutually exclusive:

if internal_faces:
    voxel_faces[i1].append(p2 + square_rot)
    voxel_faces[i2].append(p2 + square_rot)
el<the existing if>


# draw upper faces
Expand Down
31 changes: 31 additions & 0 deletions lib/mpl_toolkits/tests/test_mplot3d.py
Original file line number Diff line number Diff line change
Expand Up @@ -655,6 +655,37 @@ def test_alpha(self):
assert voxels[coord], "faces returned for absent voxel"
assert isinstance(poly, art3d.Poly3DCollection)

@image_comparison(
baseline_images=['voxels-internal-faces'],
extensions=['png'],
remove_text=True
)
def test_internal_faces(self):
# Create 3 voxels (with one of them nearly invisible)
# and adjust the view so that we can see if the adjacent
# voxels are drawn or not.
fig, ax = plt.subplots(subplot_kw={"projection": "3d"})

test_data = np.array(
[[[ 0., 1],
[ 0., 2]],

[[ 0., 0.],
[ 0., 1]]])
alphas = np.divide(test_data, np.max(test_data))/2.0
colors = np.empty(test_data.shape + (4,))
colors[:, :, :] = [1, 0, 0, 1.0]
colors[:, :, :, 3] = alphas

v = ax.voxels(test_data, facecolors=colors, internal_faces=True)
ax.view_init(azim=45, elev=45)

assert type(v) is dict
assert len(v) == 3
for coord, poly in v.items():
assert isinstance(poly, art3d.Poly3DCollection)
assert len(poly.get_paths()) == 6, "voxel %s is wrong" % (coord,)

@image_comparison(
baseline_images=['voxels-xyz'],
extensions=['png'],
Expand Down