File: bezier4p_interpolation.py

package info (click to toggle)
ezdxf 1.4.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 104,528 kB
  • sloc: python: 182,341; makefile: 116; lisp: 20; ansic: 4
file content (43 lines) | stat: -rw-r--r-- 1,225 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# Copyright (c) 2020-2022, Manfred Moitzi
# License: MIT License
import time
import ezdxf
import pathlib
import math
from ezdxf.math import cubic_bezier_interpolation, BoundingBox
from ezdxf.render import random_3d_path

CWD = pathlib.Path("~/Desktop/Outbox").expanduser()
if not CWD.exists():
    CWD = pathlib.Path(".")


def profile_bezier_interpolation(count, path):
    for _ in range(count):
        curves = list(cubic_bezier_interpolation(path))


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})
    for curve in cubic_bezier_interpolation(path):
        msp.add_polyline3d(
            curve.approximate(20), dxfattribs={"layer": "Bézier", "color": 1}
        )
    doc.set_modelspace_vport(center=bbox.center, height=bbox.size[1])
    doc.saveas(CWD / "path1.dxf")


path = list(random_3d_path(100, max_step_size=10, max_heading=math.pi * 0.8))
export_path(path)

profile("Cubic Bézier interpolation: ", profile_bezier_interpolation, 100, path)