test_docs_sniplets.py 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. ## This contains the script snippets that come with the documetation for testing
  2. import numpy as np
  3. from vedo import *
  4. from vedo.pyplot import plot
  5. import vedo
  6. doshow = 1
  7. ##################################################################### addons.py
  8. box = Box(pos=(1,2,3), length=8, width=9, height=7).alpha(0.1)
  9. axs = Axes(box, c='k') # returns Assembly object
  10. for a in axs.unpack():
  11. print(a.name)
  12. if doshow:
  13. show(box, axs).close()
  14. ######################################################
  15. print("Test 1")
  16. b = Box(pos=(0, 0, 0), length=80, width=90, height=70).alpha(0.1)
  17. if doshow:
  18. show(
  19. b,
  20. axes={
  21. "xtitle": "Some long variable [a.u.]",
  22. "number_of_divisions": 4,
  23. # ...
  24. },
  25. ).close()
  26. ##################################################################### base.py
  27. print("Test 2")
  28. c1 = Cube()
  29. c2 = c1.clone().c('violet').alpha(0.5) # copy of c1
  30. v = vector(0.2,1,0)
  31. p = vector(1,0,0) # axis passes through this point
  32. c2.rotate(90, axis=v, point=p)
  33. l = Line(-v+p, v+p).lw(3).c('red')
  34. if doshow:
  35. show(c1, l, c2, axes=1).close()
  36. ######################################################
  37. print("Test 3")
  38. objs = []
  39. for i in range(-5, 5):
  40. p = [i/3, i/2, i]
  41. v = vector(i/10, i/20, 1)
  42. c = Circle(r=i/5+1.2).pos(p).lw(3)
  43. objs += [c, Arrow(p,p+v)]
  44. if doshow:
  45. show(objs, axes=1).close()
  46. ######################################################
  47. print("Test 4")
  48. c1 = Cube()
  49. c2 = c1.clone().c('violet').alpha(0.5) # copy of c1
  50. v = vector(0.2,1,0)
  51. p = vector(1,0,0) # axis passes through this point
  52. c2.rotate(90, axis=v, point=p)
  53. # get the inverse of the current transformation
  54. T = c2.transform.compute_inverse()
  55. c2.apply_transform(T) # put back c2 in place
  56. l = Line(p-v, p+v).lw(3).c('red')
  57. if doshow:
  58. show(c1.wireframe().lw(3), l, c2, axes=1).close()
  59. ######################################################
  60. print("Test 5")
  61. tetmesh = TetMesh(dataurl+'limb_ugrid.vtk')
  62. tetmesh.color('rainbow')
  63. cu = Cube(side=500).x(500) # any Mesh works
  64. tetmesh.cut_with_box(cu)
  65. if doshow:
  66. show(axes=1).close()
  67. ##################################################################### mesh.py
  68. print("Test 6")
  69. s = Sphere().crop(right=0.3, left=0.1)
  70. if doshow:
  71. show(s).close()
  72. ######################################################
  73. print("Test 7")
  74. c1 = Cylinder(pos=(0,0,0), r=2, height=3, axis=(1,.0,0), alpha=.1).triangulate()
  75. c2 = Cylinder(pos=(0,0,2), r=1, height=2, axis=(0,.3,1), alpha=.1).triangulate()
  76. intersect = c1.intersect_with(c2).join(reset=True)
  77. spline = Spline(intersect).c('blue').lw(5)
  78. if doshow:
  79. show(c1, c2, spline, intersect.labels('id'), axes=1).close()
  80. ######################################################
  81. print("Test 8")
  82. grid = Grid()#.triangulate()
  83. circle = Circle(r=0.3, res=24).pos(0.11,0.12)
  84. line = Line(circle, closed=True, lw=4, c='r4')
  85. # grid.imprint(line)
  86. if doshow:
  87. show(grid, line, axes=1).close()
  88. ##################################################################### Image.py
  89. print("Test 9")
  90. if doshow:
  91. pic = Image(dataurl+'dog.jpg').pad()
  92. pic.append([pic,pic,pic], axis='y')
  93. pic.append([pic,pic,pic,pic], axis='x')
  94. pic.show(axes=1).close()
  95. ######################################################
  96. print("Test 10")
  97. if doshow:
  98. p = vedo.Image(vedo.dataurl+'images/dog.jpg').bw()
  99. pe = p.clone().enhance()
  100. show(p, pe, N=2, mode='image', zoom='tight').close()
  101. ######################################################
  102. print("Test 11")
  103. if doshow:
  104. pic1 = Image("https://aws.glamour.es/prod/designs/v1/assets/620x459/547577.jpg")
  105. pic2 = pic1.clone().invert()
  106. pic3 = pic1.clone().binarize()
  107. show(pic1, pic2, pic3, N=3, bg="blue9").close()
  108. ######################################################
  109. print("Test 12")
  110. if doshow:
  111. pic = vedo.Image(vedo.dataurl+"images/dog.jpg")
  112. pic.add_rectangle([100,300], [100,200], c='green4', alpha=0.7)
  113. pic.add_line([100,100],[400,500], lw=2, alpha=1)
  114. pic.add_triangle([250,300], [100,300], [200,400])
  115. show(pic, axes=1).close()
  116. ##################################################################### plotter.py
  117. print("Test 13")
  118. cone = Cone()
  119. if doshow:
  120. cone.show(axes=1).fly_to([1,0,0])
  121. cone.show().close()
  122. ######################################################
  123. print("Test 14")
  124. settings.use_parallel_projection = True # or else it doesnt make sense!
  125. cube = Cube().alpha(0.2)
  126. plt = Plotter(size=(900,600), axes=dict(xtitle='x (um)'))
  127. if doshow:
  128. plt.add_scale_indicator(units='um', c='blue4')
  129. plt.show(cube, "Scale indicator with units").close()
  130. settings.use_parallel_projection = False
  131. ######################################################
  132. print("Test 15")
  133. def func(evt): # called every time the mouse moves
  134. # evt is a dotted dictionary
  135. if not evt.actor:
  136. return # no hit, return
  137. print("point coords =", evt.picked3d)
  138. elli = Ellipsoid()
  139. plt = Plotter(axes=1)
  140. plt.add_callback('mouse hovering', func)
  141. if doshow:
  142. plt.show(elli).close()
  143. ##################################################################### pointcloud.py
  144. # print("Test 16")
  145. # s = Ellipsoid().rotate_y(30)
  146. # #Camera options: pos, focal_point, viewup, distance,
  147. # # clippingRange, parallelScale, thickness, viewAngle
  148. # camopts = dict(pos=(0,0,25), focal_point=(0,0,0))
  149. # if doshow:
  150. # show(s, camera=camopts, offscreen=True).close()
  151. # m = s.visible_points()
  152. # #print('visible pts:', m.points()) # numpy array
  153. # show(m, new=True, axes=1).close() # optionally draw result on a new window
  154. ######################################################
  155. print("Test 17")
  156. def fibonacci_sphere(n):
  157. s = np.linspace(0, n, num=n, endpoint=False)
  158. theta = s * 2.399963229728653
  159. y = 1 - s * (2/(n-1))
  160. r = np.sqrt(1 - y * y)
  161. x = np.cos(theta) * r
  162. z = np.sin(theta) * r
  163. return [x,y,z]
  164. # print(np.c_[fibonacci_sphere(10)].T)
  165. fpoints = Points(np.c_[fibonacci_sphere(1000)].T)
  166. if doshow:
  167. fpoints.show(axes=1).close()
  168. ######################################################
  169. print("Test 18")
  170. s = Sphere(res=10).linewidth(1).c("orange").compute_normals()
  171. point_ids = s.labels('id', on="points").c('green')
  172. cell_ids = s.labels('id', on="cells").c('black')
  173. if doshow:
  174. show(s, point_ids, cell_ids).close()
  175. ######################################################
  176. print("Test 19")
  177. sph = Sphere(quads=True, res=4).compute_normals().wireframe()
  178. sph.celldata["zvals"] = sph.cell_centers().coordinates[:,2]
  179. l2d = sph.labels("zvals", on="cells", precision=2).backcolor('orange9')
  180. if doshow:
  181. show(sph, l2d, axes=1).close()
  182. ######################################################
  183. print("Test 20")
  184. c1 = Cube().rotate_z(5).x(2).y(1)
  185. print("cube1 position", c1.pos())
  186. T = c1.transform # rotate by 5 degrees, sum 2 to x and 1 to y
  187. c2 = Cube().c('r4')
  188. c2.apply_transform(T)
  189. c2.apply_transform(T)
  190. c2.apply_transform(T)
  191. print("cube2 position", c2.pos())
  192. if doshow:
  193. show(c1, c2, axes=1).close()
  194. ######################################################
  195. print("Test 21")
  196. disc = Disc(r1=1, r2=1.2)
  197. mesh = disc.extrude(3, res=50).linewidth(1)
  198. mesh.cut_with_cylinder([0,0,2], r=0.4, axis='y', invert=True)
  199. if doshow:
  200. show(mesh, axes=1)
  201. ######################################################
  202. print("Test 22")
  203. disc = Disc(r1=1, r2=1.2)
  204. mesh = disc.extrude(3, res=50).linewidth(1)
  205. mesh.cut_with_sphere([1,-0.7,2], r=1.5, invert=True)
  206. if doshow:
  207. show(mesh, axes=1).close()
  208. ######################################################
  209. print("Test 23")
  210. arr = np.random.randn(100000, 3)/2
  211. pts = Points(arr).c('red3').pos(5,0,0)
  212. cube = Cube().pos(4,0.5,0)
  213. assem = pts.cut_with_mesh(cube, keep=True)
  214. if doshow:
  215. show(assem.unpack(), axes=1).close()
  216. ##################################################################### shapes.py
  217. print("Test 24")
  218. pts = [[1, 0, 0], [5, 2, 0], [3, 3, 1]]
  219. ln = Line(pts, c='r', lw=5).pattern('- -', repeats=10)
  220. if doshow:
  221. ln.show(axes=1).close()
  222. ######################################################
  223. print("Test 25")
  224. if doshow:
  225. shape = Assembly(dataurl+"timecourse1d.npy")[58]
  226. pts = shape.rotate_x(30).coordinates
  227. tangents = Line(pts).tangents()
  228. arrs = Arrows(pts, pts+tangents, c='blue9')
  229. show(shape.c('red5').lw(5), arrs, bg='bb', axes=1).close()
  230. ######################################################
  231. print("Test 26")
  232. if doshow:
  233. shape = Assembly(dataurl+"timecourse1d.npy")[55]
  234. curvs = Line(shape.coordinates).curvature()
  235. shape.cmap('coolwarm', curvs, vmin=-2,vmax=2).add_scalarbar3d(c='w')
  236. shape.render_lines_as_tubes().lw(12)
  237. pp = plot(curvs, ac='white', lc='yellow5')
  238. show(shape, pp, N=2, bg='bb', sharecam=False).close()
  239. ######################################################
  240. print("Test 27")
  241. aline = Line([(0,0,0),(1,3,0),(2,4,0)])
  242. surf1 = aline.sweep((1,0.2,0), res=3)
  243. surf2 = aline.sweep((0.2,0,1))
  244. aline.color('r').linewidth(4)
  245. if doshow:
  246. show(surf1, surf2, aline, axes=1).close()
  247. ######################################################
  248. print("Test 28")
  249. pts = [(-4,-3),(1,1),(2,4),(4,1),(3,-1),(2,-5),(9,-3)]
  250. ln = Line(pts, c='r', lw=2).z(0.01)
  251. rl = RoundedLine(pts, 0.6)
  252. if doshow:
  253. show(Points(pts), ln, rl, axes=1).close()
  254. ######################################################
  255. print("Test 29")
  256. pts = np.random.randn(25,3)
  257. for i,p in enumerate(pts):
  258. p += [5*i, 15*sin(i/2), i*i*i/200]
  259. if doshow:
  260. show(Points(pts), Bezier(pts), axes=1).close()
  261. ######################################################
  262. print("Test 30")
  263. xcoords = np.arange(0, 2, 0.2)
  264. ycoords = np.arange(0, 1, 0.2)
  265. sqrtx = sqrt(xcoords)
  266. grid = Grid(s=(sqrtx, ycoords))
  267. if doshow:
  268. grid.show(axes=8)
  269. # can also create a grid from np.mgrid:
  270. X, Y = np.mgrid[-12:12:1000*1j, 0:15:1000*1j]
  271. vgrid = Grid(s=(X[:,0], Y[0]))
  272. if doshow:
  273. vgrid.show(axes=8).close()
  274. ######################################################
  275. print("Test 31")
  276. settings.immediate_rendering = False
  277. plt = Plotter(N=18)
  278. for i in range(18):
  279. ps = ParametricShape(i).color(i)
  280. if doshow:
  281. plt.at(i).show(ps, ps.name)
  282. if doshow:
  283. plt.interactive()