def asteroid_plots(asteroid_name, asteroid_path, output_directory): """This will generate 4 images for each asteroid. A view along each axis followed by a orthographic view """ # load the asteroid v, f = wavefront.read_obj(asteroid_path) # create the image mfig = graphics.mayavi_figure(offscreen=True) graphics.mayavi_addMesh(mfig, v, f) filename = os.path.join(output_directory, asteroid_name + '_isometric.jpg') graphics.mayavi_savefig(mfig, filename, magnification=4) graphics.mayavi_view(mfig, azimuth=0, elevation=0) filename = os.path.join(output_directory, asteroid_name + '_x.jpg') graphics.mayavi_savefig(mfig, filename, magnification=4) graphics.mayavi_view(mfig, azimuth=90, elevation=0) filename = os.path.join(output_directory, asteroid_name + '_y.jpg') graphics.mayavi_savefig(mfig, filename, magnification=4) graphics.mayavi_view(mfig, azimuth=0, elevation=90) filename = os.path.join(output_directory, asteroid_name + '_z.jpg') graphics.mayavi_savefig(mfig, filename, magnification=4)
def test_face_insertion(pt=np.array([1, 0.1, 0])): v, f = wavefront.read_obj('./integration/cube.obj') mesh_parameters = wavefront.polyhedron_parameters(v, f) D, P, V, E, F, primitive = wavefront.distance_to_mesh( pt, v, f, mesh_parameters) nv, nf = wavefront.face_insertion(pt, v, f, D, P, V, E, F) mfig = graphics.mayavi_figure() graphics.mayavi_addMesh(mfig, nv, nf)
def view_final_reconstruction(data_path): """View the final mesh after reconstruction""" with h5py.File(data_path, 'r') as hf: rv = hf['reconstructed_vertex'] v_keys = np.array(utilities.sorted_nicely(list(rv.keys()))) v = rv[v_keys[-1]][()] f = hf['initial_faces'][()] mfig = graphics.mayavi_figure() graphics.mayavi_addMesh(mfig, v, f)
def main(input_file): mfig = graphics.mayavi_figure() v, f = wavefront.read_obj(input_file) mesh = graphics.mayavi_addMesh(mfig, v, f, color=(1, 0, 0), representation='wireframe') graphics.mayavi_addMesh(mfig, v, f, representation='surface') graphics.mlab.view(*(0, 90, 2.76, np.array([0, 0, 0]))) print(input_file.split('.')) graphics.mayavi_savefig(mfig, '/tmp/isotropic.jpg', magnification=4)
def sphere_into_ellipsoid(img_path): """See if we can turn a sphere into an ellipse by changing the radius of vertices The point cloud (ellipse) should have more points than the initial mesh. When the intial mesh is coarse the resulting mesh will also be heavily faceted, but this will avoid the big holes, and large changes in depth """ # define the sphere vs, fs = wavefront.ellipsoid_mesh(1, 1, 1, density=20, subdivisions=1) ve, fe = wavefront.ellipsoid_mesh(2, 3, 4, density=20, subdivisions=1) mfig = graphics.mayavi_figure(offscreen=True) mesh = graphics.mayavi_addMesh(mfig, vs, fs) ms = mesh.mlab_source index = 0 # in a loop add each vertex of the ellipse into the sphere mesh for jj in range(2): for ii, pt in enumerate(ve): index += 1 filename = os.path.join( img_path, 'sphere_ellipsoid_' + str(index).zfill(6) + '.jpg') graphics.mlab.savefig(filename, magnification=4) mesh_param = wavefront.polyhedron_parameters(vs, fs) vs, fs = wavefront.radius_mesh_incremental_update( pt, vs, fs, mesh_param, max_angle=np.deg2rad(10)) ms.reset(x=vs[:, 0], y=vs[:, 1], z=vs[:, 2], triangles=fs) graphics.mayavi_addPoint(mfig, pt) vs, fs = wavefront.mesh_subdivide(vs, fs, 1) ms.reset(x=vs[:, 0], y=vs[:, 1], z=vs[:, 2], triangles=fs) return 0
def test_radius_sphere_into_ellipse(): """See if we can turn a sphere into an ellipse by changing the radius of vertices The point cloud (ellipse) should have more points than the initial mesh. When the intial mesh is coarse the resulting mesh will also be heavily faceted, but this will avoid the big holes, and large changes in depth """ # define the sphere vs, fs = wavefront.ellipsoid_mesh(1, 1, 1, density=10, subdivisions=0) ve, fe = wavefront.ellipsoid_mesh(2, 3, 4, density=10, subdivisions=2) mfig = graphics.mayavi_figure() mesh = graphics.mayavi_addMesh(mfig, vs, fs) ms = mesh.mlab_source # in a loop add each vertex of the ellipse into the sphere mesh for pt in ve: vs, fs = wavefront.radius_mesh_incremental_update(pt, vs, fs) ms.reset(x=vs[:, 0], y=vs[:, 1], z=vs[:, 2], triangles=fs) input('Enter for mesh subdivision') vs, fs = wavefront.mesh_subdivide(vs, fs, 1) ms.reset(x=vs[:, 0], y=vs[:, 1], z=vs[:, 2], triangles=fs) input('Enter for second round of updating') ve, fe = wavefront.ellipsoid_mesh(2, 3, 4, density=20, subdivisions=2) for pt in ve: vs, fs = wavefront.radius_mesh_incremental_update(pt, vs, fs) ms.reset(x=vs[:, 0], y=vs[:, 1], z=vs[:, 2], triangles=fs) return vs, fs
def cube_into_sphere(img_path): """Transform a cube into a sphere """ vc, fc = wavefront.read_obj('./integration/cube.obj') vs, fs = wavefront.ellipsoid_mesh(2, 2, 2, density=10, subdivisions=0) mfig = graphics.mayavi_figure(offscreen=True) mesh = graphics.mayavi_addMesh(mfig, vc, fc) ms = mesh.mlab_source index = 0 for ii in range(5): for jj, pt in enumerate(vs): index += 1 filename = os.path.join( img_path, 'cube_sphere_' + str(index).zfill(6) + '.jpg') graphics.mlab.savefig(filename, magnification=4) mesh_param = wavefront.polyhedron_parameters(vc, fc) vc, fc = wavefront.radius_mesh_incremental_update( pt, vc, fc, mesh_param, max_angle=np.deg2rad(5)) ms.reset(x=vc[:, 0], y=vc[:, 1], z=vc[:, 2], triangles=fc) graphics.mayavi_addPoint(mfig, pt) vc, fc = wavefront.mesh_subdivide(vc, fc, 1) ms.reset(x=vc[:, 0], y=vc[:, 1], z=vc[:, 2], triangles=fc) return 0
def test_radius_mesh_update_cube(pt=np.array([1, 0, 0])): """Update the mesh by modifying the radius of the closest point """ # load the cube v, f = wavefront.read_obj('./integration/cube.obj') mesh_parameters = wavefront.polyhedron_parameters(v, f) # pick a point nv, nf = wavefront.radius_mesh_incremental_update(pt, v, f, mesh_parameters, max_angle=np.deg2rad(10)) # plot the new mesh mfig = graphics.mayavi_figure() graphics.mayavi_addMesh(mfig, nv, nf) graphics.mayavi_addPoint(mfig, pt) graphics.mayavi_points3d(mfig, v, color=(0, 1, 0)) return nv, nf
def exploration_generate_plots(data_path, img_path='/tmp/diss_explore', magnification=4, step=1, show=True): """Given a HDF5 file generated by explore (C++) this will generate some plots """ if not os.path.exists(img_path): os.makedirs(img_path) with h5py.File(data_path, 'r') as hf: rv = hf['reconstructed_vertex'] rw = hf['reconstructed_weight'] # get all the keys v_keys = np.array(utilities.sorted_nicely(list(rv.keys()))) w_keys = np.array(utilities.sorted_nicely(list(rw.keys()))) v_initial = hf['initial_vertex'][()] f_initial = hf['initial_faces'][()] w_initial = np.squeeze(hf['initial_weight'][()]) # determine the maximum extents of the mesh and add some margin scale = 1.25 max_x = scale*np.max(v_initial[:, 0]) min_x = scale*np.min(v_initial[:, 0]) max_y = scale*np.max(v_initial[:, 1]) min_y = scale*np.min(v_initial[:, 1]) max_z = scale*np.max(v_initial[:, 2]) min_z = scale*np.min(v_initial[:, 2]) """Partial images during the reconstruction""" mfig = graphics.mayavi_figure(offscreen=(not show)) mesh = graphics.mayavi_addMesh(mfig, v_initial, f_initial) ms = mesh.mlab_source graphics.mayavi_axes(mfig, [min_x, max_x, min_x, max_x, min_x, max_x], line_width=5, color=(1, 0, 0)) graphics.mayavi_view(fig=mfig) partial_index = np.array([0, v_keys.shape[0]*1/4, v_keys.shape[0]*1/2, v_keys.shape[0]*3/4, v_keys.shape[0]*4/4-1], dtype=np.int) for img_index, vk in enumerate(partial_index): filename = os.path.join(img_path, 'partial_' + str(vk) + '.jpg') v = rv[str(vk)][()] # generate an image and save it ms.reset(x=v[:, 0], y=v[:, 1], z=v[:,2], triangles=f_initial) graphics.mlab.savefig(filename, magnification=magnification) """Plot the truth asteroid""" v_true = hf['truth_vertex'][()] f_true = hf['truth_faces'][()] ms.reset(x=v_true[:, 0], y=v_true[:, 1], z=v_true[:,2], triangles=f_true) graphics.mayavi_axes(mfig, [min_x, max_x, min_x, max_x, min_x, max_x], line_width=5, color=(1, 0, 0)) graphics.mayavi_view(fig=mfig) graphics.mlab.savefig(os.path.join(img_path, 'truth.jpg' ), magnification=magnification)
def cube_mesh_with_vertices_edges_faces(img_path): """Plot the example cube with all faces, vertices, and edges """ filename = os.path.join(img_path, 'cube_mesh.jpg') size = (800 * 1.618, 800) # read the cube v, f = wavefront.read_obj('./integration/cube.obj') # create the figure mfig = graphics.mayavi_figure(size=size, bg=(1, 1, 1)) # draw the mesh mesh = graphics.mayavi_addMesh(mfig, v, f, color=(0.5, 0.5, 0.5), representation='surface') # draw all the vertices points = graphics.mayavi_points3d(mfig, v, scale_factor=0.1, color=(0, 0, 1)) # draw the edges mesh_edges = graphics.mayavi_addMesh(mfig, v, f, color=(1, 0, 0), representation='mesh') graphics.mlab.view(azimuth=view['azimuth'], elevation=view['elevation'], distance=view['distance'], focalpoint=view['focalpoint'], figure=mfig) # save the figure to eps graphics.mlab.savefig(filename, magnification=4)
def castalia_reconstruction(img_path): """Incrementally modify an ellipse into a low resolution verision of castalia by adding vertices and modifying the mesh """ surf_area = 0.01 a = 0.22 delta = 0.01 # load a low resolution ellipse to start ast = asteroid.Asteroid('castalia', 0, 'obj') ellipsoid = surface_mesh.SurfMesh(ast.axes[0], ast.axes[1], ast.axes[2], 10, 0.025, 0.5) ve, fe = ellipsoid.verts(), ellipsoid.faces() vc, fc = ast.V, ast.F # sort the vertices in in order (x component) vc = vc[vc[:, 0].argsort()] # uncertainty for each vertex in meters (1/variance) vert_weight = np.full(ve.shape[0], (np.pi * np.max(ast.axes))**2) # calculate maximum angle as function of surface area max_angle = wavefront.spherical_surface_area(np.max(ast.axes), surf_area) # loop and create many figures mfig = graphics.mayavi_figure(offscreen=False) mesh = graphics.mayavi_addMesh(mfig, ve, fe) ms = mesh.mlab_source index = 0 for ii, pt in enumerate(vc): index += 1 filename = os.path.join( img_path, 'castalia_reconstruct_' + str(index).zfill(7) + '.jpg') # graphics.mlab.savefig(filename, magnification=4) ve, vert_weight = wavefront.spherical_incremental_mesh_update( pt, ve, fe, vertex_weight=vert_weight, max_angle=max_angle) ms.reset(x=ve[:, 0], y=ve[:, 1], z=ve[:, 2], triangles=fe) graphics.mayavi_addPoint(mfig, pt, radius=0.01) graphics.mayavi_points3d(mfig, ve, scale_factor=0.01, color=(1, 0, 0)) return 0
def test_normal_face_plot(): """Plot the normals to the faces on a mesh and view in Mayavi """ v, f = wavefront.read_obj('./integration/cube.obj') normal_face = wavefront.normal_face(v, f) cof = wavefront.center_of_face(v, f) mfig = graphics.mayavi_figure() _ = graphics.mayavi_addMesh(mfig, v, f) for p1, u in zip(cof, normal_face): graphics.mayavi_addPoint(mfig, p1, radius=0.1, color=(1, 0, 0)) graphics.mayavi_addLine(mfig, p1, u + p1) graphics.mayavi_addTitle(mfig, 'Normals to each face', color=(0, 0, 0), size=0.5)
def test_radius_cube_into_sphere(): """Transform a cube into a sphere """ vc, fc = wavefront.read_obj('./integration/cube.obj') vs, fs = wavefront.ellipsoid_mesh(2, 2, 2, density=10, subdivisions=0) mfig = graphics.mayavi_figure() mesh = graphics.mayavi_addMesh(mfig, vc, fc) graphics.mayavi_points3d(mfig, vc, color=(0, 1, 0)) ms = mesh.mlab_source for ii in range(5): for pt in vs: mesh_param = wavefront.polyhedron_parameters(vc, fc) vc, fc = wavefront.radius_mesh_incremental_update( pt, vc, fc, mesh_param, max_angle=np.deg2rad(45)) ms.reset(x=vc[:, 0], y=vc[:, 1], z=vc[:, 2], triangles=fc) graphics.mayavi_addPoint(mfig, pt) input('Mesh subdivison') vc, fc = wavefront.mesh_subdivide(vc, fc, 1) ms.reset(x=vc[:, 0], y=vc[:, 1], z=vc[:, 2], triangles=fc)
def test_point_insertion_random(): num_points = 100 nv, nf = wavefront.read_obj('./integration/cube.obj') # loop over random points and add them to the cube for ii in range(num_points): pt = np.random.uniform(0.6, 0.7) * sphere.rand(2) mesh_parameters = wavefront.polyhedron_parameters(nv, nf) D, P, V, E, F, primitive = wavefront.distance_to_mesh( pt, nv, nf, mesh_parameters) if primitive == 'vertex': nv, nf = wavefront.vertex_insertion(pt, nv, nf, D, P, V, E, F) elif primitive == 'edge': nv, nf = wavefront.edge_insertion(pt, nv, nf, D, P, V, E, F) elif primitive == 'face': nv, nf = wavefront.face_insertion(pt, nv, nf, D, P, V, E, F) mfig = graphics.mayavi_figure() mesh = graphics.mayavi_addMesh(mfig, nv, nf)
def sphere_into_ellipsoid_spherical_coordinates(img_path): """See if we can turn a sphere into an ellipse by changing the radius of vertices in spherical coordinates The point cloud (ellipse) should have the same number of points than the initial mesh. """ surf_area = 0.06 a = 0.25 # at a*100 % of maximum angle the scale will be 50% of measurement delta = 0.01 # define the sphere # vs, fs = wavefront.ellipsoid_mesh(0.5, 0.5, 0.5, density=10, subdivisions=1) # ve, fe = wavefront.ellipsoid_mesh(1,2, 3, density=10, subdivisions=1) # import the sphere and ellipsoid from matlab files sphere_data = scipy.io.loadmat('./data/sphere_distmesh.mat') ellipsoid_data = scipy.io.loadmat('./data/ellipsoid_distmesh.mat') vs, fs = sphere_data['v'], sphere_data['f'] ve, fe = ellipsoid_data['v'], ellipsoid_data['f'] # sphere = surface_mesh.SurfMesh(0.5, 0.5, 0.5, 10, 0.05, 0.5) # ellipsoid = surface_mesh.SurfMesh(1, 2, 3, 10, 0.2, 0.5) # vs, fs = sphere.verts(), sphere.faces() # ve, fe = ellipsoid.verts(), sphere.faces() print("Sphere V: {} F: {}".format(vs.shape[0], fs.shape[0])) print("Ellipsoid V: {} F: {}".format(ve.shape[0], fe.shape[0])) # convert to spherical coordinates vs_spherical = wavefront.cartesian2spherical(vs) ve_spherical = wavefront.cartesian2spherical(ve) mfig = graphics.mayavi_figure(offscreen=False) mesh = graphics.mayavi_addMesh(mfig, vs, fs) ms = mesh.mlab_source index = 0 # graphics.mayavi_points3d(mfig, vs, color=(1, 0, 0)) # graphics.mayavi_points3d(mfig, ve, color=(0, 1, 0)) # in a loop add each vertex of the ellipse into the sphere mesh for ii, pt in enumerate(ve_spherical): index += 1 filename = os.path.join( img_path, 'sphere_ellipsoid_' + str(index).zfill(6) + '.jpg') # graphics.mlab.savefig(filename, magnification=4) vs_spherical, fs = wavefront.spherical_incremental_mesh_update( mfig, pt, vs_spherical, fs, surf_area=surf_area, a=a, delta=delta) # convert back to cartesian for plotting vs_cartesian = wavefront.spherical2cartesian(vs_spherical) ms.reset(x=vs_cartesian[:, 0], y=vs_cartesian[:, 1], z=vs_cartesian[:, 2], triangles=fs) graphics.mayavi_addPoint(mfig, wavefront.spherical2cartesian(pt), radius=0.02) graphics.mayavi_points3d(mfig, vs_cartesian, scale_factor=0.02, color=(1, 0, 0)) return 0
"""Test out creating and plotting a geodesic waypoint (great circle) on the sphere """ from point_cloud import wavefront from kinematics import sphere from visualization import graphics from lib import geodesic, surface_mesh import numpy as np # generate a sphere for plotting sphere_mesh = surface_mesh.SurfMesh(1, 1, 1, 10, 0.15, 0.5) vs, fs = sphere_mesh.verts(), sphere_mesh.faces() # create two random points on the sphere initial_point_cartesian = sphere.rand(2) final_point_cartesian = sphere.rand(2) # compute waypoints inbetween waypoints_cartesian = geodesic.sphere_waypoint(initial_point_cartesian, final_point_cartesian, 5) # plot everythign on a mayavi figure mfig = graphics.mayavi_figure() graphics.mayavi_axes(mfig, [-1, 1, -1, 1, -1, 1]) graphics.mayavi_addMesh(mfig, vs, fs, color=(0, 0, 1), opacity=0.2) graphics.mayavi_points3d(mfig, waypoints_cartesian, color=(0, 0, 1)) graphics.mayavi_addPoint(mfig, initial_point_cartesian, color=(0, 1, 0)) graphics.mayavi_addPoint(mfig, final_point_cartesian, color=(1, 0, 0))
Author ------ Shankar Kulumani GWU [email protected] """ import numpy as np from lib import surface_mesh from visualization import graphics # generate two triaxial ellipsoids smesh_1 = surface_mesh.SurfMesh(1, 0.5, 0.5, 10, 0.2, 0.1) smesh_2 = surface_mesh.SurfMesh(1, 0.5, 0.5, 10, 0.1, 0.1) view =(45.00000000000001, 54.73561031724535, 2.705956013687095, np.array([ 0.12708219, -0.05595058, -0.07152687])) mfig = graphics.mayavi_figure(offscreen=False) mesh = graphics.mayavi_addMesh(mfig, smesh_1.get_verts(), smesh_1.get_faces(), representation='wireframe') graphics.mlab.view(*view) graphics.mlab.savefig('/tmp/uniform_mesh_coarse.jpg', magnification=4) ms = mesh.mlab_source ms.reset(x=smesh_2.get_verts()[:, 0], y=smesh_2.get_verts()[:, 1], z=smesh_2.get_verts()[:, 2], triangles=smesh_2.get_faces()) graphics.mlab.view(*view) graphics.mlab.savefig('/tmp/uniform_mesh_fine.jpg', magnification=4)
def plot_data(pt, v, f, D, P, V, E, F, string='Closest Primitive', radius=0.1): # draw the mayavi figure mfig = graphics.mayavi_figure() graphics.mayavi_addMesh(mfig, v, f) graphics.mayavi_addPoint(mfig, pt, radius=radius, color=(0, 1, 0)) if P.any(): graphics.mayavi_points3d(mfig, P, scale_factor=radius, color=(1, 0, 0)) # different color for each face if F.size: try: _ = iter(F[0]) for f_list in F: for f_ind in f_list: face_verts = v[f[f_ind, :], :] graphics.mayavi_addMesh(mfig, face_verts, [(0, 1, 2)], color=tuple(np.random.rand(3))) except IndexError as err: face_verts = v[f[F, :], :] graphics.mayavi_addMesh(mfig, face_verts, [(0, 1, 2)], color=tuple(np.random.rand(3))) except (TypeError, ) as err: for f_ind in F: face_verts = v[f[f_ind, :], :] graphics.mayavi_addMesh(mfig, face_verts, [(0, 1, 2)], color=tuple(np.random.rand(3))) # draw the points which make up the edges and draw a line for the edge if V.size: try: _ = iter(V) for v_ind in V: graphics.mayavi_addPoint(mfig, v[v_ind, :], radius=radius, color=(0, 0, 1)) except TypeError as err: graphics.mayavi_addPoint(mfig, v[V, :], radius=radius, color=(0, 0, 1)) # draw edges if E.size: try: _ = iter(E[0][0]) for e_list in E: for e_ind in e_list: graphics.mayavi_addLine(mfig, v[e_ind[0], :], v[e_ind[1], :], color=(0, 0, 0)) except IndexError as err: graphics.mayavi_addLine(mfig, v[E[0], :], v[E[1], :], color=(0, 0, 0)) except (TypeError, ) as err: for e_ind in E: graphics.mayavi_addLine(mfig, v[e_ind[0], :], v[e_ind[1], :], color=(0, 0, 0)) graphics.mayavi_addTitle(mfig, string, color=(0, 0, 0), size=0.5)
def save_animation(filename, output_path, mesh_weight=False, magnification=4): output_path = os.path.join(output_path, 'animation') if not os.path.exists(output_path): os.makedirs(output_path) with h5py.File(filename, 'r') as hf: rv = hf['reconstructed_vertex'] rw = hf['reconstructed_weight'] # get all the keys v_keys = np.array(utilities.sorted_nicely(list(rv.keys()))) w_keys = np.array(utilities.sorted_nicely(list(rw.keys()))) v_initial = hf['initial_vertex'][()] f_initial = hf['initial_faces'][()] w_initial = np.squeeze(hf['initial_weight'][()]) # think about a black background as well mfig = graphics.mayavi_figure(bg=(0, 0, 0), size=(800,600), offscreen=True) if mesh_weight: mesh = graphics.mayavi_addMesh(mfig, v_initial, f_initial, scalars=w_initial, color=None, colormap='viridis') else: mesh = graphics.mayavi_addMesh(mfig, v_initial, f_initial) # xaxis = graphics.mayavi_addLine(mfig, np.array([0, 0, 0]), np.array([2, 0, 0]), color=(1, 0, 0)) # yaxis = graphics.mayavi_addLine(mfig, np.array([0, 0, 0]), np.array([0, 2, 0]), color=(0, 1, 0)) # zaxis = graphics.mayavi_addLine(mfig, np.array([0, 0, 0]), np.array([0, 0, 2]), color=(0, 0, 1)) # ast_axes = (xaxis, yaxis, zaxis) print("Images will be saved to {}".format(output_path)) ms = mesh.mlab_source # determine the maximum extents of the mesh and add some margin scale = 1.25 max_x = scale*np.max(v_initial[:, 0]) min_x = scale*np.min(v_initial[:, 0]) max_y = scale*np.max(v_initial[:, 1]) min_y = scale*np.min(v_initial[:, 1]) max_z = scale*np.max(v_initial[:, 2]) min_z = scale*np.min(v_initial[:, 2]) graphics.mayavi_axes(mfig, [min_x, max_x, min_x, max_x, min_x, max_x], line_width=5, color=(1, 0, 0)) graphics.mayavi_view(fig=mfig) # loop over keys and save images for img_index, vk in enumerate(v_keys): filename = os.path.join(output_path, str(vk).zfill(7) + '.jpg') new_vertices = rv[str(vk)][()] new_faces = f_initial new_weight = rw[str(vk)][()] # generate an image and save it if mesh_weight: ms.set(x=new_vertices[:, 0],y=new_vertices[:, 1], z=new_vertices[:,2], triangles=new_faces, scalars=new_weight) else: ms.set(x=new_vertices[:, 0],y=new_vertices[:, 1], z=new_vertices[:,2], triangles=new_faces) graphics.mlab.savefig(filename, magnification=magnification) # now call ffmpeg fps = 60 name_str = "reconstruction_" + datetime.datetime.now().strftime("%Y%m%dT%H%M%S") + ".mp4" name = os.path.join(output_path, name_str) ffmpeg_fname = os.path.join(output_path, '%07d.jpg') cmd = "ffmpeg -framerate {} -i {} -c:v libx264 -profile:v high -crf 20 -pix_fmt yuv420p -vf 'scale=trunc(iw/2)*2:trunc(ih/2)*2' {}".format(fps, ffmpeg_fname, name) print(cmd) subprocess.check_output(['bash', '-c', cmd]) # remove folder now for file in os.listdir(output_path): file_path = os.path.join(output_path, file) if file_path.endswith('.jpg'): os.remove(file_path)
for ints in intersections_bsp: graphics.mayavi_addPoint(fig, ints, radius=0.02, color=(1, 1, 0)) graphics.mlab.orientation_axes(figure=fig) # graphics.mlab.text3d(0, 0, 0.1, 'Green: OBBTree', figure=fig, scale=0.1) # graphics.mlab.text3d(0, 0, -0.1, 'Yellow: BSPTree', figure=fig, scale=0.1) # cgal version v, f = wavefront.read_obj(filename) mesh = mesh_data.MeshData(v, f) caster = cgal.RayCaster(mesh) intersections = caster.castarray(pos, targets) mfig = graphics.mayavi_figure() graphics.mayavi_addMesh(mfig, mesh.get_verts(), mesh.get_faces()) graphics.mayavi_addPoint(mfig, pos, radius=0.05) # draw lines from pos to all targets for pt in targets: graphics.mayavi_addLine(mfig, pos, pt, color=(1, 0, 0)) # draw a point at all intersections # for ints in intersections_obb: # graphics.mayavi_addPoint(fig, ints, radius=0.01, color=(0, 1, 0)) for ints in intersections: graphics.mayavi_addPoint(mfig, ints, radius=0.02, color=(1, 1, 0)) graphics.mlab.orientation_axes(figure=mfig)
fs.shape[0])) print("Distmesh Ellipsoid: Vertices: {} Faces: {}".format( ve.shape[0], fe.shape[0])) print("SurfMesh Sphere: Vertices: {} Faces: {}".format( sphere.verts().shape[0], sphere.faces().shape[0])) print("SurfMesh Ellipsoid: Vertices: {} Faces: {}".format( ellipsoid.verts().shape[0], ellipsoid.faces().shape[0])) mfig_distmesh = graphics.mayavi_figure() mfig_surfmesh = graphics.mayavi_figure() mfig_distmesh_ellipsoid = graphics.mayavi_figure() mfig_surfmesh_ellipsoid = graphics.mayavi_figure() graphics.mayavi_addMesh(mfig_distmesh, vs, fs, representation='wireframe') graphics.mayavi_addTitle(mfig_distmesh, "Distmesh", color=(0, 0, 0)) graphics.mayavi_addMesh(mfig_surfmesh, sphere.verts(), sphere.faces(), representation='wireframe') graphics.mayavi_addTitle(mfig_surfmesh, "CGAL Surface Mesh generation", color=(0, 0, 0)) graphics.mayavi_addMesh(mfig_distmesh_ellipsoid, ve, fe, representation='wireframe') graphics.mayavi_addTitle(mfig_distmesh_ellipsoid, "Distmesh", color=(0, 0, 0)) graphics.mayavi_addMesh(mfig_surfmesh_ellipsoid,
mfig = graphics.mayavi_figure() # mesh_param = wavefront.polyhedron_parameters(v, f) pt = np.array([0, 0, 1]) vert_weight = np.full(vs.shape[0], (np.pi * np.max(np.linalg.norm(vs, axis=1)))**2) vs_new, vw_new = wavefront.spherical_incremental_mesh_update( pt, vs, fs, vertex_weight=vert_weight, max_angle=max_angle) # view the mesh graphics.mayavi_addMesh(mfig, vs_new, fs, representation='surface', scalars=vw_new, colormap='viridis', color=None) graphics.mayavi_addPoint(mfig, pt) # duplicate the same thing with c++ mfig_cpp = graphics.mayavi_figure() vw = np.full(vs.shape[0], (np.pi * np.max(np.linalg.norm(vs, axis=1)))**2) # mesh = mesh_data.MeshData(vs, fs) rmesh = reconstruct.ReconstructMesh(vs, fs, vw) rmesh.update(pt, max_angle) graphics.mayavi_addMesh(mfig_cpp,
def read_mesh_reconstruct(filename, output_path='/tmp/reconstruct_images'): """Use H5PY to read the data back and plot """ logger = logging.getLogger(__name__) logger.info('Starting the image generation') # check if location exists if not os.path.exists(output_path): os.makedirs(output_path) logger.info('Opening {}'.format(filename)) with h5py.File(filename, 'r') as hf: rv = hf['reconstructed_vertex'] rf = hf['reconstructed_face'] rw = hf['reconstructed_weight'] # get all the keys for the groups v_keys = np.array(utilities.sorted_nicely(list(rv.keys()))) f_keys = np.array(utilities.sorted_nicely(list(rf.keys()))) w_keys = np.array(utilities.sorted_nicely(list(rw.keys()))) v_initial = hf['initial_vertex'][()] f_initial = hf['initial_faces'][()] w_initial = hf['initial_weight'][()] """Partial images during the reconstruction""" logger.info('Starting on partial reconstruction images') mfig = graphics.mayavi_figure(offscreen=True) mesh = graphics.mayavi_addMesh(mfig, v_initial, f_initial) ms = mesh.mlab_source graphics.mayavi_axes(mfig, [-1, 1, -1, 1, -1, 1], line_width=5, color=(1, 0, 0)) graphics.mayavi_view(fig=mfig) partial_index = np.array([ 0, v_keys.shape[0] * 1 / 4, v_keys.shape[0] * 1 / 2, v_keys.shape[0] * 3 / 4, v_keys.shape[0] * 4 / 4 - 1 ], dtype=np.int) for img_index, vk in enumerate(partial_index): filename = os.path.join(output_path, 'partial_' + str(vk) + '.jpg') v = rv[str(vk)][()] # generate an image and save it ms.reset(x=v[:, 0], y=v[:, 1], z=v[:, 2], triangles=f_initial) graphics.mlab.savefig(filename, magnification=4) """Partial images using a colormap for the data""" logger.info('Now using a colormap for the uncertainty') mfig = graphics.mayavi_figure(offscreen=True) mesh = graphics.mayavi_addMesh(mfig, v_initial, f_initial, color=None, colormap='viridis', scalars=w_initial) ms = mesh.mlab_source graphics.mayavi_axes(mfig, [-1, 1, -1, 1, -1, 1], line_width=5, color=(1, 0, 0)) graphics.mayavi_view(fig=mfig) partial_index = np.array([ 0, v_keys.shape[0] * 1 / 4, v_keys.shape[0] * 1 / 2, v_keys.shape[0] * 3 / 4, v_keys.shape[0] * 4 / 4 - 1 ], dtype=np.int) for img_index, vk in enumerate(partial_index): filename = os.path.join(output_path, 'partial_weights_' + str(vk) + '.jpg') v = rv[str(vk)][()] w = rw[str(vk)][()] # generate an image and save it ms.reset(x=v[:, 0], y=v[:, 1], z=v[:, 2], triangles=f_initial, scalars=w) graphics.mlab.savefig(filename, magnification=4) """Generate the completed shape at a variety of different angles""" logger.info('Now generating some views of the final shape') # change the mesh to the finished mesh ms.reset(x=rv[v_keys[-1]][()][:, 0], y=rv[v_keys[-1]][()][:, 1], z=rv[v_keys[-1]][()][:, 2], triangles=f_initial) elevation = np.array([30, -30]) azimuth = np.array([0, 45, 135, 215, 315]) for az, el in itertools.product(azimuth, elevation): filename = os.path.join( output_path, 'final_az=' + str(az) + '_el=' + str(el) + '.jpg') graphics.mayavi_view(fig=mfig, azimuth=az, elevation=el) graphics.mlab.savefig(filename, magnification=4) """Create a bunch of images for animation""" logger.info('Now making images for a movie') animation_path = os.path.join(output_path, 'animation') if not os.path.exists(animation_path): os.makedirs(animation_path) ms.reset(x=v_initial[:, 0], y=v_initial[:, 1], z=v_initial[:, 2], triangles=f_initial) for ii, vk in enumerate(v_keys): filename = os.path.join(animation_path, str(ii).zfill(7) + '.jpg') v = rv[vk][()] ms.reset(x=v[:, 0], y=v[:, 1], z=v[:, 2], triangles=f_initial) graphics.mayavi_savefig(mfig, filename, magnification=4) logger.info('Finished') return mfig
def main(input_file): mfig = graphics.mayavi_figure() v, f = wavefront.read_obj(input_file) mesh = graphics.mayavi_addMesh(mfig, v, f, representation='wireframe')
import pdb import numpy as np import os import h5py import argparse from point_cloud import wavefront from visualization import graphics import utilities view = (0, 25, 3.32, np.array([-0.04, -0.015, -0.029])) # load the bumpy castalia vb_true, fb_true = wavefront.read_obj( './data/shape_model/CASTALIA/castalia_bump_2.obj') mfig = graphics.mayavi_figure(offscreen=False) mesh = graphics.mayavi_addMesh(mfig, vb_true, fb_true) graphics.mayavi_view(mfig, *view) graphics.mayavi_savefig(mfig, '/tmp/castalia_bump_true.jpg', magnification=4) # load the refinement final version with h5py.File('./data/exploration/refine/20180619_castalia_refinement.hdf5', 'r') as hf: # oriignal vertices est_initial_vertices = hf[ 'simulation_parameters/estimate_asteroid/initial_vertices'][()] num_vert = est_initial_vertices.shape[0] rv_group = hf['refinement/reconstructed_vertex'] rf_group = hf['refinement/reconstructed_face'] rw_group = hf['refinement/reconstructed_weight'] Ra_group = hf['refinement/Ra']
def asteroid_generate_plots(data_path, img_path='/tmp/diss_reconstruct', magnification=1, step=10): """Given a HDF5 file this will read the data and create a bunch of plots/images """ # check and create output directory if not existed if not os.path.exists(img_path): os.makedirs(img_path) with h5py.File(data_path, 'r') as hf: rv = hf['reconstructed_vertex'] rw = hf['reconstructed_weight'] # get all the keys for the groups v_keys = np.array(utilities.sorted_nicely(list(rv.keys()))) w_keys = np.array(utilities.sorted_nicely(list(rw.keys()))) v_initial = hf['initial_vertex'][()] f_initial = hf['initial_faces'][()] w_initial = hf['initial_weight'][()] """Partial images during the reconstruction""" mfig = graphics.mayavi_figure(offscreen=True) mesh = graphics.mayavi_addMesh(mfig, v_initial, f_initial) ms = mesh.mlab_source graphics.mayavi_axes(mfig, [-1, 1, -1, 1, -1, 1], line_width=5, color=(1, 0, 0)) graphics.mayavi_view(fig=mfig) partial_index = np.array([ 0, v_keys.shape[0] * 1 / 4, v_keys.shape[0] * 1 / 2, v_keys.shape[0] * 3 / 4, v_keys.shape[0] * 4 / 4 - 1 ], dtype=np.int) for img_index, vk in enumerate(partial_index): filename = os.path.join(img_path, 'partial_' + str(vk) + '.jpg') v = rv[str(vk)][()] # generate an image and save it ms.reset(x=v[:, 0], y=v[:, 1], z=v[:, 2], triangles=f_initial) graphics.mlab.savefig(filename, magnification=4) """Partial images using a colormap for the data""" mfig = graphics.mayavi_figure(offscreen=True) mesh = graphics.mayavi_addMesh(mfig, v_initial, f_initial, color=None, colormap='viridis', scalars=w_initial) ms = mesh.mlab_source graphics.mayavi_axes(mfig, [-1, 1, -1, 1, -1, 1], line_width=5, color=(1, 0, 0)) graphics.mayavi_view(fig=mfig) partial_index = np.array([ 0, v_keys.shape[0] * 1 / 4, v_keys.shape[0] * 1 / 2, v_keys.shape[0] * 3 / 4, v_keys.shape[0] * 4 / 4 - 1 ], dtype=np.int) for img_index, vk in enumerate(partial_index): filename = os.path.join(img_path, 'partial_weights_' + str(vk) + '.jpg') v = rv[str(vk)][()] w = rw[str(vk)][()] # generate an image and save it ms.reset(x=v[:, 0], y=v[:, 1], z=v[:, 2], triangles=f_initial, scalars=w) graphics.mlab.savefig(filename, magnification=4) """Generate the completed shape at a variety of different angles""" # change the mesh to the finished mesh ms.reset(x=rv[v_keys[-1]][()][:, 0], y=rv[v_keys[-1]][()][:, 1], z=rv[v_keys[-1]][()][:, 2], triangles=f_initial) elevation = np.array([30, -30]) azimuth = np.array([0, 45, 135, 215, 315]) for az, el in itertools.product(azimuth, elevation): filename = os.path.join( img_path, 'final_az=' + str(az) + '_el=' + str(el) + '.jpg') graphics.mayavi_view(fig=mfig, azimuth=az, elevation=el) graphics.mlab.savefig(filename, magnification=4) """Create a bunch of images for animation""" animation_path = os.path.join(img_path, 'animation') if not os.path.exists(animation_path): os.makedirs(animation_path) ms.reset(x=v_initial[:, 0], y=v_initial[:, 1], z=v_initial[:, 2], triangles=f_initial, scalars=w_initial) graphics.mayavi_view(mfig) for ii, vk in enumerate(v_keys[::step]): filename = os.path.join(animation_path, str(ii).zfill(7) + '.jpg') v = rv[vk][()] w = rw[str(vk)][()] ms.reset(x=v[:, 0], y=v[:, 1], z=v[:, 2], triangles=f_initial, scalars=w) graphics.mayavi_savefig(mfig, filename, magnification=magnification) return 0