Skip to content

Commit

Permalink
add trasparency to screenshot and others fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
marcomusy committed Dec 2, 2024
1 parent 6ffc1cd commit c1ec095
Show file tree
Hide file tree
Showing 10 changed files with 26 additions and 15 deletions.
6 changes: 3 additions & 3 deletions docs/changes.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,13 @@


## Soft-breaking Changes
Changes that will break existing code whose fixing is trivial:
Changes that may break existing code whose fixing is trivial:

- None
- in `file_io.screenshot()` add fourth channel representing trasparency @miek0tube


## Hard-breaking Changes
Changes that will break existing code and need active thinking and work to adapt
Changes that will break existing code and need active thinking and some work to adapt

- None

Expand Down
2 changes: 1 addition & 1 deletion examples/basic/cells_within_bounds.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
ids = mesh.find_cells_in_bounds(zbounds=(z1,z2))

# Print the cell IDs in green to the console
printc('IDs of cells within bounds:\n', sorted(ids), c='g')
printc('IDs of cells within bounds:\n', ids, c='g')

# Create two Plane objects at the specified z-positions
p1 = Plane(normal=(0,0,1), s=[2,2]).z(z1).alpha(0.5)
Expand Down
1 change: 1 addition & 0 deletions examples/basic/voronoi1.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
vor.cmap('Set3', "VoronoiID", on='cells').wireframe(False)

# Create a label for each cell showing its ID
vor.compute_normals() # needed for the labels to face the camera
labels = vor.labels("VoronoiID", on='cells', scale=0.01, justify='center')

# Plot the objects and close the window to continue
Expand Down
2 changes: 2 additions & 0 deletions tests/issues/issue_1109.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
cc = CellCenters(zslice).shift([-spacing[0] / 2, -spacing[1] / 2, 0])
cc.resample_data_from(zslice)

zslice.compute_normals()

zslice2 = zslice.clone()
zslice2.celldata["pixel_value"] = cc.pointdata["input_scalars"]
print(zslice2.celldata["pixel_value"])
Expand Down
2 changes: 1 addition & 1 deletion tests/pipeline.txt
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ cd ~/Projects/server/welsh_embryo_stager
python stager.py pics/E14.5_L3-03_HL2.5X.jpg
################
cd ~/Projects/oocytes
python step3.py
python step5.py
################
cd ~/Projects/umap_viewer3d
python main6.py
Expand Down
8 changes: 5 additions & 3 deletions vedo/assembly.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
from weakref import ref as weak_ref_to
from typing import List, Union, Any, Self
from typing import List, Union, Any
from typing_extensions import Self

import numpy as np

import vedo.file_io
Expand Down Expand Up @@ -331,8 +333,8 @@ def __str__(self):

n = len(self.unpack())
out += "n. of objects".ljust(14) + ": " + str(n) + " "
names = set([a.name for a in self.unpack() if a.name])
if names:
names = np.unique([a.name for a in self.unpack() if a.name])
if len(names):
out += str(names).replace("'","")[:56]
out += "\n"

Expand Down
6 changes: 1 addition & 5 deletions vedo/file_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -1881,16 +1881,12 @@ def screenshot(filename="screenshot.png", scale=1, asarray=False) -> Union["vedo
if asarray:
pd = w2if.GetOutput().GetPointData()
npdata = utils.vtk2numpy(pd.GetArray("ImageScalars"))
npdata = npdata[:, [0, 1, 2]]
# npdata = npdata[:, [0, 1, 2]] # remove alpha channel, issue #1199
ydim, xdim, _ = w2if.GetOutput().GetDimensions()
npdata = npdata.reshape([xdim, ydim, -1])
npdata = np.flip(npdata, axis=0)
return npdata ###########################

# elif settings.default_backend == "2d" and vedo.notebook_plotter:
# vedo.notebook_plotter.save(filename) # a PIL Image
# return vedo.notebook_plotter ##########

if filename.lower().endswith(".png"):
writer = vtki.new("PNGWriter")
writer.SetFileName(filename)
Expand Down
6 changes: 5 additions & 1 deletion vedo/mesh.py
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,11 @@ def cell_normals(self):
vtknormals = self.dataset.GetCellData().GetNormals()
numpy_normals = vtk2numpy(vtknormals)
if len(numpy_normals) == 0 and len(self.cells) != 0:
raise ValueError("VTK failed to return any normal vectors. You may need to call `Mesh.compute_normals()` before accessing `Mesh.cell_normals`.")
vedo.logger.warning(
"failed to return normal vectors.\n"
"You may need to call `Mesh.compute_normals()` before accessing 'Mesh.cell_normals'."
)
numpy_normals = np.zeros((self.ncells, 3)) + [0,0,1]
return numpy_normals

def compute_normals(self, points=True, cells=True, feature_angle=None, consistency=True) -> Self:
Expand Down
1 change: 1 addition & 0 deletions vedo/pyplot.py
Original file line number Diff line number Diff line change
Expand Up @@ -3683,6 +3683,7 @@ def matrix(
sbar = gr.scalarbar
labs = None
if scale != 0:
gr.compute_normals(points=False)
labs = gr.labels(
on="cells",
scale=scale / max(m, n),
Expand Down
7 changes: 6 additions & 1 deletion vedo/volume.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,12 @@ def __init__(
self.properties.SetShade(True)
self.properties.SetInterpolationType(1)
self.cmap("RdBu_r")
self.alpha([0.0, 0.2, 0.4, 0.6, 0.8, 1.0])
# make asigmoidal transfer function by default
# xvalues = np.linspace(0, 1, 11)
# sigmoid = np.clip(1/(1+np.exp(-20*(xvalues-0.5))), 0, 1)
# print("Volume: setting sigmoidal transfer function", xvalues, sigmoid)
# self.alpha(sigmoid)
self.alpha([0.0, 0.001, 0.3, 0.5, 0.7, 0.8, 1.0]) # we need to revert this..
self.alpha_gradient(None)
self.properties.SetScalarOpacityUnitDistance(1.0)

Expand Down

0 comments on commit c1ec095

Please sign in to comment.