Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix lines_from_points #399

Merged
merged 1 commit into from Oct 9, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
39 changes: 35 additions & 4 deletions pyvista/utilities/utilities.py
Expand Up @@ -241,15 +241,18 @@ def vtk_points(points, deep=True):
return vtkpts


def lines_from_points(points):
def line_segments_from_points(points):
"""
Generates line from points. Assumes points are ordered as line segments.
Generates non-connected line segments from points.
Assumes points are ordered as line segments and an even number of points
are

Parameters
----------
points : np.ndarray
Points representing line segments. For example, two line segments
would be represented as:
Points representing line segments. An even number must be given as
every two vertices represent a single line segment. For example, two
line segments would be represented as:

np.array([[0, 0, 0], [1, 0, 0], [1, 0, 0], [1, 1, 0]])

Expand All @@ -269,6 +272,8 @@ def lines_from_points(points):
>>> lines.plot() # doctest:+SKIP

"""
if len(points) % 2:
raise RuntimeError("An even number of points must be given to define each segment.")
# Assuming ordered points, create array defining line order
n_points = len(points)
n_lines = n_points // 2
Expand All @@ -281,6 +286,32 @@ def lines_from_points(points):
return poly


def lines_from_points(points):
"""Given an array of points, make a connected line set

Parameters
----------
points : np.ndarray
Points representing the vertices of the connected segments. For
example, two line segments would be represented as:

np.array([[0, 0, 0], [1, 0, 0], [1, 1, 0]])

Returns
-------
lines : pyvista.PolyData
PolyData with lines and cells.

"""
poly = pyvista.PolyData()
poly.points = points
cells = np.full((len(points)-1, 3), 2, dtype=np.int)
cells[:, 1] = np.arange(0, len(points)-1, dtype=np.int)
cells[:, 2] = np.arange(1, len(points), dtype=np.int)
poly.lines = cells
return poly


def vector_poly_data(orig, vec):
""" Creates a vtkPolyData object composed of vectors """

Expand Down
14 changes: 12 additions & 2 deletions tests/test_utilities.py
Expand Up @@ -108,11 +108,21 @@ def test_report():
assert report is not None


def test_lines_from_points():
def test_line_segments_from_points():
points = np.array([[0, 0, 0], [1, 0, 0], [1, 0, 0], [1, 1, 0]])
poly = pyvista.lines_from_points(points)
poly = pyvista.line_segments_from_points(points)
assert poly.n_cells == 2
assert poly.n_points == 4
cells = poly.lines
assert np.allclose(cells[0], [2, 0,1])
assert np.allclose(cells[1], [2, 2,3])


def test_lines_from_points():
points = np.array([[0, 0, 0], [1, 0, 0], [1, 1, 0]])
poly = pyvista.lines_from_points(points)
assert poly.n_cells == 2
assert poly.n_points == 3
cells = poly.lines
assert np.allclose(cells[0], [2, 0,1])
assert np.allclose(cells[1], [2, 1,2])