Example #1
0
    def create_spb(bbox, coors, degree=3, nsg=None):
        bbox = nm.array(bbox)
        coors = nm.array(coors)
        dim = coors.shape[1]
        spbox = []

        if nsg is None:
            nsg = nm.ones((dim,), dtype=nm.int)
        else:
            nsg = nm.array(nsg)

        ncpoints = 1
        spbox = {'base': [None] * dim, 'uidx': [None] * dim,
                 'cp': [None] * dim, 'ncp': [None] * dim,
                 'cp_idx': [None] * dim}
        for idim in range(dim):
            ucoors, ucoors_idx = nm.unique(coors[:,idim], return_inverse=True)
            ncp = degree + nsg[idim]
            bspl = BSpline(degree, ncp=ncp)
            bspl.make_knot_vector(knot_range=(bbox[idim,0], bbox[idim,1]))
            knots = bspl.get_knot_vector()
            cp = nm.zeros((ncp,), dtype=nm.double)
            for j in range(cp.shape[0]):
                cp[j] = nm.sum(knots[(j + 1):(j + degree + 1)]) / degree

            spbox['base'][idim] = bspl.eval_basis(t=ucoors,
                                                  return_val=True).copy()
            spbox['uidx'][idim] = ucoors_idx[:]
            spbox['cp'][idim] = cp[:]
            spbox['ncp'][idim] = ncp
            ncpoints *= ncp

        cpoints = nm.zeros((ncpoints, dim), dtype=nm.double)
        ncp = spbox['ncp']
        cp = spbox['cp']
        if dim == 2:
            idxs = nm.mgrid[0:ncp[0],0:ncp[1]]

        elif dim == 3:
            idxs = nm.mgrid[0:ncp[0],0:ncp[1],0:ncp[2]]

        aux = [1]
        for ii in range(dim - 1):
            aux.append(aux[ii] * ncp[ii])
        spbox['mul_cp_idx'] = nm.array(aux)

        for ii in range(dim):
            spbox['cp_idx'][ii] = idxs[ii].reshape(ncpoints, order='F')
            cpoints[:,ii] = cp[ii][spbox['cp_idx'][ii]]

        return spbox, cpoints
Example #2
0
    def create_spb(bbox, coors, degree=3, nsg=None):
        nc, dim = coors.shape
        inside = nm.ones((nc, ), dtype=nm.bool)
        nsg = nm.ones((dim, ), dtype=nm.int) if nsg is None else nm.array(nsg)

        for idim in range(dim):
            inrange = nm.logical_and(coors[:, idim] >= bbox[idim][0],
                                     coors[:, idim] <= bbox[idim][1])
            inside = nm.logical_and(inside, inrange)

        ncpoints = 1
        base, uidx, ncp, cp = [], [], [], []
        for idim in range(dim):
            ucoors, ucoors_idx = nm.unique(coors[inside, idim],
                                           return_inverse=True)
            ncp0 = degree + nsg[idim]
            bspl = BSpline(degree, ncp=ncp0)
            bspl.make_knot_vector(knot_range=(bbox[idim][0], bbox[idim][1]))
            knots = bspl.get_knot_vector()
            cp0 = nm.zeros((ncp0, ), dtype=nm.double)
            for j in range(cp0.shape[0]):
                cp0[j] = nm.sum(knots[(j + 1):(j + degree + 1)]) / degree

            base.append(bspl.eval_basis(t=ucoors, return_val=True))
            uidx.append(ucoors_idx)
            ncp.append(ncp0)
            cp.append(cp0)
            ncpoints *= ncp0

        cpoints = nm.zeros((ncpoints, dim), dtype=nm.double)
        cp_idx, mul_cp_idx = SplineBox.gen_cp_idxs(ncp)
        for ii in range(dim):
            cpoints[:, ii] = cp[ii][cp_idx[ii]]

        return {
            'base': base,
            'uidx': uidx,
            'ncp': ncp,
            'cp_idx': cp_idx,
            'mul_cp_idx': mul_cp_idx,
            'control_points': cpoints,
            'idxs_inside': inside
        }
Example #3
0
    def create_spb(bbox, coors, degree=3, nsg=None):
        nc, dim = coors.shape
        inside = nm.ones((nc,), dtype=nm.bool)
        nsg = nm.ones((dim,), dtype=nm.int) if nsg is None else nm.array(nsg)

        for idim in range(dim):
            inrange = nm.logical_and(coors[:,idim] >= bbox[idim][0],
                                     coors[:,idim] <= bbox[idim][1])
            inside = nm.logical_and(inside, inrange)

        ncpoints = 1
        base, uidx, ncp, cp = [], [], [], []
        for idim in range(dim):
            ucoors, ucoors_idx = nm.unique(coors[inside,idim],
                                           return_inverse=True)
            ncp0 = degree + nsg[idim]
            bspl = BSpline(degree, ncp=ncp0)
            bspl.make_knot_vector(knot_range=(bbox[idim][0], bbox[idim][1]))
            knots = bspl.get_knot_vector()
            cp0 = nm.zeros((ncp0,), dtype=nm.double)
            for j in range(cp0.shape[0]):
                cp0[j] = nm.sum(knots[(j + 1):(j + degree + 1)]) / degree

            base.append(bspl.eval_basis(t=ucoors, return_val=True))
            uidx.append(ucoors_idx)
            ncp.append(ncp0)
            cp.append(cp0)
            ncpoints *= ncp0

        cpoints = nm.zeros((ncpoints, dim), dtype=nm.double)
        cp_idx, mul_cp_idx = SplineBox.gen_cp_idxs(ncp)
        for ii in range(dim):
            cpoints[:,ii] = cp[ii][cp_idx[ii]]

        return {'base': base,
                'uidx': uidx,
                'ncp': ncp,
                'cp_idx': cp_idx,
                'mul_cp_idx': mul_cp_idx,
                'control_points': cpoints,
                'idxs_inside' : inside}