From 6eb732b923b390c381eca64f5f07d2dee428784e Mon Sep 17 00:00:00 2001 From: LeanderSilur Date: Tue, 1 Sep 2020 11:48:39 +0200 Subject: [PATCH 1/3] Performance Improvement and Robustness Added performance improvements for the magnitude. This [post](https://stackoverflow.com/a/12712725) says: %timeit np.apply_along_axis(np.linalg.norm, 1, vectors) # Output: 100 loops, best of 3: 2.39 ms per loop %timeit np.sqrt((vectors ** 2).sum(-1))[..., np.newaxis] # Output: 10000 loops, best of 3: 13.8 us per loop Normalize method now won't fail, if the input has no length. --- fitCurves.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/fitCurves.py b/fitCurves.py index e19d08e..2369da9 100644 --- a/fitCurves.py +++ b/fitCurves.py @@ -6,6 +6,7 @@ from __future__ import print_function from numpy import * import bezier +import sys # Fit one (ore more) Bezier curves to a set of points @@ -148,7 +149,7 @@ def computeMaxError(points, bez, parameters): maxDist = 0.0 splitPoint = len(points)/2 for i, (point, u) in enumerate(zip(points, parameters)): - dist = linalg.norm(bezier.q(bez, u)-point)**2 + dist = ((bezier.q(bez, u) - point) ** 2).sum(-1) if dist > maxDist: maxDist = dist splitPoint = i @@ -157,5 +158,8 @@ def computeMaxError(points, bez, parameters): def normalize(v): - return v / linalg.norm(v) + magnitude = np.sqrt(v.dot(v)) + if magnitude < sys.float_info.epsilon: + return v + return v / magnitude From 25e4ebac00efc5c6803872f95fbcf733956585de Mon Sep 17 00:00:00 2001 From: LeanderSilur Date: Tue, 1 Sep 2020 12:13:20 +0200 Subject: [PATCH 2/3] fixed min --- fitCurves.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/fitCurves.py b/fitCurves.py index 2369da9..c563759 100644 --- a/fitCurves.py +++ b/fitCurves.py @@ -6,7 +6,6 @@ from __future__ import print_function from numpy import * import bezier -import sys # Fit one (ore more) Bezier curves to a set of points @@ -158,8 +157,7 @@ def computeMaxError(points, bez, parameters): def normalize(v): - magnitude = np.sqrt(v.dot(v)) - if magnitude < sys.float_info.epsilon: + magnitude = sqrt(v.dot(v)) + if magnitude < finfo(np.float).eps: return v return v / magnitude - From 6a3fb299f24ed1e441d1619318ccb3f81974a4e0 Mon Sep 17 00:00:00 2001 From: LeanderSilur Date: Wed, 2 Sep 2020 14:42:17 +0200 Subject: [PATCH 3/3] another typo --- fitCurves.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/fitCurves.py b/fitCurves.py index c563759..5e9391b 100644 --- a/fitCurves.py +++ b/fitCurves.py @@ -158,6 +158,6 @@ def computeMaxError(points, bez, parameters): def normalize(v): magnitude = sqrt(v.dot(v)) - if magnitude < finfo(np.float).eps: + if magnitude < finfo(float).eps: return v return v / magnitude