import pymeshlab import vedo filepath = vedo.download(vedo.dataurl+'bunny.obj') ms = pymeshlab.MeshSet() ms.load_new_mesh(filepath) # vedo.show(ms, axes=True) # this already works! pt = [0.02343884, 0.0484675, 0.03972297] ms.colorize_by_geodesic_distance_from_a_given_point(startpoint=pt) mlab_mesh = ms.current_mesh() vedo_mesh = vedo.Mesh(mlab_mesh).cmap('Paired').addScalarBar("distance") print("Can convert back to pymeshlab.MeshSet:", type(vedo_mesh.to_meshlab())) vedo.show("pymeshlab interoperability example", vedo_mesh, vedo.Point(pt), axes=True, bg='green9', bg2='blue9', title="vedo + pymeshlab", ) ################################################################################ # Full list of filters, https://pymeshlab.readthedocs.io/en/latest/filter_list.html # # MeshLab offers plenty of useful filters, among which: # # ambient_occlusion
"""Find the closest point on the mesh to each random point """ import trimesh import numpy as np from vedo import show, Arrows, download plyfile = download( 'https://github.com/mikedh/trimesh/blob/main/models/cycloidal.ply') mesh = trimesh.load(plyfile) points = mesh.bounding_box_oriented.sample_volume(count=30) # find the closest point on the mesh to each random point closest_points, distances, triangle_id = mesh.nearest.on_surface(points) #print('Distance from point to surface of mesh:\n{}'.format(distances)) # create a PointCloud object out of each (n,3) list of points cloud_original = trimesh.points.PointCloud(points) cloud_close = trimesh.points.PointCloud(closest_points) # create a unique color for each point cloud_colors = np.array([trimesh.visual.random_color() for i in points]) # set the colors on the random point and its nearest point to be the same cloud_original.vertices_color = cloud_colors cloud_close.vertices_color = cloud_colors arrs = Arrows(cloud_original.vertices, cloud_close.vertices, c='w') ## create a scene containing the mesh and two sets of points
"""Read and show meshio objects""" import meshio from vedo import download, show, Mesh fpath = download('https://vedo.embl.es/examples/data/shuttle.obj') mesh = meshio.read(fpath) # vedo understands meshio format for polygonal data: # show(mesh, __doc__, axes=7) # explicitly convert it to a vedo.Mesh object: m = Mesh(mesh).lineWidth(1).color('tomato').printInfo() show(m, __doc__, axes=7).close()
import pymeshlab import vedo filepath = vedo.download(vedo.datadir + 'bunny.obj') ms = pymeshlab.MeshSet() ms.load_new_mesh(filepath) # vedo.show(ms, axes=True) # this already works! filter_name = 'close_holes' ms.print_filter_parameter_list(filter_name) ms.apply_filter(filter_name) mlab_mesh = ms.current_mesh() vedo_mesh = vedo.Mesh(mlab_mesh).color('b5').lw(0.1) print("Can convert back to pymeshlab.MeshSet:\n\t", vedo_mesh.to_meshlab()) vedo.show(vedo_mesh, "Applied pymeshlab filter:\n " + filter_name, axes=True, bg='green9', bg2='blue9', title="pymeshlab + vedo") ################################################################################ # MeshLab offers plenty of useful filters, among which: # # ambient_occlusion # compute_curvature_principal_directions
""" trimesh to vedo interoperability """ # Install trimesh with: # sudo apt install python3-rtree # pip install rtree shapely # conda install trimesh import trimesh import vedo from vedo import trimesh2vedo url = 'https://raw.githubusercontent.com/mikedh/trimesh/master/models/' filename = vedo.download(url + 'machinist.XAML') mesh = trimesh.load(filename) vedo.show(mesh) # vedo visualizer (conversion is on the fly) # explicit conversion vmesh = trimesh2vedo(mesh) # returns a vedo.Mesh(vtkActor) object trimsh_reconverted = vmesh.to_trimesh() try: trimsh_reconverted.show() # this is the trimesh built-in visualizer except: pass
import trimesh import numpy as np from vedo import show, Plane, printc, download # load the mesh from filename, file objects are also supported f = download( 'https://github.com/mikedh/trimesh/raw/master/models/featuretype.STL') mesh = trimesh.load_mesh(f) # get a single cross section of the mesh txt = 'cross section of the mesh' mslice = mesh.section(plane_origin=mesh.centroid, plane_normal=[0, 0, 1]) pl = Plane(mesh.centroid, normal=[0, 0, 1], sx=6, sy=4, alpha=0.3) slice_2D, to_3D = mslice.to_planar() # show objects on N=2 non-synced renderers: show([(mesh, pl), (slice_2D, txt)], N=2, sharecam=False, axes=True) # if we wanted to take a bunch of parallel slices, like for a 3D printer # we can do that easily with the section_multiplane method # we're going to slice the mesh into evenly spaced chunks along z # this takes the (2,3) bounding box and slices it into [minz, maxz] z_extents = mesh.bounds[:, 2] # slice every .125 model units (eg, inches) z_levels = np.arange(*z_extents, step=0.125) # find a bunch of parallel cross sections sections = mesh.section_multiplane(plane_origin=mesh.bounds[0], plane_normal=[0, 0, 1],
def furniture(): # generate a whole bunch of planes which correspond to # the geometry in the analysis; tables, bookshelves and so on. from vedo import download reader = vtk.vtkStructuredGridReader() fpath = download('https://vedo.embl.es/examples/data/office.binary.vtk', verbose=0) reader.SetFileName(fpath) reader.Update() sgrid = reader.GetOutput() table1 = vtk.vtkStructuredGridGeometryFilter() table1.SetInputData(sgrid) table1.SetExtent(11, 15, 7, 9, 8, 8) mapTable1 = vtk.vtkPolyDataMapper() mapTable1.SetInputConnection(table1.GetOutputPort()) mapTable1.ScalarVisibilityOff() table1Actor = vtk.vtkActor() table1Actor.SetMapper(mapTable1) table1Actor.GetProperty().SetColor(.59, .427, .392) table2 = vtk.vtkStructuredGridGeometryFilter() table2.SetInputData(sgrid) table2.SetExtent(11, 15, 10, 12, 8, 8) mapTable2 = vtk.vtkPolyDataMapper() mapTable2.SetInputConnection(table2.GetOutputPort()) mapTable2.ScalarVisibilityOff() table2Actor = vtk.vtkActor() table2Actor.SetMapper(mapTable2) table2Actor.GetProperty().SetColor(.59, .427, .392) FilingCabinet1 = vtk.vtkStructuredGridGeometryFilter() FilingCabinet1.SetInputData(sgrid) FilingCabinet1.SetExtent(15, 15, 7, 9, 0, 8) mapFilingCabinet1 = vtk.vtkPolyDataMapper() mapFilingCabinet1.SetInputConnection(FilingCabinet1.GetOutputPort()) mapFilingCabinet1.ScalarVisibilityOff() FilingCabinet1Actor = vtk.vtkActor() FilingCabinet1Actor.SetMapper(mapFilingCabinet1) FilingCabinet1Actor.GetProperty().SetColor(.8, .8, .6) FilingCabinet2 = vtk.vtkStructuredGridGeometryFilter() FilingCabinet2.SetInputData(sgrid) FilingCabinet2.SetExtent(15, 15, 10, 12, 0, 8) mapFilingCabinet2 = vtk.vtkPolyDataMapper() mapFilingCabinet2.SetInputConnection(FilingCabinet2.GetOutputPort()) mapFilingCabinet2.ScalarVisibilityOff() FilingCabinet2Actor = vtk.vtkActor() FilingCabinet2Actor.SetMapper(mapFilingCabinet2) FilingCabinet2Actor.GetProperty().SetColor(.8, .8, .6) bookshelf1Top = vtk.vtkStructuredGridGeometryFilter() bookshelf1Top.SetInputData(sgrid) bookshelf1Top.SetExtent(13, 13, 0, 4, 0, 11) mapBookshelf1Top = vtk.vtkPolyDataMapper() mapBookshelf1Top.SetInputConnection(bookshelf1Top.GetOutputPort()) mapBookshelf1Top.ScalarVisibilityOff() bookshelf1TopActor = vtk.vtkActor() bookshelf1TopActor.SetMapper(mapBookshelf1Top) bookshelf1TopActor.GetProperty().SetColor(.8, .8, .6) bookshelf1Bottom = vtk.vtkStructuredGridGeometryFilter() bookshelf1Bottom.SetInputData(sgrid) bookshelf1Bottom.SetExtent(20, 20, 0, 4, 0, 11) mapBookshelf1Bottom = vtk.vtkPolyDataMapper() mapBookshelf1Bottom.SetInputConnection(bookshelf1Bottom.GetOutputPort()) mapBookshelf1Bottom.ScalarVisibilityOff() bookshelf1BottomActor = vtk.vtkActor() bookshelf1BottomActor.SetMapper(mapBookshelf1Bottom) bookshelf1BottomActor.GetProperty().SetColor(.8, .8, .6) bookshelf1Front = vtk.vtkStructuredGridGeometryFilter() bookshelf1Front.SetInputData(sgrid) bookshelf1Front.SetExtent(13, 20, 0, 0, 0, 11) mapBookshelf1Front = vtk.vtkPolyDataMapper() mapBookshelf1Front.SetInputConnection(bookshelf1Front.GetOutputPort()) mapBookshelf1Front.ScalarVisibilityOff() bookshelf1FrontActor = vtk.vtkActor() bookshelf1FrontActor.SetMapper(mapBookshelf1Front) bookshelf1FrontActor.GetProperty().SetColor(.8, .8, .6) bookshelf1Back = vtk.vtkStructuredGridGeometryFilter() bookshelf1Back.SetInputData(sgrid) bookshelf1Back.SetExtent(13, 20, 4, 4, 0, 11) mapBookshelf1Back = vtk.vtkPolyDataMapper() mapBookshelf1Back.SetInputConnection(bookshelf1Back.GetOutputPort()) mapBookshelf1Back.ScalarVisibilityOff() bookshelf1BackActor = vtk.vtkActor() bookshelf1BackActor.SetMapper(mapBookshelf1Back) bookshelf1BackActor.GetProperty().SetColor(.8, .8, .6) bookshelf1LHS = vtk.vtkStructuredGridGeometryFilter() bookshelf1LHS.SetInputData(sgrid) bookshelf1LHS.SetExtent(13, 20, 0, 4, 0, 0) mapBookshelf1LHS = vtk.vtkPolyDataMapper() mapBookshelf1LHS.SetInputConnection(bookshelf1LHS.GetOutputPort()) mapBookshelf1LHS.ScalarVisibilityOff() bookshelf1LHSActor = vtk.vtkActor() bookshelf1LHSActor.SetMapper(mapBookshelf1LHS) bookshelf1LHSActor.GetProperty().SetColor(.8, .8, .6) bookshelf1RHS = vtk.vtkStructuredGridGeometryFilter() bookshelf1RHS.SetInputData(sgrid) bookshelf1RHS.SetExtent(13, 20, 0, 4, 11, 11) mapBookshelf1RHS = vtk.vtkPolyDataMapper() mapBookshelf1RHS.SetInputConnection(bookshelf1RHS.GetOutputPort()) mapBookshelf1RHS.ScalarVisibilityOff() bookshelf1RHSActor = vtk.vtkActor() bookshelf1RHSActor.SetMapper(mapBookshelf1RHS) bookshelf1RHSActor.GetProperty().SetColor(.8, .8, .6) bookshelf2Top = vtk.vtkStructuredGridGeometryFilter() bookshelf2Top.SetInputData(sgrid) bookshelf2Top.SetExtent(13, 13, 15, 19, 0, 11) mapBookshelf2Top = vtk.vtkPolyDataMapper() mapBookshelf2Top.SetInputConnection(bookshelf2Top.GetOutputPort()) mapBookshelf2Top.ScalarVisibilityOff() bookshelf2TopActor = vtk.vtkActor() bookshelf2TopActor.SetMapper(mapBookshelf2Top) bookshelf2TopActor.GetProperty().SetColor(.8, .8, .6) bookshelf2Bottom = vtk.vtkStructuredGridGeometryFilter() bookshelf2Bottom.SetInputData(sgrid) bookshelf2Bottom.SetExtent(20, 20, 15, 19, 0, 11) mapBookshelf2Bottom = vtk.vtkPolyDataMapper() mapBookshelf2Bottom.SetInputConnection(bookshelf2Bottom.GetOutputPort()) mapBookshelf2Bottom.ScalarVisibilityOff() bookshelf2BottomActor = vtk.vtkActor() bookshelf2BottomActor.SetMapper(mapBookshelf2Bottom) bookshelf2BottomActor.GetProperty().SetColor(.8, .8, .6) bookshelf2Front = vtk.vtkStructuredGridGeometryFilter() bookshelf2Front.SetInputData(sgrid) bookshelf2Front.SetExtent(13, 20, 15, 15, 0, 11) mapBookshelf2Front = vtk.vtkPolyDataMapper() mapBookshelf2Front.SetInputConnection(bookshelf2Front.GetOutputPort()) mapBookshelf2Front.ScalarVisibilityOff() bookshelf2FrontActor = vtk.vtkActor() bookshelf2FrontActor.SetMapper(mapBookshelf2Front) bookshelf2FrontActor.GetProperty().SetColor(.8, .8, .6) bookshelf2Back = vtk.vtkStructuredGridGeometryFilter() bookshelf2Back.SetInputData(sgrid) bookshelf2Back.SetExtent(13, 20, 19, 19, 0, 11) mapBookshelf2Back = vtk.vtkPolyDataMapper() mapBookshelf2Back.SetInputConnection(bookshelf2Back.GetOutputPort()) mapBookshelf2Back.ScalarVisibilityOff() bookshelf2BackActor = vtk.vtkActor() bookshelf2BackActor.SetMapper(mapBookshelf2Back) bookshelf2BackActor.GetProperty().SetColor(.8, .8, .6) bookshelf2LHS = vtk.vtkStructuredGridGeometryFilter() bookshelf2LHS.SetInputData(sgrid) bookshelf2LHS.SetExtent(13, 20, 15, 19, 0, 0) mapBookshelf2LHS = vtk.vtkPolyDataMapper() mapBookshelf2LHS.SetInputConnection(bookshelf2LHS.GetOutputPort()) mapBookshelf2LHS.ScalarVisibilityOff() bookshelf2LHSActor = vtk.vtkActor() bookshelf2LHSActor.SetMapper(mapBookshelf2LHS) bookshelf2LHSActor.GetProperty().SetColor(.8, .8, .6) bookshelf2RHS = vtk.vtkStructuredGridGeometryFilter() bookshelf2RHS.SetInputData(sgrid) bookshelf2RHS.SetExtent(13, 20, 15, 19, 11, 11) mapBookshelf2RHS = vtk.vtkPolyDataMapper() mapBookshelf2RHS.SetInputConnection(bookshelf2RHS.GetOutputPort()) mapBookshelf2RHS.ScalarVisibilityOff() bookshelf2RHSActor = vtk.vtkActor() bookshelf2RHSActor.SetMapper(mapBookshelf2RHS) bookshelf2RHSActor.GetProperty().SetColor(.8, .8, .6) window = vtk.vtkStructuredGridGeometryFilter() window.SetInputData(sgrid) window.SetExtent(20, 20, 6, 13, 10, 13) mapWindow = vtk.vtkPolyDataMapper() mapWindow.SetInputConnection(window.GetOutputPort()) mapWindow.ScalarVisibilityOff() windowActor = vtk.vtkActor() windowActor.SetMapper(mapWindow) windowActor.GetProperty().SetColor(.3, .3, .5) outlet = vtk.vtkStructuredGridGeometryFilter() outlet.SetInputData(sgrid) outlet.SetExtent(0, 0, 9, 10, 14, 16) mapOutlet = vtk.vtkPolyDataMapper() mapOutlet.SetInputConnection(outlet.GetOutputPort()) mapOutlet.ScalarVisibilityOff() outletActor = vtk.vtkActor() outletActor.SetMapper(mapOutlet) outletActor.GetProperty().SetColor(1, 1, 1) inlet = vtk.vtkStructuredGridGeometryFilter() inlet.SetInputData(sgrid) inlet.SetExtent(0, 0, 9, 10, 0, 6) mapInlet = vtk.vtkPolyDataMapper() mapInlet.SetInputConnection(inlet.GetOutputPort()) mapInlet.ScalarVisibilityOff() inletActor = vtk.vtkActor() inletActor.SetMapper(mapInlet) inletActor.GetProperty().SetColor(1, 1, 1) outline = vtk.vtkStructuredGridOutlineFilter() outline.SetInputData(sgrid) mapOutline = vtk.vtkPolyDataMapper() mapOutline.SetInputConnection(outline.GetOutputPort()) outlineActor = vtk.vtkActor() outlineActor.SetMapper(mapOutline) outlineActor.GetProperty().SetColor(1, 1, 1) acts = [] acts.append(table1Actor) acts.append(table2Actor) acts.append(FilingCabinet1Actor) acts.append(FilingCabinet2Actor) acts.append(bookshelf1TopActor) acts.append(bookshelf1BottomActor) acts.append(bookshelf1FrontActor) acts.append(bookshelf1BackActor) acts.append(bookshelf1LHSActor) acts.append(bookshelf1RHSActor) acts.append(bookshelf2TopActor) acts.append(bookshelf2BottomActor) acts.append(bookshelf2FrontActor) acts.append(bookshelf2BackActor) acts.append(bookshelf2LHSActor) acts.append(bookshelf2RHSActor) acts.append(windowActor) acts.append(outletActor) acts.append(inletActor) acts.append(outlineActor) return acts
"""Read a data from ascii file and make a simple analysis visualizing 3 of the 5 dimensions of the dataset""" import numpy as np from vedo import download, Points, show from vedo.pyplot import histogram ################################### Read the csv data: delimiter = ',' fpath = download('https://vedo.embl.es/examples/data/genes.csv') with open(fpath, "r") as f: lines = f.readlines() data = [] for i, lns in enumerate(lines): if i == 0: names = lns.split(delimiter) # read header continue ln = lns.split(delimiter) vals = [float(x) for x in ln] data.append(vals) data = np.array(data) print("Print first 5 rows:\n", names) print(data[:5]) print("Number of rows:", len(data)) ################################################## # extract the columns into separate vectors: g0, g1, g2, g3, g4 = data.T # unpack genes n0, n1, n2, n3, n4 = names # now create and show histograms of the gene expressions
"""Earthquakes of magnitude 2.5+ in the past 30 days areas are proportional to energy release [hover mouse to get more info]""" import pandas, numpy as np from vedo import download, Picture, Polygon, ProgressBar, colorMap, Plotter, Text2D num = 50 # nr of earthquakes to be visualized to define a time window path = download("https://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/2.5_month.csv") usecols = ['time','place','latitude','longitude','depth','mag'] data = pandas.read_csv(path, usecols=usecols)[usecols][::-1].reset_index(drop=True) # reverse list pic = Picture("https://eoimages.gsfc.nasa.gov/images/imagerecords/147000/147190/eo_base_2020_clean_3600x1800.png") pic.pickable(False).level(185).window(120) # add some contrast to the original image scale = [pic.shape[0]/2, pic.shape[1]/2, 1] def GeoCircle(lat, lon, r, res=50): coords = [] sinr, cosr = np.sin(r), np.cos(r) sinlat, coslat = np.sin(lat), np.cos(lat) for phi in np.linspace(0, 2*np.pi, num=res, endpoint=False): clat = np.arcsin(sinlat * cosr + coslat * sinr * np.cos(phi)) clng = lon + np.arctan2(np.sin(phi) * sinr * coslat, cosr - sinlat * np.sin(clat)) coords.append([clng/np.pi + 1, clat*2/np.pi + 1, 0]) return Polygon(nsides=res).points(coords) # warp polygon points to match geo projection centers = [] pb = ProgressBar(0, len(data)) for i, d in data.iterrows(): pb.print("Parsing USGS data..") M = d['mag'] # earthquake estimated magnitude
# Download a large file from a URL link and unzip it # from vedo import download, gunzip, show fgz = download('https://vedo.embl.es/examples/truck.vtk.gz') # 200MB filename = gunzip(fgz) print('gunzip-ped to temporary file:', filename) show(filename)