morphomatics_tube.py 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. """Morphomatics example"""
  2. import numpy as np
  3. import vedo
  4. try:
  5. from morphomatics.geom import Surface
  6. from morphomatics.stats import StatisticalShapeModel
  7. from morphomatics.manifold import FundamentalCoords
  8. except ModuleNotFoundError:
  9. print("Install with:")
  10. print("pip install -U git+https://github.com/morphomatics/morphomatics.git#egg=morphomatics")
  11. exit(0)
  12. ln1 = [[1, 1, x / 2] for x in np.arange(0, 15, 0.2)]
  13. ln2 = [[np.sin(x), np.cos(x), x / 2] for x in np.arange(0,15, 0.2)]
  14. rads= [0.4*(np.cos(6*ir/len(ln2)))**2+0.2 for ir in range(len(ln2))]
  15. vmesh1 = vedo.Tube(ln1, r=0.08).triangulate().clean()
  16. vmesh2 = vedo.Tube(ln2, r=rads).triangulate().clean()
  17. verts1 = vmesh1.vertices
  18. verts2 = vmesh2.vertices
  19. faces = np.array(vmesh1.cells)
  20. # construct model
  21. SSM = StatisticalShapeModel(lambda ref: FundamentalCoords(ref))
  22. surfaces = [Surface(v, faces) for v in [verts1, verts2]]
  23. SSM.construct(surfaces)
  24. # sample trajectory along the main mode of variation
  25. shapes = []
  26. std = np.sqrt(SSM.variances[0])
  27. for t in np.linspace(-1.0, 1.0, 20):
  28. e = SSM.space.connec.exp(SSM.mean_coords, t * std * SSM.modes[0])
  29. v = SSM.space.from_coords(e)
  30. shapes.append(vedo.Mesh([v, faces]))
  31. plt = vedo.applications.Browser(shapes, slider_title="shape", bg2='blue9')
  32. plt.add(shapes[ 0].clone().on().wireframe().alpha(0.1).lighting('off'))
  33. plt.add(shapes[-1].clone().on().wireframe().alpha(0.1).lighting('off'))
  34. plt.show(viewup='z', axes=1).close()