forked from mozman/ezdxf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbspline.py
177 lines (140 loc) · 4.48 KB
/
bspline.py
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# Copyright (c) 2020-2024, Manfred Moitzi
# License: MIT License
import sys
import time
from datetime import datetime
from pathlib import Path
import numpy as np
from ezdxf.acc import USE_C_EXT
from ezdxf.version import __version__
# Python implementations:
from ezdxf.math._bspline import Basis, Evaluator
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
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):
return self.evaluator.derivative(u, n)
def derivatives(self, t, n):
return self.evaluator.derivatives(t, n)
class CySpline(PySpline):
def __init__(self, bspline, weights=None):
self.basis = CBasis(
bspline.knots(), bspline.order, bspline.count, weights
)
self.evaluator = CEvaluator(self.basis, bspline.control_points)
def open_log(name: str):
parent = Path(__file__).parent
p = parent / "logs" / Path(name + ".csv")
if not p.exists():
with open(p, mode="wt") as fp:
fp.write(
'"timestamp"; "pytime"; "cytime"; '
'"python_version"; "ezdxf_version"\n'
)
log_file = open(p, mode="at")
return log_file
def log(name: str, pytime: float, cytime: float):
log_file = open_log(name)
timestamp = datetime.now().isoformat()
py_version = sys.version.replace("\n", " ")
log_file.write(
f'{timestamp}; {pytime}; {cytime}; "{py_version}"; "{__version__}"\n'
)
log_file.close()
def bspline_points(cls, count):
for curve in splines:
spline = cls(curve)
for u in np.linspace(0, spline.basis.max_t, count):
spline.point(u)
def bspline_multi_points(cls, count):
for curve in splines:
spline = cls(curve)
list(spline.points(np.linspace(0, spline.basis.max_t, count)))
def bspline_derivative(cls, count):
for curve in splines:
spline = cls(curve)
for u in np.linspace(0, spline.basis.max_t, count):
spline.derivative(u, 1)
def bspline_multi_derivative(cls, count):
for curve in splines:
spline = cls(curve)
list(spline.derivatives(np.linspace(0, spline.basis.max_t, count), 1))
def bspline_points_rational(cls, count):
for curve in splines:
weights = [1.0] * curve.count
spline = cls(curve, weights)
for u in np.linspace(0, spline.basis.max_t, count):
spline.point(u)
def profile1(func, *args) -> float:
t0 = time.perf_counter()
func(*args)
t1 = time.perf_counter()
return t1 - t0
def profile(text, func, pytype, cytype, *args):
pytime = profile1(func, pytype, *args)
cytime = profile1(func, cytype, *args)
ratio = pytime / cytime
print(f"Python - {text} {pytime:.3f}s")
print(f"Cython - {text} {cytime:.3f}s")
print(f"Ratio {ratio:.1f}x")
log(func.__name__, pytime, cytime)
POINT_COUNT_1 = 10_000
print(f"Profiling BSpline Python and Cython implementation:")
profile(
f"calc {POINT_COUNT_1}x single point for {SPLINE_COUNT} BSplines: ",
bspline_points,
PySpline,
CySpline,
POINT_COUNT_1,
)
profile(
f"calc {POINT_COUNT_1}x single point for {SPLINE_COUNT} rational BSplines: ",
bspline_points_rational,
PySpline,
CySpline,
POINT_COUNT_1,
)
profile(
f"calc {POINT_COUNT_1}x multi point for {SPLINE_COUNT} BSplines: ",
bspline_multi_points,
PySpline,
CySpline,
POINT_COUNT_1,
)
profile(
f"calc {POINT_COUNT_1}x single point & derivative for {SPLINE_COUNT} BSplines: ",
bspline_derivative,
PySpline,
CySpline,
POINT_COUNT_1,
)
profile(
f"calc {POINT_COUNT_1}x multi point & derivative for {SPLINE_COUNT} BSplines: ",
bspline_multi_derivative,
PySpline,
CySpline,
POINT_COUNT_1,
)