Skip to content

Commit

Permalink
improvemnts to Browser
Browse files Browse the repository at this point in the history
  • Loading branch information
marcomusy committed Sep 14, 2023
1 parent 66c9fc6 commit 1eedc25
Show file tree
Hide file tree
Showing 7 changed files with 86 additions and 39 deletions.
3 changes: 1 addition & 2 deletions examples/other/morphomatics_tube.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,5 @@
v = SSM.space.from_coords(e)
shapes.append(vedo.Mesh([v, faces]))

plt = vedo.applications.Browser(shapes, prefix="shape ", bg2='lb')
plt += vedo.Axes(shapes[-1])
plt = vedo.applications.Browser(shapes, slider_title="shape", bg2='lb')
plt.show(viewup='z').close()
4 changes: 2 additions & 2 deletions examples/volumetric/tetralize_surface.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@

settings.use_depth_peeling = True

surf = Sphere(quads=True, res=15)
# surf = Sphere(quads=True, res=15)
# surf = TessellatedBox()
# surf = Mesh(dataurl+'290_closed.vtk')
# surf = Mesh(dataurl+'bunny.obj', c='g3').fill_holes().cap().smooth()
surf = Mesh(dataurl+'bunny.obj', c='g3').fill_holes().cap().smooth()

tmesh = surf.tetralize(side=0.015, debug=True)
#tmesh.write('mytetmesh.vtk') # save to disk!
Expand Down
75 changes: 47 additions & 28 deletions vedo/applications.py
Original file line number Diff line number Diff line change
Expand Up @@ -724,7 +724,7 @@ def __init__(
objects=(),
sliderpos=((0.50, 0.07), (0.95, 0.07)),
c=None, # slider color
prefix="",
slider_title="",
font="Calco", # slider font
axes=1,
resetcam=False, # resetcam while using the slider
Expand All @@ -733,6 +733,8 @@ def __init__(
"""
Browse a series of vedo objects by using a simple slider.
The input object can be a list of objects or a list of lists of objects.
Examples:
```python
from vedo import load, dataurl
Expand All @@ -746,61 +748,78 @@ def __init__(
- [morphomatics_tube.py](https://github.com/marcomusy/vedo/tree/master/examples/other/morphomatics_tube.py)
"""
kwargs.pop("N", 1)
kwargs.pop("shape", [])
Plotter.__init__(self, axes=axes, **kwargs)

if isinstance(objects, str):
objects = vedo.file_io.load(objects)

self += objects

if is_sequence(objects[0]):
nobs = len(objects[0])
else:
nobs = len(objects)
objects = [objects]

self.slider = None
self.timer_callback_id = None
self._oldk = None

# define the slider func ##########################
def slider_function(widget=None, event=None):

must_render = False
if isinstance(widget, vedo.plotter.Event):
if self.slider.value < len(self.actors)-1:
self.slider.value = self.slider.value + 1
else:
self.slider.value = 0
must_render = True

k = int(self.slider.value)
ak = self.actors[k]
for a in self.actors:
if a == ak:
a.on()
else:
a.off()

if k == self._oldk:
return # no change
self._oldk = k

n = len(objects)
m = len(objects[0])
for i in range(n):
for j in range(m):
ak = objects[i][j]
try:
if j == k:
ak.on()
akon = ak
else:
ak.off()
except AttributeError:
pass

try:
tx = str(k)
if slider_title:
tx = slider_title + " " + tx
elif n == 1 and akon.filename:
tx = akon.filename.split("/")[-1]
tx = tx.split("\\")[-1] # windows os
elif akon.name:
tx = ak.name + " " + tx
except:
pass
self.slider.title = tx

if resetcam:
self.reset_camera()
tx = str(k)
if ak.filename:
tx = ak.filename.split("/")[-1]
tx = tx.split("\\")[-1] # windows os
elif ak.name:
tx = ak.name

self.slider.title = prefix + tx

if must_render:
self.render()
self.render()
##################################################

self.slider_function = slider_function
self.slider = self.add_slider(
slider_function,
0.5,
len(objects) - 0.5,
nobs - 0.5,
pos=sliderpos,
font=font,
c=c,
show_value=False,
)
self.slider.GetRepresentation().SetTitleHeight(0.020)
slider_function(self.slider) # init call
slider_function() # init call

def play(self, dt=100):
"""Start playing the slides at a given speed."""
Expand Down
14 changes: 12 additions & 2 deletions vedo/file_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -542,11 +542,21 @@ def file_info(file_path):


###################################################################
def loadStructuredPoints(filename):
"""Load and return a `vtkStructuredPoints` object from file."""
def loadStructuredPoints(filename, as_points=True):
"""
Load and return a `vtkStructuredPoints` object from file.
If `as_points` is True, return a `Points` object instead of `vtkStructuredPoints`.
"""
reader = vtk.vtkStructuredPointsReader()
reader.SetFileName(filename)
reader.Update()
if as_points:
v2p = vtk.vtkImageToPoints()
v2p.SetInputData(reader.GetOutput())
v2p.Update()
pts = Points(v2p.GetOutput())
return pts
return reader.GetOutput()


Expand Down
16 changes: 13 additions & 3 deletions vedo/mesh.py
Original file line number Diff line number Diff line change
Expand Up @@ -313,10 +313,12 @@ def _repr_html_(self):
]
return "\n".join(allt)

def faces(self):
def faces(self, ids=()):
"""
Get cell polygonal connectivity ids as a python `list`.
The output format is: `[[id0 ... idn], [id0 ... idm], etc]`.
If ids is set, return only the faces of the given cells.
"""
arr1d = vtk2numpy(self._data.GetPolys().GetData())
if arr1d is None:
Expand All @@ -339,6 +341,8 @@ def faces(self):
i += arr1d[i] + 1
if i >= n:
break
if len(ids):
return conn[ids]
return conn # cannot always make a numpy array of it!

def cells(self):
Expand Down Expand Up @@ -376,8 +380,12 @@ def lines(self, flat=False):

return conn # cannot always make a numpy array of it!

def edges(self):
"""Return an array containing the edges connectivity."""
def edges(self, ids=()):
"""
Return an array containing the edges connectivity.
If ids is set, return only the edges of the given cells.
"""
extractEdges = vtk.vtkExtractEdges()
extractEdges.SetInputData(self._data)
# eed.UseAllPointsOn()
Expand All @@ -396,6 +404,8 @@ def edges(self):
i += arr1d[i] + 1
if i >= n:
break
if len(ids):
return conn[ids]
return conn # cannot always make a numpy array of it!

def texture(
Expand Down
11 changes: 10 additions & 1 deletion vedo/shapes.py
Original file line number Diff line number Diff line change
Expand Up @@ -2679,7 +2679,7 @@ def __init__(self, pos=(0, 0, 0), r=1.0, res=24, quads=False, c="r5", alpha=1.0)
_, theta, phi = utils.cart2spher(x, y, z)

pts = utils.spher2cart(np.ones_like(phi) * r, theta, phi)
self.points(pts)
self.points(pts.T)

else:
if utils.is_sequence(res):
Expand Down Expand Up @@ -4484,6 +4484,15 @@ def font(self, font):

return self

def on(self):
"""Make text visible"""
self.SetVisibility(True)
return self

def off(self):
"""Make text invisible"""
self.SetVisibility(False)
return self

class Text2D(TextBase, vtk.vtkActor2D):
"""
Expand Down
2 changes: 1 addition & 1 deletion vedo/version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
_version = '2023.4.6+dev9'
_version = '2023.4.6+dev10'

0 comments on commit 1eedc25

Please sign in to comment.