def plot_beizer_spline_2d(points, function, n, figname, steps=100): points_xs = [p[0] for p in points] points_ys = [p[1] for p in points] cpoints_chunks = fit.fit_bezier_spline(points, function, n) curves = [] cpoints = [] for chunk in cpoints_chunks: cpoints += chunk verts = map(lambda x: geom.Vertex(x), chunk) curves += [geom.BaseCurve(list(verts), function)] curve = geom.SuperCurve(curves) spline_ts = np.linspace(0.0, 1.0, steps) spline_points = [curve.t(t) for t in spline_ts] curve_xs = [p.x for p in spline_points] curve_ys = [p.y for p in spline_points] # Compute the control points cpoints_xs = [cp[0] for cp in cpoints] cpoints_ys = [cp[1] for cp in cpoints] plt.plot(points_xs, points_ys, "o", color="blue", label="Input points") plt.plot(cpoints_xs, cpoints_ys, "o", color="red", label="Control points") plt.plot(curve_xs, curve_ys, color="green", label="Bezier spline") plt.grid() plt.legend() plt.savefig(OUT_GRAPH_DIR + figname) plt.show()
def test_spline_fitting(self): # Test a real spline interpolation xs = np.linspace(0.0, 2 * math.pi, 100) ys = np.array([math.sin(x) for x in xs]) points = [geom.Vertex((xs[i], ys[i], 0.0)) for i in range(len(xs))] cpoints_chunks = fit.fit_bezier_spline(points, mmath.interp_bezier_curve_2, 2) curves = [] cpoints = [] for chunk in cpoints_chunks: cpoints += chunk verts = map(lambda x: geom.Vertex(x), chunk) curves += [ geom.BaseCurve(list(verts), mmath.interp_bezier_curve_2) ] curve = geom.SuperCurve(curves) # Compute the current spline spline_ts = np.linspace(0.0, 1.0, 100) spline_points = [curve.t(t) for t in spline_ts] spline_xs = [p.x for p in spline_points] spline_ys = [p.y for p in spline_points] # Compute the control points cpointsx = [cp[0] for cp in cpoints] cpointsy = [cp[1] for cp in cpoints] # Actually plot the graph if DRAW_GRAPHS: plt.plot(spline_xs, spline_ys) # Approximate function plt.plot(xs, ys) # Original function plt.plot(cpointsx, cpointsy, "o") plt.show()
def test_very_simple_spline_fitting(self): # Manually construct a sort of sine wave using bezier curves sin = lambda x: math.sin(x) xs = np.linspace(0.0, 2 * math.pi, 5) ys = np.array([math.sin(x) for x in xs]) cpoints = [geom.Vertex((xs[i], ys[i], 0.0)) for i in range(len(xs))] # We need to repeat the control points to have a spline cpoints_chunks = ut.splinify_list(cpoints, 3) curves = [ geom.BaseCurve(ps, mmath.interp_bezier_curve_2) for ps in cpoints_chunks ] curve = geom.SuperCurve(curves) # Plot the current spline spline_ts = np.linspace(0.0, 1.0, 100) spline_points = [curve.t(t) for t in spline_ts] spline_xs = [p.x for p in spline_points] spline_ys = [p.y for p in spline_points] # Print the graph if DRAW_GRAPHS: plt.plot(spline_xs, spline_ys) plt.show()
def plot_bezier_spline_3d(points, function, n, figname, steps=100): cpoints_chunks = fit.fit_bezier_spline(points, mmath.bezier_curve_3, 5) curves = [] cpoints = [] for chunk in cpoints_chunks: cpoints += chunk verts = map(lambda x: geom.Vertex(x), chunk) curves += [geom.BaseCurve(list(verts), mmath.bezier_curve_3)] curve = geom.SuperCurve(curves) # Compute the current spline spline_ts = np.linspace(0.0, 1.0, steps) spline_points = [curve.t(t) for t in spline_ts] spline_xs = [p.x for p in spline_points] spline_ys = [p.y for p in spline_points] spline_zs = [p.z for p in spline_points] # Compute the current control points cpointsx = [cp[0] for cp in cpoints] cpointsy = [cp[1] for cp in cpoints] cpointsz = [cp[2] for cp in cpoints] # Draw the graph fig = plt.figure() ax = fig.gca(projection='3d') ax.plot([p[0] for p in points], [p[1] for p in points], [p[2] for p in points], "o", color="blue", label="Input points") ax.plot(spline_xs, spline_ys, spline_zs, color="green", label='Bezier spline') ax.plot(cpointsx, cpointsy, cpointsz, "o", color="red", label="Control points") plt.grid() plt.legend() plt.savefig(OUT_GRAPH_DIR + figname) plt.show()
def test_simple_net(self): v0 = geom.Vertex((0.0, 0.0, 0.0)) v1 = geom.Vertex((1.0, 0.0, 0.0)) v2 = geom.Vertex((1.0, 1.0, 0.0)) v3 = geom.Vertex((0.0, 1.0, 0.0)) disp = geom.Vertex((0.0, 0.0, 0.5)) m0 = (v0 + v1) / 2.0 + disp m1 = (v1 + v2) / 2.0 + disp m2 = (v2 + v3) / 2.0 + disp m3 = (v3 + v0) / 2.0 + disp c1 = geom.SuperCurve( [geom.BaseCurve((v0, m0, v1), mmath.interp_bezier_curve_2)]) c2 = geom.BaseCurve((v1, m1, v2), mmath.interp_bezier_curve_2) c3 = geom.BaseCurve((v2, m2, v3), mmath.interp_bezier_curve_2) c4 = geom.BaseCurve((v3, m3, v0), mmath.interp_bezier_curve_2) c1_points = list(geom.sample_curve_samples(c1, 10)) c2_points = list(geom.sample_curve_samples(c2, 10)) c3_points = list(geom.sample_curve_samples(c3, 10)) c4_points = list(geom.sample_curve_samples(c4, 10)) polygon = geom.PolygonsNetQuad((c1, c2, c3, c4)) points = list(geom.sample_patch_samples(polygon, 70)) if DRAW_GRAPHS: fig = plt.figure() ax = fig.gca(projection='3d') ax.plot([p.x for p in c1_points], [p.y for p in c1_points], [p.z for p in c1_points]) ax.plot([p.x for p in c2_points], [p.y for p in c2_points], [p.z for p in c2_points]) ax.plot([p.x for p in c3_points], [p.y for p in c3_points], [p.z for p in c3_points]) ax.plot([p.x for p in c4_points], [p.y for p in c4_points], [p.z for p in c4_points]) ax.plot([p.x for p in points], [p.y for p in points], [p.z for p in points], "o", label="Geometry points") plt.show()
def test_spline_fitting_3d(self): '''Fit a complex 3d curve with a bezier spline of degree three (5 curves).''' # Generate a cool 3d curve theta = np.linspace(-4 * np.pi, 4 * np.pi, 100) zs = np.linspace(-2, 2, 100) r = zs**2 + 1 xs = r * np.sin(theta) ys = r * np.cos(theta) # Fit the data points = list(zip(xs, ys, zs)) cpoints_chunks = fit.fit_bezier_spline(points, mmath.bezier_curve_3, 5) curves = [] cpoints = [] for chunk in cpoints_chunks: cpoints += chunk verts = map(lambda x: geom.Vertex(x), chunk) curves += [geom.BaseCurve(list(verts), mmath.bezier_curve_3)] curve = geom.SuperCurve(curves) # Compute the current spline spline_ts = np.linspace(0.0, 1.0, 100) spline_points = [curve.t(t) for t in spline_ts] spline_xs = [p.x for p in spline_points] spline_ys = [p.y for p in spline_points] spline_zs = [p.z for p in spline_points] # Compute the current control points cpointsx = [cp[0] for cp in cpoints] cpointsy = [cp[1] for cp in cpoints] cpointsz = [cp[2] for cp in cpoints] # Draw the graph if DRAW_GRAPHS: fig = plt.figure() ax = fig.gca(projection='3d') ax.plot(xs, ys, zs, label='parametric curve') ax.plot(spline_xs, spline_ys, spline_zs, label='Fitted spline') ax.plot(cpointsx, cpointsy, cpointsz, "o", label="Control points") plt.legend() plt.show()