summaryrefslogtreecommitdiff
path: root/lib/utils
diff options
context:
space:
mode:
authorLex Neva <github.com@lexneva.name>2023-01-17 21:44:23 -0500
committerLex Neva <github.com@lexneva.name>2023-02-20 15:27:55 -0500
commit847e133f97d570e2967dfa7dcfc16a212dc2bbbc (patch)
treedf2fb20547bea95ad437155a1fac91290a9d07f3 /lib/utils
parente2965e78f03fb41c9a02c92ef120caf038b837ae (diff)
meander fill: more work
Diffstat (limited to 'lib/utils')
-rw-r--r--lib/utils/geometry.py12
-rw-r--r--lib/utils/list.py14
-rw-r--r--lib/utils/string.py7
3 files changed, 28 insertions, 5 deletions
diff --git a/lib/utils/geometry.py b/lib/utils/geometry.py
index 2903bc56..366a433f 100644
--- a/lib/utils/geometry.py
+++ b/lib/utils/geometry.py
@@ -166,7 +166,7 @@ def _remove_duplicate_coordinates(coords_array):
return coords_array[keepers]
-def smooth_path(path, smoothness=100.0):
+def smooth_path(path, smoothness=1.0):
"""Smooth a path of coordinates.
Arguments:
@@ -178,6 +178,11 @@ def smooth_path(path, smoothness=100.0):
A list of Points.
"""
+ if smoothness == 0:
+ # s of exactly zero seems to indicate a default level of smoothing
+ # in splprep, so we'll just exit instead.
+ return path
+
# splprep blows up on duplicated consecutive points with "Invalid inputs"
coords = _remove_duplicate_coordinates(np.array(path))
num_points = len(coords)
@@ -188,7 +193,7 @@ def smooth_path(path, smoothness=100.0):
# the smoothed path and the original path is equal to the smoothness.
# In practical terms, if smoothness is 1mm, then the smoothed path can be
# up to 1mm away from the original path.
- s = num_points * smoothness ** 2
+ s = num_points * (smoothness ** 2)
# .T transposes the array (for some reason splprep expects
# [[x1, x2, ...], [y1, y2, ...]]
@@ -280,6 +285,9 @@ class Point:
def rotate(self, angle):
return self.__class__(self.x * math.cos(angle) - self.y * math.sin(angle), self.y * math.cos(angle) + self.x * math.sin(angle))
+ def scale(self, x_scale, y_scale):
+ return self.__class__(self.x * x_scale, self.y * y_scale)
+
def as_int(self):
return self.__class__(int(round(self.x)), int(round(self.y)))
diff --git a/lib/utils/list.py b/lib/utils/list.py
index 2bfe2cd7..efa3969e 100644
--- a/lib/utils/list.py
+++ b/lib/utils/list.py
@@ -1,8 +1,16 @@
-from random import randrange
+import random
-def poprandom(sequence):
- index = randrange(len(sequence))
+def _uniform_rng():
+ while True:
+ yield random.uniform(0, 1)
+
+
+_rng = _uniform_rng()
+
+
+def poprandom(sequence, rng=_rng):
+ index = int(round(next(rng) * (len(sequence) - 1)))
item = sequence[index]
# It's O(1) to pop the last item, and O(n) to pop any other item. So we'll
diff --git a/lib/utils/string.py b/lib/utils/string.py
index cb852ce3..e9204076 100644
--- a/lib/utils/string.py
+++ b/lib/utils/string.py
@@ -8,3 +8,10 @@ def string_to_floats(string, delimiter=","):
floats = string.split(delimiter)
return [float(num) for num in floats]
+
+
+def remove_suffix(string, suffix):
+ if string.endswith(suffix):
+ return string[:-len(suffix)]
+ else:
+ return string