def addSyntheticHelixAngles( ugrid, helix_angle_end, helix_angle_epi, type_of_support="cell", verbose=0): myVTK.myPrint(verbose, "*** addSyntheticHelixAngles ***") if (type_of_support == "cell"): ugrid_data = ugrid.GetCellData() elif (type_of_support == "point"): ugrid_data = ugrid.GetPointData() farray_rr = ugrid_data.GetArray("rr") farray_angle_helix = ugrid_data.GetArray("angle_helix") farray_angle_helix = computeSyntheticHelixAngles( farray_rr=farray_rr, helix_angle_end=helix_angle_end, helix_angle_epi=helix_angle_epi, farray_angle_helix=farray_angle_helix, verbose=verbose-1) ugrid_data.AddArray(farray_angle_helix) return farray_angle_helix
def computeSyntheticHelixAngles( farray_rr, helix_angle_end, helix_angle_epi, farray_angle_helix=None, verbose=0): myVTK.myPrint(verbose, "*** computeSyntheticHelixAngles ***") n_cells = farray_rr.GetNumberOfTuples() if (farray_angle_helix is None): farray_angle_helix = myVTK.createFloatArray( name="angle_helix", n_components=1, n_tuples=n_cells) for k_cell in xrange(n_cells): rr = farray_rr.GetTuple1(k_cell) helix_angle_in_degrees = (1.-rr) * helix_angle_end \ + rr * helix_angle_epi farray_angle_helix.SetTuple1( k_cell, helix_angle_in_degrees) return farray_angle_helix
def clipSurfacesForFullLVMesh(endo, epi, verbose=0): myVTK.myPrint(verbose, "*** clipSurfacesForFullLVMesh ***") endo_implicit_distance = vtk.vtkImplicitPolyDataDistance() endo_implicit_distance.SetInput(endo) epi_implicit_distance = vtk.vtkImplicitPolyDataDistance() epi_implicit_distance.SetInput(epi) epi_clip = vtk.vtkClipPolyData() if (vtk.vtkVersion.GetVTKMajorVersion() >= 6): epi_clip.SetInputData(epi) else: epi_clip.SetInput(epi) epi_clip.SetClipFunction(endo_implicit_distance) epi_clip.GenerateClippedOutputOn() epi_clip.Update() clipped_epi = epi_clip.GetOutput(0) clipped_valve = epi_clip.GetOutput(1) endo_clip = vtk.vtkClipPolyData() if (vtk.vtkVersion.GetVTKMajorVersion() >= 6): endo_clip.SetInputData(endo) else: endo_clip.SetInput(endo) endo_clip.SetClipFunction(epi_implicit_distance) endo_clip.InsideOutOn() endo_clip.Update() clipped_endo = endo_clip.GetOutput(0) return (clipped_endo, clipped_epi, clipped_valve)
def addSyntheticHelixAngles2(ugrid, angles_end, angles_epi, type_of_support="cell", sigma=0, verbose=0): myVTK.myPrint(verbose, "*** addSyntheticHelixAngles2 ***") if (type_of_support == "cell"): ugrid_data = ugrid.GetCellData() elif (type_of_support == "point"): ugrid_data = ugrid.GetPointData() farray_rr = ugrid_data.GetArray("rr") farray_cc = ugrid_data.GetArray("cc") farray_ll = ugrid_data.GetArray("ll") farray_angle_helix = ugrid_data.GetArray("angle_helix") farray_angle_helix = computeSyntheticHelixAngles2( farray_rr=farray_rr, farray_cc=farray_cc, farray_ll=farray_ll, angles_end=angles_end, angles_epi=angles_epi, sigma=sigma, farray_angle_helix=farray_angle_helix, verbose=verbose - 1) ugrid_data.AddArray(farray_angle_helix) return farray_angle_helix
def addSyntheticHelixAngles2( ugrid, angles_end, angles_epi, type_of_support="cell", sigma=0, verbose=0): myVTK.myPrint(verbose, "*** addSyntheticHelixAngles2 ***") if (type_of_support == "cell"): ugrid_data = ugrid.GetCellData() elif (type_of_support == "point"): ugrid_data = ugrid.GetPointData() farray_rr = ugrid_data.GetArray("rr") farray_cc = ugrid_data.GetArray("cc") farray_ll = ugrid_data.GetArray("ll") farray_angle_helix = ugrid_data.GetArray("angle_helix") farray_angle_helix = computeSyntheticHelixAngles2( farray_rr=farray_rr, farray_cc=farray_cc, farray_ll=farray_ll, angles_end=angles_end, angles_epi=angles_epi, sigma=sigma, farray_angle_helix=farray_angle_helix, verbose=verbose-1) ugrid_data.AddArray(farray_angle_helix) return farray_angle_helix
def mulArrays( array1, array2, array3=None, verbose=0): myVTK.myPrint(verbose, "*** mulArrays ***") n_components = array1.GetNumberOfComponents() assert (array2.GetNumberOfComponents() == n_components) n_tuples = array1.GetNumberOfTuples() assert (array2.GetNumberOfTuples() == n_tuples) array_type = type(array1.GetTuple(0)[0]) assert (array_type in [int, float]) assert (type(array2.GetTuple(0)[0]) is array_type) if (array3 is None): array3 = myVTK.createArray( name="", n_components=n_components, n_tuples=n_tuples, array_type=array_type) else: assert (array3.GetNumberOfComponents() == n_components) assert (array3.GetNumberOfTuples() == n_tuples) assert (type(array3.GetTuple(0)[0]) is array_type) for k_tuple in xrange(n_tuples): array3.SetTuple( k_tuple, numpy.array(array1.GetTuple(k_tuple)) * numpy.array(array2.GetTuple(k_tuple))) return array3
def computeHelixAngles( farray_eRR, farray_eCC, farray_eLL, farray_eF, verbose=0): myVTK.myPrint(verbose, "*** computeHelixAngles ***") n_tuples = farray_eRR.GetNumberOfTuples() assert (farray_eCC.GetNumberOfTuples() == n_tuples) assert (farray_eLL.GetNumberOfTuples() == n_tuples) assert (farray_eF.GetNumberOfTuples() == n_tuples) farray_angle_helix = myVTK.createFloatArray("angle_helix", 1, n_tuples) eRR = numpy.empty(3) eCC = numpy.empty(3) eLL = numpy.empty(3) eF = numpy.empty(3) for k_tuple in xrange(n_tuples): farray_eRR.GetTuple(k_tuple, eRR) farray_eCC.GetTuple(k_tuple, eCC) farray_eLL.GetTuple(k_tuple, eLL) farray_eF.GetTuple(k_tuple, eF) eF -= numpy.dot(eF, eRR) * eRR eF /= numpy.linalg.norm(eF) helix_angle = math.copysign(1., numpy.dot(eF, eCC)) * math.asin(min(1., max(-1., numpy.dot(eF, eLL)))) * (180./math.pi) farray_angle_helix.SetTuple1(k_tuple, helix_angle) return farray_angle_helix
def writeFiberOrientationFileForAbaqus( mesh, filename, eF_field_name="eF", eS_field_name="eS", sep=", ", verbose=0): myVTK.myPrint(verbose, "*** writeFiberOrientationFileForAbaqus ***") orientation_file = open(filename, "w") orientation_file.write(", 1., 0., 0., 0., 1., 0."+"\n") n_cells = mesh.GetNumberOfCells() eF_array = mesh.GetCellData().GetArray(eF_field_name) eS_array = mesh.GetCellData().GetArray(eS_field_name) eF = numpy.empty(3) eS = numpy.empty(3) for k_cell in xrange(n_cells): eF_array.GetTuple(k_cell, eF) eS_array.GetTuple(k_cell, eS) line = str(k_cell+1) for k in xrange(3): line += sep + str(eF[k]) for k in xrange(3): line += sep + str(eS[k]) line += "\n" orientation_file.write(line) orientation_file.close()
def clipPDataUsingPlane(pdata_mesh, plane_O, plane_N, verbose=0): myVTK.myPrint(verbose, "*** clipPDataUsingPlane ***") plane = vtk.vtkPlane() plane.SetOrigin(plane_O) plane.SetNormal(plane_N) #myVTK.myPrint(verbose-1, "pdata_mesh.GetBounds() = "+str(pdata_mesh.GetBounds())) #myVTK.myPrint(verbose-1, "plane_O = "+str(plane_O)) #myVTK.myPrint(verbose-1, "plane_N = "+str(plane_N)) clip = vtk.vtkClipPolyData() clip.SetClipFunction(plane) clip.GenerateClippedOutputOn() if (vtk.vtkVersion.GetVTKMajorVersion() >= 6): clip.SetInputData(pdata_mesh) else: clip.SetInput(pdata_mesh) clip.Update() clipped0 = clip.GetOutput(0) clipped1 = clip.GetOutput(1) #myVTK.myPrint(verbose-1, "clipped0.GetNumberOfPoints() = "+str(clipped0.GetNumberOfPoints())) #myVTK.myPrint(verbose-1, "clipped1.GetNumberOfPoints() = "+str(clipped1.GetNumberOfPoints())) #myVTK.myPrint(verbose-1, "clipped0.GetNumberOfCells() = "+str(clipped0.GetNumberOfCells())) #myVTK.myPrint(verbose-1, "clipped1.GetNumberOfCells() = "+str(clipped1.GetNumberOfCells())) if (clipped0.GetNumberOfCells() > clipped1.GetNumberOfCells()): return clipped0, clipped1 else: return clipped1, clipped0
def readAbaqusFibersFromINP( filename, verbose=1): myVTK.myPrint(verbose, "*** readAbaqusFibersFromINP: " + filename + " ***") eF_array = myVTK.createFloatArray('eF', 3) eS_array = myVTK.createFloatArray('eS', 3) eN_array = myVTK.createFloatArray('eN', 3) file = open(filename, 'r') file.readline() for line in file: line = line.split(', ') #print line eF = [float(item) for item in line[1:4]] eS = [float(item) for item in line[4:7]] eN = numpy.cross(eF,eS) #print "eF =", eF #print "eS =", eS #print "eN =", eN eF_array.InsertNextTuple(eF) eS_array.InsertNextTuple(eS) eN_array.InsertNextTuple(eN) file.close() myVTK.myPrint(verbose, "n_tuples = " + str(eF_array.GetNumberOfTuples())) return (eF_array, eS_array, eN_array)
def readAbaqusFibersFromINP(filename, verbose=0): myVTK.myPrint(verbose, "*** readAbaqusFibersFromINP: " + filename + " ***") eF_array = myVTK.createFloatArray('eF', 3) eS_array = myVTK.createFloatArray('eS', 3) eN_array = myVTK.createFloatArray('eN', 3) file = open(filename, 'r') file.readline() for line in file: line = line.split(', ') #print line eF = [float(item) for item in line[1:4]] eS = [float(item) for item in line[4:7]] eN = numpy.cross(eF, eS) #print "eF =", eF #print "eS =", eS #print "eN =", eN eF_array.InsertNextTuple(eF) eS_array.InsertNextTuple(eS) eN_array.InsertNextTuple(eN) file.close() myVTK.myPrint(verbose - 1, "n_tuples = " + str(eF_array.GetNumberOfTuples())) return (eF_array, eS_array, eN_array)
def clipSurfacesForFullLVMesh(endo, epi, verbose=1): myVTK.myPrint(verbose, "*** clipSurfacesForFullLVMesh ***") endo_implicit_distance = vtk.vtkImplicitPolyDataDistance() endo_implicit_distance.SetInput(endo) epi_implicit_distance = vtk.vtkImplicitPolyDataDistance() epi_implicit_distance.SetInput(epi) epi_clip = vtk.vtkClipPolyData() epi_clip.SetInputData(epi) epi_clip.SetClipFunction(endo_implicit_distance) epi_clip.GenerateClippedOutputOn() epi_clip.Update() clipped_epi = epi_clip.GetOutput(0) clipped_valve = epi_clip.GetOutput(1) endo_clip = vtk.vtkClipPolyData() endo_clip.SetInputData(endo) endo_clip.SetClipFunction(epi_implicit_distance) endo_clip.InsideOutOn() endo_clip.Update() clipped_endo = endo_clip.GetOutput(0) return (clipped_endo, clipped_epi, clipped_valve)
def computeABPointsFromBoundsAndCenter( mesh, AB=[0,0,1], verbose=0): myVTK.myPrint(verbose, "*** computeABPointsFromBoundsAndCenter ***") C = numpy.array(mesh.GetCenter()) #print "C ="+str(C) bounds = mesh.GetBounds() diag = numpy.array([bounds[1]-bounds[0], bounds[3]-bounds[2], bounds[5]-bounds[4]]) AB = numpy.array(AB) AB = abs(numpy.dot(diag, AB)) * AB #print "bounds ="+str(bounds) #print "diag ="+str(diag) #print "AB ="+str(AB) point_A = C - AB/2 point_B = C + AB/2 #print "point_A ="+str(point_A) #print "point_B ="+str(point_B) points_AB = vtk.vtkPoints() points_AB.InsertNextPoint(point_A) points_AB.InsertNextPoint(point_B) #print points_AB return points_AB
def addFractionalAnisotropy(ugrid, field_name, type_of_support="cell", verbose=0): myVTK.myPrint(verbose, "*** addFractionalAnisotropy ***") if (type_of_support == "cell"): data = ugrid.GetCellData() elif (type_of_support == "point"): data = ugrid.GetPointData() farray_e1 = data.GetArray(field_name + "_Lmax") farray_e2 = data.GetArray(field_name + "_Lmid") farray_e3 = data.GetArray(field_name + "_Lmin") (farray_FA, farray_FA12, farray_FA23) = computeFractionalAnisotropy(farray_e1=farray_e1, farray_e2=farray_e2, farray_e3=farray_e3, verbose=verbose - 1) farray_FA.SetName(field_name + "_FA") farray_FA12.SetName(field_name + "_FA_12") farray_FA23.SetName(field_name + "_FA_23") data.AddArray(farray_FA) data.AddArray(farray_FA12) data.AddArray(farray_FA23) return (farray_FA, farray_FA12, farray_FA23)
def computeRegionsForBiV(points, pdata_endLV, pdata_endRV, pdata_epi, verbose=0): myVTK.myPrint(verbose, "*** computeRegionsForBiV ***") myVTK.myPrint(verbose, "Initializing cell locators...") (cell_locator_endLV, closest_point_endLV, generic_cell, cellId_endLV, subId, dist_endLV) = myVTK.getCellLocator( mesh=pdata_endLV, verbose=verbose - 1 ) (cell_locator_endRV, closest_point_endRV, generic_cell, cellId_endRV, subId, dist_endRV) = myVTK.getCellLocator( mesh=pdata_endRV, verbose=verbose - 1 ) (cell_locator_epi, closest_point_epi, generic_cell, cellId_epi, subId, dist_epi) = myVTK.getCellLocator( mesh=pdata_epi, verbose=verbose - 1 ) n_points = points.GetNumberOfPoints() iarray_region = myVTK.createIntArray("region_id", 1, n_points) for k_point in range(n_points): point = numpy.array(points.GetPoint(k_point)) cell_locator_endLV.FindClosestPoint(point, closest_point_endLV, generic_cell, cellId_endLV, subId, dist_endLV) cell_locator_endRV.FindClosestPoint(point, closest_point_endRV, generic_cell, cellId_endRV, subId, dist_endRV) cell_locator_epi.FindClosestPoint(point, closest_point_epi, generic_cell, cellId_epi, subId, dist_epi) if dist_endRV == max(dist_endLV, dist_endRV, dist_epi): iarray_region.SetTuple(k_point, [0]) elif dist_epi == max(dist_endLV, dist_endRV, dist_epi): iarray_region.SetTuple(k_point, [1]) elif dist_endLV == max(dist_endLV, dist_endRV, dist_epi): iarray_region.SetTuple(k_point, [2]) return iarray_region
def clipSurfacesForCutLVMesh( endo, epi, height, verbose=1): myVTK.myPrint(verbose, "*** clipSurfacesForCutLVMesh ***") plane = vtk.vtkPlane() plane.SetNormal(0,0,-1) plane.SetOrigin(0,0,height) clip = vtk.vtkClipPolyData() clip.SetClipFunction(plane) clip.SetInputData(endo) clip.Update() clipped_endo = clip.GetOutput(0) clip = vtk.vtkClipPolyData() clip.SetClipFunction(plane) clip.SetInputData(epi) clip.Update() clipped_epi = clip.GetOutput(0) return (clipped_endo, clipped_epi)
def thresholdUGrid( ugrid_mesh, field_support, field_name, threshold_value, threshold_by_upper_or_lower, verbose=0): myVTK.myPrint(verbose, "*** thresholdUGrid ***") threshold = vtk.vtkThreshold() if (vtk.vtkVersion.GetVTKMajorVersion() >= 6): threshold.SetInputData(ugrid_mesh) else: threshold.SetInput(ugrid_mesh) if (field_support == "points"): association = vtk.vtkDataObject.FIELD_ASSOCIATION_POINTS elif (field_support == "cells"): association = vtk.vtkDataObject.FIELD_ASSOCIATION_CELLS threshold.SetInputArrayToProcess(0, 0, 0, association, field_name) if (threshold_by_upper_or_lower == "upper"): threshold.ThresholdByUpper(threshold_value) elif (threshold_by_upper_or_lower == "lower"): threshold.ThresholdByLower(threshold_value) threshold.Update() ugrid_thresholded_mesh = threshold.GetOutput() return ugrid_thresholded_mesh
def readPData( filename, verbose=1): myVTK.myPrint(verbose, "*** readPData: " + filename + " ***") if ('vtk' in filename): pdata_reader = vtk.vtkPolyDataReader() elif ('vtp' in filename): pdata_reader = vtk.vtkXMLPolyDataReader() else: assert 0, "File must be .vtk or .vtp. Aborting." assert (os.path.isfile(filename)), "Wrong filename. Aborting." pdata_reader.SetFileName(filename) pdata_reader.Update() pdata = pdata_reader.GetOutput() if (verbose): print "n_points = " + str(pdata.GetNumberOfPoints()) print "n_verts = " + str(pdata.GetNumberOfVerts()) print "n_lines = " + str(pdata.GetNumberOfLines()) print "n_polys = " + str(pdata.GetNumberOfPolys()) print "n_strips = " + str(pdata.GetNumberOfStrips()) return pdata
def clipSurfacesForCutLVMesh(endo, epi, height, verbose=0): myVTK.myPrint(verbose, "*** clipSurfacesForCutLVMesh ***") plane = vtk.vtkPlane() plane.SetNormal(0, 0, -1) plane.SetOrigin(0, 0, height) clip = vtk.vtkClipPolyData() clip.SetClipFunction(plane) if (vtk.vtkVersion.GetVTKMajorVersion() >= 6): clip.SetInputData(endo) else: clip.SetInput(endo) clip.Update() clipped_endo = clip.GetOutput(0) clip = vtk.vtkClipPolyData() clip.SetClipFunction(plane) if (vtk.vtkVersion.GetVTKMajorVersion() >= 6): clip.SetInputData(epi) else: clip.SetInput(epi) clip.Update() clipped_epi = clip.GetOutput(0) return (clipped_endo, clipped_epi)
def addSyntheticHelixAngles( ugrid, helix_angle_end, helix_angle_epi, type_of_support="cell", verbose=1): myVTK.myPrint(verbose, "*** addSyntheticHelixAngles ***") if (type_of_support == "cell"): ugrid_data = ugrid.GetCellData() elif (type_of_support == "point"): ugrid_data = ugrid.GetPointData() farray_rr = ugrid_data.GetArray("rr") farray_angle_helix = ugrid_data.GetArray("angle_helix") farray_angle_helix = computeSyntheticHelixAngles( farray_rr=farray_rr, helix_angle_end=helix_angle_end, helix_angle_epi=helix_angle_epi, farray_angle_helix=farray_angle_helix, verbose=verbose-1) ugrid_data.AddArray(farray_angle_helix) return farray_angle_helix
def computeSyntheticHelixAngles( farray_rr, helix_angle_end, helix_angle_epi, farray_angle_helix=None, verbose=1): myVTK.myPrint(verbose, "*** computeSyntheticHelixAngles ***") n_cells = farray_rr.GetNumberOfTuples() if (farray_angle_helix is None): farray_angle_helix = myVTK.createFloatArray( name="angle_helix", n_components=1, n_tuples=n_cells) for k_cell in xrange(n_cells): rr = farray_rr.GetTuple1(k_cell) helix_angle_in_degrees = (1.-rr) * helix_angle_end \ + rr * helix_angle_epi farray_angle_helix.SetTuple1( k_cell, helix_angle_in_degrees) return farray_angle_helix
def filterUGridIntoPData( ugrid, only_trianlges=False, verbose=0): myVTK.myPrint(verbose, "*** filterUGridIntoPData ***") filter_geometry = vtk.vtkGeometryFilter() if (vtk.vtkVersion.GetVTKMajorVersion() >= 6): filter_geometry.SetInputData(ugrid) else: filter_geometry.SetInput(ugrid) filter_geometry.Update() pdata = filter_geometry.GetOutput() if (only_trianlges): filter_triangle = vtk.vtkTriangleFilter() if (vtk.vtkVersion.GetVTKMajorVersion() >= 6): filter_triangle.SetInputData(pdata) else: filter_triangle.SetInput(pdata) filter_triangle.Update() pdata = filter_triangle.GetOutput() return pdata
def computeFractionalAnisotropy( farray_e1, farray_e2, farray_e3, verbose=1): myVTK.myPrint(verbose, "*** computeFractionalAnisotropy ***") n_tuples = farray_e1.GetNumberOfTuples() farray_FA = myVTK.createFloatArray("FA" , 1, n_tuples) farray_FA12 = myVTK.createFloatArray("FA_12", 1, n_tuples) farray_FA23 = myVTK.createFloatArray("FA_23", 1, n_tuples) for k_tuple in xrange(n_tuples): e1 = farray_e1.GetTuple1(k_tuple) e2 = farray_e2.GetTuple1(k_tuple) e3 = farray_e3.GetTuple1(k_tuple) FA = ((e1-e2)**2+(e1-e3)**2+(e2-e3)**2)**(0.5) / (2*(e1**2+e2**2+e3**2))**(0.5) FA12 = ((e1-e2)**2)**(0.5) / (e1**2+e2**2)**(0.5) FA23 = ((e2-e3)**2)**(0.5) / (e2**2+e3**2)**(0.5) farray_FA.SetTuple1(k_tuple, FA) farray_FA12.SetTuple1(k_tuple, FA12) farray_FA23.SetTuple1(k_tuple, FA23) return (farray_FA, farray_FA12, farray_FA23)
def addHelixAngles( ugrid, type_of_support="cell", verbose=0): myVTK.myPrint(verbose, "*** addHelixAngles ***") if (type_of_support == "cell"): ugrid_data = ugrid.GetCellData() elif (type_of_support == "point"): ugrid_data = ugrid.GetPointData() farray_eRR = ugrid_data.GetArray("eRR") farray_eCC = ugrid_data.GetArray("eCC") farray_eLL = ugrid_data.GetArray("eLL") farray_eF = ugrid_data.GetArray("eF") farray_angle_helix = computeHelixAngles( farray_eRR=farray_eRR, farray_eCC=farray_eCC, farray_eLL=farray_eLL, farray_eF=farray_eF, verbose=verbose-1) ugrid_data.AddArray(farray_angle_helix) return farray_angle_helix
def computeMeanStddevAngles( angles, angles_in_degrees=True, angles_in_pm_pi=True, verbose=1): myVTK.myPrint(verbose, "*** computeMeanStddevAngles ***") if (angles_in_degrees): if (angles_in_pm_pi): mean = math.atan2(numpy.mean([numpy.sin(2*numpy.array(angles)*numpy.pi/180)]), numpy.mean([numpy.cos(2*numpy.array(angles)*numpy.pi/180)]))*180/math.pi/2 else: mean = math.atan2(numpy.mean([numpy.sin(numpy.array(angles)*numpy.pi/180)]), numpy.mean([numpy.cos(numpy.array(angles)*numpy.pi/180)]))*180/math.pi stddev = numpy.sqrt(numpy.mean(((((numpy.array(angles)-mean)+90)%180)-90)**2)) else: if (angles_in_pm_pi): mean = math.atan2(numpy.mean([numpy.sin(2*numpy.array(angles))]), numpy.mean([numpy.cos(2*numpy.array(angles))]))/2 else: mean = math.atan2(numpy.mean([numpy.sin(numpy.array(angles))]), numpy.mean([numpy.cos(numpy.array(angles))])) stddev = numpy.sqrt(numpy.mean(((((numpy.array(angles)-mean)+math.pi/2)%math.pi)-math.pi/2)**2)) return (mean, stddev)
def readUGrid( filename, verbose=1): myVTK.myPrint(verbose, "*** readUGrid: " + filename + " ***") if ('vtk' in filename): ugrid_reader = vtk.vtkUnstructuredGridReader() elif ('vtu' in filename): ugrid_reader = vtk.vtkXMLUnstructuredGridReader() else: assert 0, "File must be .vtk or .vtu. Aborting." assert (os.path.isfile(filename)), "Wrong filename. Aborting." ugrid_reader.SetFileName(filename) ugrid_reader.Update() ugrid = ugrid_reader.GetOutput() if (verbose): n_points = ugrid.GetNumberOfPoints() print 'n_points =', n_points n_cells = ugrid.GetNumberOfCells() print 'n_cells =', n_cells return ugrid
def computeSectorsForLV(farray_rr, farray_cc, farray_ll, n_r=1, n_c=1, n_l=1, iarray_part_id=None, verbose=0): myVTK.myPrint(verbose, "*** computeSectorsForLV ***") n_cells = farray_rr.GetNumberOfTuples() iarray_sector = myVTK.createIntArray("sector_id", 1, n_cells) for k_cell in range(n_cells): if (iarray_part_id is not None) and (int(iarray_part_id.GetTuple1(k_cell)) > 0): sector_id = -1 else: rr = farray_rr.GetTuple1(k_cell) cc = farray_cc.GetTuple1(k_cell) ll = farray_ll.GetTuple1(k_cell) k_r = int(rr * n_r / 1.000001) k_c = int(cc * n_c / 1.000001) k_l = int((1. - ll) * n_l / 1.000001) sector_id = k_l * n_c * n_r + k_c * n_r + k_r iarray_sector.SetTuple1(k_cell, sector_id) return iarray_sector
def thresholdUGrid(ugrid_mesh, field_support, field_name, threshold_value, threshold_by_upper_or_lower, verbose=0): myVTK.myPrint(verbose, "*** thresholdUGrid ***") threshold = vtk.vtkThreshold() if (vtk.vtkVersion.GetVTKMajorVersion() >= 6): threshold.SetInputData(ugrid_mesh) else: threshold.SetInput(ugrid_mesh) if (field_support == "points"): association = vtk.vtkDataObject.FIELD_ASSOCIATION_POINTS elif (field_support == "cells"): association = vtk.vtkDataObject.FIELD_ASSOCIATION_CELLS threshold.SetInputArrayToProcess(0, 0, 0, association, field_name) if (threshold_by_upper_or_lower == "upper"): threshold.ThresholdByUpper(threshold_value) elif (threshold_by_upper_or_lower == "lower"): threshold.ThresholdByLower(threshold_value) threshold.Update() ugrid_thresholded_mesh = threshold.GetOutput() return ugrid_thresholded_mesh
def mulArrays(array1, array2, array3=None, verbose=0): myVTK.myPrint(verbose, "*** mulArrays ***") n_components = array1.GetNumberOfComponents() assert (array2.GetNumberOfComponents() == n_components) n_tuples = array1.GetNumberOfTuples() assert (array2.GetNumberOfTuples() == n_tuples) array_type = type(array1.GetTuple(0)[0]) assert (array_type in [int, float]) assert (type(array2.GetTuple(0)[0]) is array_type) if (array3 is None): array3 = myVTK.createArray(name="", n_components=n_components, n_tuples=n_tuples, array_type=array_type) else: assert (array3.GetNumberOfComponents() == n_components) assert (array3.GetNumberOfTuples() == n_tuples) assert (type(array3.GetTuple(0)[0]) is array_type) for k_tuple in xrange(n_tuples): array3.SetTuple( k_tuple, numpy.array(array1.GetTuple(k_tuple)) * numpy.array(array2.GetTuple(k_tuple))) return array3
def readAbaqusStressFromDAT( data_filename, verbose=1): myVTK.myPrint(verbose, "*** readAbaqusStressFromDAT: " + data_filename + " ***") s_array = myVTK.createFloatArray("", 6) data_file = open(data_filename, 'r') context = "" k_cell = 0 for line in data_file: if (context == "reading stresses"): #print line if ("MAXIMUM" in line): context = "" continue if ("OR" in line): splitted_line = line.split() assert (int(splitted_line[0]) == k_cell+1), "Wrong element number. Aborting." s_list = [float(splitted_line[k]) for k in xrange(3,9)] s_array.InsertNextTuple(s_list) k_cell += 1 if (line == " ELEMENT PT FOOT- S11 S22 S33 S12 S13 S23 \n"): context = "reading stresses" data_file.close() myVTK.myPrint(verbose, "n_tuples = " + str(s_array.GetNumberOfTuples())) return s_array
def readDynaDeformationGradients( mesh, hystory_files_basename, array_name, verbose=1): myVTK.myPrint(verbose, "*** readDynaDeformationGradients ***") n_cells = mesh.GetNumberOfCells() history_files_names = [hystory_files_basename + '.history#' + str(num) for num in xrange(11,20)] F_list = [[0. for k_component in xrange(9)] for k_cell in xrange(n_cells)] for k_component in xrange(9): history_file = open(history_files_names[k_component], 'r') for line in history_file: if line.startswith('*') or line.startswith('$'): continue line = line.split() F_list[int(line[0])-1][k_component] = float(line[1]) history_file.close() F_array = myVTK.createFloatArray(array_name, 9, n_cells) for k_cell in xrange(n_cells): F_array.InsertTuple(k_cell, F_list[k_cell]) myVTK.myPrint(verbose, "n_tuples = " + str(F_array.GetNumberOfTuples())) mesh.GetCellData().AddArray(F_array)
def readDynaDeformationGradients(mesh, hystory_files_basename, array_name, verbose=0): myVTK.myPrint(verbose, "*** readDynaDeformationGradients ***") n_cells = mesh.GetNumberOfCells() history_files_names = [ hystory_files_basename + '.history#' + str(num) for num in xrange(11, 20) ] F_list = [[0. for k_component in xrange(9)] for k_cell in xrange(n_cells)] for k_component in xrange(9): history_file = open(history_files_names[k_component], 'r') for line in history_file: if line.startswith('*') or line.startswith('$'): continue line = line.split() F_list[int(line[0]) - 1][k_component] = float(line[1]) history_file.close() F_array = myVTK.createFloatArray(array_name, 9, n_cells) for k_cell in xrange(n_cells): F_array.SetTuple(k_cell, F_list[k_cell]) myVTK.myPrint(verbose - 1, "n_tuples = " + str(F_array.GetNumberOfTuples())) mesh.GetCellData().AddArray(F_array)
def computeHelixAngles(farray_eRR, farray_eCC, farray_eLL, farray_eF, verbose=0): myVTK.myPrint(verbose, "*** computeHelixAngles ***") n_tuples = farray_eRR.GetNumberOfTuples() assert (farray_eCC.GetNumberOfTuples() == n_tuples) assert (farray_eLL.GetNumberOfTuples() == n_tuples) assert (farray_eF.GetNumberOfTuples() == n_tuples) farray_angle_helix = myVTK.createFloatArray("angle_helix", 1, n_tuples) eRR = numpy.empty(3) eCC = numpy.empty(3) eLL = numpy.empty(3) eF = numpy.empty(3) for k_tuple in xrange(n_tuples): farray_eRR.GetTuple(k_tuple, eRR) farray_eCC.GetTuple(k_tuple, eCC) farray_eLL.GetTuple(k_tuple, eLL) farray_eF.GetTuple(k_tuple, eF) eF -= numpy.dot(eF, eRR) * eRR eF /= numpy.linalg.norm(eF) helix_angle = math.copysign(1., numpy.dot(eF, eCC)) * math.asin( min(1., max(-1., numpy.dot(eF, eLL)))) * (180. / math.pi) farray_angle_helix.SetTuple1(k_tuple, helix_angle) return farray_angle_helix
def computeImageGradient( image=None, image_filename=None, image_dimensionality=None, verbose=0): myVTK.myPrint(verbose, "*** computeImageGradient ***") image = myVTK.initImage(image, image_filename, verbose-1) if (image_dimensionality is None): image_dimensionality = myVTK.computeImageDimensionality( image=image, verbose=verbose-1) image_gradient = vtk.vtkImageGradient() if (vtk.vtkVersion.GetVTKMajorVersion() >= 6): image_gradient.SetInputData(image) else: image_gradient.SetInput(image) image_gradient.SetDimensionality(image_dimensionality) image_gradient.Update() image_w_grad = image_gradient.GetOutput() return image_w_grad
def computeImageDimensionality( image=None, image_filename=None, verbose=1): myVTK.myPrint(verbose, "*** computeImageDimensionality ***") image = myVTK.initImage(image, image_filename, verbose-1) extent = image.GetExtent() DX = extent[1]+1-extent[0] DY = extent[3]+1-extent[2] DZ = extent[5]+1-extent[4] if (DX > 1) and (DY > 1) and (DZ > 1): dimensionality = 3 elif (DX > 1) and (DY > 1) and (DZ == 1): dimensionality = 2 elif (DX > 1) and (DY == 1) and (DZ == 1): dimensionality = 1 else: assert (0), "Wrong image dimensionality ("+str(extent)+")" #dimensionality = sum([extent[2*k_dim+1]>extent[2*k_dim] for k_dim in range(3)]) myVTK.myPrint(verbose-1, "dimensionality = "+str(dimensionality)) return dimensionality
def readAbaqusStressFromDAT(data_filename, verbose=0): myVTK.myPrint(verbose, "*** readAbaqusStressFromDAT: " + data_filename + " ***") s_array = myVTK.createFloatArray("", 6) data_file = open(data_filename, 'r') context = "" k_cell = 0 for line in data_file: if (context == "reading stresses"): #print line if ("MAXIMUM" in line): context = "" continue if ("OR" in line): splitted_line = line.split() assert (int(splitted_line[0]) == k_cell + 1), "Wrong element number. Aborting." s_list = [float(splitted_line[k]) for k in xrange(3, 9)] s_array.InsertNextTuple(s_list) k_cell += 1 if (line == " ELEMENT PT FOOT- S11 S22 S33 S12 S13 S23 \n" ): context = "reading stresses" data_file.close() myVTK.myPrint(verbose - 1, "n_tuples = " + str(s_array.GetNumberOfTuples())) return s_array
def addCartesianCoordinates( ugrid, verbose=1): myVTK.myPrint(verbose, "*** addCartesianCoordinates ***") points = ugrid.GetPoints() (farray_xx, farray_yy, farray_zz) = computeCartesianCoordinates( points=points, verbose=verbose-1) ugrid.GetPointData().AddArray(farray_xx) ugrid.GetPointData().AddArray(farray_yy) ugrid.GetPointData().AddArray(farray_zz) cell_centers = myVTK.getCellCenters( mesh=ugrid, verbose=verbose-1) (farray_xx, farray_yy, farray_zz) = computeCartesianCoordinates( points=cell_centers, verbose=verbose-1) ugrid.GetCellData().AddArray(farray_xx) ugrid.GetCellData().AddArray(farray_yy) ugrid.GetCellData().AddArray(farray_zz)
def computeABPointsFromBoundsAndCenter(mesh, AB=[0, 0, 1], verbose=0): myVTK.myPrint(verbose, "*** computeABPointsFromBoundsAndCenter ***") C = numpy.array(mesh.GetCenter()) #print "C ="+str(C) bounds = mesh.GetBounds() diag = numpy.array( [bounds[1] - bounds[0], bounds[3] - bounds[2], bounds[5] - bounds[4]]) AB = numpy.array(AB) AB = abs(numpy.dot(diag, AB)) * AB #print "bounds ="+str(bounds) #print "diag ="+str(diag) #print "AB ="+str(AB) point_A = C - AB / 2 point_B = C + AB / 2 #print "point_A ="+str(point_A) #print "point_B ="+str(point_B) points_AB = vtk.vtkPoints() points_AB.InsertNextPoint(point_A) points_AB.InsertNextPoint(point_B) #print points_AB return points_AB
def computeSectorsForLV( farray_rr, farray_cc, farray_ll, n_r=1, n_c=1, n_l=1, iarray_part_id=None, verbose=0): myVTK.myPrint(verbose, "*** computeSectorsForLV ***") n_cells = farray_rr.GetNumberOfTuples() iarray_sector = myVTK.createIntArray("sector_id", 1, n_cells) for k_cell in range(n_cells): if (iarray_part_id is not None) and (int(iarray_part_id.GetTuple1(k_cell)) > 0): sector_id = -1 else: rr = farray_rr.GetTuple1(k_cell) cc = farray_cc.GetTuple1(k_cell) ll = farray_ll.GetTuple1(k_cell) k_r = int(rr*n_r/1.000001) k_c = int(cc*n_c/1.000001) k_l = int((1.-ll)*n_l/1.000001) sector_id = k_l * n_c * n_r + k_c * n_r + k_r iarray_sector.SetTuple1(k_cell, sector_id) return iarray_sector
def readAbaqusDeformationGradientsFromDAT( data_filename, verbose=0): myVTK.myPrint(verbose, "*** readAbaqusDeformationGradientsFromDAT: "+data_filename+" ***") farray_F = myVTK.createFloatArray("F", 9) data_file = open(data_filename, 'r') context = "" k_cell = 0 for line in data_file: if (context == "reading deformation gradients"): #print line if ("MAXIMUM" in line): context = "" continue if ("OR" in line): splitted_line = line.split() assert (int(splitted_line[0]) == k_cell+1), "Wrong element number. Aborting." F_list = [float(splitted_line[ 3]), float(splitted_line[ 6]), float(splitted_line[7]), float(splitted_line[ 9]), float(splitted_line[ 4]), float(splitted_line[8]), float(splitted_line[10]), float(splitted_line[11]), float(splitted_line[5])] farray_F.InsertNextTuple(F_list) k_cell += 1 if (line == " ELEMENT PT FOOT- DG11 DG22 DG33 DG12 DG13 DG23 DG21 DG31 DG32 \n"): context = "reading deformation gradients" data_file.close() myVTK.myPrint(verbose-1, "n_tuples = "+str(farray_F.GetNumberOfTuples())) return farray_F
def rotatePoints( old_points, C, R, verbose=1): myVTK.myPrint(verbose, "*** rotatePoints ***") n_points = old_points.GetNumberOfPoints() new_points = vtk.vtkPoints() new_points.SetNumberOfPoints(n_points) old_point = numpy.array([0.]*3) for k_point in xrange(n_points): old_points.GetPoint(k_point, old_point) #print old_point new_point = C + numpy.dot(R, old_point - C) #print new_point new_points.InsertPoint(k_point, new_point) return new_points
def computeABPointsFromTTTSectors( ugrid_sectors, verbose=1): myVTK.myPrint(verbose, "*** computeABPointsFromTTTSectors ***") n_points = ugrid_sectors.GetNumberOfPoints() n_cells = ugrid_sectors.GetNumberOfCells() n_csects = 12 n_rsects = 3 n_slices = n_points / (n_rsects+1) / (n_csects+1) myVTK.myPrint(verbose-1, "n_slices =", n_slices) zmin = ugrid_sectors.GetPoint(0)[2] zmax = ugrid_sectors.GetPoint(ugrid_sectors.GetNumberOfPoints()-1)[2] dist_btw_slices = abs(zmin-zmax)/(n_slices-1) myVTK.myPrint(verbose-1, "dist_btw_slices =", dist_btw_slices) A = ugrid_sectors.GetPoints().GetPoint(0) B = ugrid_sectors.GetPoints().GetPoint(6) C = ugrid_sectors.GetPoints().GetPoint(3) D = ugrid_sectors.GetPoints().GetPoint(9) #print A #print B #print C #print D Px = ((A[0]*B[1]-A[1]*B[0])*(C[0]-D[0])-(A[0]-B[0])*(C[0]*D[1]-C[1]*D[0]))/((A[0]-B[0])*(C[1]-D[1])-(A[1]-B[1])*(C[0]-D[0])) Py = ((A[0]*B[1]-A[1]*B[0])*(C[1]-D[1])-(A[1]-B[1])*(C[0]*D[1]-C[1]*D[0]))/((A[0]-B[0])*(C[1]-D[1])-(A[1]-B[1])*(C[0]-D[0])) #print Px #print Py A = [Px, Py, zmin] B = [Px, Py, zmax] myVTK.myPrint(verbose-1, "A =", A) myVTK.myPrint(verbose-1, "B =", B) points_AB = vtk.vtkPoints() points_AB.InsertNextPoint(A) points_AB.InsertNextPoint(B) cells_AB = vtk.vtkCellArray() cell_AB = vtk.vtkVertex() cell_AB.GetPointIds().SetId(0, 0) cells_AB.InsertNextCell(cell_AB) cell_AB.GetPointIds().SetId(0, 1) cells_AB.InsertNextCell(cell_AB) AB_ugrid = vtk.vtkUnstructuredGrid() AB_ugrid.SetPoints(points_AB) AB_ugrid.SetCells(vtk.VTK_VERTEX, cells_AB) return points_AB
def writeFiberOrientationFileForGNUPlot(angles_end, angles_epi, filename, verbose=0): myVTK.myPrint(verbose, "*** writeFiberOrientationFileForGNUPlot ***") assert ( len(angles_end) == len(angles_epi) ), "angles_end and angle_epi must have same length (n_l). Aborting." n_l = len(angles_end) d_l = 1. / (n_l - 1) n_c = len(angles_end[0]) for angles in angles_end + angles_epi: assert (len(angles) == n_c ), "angles lists must have same length (n_c). Aborting." d_c = 1. / n_c fiber_orientation_file = open(filename, 'w') fiber_orientation_file.write('# c ang_end ang_epi z\n') n_c = 12 for k_c in xrange(n_c + 1): c = float(k_c) / n_c i_c = int(c / d_c / 1.000001) zeta = (c - i_c * d_c) / d_c n_l = 10 for k_l in xrange(n_l + 1): l = float(k_l) / n_l i_l = int(l / d_l / 1.000001) eta = (l - i_l * d_l) / d_l t_ii_end = angles_end[i_l][i_c % n_c] t_ji_end = angles_end[i_l][(i_c + 1) % n_c] t_ij_end = angles_end[i_l + 1][i_c % n_c] t_jj_end = angles_end[i_l + 1][(i_c + 1) % n_c] t_ii_epi = angles_epi[i_l][i_c % n_c] t_ji_epi = angles_epi[i_l][(i_c + 1) % n_c] t_ij_epi = angles_epi[i_l + 1][i_c % n_c] t_jj_epi = angles_epi[i_l + 1][(i_c + 1) % n_c] helix_angle_end = t_ii_end * (1 - zeta - eta + zeta*eta) \ + t_ji_end * ( zeta - zeta*eta) \ + t_ij_end * ( eta - zeta*eta) \ + t_jj_end * ( zeta*eta) helix_angle_epi = t_ii_epi * (1 - zeta - eta + zeta*eta) \ + t_ji_epi * ( zeta - zeta*eta) \ + t_ij_epi * ( eta - zeta*eta) \ + t_jj_epi * ( zeta*eta) fiber_orientation_file.write(" ".join( [str(x) for x in [t, helix_angle_end, helix_angle_epi, z]]) + "\n") fiber_orientation_file.write("\n") fiber_orientation_file.close()
def generateWarpedMesh( mesh_folder, mesh_basename, images, structure, deformation, evolution, verbose=0): myVTK.myPrint(verbose, "*** generateWarpedMesh ***") mesh = myVTK.readUGrid( filename=mesh_folder+"/"+mesh_basename+".vtk", verbose=verbose-1) n_points = mesh.GetNumberOfPoints() n_cells = mesh.GetNumberOfCells() if os.path.exists(mesh_folder+"/"+mesh_basename+"-WithLocalBasis.vtk"): ref_mesh = myVTK.readUGrid( filename=mesh_folder+"/"+mesh_basename+"-WithLocalBasis.vtk", verbose=verbose-1) else: ref_mesh = None farray_disp = myVTK.createFloatArray( name="displacement", n_components=3, n_tuples=n_points, verbose=verbose-1) mesh.GetPointData().AddArray(farray_disp) mapping = Mapping(images, structure, deformation, evolution) X = numpy.empty(3) x = numpy.empty(3) U = numpy.empty(3) if ("zfill" not in images.keys()): images["zfill"] = len(str(images["n_frames"])) for k_frame in xrange(images["n_frames"]): t = images["T"]*float(k_frame)/(images["n_frames"]-1) if (images["n_frames"]>1) else 0. mapping.init_t(t) for k_point in xrange(n_points): mesh.GetPoint(k_point, X) mapping.x(X, x) U = x - X farray_disp.SetTuple(k_point, U) myVTK.computeStrainsFromDisplacements( mesh=mesh, disp_array_name="displacement", ref_mesh=ref_mesh, verbose=verbose-1) myVTK.writeUGrid( ugrid=mesh, filename=mesh_folder+"/"+mesh_basename+"_"+str(k_frame).zfill(images["zfill"])+".vtk", verbose=verbose-1)
def getPointLocator(mesh, verbose=0): myVTK.myPrint(verbose, "*** getPointLocator ***") point_locator = vtk.vtkPointLocator() point_locator.SetDataSet(mesh) point_locator.Update() return point_locator
def addPseudoProlateSpheroidalCoordinatesAndBasisToBiV( ugrid, pdata_endLV, pdata_endRV, pdata_epi, verbose=1): myVTK.myPrint(verbose, "*** addPseudoProlateSpheroidalCoordinatesAndBasisToBiV ***") (farray_rr, farray_cc, farray_ll, farray_eRR, farray_eCC, farray_eLL) = computePseudoProlateSpheroidalCoordinatesAndBasisForBiV( points=ugrid.GetPoints(), iarray_regions=ugrid.GetPointData().GetArray("region_id"), farray_c=ugrid.GetPointData().GetArray("c"), farray_l=ugrid.GetPointData().GetArray("l"), farray_eL=ugrid.GetPointData().GetArray("eL"), pdata_endLV=pdata_endLV, pdata_endRV=pdata_endRV, pdata_epi=pdata_epi, iarray_part_id=ugrid.GetPointData().GetArray("part_id"), verbose=verbose-1) ugrid.GetPointData().AddArray(farray_rr) ugrid.GetPointData().AddArray(farray_cc) ugrid.GetPointData().AddArray(farray_ll) ugrid.GetPointData().AddArray(farray_eRR) ugrid.GetPointData().AddArray(farray_eCC) ugrid.GetPointData().AddArray(farray_eLL) cell_centers = myVTK.getCellCenters( mesh=ugrid, verbose=verbose-1) (farray_rr, farray_cc, farray_ll, farray_eRR, farray_eCC, farray_eLL) = computePseudoProlateSpheroidalCoordinatesAndBasisForBiV( points=cell_centers, iarray_regions=ugrid.GetCellData().GetArray("region_id"), farray_c=ugrid.GetCellData().GetArray("c"), farray_l=ugrid.GetCellData().GetArray("l"), farray_eL=ugrid.GetCellData().GetArray("eL"), pdata_endLV=pdata_endLV, pdata_endRV=pdata_endRV, pdata_epi=pdata_epi, iarray_part_id=ugrid.GetCellData().GetArray("part_id"), verbose=verbose-1) ugrid.GetCellData().AddArray(farray_rr) ugrid.GetCellData().AddArray(farray_cc) ugrid.GetCellData().AddArray(farray_ll) ugrid.GetCellData().AddArray(farray_eRR) ugrid.GetCellData().AddArray(farray_eCC) ugrid.GetCellData().AddArray(farray_eLL)
def clipSurfacesForFullBiVMesh(pdata_endLV, pdata_endRV, pdata_epi, verbose=0): myVTK.myPrint(verbose, "*** clipSurfacesForFullBiVMesh ***") pdata_endLV_implicit_distance = vtk.vtkImplicitPolyDataDistance() pdata_endLV_implicit_distance.SetInput(pdata_endLV) pdata_endRV_implicit_distance = vtk.vtkImplicitPolyDataDistance() pdata_endRV_implicit_distance.SetInput(pdata_endRV) pdata_epi_implicit_distance = vtk.vtkImplicitPolyDataDistance() pdata_epi_implicit_distance.SetInput(pdata_epi) pdata_endLV_clip = vtk.vtkClipPolyData() if (vtk.vtkVersion.GetVTKMajorVersion() >= 6): pdata_endLV_clip.SetInputData(pdata_endLV) else: pdata_endLV_clip.SetInput(pdata_endLV) pdata_endLV_clip.SetClipFunction(pdata_epi_implicit_distance) pdata_endLV_clip.InsideOutOn() pdata_endLV_clip.Update() clipped_pdata_endLV = pdata_endLV_clip.GetOutput(0) pdata_endRV_clip = vtk.vtkClipPolyData() if (vtk.vtkVersion.GetVTKMajorVersion() >= 6): pdata_endRV_clip.SetInputData(pdata_endRV) else: pdata_endRV_clip.SetInput(pdata_endRV) pdata_endRV_clip.SetClipFunction(pdata_epi_implicit_distance) pdata_endRV_clip.InsideOutOn() pdata_endRV_clip.Update() clipped_pdata_endRV = pdata_endRV_clip.GetOutput(0) pdata_epi_clip = vtk.vtkClipPolyData() if (vtk.vtkVersion.GetVTKMajorVersion() >= 6): pdata_epi_clip.SetInputData(pdata_epi) else: pdata_epi_clip.SetInput(pdata_epi) pdata_epi_clip.SetClipFunction(pdata_endLV_implicit_distance) pdata_epi_clip.GenerateClippedOutputOn() pdata_epi_clip.Update() clipped_pdata_epi = pdata_epi_clip.GetOutput(0) clipped_valM = pdata_epi_clip.GetOutput(1) pdata_epi_clip = vtk.vtkClipPolyData() if (vtk.vtkVersion.GetVTKMajorVersion() >= 6): pdata_epi_clip.SetInputData(clipped_pdata_epi) else: pdata_epi_clip.SetInput(clipped_pdata_epi) pdata_epi_clip.SetClipFunction(pdata_endRV_implicit_distance) pdata_epi_clip.GenerateClippedOutputOn() pdata_epi_clip.Update() clipped_pdata_epi = pdata_epi_clip.GetOutput(0) clipped_valP = pdata_epi_clip.GetOutput(1) return (clipped_pdata_endLV, clipped_pdata_endRV, clipped_pdata_epi, clipped_valM, clipped_valP)
def generateWarpedMesh(mesh_folder, mesh_basename, images, structure, deformation, evolution, verbose=0): myVTK.myPrint(verbose, "*** generateWarpedMesh ***") mesh = myVTK.readUGrid(filename=mesh_folder + "/" + mesh_basename + ".vtk", verbose=verbose - 1) n_points = mesh.GetNumberOfPoints() n_cells = mesh.GetNumberOfCells() if os.path.exists(mesh_folder + "/" + mesh_basename + "-WithLocalBasis.vtk"): ref_mesh = myVTK.readUGrid(filename=mesh_folder + "/" + mesh_basename + "-WithLocalBasis.vtk", verbose=verbose - 1) else: ref_mesh = None farray_disp = myVTK.createFloatArray(name="displacement", n_components=3, n_tuples=n_points, verbose=verbose - 1) mesh.GetPointData().AddArray(farray_disp) mapping = Mapping(images, structure, deformation, evolution) X = numpy.empty(3) x = numpy.empty(3) U = numpy.empty(3) if ("zfill" not in images.keys()): images["zfill"] = len(str(images["n_frames"])) for k_frame in xrange(images["n_frames"]): t = images["T"] * float(k_frame) / (images["n_frames"] - 1) if ( images["n_frames"] > 1) else 0. mapping.init_t(t) for k_point in xrange(n_points): mesh.GetPoint(k_point, X) mapping.x(X, x) U = x - X farray_disp.SetTuple(k_point, U) myVTK.computeStrainsFromDisplacements(mesh=mesh, disp_array_name="displacement", ref_mesh=ref_mesh, verbose=verbose - 1) myVTK.writeUGrid(ugrid=mesh, filename=mesh_folder + "/" + mesh_basename + "_" + str(k_frame).zfill(images["zfill"]) + ".vtk", verbose=verbose - 1)
def createMassProperties( pdata, verbose=1): myVTK.myPrint(verbose, "*** createMassProperties ***") mass_properties = vtk.vtkMassProperties() mass_properties.SetInputData(pdata) return mass_properties
def getPDataSurfaceArea( pdata, verbose=0): myVTK.myPrint(verbose, "*** getPDataSurfaceArea ***") mass_properties = myVTK.createMassProperties( pdata=pdata, verbose=verbose-1) return mass_properties.GetSurfaceArea()
def createMassProperties(pdata, verbose=0): myVTK.myPrint(verbose, "*** createMassProperties ***") mass_properties = vtk.vtkMassProperties() if (vtk.vtkVersion.GetVTKMajorVersion() >= 6): mass_properties.SetInputData(pdata) else: mass_properties.SetInput(pdata) return mass_properties
def computeImageHessian(image=None, image_filename=None, verbose=0): myVTK.myPrint(verbose, "*** computeImageHessian ***") image = myVTK.initImage(image, image_filename, verbose - 1) image_dimensionality = myVTK.computeImageDimensionality(image=image, verbose=verbose - 1) image_gradient = vtk.vtkImageGradient() if (vtk.vtkVersion.GetVTKMajorVersion() >= 6): image_gradient.SetInputData(image) else: image_gradient.SetInput(image) image_gradient.SetDimensionality(image_dimensionality) image_gradient.Update() image_w_gradient = image_gradient.GetOutput() image_append_components = vtk.vtkImageAppendComponents() for k_dim in xrange(image_dimensionality): image_extract_components = vtk.vtkImageExtractComponents() if (vtk.vtkVersion.GetVTKMajorVersion() >= 6): image_extract_components.SetInputData(image_w_gradient) else: image_extract_components.SetInput(image_w_gradient) image_extract_components.SetComponents(k_dim) image_extract_components.Update() image_w_gradient_component = image_extract_components.GetOutput() image_gradient = vtk.vtkImageGradient() if (vtk.vtkVersion.GetVTKMajorVersion() >= 6): image_gradient.SetInputData(image_w_gradient_component) else: image_gradient.SetInput(image_w_gradient_component) image_gradient.SetDimensionality(image_dimensionality) image_gradient.Update() image_w_hessian_component = image_gradient.GetOutput() if (vtk.vtkVersion.GetVTKMajorVersion() >= 6): image_append_components.AddInputData(image_w_hessian_component) else: image_append_components.AddInput(image_w_hessian_component) image_append_components.Update() image_w_hessian = image_append_components.GetOutput() name = image.GetPointData().GetScalars().GetName() image.GetPointData().AddArray( image_w_gradient.GetPointData().GetArray(name + "Gradient")) image.GetPointData().AddArray( image_w_hessian.GetPointData().GetArray(name + "GradientGradient")) image.GetPointData().SetActiveScalars(name + "GradientGradient") return image
def computeHelixTransverseSheetAngles(farray_eRR, farray_eCC, farray_eLL, farray_eF, farray_eS, farray_eN, use_new_definition=False, verbose=0): myVTK.myPrint(verbose, "*** computeHelixTransverseSheetAngles ***") n_tuples = farray_eRR.GetNumberOfTuples() assert (farray_eCC.GetNumberOfTuples() == n_tuples) assert (farray_eLL.GetNumberOfTuples() == n_tuples) assert (farray_eF.GetNumberOfTuples() == n_tuples) assert (farray_eS.GetNumberOfTuples() == n_tuples) assert (farray_eN.GetNumberOfTuples() == n_tuples) farray_angle_helix = myVTK.createFloatArray("angle_helix", 1, n_tuples) farray_angle_trans = myVTK.createFloatArray("angle_trans", 1, n_tuples) farray_angle_sheet = myVTK.createFloatArray("angle_sheet", 1, n_tuples) eRR = numpy.empty(3) eCC = numpy.empty(3) eLL = numpy.empty(3) eF = numpy.empty(3) for k_tuple in xrange(n_tuples): farray_eRR.GetTuple(k_tuple, eRR) farray_eCC.GetTuple(k_tuple, eCC) farray_eLL.GetTuple(k_tuple, eLL) farray_eF.GetTuple(k_tuple, eF) eF -= numpy.dot(eF, eRR) * eRR eF /= numpy.linalg.norm(eF) angle_helix = math.copysign(1., numpy.dot(eF, eCC)) * math.asin( min(1., max(-1., numpy.dot(eF, eLL)))) * (180. / math.pi) farray_angle_helix.SetTuple1(k_tuple, angle_helix) eF = numpy.array(farray_eF.GetTuple(k_tuple)) eF -= numpy.dot(eF, eLL) * eLL eF /= numpy.linalg.norm(eF) angle_trans = math.copysign(-1., numpy.dot(eF, eCC)) * math.asin( min(1., max(-1., numpy.dot(eF, eRR)))) * (180. / math.pi) farray_angle_trans.SetTuple1(k_tuple, angle_trans) #if (use_new_definition): #assert 0, "TODO" #else: #assert 0, "TODO" return (farray_angle_helix, farray_angle_trans, farray_angle_sheet)
def filterPDataIntoUGrid( pdata, verbose=0): myVTK.myPrint(verbose, "*** filterPDataIntoUGrid ***") filter_append = vtk.vtkAppendFilter() if (vtk.vtkVersion.GetVTKMajorVersion() >= 6): filter_append.SetInputData(pdata) else: filter_append.SetInput(pdata) filter_append.Update() return filter_append.GetOutput()
def thresholdPData(pdata_mesh, field_support, field_name, threshold_value, threshold_by_upper_or_lower, verbose=0): myVTK.myPrint(verbose, "*** thresholdPData ***") ugrid_thresholded_mesh = thresholdUGrid(pdata_mesh, field_support, field_name, threshold_value, threshold_by_upper_or_lower, False) pdata_thresholded_mesh = filterUGridIntoPData(ugrid_thresholded_mesh, False) return pdata_thresholded_mesh
def splitDomainBetweenEndoAndEpi( pdata_domain, r=0.99, verbose=0): myVTK.myPrint(verbose, "*** splitDomainBetweenEndoAndEpi ***") bounds = pdata_domain.GetBounds() assert (r > 0.) assert (r < 1.) origin = [(1./2)*bounds[0]+(1./2)*bounds[1], (1./2)*bounds[2]+(1./2)*bounds[3], (1.-r)*bounds[4]+( r )*bounds[5]] #myVTK.myPrint(verbose-1, "bounds = "+str(bounds)) #myVTK.myPrint(verbose-1, "origin = "+str(origin)) (pdata_domain, cap) = myVTK.clipPDataUsingPlane( pdata_mesh=pdata_domain, plane_O=origin, plane_N=[0,0,1], verbose=verbose-1) connectivity0 = vtk.vtkPolyDataConnectivityFilter() connectivity0.SetExtractionModeToSpecifiedRegions() connectivity0.AddSpecifiedRegion(0) if (vtk.vtkVersion.GetVTKMajorVersion() >= 6): connectivity0.SetInputData(pdata_domain) else: connectivity0.SetInput(pdata_domain) connectivity0.Update() pdata0 = connectivity0.GetOutput() assert (pdata0.GetNumberOfPoints()) connectivity1 = vtk.vtkPolyDataConnectivityFilter() connectivity1.SetExtractionModeToSpecifiedRegions() connectivity1.AddSpecifiedRegion(1) if (vtk.vtkVersion.GetVTKMajorVersion() >= 6): connectivity1.SetInputData(pdata_domain) else: connectivity1.SetInput(pdata_domain) connectivity1.Update() pdata1 = connectivity1.GetOutput() assert (pdata1.GetNumberOfPoints()) if (myVTK.getPDataSurfaceArea(pdata0,0) < myVTK.getPDataSurfaceArea(pdata1,0)): return pdata0, pdata1 else: return pdata1, pdata0
def discretizeData(farray_rr, farray_cc, farray_ll, farray_h, n_r, n_c, n_l, verbose=True): myVTK.myPrint(verbose, "*** discretizeData ***") n_tuples = farray_ll.GetNumberOfTuples() h = numpy.empty((n_r, n_c, n_l)) for k_l in xrange(n_l): #print "k_l = "+str(k_l) sel_l = [ k_tuple for k_tuple in xrange(n_tuples) if (math.floor(n_l * farray_ll.GetTuple1(k_tuple)) == k_l) ] #print len(sel_l) for k_c in xrange(n_c): #print "k_c = "+str(k_c) sel_c = [ k_tuple for k_tuple in sel_l if (math.floor(n_c * farray_cc.GetTuple1(k_tuple)) == k_c) ] #print len(sel_c) for k_r in xrange(n_r): #print "k_r = "+str(k_r) sel_r = [ k_tuple for k_tuple in sel_c if (math.floor(n_r * farray_rr.GetTuple1(k_tuple)) == k_r) ] #print len(sel_r) (m, s) = computeMeanStddevAngles( [farray_h.GetTuple1(k_tuple) for k_tuple in sel_r], verbose=False) h[k_r, k_c, k_l] = m if (verbose >= 2): print h return h
def addSectorsToLV(ugrid_mesh, n_r=1, n_c=1, n_l=1, verbose=0): myVTK.myPrint(verbose, "*** addSectorsToLV ***") iarray_sector = computeSectorsForLV( farray_rr=ugrid_mesh.GetCellData().GetArray("rr"), farray_cc=ugrid_mesh.GetCellData().GetArray("cc"), farray_ll=ugrid_mesh.GetCellData().GetArray("ll"), n_r=n_r, n_c=n_c, n_l=n_l, iarray_part_id=ugrid_mesh.GetCellData().GetArray("part_id"), verbose=verbose - 1) ugrid_mesh.GetCellData().AddArray(iarray_sector)