How to use Splipy - 10 common examples

To help you get started, we’ve selected a few Splipy examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github sintefmath / Splipy / splipy / io / g2.py View on Github external
def read_basis(self):
        ncps, order = map(int, next(self.fstream).split())
        kts = list(map(float, next(self.fstream).split()))
        return BSplineBasis(order, kts, -1)
github sintefmath / Splipy / splipy / SplineObject.py View on Github external
def make_periodic(self, continuity=None, direction=0):
        """  Make the spline object periodic in a given parametric direction.

        :param int continuity: The continuity along the boundary (default max).
        :param int direction: The direction to ensure continuity in.
        """

        direction = check_direction(direction, self.pardim)
        basis = self.bases[direction]
        if continuity is None:
            continuity = basis.order - 2
        if not -1 <= continuity <= basis.order - 2:
            raise ValueError('Illegal continuity for basis of order {}: {}'.format(
                continuity, basis.order
            ))
        if continuity == -1:
            raise ValueError(
                'Operation not supported. '
                'For discontinuous spline spaces, consider SplineObject.split().'
            )
        if basis.periodic >= 0:
            raise ValueError('Basis is already periodic')

        basis = basis.make_periodic(continuity)
github sintefmath / Splipy / examples / reuleaux.py View on Github external
# Institute: Norwegian University of Science and Technology (NTNU)
# Date:      March 2016
#

from sys import path
path.append('../')
from splipy import *
import splipy.curve_factory as curves
import splipy.surface_factory as surfaces
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
from math import pi, cos, sin

# create the three sides of the triangle, each consisting of a circle segment
c1 = curves.circle_segment(pi/3)
c2 = c1.clone().rotate(2*pi/3) + [1,0]
c3 = c1.clone().rotate(4*pi/3) + [cos(pi/3), sin(pi/3)]

# merge the three cirlces into one, and center it at the origin
c = c1.append(c2).append(c3)
c -= c.center()

# plot the reuleaux triangle
t = np.linspace(c.start(0), c.end(0), 151) # 151 parametric evaluation points
x = c(t)                                   # evaluate (x,y)-coordinates
plt.plot(x[:,0], x[:,1])
plt.axis('equal')
plt.show()

# split the triangle in two, and align this with the y-axis
two_parts = c.split((c.start(0) + c.end(0)) / 2.0)
github sintefmath / Splipy / splipy / utils / image.py View on Github external
knot[i] = (knot[i-1]+knot[i+1])/2.0

        # make C^0 at corners and C^-1 at endpoints by duplicating knots
        knot = sorted(knot + c + c + [0,n-1]) # both c and knot contains a copy of the endpoints

        # make it span [0,1] instead of [0,n-1]
        for i in range(len(knot)):
            knot[i] /= float(n-1)

        # make it periodic since these are all closed curves
        knot[0]  -= knot[-1] - knot[-5]
        knot[-1] += knot[4]  - knot[1]

        basis = BSplineBasis(4, knot, 0)

        c = curve_factory.least_square_fit(np.array(pts), basis, parpt)
        result.append(c)

    return result
github sintefmath / Splipy / splipy / io / grdecl.py View on Github external
if jrange[1] == None: jrange[1] = vol.shape[1]
        if irange[0] == None: irange[0] = 0
        if jrange[0] == None: jrange[0] = 0

        nu = np.diff(irange)
        nv = np.diff(jrange)
        nw = vol.shape[2]

        u = np.linspace(0, 1, nu)
        v = np.linspace(0, 1, nv)
        w = np.linspace(0, 1, nw)

        crvs = []
        crvs.append(curve_factory.polygon(vol[i          ,jrange[0]  , 0,:].squeeze()))
        crvs.append(curve_factory.polygon(vol[i          ,jrange[0]  ,-1,:].squeeze()))
        crvs.append(curve_factory.polygon(vol[i          ,jrange[1]-1, 0,:].squeeze()))
        crvs.append(curve_factory.polygon(vol[i          ,jrange[1]-1,-1,:].squeeze()))
        crvs.append(curve_factory.polygon(vol[irange[0]  ,j          , 0,:].squeeze()))
        crvs.append(curve_factory.polygon(vol[irange[0]  ,j          ,-1,:].squeeze()))
        crvs.append(curve_factory.polygon(vol[irange[1]-1,j          , 0,:].squeeze()))
        crvs.append(curve_factory.polygon(vol[irange[1]-1,j          ,-1,:].squeeze()))
        crvs.append(curve_factory.polygon(vol[irange[0]  ,jrange[0]  , :,:].squeeze()))
        crvs.append(curve_factory.polygon(vol[irange[0]  ,jrange[1]-1, :,:].squeeze()))
        crvs.append(curve_factory.polygon(vol[irange[1]-1,jrange[0]  , :,:].squeeze()))
        crvs.append(curve_factory.polygon(vol[irange[1]-1,jrange[1]-1, :,:].squeeze()))
#        with G2('curves.g2') as myfile:
#            myfile.write(crvs)
#        print('Written curve.g2')


        if method == 'full':
            bottom = l2_fit(vol[i,          j,          0,:].squeeze(), [b1, b2], [u, v])
github sintefmath / Splipy / examples / trefoil.py View on Github external
from splipy import *
from splipy.io import *
from numpy import pi, cos, sin
import numpy as np
import splipy.curve_factory   as curves
import splipy.surface_factory as surfaces


### create a parametric 3D-function
def trefoil(t):
    t = np.array(t)
    return np.array([sin(t) + 2*sin(2*t), cos(t) - 2*cos(2*t), -sin(3*t)]).T

### do an adaptive best cubic spline-fit of this function
path  = curves.fit(trefoil, 0, 2*pi)

### since we know it is a closed curve, enforce this on the path
path  = path.make_periodic(0,0)

### create a sweeping curve (either a circle or square)
shape = curves.circle(r=0.2)
# shape = 16*curves.n_gon(4)

### sweep *shape along *path
srf = surfaces.sweep(path, shape)

### write results to file. Use meshlab (www.meshlab.net) to view stl-files
with STL('trefoil.stl') as f:
    f.write(srf, n=(150,30))
github sintefmath / Splipy / examples / trefoil.py View on Github external
import splipy.surface_factory as surfaces


### create a parametric 3D-function
def trefoil(t):
    t = np.array(t)
    return np.array([sin(t) + 2*sin(2*t), cos(t) - 2*cos(2*t), -sin(3*t)]).T

### do an adaptive best cubic spline-fit of this function
path  = curves.fit(trefoil, 0, 2*pi)

### since we know it is a closed curve, enforce this on the path
path  = path.make_periodic(0,0)

### create a sweeping curve (either a circle or square)
shape = curves.circle(r=0.2)
# shape = 16*curves.n_gon(4)

### sweep *shape along *path
srf = surfaces.sweep(path, shape)

### write results to file. Use meshlab (www.meshlab.net) to view stl-files
with STL('trefoil.stl') as f:
    f.write(srf, n=(150,30))
github sintefmath / Splipy / splipy / io / g2.py View on Github external
def line(self):
        dim      = int(     self.read_next_non_whitespace().strip())
        start    = np.array(next(self.fstream).split(), dtype=float)
        direction= np.array(next(self.fstream).split(), dtype=float)
        finite   =          next(self.fstream).strip() != '0'
        param    = np.array(next(self.fstream).split(), dtype=float)
        reverse  =          next(self.fstream).strip() != '0'
        d = np.array(direction)
        s = np.array(start)
        # d /= np.linalg.norm(d)
        if not finite:
            param = [-state.unlimited, +state.unlimited]

        result = CurveFactory.line(s+d*param[0], s+d*param[1])
        if reverse:
            result.reverse()
        return result
github sintefmath / Splipy / splipy / io / g2.py View on Github external
def circle(self):
        dim   = int(     self.read_next_non_whitespace().strip())
        r     = float(   next(self.fstream).strip())
        center= np.array(next(self.fstream).split(), dtype=float)
        normal= np.array(next(self.fstream).split(), dtype=float)
        xaxis = np.array(next(self.fstream).split(), dtype=float)
        param = np.array(next(self.fstream).split(), dtype=float)
        reverse =        next(self.fstream).strip() != '0'

        result = CurveFactory.circle(r=r, center=center, normal=normal, xaxis=xaxis)
        result.reparam(param)
        if reverse:
            result.reverse()
        return result
github sintefmath / Splipy / splipy / io / g2.py View on Github external
dim        = int(     self.read_next_non_whitespace().strip())
        center     = np.array(next(self.fstream).split(), dtype=float)
        normal     = np.array(next(self.fstream).split(), dtype=float)
        x_axis     = np.array(next(self.fstream).split(), dtype=float)
        finite     =          next(self.fstream).strip() != '0'
        if finite:
            param_u= np.array(next(self.fstream).split(), dtype=float)
            param_v= np.array(next(self.fstream).split(), dtype=float)
        else:
            param_u= [-state.unlimited, +state.unlimited]
            param_v= [-state.unlimited, +state.unlimited]
        swap       =          next(self.fstream).strip() != '0'

        result = Surface() * [param_u[1]-param_u[0], param_v[1]-param_v[0]] + [param_u[0],param_v[0]]
        result.rotate(rotate_local_x_axis(x_axis, normal))
        result = flip_and_move_plane_geometry(result,center,normal)
        result.reparam(param_u, param_v)
        if(swap):
            result.swap()
        return result