def get_geometry(self): if self.mode == 'CURVE': curves = self.inputs['Curves'].sv_get() if isinstance(curves[0], (list, tuple)): curves = sum(curves, []) container = multi.CurveContainer() for i, curve in enumerate(curves): if not isinstance(curve, SvNurbsCurve): if hasattr(curve, 'to_nurbs'): curve = curve.to_nurbs( implementation=SvNurbsCurve.GEOMDL) else: raise TypeError( "Provided object #%s is not a NURBS curve, but %s!" % (i, type(curve))) container.append(SvGeomdlCurve.from_any_nurbs(curve).curve) return container else: # SURFACE surfaces = self.inputs['Surfaces'].sv_get() if isinstance(surfaces[0], (list, tuple)): surfaces = sum(surfaces, []) container = multi.SurfaceContainer() for i, surface in enumerate(surfaces): if not isinstance(surface, SvNurbsSurface): if hasattr(surface, 'to_nurbs'): surface = surface.to_nurbs( implementation=SvNurbsCurve.GEOMDL) else: raise TypeError( "Provided object #%s is not a NURBS surface, but %s!" % (i, type(surface))) container.append( SvGeomdlSurface.from_any_nurbs(surface).surface) return container
def test_deriv_curve_fig(bspline_curve2d): fname = "test-derivative_curve.png" data = operations.derivative_curve(bspline_curve2d) multi_shape = multi.CurveContainer(data) multi_shape.vis = VisMPL.VisCurve2D() multi_shape.render(filename=fname, plot=False) assert os.path.isfile(fname) assert os.path.getsize(fname) > 0 # Clean up temporary file if exists if os.path.isfile(fname): os.remove(fname)
def test_curve3d_multi_fig_nowindow(bspline_curve3d): conf = VisMPL.VisConfig() vis = VisMPL.VisCurve3D(config=conf) data = operations.decompose_curve(bspline_curve3d) multi_shape = multi.CurveContainer(data) multi_shape.vis = vis multi_shape.render(plot=False) assert os.path.isfile(conf.figure_image_filename) assert os.path.getsize(conf.figure_image_filename) > 0 # Clean up temporary file if exists if os.path.isfile(conf.figure_image_filename): os.remove(conf.figure_image_filename)
def test_curve3d_multi_fig_save(bspline_curve3d): conf = VisMPL.VisConfig() vis = VisMPL.VisCurve3D(config=conf) fname = "test-multi_curve.png" data = operations.decompose_curve(bspline_curve3d) multi_shape = multi.CurveContainer(data) multi_shape.vis = vis multi_shape.render(filename=fname, plot=False) assert os.path.isfile(fname) assert os.path.getsize(fname) > 0 # Clean up temporary file if exists if os.path.isfile(fname): os.remove(fname)
def get_geometry(self): if self.mode == 'CURVE': curves = self.inputs['Curves'].sv_get() if isinstance(curves[0], (list, tuple)): curves = sum(curves, []) container = multi.CurveContainer() for i, curve in enumerate(curves): if not isinstance(curve, SvExGeomdlCurve): raise TypeError("Provided object #%s is not a NURBS curve, but %s!" % (i, type(curve))) container.append(curve.curve) return container else: # SURFACE surfaces = self.inputs['Surfaces'].sv_get() if isinstance(surfaces[0], (list, tuple)): surfaces = sum(surfaces, []) container = multi.SurfaceContainer() for i, surface in enumerate(surfaces): if not isinstance(surface, SvExGeomdlSurface): raise TypeError("Provided object #%s is not a NURBS surface, but %s!" % (i, type(surface))) container.append(surface.surface) return container
def generate_nurbs_from_file(file_name, delta, shape_idx, file_type=''): """Generates NURBS objects from supported file formats""" # Fix input types delta = float(delta) shape_idx = int(shape_idx) # Try to find file type from its extension if not file_type: fname, fext = os.path.splitext(file_name) file_type = fext[1:] ftype = file_type.lower() if ftype in CLI_FILE_IMPORT_TYPES: # Build NURBS object nurbs_objs = CLI_FILE_IMPORT_TYPES[ftype](file_name, delta=delta, jinja2=True) # Return the shape if len(nurbs_objs) == 1: return nurbs_objs[0] if isinstance(nurbs_objs[0], NURBS.Curve): result = multi.CurveContainer(nurbs_objs) elif isinstance(nurbs_objs[0], NURBS.Surface): result = multi.SurfaceContainer(nurbs_objs) else: result = multi.VolumeContainer(nurbs_objs) # Set the delta for multi shape objects if 0.0 < delta < 1.0: result.delta = delta if shape_idx >= 0: return result[shape_idx] return result else: raise RuntimeError("The input file type '" + str(file_type) + "' is not supported")
# Fix file path os.chdir(os.path.dirname(os.path.realpath(__file__))) # Create a BSpline curve instance curve = BSpline.Curve() # Set degree curve.degree = 3 # Read control points from a file curve.ctrlpts = exchange.import_txt("ex_curve03.cpt") # Set knot vector curve.knotvector = [0, 0, 0, 0, 0.25, 0.25, 0.5, 0.75, 0.75, 1, 1, 1, 1] # Decompose the curve curve_list = operations.decompose_curve(curve) curve_decomposed = multi.CurveContainer(curve_list) # Set sample size for the decomposed curve curve_decomposed.sample_size = 25 # Plot the decomposed curve vis_comp = vis.VisCurve2D() curve_decomposed.vis = vis_comp curve_decomposed.render() # Good to have something here to put a breakpoint pass
from geomdl import operations from geomdl import multi from geomdl.visualization import VisMPL # Fix file path os.chdir(os.path.dirname(os.path.realpath(__file__))) # Create a B-Spline curve instance curve = BSpline.Curve() # Set up the curve curve.degree = 4 curve.ctrlpts = exchange.import_txt("ex_curve01.cpt") # Auto-generate knot vector curve.knotvector = utilities.generate_knot_vector(curve.degree, len(curve.ctrlpts)) # Decompose the curve into Bezier curve segments bezier_curves = operations.decompose_curve(curve) bezier = multi.CurveContainer(bezier_curves) # Plot the curves using the curve container bezier.sample_size = 40 vis_comp = VisMPL.VisCurve2D() bezier.vis = vis_comp bezier.render() # Good to have something here to put a breakpoint pass
def get_cm_from_interpolation_length(pts, interpolation_factor, center_circle, radius): # 0 is circ center = center_circle cm = multi.CurveContainer() cm.vis = VisMPL.VisCurve2D(axes=False, labels=False, ctrlpts=False, legend=False) circle_length = 2 * math.pi * radius tangents = [] curve_lengths = [] for i in range(len(pts)): p1 = pts[(i - 1) % len(pts)] p2 = pts[(i + 1) % len(pts)] pr = (p2[0] - p1[0], p2[1] - p1[1]) alpha = math.atan2(pr[1], pr[0]) % math.pi tangents.append(alpha) dists = [] for i in range(len(pts)): dist = functions.euclid_dist(pts[i], pts[(i + 1) % len(pts)]) d = dist * 0.25 dists.append(d) for i in range(len(pts)): c = BSpline.Curve() c.degree = 3 d = dists[i] p1, p2 = get_points_on_tangent(pts[i], tangents[i], d) if functions.euclid_dist( p1, pts[(i + 1) % len(pts)]) < functions.euclid_dist( p2, pts[(i + 1) % len(pts)]): p2, p1 = p1, p2 p3, p4 = get_points_on_tangent(pts[(i + 1) % len(pts)], tangents[(i + 1) % len(pts)], d) if functions.euclid_dist(p4, pts[i]) < functions.euclid_dist( p3, pts[i]): p4, p3 = p3, p4 c.ctrlpts = [pts[i], p2, p3, pts[(i + 1) % len(pts)]] c.knotvector = [0, 0, 0, 0, 1, 1, 1, 1] length = get_curve_length(c) curve_lengths.append(length) cm.add(c) curve_lengths.reverse() xc, yc = center_circle pts_r = list(pts) pts_r.reverse() distance_factor = circle_length / sum(curve_lengths) pts_interpolated = [] starting_point = functions.project_to_circle(pts[0], center_circle, radius) pts_interpolated.append(starting_point) starting_angle = math.atan2(starting_point[1] - yc, starting_point[0] - xc) alpha = curve_lengths[0] * distance_factor / radius xp = xc + radius * math.cos(alpha) yp = yc + radius * math.sin(alpha) angle_sum = starting_angle for i in range(len(pts) - 1): alpha = curve_lengths[i] * distance_factor / radius angle_sum += alpha xp = xc + radius * math.cos(angle_sum) yp = yc + radius * math.sin(angle_sum) pts_interpolated.append((xp, yp)) pts_interpolated.reverse() pts_interpolated.insert(0, pts_interpolated.pop()) tangents_interpolated = [] for i in range(len(pts_interpolated)): x, y = pts_interpolated[i] alpha = math.atan2(y - center_circle[1], x - center_circle[0]) alpha -= 0.5 * math.pi alpha = alpha % math.pi tangents_interpolated.append(alpha) dists_interpolated = [] for i in range(len(pts_interpolated)): x1, y1 = pts_interpolated[i] x4, y4 = pts_interpolated[(i + 1) % len(pts_interpolated)] xc, yc = center_circle axx = x1 - xc ay = y1 - yc bx = x4 - xc by = y4 - yc q1 = axx * axx + ay * ay q2 = q1 + axx * bx + ay * by k2 = 4 / 3 * (math.sqrt(2 * q1 * q2) - q2) / (axx * by - ay * bx) d = math.sqrt((k2 * ay)**2 + (k2 * axx)**2) dists_interpolated.append(d) cm = multi.CurveContainer() cm.vis = VisMPL.VisCurve2D(axes=False, labels=False, ctrlpts=False, legend=False) pts = [[ interpolation_factor * pp[0] + (1 - interpolation_factor) * pc[0], interpolation_factor * pp[1] + (1 - interpolation_factor) * pc[1] ] for (pp, pc) in zip(pts, pts_interpolated)] new_tangents = [] for tp, tc in zip(tangents, tangents_interpolated): if abs(tp - tc) > math.pi / 2: if tp < math.pi / 2: t = interpolation_factor * (tp + math.pi) + ( 1 - interpolation_factor) * tc elif tc < math.pi / 2: t = interpolation_factor * tp + (1 - interpolation_factor) * ( tc + math.pi) else: t = interpolation_factor * tp + (1 - interpolation_factor) * tc new_tangents.append(t) tangents = new_tangents dists = [ interpolation_factor * dp + (1 - interpolation_factor) * dc for (dp, dc) in zip(dists, dists_interpolated) ] angle_mapping = {} ctrlpts = [] for i in range(len(pts)): c = BSpline.Curve() c.degree = 3 d = dists[i] p1, p2 = get_points_on_tangent(pts[i], tangents[i], d) if i == 0: print(tangents[i]) if p1[1] < p2[1] and tangents[i] < math.pi: p1, p2 = p2, p1 elif p1[1] > p2[1] and tangents[i] > math.pi: p1, p2 = p2, p1 else: if math.atan2(p1[1] - center[1], p1[0] - center[0]) < math.atan2( p2[1] - center[1], p2[0] - center[0]): p1, p2 = p2, p1 if functions.euclid_dist( p1, pts[(i + 1) % len(pts)]) < functions.euclid_dist( p2, pts[(i + 1) % len(pts)]): p2, p1 = p1, p2 xp, yp = pts[i] if (xp, yp) in angle_mapping: p = angle_mapping[(xp, yp)] if p[0] > xp and p2[0] > xp or p[0] < xp and p2[0] < xp: p1, p2 = p2, p1 angle_mapping[(xp, yp)] = p2 p3, p4 = get_points_on_tangent(pts[(i + 1) % len(pts)], tangents[(i + 1) % len(pts)], d) if functions.euclid_dist(p4, pts[i]) < functions.euclid_dist( p3, pts[i]): p4, p3 = p3, p4 xp, yp = pts[(i + 1) % len(pts)] angle_mapping[(xp, yp)] = p3 c.ctrlpts = [pts[i], p2, p3, pts[(i + 1) % len(pts)]] c.knotvector = [0, 0, 0, 0, 1, 1, 1, 1] cm.add(c) return cm
def get_cm_from_interpolation_radial(pts, interpolation_factor): # 0 is circ center_circle = (200, 540) center_polygon = (223, 555) center = center_circle[0] * ( 1 - interpolation_factor ) + center_polygon[0] * interpolation_factor, center_circle[1] * ( 1 - interpolation_factor) + center_polygon[1] * interpolation_factor radius = 70 tangents = [] for i in range(len(pts)): p1 = pts[(i - 1) % len(pts)] p2 = pts[(i + 1) % len(pts)] pr = (p2[0] - p1[0], p2[1] - p1[1]) alpha = math.atan2(pr[1], pr[0]) tangents.append(alpha) dists = [] for i in range(len(pts)): dist = functions.euclid_dist(pts[i], pts[(i + 1) % len(pts)]) d = dist * 0.3 dists.append(d) pts_interpolated = [ functions.project_to_circle(p, center_circle, radius) for p in pts ] tangents_interpolated = [] for i in range(len(pts)): x, y = pts[i] alpha = math.atan2(y - center_circle[1], x - center_circle[0]) alpha -= 0.5 * math.pi alpha = alpha % math.pi tangents_interpolated.append(alpha) dists_interpolated = [] for i in range(len(pts_interpolated)): x1, y1 = pts_interpolated[i] x4, y4 = pts_interpolated[(i + 1) % len(pts_interpolated)] xc, yc = center_circle axx = x1 - xc ay = y1 - yc bx = x4 - xc by = y4 - yc q1 = axx * axx + ay * ay q2 = q1 + axx * bx + ay * by k2 = 4 / 3 * (math.sqrt(2 * q1 * q2) - q2) / (axx * by - ay * bx) d = math.sqrt((k2 * ay)**2 + (k2 * axx)**2) dists_interpolated.append(d) cm = multi.CurveContainer() cm.vis = VisMPL.VisCurve2D(axes=False, labels=False, ctrlpts=False, legend=False) pts = [[ interpolation_factor * pp[0] + (1 - interpolation_factor) * pc[0], interpolation_factor * pp[1] + (1 - interpolation_factor) * pc[1] ] for (pp, pc) in zip(pts, pts_interpolated)] new_tangents = [] for tp, tc in zip(tangents, tangents_interpolated): if abs(tp - tc) > math.pi / 2: if tp < math.pi / 2: t = interpolation_factor * (tp + math.pi) + ( 1 - interpolation_factor) * tc elif tc < math.pi / 2: t = interpolation_factor * tp + (1 - interpolation_factor) * ( tc + math.pi) else: t = interpolation_factor * tp + (1 - interpolation_factor) * tc new_tangents.append(t) tangents = new_tangents dists = [ interpolation_factor * dp + (1 - interpolation_factor) * dc for (dp, dc) in zip(dists, dists_interpolated) ] ctrlpts = [] for i in range(len(pts)): c = BSpline.Curve() c.degree = 3 d = dists[i] p1, p2 = get_points_on_tangent(pts[i], tangents[i], d) if math.atan2(p1[1] - center[1], p1[0] - center[0]) < math.atan2( p2[1] - center[1], p2[0] - center[0]): p1, p2 = p2, p1 if functions.euclid_dist( p1, pts[(i + 1) % len(pts)]) < functions.euclid_dist( p2, pts[(i + 1) % len(pts)]): p2, p1 = p1, p2 p3, p4 = get_points_on_tangent(pts[(i + 1) % len(pts)], tangents[(i + 1) % len(pts)], d) if functions.euclid_dist(p4, pts[i]) < functions.euclid_dist( p3, pts[i]): p4, p3 = p3, p4 c.ctrlpts = [pts[i], p2, p3, pts[(i + 1) % len(pts)]] c.knotvector = [0, 0, 0, 0, 1, 1, 1, 1] cm.add(c) return cm
bx = x4 - xc by = y4 - yc q1 = ax * ax + ay * ay q2 = q1 + ax * bx + ay * by k2 = 4 / 3 * (math.sqrt(2 * q1 * q2) - q2) / (ax * by - ay * bx) x2 = xc + ax - k2 * ay y2 = yc + ay + k2 * ax x3 = xc + bx + k2 * by y3 = yc + by - k2 * bx return 0, (x2, y2), (x3, y3) curve = NURBS.Curve() mcrv_circle = multi.CurveContainer() mcrv_circle.vis = VisMPL.VisCurve2D(axes=False, labels=False, ctrlpts=False, legend=False) center = (317, 465) #pts = [[204, 604], [284, 612], [387, 577], [485, 475], [430, 366], [325, 231], [274, 438], [193, 436], [150, 525], [204, 604], [284, 612], [387, 577]] pts = [[204, 604], [284, 612], [387, 577], [485, 475], [430, 366], [325, 231], [274, 438], [193, 436], [150, 525], [204, 604]] pts_c = [[204, 604], [284, 612], [387, 577], [485, 475], [430, 366], [325, 231], [274, 438], [193, 436], [150, 525], [204, 604], [284, 612], [387, 577]] #pts = [[204, 604], [284, 612], [430, 366], [274, 438], [193, 436], [204, 604]] #pts_c = [[204, 604], [284, 612], [430, 366], [274, 438], [193, 436], [204, 604], [284, 612], [430, 366]] #radius = max([functions.euclid_dist(center, p) for p in pts])
curve = BSpline.Curve() # Set up the curve curve.degree = 3 curve.ctrlpts = exchange.import_txt("ex_curve02.cpt") # Auto-generate knot vector curve.knotvector = utilities.generate_knot_vector(curve.degree, len(curve.ctrlpts)) # Split the curve curve1, curve2 = operations.split_curve(curve, 0.2) # Move the 1st curve a little bit far away from the 2nd curve c2tan = curve2.tangent(0.0, normalize=True) c2tanvec = [-1 * p for p in c2tan[1]] operations.translate(curve1, c2tanvec, inplace=True) # Plot the curves using the curve container curves = multi.CurveContainer() curves.sample_size = 40 curves.add(curve1) curves.add(curve2) vis_comp = VisMPL.VisCurve2D() curves.vis = vis_comp curves.render() # Good to have something here to put a breakpoint pass
q1 = ax * ax + ay * ay q2 = q1 + ax * bx + ay * by k2 = 4 / 3 * (math.sqrt(2 * q1 * q2) - q2) / (ax * by - ay * bx) x2 = xc + ax - k2 * ay y2 = yc + ay + k2 * ax x3 = xc + bx + k2 * by y3 = yc + by - k2 * bx return 0, (x2, y2), (x3, y3) #pts = [[204, 604], [284, 612], [387, 577], [485, 475], [430, 366], [325, 231], [274, 438], [193, 436], [150, 525], [204, 604], [284, 612], [387, 577]] pts = [[485, 475], [430, 366], [325, 231], [274, 438], [193, 436], [150, 525], [204, 604], [284, 612], [387, 577]] cm = multi.CurveContainer() cm.vis = VisMPL.VisCurve2D(axes=False, labels=False, ctrlpts=True, legend=False) x = [p[0] for p in pts] y = [p[1] for p in pts] # append the starting x,y coordinates x = np.r_[x, x[0]] y = np.r_[y, y[0]] # fit splines to x=f(u) and y=g(u), treating both as periodic. also note that s=0 # is needed in order to force the spline fit to pass through all the input points. tck, u = interpolate.splprep([x, y], s=0, per=True)
os.chdir(os.path.dirname(os.path.realpath(__file__))) # Create a B-Spline curve instance curve = BSpline.Curve() # Set up curve curve.degree = 4 curve.ctrlpts = exchange.import_txt("ex_curve3d01.cpt") # Auto-generate knot vector curve.knotvector = utilities.generate_knot_vector(curve.degree, len(curve.ctrlpts)) # Split the curve split_curves = operations.split_curve(curve, 0.3) curves = multi.CurveContainer(split_curves) # Move the 1st curve a little bit far away from the 2nd curve c2tan = operations.tangent(curves[1], 0.0, normalize=True) c2tanvec = [-1 * p for p in c2tan[1]] operations.translate(curves[0], c2tanvec, inplace=True) # Plot the curves using the curve container curves.sample_size = 100 # Plot the control point polygon and the evaluated curve vis_comp = VisPlotly.VisCurve3D() curves.vis = vis_comp curves.render() # Good to have something here to put a breakpoint
from geomdl.shapes import curve2d from geomdl import operations from geomdl import multi from geomdl.visualization import VisMPL # Generate a NURBS full circle from 7 control points circle = curve2d.full_circle2(radius=5.0) circle.sample_size = 75 # Render the circle and the control points polygon vis_config = VisMPL.VisConfig(ctrlpts=True, figure_size=[9, 8]) vis_comp = VisMPL.VisCurve2D(config=vis_config) circle.vis = vis_comp circle.render() # Decompose the circle into Bezier curve segments segments = operations.decompose_curve(circle) bezier_segments = multi.CurveContainer(segments) # Set sample size (delta) bezier_segments.sample_size = 25 # Render the Bezier curve segments and their control points polygons vis_config = VisMPL.VisConfig(ctrlpts=True, figure_size=[9, 8]) vis_comp = VisMPL.VisCurve2D(config=vis_config) bezier_segments.vis = vis_comp bezier_segments.render() # Good to have something here to put a breakpoint pass
# Fix file path os.chdir(os.path.dirname(os.path.realpath(__file__))) # Create a NURBS curve instance (full circle) curve = NURBS.Curve() # Set up the curve curve.degree = 2 curve.ctrlptsw = exchange.import_txt("ex_curve04.cptw") # Use a specialized knot vector curve.knotvector = [0, 0, 0, 0.25, 0.25, 0.5, 0.5, 0.75, 0.75, 1, 1, 1] # Decompose the curve into Bezier curve segments curve_list = operations.decompose_curve(curve) bezier = multi.CurveContainer(curve_list) # Set sample size of the Bezier curves bezier.sample_size = 25 # Plot the Bezier curves vis_config = VisMPL.VisConfig(figure_size=[8, 8]) vis_comp = VisMPL.VisCurve2D(vis_config) bezier.vis = vis_comp bezier.render() # Good to have something here to put a breakpoint pass
# Fix file path os.chdir(os.path.dirname(os.path.realpath(__file__))) # Create a B-Spline curve instance curve = BSpline.Curve() # Set up the curve curve.degree = 4 curve.ctrlpts = exchange.import_txt("ex_curve01.cpt") # Auto-generate knot vector curve.knotvector = utilities.generate_knot_vector(curve.degree, len(curve.ctrlpts)) # Split the curve curve_list = operations.split_curve(curve, 0.6) curves = multi.CurveContainer(curve_list) # Move the 1st curve a little bit far away from the 2nd curve c2tan = curves[1].tangent(0.0, normalize=True) c2tanvec = [-3 * p for p in c2tan[1]] operations.translate(curves[0], c2tanvec, inplace=True) # Plot the curves using the curve container curves.sample_size = 40 # Plot the curve vis_comp = VisMPL.VisCurve2D() curves.vis = vis_comp curves.render() # Good to have something here to put a breakpoint
crv1.knotvector = knotvector.generate(crv1.degree, crv1.ctrlpts_size) # Create the curve instance #2 crv2 = BSpline.Curve() # Set degree crv2.degree = 3 # Set control points crv2.ctrlpts = [[1, 0, 0], [1, 1, 0], [2, 1, 0], [1, 1, 0]] # Generate a uniform knot vector crv2.knotvector = knotvector.generate(crv2.degree, crv2.ctrlpts_size) # Create a curve container mcrv = multi.CurveContainer(crv1, crv2) # Import Matplotlib visualization module from geomdl.visualization import VisMPL # Set the visualization component of the curve container mcrv.vis = VisMPL.VisCurve3D() # Plot the curves in the curve container mcrv.render() ''' Convert non-rational to rational curve Generates a B-Spline (non-rational) curve and converts it into a NURBS (rational) curve. '''