def parametrize_dataset(dataset, grid): from paraview.numpy_support import vtk_to_numpy xyz = vtk_to_numpy(dataset.GetPoints().GetData()) from numpy import sqrt from numpy import zeros_like xrt = zeros_like(xyz) xrt[:, 0] = xyz[:, 0] xrt[:, 1] = sqrt(xyz[:, 1] ** 2 + xyz[:, 2] ** 2) from paraview.numpy_support import numpy_to_vtk from paraview.vtk import vtkPoints points = vtkPoints() points.SetData(numpy_to_vtk(xrt, deep=1)) input = vtk.vtkPolyData() input.SetPoints(points) probe = vtk.vtkProbeFilter() probe.SetInput(input) probe.SetSource(grid) probe.Update() outputPointData = probe.GetOutput().GetPointData() pointData = dataset.GetPointData() pointData.AddArray(outputPointData.GetArray('hsH')) pointData.AddArray(outputPointData.GetArray('xm'))
def _lattice2poly_data(cuds): poly_data = vtk.vtkPolyData() points = vtk.vtkPoints() coordinates = cuds.get_coordinate # copy node data point_data = poly_data.GetPointData() data_collector = CUBADataAccumulator(container=point_data) for node in cuds.iter(item_type=CUBA.NODE): points.InsertNextPoint(coordinates(node.index)) data_collector.append(node.data) poly_data.SetPoints(points) return poly_data
def _particles2poly_data(cuds): particle2index = {} points = vtk.vtkPoints() lines = vtk.vtkCellArray() poly_data = vtk.vtkPolyData() point_data = poly_data.GetPointData() data_collector = CUBADataAccumulator(container=point_data) for index, particle in enumerate(cuds.iter(item_type=CUBA.PARTICLE)): particle2index[particle.uid] = index points.InsertPoint(index, *particle.coordinates) data_collector.append(particle.data) cell_data = poly_data.GetCellData() data_collector = CUBADataAccumulator(container=cell_data) for bond in cuds.iter(item_type=CUBA.BOND): lines.InsertNextCell(len(bond.particles)) for uuid in bond.particles: lines.InsertCellPoint(particle2index[uuid]) data_collector.append(bond.data) poly_data.SetPoints(points) poly_data.SetLines(lines) return poly_data
def trouver_centres_spheres_inscrites(surface_1, surface_2, precision=1e-6, pas_initial=10): """fonction qui recherche les centrse des spheres inscrites entre les surface_1 et surface_2 le vecteur 'Normals' doit etre present aux noeuds de surface_1 la recherche se fait par trouver_zero_fonction_croissante, pour chacun des points de surface_1 on cherche un point sur la ligne orthogonale a surface_1, equidistant de surface_1 et surface_2 --> centre de la sphere inscrite entre surface_1 et surface_2 passant par le point de surface_1 considere surface_1 et surface_2 doivent etre des vtkPolyData """ # initialisation des outils vtk de calcul des distances calcul_distance_1 = vtk.vtkKdTreePointLocator() calcul_distance_1.SetDataSet(surface_1) calcul_distance_1.BuildLocator() calcul_distance_2 = vtk.vtkKdTreePointLocator() calcul_distance_2.SetDataSet(surface_2) calcul_distance_2.BuildLocator() vtkMath = vtk.vtkMath() # lecture des donnees de la surface liste_normals_surface_1 = numpy_support.vtk_to_numpy(surface_1.GetPointData().GetArray('Normals')) liste_points_surface_1 = numpy_support.vtk_to_numpy(surface_1.GetPoints().GetData()) # ACTION liste_centres = numpy.empty((0,3)) print "progression ", printed = False for numero_point_surface_1 in range(0, surface_1.GetNumberOfPoints()): progress = ((numero_point_surface_1 + 1) * 100) // surface_1.GetNumberOfPoints() if progress % 10 == 0 and printed is False: print "{0}%".format(progress), printed = True elif progress % 10 != 0: printed = False """le vecteur normal et les coordonnees de point definissent une ligne sur laquelle chercher le centre de la sphere""" normal = liste_normals_surface_1[numero_point_surface_1] point = liste_points_surface_1[numero_point_surface_1] def fonction_a_annuler(x): centre = point + x * normal p1 = surface_1.GetPoint(calcul_distance_1.FindClosestPoint(centre)) p2 = surface_2.GetPoint(calcul_distance_2.FindClosestPoint(centre)) dist_1 = vtkMath.Distance2BetweenPoints(p1, centre) dist_2 = vtkMath.Distance2BetweenPoints(p2, centre) nv_ecart = dist_1 ** (1. / 2) - dist_2 ** (1. / 2) return nv_ecart """ recherche du centre de la sphere inscrite par trouver_zero_fonction_croissante""" # il est possible que le pas initial indique soit trop petit pas = pas_initial while fonction_a_annuler(pas) < 0: pas *= 2. # recherche par dichotomie x_solution = trouver_zero_fonction_croissante(0.0, pas, fonction_a_annuler, precision) centre = point + x_solution * normal liste_centres = numpy.r_[liste_centres, centre.reshape((1, 3))] print "termine" # creation d'un object vtkPolyData vtkPoints_mean = vtk.vtkPoints() vtkPoints_mean.SetData(numpy_support.numpy_to_vtk(liste_centres, deep = 1)) surface_moyenne = vtk.vtkPolyData() surface_moyenne.SetPoints(vtkPoints_mean) surface_moyenne.SetPolys(surface_1.GetPolys()) surface_moyenne.Update() return surface_moyenne