Skip to content

Commit

Permalink
add Line.find_index_at_position() which finds the index of the line v…
Browse files Browse the repository at this point in the history
…ertex that is closest to a point
  • Loading branch information
marcomusy committed Jan 11, 2024
1 parent 19e5b4d commit 86e9881
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 31 deletions.
1 change: 1 addition & 0 deletions docs/changes.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ point in the same cloud of points.
- add `mesh.cut_closed_surface()`
- fix `image.clone()` in #1011
- add `transformations.TransformInterpolator` class
- add `Line.find_index_at_position()` finds the index of the line vertex that is closest to a point


## Breaking changes
Expand Down
64 changes: 33 additions & 31 deletions vedo/assembly.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ def procrustes_alignment(sources, rigid=False):


#################################################
class Group(CommonVisual, vtk.vtkPropAssembly):
class Group(vtk.vtkPropAssembly):
"""Form groups of generic objects (not necessarily meshes)."""

def __init__(self, objects=()):
Expand Down Expand Up @@ -100,6 +100,28 @@ def __init__(self, objects=()):
self.PickableOff()


def __str__(self):
"""Print info about Group object."""
module = self.__class__.__module__
name = self.__class__.__name__
out = vedo.printc(
f"{module}.{name} at ({hex(id(self))})".ljust(75),
bold=True, invert=True, return_string=True,
)
out += "\x1b[0m"
if self.name:
out += "name".ljust(14) + ": " + self.name
if "legend" in self.info.keys() and self.info["legend"]:
out+= f", legend='{self.info['legend']}'"
out += "\n"

n = len(self.unpack())
out += "n. of objects".ljust(14) + ": " + str(n) + " "
names = [a.name for a in self.unpack() if a.name]
if names:
out += str(names).replace("'","")[:56]
return out.rstrip() + "\x1b[0m"

def __iadd__(self, obj):
"""
Add an object to the group
Expand Down Expand Up @@ -148,43 +170,23 @@ def off(self):
self.VisibilityOff()
return self

def pickable(self, value=None):
"""Set/get the pickability property of an object."""
def pickable(self, value=True):
"""The pickability property of the Group."""
if value is None:
return self.GetPickable()
self.SetPickable(value)
return self

def draggable(self, value=None):
"""Set/get the draggability property of an object."""
if value is None:
return self.GetDragable()
self.SetDragable(value)

def use_bounds(self, value=True):
"""Set the use bounds property of the Group."""
self.SetUseBounds(value)
return self

def pos(self, x=None, y=None):
"""Set/Get object 2D position on the screen."""
if x is None: # get functionality
return np.array(self.GetPosition())

if y is None: # assume x is of the form (x,y)
x, y = x
self.SetPosition(x, y)
return self

def shift(self, ds):
"""Add a shift to the current object position on the screen."""
p = np.array(self.GetPosition())
self.SetPosition(p + ds)

def print(self):
"""Print info about the object."""
print(self)
return self

def bounds(self):
"""
Get the object 2D bounds.
Returns a list in format [xmin,xmax, ymin,ymax].
"""
return self.GetBounds()


#################################################
class Assembly(CommonVisual, Actor3DHelper, vtk.vtkAssembly):
Expand Down
21 changes: 21 additions & 0 deletions vedo/shapes.py
Original file line number Diff line number Diff line change
Expand Up @@ -548,6 +548,19 @@ def eval(self, x):
v = p0 + seg * (x - w0) / (w1 - w0)
return v

def find_index_at_position(self, p):
"""
Find the index of the line vertex that is closest to the point `p`.
Note that the returned index can be fractional if `p` is not exactly
one of the vertices of the line.
"""
q = self.closest_point(p)
a, b = sorted(self.closest_point(q, n=2, return_point_id=True))
pts = self.vertices
d = np.linalg.norm(pts[a] - pts[b])
t = a + np.linalg.norm(pts[a] - q) / d
return t

def pattern(self, stipple, repeats=10):
"""
Define a stipple pattern for dashing the line.
Expand Down Expand Up @@ -1222,6 +1235,10 @@ def __init__(self, points,
![](https://user-images.githubusercontent.com/32848391/65975805-73fd6580-e46f-11e9-8957-75eddb28fa72.png)
Warning:
This class is not necessarily generating the exact number of points
as requested by `res`. Some points may be concident and removed.
See also: `Spline` and `CSpline`.
"""
if isinstance(points, Points):
Expand Down Expand Up @@ -1284,6 +1301,10 @@ def __init__(self, points, closed=False, res=None):
approximate resolution of the output line.
Default is 20 times the number of input points.
Warning:
This class is not necessarily generating the exact number of points
as requested by `res`. Some points may be concident and removed.
See also: `Spline` and `KSpline`.
"""

Expand Down
1 change: 1 addition & 0 deletions vedo/visual.py
Original file line number Diff line number Diff line change
Expand Up @@ -498,6 +498,7 @@ def __init__(self):

self.dataset = None
self.properties = self.GetProperty()
self.name = ""
self.filename = ""
self.shape = [] # for images

Expand Down

0 comments on commit 86e9881

Please sign in to comment.