test_core1.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. import numpy as np
  2. from vedo import Cone, Sphere, merge, Volume, dataurl, Assembly
  3. import vtk
  4. print('\n\n---------------------------------')
  5. print('vtkVersion', vtk.vtkVersion().GetVTKVersion())
  6. print('---------------------------------')
  7. #####################################
  8. cone = Cone(res=48)
  9. sphere = Sphere(res=24)
  10. carr = cone.cell_centers().coordinates[:, 2]
  11. parr = cone.vertices[:, 0]
  12. cone.pointdata["parr"] = parr
  13. cone.celldata["carr"] = carr
  14. carr = sphere.cell_centers().coordinates[:, 2]
  15. parr = sphere.vertices[:, 0]
  16. sphere.pointdata["parr"] = parr
  17. sphere.celldata["carr"] = carr
  18. sphere.pointdata["pvectors"] = np.sin(sphere.vertices)
  19. sphere.compute_elevation()
  20. cone.compute_normals()
  21. sphere.compute_normals()
  22. ###################################### test clone()
  23. c2 = cone.clone()
  24. print('clone()', cone.npoints, c2.npoints)
  25. assert cone.npoints == c2.npoints
  26. print('clone()', cone.ncells, c2.ncells)
  27. assert cone.ncells == c2.ncells
  28. ###################################### test merge()
  29. m = merge(sphere, cone)
  30. print('merge()', m.npoints, cone.npoints + sphere.npoints)
  31. assert m.npoints == cone.npoints + sphere.npoints
  32. print('merge()', m.ncells, cone.ncells + sphere.ncells)
  33. assert m.ncells == cone.ncells + sphere.ncells
  34. ###################################### inputdata
  35. print('dataset', [cone.dataset], "vtk.vtkPolyData")
  36. assert isinstance(cone.dataset, vtk.vtkPolyData)
  37. ###################################### mapper
  38. print('mapper', [cone.mapper], "vtk.vtkPolyDataMapper")
  39. assert isinstance(cone.mapper, vtk.vtkPolyDataMapper)
  40. ###################################### pickable
  41. cone.pickable(False)
  42. cone.pickable(True)
  43. print('pickable', cone.pickable(), True)
  44. assert cone.pickable()
  45. ###################################### pos
  46. cone.pos(1,2,3)
  47. print('pos', [1,2,3], cone.pos())
  48. assert np.allclose([1,2,3], cone.pos())
  49. cone.pos(5,6)
  50. print('pos',[5,6,0], cone.pos())
  51. assert np.allclose([5,6,0], cone.pos())
  52. ###################################### shift
  53. cone.pos(5,6,7).shift(3,0,0)
  54. print('shift',[8,6,7], cone.pos())
  55. assert np.allclose([8,6,7], cone.pos(), atol=0.001)
  56. ###################################### x y z
  57. cone.pos(10,11,12)
  58. cone.x(1.1)
  59. print('x y z',[1.1,11,12], cone.pos())
  60. assert np.allclose([1.1,11,12], cone.pos(), atol=0.001)
  61. cone.y(1.2)
  62. print('x y z',[1.1,1.2,12], cone.pos())
  63. assert np.allclose([1.1,1.2,12], cone.pos(), atol=0.001)
  64. cone.z(1.3)
  65. print('x y z',[1.1,1.2,1.3], cone.pos())
  66. assert np.allclose([1.1,1.2,1.3], cone.pos(), atol=0.001)
  67. ###################################### rotate
  68. cr = cone.pos(0,0,0).clone().rotate(90, axis=(0, 1, 0))
  69. print('rotate', np.max(cr.vertices[:,2]) ,'<', 1.01)
  70. assert np.max(cr.vertices[:,2]) < 1.01
  71. ###################################### orientation
  72. cr = cone.pos(0,0,0).clone().reorient([0,0,1], (1, 1, 0))
  73. print('orientation',np.max(cr.vertices[:,2]) ,'<', 1.01)
  74. assert np.max(cr.vertices[:,2]) < 1.01
  75. ####################################### scale
  76. cr.scale(5)
  77. print('scale',np.max(cr.vertices[:,2]) ,'>', 4.98)
  78. assert np.max(cr.vertices[:,2]) > 4.98
  79. ###################################### box
  80. bx = cone.box()
  81. print('box',bx.npoints, 24)
  82. assert bx.npoints == 24
  83. print('box',bx.clean().npoints , 8)
  84. assert bx.clean().npoints == 8
  85. ###################################### transform
  86. ct = cone.clone().rotate_x(10).rotate_y(10).rotate_z(10)
  87. print('transform', [ct.transform.T], [vtk.vtkTransform])
  88. assert isinstance(ct.transform.T, vtk.vtkTransform)
  89. print('ntransforms',ct.transform.T.GetNumberOfConcatenatedTransforms())
  90. assert ct.transform.T.GetNumberOfConcatenatedTransforms()
  91. ###################################### pointdata and celldata
  92. arrnames = cone.pointdata.keys()
  93. print('pointdata', arrnames, 'parr')
  94. assert 'parr' in arrnames
  95. arrnames = cone.celldata.keys()
  96. print('celldata.keys', arrnames, 'carr')
  97. assert 'carr' in arrnames
  98. ###################################### Get Point Data
  99. arr = sphere.pointdata['parr']
  100. print('pointdata',len(arr))
  101. assert len(arr)
  102. print('pointdata',np.max(arr) ,'>', .99)
  103. assert np.max(arr) > .99
  104. arr = sphere.celldata['carr']
  105. print('celldata',[arr])
  106. assert len(arr)
  107. print('celldata',np.max(arr) ,'>', .99)
  108. assert np.max(arr) > .99
  109. ######################################__add__
  110. print('__add__', [cone+sphere])
  111. assert isinstance(cone+sphere, Assembly)
  112. ###################################### vertices
  113. s2 = sphere.clone()
  114. pts = sphere.vertices
  115. pts2 = pts + [1,2,3]
  116. s2.vertices = pts2
  117. pts3 = s2.vertices
  118. print('vertices',sum(pts3-pts2))
  119. assert np.allclose(pts2, pts3, atol=0.001)
  120. ###################################### cells
  121. print('cells')
  122. assert np.array(sphere.cells).shape == (2112, 3)
  123. ###################################### texture
  124. st = sphere.clone().texture(dataurl+'textures/wood2.jpg')
  125. print('texture test')
  126. assert isinstance(st.actor.GetTexture(), vtk.vtkTexture)
  127. ###################################### delete_cells_by_point_index
  128. sd = sphere.clone().delete_cells_by_point_index(range(100))
  129. print('delete_cells_by_point_index',sd.npoints , sphere.npoints)
  130. assert sd.npoints == sphere.npoints
  131. print('delete_cells_by_point_index',sd.ncells ,'<', sphere.ncells)
  132. assert sd.ncells < sphere.ncells
  133. ###################################### reverse
  134. # this fails on some archs (see issue #185)
  135. # lets comment it out temporarily
  136. # sr = sphere.clone().reverse().cut_with_plane()
  137. # print('DISABLED: reverse test', sr.npoints, 576)
  138. # rev = vtk.vtkReverseSense()
  139. # rev.SetInputData(sr.polydata())
  140. # rev.Update()
  141. # print('DISABLED: reverse vtk nr.pts, nr.cells')
  142. # print(rev.GetOutput().GetNumberOfPoints(),sr.polydata().GetNumberOfPoints(),
  143. # rev.GetOutput().GetNumberOfCells(), sr.polydata().GetNumberOfCells())
  144. # assert sr.npoints == 576
  145. ###################################### quantize
  146. sq = sphere.clone().quantize(0.1)
  147. print('quantize',sq.npoints , 834)
  148. assert sq.npoints == 834
  149. ###################################### bounds
  150. ss = sphere.clone().scale([1,2,3])
  151. print('bounds',ss.xbounds())
  152. assert np.allclose(ss.xbounds(), [-1,1], atol=0.01)
  153. print('bounds',ss.ybounds())
  154. assert np.allclose(ss.ybounds(), [-2,2], atol=0.01)
  155. print('bounds',ss.zbounds())
  156. assert np.allclose(ss.zbounds(), [-3,3], atol=0.01)
  157. ###################################### average_size
  158. print('average_size', Sphere().scale(10).pos(1,3,7).average_size())
  159. assert 9.9 < Sphere().scale(10).pos(1,3,7).average_size() < 10.1
  160. print('diagonal_size',sphere.diagonal_size())
  161. assert 3.3 < sphere.diagonal_size() < 3.5
  162. print('center_of_mass',sphere.center_of_mass())
  163. assert np.allclose(sphere.center_of_mass(), [0,0,0], atol=0.001)
  164. print('volume',sphere.volume())
  165. assert 4.1 < sphere.volume() < 4.2
  166. print('area',sphere.area())
  167. assert 12.5 < sphere.area() < 12.6
  168. ###################################### closest_point
  169. pt = [12,34,52]
  170. print('closest_point',sphere.closest_point(pt), [0.19883616, 0.48003298, 0.85441941])
  171. assert np.allclose(sphere.closest_point(pt),
  172. [0.19883616, 0.48003298, 0.85441941], atol=0.001)
  173. ###################################### find_cells_in_bounds
  174. ics = sphere.find_cells_in_bounds(xbounds=(-0.5, 0.5))
  175. print('find_cells_in',len(ics) , 1404)
  176. assert len(ics) == 1576
  177. ######################################transformMesh
  178. T = cone.clone().pos(35,67,87).transform
  179. s3 = Sphere().apply_transform(T)
  180. print('transformMesh',s3.center_of_mass(), (35,67,87))
  181. assert np.allclose(s3.center_of_mass(), (35,67,87), atol=0.001)
  182. ######################################normalize
  183. s3 = sphere.clone().pos(10,20,30).scale([7,8,9]).normalize()
  184. print('normalize',s3.center_of_mass(), (10,20,30))
  185. assert np.allclose(s3.center_of_mass(), (10,20,30), atol=0.001)
  186. print('normalize',s3.average_size())
  187. assert 0.9 < s3.average_size() < 1.1
  188. ###################################### crop
  189. c2 = cone.clone().crop(left=0.5)
  190. print('crop',np.min(c2.vertices[:,0]), '>', -0.001)
  191. assert np.min(c2.vertices[:,0]) > -0.001
  192. ###################################### subdivide
  193. s2 = sphere.clone().subdivide(4)
  194. print('subdivide',s2.npoints , 270338)
  195. assert s2.npoints == 270338
  196. ###################################### decimate
  197. s2 = sphere.clone().decimate(0.2)
  198. print('decimate',s2.npoints , 213)
  199. assert s2.npoints == 213
  200. ###################################### vertex_normals
  201. print('vertex_normals',sphere.vertex_normals[12], [9.97668684e-01, 1.01513637e-04, 6.82437494e-02])
  202. assert np.allclose(sphere.vertex_normals[12], [9.97668684e-01, 1.01513637e-04, 6.82437494e-02], atol=0.001)
  203. ###################################### contains
  204. print('is_inside',sphere.contains([0.1,0.2,0.3]))
  205. assert Sphere().contains([0.1,0.2,0.3])
  206. ###################################### intersectWithLine (fails vtk7..)
  207. # pts = sphere.intersectWithLine([-2,-2,-2], [2,3,4])
  208. # print('intersectWithLine',pts[0])
  209. # assert np.allclose(pts[0], [-0.8179885149002075, -0.522485613822937, -0.2269827425479889], atol=0.001)
  210. # print('intersectWithLine',pts[1])
  211. # assert np.allclose(pts[1], [-0.06572723388671875, 0.41784095764160156, 0.9014091491699219], atol=0.001)
  212. ############################################################################
  213. ############################################################################ Assembly
  214. asse = cone+sphere
  215. ######################################
  216. print('unpack',len(asse.unpack()) , 2)
  217. assert len(asse.unpack()) ==2
  218. print('unpack', asse.unpack(0).name)
  219. assert asse.unpack(0) == cone
  220. print('unpack',asse.unpack(1).name)
  221. assert asse.unpack(1) == sphere
  222. print('unpack',asse.diagonal_size(), 4.15)
  223. assert 4.1 < asse.diagonal_size() < 4.2
  224. ############################################################################ Volume
  225. X, Y, Z = np.mgrid[:30, :30, :30]
  226. scalar_field = ((X-15)**2 + (Y-15)**2 + (Z-15)**2)/225
  227. print('Test Volume, scalar min, max =', np.min(scalar_field), np.max(scalar_field))
  228. vol = Volume(scalar_field)
  229. volarr = vol.pointdata[0]
  230. print('Volume',volarr.shape[0] , 27000)
  231. assert volarr.shape[0] == 27000
  232. print('Volume',np.max(volarr) , 3)
  233. assert np.max(volarr) == 3
  234. print('Volume',np.min(volarr) , 0)
  235. assert np.min(volarr) == 0
  236. ###################################### isosurface
  237. iso = vol.isosurface(1.0)
  238. print('isosurface', iso.area())
  239. assert 2540 < iso.area() < 3000
  240. ###################################### utils change of coords
  241. from vedo import transformations
  242. q = [5,2,3]
  243. q = transformations.cart2spher(*q)
  244. q = transformations.spher2cart(*q)
  245. print("cart2spher spher2cart", q)
  246. assert np.allclose(q, [5,2,3], atol=0.001)
  247. q = transformations.cart2cyl(*q)
  248. q = transformations.cyl2cart(*q)
  249. print("cart2cyl cyl2cart", q)
  250. assert np.allclose(q, [5,2,3], atol=0.001)
  251. q = transformations.cart2cyl(*q)
  252. q = transformations.cyl2spher(*q)
  253. q = transformations.spher2cart(*q)
  254. print("cart2cyl cyl2spher spher2cart", q)
  255. assert np.allclose(q, [5,2,3], atol=0.001)
  256. q = transformations.cart2spher(*q)
  257. q = transformations.spher2cyl(*q)
  258. q = transformations.cyl2cart(*q)
  259. print("cart2spher spher2cyl cyl2cart", q)
  260. assert np.allclose(q, [5,2,3], atol=0.001)
  261. ######################################
  262. print("OK with test_actors")