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

added splines #246

Merged
merged 1 commit into from Jun 5, 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
48 changes: 48 additions & 0 deletions examples/00-load/create-spline.py
@@ -0,0 +1,48 @@
"""
.. _ref_create_spline:

Creating a Spline
~~~~~~~~~~~~~~~~~

Create a spline from numpy arrays
"""

# sphinx_gallery_thumbnail_number = 2
import pyvista as pv
import numpy as np


################################################################################
# Create a dataset to plot

# data
n_points = 100
theta = np.linspace(-4 * np.pi, 4 * np.pi, n_points)
z = np.linspace(-2, 2, n_points)
r = z**2 + 1
x = r * np.sin(theta)
y = r * np.cos(theta)
points = np.column_stack((x, y, z))

################################################################################
# Now pass the NumPy data to PyVista

# Create spline with 1000 interpolation points
spline = pv.Spline(points, 1000)

################################################################################
# Plot spline as a tube

# add scalars to spline and plot it
spline['scalars'] = np.arange(spline.n_points)
tube = spline.tube(radius=0.1)
tube.plot(smooth_shading=True)

################################################################################
# Line can also be plotted as a plain line

# generate same spline with 400 interpolation points
spline = pv.Spline(points, 400)

# plot without scalars
spline.plot(line_width=4, color='k')
42 changes: 42 additions & 0 deletions pyvista/geometric_objects.py
Expand Up @@ -619,6 +619,48 @@ def parametric_keywords(parametric_function, min_u=0, max_u=2*pi,
parametric_function.SetClockwiseOrdering(clockwise)


def Spline(points, n_points=None):
"""Create a spline from points

Parameters
----------
points : np.ndarray
Array of points to build a spline out of. Array must be 3D
and directionally ordered.

n_points : int, optional
Number of points to interpolate along the points array.

Returns
-------
spline : pyvista.PolyData
Line mesh of spline.

Examples
--------
Construct a spline
>>> import numpy as np
>>> import pyvista as pv
>>> theta = np.linspace(-4 * np.pi, 4 * np.pi, 100)
>>> z = np.linspace(-2, 2, 100)
>>> r = z**2 + 1
>>> x = r * np.sin(theta)
>>> y = r * np.cos(theta)
>>> points = np.column_stack((x, y, z))
>>> spline = pv.Spline(points, 1000)
"""
spline_function = vtk.vtkParametricSpline()
spline_function.SetPoints(pyvista.vtk_points(points, False))

# get interpolation density
u_res = n_points
if u_res is None:
u_res = points.shape[0]

u_res -= 1
return surface_from_para(spline_function, u_res)


def surface_from_para(parametric_function, u_res=100, v_res=100,
w_res=100, **kwargs):
"""Construct a mesh from a parametric function.
Expand Down
1 change: 1 addition & 0 deletions pyvista/utilities.py
Expand Up @@ -333,6 +333,7 @@ def wrap(vtkdataset):
'vtkImageData' : pyvista.UniformGrid,
'vtkStructuredPoints' : pyvista.UniformGrid,
'vtkMultiBlockDataSet' : pyvista.MultiBlock,
# 'vtkParametricSpline' : pyvista.Spline,
}
# Otherwise, we assume a VTK data object was passed
if hasattr(vtkdataset, 'GetClassName'):
Expand Down
12 changes: 12 additions & 0 deletions tests/test_geometric_objects.py
Expand Up @@ -76,3 +76,15 @@ def test_supertoroid():
def test_ellipsoid():
geom = pyvista.Ellipsoid()
assert np.any(geom.points)


def test_spline():
theta = np.linspace(-4 * np.pi, 4 * np.pi, 100)
z = np.linspace(-2, 2, 100)
r = z**2 + 1
x = r * np.sin(theta)
y = r * np.cos(theta)

points = np.column_stack((x, y, z))
spline = pyvista.Spline(points, 1000)
assert spline.n_points == 1000