def trim(RunningVolume, Sdata, Bdata, Pdata, Rdata, Cdata): ''' It is the job of the trim function to generate the unified polyline curves of the yoyo as well as calculate the volume of the final yoyo half by calling the individual, pupose built subtracting functions of each yoyo section ''' Pxc = [] Pyc = [] Rxc = [] Ryc = [] Cxc = [] Cyc = [] for i in range(0, len(Pdata)): Pxc = np.append(Pxc, Pdata[i, 0]) Pyc = np.append(Pyc, Pdata[i, 1]) Px, Py = Bezier(list(zip(Pxc, Pyc))).T RunningVolume = RunningVolume - ProfileSubtract(Px, Py) for i in range(0, len(Rdata)): Rxc = np.append(Rxc, Rdata[i, 0]) Ryc = np.append(Ryc, Rdata[i, 1]) Rx, Ry = Bezier(list(zip(Rxc, Ryc))).T RunningVolume = RunningVolume - RimSubtract(Rx, Ry) for i in range(0, len(Cdata)): Cxc = np.append(Cxc, Cdata[i, 0]) Cyc = np.append(Cyc, Cdata[i, 1]) Cx, Cy = Bezier(list(zip(Cxc, Cyc))).T RunningVolume = RunningVolume - CupSubtract(Cx, Cy) density = Sdata[7] / (10**(9)) halfmass = RunningVolume * density * (10**3) megamatx, megamaty, prof, rim, cup, bulkmat = Bulkmat( Px, Py, Rx, Ry, Cx, Cy, Bdata, Pdata, Rdata, Cdata) return megamatx, megamaty, prof, rim, cup, bulkmat, halfmass
def drawAtLength(self, t): p = PVector(0, 0) s = self.scale l = 0 for sg in self.segments: for g in sg: if l + g[2] > t: if g[0] == LINETO: t = (t - l) / g[2] q = t * g[1] + (1 - t) * p line(s * p.x, s * p.y, s * q.x, s * q.y) elif g[0] == CURVETO: t = t - l bz = Bezier(s * p, s * g[1][0], s * g[1][1], s * g[1][2]) bz.drawToLength(s * t) return if g[0] == MOVETO: p = g[1] elif g[0] == LINETO: line(s * p.x, s * p.y, s * g[1].x, s * g[1].y) p = g[1] elif g[0] == CURVETO: bezier(s * p.x, s * p.y, s * g[1][0].x, s * g[1][0].y, s * g[1][1].x, s * g[1][1].y, s * g[1][2].x, s * g[1][2].y) p = g[1][2] l += g[2]
def computeBezier(self): if len(self.curvePoints) > 1: spline = Bezier() spline.compute(self.curvePoints) self.history.append(spline) self.curvePoints = [] self.updateGL()
def drawCartesianAtLength(self, pen, t): p = PVector(0, 0) l = 0 sgs = [] for sg in self.segments: nsg = [] sgs.append(nsg) for g in sg: if l + g[2] > t: if g[0] == LINETO: t = (t - l) / g[2] q = t * g[1] + (1 - t) * p nsg.append([LINETO, q, q.dist(p)]) elif g[0] == CURVETO: t = t - l bz = Bezier(p, g[1][0], g[1][1], g[1][2]) bza, bzb = bz.splitAtLength(t) nsg.append([ CURVETO, [bza.points[1], bza.points[2], bza.points[3]], bza.length ]) renderPath(sgs, self.scale, pen) return nsg.append(g) if g[0] == CURVETO: p = g[1][2] else: p = g[1] l += g[2] renderPath(sgs, self.scale, pen)
def addCurveTo(self, b, c, d): bz = Bezier(self.point, b, c, d) l = bz.length() self.segment.append([CURVETO, [b, c, d], l]) bz = Bezier(self.point, b, c, d) self.length += l self.point = d self.updatebb(b) self.updatebb(c) self.updatebb(d)
def addCurveRelTo(self, b, c, d): bz = Bezier(self.point, self.point + b, d + c, d) l = bz.length() self.segment.append([CURVETO, [self.point + b, d + c, d], l]) bz = Bezier(self.point, self.point + b, d + c, d) self.length += l self.updatebb(self.point + b) self.updatebb(d + c) self.updatebb(d) self.point = d
def draw_curve(fig, ax): t_points = np.arange(0, 1, 0.01) curve = Bezier.Curve(t_points, points) ax.plot( curve[:, 0], # x-coordinates. curve[:, 1], # y-coordinates. )
def pico(curvaNivel=False): aumento = 0.1 rango = 6 aumentoX = 0.4 h = 2.6 p = 1 if curvaNivel: aumento = 0.025 rango = 18 aumentoX = 0.1 b = 0.6 x = 0 for i in range(0, rango): points_set_1 = a([[-b, -1, h], [-1, -p - x, h], [1, -p - x, h], [b, -1, h]]) t_points = np.arange(0, 1, 0.01) curve_set_1 = Bezier.Curve(t_points, points_set_1) ax.plot(curve_set_1[:, 0], curve_set_1[:, 1], curve_set_1[:, 2], color="green") h = h + aumento x = x + aumentoX
def SaveBest(self, seed, d): # make Bezier curves representing the fibers t_points = np.arange(0, 1, 0.01) # plane of vertical fibers vertical_grid = np.zeros((c.GRID_SIZE, c.GRID_SIZE)) for n in range(c.FIBERS_TOP): points = np.array([[self.genome[n, 0], self.genome[n, 1]], [self.genome[n, 2], self.genome[n, 3]]]) curve = Bezier.Curve(t_points, points) grid = Bezier.DistCurve(curve) vertical_grid = np.logical_or(vertical_grid, grid) # plane of horizontal fibers horizontal_grid = np.zeros((c.GRID_SIZE, c.GRID_SIZE)) for n in range(c.FIBERS_TOP, c.FIBERS): points = np.array([[self.genome[n, 0], self.genome[n, 1]], [self.genome[n, 2], self.genome[n, 3]]]) curve = Bezier.Curve(t_points, points) grid = Bezier.DistCurve(curve) horizontal_grid = np.logical_or(horizontal_grid, grid) file = open(f'results/best/Bezier-D{d}-Seed-{seed}.csv', 'w') l = np.count_nonzero(horizontal_grid) + np.count_nonzero(vertical_grid) file.write(f'{l} ') for i in range(c.GRID_SIZE): for j in range(c.GRID_SIZE): if horizontal_grid[i, j]: file.write(f'{i+1} {j+1} -2 ') for i in range(c.GRID_SIZE): for j in range(c.GRID_SIZE): if vertical_grid[i, j]: file.write(f'{i+1} {j+1} 0 ') file.close() file = open(f'results/best/Bezier-D{d}-Seed-{seed}-grid.csv', 'w') file.write('top grid:\n') for i in range(c.GRID_SIZE): for j in range(c.GRID_SIZE): file.write(f'{int(vertical_grid[i, j])} ') file.write('\n') file.write('\nbot grid:\n') for i in range(c.GRID_SIZE): for j in range(c.GRID_SIZE): file.write(f'{int(horizontal_grid[i, j])} ') file.write('\n') file.close()
def splitCurve(curve, splits): p1, p2, p3 = curve.controlPoints q1 = p1 r3 = p3 q2 = PathUtilities.midpoint([p1, p2]) r2 = PathUtilities.midpoint([p2, p3]) q3 = r1 = PathUtilities.midpoint([q2, r2]) q = Bezier([q1, q2, q3]) r = Bezier([r1, r2, r3]) if q.direction != Bezier.dir_mixed: splits.append(q) else: splitCurve(q, splits) if r.direction != Bezier.dir_mixed: splits.append(r) else: splitCurve(r, splits)
def CalculateCurvePoints(self, points, resolution=0.1) -> list: ''' Calculates points of a curves and returns them. Parameters: points : list : list of co-ordinates used as the controls points resolution : float : number of steps in the 3D curve Returns : list : list of co-ordinates Exceptions : None ''' t_points = np.arange(0, 1, resolution) curve_set = Bezier.Curve(t_points, np.array(points)) return_l = curve_set.tolist() return_l.append(points[len(points) - 1]) return return_l
def Start_Evaluation(self, plot=False): self.rnd1 = np.random.randint(low=1, high=10000) self.rnd2 = np.random.randint(low=1, high=10000) # make Bezier curves representing the fibers t_points = np.arange(0, 1, 0.01) # plane of vertical fibers vertical_grid = np.zeros((c.GRID_SIZE, c.GRID_SIZE)) for n in range(c.FIBERS_TOP): points = np.array([[self.genome[n, 0], self.genome[n, 1]], [self.genome[n, 2], self.genome[n, 3]]]) curve = Bezier.Curve(t_points, points) grid = Bezier.DistCurve(curve) vertical_grid = np.logical_or(vertical_grid, grid) # plane of horizontal fibers horizontal_grid = np.zeros((c.GRID_SIZE, c.GRID_SIZE)) for n in range(c.FIBERS_TOP, c.FIBERS): points = np.array([[self.genome[n, 0], self.genome[n, 1]], [self.genome[n, 2], self.genome[n, 3]]]) curve = Bezier.Curve(t_points, points) grid = Bezier.DistCurve(curve) horizontal_grid = np.logical_or(horizontal_grid, grid) # save the grids if plot: Bezier.PlotGrid(vertical_grid, "verticalGrid.jpg") Bezier.PlotGrid(horizontal_grid, "horizontalGrid.jpg") file = open( f'results/bezier/genome{self.ID}_{self.rnd1}_{self.rnd2}.csv', 'w') l = np.count_nonzero(horizontal_grid) + np.count_nonzero(vertical_grid) file.write(f'{l} ') for i in range(c.GRID_SIZE): for j in range(c.GRID_SIZE): if horizontal_grid[i, j]: file.write(f'{i+1} {j+1} -2 ') for i in range(c.GRID_SIZE): for j in range(c.GRID_SIZE): if vertical_grid[i, j]: file.write(f'{i+1} {j+1} 0 ') file.close() call([ './membrane.exe', f'results/bezier/genome{self.ID}_{self.rnd1}_{self.rnd2}.csv', f'target/d{self.d}.csv.res' ])
b = b - 0.1 elif i<30: b = b + 0.1 elif i<55: b= b elif i>55: b = b -0.1 h = h + 0.02 h = h - (0.02)*5 #Generar con curvas de Bezier la salida del mortero x = 0 for i in range(0, 12): points_set_1 = a([[-b, -2.1, h], [-1, -2.2 - x, h], [1, -2.2 - x, h], [b, -2.1, h]]) t_points = np.arange(0, 1, 0.01) curve_set_1 = Bezier.Curve(t_points, points_set_1) ax.plot(curve_set_1[:, 0], curve_set_1[:, 1], curve_set_1[:, 2], color="green") points_set_2 = a([[-b, 2.1, h], [-1, 3.2, h], [1, 3.2, h], [b, 2.1, h]]) t_points = np.arange(0, 1, 0.01) curve_set_2 = Bezier.Curve(t_points, points_set_2) ax.plot(curve_set_2[:, 0], curve_set_2[:, 1], curve_set_2[:, 2], color="green") points_set_3 = a([[2.1, b, h], [3.2, 1, h], [3.2, -1, h], [2.1, -b, h]]) t_points = np.arange(0, 1, 0.01) curve_set_3 = Bezier.Curve(t_points, points_set_3) ax.plot(curve_set_3[:, 0], curve_set_3[:, 1], curve_set_3[:, 2], color="green") points_set_4 = a([[-2.1, -b, h], [-3.2, -1, h], [-3.2, 1, h], [-2.1, b, h]]) t_points = np.arange(0, 1, 0.01) curve_set_4 = Bezier.Curve(t_points, points_set_4)
lenB = len(other.constPoints) if lenA > lenB: pointsA = self.constPoints pointsB = list(map( lambda a: Point(a[0], a[1]), other.renderPoints(lenA) )) else: pointsA = list(map( lambda a: Point(a[0], a[1]), self.renderPoints(lenB), )) pointsB = other.constPoints newPoints = list(map( lambda a, b: a.transition(b, proc), pointsA, pointsB ) ) return OscObject(newPoints) if if __name__ == "__main__": a = OscObject(list()) b = Bezier(list()) c = OscObject(list()) a.cpTransitionTo(c, 0.5)
from Bezier import Bezier import numpy as np from numpy import array as a import matplotlib.pyplot as plt from mpl_toolkits.mplot3d import Axes3D #~~~# fig = plt.figure(dpi=128) #~~~# Simple arch. t_points = np.arange(0, 1, 0.01) test = a([[0, 0], [0, 8], [5, 10], [9, 7], [4, 3]]) test_set_1 = Bezier.Curve(t_points, test) plt.subplot(2, 3, 3) plt.xticks([i1 for i1 in range(-20, 20) ]), plt.yticks([i1 for i1 in range(-20, 20)]) plt.gca().set_aspect('equal', adjustable='box') plt.grid(b=True, which='major', axis='both') plt.plot(test_set_1[:, 0], test_set_1[:, 1]) plt.plot(test[:, 0], test[:, 1], 'ro:') #~~~# Simple wave. t_points = np.arange(0, 1, 0.01) test = a([[0, 5], [4, 10], [6, 10], [4, 0], [6, 0], [10, 5]]) test_set_1 = Bezier.Curve(t_points, test)
def y(self, t): return ( self.y1 + self._by * t + self._cy * t ** 2 + self._dy * t ** 3 ) def dx(self, t): return ( self._bx + 2 * self._cx * t + 3 * self._dx * t ** 2 ) def dy(self, t): return ( self._by + 2 * self._cy * t + 3 * self._dy * t ** 2 ) def f(self, t): return np.sqrt(self.dx(t) ** 2 + self.dy(t) ** 2) curve = Bezier( [(0, 0, 0), (2, 2, 0), (-1, 2, 0), (1, 0, 0)] )
Rdata = np.loadtxt('rim.txt', delimiter=',') x1 = [] y1 = [] x2 = [] y2 = [] x3 = [] y3 = [] for i in range(0, len(Bdata)): x1 = np.append(x1, Bdata[i, 0]) y1 = np.append(y1, Bdata[i, 1]) for i in range(0, len(Pdata)): x2 = np.append(x2, Pdata[i, 0]) y2 = np.append(y2, Pdata[i, 1]) for i in range(0, len(Rdata)): x3 = np.append(x3, Rdata[i, 0]) y3 = np.append(y3, Rdata[i, 1]) xp, yp = Bezier(list(zip(x2, y2))).T xr, yr = Bezier(list(zip(x3, y3))).T plt.plot(xp, yp) plt.plot(xr, yr) plt.plot(x1, y1) plt.axis('equal') plt.plot(x2, y2, "ro") plt.plot(x2, y2, "b--") plt.show()
Cdata = np.loadtxt('cup.txt', delimiter=',') Blank_Radius = Sdata[1] / 2 Blank_Length = Sdata[2] / 2 Blank_Volume = ((Blank_Radius**2) * math.pi) * Blank_Length RunningVolume = Blank_Volume RunningVolume = RunningVolume - BearingSubtract(3) Pxc = [] Pyc = [] Rxc = [] Ryc = [] Cxc = [] Cyc = [] for i in range(0, len(Pdata)): Pxc = np.append(Pxc, Pdata[i, 0]) Pyc = np.append(Pyc, Pdata[i, 1]) Px, Py = Bezier(list(zip(Pxc, Pyc))).T RunningVolume = RunningVolume - ProfileSubtract(Px, Py) for i in range(0, len(Rdata)): Rxc = np.append(Rxc, Rdata[i, 0]) Ryc = np.append(Ryc, Rdata[i, 1]) Rx, Ry = Bezier(list(zip(Rxc, Ryc))).T RunningVolume = RunningVolume - RimSubtract(Rx, Ry) for i in range(0, len(Cdata)): Cxc = np.append(Cxc, Cdata[i, 0]) Cyc = np.append(Cyc, Cdata[i, 1]) Cx, Cy = Bezier(list(zip(Cxc, Cyc))).T RunningVolume = RunningVolume - CupSubtract(Cx, Cy) density = Sdata[7] / (10**(9)) halfmass = RunningVolume * density * (10**3) # This populates matrices for plotting megamatx, megamaty, prof, rim, cup, bulkmat = Bulkmat(Px, Py, Rx, Ry, Cx, Cy,
x1 = np.append(x1, Bdata[i, 0]) y1 = np.append(y1, Bdata[i, 1]) # Generates coordinates of profile for i in range(0, len(Pdata)): x2 = np.append(x2, Pdata[i, 0]) y2 = np.append(y2, Pdata[i, 1]) # Generates coordinates of rim for i in range(0, len(Rdata)): x3 = np.append(x3, Rdata[i, 0]) y3 = np.append(y3, Rdata[i, 1]) # Generates coordinates of cup for i in range(0, len(Cdata)): x4 = np.append(x4, Cdata[i, 0]) y4 = np.append(y4, Cdata[i, 1]) xp, yp = Bezier(list(zip(x2, y2))).T xr, yr = Bezier(list(zip(x3, y3))).T xc, yc = Bezier(list(zip(x4, y4))).T plt.plot(xp, yp) plt.plot(xr, yr) plt.plot(xc, yc) plt.axis('equal') plt.plot(x1, y1) plt.plot(x2, y2, "ro") plt.plot(x2, y2, "b--") plt.plot(x3, y3, "ro") plt.plot(x3, y3, "b--") plt.plot(x4, y4, "ro") plt.plot(x4, y4, "b--")