From f43f909bf20dafb2c10520815282046cffe495f2 Mon Sep 17 00:00:00 2001 From: Leonard Kugis Date: Sat, 22 Aug 2020 16:02:17 +0200 Subject: Refactoring, Bezier Refactored module layout. Added missing dependencies to setup. Added time scaling to curves. --- AutopyExtended/Curve/Bezier.py | 24 ++++++++++++++++ AutopyExtended/Curve/CurveBezier.py | 27 ------------------ AutopyExtended/Curve/CurveLinear.py | 10 ------- AutopyExtended/Curve/Linear.py | 10 +++++++ AutopyExtended/Curve/__init__.py | 0 AutopyExtended/Curve/test.py | 55 ------------------------------------- AutopyExtended/Curve/test1.py | 9 ------ 7 files changed, 34 insertions(+), 101 deletions(-) create mode 100644 AutopyExtended/Curve/Bezier.py delete mode 100644 AutopyExtended/Curve/CurveBezier.py delete mode 100644 AutopyExtended/Curve/CurveLinear.py create mode 100644 AutopyExtended/Curve/Linear.py create mode 100644 AutopyExtended/Curve/__init__.py delete mode 100644 AutopyExtended/Curve/test.py delete mode 100644 AutopyExtended/Curve/test1.py (limited to 'AutopyExtended/Curve') diff --git a/AutopyExtended/Curve/Bezier.py b/AutopyExtended/Curve/Bezier.py new file mode 100644 index 0000000..ab79ec0 --- /dev/null +++ b/AutopyExtended/Curve/Bezier.py @@ -0,0 +1,24 @@ +import numpy as np + +from AutopyExtended.Curve.Curve import Curve +from scipy.special import comb + +class CurveBezier(Curve): + def __init__(self, cp, tn): + self.__tn = tn + x = np.array([p[0] for p in cp]) + y = np.array([p[1] for p in cp]) + t = np.linspace(0, 1, tn) + poly = np.array([CurveBezier.bernstein(i, len(cp) - 1, t) for i in range(0, len(cp))]) + self.__x = np.flip(np.dot(x, poly)) + self.__y = np.flip(np.dot(y, poly)) + + def point(self, t, t_target): + t_scaled = min([t * self.__tn / t_target, self.__tn - 1]) + return np.add((self.__x[int(np.floor(t_scaled))], self.__y[int(np.floor(t_scaled))]), + np.subtract((self.__x[int(np.ceil(t_scaled))], self.__y[int(np.ceil(t_scaled))]), + (self.__x[int(np.floor(t_scaled))], self.__y[int(np.floor(t_scaled))])) * (t_scaled - np.floor(t_scaled))) + + @staticmethod + def bernstein(i, n, t): + return comb(n, i) * (t ** (n - i)) * (1 - t) ** i diff --git a/AutopyExtended/Curve/CurveBezier.py b/AutopyExtended/Curve/CurveBezier.py deleted file mode 100644 index 4aedb9d..0000000 --- a/AutopyExtended/Curve/CurveBezier.py +++ /dev/null @@ -1,27 +0,0 @@ -import numpy as np - -from Curve import Curve -from scipy.special import comb - -class CurveBezier(Curve): - def __init__(self, cp, tn): - self.__tn = tn - x = np.array([p[0] for p in cp]) - y = np.array([p[1] for p in cp]) - t = np.linspace(0, 1, tn) - poly = np.array([CurveBezier.bernstein(i, len(cp) - 1, t) for i in range(0, len(cp))]) - self.__x = np.flip(np.dot(x, poly)) - self.__y = np.flip(np.dot(y, poly)) - - def point(self, t, t_target=None): - if(t_target == None): - t_target = self.__tn - if(t_target != self.__tn): - raise ValueError("t_target must be equal to tn.") - return np.add((self.__x[int(np.floor(t))], self.__y[int(np.floor(t))]), - np.subtract((self.__x[int(np.ceil(t))], self.__y[int(np.ceil(t))]), - (self.__x[int(np.floor(t))], self.__y[int(np.floor(t))])) * (t - np.floor(t))) - - @staticmethod - def bernstein(i, n, t): - return comb(n, i) * (t ** (n - i)) * (1 - t) ** i diff --git a/AutopyExtended/Curve/CurveLinear.py b/AutopyExtended/Curve/CurveLinear.py deleted file mode 100644 index bee70cc..0000000 --- a/AutopyExtended/Curve/CurveLinear.py +++ /dev/null @@ -1,10 +0,0 @@ -import numpy as np -import Curve - -class CurveLinear(Curve): - def __init__(self, start, end): - self.__start = np.array(start) - self.__end = np.array(end) - def point(self, t, t_target): - d = np.subtract(self.__end, self.__start) - return np.add(self.__start, (round(d[0] * t / t_target), round(d[1] * t / t_target))) diff --git a/AutopyExtended/Curve/Linear.py b/AutopyExtended/Curve/Linear.py new file mode 100644 index 0000000..f35c095 --- /dev/null +++ b/AutopyExtended/Curve/Linear.py @@ -0,0 +1,10 @@ +import numpy as np +from AutopyExtended.Curve.Curve import Curve + +class CurveLinear(Curve): + def __init__(self, start, end): + self.__start = np.array(start) + self.__end = np.array(end) + def point(self, t, t_target): + d = np.subtract(self.__end, self.__start) + return np.add(self.__start, (round(d[0] * t / t_target), round(d[1] * t / t_target))) diff --git a/AutopyExtended/Curve/__init__.py b/AutopyExtended/Curve/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/AutopyExtended/Curve/test.py b/AutopyExtended/Curve/test.py deleted file mode 100644 index ab9ee6c..0000000 --- a/AutopyExtended/Curve/test.py +++ /dev/null @@ -1,55 +0,0 @@ -import numpy as np -from scipy.special import comb - -def bernstein_poly(i, n, t): - """ - The Bernstein polynomial of n, i as a function of t - """ - - return comb(n, i) * ( t**(n-i) ) * (1 - t)**i - - - -def bezier_curve(points, nTimes=1000): - """ - Given a set of control points, return the - bezier curve defined by the control points. - points should be a list of lists, or list of tuples - such as [ [1,1], - [2,3], - [4,5], ..[Xn, Yn] ] - nTimes is the number of time steps, defaults to 1000 - See http://processingjs.nihongoresources.com/bezierinfo/ - """ - - nPoints = len(points) - xPoints = np.array([p[0] for p in points]) - yPoints = np.array([p[1] for p in points]) - - t = np.linspace(0.0, 1.0, nTimes) - - polynomial_array = np.array([ bernstein_poly(i, nPoints-1, t) for i in range(0, nPoints) ]) - - xvals = np.dot(xPoints, polynomial_array) - yvals = np.dot(yPoints, polynomial_array) - - return xvals, yvals - - - -if __name__ == "__main__": - from matplotlib import pyplot as plt - - nPoints = 4 - points = np.random.rand(nPoints,2)*200 - xpoints = [p[0] for p in points] - ypoints = [p[1] for p in points] - - xvals, yvals = bezier_curve(points, nTimes=1000) - plt.plot(xvals, yvals) - plt.plot(xpoints, ypoints, "ro") - for nr in range(len(points)): - plt.text(points[nr][0], points[nr][1], nr) - - - plt.show() diff --git a/AutopyExtended/Curve/test1.py b/AutopyExtended/Curve/test1.py deleted file mode 100644 index 05f56d8..0000000 --- a/AutopyExtended/Curve/test1.py +++ /dev/null @@ -1,9 +0,0 @@ -import matplotlib.pyplot as plot -from CurveBezier import CurveBezier - -curve = CurveBezier([(0,0),(53,178),(5,87),(140,75),(48,647),(37,49)],1000) -points = [curve.point(t) for t in range(0,1000)] -x = [p[0] for p in points] -y = [p[1] for p in points] -plot.scatter(x,y) -plot.show() -- cgit v1.2.1