Advertisement
Guest User

Example usage of vtkVoxelContoursToSurfaceFilter for gluing

a guest
Mar 5th, 2018
361
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.57 KB | None | 0 0
  1.     # Example that illustrates usage of vtkVoxelContoursToSurfaceFilter.
  2.     # The code can serve as guidance, but it is not functional, since
  3.     # several functions are not shown here.
  4.     # NOTE: It appears that vtkVoxelContoursToSurfaceFilter assumes the
  5.     #       contours to be aligned with the coordinate axes.
  6.     #
  7.     # Code is written by Normanius Feb. 2018.
  8.    
  9.     # Compute transform that maps edge1 onto the XY-plane.
  10.     # Center of mass is mapped to the origin.
  11.     trafo1 = findTransformFromPoints(edge1)
  12.  
  13.     # vtkVoxelContoursToSurfaceFilter assumes a polygon (not a polyline).
  14.     edge1 = polylineToPolygon(edge1)
  15.     edge2 = polylineToPolygon(edge2)
  16.  
  17.     # Compute diff vector.
  18.     center1 = sampleCenter(edge1)
  19.     center2 = sampleCenter(edge2)
  20.     center1T = np.array(trafo1.TransformPoint(center1))
  21.     center2T = np.array(trafo1.TransformPoint(center2))
  22.     diff = center2T-center1T
  23.  
  24.     # Transform the edge planes with the computed transform
  25.     edge1T = applyTransform(edge1, trafo1)
  26.     edge2T = applyTransform(edge2, trafo1)
  27.  
  28.     # Combine the polydata objects (using vtkAppendPolyData)
  29.     combo = combinePolyData(edge1T, edge2T)
  30.  
  31.     # Convert to ijk coordinates for the contour to surface filter.
  32.     # This follows pretty much the "official" example of
  33.     # vtkVoxelContoursToSurfaceFilter.
  34.     bounds = combo.GetBounds()
  35.     origin = ((bounds[0], bounds[2], bounds[4]))
  36.     spacing = ((bounds[1] - bounds[0]) / 80.,
  37.                (bounds[3] - bounds[2]) / 80.,
  38.                diff[2])  # This must be the distance between the two contours
  39.  
  40.     poly = vtk.vtkPolyData()
  41.     points = vtk.vtkPoints()
  42.     points.SetNumberOfPoints(combo.GetNumberOfPoints())
  43.     for i in range(combo.GetNumberOfPoints()):
  44.         p = list(combo.GetPoint(i))
  45.         p[0] = round((p[0] - origin[0]) / spacing[0])
  46.         p[1] = round((p[1] - origin[1]) / spacing[1])
  47.         p[2] = round((p[2] - origin[2]) / spacing[2])
  48.         points.SetPoint(i,p)
  49.     poly.SetPolys(combo.GetPolys())
  50.     poly.SetPoints(points)
  51.     poly.BuildLinks()
  52.     result = vtk.vtkVoxelContoursToSurfaceFilter()
  53.     result.SetInputData(poly)
  54.     result.SetSpacing(spacing)
  55.     result.Update()
  56.  
  57.     # Scale and move the result.
  58.     scaleCenter = np.array(result.GetOutput().GetCenter())
  59.     scaleBounds = np.array(result.GetOutput().GetBounds())
  60.     center = np.array(combo.GetCenter())
  61.  
  62.     trafoFix = vtk.vtkTransform()
  63.     trafoFix.Translate(-scaleCenter)
  64.     trafoFix.Scale(float(bounds[1] - bounds[0])/(scaleBounds[1] - scaleBounds[0]),
  65.                 float(bounds[3] - bounds[2])/(scaleBounds[3] - scaleBounds[2]),
  66.                 float(bounds[5] - bounds[4])/(scaleBounds[5] - scaleBounds[4]))
  67.     trafoFix.Translate(center)
  68.     result = applyTransform(result, trafoFix)
  69.  
  70.     # This is tricky: we need to remove the "caps" that have been created
  71.     # by vtkVoxelContoursToSurfaceFilter: the resulting object is closed,
  72.     # but for our problem, we need the caps to be removed.
  73.     result = removeCellsCloseToReference(source=result,
  74.                                          reference=edge1T,
  75.                                          referenceNormal=[0.,0.,1],
  76.                                          zTolerance=None)
  77.     result = removeCellsCloseToReference(source=result,
  78.                                          reference=edge2T,
  79.                                          referenceNormal=[0.,0.,1],
  80.                                          zTolerance=None)
  81.  
  82.     # Transform back to original position.
  83.     result = applyTransform(result, trafo1.GetInverse())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement