Skip to content

Commit

Permalink
fix volume masking in #1146
Browse files Browse the repository at this point in the history
  • Loading branch information
marcomusy committed Jun 25, 2024
1 parent 58cdf86 commit 4d3a594
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 33 deletions.
5 changes: 5 additions & 0 deletions docs/changes.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@

## Changes and Fixes

- add `settings.force_single_precision_points = False` in #1137 by @JeffreyWardman and @sean-d-zydex
- fix Volume masking in #1146 by @ivishalanand




## Soft-breaking Changes
Changes that will break existing code whose fixing is trivial:
Expand Down
2 changes: 1 addition & 1 deletion examples/basic/sliders_range.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
"""Create a range slider to scale two spheres"""
"""Create a double range slider to scale two spheres"""
from vedo import *


Expand Down
71 changes: 39 additions & 32 deletions vedo/visual.py
Original file line number Diff line number Diff line change
Expand Up @@ -2727,7 +2727,7 @@ def hide_voxels(self, ids) -> Self:
```python
from vedo import *
embryo = Volume(dataurl+'embryo.tif')
embryo.hide_voxels(list(range(10000)))
embryo.hide_voxels(list(range(400000)))
show(embryo, axes=1).close()
```
Expand All @@ -2742,6 +2742,44 @@ def hide_voxels(self, ids) -> Self:
self.dataset.GetCellData().Modified()
return self

def mask(self, data) -> Self:
"""
Mask a volume visualization with a binary value.
Needs to specify `volume.mapper = "gpu"`.
Example:
```python
from vedo import np, Volume, show
data_matrix = np.zeros([75, 75, 75], dtype=np.uint8)
# all voxels have value zero except:
data_matrix[ 0:35, 0:35, 0:35] = 1
data_matrix[35:55, 35:55, 35:55] = 2
data_matrix[55:74, 55:74, 55:74] = 3
vol = Volume(data_matrix).cmap('Blues')
vol.mapper = "gpu"
data_mask = np.zeros_like(data_matrix)
data_mask[10:65, 10:60, 20:70] = 1
vol.mask(data_mask)
show(vol, axes=1).close()
```
See also:
`volume.hide_voxels()`
"""
rdata = data.astype(np.uint8).ravel(order="F")
varr = utils.numpy2vtk(rdata, name="input_mask")

img = vtki.vtkImageData()
img.SetDimensions(self.dimensions())
img.GetPointData().AddArray(varr)
img.GetPointData().SetActiveScalars(varr.GetName())

try:
self.mapper.SetMaskTypeToBinary()
self.mapper.SetMaskInput(img)
except AttributeError:
vedo.logger.error("volume.mask() must specify volume.mapper = 'gpu'")
return self


def mode(self, mode=None) -> Union[Self, int]:
"""
Expand Down Expand Up @@ -2819,37 +2857,6 @@ def shade(self, status=None) -> Union[Self, bool]:
self.properties.SetShade(status)
return self


def mask(self, data) -> Self:
"""
Mask a volume visualization with a binary value.
Needs to specify keyword mapper='gpu'.
Example:
```python
from vedo import np, Volume, show
data_matrix = np.zeros([75, 75, 75], dtype=np.uint8)
# all voxels have value zero except:
data_matrix[0:35, 0:35, 0:35] = 1
data_matrix[35:55, 35:55, 35:55] = 2
data_matrix[55:74, 55:74, 55:74] = 3
vol = Volume(data_matrix, c=['white','b','g','r'], mapper='gpu')
data_mask = np.zeros_like(data_matrix)
data_mask[10:65, 10:45, 20:75] = 1
vol.mask(data_mask)
show(vol, axes=1).close()
```
See also:
`volume.hide_voxels()`
"""
mask = vedo.Volume(data.astype(np.uint8))
try:
self.mapper.SetMaskTypeToBinary()
self.mapper.SetMaskInput(mask.dataset)
except AttributeError:
vedo.logger.error("volume.mask() must create the volume with Volume(..., mapper='gpu')")
return self

def interpolation(self, itype) -> Self:
"""
Set interpolation type.
Expand Down
1 change: 1 addition & 0 deletions vedo/volume.py
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,7 @@ def mapper(self, mapper):
else:
print("Error unknown mapper type", [mapper])
raise RuntimeError()
mapper.SetInputData(self.dataset)
self.actor.SetMapper(mapper)

def c(self, *args, **kwargs) -> Self:
Expand Down

0 comments on commit 4d3a594

Please sign in to comment.