def test_it(self): import bezier nodes1 = np.asfortranarray([[0.0, 1.0, 0.0], [0.0, 0.0, 1.0]]) surface1 = bezier.Surface(nodes1, degree=1, _copy=False) nodes2 = np.asfortranarray([[0.25, -0.75, 0.25], [0.25, 0.25, -0.75]]) surface2 = bezier.Surface(nodes2, degree=1, _copy=False) edge_nodes = tuple(edge._nodes for edge in surface1.edges) + tuple( edge._nodes for edge in surface2.edges) edge_info = ( (0, 0.0, 0.25), (5, 0.75, 1.0), (3, 0.0, 0.25), (2, 0.75, 1.0), ) result = self._call_function_under_test(edge_info, edge_nodes) self.assertIsInstance(result, bezier.CurvedPolygon) self.assertEqual(result._metadata, edge_info) self.assertEqual(result.num_sides, 4) # pylint: disable=unbalanced-tuple-unpacking edge0, edge1, edge2, edge3 = result._edges # pylint: enable=unbalanced-tuple-unpacking # First edge. expected = np.asfortranarray([[0.0, 0.25], [0.0, 0.0]]) self.assertEqual(edge0._nodes, expected) # Second edge. expected = np.asfortranarray([[0.25, 0.25], [0.0, 0.25]]) self.assertEqual(edge1._nodes, expected) # Third edge. expected = np.asfortranarray([[0.25, 0.0], [0.25, 0.25]]) self.assertEqual(edge2._nodes, expected) # Fourth edge. expected = np.asfortranarray([[0.0, 0.0], [0.25, 0.0]]) self.assertEqual(edge3._nodes, expected)
def unit_triangle(): """Image for :class:`.surface.Surface` docstring.""" if NO_IMAGES: return nodes = np.asfortranarray([[0.0, 1.0, 0.0], [0.0, 0.0, 1.0]]) surface = bezier.Surface(nodes, degree=1) ax = surface.plot(256) ax.axis("scaled") _plot_helpers.add_plot_boundary(ax) save_image(ax.figure, "unit_triangle.png")
def image1(): figure, (ax1, ax2) = plt.subplots(1, 2) nodes1 = np.asfortranarray([[0.0, 3.0, 7.0], [5.0, 0.0, 8.0]]) triangle1 = bezier.Surface(nodes1, degree=1) triangle1.plot(256, ax=ax1) nodes2 = np.asfortranarray([[0.0, 1.0, 2.0, 2.0, 2.0, 0.0], [0.0, 0.0, 0.0, 1.0, 2.0, 2.0]]) triangle2 = bezier.Surface(nodes2, degree=2) triangle2.plot(256, ax=ax2) params = np.asfortranarray([[0.125, 0.125], [0.125, 0.75]]) points1 = triangle1.evaluate_cartesian_multi(params) ax1.plot(points1[0, :], points1[1, :], marker="o", color="black") points2 = triangle2.evaluate_cartesian_multi(params) ax2.plot(points2[0, :], points2[1, :], marker="o", color="black") for ax in (ax1, ax2): ax.tick_params(labelsize=LABEL_SIZE, which="both") ax.axis("equal") ax1.set_title("Convex", fontsize=FONT_SIZE) ax2.set_title("Not (Necessarily) Convex", fontsize=FONT_SIZE) figure.set_size_inches(8.74, 4.8) figure.subplots_adjust(left=0.05, bottom=0.06, right=0.99, top=0.93, wspace=0.12, hspace=0.2) filename = "not_convex.pdf" path = plot_utils.get_path("slides", filename) figure.savefig(path) print("Saved {}".format(filename)) plt.close(figure)
def image1(): nodes = np.asfortranarray([[0.0, 1.5, 3.0, 0.75, 2.25, 0.0], [0.0, -0.5, 0.0, 1.0, 1.5, 2.0]]) triangle = bezier.Surface(nodes, degree=2, _copy=False) point = triangle.evaluate_cartesian(0.25, 0.125) xv, yv = point.flatten() sub_triangles = triangle.subdivide() edges = triangle.edges figure, all_axes = plt.subplots(2, 2, sharex=True, sharey=True) all_axes = all_axes.flatten() for ax, sub_triangle in zip(all_axes, sub_triangles): # Add the bounding box for the sub triangle. add_patch(sub_triangle, ax, plot_utils.GREEN) # Add the triangle boundary to each subplot. for edge in edges: edge.plot(256, ax=ax, color=plot_utils.BLUE) # Add the sub triangle. sub_triangle.plot(256, ax=ax, color=plot_utils.BLUE) # Add the point to be found. ax.plot( [xv], [yv], color="black", marker="o", markersize=3, linestyle="none", ) for ax in all_axes: ax.set_aspect("equal") # One axis sets all axis all_axes[0].set_xticklabels([]) all_axes[0].set_yticklabels([]) figure.set_size_inches(4.0, 3.0) figure.subplots_adjust(left=0.01, bottom=0.01, right=0.99, top=0.99, wspace=0.03, hspace=0.04) filename = "locate_in_triangle.pdf" path = plot_utils.get_path("bezier-intersection", filename) figure.savefig(path) print("Saved {}".format(filename)) plt.close(figure)
def main(): nodes = np.asfortranarray( [[1.0, 0.0, 1.0, 0.0, 0.0, 0.0], [0.0, 0.0, 1.0, 0.0, 0.0, 1.0]] ) bez_triangle = bezier.Surface(nodes, degree=2, _copy=False) # b(s, t) = [(1 - s - t)^2 + t^2, s^2 + t^2] figure, ((ax1, ax2, ax3), (ax4, ax5, ax6)) = plt.subplots( 2, 3, sharex=True, sharey=True ) for ax in (ax1, ax2, ax3): ax.plot([0, 1, 0, 0], [0, 0, 1, 0], color="black") # The "left" edge b(0, t) = [(1 - t)^2 + t^2, t^2] lies on the algebraic # curve 4x = (1 + x - y)^2. Plugging b(s, t) into this algebraic curve # we find interior points as well. sv = np.linspace(0.0, 2.0 / 5.0, 250) sqrt_part = np.sqrt((2.0 - 5.0 * sv) / (2.0 - sv)) poly1 = np.empty((501, 2)) poly1[:250, 0] = sv poly1[:250, 1] = 0.5 * (1.0 - sv + sqrt_part) poly1[250:499, 0] = sv[-2::-1] poly1[250:499, 1] = 0.5 * (1.0 - sv[-2::-1] - sqrt_part[-2::-1]) poly1[499, :] = 1.0, 0.0 poly1[500, :] = 0.0, 1.0 patch = matplotlib.patches.PathPatch( matplotlib.path.Path(poly1), color=plot_utils.GREEN ) ax1.add_patch(patch) for ax in (ax2, ax3): patch = matplotlib.patches.PathPatch( matplotlib.path.Path(poly1), color=plot_utils.GREEN, alpha=ALPHA ) ax.add_patch(patch) for ax in (ax4, ax5, ax6): bez_triangle.plot(256, ax=ax, color=plot_utils.GREEN) for edge in bez_triangle.edges: edge.plot(256, ax=ax, color="black") ax4.patches[-1].set_alpha(1.0) ax5.patches[-1].set_alpha(ALPHA) ax6.patches[-1].set_alpha(ALPHA) # det(J) = 4[t^2 + (s - 1) t + (s - s^2)] sv = np.linspace(0.0, 1.0 / 5.0, 250) sqrt_part = np.sqrt((1.0 - sv) * (1.0 - 5.0 * sv)) poly2 = np.empty((500, 2)) poly2[:250, 0] = sv poly2[:250, 1] = 0.5 * (1.0 - sv - sqrt_part) poly2[250:499, 0] = sv[-2::-1] poly2[250:499, 1] = 0.5 * (1.0 - sv[-2::-1] + sqrt_part[-2::-1]) poly2[499, :] = 0.0, 0.0 for ax in (ax1, ax2, ax3): ax.plot( poly2[:499, 0], poly2[:499, 1], color="black", linestyle="dashed" ) patch = matplotlib.patches.PathPatch( matplotlib.path.Path(poly2), color=plot_utils.RED ) ax3.add_patch(patch) for ax in (ax1, ax2): patch = matplotlib.patches.PathPatch( matplotlib.path.Path(poly2), color="black", alpha=ALPHA ) ax.add_patch(patch) # Shared slice in between two curves. poly3 = np.empty((997, 2)) poly3[:499, :] = poly2[498::-1, :] poly3[499:, :] = poly1[497::-1, :] patch = matplotlib.patches.PathPatch( matplotlib.path.Path(poly3), color=plot_utils.BLUE ) ax2.add_patch(patch) for ax in (ax1, ax3): patch = matplotlib.patches.PathPatch( matplotlib.path.Path(poly3), color="black", alpha=ALPHA ) ax.add_patch(patch) # Now, compute the image of the det(J) = 0 curve under b(s, t). poly4 = np.empty((598, 2)) sv = poly2[:499, 0][::-1] tv = poly2[:499, 1][::-1] poly4[:499, 0] = (1.0 - sv - tv) * (1.0 - sv - tv) + sv * sv poly4[:499, 1] = sv * sv + tv * tv for ax in (ax4, ax5, ax6): ax.plot( poly4[:499, 0], poly4[:499, 1], color="black", linestyle="dashed" ) # Combine this with the image b(0, t) tv = np.linspace(0.0, 1.0, 100)[1:] poly4[499:, 0] = (1.0 - tv) * (1.0 - tv) poly4[499:, 1] = tv * tv patch = matplotlib.patches.PathPatch( matplotlib.path.Path(poly4), color="black", alpha=ALPHA ) ax4.add_patch(patch) for ax, color in ((ax5, plot_utils.BLUE), (ax6, plot_utils.RED)): patch = matplotlib.patches.PathPatch( matplotlib.path.Path(poly4), color=color ) ax.add_patch(patch) for ax in (ax1, ax2, ax3, ax4, ax5, ax6): ax.set_aspect("equal") # One axis sets all axis ax1.set_xticklabels([]) ax1.set_yticklabels([]) ax2.text( 0.3, 0.35, r"$+$", horizontalalignment="center", verticalalignment="center", fontsize=16, fontweight="bold", color="black", ) ax2.add_patch( matplotlib.patches.Circle( (0.3, 0.35), radius=0.06, fill=False, edgecolor="black", linewidth=1.0, ) ) ax3.text( 0.1, 0.42, r"$-$", horizontalalignment="center", verticalalignment="center", fontsize=16, fontweight="bold", color="black", ) ax3.add_patch( matplotlib.patches.Circle( (0.1, 0.422), radius=0.06, fill=False, edgecolor="black", linewidth=1.0, ) ) figure.set_size_inches(4.8, 3.3) figure.subplots_adjust( left=0.01, bottom=0.01, right=0.99, top=0.99, wspace=0.04, hspace=0.0 ) filename = "inverted_element.pdf" path = plot_utils.get_path("preliminaries", filename) figure.savefig(path) print("Saved {}".format(filename)) plt.close(figure)
def boundary_leak(slide_num): bez_triangle = bezier.Surface(NODES, degree=2, _copy=False) figure, ((ax1, ax2), (ax3, ax4)) = plt.subplots(2, 2, sharex=True, sharey=True) # Add reference triangle to both top subplots. for ax in (ax1, ax2): if slide_num == 1: ax.plot([0, 1], [0, 0], color=plot_utils.BLUE) ax.plot([1, 0], [0, 1], color="black", alpha=ALPHA) ax.plot([0, 0], [1, 0], color="black", alpha=ALPHA) elif slide_num == 2: ax.plot([0, 1], [0, 0], color="black", alpha=ALPHA) ax.plot([1, 0], [0, 1], color=plot_utils.BLUE) ax.plot([0, 0], [1, 0], color="black", alpha=ALPHA) elif slide_num == 3: ax.plot([0, 1], [0, 0], color="black", alpha=ALPHA) ax.plot([1, 0], [0, 1], color="black", alpha=ALPHA) ax.plot([0, 0], [1, 0], color=plot_utils.BLUE) else: ax.plot([0, 1], [0, 0], color="black", alpha=ALPHA) ax.plot([1, 0], [0, 1], color="black", alpha=ALPHA) ax.plot([0, 0], [1, 0], color="black", alpha=ALPHA) # Add the "wrong" triangle to both bottom subplots. edge1, edge2, edge3 = bez_triangle.edges for ax in (ax3, ax4): bez_triangle.plot(256, ax=ax, color="black") ax.patches[-1].set_alpha(ALPHA) # Remove the black boundary and add colored. ax.lines.pop() if slide_num == 1: edge1.plot(256, ax=ax, color=plot_utils.BLUE) elif slide_num == 2: edge2.plot(256, ax=ax, color=plot_utils.BLUE) elif slide_num == 3: edge3.plot(256, ax=ax, color=plot_utils.BLUE) # det(J) = 4[t^2 + (s - 1) t + (s - s^2)] sv = np.linspace(0.0, 1.0 / 5.0, 250) sqrt_part = np.sqrt((1.0 - sv) * (1.0 - 5.0 * sv)) jacobian_zero = np.empty((499, 2)) jacobian_zero[:250, 0] = sv jacobian_zero[:250, 1] = 0.5 * (1.0 - sv - sqrt_part) jacobian_zero[250:, 0] = sv[-2::-1] jacobian_zero[250:, 1] = 0.5 * (1.0 - sv[-2::-1] + sqrt_part[-2::-1]) ax2.plot( jacobian_zero[:, 0], jacobian_zero[:, 1], color="black", linestyle="dashed", ) # Now, compute the image of the det(J) = 0 curve under b(s, t). poly4 = np.empty((598, 2)) sv = jacobian_zero[::-1, 0] tv = jacobian_zero[::-1, 1] poly4[:499, 0] = (1.0 - sv - tv) * (1.0 - sv - tv) + sv * sv poly4[:499, 1] = sv * sv + tv * tv ax4.plot(poly4[:499, 0], poly4[:499, 1], color="black", linestyle="dashed") # Combine this with the image b(0, t) tv = np.linspace(0.0, 1.0, 100)[1:] poly4[499:, 0] = (1.0 - tv) * (1.0 - tv) poly4[499:, 1] = tv * tv if slide_num == 6: patch = matplotlib.patches.PathPatch(matplotlib.path.Path(poly4), color="black", alpha=1.5 * ALPHA) ax4.add_patch(patch) for ax in (ax1, ax2, ax3, ax4): ax.set_aspect("equal") # One axis sets all axis ax1.set_xticklabels([]) ax1.set_yticklabels([]) if slide_num < 5: ax4.set_visible(False) if slide_num < 4: ax2.set_visible(False) figure.set_size_inches(3.2, 3.3) figure.subplots_adjust(left=0.01, bottom=0.01, right=0.99, top=0.99, wspace=0.04, hspace=0.0) filename = "boundary_leak{}.pdf".format(slide_num) path = plot_utils.get_path("slides", filename) figure.savefig(path) print("Saved {}".format(filename)) plt.close(figure)