def generate_point_cloud(max_size=10): np.random.seed(1) # generate random plane with hole plane = generate_3d_plane( bounds_x=[0, max_size, 0.5], bounds_y=[0, max_size, 0.5], holes=[], #holes=[[[3, 5], [3, 5]]], height_noise=0.05, planar_noise=0.02) # Add double plane to simulate extra noise # plane2 = plane + [0.1, 0.1, 0.05] # plane = np.vstack((plane, plane2)) # Generate top of box (causing the hole that we see) box_top = generate_3d_plane(bounds_x=[3, 5, 0.2], bounds_y=[3, 5, 0.2], holes=[], height_noise=0.02, height=2, planar_noise=0.02) # Generate side of box (causing the hole that we see) box_side = generate_3d_plane(bounds_x=[0, 2, 0.2], bounds_y=[0, 2, 0.2], holes=[], height_noise=0.02, planar_noise=0.02) rm = rotation_matrix([0, 1, 0], -math.pi / 2.0) box_side = apply_rotation(rm, box_side) + [5, 3, 0] # box_side = r.apply(box_side) + [5, 3, 0] # All points joined together points = np.ascontiguousarray(np.concatenate((plane, box_side, box_top))) return points
def main(): np.random.seed(1) # generate random plane with hole plane = generate_3d_plane(bounds_x=[0, 10, 0.5], bounds_y=[0, 10, 0.5], holes=[[[3, 5], [3, 5]]], height_noise=0.02, planar_noise=0.02) # Generate top of box (causing the hole that we see) box_top = generate_3d_plane(bounds_x=[3, 5, 0.2], bounds_y=[3, 5, 0.2], holes=[], height_noise=0.02, height=2, planar_noise=0.02) # Generate side of box (causing the hole that we see) box_side = generate_3d_plane(bounds_x=[0, 2, 0.2], bounds_y=[0, 2, 0.2], holes=[], height_noise=0.02, planar_noise=0.02) rm = rotation_matrix([0, 1, 0], -math.pi / 2.0) box_side = apply_rotation(rm, box_side) + [5, 3, 0] # All points joined together points = np.concatenate((plane, box_side, box_top)) points_mat = MatrixDouble(points) polylidar_kwargs = dict(alpha=0.0, lmax=1.0, min_triangles=20, z_thresh=0.1, norm_thresh_min=0.94) polylidar = Polylidar3D(**polylidar_kwargs) fig, ax = plt.subplots(figsize=(10, 10), nrows=1, ncols=1, subplot_kw=dict(projection='3d')) # plot points ax.scatter(*scale_points(points), s=2.5, c=points[:, 2], cmap=plt.cm.plasma) set_axes_equal(ax) ax.view_init(elev=15., azim=-35) plt.show() # Extracts planes and polygons, time t1 = time.time() mesh, planes, polygons = polylidar.extract_planes_and_polygons(points_mat) t2 = time.time() print("Took {:.2f} milliseconds".format((t2 - t1) * 1000)) print("Should see two planes extracted, please rotate.") triangles = np.asarray(mesh.triangles) fig, ax = plt.subplots(figsize=(10, 10), nrows=1, ncols=1, subplot_kw=dict(projection='3d')) # plot all triangles plot_planes_3d(points, triangles, planes, ax) plot_polygons_3d(points, polygons, ax) # plot points ax.scatter(*scale_points(points), c='k', s=0.1) set_axes_equal(ax) ax.view_init(elev=15., azim=-35) plt.show() print("")
def main(): # np.random.seed(5) np.random.seed(9) # generate random plane with hole plane = generate_3d_plane(bounds_x=[0, 10, 0.5], bounds_y=[0, 10, 0.5], holes=[[[3, 5], [3, 5]]], height_noise=0.05, planar_noise=0.10) # Generate top of box (causing the hole that we see) box_top = generate_3d_plane(bounds_x=[3, 5, 0.2], bounds_y=[3, 5, 0.2], holes=[], height_noise=0.02, height=2, planar_noise=0.02) # Generate side of box (causing the hole that we see) box_side = generate_3d_plane(bounds_x=[0, 2, 0.2], bounds_y=[0, 2, 0.2], holes=[], height_noise=0.02, planar_noise=0.02) rm = rotation_matrix([0, 1, 0], -math.pi / 2.0) box_side = apply_rotation(rm, box_side) + [5, 3, 0] # box_side = r.apply(box_side) + [5, 3, 0] # All points joined together points = np.concatenate((plane, box_side, box_top)) points_mat = MatrixDouble(points) polylidar_kwargs = dict(alpha=0.0, lmax=1.0, min_triangles=20, z_thresh=0.1, norm_thresh_min=0.94) polylidar = Polylidar3D(**polylidar_kwargs) elev = 15.0 azim = -125 # Extracts planes and polygons, time t1 = time.time() mesh, planes, polygons = polylidar.extract_planes_and_polygons(points_mat) t2 = time.time() print("Polylidar Took {:.2f} milliseconds".format((t2 - t1) * 1000)) triangles = np.asarray(mesh.triangles) all_planes = [np.arange(triangles.shape[0])] print("") print("Should see a mesh segments") fig, ax = plt.subplots(figsize=(10, 10), nrows=1, ncols=1, subplot_kw=dict(projection='3d')) # plot all triangles # plot_polygons_3d(points, polygons, ax) rm_45 = rotation_matrix([0, 1, 0], -math.pi / 4.0) points_rot = apply_rotation(rm_45, points) plot_planes_3d(points_rot, triangles, [planes[1]], ax, alpha=[0.85]) # plot points # ax.scatter(*scale_points(points), c='k', s=0.1) set_axes_equal(ax) ax.view_init(elev=elev, azim=azim) ax.set_xlabel('X') ax.set_ylabel('Y') ax.set_zlabel('Z') fig.savefig("assets/scratch/PolygonExtraction_a.pdf", bbox_inches='tight') # fig.savefig("assets/scratch/Basic25DAlgorithm_polygons.png", bbox_inches='tight', pad_inches=-0.8) plt.show() # Show 2D Projection and Polygon Extraction print("") print("Should see projected vertices to geometric plane") fig, ax = plt.subplots(nrows=1, ncols=1, figsize=(5, 5)) simplices_plane = triangles[planes[1], :] plot_polygons([polygons[1]], points[:, :2], ax, shell_color=COLOR_PALETTE[4], hole_color=COLOR_PALETTE[4]) ax.triplot(points[:, 0], points[:, 1], simplices_plane, c='k', alpha=0.75) xlim = ax.get_xlim() ylim = ax.get_ylim() ax.set_xlabel('X') ax.set_ylabel('Y') fig.savefig("assets/scratch/PolygonExtraction_b1.pdf", bbox_inches='tight') plt.show() # Show 2D Projection and Polygon Extraction print("") print("Should see projected vertices to geometric plane") fig, ax = plt.subplots(nrows=1, ncols=1, figsize=(5, 5)) simplices_plane = triangles[planes[1], :] plot_polygons([polygons[1]], points[:, :2], ax) ax.triplot(points[:, 0], points[:, 1], simplices_plane, c='k', alpha=0.75) ax.set_xlabel('X') ax.set_ylabel('Y') fig.savefig("assets/scratch/PolygonExtraction_b2.pdf", bbox_inches='tight') plt.show() poly = polygons[1] shell_coords = get_points(poly.shell, points) hole_coords = [get_points(hole, points) for hole in poly.holes] poly_shape = Polygon(shell=shell_coords, holes=hole_coords) # Show Simplification print("") print("Should see simplified Polygon") fig, ax = plt.subplots(nrows=1, ncols=1, figsize=(5, 5)) patch = PolygonPatch(poly_shape, fill=True, alpha=0.5) ax.add_patch(patch) # plot_polygons([polygons[1]], points[:, :2], ax) poly_simplify = poly_shape.simplify(.2, preserve_topology=True) patch = PolygonPatch(poly_simplify, color='k', fill=False, linewidth=1.5, linestyle='--', zorder=1) ax.add_patch(patch) ax.set_xlim(xlim) ax.set_ylim(ylim) print(f"Before Vertices: {len(poly_shape.exterior.coords)}") print(f"After Vertices: {len(poly_simplify.exterior.coords)}") ax.set_xlabel('X') ax.set_ylabel('Y') fig.savefig("assets/scratch/PolygonExtraction_c.pdf", bbox_inches='tight') plt.show() # Show Positive Buffer print("") print("Should see positive buffer Polygon") fig, ax = plt.subplots(nrows=1, ncols=1, figsize=(5, 5)) patch = PolygonPatch(poly_shape, fill=True, alpha=0.5) ax.add_patch(patch) # plot_polygons([polygons[1]], points[:, :2], ax) poly_buffer = poly_simplify.buffer(.2) patch = PolygonPatch(poly_buffer, color='k', fill=False, linewidth=1.5, linestyle='--', zorder=1) ax.add_patch(patch) ax.set_xlim(xlim) ax.set_ylim(ylim) print(f"After Vertices: {len(poly_buffer.exterior.coords)}") ax.set_xlabel('X') ax.set_ylabel('Y') fig.savefig("assets/scratch/PolygonExtraction_d.pdf", bbox_inches='tight') plt.show() # Show Negative Buffer print("") print("Should see negative buffer Polygon") fig, ax = plt.subplots(nrows=1, ncols=1, figsize=(5, 5)) patch = PolygonPatch(poly_shape, fill=True, alpha=0.5) ax.add_patch(patch) # plot_polygons([polygons[1]], points[:, :2], ax) poly_buffer = poly_buffer.buffer(-.2) patch = PolygonPatch(poly_buffer, color='k', fill=False, linewidth=1.5, linestyle='--', zorder=1) ax.add_patch(patch) ax.set_xlim(xlim) ax.set_ylim(ylim) print(f"After Vertices: {len(poly_buffer.exterior.coords)}") ax.set_xlabel('X') ax.set_ylabel('Y') fig.savefig("assets/scratch/PolygonExtraction_e.pdf", bbox_inches='tight') plt.show()
def main(): np.random.seed(1) # generate random plane with hole plane = generate_3d_plane(bounds_x=[0, 10, 0.5], bounds_y=[0, 10, 0.5], holes=[ [[3, 5], [3, 5]]], height_noise=0.02, planar_noise=0.02) # Generate top of box (causing the hole that we see) box_top = generate_3d_plane(bounds_x=[3, 5, 0.2], bounds_y=[3, 5, 0.2], holes=[ ], height_noise=0.02, height=2, planar_noise=0.02) # Generate side of box (causing the hole that we see) box_side = generate_3d_plane(bounds_x=[0, 2, 0.2], bounds_y=[ 0, 2, 0.2], holes=[], height_noise=0.02, planar_noise=0.02) rm = rotation_matrix([0, 1, 0], -math.pi / 2.0) box_side = apply_rotation(rm, box_side) + [5, 3, 0] # All points joined together points = np.concatenate((plane, box_side, box_top)) points_mat = MatrixDouble(points) polylidar_kwargs = dict(alpha=0.0, lmax=1.0, min_triangles=20, z_thresh=0.1, norm_thresh_min=0.94) polylidar = Polylidar3D(**polylidar_kwargs) elev = 15.0 azim = -35 # Show Point Cloud print("Should see point raw point cloud") fig, ax = plt.subplots(figsize=(10, 10), nrows=1, ncols=1, subplot_kw=dict(projection='3d')) # plot points ax.scatter(*scale_points(points), s=20.0, c=points[:, 2], cmap=plt.cm.plasma) set_axes_equal(ax) ax.view_init(elev=elev, azim=azim) fig.savefig("assets/scratch/Basic25DAlgorithm_pointcloud.pdf", bbox_inches='tight') fig.savefig("assets/scratch/Basic25DAlgorithm_pointcloud.png", bbox_inches='tight', pad_inches=-0.8) plt.show() # Extracts planes and polygons, time t1 = time.time() mesh, planes, polygons = polylidar.extract_planes_and_polygons(points_mat) t2 = time.time() print("Polylidar Took {:.2f} milliseconds".format((t2 - t1) * 1000)) triangles = np.asarray(mesh.triangles) all_planes = [np.arange(triangles.shape[0])] # Show Triangulation fig, ax = plt.subplots(figsize=(10, 10), nrows=1, ncols=1, subplot_kw=dict(projection='3d')) plot_planes_3d(points, triangles, all_planes, ax, alpha=0.0, z_value=-4.0) plot_planes_3d(points, triangles, all_planes, ax, alpha=0.5) # plot points ax.scatter(*scale_points(points), s=0.1, c='k') set_axes_equal(ax, ignore_z=True) ax.set_zlim3d([-4, 6]) ax.view_init(elev=elev, azim=azim) print("Should see triangulation point cloud") fig.savefig("assets/scratch/Basic25DAlgorithm_mesh.pdf", bbox_inches='tight') fig.savefig("assets/scratch/Basic25DAlgorithm_mesh.png", bbox_inches='tight', pad_inches=-0.8) plt.show() print("") print("Should see two planes extracted, please rotate.") fig, ax = plt.subplots(figsize=(10, 10), nrows=1, ncols=1, subplot_kw=dict(projection='3d')) # plot all triangles plot_polygons_3d(points, polygons, ax) plot_planes_3d(points, triangles, planes, ax) # plot points ax.scatter(*scale_points(points), c='k', s=0.1) set_axes_equal(ax) ax.view_init(elev=elev, azim=azim) fig.savefig("assets/scratch/Basic25DAlgorithm_polygons.pdf", bbox_inches='tight') fig.savefig("assets/scratch/Basic25DAlgorithm_polygons.png", bbox_inches='tight', pad_inches=-0.8) plt.show() print("")