Skip to content

Commit

Permalink
Fix import issues and restore backwards compatibility (#25)
Browse files Browse the repository at this point in the history
  • Loading branch information
ewu63 authored May 21, 2021
1 parent 609bf7b commit 230f55a
Show file tree
Hide file tree
Showing 9 changed files with 181 additions and 164 deletions.
10 changes: 5 additions & 5 deletions examples/example_curve.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import numpy as np

# First party modules
from pyspline import pySpline
from pyspline import Curve

# Get some Helix-like data

Expand All @@ -15,29 +15,29 @@
y = np.sin(theta)
z = np.linspace(0, 1, n)
print("Helix Data")
curve = pySpline.Curve(x=x, y=y, z=z, k=4, Nctl=16, niter=100)
curve = Curve(x=x, y=y, z=z, k=4, Nctl=16, niter=100)
curve.writeTecplot("helix.dat")

# Load naca0012 data
print("Naca 0012 data")
x, y = np.loadtxt("naca0012", unpack=True)
curve = pySpline.Curve(x=x, y=y, k=4, Nctl=11, niter=500)
curve = Curve(x=x, y=y, k=4, Nctl=11, niter=500)
curve.writeTecplot("naca_data.dat")

# Projection Tests
print("Projection Tests")
x = [0, 2, 3, 5]
y = [-2, 5, 3, 0]
z = [0, 0, 0, 0]
curve1 = pySpline.Curve(x=x, y=y, z=z, k=4)
curve1 = Curve(x=x, y=y, z=z, k=4)

curve1.writeTecplot("curve1.dat")

x = [-2, 5, 2, 1]
y = [5, 1, 4, 2]
z = [3, 0, 1, 4]

curve2 = pySpline.Curve(x=x, y=y, z=z, k=4)
curve2 = Curve(x=x, y=y, z=z, k=4)
curve2.writeTecplot("curve2.dat")

# Get the minimum distance distance between a point and each curve
Expand Down
6 changes: 3 additions & 3 deletions examples/example_surf.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import numpy as np

# First party modules
from pyspline import pySpline
from pyspline import Curve, Surface

# Create a generic surface
nu = 20
Expand All @@ -13,15 +13,15 @@
v = np.linspace(0, 4, nv)
[V, U] = np.meshgrid(v, u)
Z = np.cos(U) * np.sin(V)
surf = pySpline.Surface(x=U, y=V, z=Z, ku=4, kv=4, Nctlu=5, Nctlv=5)
surf = Surface(x=U, y=V, z=Z, ku=4, kv=4, Nctlu=5, Nctlv=5)
surf.writeTecplot("surface.dat")

n = 100
theta = np.linspace(0.0000, 2 * np.pi, n)
x = np.cos(theta) - 1
y = np.sin(theta) + 1
z = np.linspace(0, 1, n) + 2
curve = pySpline.Curve(x=x, y=y, z=z, k=4, Nctl=16, niter=100)
curve = Curve(x=x, y=y, z=z, k=4, Nctl=16, niter=100)
curve.writeTecplot("helix.dat")

u, v, s, D = surf.projectCurve(curve, Niter=100, eps1=1e-10, eps2=1e-10, u=1, v=1, s=1)
Expand Down
4 changes: 2 additions & 2 deletions examples/example_volume.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import numpy as np

# First party modules
from pyspline import pySpline
from pyspline import Volume

X = np.zeros((2, 2, 2, 3))
X[0, 0, 0, :] = [0, 0, 0]
Expand All @@ -19,7 +19,7 @@
X[1, 1, 1, :] = [1.2, 1.0, 2]
X[0, 1, 1, :] = [-0.2, 1.3, 2.1]

vol = pySpline.Volume(X=X, ku=2, kv=2, kw=2, Nctlu=2, Nctlv=2, Nctlw=2)
vol = Volume(X=X, ku=2, kv=2, kw=2, Nctlu=2, Nctlv=2, Nctlw=2)
vol.writeTecplot("vol.dat", orig=True)

# Generate random data
Expand Down
8 changes: 4 additions & 4 deletions pyspline/pyCurve.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,13 +76,13 @@ class Curve(object):
>>> y = [0, 0.25, 1.0]
>>> s = [0., 0.5, 1.0]
>>> # Spatial interpolated seg
>>> line_seg = pySpline.Curve(x=x, y=y, k=2)
>>> line_seg = Curve(x=x, y=y, k=2)
>>> # With explicit parameter values
>>> line_seg = pySpline.Curve(x=x, y=y, k=2, s=s)
>>> line_seg = Curve(x=x, y=y, k=2, s=s)
>>> #LMS parabolic curve
>>> parabola = pySpline.Curve(x=x, y=y, k=3)
>>> parabola = Curve(x=x, y=y, k=3)
>>> #LMS parabolic curve with parameter values
>>> parabola = pySpline.Curve(x=x, y=y, k=3, s=s)
>>> parabola = Curve(x=x, y=y, k=3, s=s)
"""

def __init__(self, **kwargs):
Expand Down
151 changes: 14 additions & 137 deletions pyspline/pySpline.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,142 +6,19 @@
:class:`Volume`
"""

# External modules
import numpy as np

# Local modules
from . import libspline # noqa: F401
from .pyCurve import Curve
from .pySurface import Surface
from .pyVolume import Volume
from .utils import Error

# ----------------------------------------------------------------------
# Misc Helper Functions
# ----------------------------------------------------------------------


def trilinearVolume(*args):
"""This is a short-cut function to create a trilinear b-spline
volume. It can be created with ``x`` **OR** with ``xmin`` and
``xmax``.
Parameters
----------
x : array of size (2, 2, 2, 3)
Coordinates of the corners of the box.
xmin : array of size (3)
The extreme lower corner of the box
xmax : array of size (3)
The extreme upper corner of the box. In this case, by
construction, the box will be coordinate axis aligned.
"""
tu = [0, 0, 1, 1]
tv = [0, 0, 1, 1]
tw = [0, 0, 1, 1]
ku = 2
kv = 2
kw = 2

if len(args) == 1:
return Volume(coef=args[0], tu=tu, tv=tv, tw=tw, ku=ku, kv=kv, kw=kw)
elif len(args) == 2:
xmin = np.array(args[0]).astype("d")
xmax = np.array(args[1]).astype("d")

xLow = xmin[0]
xHigh = xmax[0]
yLow = xmin[1]
yHigh = xmax[1]
zLow = xmin[2]
zHigh = xmax[2]

coef = np.zeros((2, 2, 2, 3))
coef[0, 0, 0, :] = [xLow, yLow, zLow]
coef[1, 0, 0, :] = [xHigh, yLow, zLow]
coef[0, 1, 0, :] = [xLow, yHigh, zLow]
coef[1, 1, 0, :] = [xHigh, yHigh, zLow]
coef[0, 0, 1, :] = [xLow, yLow, zHigh]
coef[1, 0, 1, :] = [xHigh, yLow, zHigh]
coef[0, 1, 1, :] = [xLow, yHigh, zHigh]
coef[1, 1, 1, :] = [xHigh, yHigh, zHigh]

return Volume(coef=coef, tu=tu, tv=tv, tw=tw, ku=ku, kv=kv, kw=kw)
else:
raise Error("An unknown number of arguments was passed to trilinear Volume")


def bilinearSurface(*args):
"""This is short-cut function to create a bilinear surface.
Args can contain:
1. ``x`` array of size(4, 3). The four corners of the array
arranged in the coordinate direction orientation::
2 3
+----------+
| |
| |
| |
+----------+
0 1
2. ``p1``, ``p2``, ``p3``, ``p4``. Individual corner points in CCW Ordering::
3 2
+----------+
| |
| |
| |
+----------+
0 1
"""
if len(args) == 1:
# One argument passed in ... assume its X
if len(args[0]) != 4:
raise Error("A single argument passed to bilinear surface must contain 4 points and be of size (4, 3)")
coef = np.zeros((2, 2, 3))
coef[0, 0] = args[0][0]
coef[1, 0] = args[0][1]
coef[0, 1] = args[0][2]
coef[1, 1] = args[0][3]
return Surface(coef=coef, tu=[0, 0, 1, 1], tv=[0, 0, 1, 1], ku=2, kv=2)
else:
# Assume 4 arguments
coef = np.zeros([2, 2, 3])
coef[0, 0] = args[0]
coef[1, 0] = args[1]
coef[0, 1] = args[3]
coef[1, 1] = args[2]
return Surface(coef=coef, tu=[0, 0, 1, 1], tv=[0, 0, 1, 1], ku=2, kv=2)


def line(*args, **kwargs):
"""This is a short cut function to create a line as a pySpline Curve object.
Args can contain: (pick one)
1. ``X`` array of size(2, ndim). The two end points
2. ``x1``, ``x2`` The two end points (each of size ndim)
3. ``x```, dir=direction. A point and a displacement vector
4. ``x1``, dir=direction, length=length. As 3. but with a specific length
"""

if len(args) == 2:
# Its a two-point type
return Curve(coef=[args[0], args[1]], k=2, t=[0, 0, 1, 1])
elif len(args) == 1:
if len(args[0]) == 2: # its X
return Curve(coef=args[0], k=2, t=[0, 0, 1, 1])
elif "dir" in kwargs:
# We have point and direction
if "length" in kwargs:
x2 = args[0] + kwargs["dir"] / np.linalg.norm(kwargs["dir"]) * kwargs["length"]
else:
x2 = args[0] + kwargs["dir"]

return Curve(coef=[args[0], x2], k=2, t=[0, 0, 1, 1])
else:
Error("Error: dir must be specified if only 1 argument is given")
from .pyCurve import Curve # noqa: F401
from .pySurface import Surface # noqa: F401
from .pyVolume import Volume # noqa: F401
from .utils import ( # noqa: F401
bilinearSurface,
checkInput,
closeTecplot,
line,
openTecplot,
trilinearVolume,
writeTecplot1D,
writeTecplot2D,
writeTecplot3D,
)
Loading

0 comments on commit 230f55a

Please sign in to comment.