def profile(text, func, *args): t0 = time.perf_counter() func(*args) t1 = time.perf_counter() print(f'{text} {t1 - t0:.3f}s') def export_path(path): doc = ezdxf.new() msp = doc.modelspace() bbox = BoundingBox(path) msp.add_polyline3d(path, dxfattribs={'layer': 'Path', 'color': 2}) spline = msp.add_spline(dxfattribs={'layer': 'B-spline', 'color': 1}) curve = global_bspline_interpolation(path) spline.apply_construction_tool(curve) doc.set_modelspace_vport(center=bbox.center, height=bbox.size[1]) doc.saveas(DIR / 'path1.dxf') path = list(random_3d_path(100, max_step_size=10, max_heading=math.pi * 0.8)) export_path(path) profile('B-spline interpolation 300x: ', profile_bspline_interpolation, 300, path) spline = BSpline.from_fit_points(path, degree=3) profile('calculate 25x 1000 B-spline vertices: ', profile_vertex_calculation, 25, spline, 1000)
if USE_C_EXT is False: print("C-extension disabled or not available. (pypy3?)") print("Cython implementation == Python implementation.") CBasis = Basis CEvaluator = Evaluator else: # Cython implementations: from ezdxf.acc.bspline import Basis as CBasis, Evaluator as CEvaluator from ezdxf.render import random_3d_path from ezdxf.math import fit_points_to_cad_cv, linspace SPLINE_COUNT = 20 POINT_COUNT = 20 splines = [ fit_points_to_cad_cv(random_3d_path(POINT_COUNT)) for _ in range(SPLINE_COUNT) ] class PySpline: def __init__(self, bspline, weights=None): self.basis = Basis(bspline.knots(), bspline.order, bspline.count, weights) self.evaluator = Evaluator(self.basis, bspline.control_points) def point(self, u): return self.evaluator.point(u) def points(self, t): return self.evaluator.points(t)
if USE_C_EXT is False: print('C-extension disabled or not available. (pypy3?)') print('Cython implementation == Python implementation.') CBasis = Basis CEvaluator = Evaluator else: # Cython implementations: from ezdxf.acc.bspline import Basis as CBasis, Evaluator as CEvaluator from ezdxf.render import random_3d_path from ezdxf.math import fit_points_to_cad_cv, linspace SPLINE_COUNT = 20 POINT_COUNT = 20 splines = [ fit_points_to_cad_cv(random_3d_path(POINT_COUNT)) for _ in range(SPLINE_COUNT) ] class PySpline: def __init__(self, bspline, weights=None): self.basis = Basis(bspline.knots(), bspline.order, bspline.count, weights) self.evaluator = Evaluator(self.basis, bspline.control_points) def point(self, u): return self.evaluator.point(u) def points(self, t): return self.evaluator.points(t) def derivative(self, u, n):
def test_random_3d_path(): points = list(random_3d_path(steps=100)) assert len(points) == 100 assert len(set(points)) > 97