# Align 2 shapes and for each vertex of the first draw # and arrow to the closest point of the second. # The source transformation is saved in actor.transform # rigid=True doesn't allow scaling # from vtkplotter import Plotter, printc, mag2 from vtkplotter.analysis import align vp = Plotter(verbose=0, axes=4) limb = vp.load('data/270.vtk', alpha=0.3) rim = vp.load('data/270_rim.vtk') rim.color('r').lineWidth(4) arim = align(rim, limb, iters=100, rigid=True) arim.color('g').lineWidth(4) vp.actors.append(arim) d = 0 prim = arim.coordinates() for p in prim: cpt = limb.closestPoint(p) vp.arrow(p, cpt, c='g') d += mag2(p - cpt) # square of residual distance printc('ave. squared distance =', d / len(prim), c='g') vp.show()
a1 = vp.load('data/beethoven.ply', alpha=1) coords1 = a1.coordinates() pts1 = vp.points(coords1, r=4, c='g', legend='#points = ' + str(len(coords1))) vp.show([a1, pts1], at=0) a2 = a1.subdivide(method=0) # Increasing the number of points of the mesh coords2 = a2.coordinates() pts2 = vp.points(coords2, r=1, legend='#points = ' + str(len(coords2))) vp.show([a2, pts2], at=1, interactive=True) ######################################################################################## # Draw a bunch of simple objects on separate parts of the rendering window: # split window to best accomodate 9 renderers vp = Plotter(N=9, title='basic shapes') vp.sharecam = False # each object can be moved independently vp.show(at=0, actors=vp.arrow([0, 0, 0], [1, 1, 1]), legend='arrow') vp.show(at=1, actors=vp.line([0, 0, 0], [1, 1, 1]), legend='line') vp.show(at=2, actors=vp.points([[0, 0, 0], [1, 1, 1]]), legend='points') vp.show(at=3, actors=vp.text('Hello!')) vp.show(at=4, actors=vp.sphere()) vp.show(at=5, actors=vp.cube(), legend='cube') vp.show(at=6, actors=vp.ring(), legend='ring') vp.show(at=7, actors=vp.helix(), legend='helix') vp.show(at=8, actors=vp.cylinder(), legend='cylinder', interactive=1) ######################################################################################## # Draw a bunch of objects from various mesh formats. Loading is automatic. vp = Plotter(shape=(3, 3), title='mesh formats') # split window in 3 rows and 3 columns vp.sharecam = False # each object can be moved independently vp.show('data/beethoven.ply', at=0, c=0, axes=0,
# In this example we fit a plane to regions of a surface defined by # N points that are closest to a given point of the surface. # For some of these point we show the fitting plane. # Blue points are the N points used for fitting. # Green histogram is the distribution of residuals from the fitting. # Both plane center and normal can be accessed from the # attribute plane.info['center'] and plane.info['normal']. # from __future__ import division, print_function from vtkplotter import Plotter from vtkplotter.analysis import fitPlane vp = Plotter(verbose=0, axes=0) s = vp.load('data/shapes/cow.vtk').alpha(0.3).subdivide() # remesh variances = [] for i, p in enumerate(s.coordinates()): if i % 100: continue # skip most points pts = s.closestPoint(p, N=12) # find the N closest points to p plane = fitPlane(pts, bc='r', alpha=0.3) # find the fitting plane vp.actors.append(plane) vp.points(pts) # blue points vp.point(p, c='red 0.2') # mark in red the current point cn, v = plane.info['center'], plane.info['normal'] vp.arrow(cn, cn + v / 15, c='g') variances.append(plane.info['variance']) vp.histogram(variances, title='variance', c='g') vp.show()
# In this example we fit a plane to regions of a surface defined by # N points that are closest to a given point of the surface. # For some of these point we show the fitting plane. # Blue points are the N points used for fitting. # Green histogram is the distribution of residuals from the fitting. # Both plane center and normal can be accessed from the # attribute actor.center and actor.normal (direction is arbitrary). # from __future__ import division, print_function from vtkplotter import Plotter from vtkplotter.analysis import fitPlane vp = Plotter(verbose=0, axes=0) s = vp.load('data/shapes/cow.vtk').alpha(0.3).subdivide() # remesh variances = [] for i, p in enumerate(s.coordinates()): if i % 100: continue # skip most points pts = s.closestPoint(p, N=12) # find the N closest points to p plane = fitPlane(pts, bc='r', alpha=0.3) # find the fitting plane vp.actors.append(plane) vp.points(pts) # blue points vp.point(p, c='red 0.2') # mark in red the current point vp.arrow(plane.center, plane.center + plane.normal / 15, c='g') variances.append(plane.variance) vp.histogram(variances, title='variance', c='g') vp.show()
# Retrieve the vtk transformation matrix. # from __future__ import division, print_function from random import uniform as u from vtkplotter import Plotter from vtkplotter.analysis import align vp = Plotter(shape=[1,2], verbose=0, axes=2) N1 = 15 # number of points of first set N2 = 10 # number of points of second set x = 1. # add some randomness pts1 = [ (u(0,x), u(0,x), u(0,x)+i) for i in range(N1) ] pts2 = [ (u(0,x)+3, u(0,x)+i/2+2, u(0,x)+i+1) for i in range(N2) ] act1 = vp.points(pts1, c='b', legend='source') act2 = vp.points(pts2, c='r', legend='target') vp.show(at=0) # find best alignment between the 2 sets of points alpts1 = align(act1, act2).coordinates() for i in range(N1): #draw arrows to see where points end up vp.arrow(pts1[i], alpts1[i], c='k', s=0.007, alpha=.1) vp.show(at=1, interactive=1)