Skip to content

Commit

Permalink
spline fix
Browse files Browse the repository at this point in the history
  • Loading branch information
marcomusy committed Jun 14, 2024
1 parent 34d0fed commit dba974d
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 13 deletions.
8 changes: 6 additions & 2 deletions examples/advanced/spline_draw.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
from vedo import dataurl, Image
from vedo import dataurl, Image, Mesh
from vedo.applications import SplinePlotter # ready to use class!

pic = Image(dataurl + "images/embryo.jpg")

# Works with surfaces too
# pic = Mesh(dataurl + "bunny.obj").scale(80).shift(dz=-1)
# pic.color("blue9").alpha(0.75).backface_culling()

plt = SplinePlotter(pic)
plt.show(mode="image", zoom='tightest')

if plt.line:
print("Npts =", len(plt.cpoints), "NSpline =", plt.line.npoints)
print("Npts =", len(plt.points()), "NSpline =", plt.line.npoints)


#####################################################################
Expand Down
27 changes: 17 additions & 10 deletions vedo/applications.py
Original file line number Diff line number Diff line change
Expand Up @@ -1632,7 +1632,7 @@ class SplinePlotter(Plotter):
Interactive drawing of splined curves on meshes.
"""

def __init__(self, obj, init_points=(), closed=False, splined=True, **kwargs):
def __init__(self, obj, init_points=(), closed=False, splined=True, mode="auto", **kwargs):
"""
Create an interactive application that allows the user to click points and
retrieve the coordinates of such points and optionally a spline or line
Expand All @@ -1648,12 +1648,13 @@ def __init__(self, obj, init_points=(), closed=False, splined=True, **kwargs):
Close the spline or line.
splined : (bool)
Join points with a spline or a simple line.
mode : (str)
Set the mode of interaction.
**kwargs : (dict)
keyword arguments to pass to Plotter.
"""
super().__init__(**kwargs)

self.mode = "trackball"
self.verbose = True
self.splined = splined
self.resolution = None # spline resolution (None = automatic)
Expand All @@ -1672,9 +1673,13 @@ def __init__(self, obj, init_points=(), closed=False, splined=True, **kwargs):
else:
self.object = obj

if isinstance(self.object, vedo.Image):
self.mode = "image"
self.parallel_projection(True)
self.mode = mode
if self.mode == "auto":
if isinstance(self.object, vedo.Image):
self.mode = "image"
self.parallel_projection(True)
else:
self.mode = "TrackballCamera"

t = (
"Click to add a point\n"
Expand All @@ -1691,11 +1696,12 @@ def __init__(self, obj, init_points=(), closed=False, splined=True, **kwargs):
self.callid2 = self.add_callback("LeftButtonPress", self._on_left_click)
self.callid3 = self.add_callback("RightButtonPress", self._on_right_click)


def points(self, newpts=None) -> Union["SplinePlotter", np.ndarray]:
"""Retrieve the 3D coordinates of the clicked points"""
if newpts is not None:
self.cpoints = newpts
self._update()
self.update()
return self
return np.array(self.cpoints)

Expand All @@ -1706,22 +1712,22 @@ def _on_left_click(self, evt):
# remove clicked point if clicked twice
pid = self.vpoints.closest_point(evt.picked3d, return_point_id=True)
self.cpoints.pop(pid)
self._update()
self.update()
return
p = evt.picked3d
self.cpoints.append(p)
self._update()
self.update()
if self.verbose:
vedo.colors.printc("Added point:", precision(p, 4), c="g")

def _on_right_click(self, evt):
if evt.actor and len(self.cpoints) > 0:
self.cpoints.pop() # pop removes from the list the last pt
self._update()
self.update()
if self.verbose:
vedo.colors.printc("Deleted last point", c="r")

def _update(self):
def update(self):
self.remove(self.line, self.vpoints) # remove old points and spline
self.vpoints = Points(self.cpoints).ps(self.psize).c(self.pcolor)
self.vpoints.name = "points"
Expand Down Expand Up @@ -1753,6 +1759,7 @@ def _key_press(self, evt):

def start(self) -> "SplinePlotter":
"""Start the interaction"""
self.update()
self.show(self.object, self.instructions, mode=self.mode)
return self

Expand Down
2 changes: 1 addition & 1 deletion vedo/version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
_version = '2024.5.1+dev18'
_version = '2024.5.1+dev19'

0 comments on commit dba974d

Please sign in to comment.