Ejemplo n.º 1
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
Ejemplo n.º 2
0
    def createNURBSSurface(self, pts, corsequences, sagsequences, degree=3):
        # Dimensions of the control points grid
        npoints_u = len(corsequences)
        npoints_v = len(sagsequences)

        # Weights vector
        weights = [1] * (npoints_u * npoints_v)

        # Combine weights vector with the control points list
        t_ctrlptsw = compat.combine_ctrlpts_weights(pts, weights)

        # Since NURBS-Python uses v-row order, we need to convert the exported ones
        n_ctrlptsw = compat.change_ctrlpts_row_order(t_ctrlptsw, npoints_u,
                                                     npoints_v)

        # Since we have no information on knot vectors, let's auto-generate them
        n_knotvector_u = utils.generate_knot_vector(degree, npoints_u)
        n_knotvector_v = utils.generate_knot_vector(degree, npoints_v)

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

        # Using __call__ method to fill the surface object
        surf(degree, degree, npoints_u, npoints_v, n_ctrlptsw, n_knotvector_u,
             n_knotvector_v)

        return surf
Ejemplo n.º 3
0
def cylinder(radius=1, height=1):
    """ Generates a NURBS cylindrical surface.

    The cylindrical surface example is kindly contributed by John-Eric Dufour.

    :param radius: radius of the cylinder
    :type radius: int, float
    :param height: height of the cylinder
    :type height: int, float
    :return: a NURBS surface
    :rtype: NURBS.Surface
    """
    if radius <= 0 or height <= 0:
        raise ValueError("Radius and/or height cannot be less than and equal to zero")

    # Control points for a base cylinder
    control_points = [[[1.0, 0.0, 0.0, 1.0], [0.7071, 0.7071, 0.0, 0.7071], [0.0, 1.0, 0.0, 1.0],
                       [-0.7071, 0.7071, 0.0, 0.7071], [-1.0, 0.0, 0.0, 1.0], [-0.7071, -0.7071, 0.0, 0.7071],
                       [0.0, -1.0, 0.0, 1.0], [0.7071, -0.7071, 0.0, 0.7071], [1.0, 0.0, 0.0, 1.0]],
                      [[1.0, 0.0, 1.0, 1.0], [0.7071, 0.7071, 0.7071, 0.7071], [0.0, 1.0, 1.0, 1.0],
                       [-0.7071, 0.7071, 0.7071, 0.7071], [-1.0, 0.0, 1.0, 1.0], [-0.7071, -0.7071, 0.7071, 0.7071],
                       [0.0, -1.0, 1.0, 1.0], [0.7071, -0.7071, 0.7071, 0.7071], [1.0, 0.0, 1.0, 1.0]]]

    # Set height
    if height != 1:
        ctrlpts_top = []
        for point in control_points[1]:
            npt = point
            npt[2] = npt[2] * height
            ctrlpts_top.append(npt)
        control_points[1] = ctrlpts_top

    # Set radius
    ctrlpts = []
    if radius != 1:
        for row in control_points:
            temp = []
            for point in row:
                npt = [i * radius for i in point[0:2]]
                npt.append(point[2])
                npt.append(point[3])
                temp.append(npt)
            ctrlpts.append(temp)
    else:
        ctrlpts = control_points

    # Generate the surface
    surface = NURBS.Surface()
    surface.degree_u = 1
    surface.degree_v = 2
    surface.ctrlpts2d = ctrlpts
    surface.knotvector_u = [0.0, 0.0, 1.0, 1.0]
    surface.knotvector_v = [0.0, 0.0, 0.0, 0.25, 0.25, 0.5, 0.5, 0.75, 0.75, 1.0, 1.0, 1.0]

    # Return the generated surface
    return surface
Ejemplo n.º 4
0
 def __init__(self, geomData):
     self.surf = NURBS.Surface()
     self.surf.degree_u = geomData['degree_u']
     self.surf.degree_v = geomData['degree_v']
     self.surf.ctrlpts_size_u = geomData['ctrlpts_size_u']
     self.surf.ctrlpts_size_v = geomData['ctrlpts_size_v']
     self.surf.ctrlpts = self.getUnweightedCpts(geomData['ctrlpts'],
                                                geomData['weights'])
     self.surf.weights = geomData['weights']
     self.surf.knotvector_u = geomData['knotvector_u']
     self.surf.knotvector_v = geomData['knotvector_v']
def nurbs_surface():
    """ Creates a NURBS surface instance """
    surf = NURBS.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]]
    # use the auto-generated weights vector
    surf.knotvector_u = [0, 0, 0, 1, 1, 1]
    surf.knotvector_v = [0, 0, 0, 1, 1, 1]
    return surf
Ejemplo n.º 6
0
def init_nurbs(p, q, knot_u, knot_v, ctrlpts):
    """Create a python NURBS surface object

	:param p,q: degree in U and V directions
	:param knot_u,knot_v: knot vectors in U and V directions
	:param ctrlpts: control point matrix
	"""
    surf = NURBS.Surface()
    surf.degree_u = p
    surf.degree_v = q
    surf.ctrlpts2d = ctrlpts.tolist()
    surf.knotvector_u = knot_u
    surf.knotvector_v = knot_v

    return surf
Ejemplo n.º 7
0
def test_nurbs_surface_weights3():
    surf = NURBS.Surface()
    ctrlpts = [[1.0, 1.0, 10.0, 1.0], [1.0, 2.0, 11.0, 1.0],
               [1.0, 3.0, 12.0, 1.0], [2.0, 1.0, 13.0, 1.0],
               [2.0, 2.0, 14.0, 0.5], [2.0, 3.0, 15.0, 1.0],
               [3.0, 1.0, 16.0, 0.2], [3.0, 2.0, 17.0, 1.0],
               [3.0, 3.0, 18.0, 1.0], [4.0, 1.0, 19.0, 1.0],
               [4.0, 2.0, 20.0, 1.0], [4.0, 3.0, 21.0, 1.0]]
    surf.ctrlpts_size_v = 3
    surf.ctrlpts_size_u = 4
    surf.degree_u = 2
    surf.degree_v = 2
    surf.ctrlptsw = ctrlpts

    # Check assignment
    assert surf.weights[4] == 0.5
Ejemplo n.º 8
0
def test_nurbs_surface_ctrlpts2():
    surf = NURBS.Surface()
    ctrlpts = [[1.0, 1.0, 10.0, 1.0], [1.0, 2.0, 11.0, 1.0],
               [1.0, 3.0, 12.0, 1.0], [2.0, 1.0, 13.0, 1.0],
               [2.0, 2.0, 14.0, 1.0], [2.0, 3.0, 15.0, 1.0],
               [3.0, 1.0, 16.0, 1.0], [3.0, 2.0, 17.0, 1.0],
               [3.0, 3.0, 18.0, 1.0], [4.0, 1.0, 19.0, 1.0],
               [4.0, 2.0, 20.0, 1.0], [4.0, 3.0, 21.0, 1.0]]
    surf.ctrlpts_size_v = 3
    surf.ctrlpts_size_u = 4
    surf.degree_u = 2
    surf.degree_v = 2
    surf.ctrlptsw = ctrlpts

    # Check assignment
    assert surf.ctrlpts2d[2][1] == [3.0, 2.0, 17.0, 1.0]
Ejemplo n.º 9
0
def nurbs_surface():
    """ Creates a NURBS Surface instance """
    # Create a surface instance
    surf = NURBS.Surface()

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

    # Set weighted control points
    surf.set_ctrlpts(CONTROL_POINTS, 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
Ejemplo n.º 10
0
def test_nurbs_surface_knot_vector_v():
    surf = NURBS.Surface()
    ctrlpts = [[1.0, 1.0, 10.0, 1.0], [1.0, 2.0, 11.0, 1.0],
               [1.0, 3.0, 12.0, 1.0], [2.0, 1.0, 13.0, 1.0],
               [2.0, 2.0, 14.0, 1.0], [2.0, 3.0, 15.0, 1.0],
               [3.0, 1.0, 16.0, 1.0], [3.0, 2.0, 17.0, 1.0],
               [3.0, 3.0, 18.0, 1.0], [4.0, 1.0, 19.0, 1.0],
               [4.0, 2.0, 20.0, 1.0], [4.0, 3.0, 21.0, 1.0]]
    surf.ctrlpts_size_v = 3
    surf.ctrlpts_size_u = 4
    surf.degree_u = 2
    surf.degree_v = 2
    surf.ctrlptsw = ctrlpts
    surf.knotvector_u = [0.0, 0.0, 0.0, 0.5, 1.0, 1.0, 1.0]
    surf.knotvector_v = [0.0, 0.0, 0.0, 1.0, 1.0, 1.0]

    assert surf.knotvector_v == [0.0, 0.0, 0.0, 1.0, 1.0, 1.0]
Ejemplo n.º 11
0
def nurbs_surface2():
    """ Creates a NURBS Surface instance (alternative control points) """
    # Create a surface instance
    surf = NURBS.Surface()

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

    # Set weighted control points
    surf.ctrlpts_size_u = 6
    surf.ctrlpts_size_v = 6
    surf.ctrlptsw = CONTROL_POINTS2

    # 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
Ejemplo n.º 12
0
    def load_spline_surf(self, spline):
        # Create a BSpline surface
        if spline["v_rational"] or spline["u_rational"]:
            surf = NURBS.Surface()
            control_points = np.array(spline["poles"])
            size_u, size_v = control_points.shape[0], control_points.shape[1]

            # Set degrees
            surf.degree_u = spline["u_degree"]
            surf.degree_v = spline["v_degree"]

            # Set control points
            surf.ctrlpts2d = np.concatenate(
                [control_points, np.ones((size_u, size_v, 1))], 2).tolist()
            surf.knotvector_v = spline["v_knots"]
            surf.knotvector_u = spline["u_knots"]

            weights = spline["weights"]
            l = []
            for i in weights:
                l += i
            surf.weights = l
            return surf

        else:
            surf = BSpline.Surface()

            # Set degrees
            surf.degree_u = spline["u_degree"]
            surf.degree_v = spline["v_degree"]

            # Set control points
            surf.ctrlpts2d = spline["poles"]

            # Set knot vectors
            surf.knotvector_u = spline["u_knots"]
            surf.knotvector_v = spline["v_knots"]
            return surf
Ejemplo n.º 13
0
            ]
            return content_arr
    except IOError as e:
        print("An error occurred: {}".format(e.args[-1]))
        raise e


# duck1.nurbs

# Process control points and weights
d2_ctrlpts = exchange.import_txt("duck1.ctrlpts", separator=" ")
d1_weights = read_weights("duck1.weights")
d1_ctrlptsw = compatibility.combine_ctrlpts_weights(d2_ctrlpts, d1_weights)

# Create a NURBS surface
duck1 = NURBS.Surface()
duck1.name = "body"
duck1.order_u = 4
duck1.order_v = 4
duck1.ctrlpts_size_u = 14
duck1.ctrlpts_size_v = 13
duck1.ctrlptsw = d1_ctrlptsw
duck1.knotvector_u = [
    -1.5708, -1.5708, -1.5708, -1.5708, -1.0472, -0.523599, 0, 0.523599,
    0.808217, 1.04015, 1.0472, 1.24824, 1.29714, 1.46148, 1.5708, 1.5708,
    1.5708, 1.5708
]
duck1.knotvector_v = [
    -3.14159, -3.14159, -3.14159, -3.14159, -2.61799, -2.0944, -1.0472,
    -0.523599, 6.66134e-016, 0.523599, 1.0472, 2.0944, 2.61799, 3.14159,
    3.14159, 3.14159, 3.14159
Ejemplo n.º 14
0
#
# Prepare data for import
#

t_ctrlptsw = compat.combine_ctrlpts_weights(p_ctrlpts, p_weights)
n_ctrlptsw = compat.flip_ctrlpts_u(t_ctrlptsw, p_size_u, p_size_v)

# Since we have no information on knot vectors, let's auto-generate them
n_knotvector_u = utils.generate_knot_vector(p_degree_u, p_size_u)
n_knotvector_v = utils.generate_knot_vector(p_degree_v, p_size_v)

#
# Import surface to NURBS-Python
#

surf = NURBS.Surface()
surf.degree_u = p_degree_u
surf.degree_v = p_degree_v
surf.ctrlpts_size_u = p_size_u
surf.ctrlpts_size_v = p_size_v
surf.ctrlptsw = n_ctrlptsw
surf.knotvector_u = n_knotvector_u
surf.knotvector_v = n_knotvector_v

# Set evaluation delta
surf.delta = 0.05

# Set visualization component
vis_comp = VisMPL.VisSurfTriangle()
surf.vis = vis_comp
Ejemplo n.º 15
0
        def make_surface(self, face, degree_u, degree_v, vertices, planes,
                         vert_weights, tangent_weights, face_weight,
                         edge_weights_dict, edge_planes_dict):
            """
            V0 ------ [E01] --- [E0C] --- [E02] --- V1
            |          |         |        |         |
            |          |         |        |         |
            |          |         |        |         |
            [E11] --- [F1] ---- [E0F] --- [F2] --- [E21]
            |          |         |        |         |
            |          |         |        |         |
            |          |         |        |         |
            [E1C] --- [E1F] --- [CC] --- [E2F] --- [E2C]
            |          |         |        |         |
            |          |         |        |         |
            |          |         |        |         |
            [E12] --- [F3] ---- [E3F] --- [F4] --- [E22]
            |          |         |        |         |
            |          |         |        |         |
            |          |         |        |         |
            V3 ------ [E31] --- [E3C] --- [E32] --- V2
            """
            tangent_weights = [w / 3.0 for w in tangent_weights]
            vertices = [Vector(v) for v in vertices]

            def mk_edge_point(i, j):
                return (vertices[j] -
                        vertices[i]) * tangent_weights[i] + vertices[i]

            def mk_face_corner_point(i, j, k):
                dv1 = (vertices[j] - vertices[i]) * tangent_weights[i]
                dv2 = (vertices[k] - vertices[i]) * tangent_weights[i]
                #m = face_weight
                return planes[i].projection_of_point(vertices[i] + dv1 + dv2)

            # edge planes
            e0p = edge_planes_dict[(face[0], face[1])]
            e1p = edge_planes_dict[(face[0], face[3])]
            e2p = edge_planes_dict[(face[1], face[2])]
            e3p = edge_planes_dict[(face[2], face[3])]

            def mk_edge_center_point(ep1, ep2):
                return (ep1 + ep2) / 2.0

            def mk_face_edge_point(edge_plane, edge_point, edge_vec, vec1,
                                   vec2):
                length = (vec1.length + vec2.length) / 2.0
                vec = edge_plane.normal.cross(edge_vec)
                #print("EV: %s, N: %s, L: %s, Res: %s" % (edge_vec, edge_plane.normal, length, vec))
                vec = length * vec.normalized()
                return edge_point + vec

            e01 = planes[0].projection_of_point(mk_edge_point(0, 1))
            e02 = planes[1].projection_of_point(mk_edge_point(1, 0))
            e11 = planes[0].projection_of_point(mk_edge_point(0, 3))
            e21 = planes[1].projection_of_point(mk_edge_point(1, 2))
            f1 = mk_face_corner_point(0, 1, 3)
            f2 = mk_face_corner_point(1, 0, 2)
            e12 = planes[3].projection_of_point(mk_edge_point(3, 0))
            e31 = planes[3].projection_of_point(mk_edge_point(3, 2))
            e32 = planes[2].projection_of_point(mk_edge_point(2, 3))
            e22 = planes[2].projection_of_point(mk_edge_point(2, 1))
            f3 = mk_face_corner_point(3, 0, 2)
            f4 = mk_face_corner_point(2, 3, 1)

            e0c = mk_edge_center_point(e01, e02)
            e1c = mk_edge_center_point(e11, e12)
            e2c = mk_edge_center_point(e21, e22)
            e3c = mk_edge_center_point(e31, e32)

            e0f = mk_face_edge_point(e0p, e0c, (vertices[1] - vertices[0]),
                                     (f1 - e01), (f2 - e02))
            e1f = mk_face_edge_point(e1p, e1c, (vertices[0] - vertices[3]),
                                     (f3 - e12), (f1 - e11))
            e2f = mk_face_edge_point(e2p, e2c, (vertices[2] - vertices[1]),
                                     (f2 - e21), (f4 - e22))
            e3f = mk_face_edge_point(e3p, e3c, (vertices[3] - vertices[2]),
                                     (f3 - e31), (f4 - e32))

            cc = center([f1, e0f, f2, e2f, f4, e3f, f3, e1f])

            control_points = [
                vertices[0], e01, e0c, e02, vertices[1], e11, f1, e0f, f2, e21,
                e1c, e1f, cc, e2f, e2c, e12, f3, e3f, f4, e22, vertices[3],
                e31, e3c, e32, vertices[2]
            ]

            # edge point weights
            e0w = edge_weights_dict[(face[0], face[1])]
            e1w = edge_weights_dict[(face[0], face[3])]
            e2w = edge_weights_dict[(face[1], face[2])]
            e3w = edge_weights_dict[(face[2], face[3])]

            weights = [
                vert_weights[0], e0w, e0w, e0w, vert_weights[1], e1w,
                face_weight, face_weight, face_weight, e2w, e1w, face_weight,
                face_weight, face_weight, e2w, e1w, face_weight, face_weight,
                face_weight, e2w, vert_weights[3], e3w, e3w, e3w,
                vert_weights[2]
            ]

            surface = NURBS.Surface()
            surface.degree_u = degree_u
            surface.degree_v = degree_v
            surface.ctrlpts_size_u = 5
            surface.ctrlpts_size_v = 5
            surface.ctrlpts = control_points
            surface.weights = weights
            surface.knotvector_u = knotvector.generate(surface.degree_u, 5)
            surface.knotvector_v = knotvector.generate(surface.degree_v, 5)

            new_surf = SvExGeomdlSurface(surface)
            return new_surf, control_points, weights
Ejemplo n.º 16
0
def main():
    points = np.loadtxt("N2_RV_P0.txt")

    zmin_loc_temp = np.where(points[:, 1] == 0)[0]
    zmin_loc = zmin_loc_temp

    # points = remove_3dpoint(points,zmin_loc)
    N = 5
    slice(N, points)

    slice0 = slice.slices[0]
    x0 = slice0[:, 0]
    y0 = slice0[:, 1]
    z0 = slice0[:, 2]
    slice1 = slice.slices[1]
    x1 = slice1[:, 0]
    y1 = slice1[:, 1]
    z1 = slice1[:, 2]
    slice2 = slice.slices[2]
    x2 = slice2[:, 0]
    y2 = slice2[:, 1]
    z2 = slice2[:, 2]
    slice3 = slice.slices[3]
    x3 = slice3[:, 0]
    y3 = slice3[:, 1]
    z3 = slice3[:, 2]
    slice4 = slice.slices[4]
    x4 = slice4[:, 0]
    y4 = slice4[:, 1]
    z4 = slice4[:, 2]
    print(np.shape(points))
    ranges = slice.bins

    diameter = x0.max() - x0.min()
    radius = diameter
    height = z0.max()

    diameter1 = x1.max() - x1.min()
    radius1 = diameter1
    height1 = z1.max()

    diameter2 = x2.max() - x2.min()
    radius2 = diameter2
    height2 = z2.max()

    diameter3 = x3.max() - x3.min()
    radius3 = diameter3
    height3 = z3.max()

    diameter4 = x4.max() - x4.min()
    radius4 = diameter4 / 2
    height4 = x4.max()

    # Generate a cylindrical surface using the shapes component
    cylinder = surface.cylinder(radius, height)
    # vis_config = VisMPL.VisConfig(ctrlpts=True)
    # vis_comp = VisMPL.VisSurface(config=vis_config)
    # cylinder.vis = vis_comp
    # cylinder.render()
    cyl_points = cylinder.evalpts
    print("*****************")
    # print(len(cyl_points))
    print("*****************")

    cylinder1 = surface.cylinder(radius1, height1)
    cyl_points1 = cylinder1.evalpts
    # vis_config = VisMPL.VisConfig(ctrlpts=True)
    # vis_comp = VisMPL.VisSurface(config=vis_config)
    # cylinder1.vis = vis_comp
    # cylinder1.render()

    cylinder2 = surface.cylinder(radius2, height2)
    cyl_points2 = cylinder2.evalpts
    # vis_config = VisMPL.VisConfig(ctrlpts=True)
    # vis_comp = VisMPL.VisSurface(config=vis_config)
    # cylinder2.vis = vis_comp
    # cylinder2.render()

    cylinder3 = surface.cylinder(radius3, height3)
    cyl_points3 = cylinder3.evalpts
    # vis_config = VisMPL.VisConfig(ctrlpts=True)
    # vis_comp = VisMPL.VisSurface(config=vis_config)
    # cylinder2.vis = vis_comp
    # cylinder2.render()

    cylinder4 = surface.cylinder(radius4, height4)
    cyl_points4 = cylinder4.evalpts
    # vis_config = VisMPL.VisConfig(ctrlpts=True)
    # vis_comp = VisMPL.VisSurface(config=vis_config)
    # cylinder4.vis = vis_comp
    # cylinder4.render()

    #try using points from the surface not the control points
    cylinder_cpts = np.array(cylinder.ctrlpts)
    cylinder_cpts1 = np.array(cylinder1.ctrlpts)
    cylinder_cpts2 = np.array(cylinder2.ctrlpts)
    cylinder_cpts3 = np.array(cylinder3.ctrlpts)
    cylinder_cpts4 = np.array(cylinder4.ctrlpts)

    a = np.array(cdist(np.array(cyl_points), slice0)).min(axis=0)
    b = np.array(cdist(np.array(cyl_points1), slice1)).min(axis=0)
    c = np.array(cdist(np.array(cyl_points2), slice2)).min(axis=0)
    d = np.array(cdist(np.array(cyl_points3), slice3)).min(axis=0)
    e = np.array(cdist(np.array(cyl_points4), slice4)).min(axis=0)

    test = np.zeros((len(cylinder_cpts), 3))
    test1 = np.zeros((len(cylinder_cpts1), 3))
    test2 = np.zeros((len(cylinder_cpts2), 3))
    test3 = np.zeros((len(cylinder_cpts3), 3))
    test4 = np.zeros((len(cylinder_cpts4), 3))

    for i in range(0, len(cylinder_cpts)):
        test[i] = (cylinder_cpts[i] / np.linalg.norm(cylinder_cpts[i])) * a[i]
        test1[i] = (cylinder_cpts1[i] /
                    np.linalg.norm(cylinder_cpts1[i])) * b[i]
        test2[i] = (cylinder_cpts2[i] /
                    np.linalg.norm(cylinder_cpts2[i])) * c[i]
        test3[i] = (cylinder_cpts3[i] /
                    np.linalg.norm(cylinder_cpts3[i])) * d[i]
        test4[i] = (cylinder_cpts4[i] /
                    np.linalg.norm(cylinder_cpts4[i])) * e[i]

    test_zeros_loc = np.where(test[:, 2] == 0)[0]
    test1_zeros_loc = np.where(test1[:, 2] == 0)[0]
    test2_zeros_loc = np.where(test2[:, 2] == 0)[0]
    test3_zeros_loc = np.where(test3[:, 2] == 0)[0]
    test4_zeros_loc = np.where(test4[:, 2] == 0)[0]

    test = remove_3dpoint(test, test_zeros_loc)
    test1 = remove_3dpoint(test1, test1_zeros_loc)
    test2 = remove_3dpoint(test2, test2_zeros_loc)
    test3 = remove_3dpoint(test3, test3_zeros_loc)
    test4 = remove_3dpoint(test4, test4_zeros_loc)

    test = np.array(
        [test[:, 0], test[:, 1],
         np.ones(len(test[:, 2])) * ranges[0]]).T
    test1 = np.array(
        [test1[:, 0], test1[:, 1],
         np.ones(len(test1[:, 2])) * ranges[1]]).T
    test2 = np.array(
        [test2[:, 0], test2[:, 1],
         np.ones(len(test2[:, 2])) * ranges[2]]).T
    test3 = np.array(
        [test3[:, 0], test3[:, 1],
         np.ones(len(test3[:, 2])) * ranges[3]]).T
    test4 = np.array(
        [test4[:, 0], test4[:, 1],
         np.ones(len(test4[:, 2])) * ranges[4]]).T

    # for i in range(0,len(test1)):
    # test1[i] = test1[i] + [0,0,height]
    # test2[i] = test2[i]+[0,0,height1]
    # test3[i] = test3[i]+[0,0,height2]

    test = remove_3dpoint(test, len(test) - 1)
    test1 = remove_3dpoint(test1, len(test1) - 1)
    test2 = remove_3dpoint(test2, len(test2) - 1)
    test3 = remove_3dpoint(test3, len(test3) - 1)
    test4 = remove_3dpoint(test4, len(test4) - 1)

    # test = np.insert(test,[0,len(test)],[[0,0,-5],[0,0,ranges[0]]],axis=0)
    # test1 = np.insert(test1,[0,len(test1)],[0,0,ranges[1]],axis=0)
    # test2 = np.insert(test2,[0,len(test2)],[0,0,ranges[2]],axis=0)
    test = np.insert(
        test,
        [len(test) - 2, len(test) - 1, len(test)], [test[0], test[1], test[2]],
        axis=0)
    test1 = np.insert(
        test1, [len(test1) - 2, len(test1) - 1,
                len(test1)], [test1[0], test1[1], test1[2]],
        axis=0)
    test2 = np.insert(
        test2, [len(test2) - 2, len(test2) - 1,
                len(test2)], [test2[0], test2[1], test2[2]],
        axis=0)
    test3 = np.insert(
        test3, [len(test3) - 2, len(test3) - 1,
                len(test3)], [test3[0], test3[1], test3[2]],
        axis=0)
    test = np.insert(
        test4, [len(test4) - 2, len(test4) - 1,
                len(test4)], [test4[0], test4[1], test4[2]],
        axis=0)

    # print(test)
    # print(test1)
    # print(test2)
    # print(test3)

    X = np.row_stack((test, test1, test2, test3, test4))
    print(X)
    # np.random.shuffle(X)

    np.savetxt("cpts_test.csv", X, delimiter=",")
    # np.savetxt("cpts_test1.csv", test1, delimiter=",")
    fig = plt.figure()
    ax = plt.axes(projection="3d")
    ax.scatter3D(test[:, 0],
                 test[:, 1],
                 np.ones(len(test[:, 2])) * ranges[0],
                 color='red')
    ax.scatter3D(test1[:, 0], test1[:, 1], test1[:, 2], color='blue')
    ax.scatter3D(test2[:, 0], test2[:, 1], test2[:, 2], color='green')
    ax.scatter3D(test3[:, 0], test3[:, 1], test3[:, 2], color='yellow')
    ax.scatter3D(test4[:, 0], test4[:, 1], test4[:, 2], color='purple')
    # ax.scatter3D(cylinder_cpts[:,0],cylinder_cpts[:,1],cylinder_cpts[:,2])
    # ax.scatter3D(cylinder_cpts1[:,0],cylinder_cpts1[:,1],cylinder_cpts1[:,2])

    # try fitting a NURBS Surface
    surf = NURBS.Surface()
    surf.delta = 0.03
    p_ctrlpts = exchange.import_csv("cpts_test.csv")
    print(len(p_ctrlpts))
    p_weights = np.ones((len(p_ctrlpts)))
    p_size_u = 9
    p_size_v = 5
    p_degree_u = 3
    p_degree_v = 3
    t_ctrlptsw = compat.combine_ctrlpts_weights(p_ctrlpts, p_weights)
    n_ctrlptsw = compat.flip_ctrlpts_u(t_ctrlptsw, p_size_u, p_size_v)
    n_knotvector_u = utils.generate_knot_vector(p_degree_u, p_size_u)
    n_knotvector_v = utils.generate_knot_vector(p_degree_v, p_size_v)

    surf.degree_u = p_degree_u
    surf.degree_v = p_degree_v
    surf.set_ctrlpts(n_ctrlptsw, p_size_u, p_size_v)
    surf.knotvector_u = n_knotvector_u
    surf.knotvector_v = n_knotvector_v
    vis_config = vis.VisConfig(ctrlpts=True, axes=True, legend=True)
    surf.vis = vis.VisSurface(vis_config)
    surf.evaluate()
    surf.render()
Ejemplo n.º 17
0
def test_bspline_surface_degree_u():
    surf = NURBS.Surface()
    surf.degree_u = 7

    # Check assignment
    assert surf.degree_u == 7
Ejemplo n.º 18
0
        def process(self):
            vertices_s = self.inputs['ControlPoints'].sv_get()
            has_weights = self.inputs['Weights'].is_linked
            weights_s = self.inputs['Weights'].sv_get(default=[[1.0]])
            samples_s = self.inputs['Samples'].sv_get()
            u_size_s = self.inputs['USize'].sv_get()
            knots_u_s = self.inputs['KnotsU'].sv_get(default=[[]])
            knots_v_s = self.inputs['KnotsV'].sv_get(default=[[]])
            degree_u_s = self.inputs['DegreeU'].sv_get()
            degree_v_s = self.inputs['DegreeV'].sv_get()

            if self.input_mode == '1D':
                vertices_s = ensure_nesting_level(vertices_s, 3)
            else:
                vertices_s = ensure_nesting_level(vertices_s, 4)

            def convert_row(verts_row, weights_row):
                return [(x, y, z, w)
                        for (x, y, z), w in zip(verts_row, weights_row)]

            verts_out = []
            edges_out = []
            faces_out = []
            surfaces_out = []
            inputs = zip_long_repeat(vertices_s, weights_s, knots_u_s,
                                     knots_v_s, degree_u_s, degree_v_s,
                                     samples_s, u_size_s)
            for vertices, weights, knots_u, knots_v, degree_u, degree_v, samples, u_size in inputs:
                if isinstance(samples, (list, tuple)):
                    samples = samples[0]
                if isinstance(degree_u, (tuple, list)):
                    degree_u = degree_u[0]
                if isinstance(degree_v, (tuple, list)):
                    degree_v = degree_v[0]
                if isinstance(u_size, (list, tuple)):
                    u_size = u_size[0]
                if self.input_mode == '1D':
                    fullList(weights, len(vertices))
                else:
                    if isinstance(weights[0], (int, float)):
                        weights = [weights]
                    fullList(weights, len(vertices))
                    for verts_u, weights_u in zip(vertices, weights):
                        fullList(weights_u, len(verts_u))

                # Generate surface
                if self.surface_mode == 'NURBS':
                    surf = NURBS.Surface(normalize_kv=self.normalize_knots)
                else:  # BSPLINE
                    surf = BSpline.Surface(normalize_kv=self.normalize_knots)
                surf.degree_u = degree_u
                surf.degree_v = degree_v

                if self.input_mode == '1D':
                    n_v = u_size
                    n_u = len(vertices) // n_v

                    vertices = grouper(vertices, n_u)
                    weights = grouper(weights, n_u)

                if self.knot_mode == 'AUTO':
                    if self.is_cyclic_v:
                        for row_idx in range(len(vertices)):
                            vertices[row_idx].extend(
                                vertices[row_idx][:degree_v + 1])
                            weights[row_idx].extend(
                                weights[row_idx][:degree_v + 1])
                    if self.is_cyclic_u:
                        vertices.extend(vertices[:degree_u + 1])
                        weights.extend(weights[:degree_u + 1])
                    self.debug("UxV: %s x %s", len(vertices), len(vertices[0]))

                # Control points
                if self.surface_mode == 'NURBS':
                    ctrlpts = list(map(convert_row, vertices, weights))
                    surf.ctrlpts2d = ctrlpts
                else:
                    surf.ctrlpts2d = vertices
                n_u_total = len(vertices)
                n_v_total = len(vertices[0])

                if self.knot_mode == 'AUTO':
                    if self.is_cyclic_u:
                        knots_u = list(range(n_u_total + degree_u + 1))
                    else:
                        knots_u = knotvector.generate(surf.degree_u, n_u_total)
                    self.debug("Auto knots U: %s", knots_u)
                    surf.knotvector_u = knots_u

                    if self.is_cyclic_v:
                        knots_v = list(range(n_v_total + degree_v + 1))
                    else:
                        knots_v = knotvector.generate(surf.degree_v, n_v_total)
                    self.debug("Auto knots V: %s", knots_v)
                    surf.knotvector_v = knots_v
                else:
                    surf.knotvector_u = knots_u
                    surf.knotvector_v = knots_v

                new_surf = SvExGeomdlSurface(surf)
                if self.is_cyclic_u:
                    u_min = surf.knotvector_u[degree_u]
                    u_max = surf.knotvector_u[-degree_u - 2]
                    new_surf.u_bounds = u_min, u_max
                    print("U:", new_surf.u_bounds)
                else:
                    u_min = min(surf.knotvector_u)
                    u_max = max(surf.knotvector_u)
                    new_surf.u_bounds = u_min, u_max
                if self.is_cyclic_v:
                    v_min = surf.knotvector_v[degree_v]
                    v_max = surf.knotvector_v[-degree_v - 2]
                    new_surf.v_bounds = v_min, v_max
                    print("V:", new_surf.v_bounds)
                else:
                    v_min = min(surf.knotvector_v)
                    v_max = max(surf.knotvector_v)
                    new_surf.v_bounds = v_min, v_max
                surfaces_out.append(new_surf)

                if self.make_grid:
                    surf.sample_size = samples
                    surf.tessellate()
                    new_verts = [vert.data for vert in surf.vertices]
                    new_faces = [f.data for f in surf.faces]
                else:
                    new_verts = []
                    new_faces = []
                verts_out.append(new_verts)
                faces_out.append(new_faces)

            if self.make_grid:
                self.outputs['Vertices'].sv_set(verts_out)
                self.outputs['Faces'].sv_set(faces_out)
            self.outputs['Surface'].sv_set(surfaces_out)
Ejemplo n.º 19
0
def test_nurbs_surface_degree_v():
    surf = NURBS.Surface()
    surf.degree_v = 4

    # Check assignment
    assert surf.degree_v == 4