예제 #1
0
def test_bspline_surface_loadsave():
    surf_save = BSpline.Surface()
    surf_save.degree_u = S_DEGREE_U
    surf_save.degree_v = S_DEGREE_V
    surf_save.ctrlpts_size_u = 3
    surf_save.ctrlpts_size_v = 3
    surf_save.ctrlpts = S_CTRLPTS
    surf_save.knotvector_u = S_KV_U
    surf_save.knotvector_v = S_KV_V
    surf_save.save(FILE_NAME)

    surf_load = BSpline.Surface()
    surf_load.load(FILE_NAME)

    # Remove save file
    os.remove(FILE_NAME)

    assert surf_save.degree_u == surf_load.degree_u
    assert surf_save.degree_v == surf_load.degree_v
    assert surf_save.knotvector_u == surf_load.knotvector_u
    assert surf_save.knotvector_v == surf_load.knotvector_v
    assert surf_save.ctrlpts == surf_load.ctrlpts
    assert surf_save.ctrlpts_size_u == surf_load.ctrlpts_size_u
    assert surf_save.ctrlpts_size_v == surf_load.ctrlpts_size_v
    assert surf_save.dimension == surf_load.dimension
예제 #2
0
def spline_curve_kv_norm2():
    """ Creates a spline Curve without knot vector normalization """
    curve = BSpline.Curve(normalize_kv=False)
    curve.degree = 3
    curve.ctrlpts = [[5.0, 5.0], [10.0, 10.0], [20.0, 15.0], [35.0, 15.0], [45.0, 10.0], [50.0, 5.0]]
    curve.knotvector = [0.0, 0.0, 0.0, 0.0, 1.0, 2.0, 3.0, 3.0, 3.0, 3.0]
    return curve
예제 #3
0
파일: nurbs.py 프로젝트: Moult/sverchok
    def build_geomdl(cls,
                     degree,
                     knotvector,
                     control_points,
                     weights=None,
                     normalize_knots=False):
        if weights is not None:
            curve = NURBS.Curve(normalize_kv=normalize_knots)
        else:
            curve = BSpline.Curve(normalize_kv=normalize_knots)
        curve.degree = degree
        if isinstance(control_points, np.ndarray):
            control_points = control_points.tolist()
        curve.ctrlpts = control_points
        if weights is not None:
            if isinstance(weights, np.ndarray):
                weights = weights.tolist()
            curve.weights = weights
        if isinstance(knotvector, np.ndarray):
            knotvector = knotvector.tolist()
        curve.knotvector = knotvector

        result = SvGeomdlCurve(curve)
        result.u_bounds = curve.knotvector[0], curve.knotvector[-1]
        return result
def bspline_curve3d():
    """ Creates a B-Spline 3-dimensional curve instance """
    curve = BSpline.Curve()
    curve.degree = 2
    curve.ctrlpts = [[1, 1, 0], [2, 1, -1], [2, 2, 0]]
    curve.knotvector = [0, 0, 0, 1, 1, 1]
    return curve
예제 #5
0
def gen_kernel(num_spline_dots=6, curve_degree=5, kernel_size=(19, 19)):
    curve = BSpline.Curve()
    knot_dots = []

    knot_dots = []
    small_kernel_size = (int(np.round(kernel_size[0] / 1.5)),
                         int(np.round(kernel_size[1] / 1.5)))
    for i in range(num_spline_dots):
        knot_dots += [[
            np.random.randint(0, small_kernel_size[0]),
            np.random.randint(0, small_kernel_size[1])
        ]]

    curve.degree = curve_degree
    curve.ctrlpts = knot_dots
    curve.knotvector = generate_knot_vector(curve.degree, len(curve.ctrlpts))
    curve.delta = 0.005
    curve.evaluate()
    pts = np.array(curve.evalpts)
    pts[:, 0] -= (pts[:, 0].min() - 2)
    pts[:, 1] -= (pts[:, 1].min() - 2)
    k_dim = int(np.ceil(pts.max())) + 2

    k = kern_from_grid(pts, k_dim)
    cm_k = np.round(calculate_center_mass(k), 0).astype(int)
    dim_k_c = 2 * max(list(np.array(k.shape) - cm_k) + list(cm_k))
    if dim_k_c % 2 == 0: dim_k_c += 1

    k = expand(k, (dim_k_c, dim_k_c))
    k /= np.sum(k)
    return k
예제 #6
0
 def dowork(self,ch, method, properties, body):
     data=json.loads(body)
     points=data['points']
     RADIUS=data['RADIUS']
     WIDTH=data['WIDTH']
     HEIGHT=data['HEIGHT']
     SPREAD=data['SPREAD']
     curve = BSpline.Curve()
     curve.degree = 3
     curve.delta = 0.005
     lpoints = points
     curve.ctrlpts = lpoints
     curve.knotvector = knotvector.generate(3, len(curve.ctrlpts))
     curve_points = curve.evalpts
     temppixels = np.full((WIDTH, HEIGHT), 0.0)
     sl=SpreadLines(temppixels,255) 
     sl.drawline(curve_points)
     print(sl.img_data.max())
     data={}
     data['WIDTH']=WIDTH
     data['HEIGHT']=HEIGHT
     data['pixels']=temppixels.tolist()
     message=json.dumps(data)
     self.channel.basic_publish(exchange='',routing_key='feronia_result',body=message)
     ch.basic_ack(delivery_tag = method.delivery_tag)
예제 #7
0
def spline_surf():
    """ Creates a B-spline surface instance """
    # Create a surface instance
    surf = BSpline.Surface()

    # Set degrees
    surf.degree_u = 3
    surf.degree_v = 3

    ctrlpts = [[-25.0, -25.0, -10.0], [-25.0, -15.0, -5.0], [-25.0, -5.0, 0.0],
               [-25.0, 5.0, 0.0], [-25.0, 15.0, -5.0], [-25.0, 25.0, -10.0],
               [-15.0, -25.0, -8.0], [-15.0, -15.0, -4.0], [-15.0, -5.0, -4.0],
               [-15.0, 5.0, -4.0], [-15.0, 15.0, -4.0], [-15.0, 25.0, -8.0],
               [-5.0, -25.0, -5.0], [-5.0, -15.0, -3.0], [-5.0, -5.0, -8.0],
               [-5.0, 5.0, -8.0], [-5.0, 15.0, -3.0], [-5.0, 25.0, -5.0],
               [5.0, -25.0, -3.0], [5.0, -15.0, -2.0], [5.0, -5.0, -8.0],
               [5.0, 5.0, -8.0], [5.0, 15.0, -2.0], [5.0, 25.0, -3.0],
               [15.0, -25.0, -8.0], [15.0, -15.0, -4.0], [15.0, -5.0, -4.0],
               [15.0, 5.0, -4.0], [15.0, 15.0, -4.0], [15.0, 25.0, -8.0],
               [25.0, -25.0, -10.0], [25.0, -15.0, -5.0], [25.0, -5.0, 2.0],
               [25.0, 5.0, 2.0], [25.0, 15.0, -5.0], [25.0, 25.0, -10.0]]

    # Set control points
    surf.set_ctrlpts(ctrlpts, 6, 6)

    # Set knot vectors
    surf.knotvector_u = [0.0, 0.0, 0.0, 0.0, 0.33, 0.66, 1.0, 1.0, 1.0, 1.0]
    surf.knotvector_v = [0.0, 0.0, 0.0, 0.0, 0.33, 0.66, 1.0, 1.0, 1.0, 1.0]

    return surf
예제 #8
0
def geomdl_method(degree, ctrl_points):
    """Generate by geomdl package"""
    print(util.Section('B-Spline using geomdl Package'))
    # construct
    curve = BSpline.Curve()
    curve.degree = degree
    curve.ctrlpts = ctrl_points
    curve.knotvector = utilities.generate_knot_vector(degree, len(ctrl_points))
    curve.evaluate(step=0.01)

    print(
        f'knots length = {len(curve.knotvector)}, knots = {curve.knotvector}')
    print(f'c(0) = {curve.evaluate_single(0)}')
    print(f'c(0.5) = {curve.evaluate_single(0.5)}')
    print(f'c(0.6) = {curve.evaluate_single(0.6)}')
    print(f'c(1.0) = {curve.evaluate_single(1.0)}')

    # plot
    ctrl_plot_points = np.array(ctrl_points)
    curve_points = np.array(curve.evalpts)
    fig = plt.figure('B-Spline using geomdl Package')
    ax = fig.add_subplot(111)
    ax.plot(ctrl_plot_points[:, 0],
            ctrl_plot_points[:, 1],
            'g.-.',
            label='Control Points')
    ax.plot(curve_points[:, 0], curve_points[:, 1], 'b', label='BSpline Curve')
    ax.grid(True)
    ax.set_title('B-Spline using geomdl Package')
    ax.legend(loc='best')
    plt.show(block=False)
예제 #9
0
def test_convert_surface():
    surf_bs = BSpline.Surface()
    surf_bs.degree_u = S_DEGREE_U
    surf_bs.degree_v = S_DEGREE_V
    surf_bs.ctrlpts_size_u = 3
    surf_bs.ctrlpts_size_v = 3
    surf_bs.ctrlpts = S_CTRLPTS
    surf_bs.knotvector_u = S_KV_U
    surf_bs.knotvector_v = S_KV_V

    surf_nurbs = convert.bspline_to_nurbs(surf_bs)
    surf_nurbs.sample_size = SAMPLE_SIZE

    # Expected weights vector
    res_weights = [1.0 for _ in range(3*3)]

    # Expected output
    res = [[0.0, 0.0, 0.0], [0.0, 0.5, -0.1875], [0.0, 1.0, -0.75], [0.0, 1.5, -1.6875],
           [0.0, 2.0, -3.0], [0.5, 0.0, 2.25], [0.5, 0.5, 1.171875], [0.5, 1.0, 0.1875],
           [0.5, 1.5, -0.703125], [0.5, 2.0, -1.5], [1.0, 0.0, 3.0], [1.0, 0.5, 1.6875],
           [1.0, 1.0, 0.75], [1.0, 1.5, 0.1875], [1.0, 2.0, 0.0], [1.5, 0.0, 2.25],
           [1.5, 0.5, 1.359375], [1.5, 1.0, 0.9375], [1.5, 1.5, 0.984375], [1.5, 2.0, 1.5],
           [2.0, 0.0, 0.0], [2.0, 0.5, 0.1875], [2.0, 1.0, 0.75], [2.0, 1.5, 1.6875], [2.0, 2.0, 3.0]]

    assert not surf_bs.rational
    assert surf_nurbs.rational
    assert surf_nurbs.evalpts == res
    assert surf_nurbs.weights == res_weights
예제 #10
0
    def build_geomdl(cls,
                     degree_u,
                     degree_v,
                     knotvector_u,
                     knotvector_v,
                     control_points,
                     weights,
                     normalize_knots=False):
        def convert_row(verts_row, weights_row):
            return [(x * w, y * w, z * w, w)
                    for (x, y, z), w in zip(verts_row, weights_row)]

        if weights is None:
            surf = BSpline.Surface(normalize_kv=normalize_knots)
        else:
            surf = NURBS.Surface(normalize_kv=normalize_knots)
        surf.degree_u = degree_u
        surf.degree_v = degree_v
        if weights is None:
            ctrlpts = control_points
        else:
            ctrlpts = list(map(convert_row, control_points, weights))
        surf.ctrlpts2d = ctrlpts
        surf.knotvector_u = knotvector_u
        surf.knotvector_v = knotvector_v

        result = SvGeomdlSurface(surf)
        result.u_bounds = surf.knotvector_u[0], surf.knotvector_u[-1]
        result.v_bounds = surf.knotvector_v[0], surf.knotvector_v[-1]
        return result
예제 #11
0
    def surface_fit(self, terrain_grid: np.ndarray, grid_resolution: int,
                    u_degree: int, v_degree: int, delta: float):

        # Create a BSpline surface instance
        surf = BSpline.Surface()

        # Set evaluation delta
        surf.delta = delta

        # Set up the surface
        surf.degree_u = u_degree
        surf.degree_v = v_degree

        control_points = terrain_grid.tolist()
        surf.set_ctrlpts(control_points, grid_resolution, grid_resolution)

        surf.knotvector_u = utilities.generate_knot_vector(
            surf.degree_u, grid_resolution)
        surf.knotvector_v = utilities.generate_knot_vector(
            surf.degree_v, grid_resolution)

        # Evaluate surface points
        surf.evaluate()

        return surf
예제 #12
0
def get_norms(surf, timestep_limit):
    # create a 2D bezier curve
    curve = BSpline.Curve()
    curve.degree = 3

    ctrl_points = [[np.random.uniform(0, 0.2), np.random.uniform(0, 0.2)],
                   [np.random.uniform(0, 0.5), np.random.uniform(0, 0.5)],
                   [np.random.uniform(0.25, 0.75), np.random.uniform(0.25, 0.75)],
                   [np.random.uniform(0.5, 0.75), np.random.uniform(0.5, 0.75)],
                   [np.random.uniform(0.8, 1), np.random.uniform(0.8, 1.0)]]

    curve.set_ctrlpts(ctrl_points)
    curve.knotvector = utilities.generate_knot_vector(curve.degree, len(curve.ctrlpts))
    curve.sample_size = timestep_limit
    curve.evaluate()
    points_c = curve.evalpts
    norms = normal(surf, points_c)
    angles = []  # pitch, yaw
    traj = []  # x,y,z
    for i in range(len(norms)):
        nu = np.array(norms[i][1])
        yaw = 180.0*np.arctan2(np.abs(nu[1]), np.abs(nu[0]))/np.pi
        ca = np.linalg.norm(nu[0:2])
        pitch = 180.0*np.arctan2(nu[2], ca)/np.pi
        angles.append([pitch, yaw])
        point = np.array(norms[i][0])
        traj.append(np.array([point[0], point[1], point[2], pitch, yaw]))
    return traj, norms, curve
예제 #13
0
def main():
    # Fix file path
    os.chdir(os.path.dirname(os.path.realpath(__file__)))

    # Create a BSpline surface instance
    surf = BSpline.Surface()

    # Set degrees
    surf.degree_u = 3
    surf.degree_v = 3

    # Set control points
    surf.set_ctrlpts(
        *exchange.import_txt("ex_surface01.cpt", two_dimensional=True))

    # Set knot vectors
    surf.knotvector_u = [0.0, 0.0, 0.0, 0.0, 1.0, 2.0, 3.0, 3.0, 3.0, 3.0]
    surf.knotvector_v = [0.0, 0.0, 0.0, 0.0, 1.0, 2.0, 3.0, 3.0, 3.0, 3.0]

    # Set evaluation delta
    surf.delta = 0.025

    # Evaluate surface points
    surf.evaluate()

    # Import and use Matplotlib's colormaps
    from matplotlib import cm

    # Plot the control point grid and the evaluated surface
    vis_comp = vis.VisSurface()
    surf.vis = vis_comp
    surf.render(colormap=cm.cool)

    # Good to have something here to put a breakpoint
    pass
예제 #14
0
def spline2mask(crl, width, height, delta=.05, new_shape=None):
    c, r, l = crl if type(crl) == list else crl.tolist()
    cps = []
    for i in range(len(c)):
        ip = (i + 1) % len(c)
        cps.append([c[i], r[i], l[ip], c[ip]])
    connecs = []
    for i in range(len(cps)):
        curve = BSpline.Curve()
        curve.degree = 3
        curve.ctrlpts = cps[i]
        curve.knotvector = utilities.generate_knot_vector(
            curve.degree, len(curve.ctrlpts))
        # print('delta',delta)
        curve.delta = delta
        curve.evaluate()
        connecs.append(curve.evalpts)

    polygon = np.array(connecs).flatten().tolist()
    img = Image.new('L', (height, width), 255)
    ImageDraw.Draw(img).polygon(polygon, outline=0, fill=0)
    if new_shape is None:
        new_shape = (height, width)
    mask = np.array(img.resize(new_shape, Image.NEAREST))
    #print(mask.shape, width, height, downscale,int(width//downscale), int(height//downscale))
    return mask == False
예제 #15
0
def bspline_surface():
    """ Creates a B-Spline surface instance """
    # Create a surface instance
    surf = BSpline.Surface()

    # Set degrees
    surf.degree_u = 3
    surf.degree_v = 3

    # Set control points
    surf.ctrlpts_size_u = 6
    surf.ctrlpts_size_v = 6
    surf.ctrlpts = [[-25.0, -25.0, -10.0], [-25.0, -15.0, -5.0], [-25.0, -5.0, 0.0], [-25.0, 5.0, 0.0],
                    [-25.0, 15.0, -5.0], [-25.0, 25.0, -10.0], [-15.0, -25.0, -8.0], [-15.0, -15.0, -4.0],
                    [-15.0, -5.0, -4.0], [-15.0, 5.0, -4.0], [-15.0, 15.0, -4.0], [-15.0, 25.0, -8.0],
                    [-5.0, -25.0, -5.0], [-5.0, -15.0, -3.0], [-5.0, -5.0, -8.0], [-5.0, 5.0, -8.0],
                    [-5.0, 15.0, -3.0], [-5.0, 25.0, -5.0], [5.0, -25.0, -3.0], [5.0, -15.0, -2.0],
                    [5.0, -5.0, -8.0], [5.0, 5.0, -8.0], [5.0, 15.0, -2.0], [5.0, 25.0, -3.0],
                    [15.0, -25.0, -8.0], [15.0, -15.0, -4.0], [15.0, -5.0, -4.0], [15.0, 5.0, -4.0],
                    [15.0, 15.0, -4.0], [15.0, 25.0, -8.0], [25.0, -25.0, -10.0], [25.0, -15.0, -5.0],
                    [25.0, -5.0, 2.0], [25.0, 5.0, 2.0], [25.0, 15.0, -5.0], [25.0, 25.0, -10.0]]

    # Set knot vectors
    surf.knotvector_u = [0.0, 0.0, 0.0, 0.0, 0.33, 0.66, 1.0, 1.0, 1.0, 1.0]
    surf.knotvector_v = [0.0, 0.0, 0.0, 0.0, 0.33, 0.66, 1.0, 1.0, 1.0, 1.0]

    # Set sample size
    surf.sample_size = SAMPLE_SIZE

    # Return the instance
    return surf
예제 #16
0
def spline_curve():
    """ Creates a spline Curve """
    curve = BSpline.Curve()
    curve.degree = 3
    curve.ctrlpts = [[5.0, 5.0], [10.0, 10.0], [20.0, 15.0], [35.0, 15.0], [45.0, 10.0], [50.0, 5.0]]
    curve.knotvector = [0.0, 0.0, 0.0, 0.0, 0.33, 0.66, 1.0, 1.0, 1.0, 1.0]
    return curve
예제 #17
0
파일: test01.py 프로젝트: kuro26/sci-sim
def swing_leg_planning():
    curve = BSpline.Curve()
    curve.ctrlpts = (
        (-0.2, 0), (-0.3, -0.03), (0., 0.3), (0.3, 0.), (0.2, 0.))  # 控制点
    curve.delta = 0.01  # 数据间隔
    curve.degree = 4  # degree应该小于控制点数量
    curve.knotvector = utilities.generate_knot_vector(curve.degree,
                                                      len(curve.ctrlpts))
    curve.evaluate()
    x = []
    dx = []
    y = []
    dy = []
    t_list = np.linspace(0, 1, 101)
    for t in t_list:
        tmp = curve.derivatives(t, order=1)
        x.append(tmp[0][0])
        dx.append(tmp[1][0])
        y.append(tmp[0][1])
        dy.append(tmp[1][1])
    plt.plot(x, y)
    plt.xlim((-0.3, 0.3))
    plt.ylim((-0.1, 0.5))
    plt.grid()
    plt.show()
예제 #18
0
def draw(size):
    data = []
    data = np.ones((size, size), np.uint16) * 65535
    for _ in range(2):
        pnum = randint(15, 100)
        a = random() * TWOPI + linspace(0, TWOPI, pnum)
        points = column_stack((cos(a), sin(a))) * (1500 + random() * 100)
        curve = BSpline.Curve()
        curve.degree = 3
        # lpoints=list(list([i[0]+randint(-WOBBLE,WOBBLE),i[1]+randint(-WOBBLE,WOBBLE)]) for i in points)
        # curve.ctrlpts=lpoints
        # print(curve.ctrlpts)

        curve.delta = 0.00001
        # curve_points=curve.evalpts
        #         for t in curve_points:
        #             data[int(round(t[0]+size/2)),int(round(t[1]+size/2))]=65535
        # print(int(round(t[0]+size/2)),int(round(t[1]+size/2)))

        for decay in range(DECAYLENGTH):
            # print(crange[decay])
            lpoints = list(
                list([
                    i[0] + randint(-WOBBLE, WOBBLE), i[1] +
                    randint(-WOBBLE, WOBBLE)
                ]) for i in points)
            curve.ctrlpts = lpoints
            curve.knotvector = knotvector.generate(3, len(curve.ctrlpts))
            curve_points = curve.evalpts
            for t in curve_points:
                data[int(round(t[0] + size / 2)),
                     int(round(t[1] + size / 2))] += int(crange[decay])

    return data
def bspline_curve3d():
    """ Creates a 3-dimensional B-Spline curve instance """
    # Create a curve instance
    curve = BSpline.Curve()

    # Set curve degree
    curve.degree = 4

    # Set control points
    curve.ctrlpts = [[5.0, 15.0, 0.0], [10.0, 25.0, 5.0], [20.0, 20.0, 10.0],
                     [15.0, -5.0, 15.0], [7.5, 10.0, 20.0], [12.5, 15.0, 25.0],
                     [15.0, 0.0, 30.0], [5.0, -10.0, 35.0], [10.0, 15.0, 40.0],
                     [5.0, 15.0, 30.0]]

    # Set knot vector
    curve.knotvector = [
        0.0, 0.0, 0.0, 0.0, 0.0, 0.1, 0.3, 0.5, 0.7, 0.9, 1.0, 1.0, 1.0, 1.0,
        1.0
    ]

    # Set sample size
    curve.sample_size = SAMPLE_SIZE

    # Return the instance
    return curve
예제 #20
0
def interpolate_Bspline(stepsBtwFrame,
                        currentPos,
                        nextPos,
                        liftHeight,
                        draw=False):
    # This function returns a list of 2-D trajectory points. Need to use IK to convert to joint angles afterwards
    # The return looks like [[0,0], [0,0], [0,0], [0,0], ... [0,0]] length=stepsBtwFrame
    # next = [x, z]
    # curr = [x, z]

    x_offset = 5  # Top control point height above liftheight
    z_offset = 30  # End control point position outside leg stroke

    # Pick up leg and make a full curve down to a upper position of wall

    halfZstroke = (nextPos[1] - currentPos[1]) / 2.0  # half stroke
    halfZstroke_and_offset = halfZstroke + z_offset  # half stroke plus offset outside the stroke

    ctrlPoint = [
        [0.0, -halfZstroke_and_offset / 2.5], [liftHeight, -halfZstroke],
        [liftHeight + x_offset, 0.0], [liftHeight, 2 * halfZstroke]
    ]  # there was one more point: [self.liftHeight/2, 0.0, halfZstroke_and_offset/3.0]

    nurbs = BSpline.Curve()

    # Set unweighted control points

    allPts = [currentPos]
    for i in range(len(ctrlPoint)):
        allPts.append(ctrlPoint[i])

    allPts.append(nextPos)

    nurbs.degree = 2  # Degree of the curve, order = degree+1

    nurbs.ctrlpts = allPts

    # Auto-generate knot vector
    nurbs.knotvector = utilities.generate_knot_vector(nurbs.degree,
                                                      len(nurbs.ctrlpts))

    # depending on how long the looping time is and how smooth you want the trajectory to be,
    # this value should be carefully tuned
    nurbs.sample_size = stepsBtwFrame + 1  # The total # of points that goes to pts = nurbs._curve_points

    nurbs.evaluate()  # need to put start and stop points

    pts = nurbs.evalpts  #[1:(stepsBtwFrame+1)]
    if draw == True:
        ##################### For climbing between 2 walls #####################
        plt.plot([nurbs.evalpts[i][0] for i in range(nurbs.sample_size)],
                 [nurbs.evalpts[i][1] for i in range(nurbs.sample_size)])
        plt.xlabel("x")
        plt.ylabel("z")
        #plt.xlim([-200, 10])
        #plt.ylim([-200, 200])
        plt.show(block=True)

    return pts
예제 #21
0
def test_bspline_curve_loadsave():
    curve_save = BSpline.Curve()
    curve_save.degree = C_DEGREE
    curve_save.ctrlpts = C_CTRLPTS3D
    curve_save.knotvector = C_KV
    curve_save.save(FILE_NAME)

    curve_load = BSpline.Curve()
    curve_load.load(FILE_NAME)

    # Remove save file
    os.remove(FILE_NAME)

    assert curve_save.degree == curve_load.degree
    assert curve_save.knotvector == curve_load.knotvector
    assert curve_save.ctrlpts == curve_load.ctrlpts
    assert curve_save.dimension == curve_load.dimension
예제 #22
0
def gen_kern_seq(num_spline_dots=12, kernel_size=(19, 38)):
    curve_degree = num_spline_dots - 1
    curve = BSpline.Curve()
    small_kernel_size = (int(np.round(kernel_size[0] / 1.5)) - 1,
                         int(np.round(kernel_size[1] / 1.5)) - 1)
    phi = np.random.rand() * 2 * np.pi
    knot_dots = [[
        small_kernel_size[0] * np.cos(phi), small_kernel_size[1] * np.sin(phi)
    ]]
    for i in range(1, num_spline_dots - 1):
        knot_dots += [[
            np.random.randint(0, small_kernel_size[0]),
            np.random.randint(0, small_kernel_size[1])
        ]]

    phi = np.random.rand() * 2 * np.pi
    knot_dots += [[
        small_kernel_size[0] * np.cos(phi), small_kernel_size[1] * np.sin(phi)
    ]]
    curve.degree = curve_degree
    curve.ctrlpts = knot_dots
    curve.knotvector = generate_knot_vector(curve.degree, len(curve.ctrlpts))

    curve1, curve2 = split_curve(curve, np.random.rand() * 0.2 + 0.4)

    curve1.delta = 0.005
    curve2.delta = 0.005
    curve1.evaluate()
    curve2.evaluate()

    pts_1 = np.array(curve1.evalpts)
    pts_1[:, 0] -= (pts_1[:, 0].min() - 2)
    pts_1[:, 1] -= (pts_1[:, 1].min() - 2)

    pts_2 = np.array(curve2.evalpts)
    pts_2[:, 0] -= (pts_2[:, 0].min() - 2)
    pts_2[:, 1] -= (pts_2[:, 1].min() - 2)

    k_dim = int(max(np.ceil(pts_1.max()), np.ceil(pts_2.max()))) + 2

    k1 = kern_from_grid(pts_1, k_dim + 1)
    k2 = kern_from_grid(pts_2, k_dim + 1)

    cm_k1 = np.round(calculate_center_mass(k1), 0).astype(int)
    dim_k1_c = 2 * max(list(np.array(k1.shape) - cm_k1) + list(cm_k1))

    cm_k2 = np.round(calculate_center_mass(k2), 0).astype(int)
    dim_k2_c = 2 * max(list(np.array(k2.shape) - cm_k2) + list(cm_k2))

    dim_final = max(dim_k1_c, dim_k2_c)
    if dim_final % 2 == 0: dim_final += 1

    k1 = expand(k1, (dim_final, dim_final))
    k2 = expand(k2, (dim_final, dim_final))

    k1 /= k1.sum()
    k2 /= k2.sum()
    return k1, k2
예제 #23
0
def generate_bspline(degree, control_points, knots, delta=0.005):
    curve = BSpline.Curve()
    curve.degree = degree
    curve.ctrlpts = control_points
    curve.knotvector = knots
    curve.delta = delta
    curve_points = curve.evalpts
    curve_points = np.asarray(curve_points)
    return curve, curve_points
예제 #24
0
    def render_to_tolerance(self, tolerance):
        curve = BSpline.Curve()
        curve.degree = self.degree
        curve.ctrlpts = self.control.tolist()
        curve.knotvector = self.knots.tolist()
        curve.sample_size = max(
            2, math.ceil(self.length_upper_bound() / tolerance))

        return burin.types.BSpline(curve, tolerance)
def test_bspline_curve3d_evaluate():
    curve = BSpline.Curve()
    curve.degree = C_DEGREE
    curve.ctrlpts = C_CTRLPTS3D
    curve.knotvector = C_KV
    curve.sample_size = SAMPLE_SIZE

    # Expected output
    res = [[1.0, 1.0, 0.0], [1.4375, 1.0625, -0.375], [1.75, 1.25, -0.5], [1.9375, 1.5625, -0.375], [2.0, 2.0, 0.0]]

    assert curve.evalpts == res
예제 #26
0
def getQuadBezier(P0, P1, P2, Degree=2, nSample=50):
    """
    返回一个QuadBezier对象, P0 P1 P2为控制点,Degree大于2可能报错,nSample为采样数目
    """
    curve = BSpline.Curve()
    curve.ctrlpts = [P0, P1, P2]
    curve.degree = Degree
    curve.knotvector = utilities.generate_knot_vector(curve.degree,
                                                      len(curve.ctrlpts))
    curve.sample_size = nSample
    return curve
예제 #27
0
def nurbs(suc, pre, n=400):
    assert len(suc) == len(pre)
    xs = np.linspace(0, 1, len(suc) + 2).tolist()
    pts = np.array([xs[::-1] + xs[1:],
                    [0] + suc[::-1] + [0] + pre + [0]]).transpose().tolist()
    crv = BSpline.Curve()
    crv.degree = 2
    crv.ctrlpts = pts
    crv.knotvector = knotvector.generate(crv.degree, crv.ctrlpts_size)
    airfoil = np.array([crv.evaluate_single(t) for t in np.linspace(0, 1, n)])
    return airfoil[:, 0], airfoil[:, 1]
예제 #28
0
def bs_surface2():
    surf = BSpline.Surface()
    ctrlpts = [[1.0, 1.0, 10.0], [1.0, 2.0, 11.0], [1.0, 3.0, 12.0],
               [2.0, 1.0, 13.0], [2.0, 2.0, 14.0], [2.0, 3.0, 15.0],
               [3.0, 1.0, 16.0], [3.0, 2.0, 17.0], [3.0, 3.0, 18.0],
               [4.0, 1.0, 19.0], [4.0, 2.0, 20.0], [4.0, 3.0, 21.0]]
    surf.ctrlpts_size_v = 3
    surf.ctrlpts_size_u = 4
    surf.degree_u = 2
    surf.degree_v = 2
    surf.ctrlpts = ctrlpts
    return surf
def bspline_surface():
    """ Creates a B-Spline surface instance """
    surf = BSpline.Surface()
    surf.degree_u = 2
    surf.degree_v = 2
    surf.ctrlpts_size_u = 3
    surf.ctrlpts_size_v = 3
    surf.ctrlpts = [[0, 0, 0], [0, 1, 0], [0, 2, -3], [1, 0, 6], [1, 1, 0],
                    [1, 2, 0], [2, 0, 0], [2, 1, 0], [2, 2, 3]]
    surf.knotvector_u = [0, 0, 0, 1, 1, 1]
    surf.knotvector_v = [0, 0, 0, 1, 1, 1]
    return surf
예제 #30
0
def resample_coords_smooth(coords, sample_size=120, degree=3):
    """Resamples an array of coordinates while smoothing out the new values with a b-spline."""

    #Prevents code from breaking when the stroke contains too few values.
    while coords.shape[0] < degree + 1:
        coords = np.concatenate((coords, np.expand_dims(coords[-1], axis=0)), axis=0)

    curve = BSpline.Curve()
    curve.degree = degree
    curve.ctrlpts = coords.tolist()
    curve.knotvector = generate_knot_vector(degree, len(curve.ctrlpts))
    curve.sample_size = sample_size
    return np.array(curve.evalpts)