def __init__(self, module_manager): SimpleVTKClassModuleBase.__init__( self, module_manager, vtk.vtkMarchingSquares(), 'Processing.', ('vtkImageData',), ('vtkPolyData',), replaceDoc=True, inputFunctions=None, outputFunctions=None)
def marchingSquares(img, iso=0.0, mode='center'): s = img.shape alg = vtk.vtkMarchingSquares() sp = VTKNumpytoSP(img) alg.SetInputData(sp) alg.SetValue(0, iso) alg.Update() pds = alg.GetOutput() a = vtk.vtkPolyDataConnectivityFilter() a.SetInputData(pds) if mode == 'center': a.SetExtractionModeToClosestPointRegion() a.SetClosestPoint(float(s[0]) / 2, float(s[1]) / 2, 0.0) elif mode == 'all': a.SetExtractionModeToAllRegions() a.Update() pds = a.GetOutput() if pds.GetPoints() is None: return np.asarray([[0.0, 0.0], [0.0, 0.0], [0.0, 0.0]]) else: pds = VTKPDPointstoNumpy(pds) if len(pds) <= 1: return np.asarray([[0.0, 0.0], [0.0, 0.0], [0.0, 0.0]]) return pds
def marchingSquares(img, iso=0.0, mode='center', asNumpy=True): alg = vtk.vtkMarchingSquares() if asNumpy: sp = VTKNumpytoSP(img) else: sp = img alg.SetInputData(sp) alg.SetValue(0, iso) alg.Update() pds = alg.GetOutput() a = vtk.vtkPolyDataConnectivityFilter() a.SetInputData(pds) if mode == 'center': a.SetExtractionModeToClosestPointRegion() a.SetClosestPoint(0.0, 0.0, 0.0) elif mode == 'all': a.SetExtractionModeToAllRegions() a.Update() pds = a.GetOutput() return pds
def threshold(self, value=None, flip=False): """ Create a polygonal Mesh from a Picture by filling regions with pixels luminosity above a specified value. Parameters ---------- value : float, optional The default is None, e.i. 1/3 of the scalar range. flip: bool, optional Flip polygon orientations Returns ------- Mesh A polygonal mesh. """ mgf = vtk.vtkImageMagnitude() mgf.SetInputData(self._data) mgf.Update() msq = vtk.vtkMarchingSquares() msq.SetInputData(mgf.GetOutput()) if value is None: r0, r1 = self._data.GetScalarRange() value = r0 + (r1 - r0) / 3 msq.SetValue(0, value) msq.Update() if flip: rs = vtk.vtkReverseSense() rs.SetInputData(msq.GetOutput()) rs.ReverseCellsOn() rs.ReverseNormalsOff() rs.Update() output = rs.GetOutput() else: output = msq.GetOutput() ctr = vtk.vtkContourTriangulator() ctr.SetInputData(output) ctr.Update() return vedo.Mesh(ctr.GetOutput(), c='k').bc('t').lighting('off')
def filter_marching_squares(vtkObject, isovalue=1e-5): """ Create vtkMarchingSquares filter on vtkObject. :param vtkObject: input :param isovalue: contour value :param vtkObject: :py:class:`vtk.vtkObject` :type isovalue: float >>> image_data = create_image_data_from_array(surf) >>> squares = filter_marching_cubes(image_data) >>> show(squares) """ squares = vtk.vtkMarchingSquares() pipe(vtkObject, squares) squares.SetValue(0, isovalue) #squares.ComputeScalarsOn() #squares.ComputeNormalsOn() return squares
renWin = vtk.vtkRenderWindow() renWin.AddRenderer(ren1) iren = vtk.vtkRenderWindowInteractor() iren.SetRenderWindow(renWin) # create pipeline # v16 = vtk.vtkVolume16Reader() v16.SetDataDimensions(64,64) v16.GetOutput().SetOrigin(0.0,0.0,0.0) v16.SetDataByteOrderToLittleEndian() v16.SetFilePrefix("" + str(VTK_DATA_ROOT) + "/Data/headsq/quarter") v16.SetImageRange(1,93) v16.SetDataSpacing(3.2,3.2,1.5) v16.Update() myLocator = vtk.vtkMergePoints() isoXY = vtk.vtkMarchingSquares() isoXY.SetInputConnection(v16.GetOutputPort()) isoXY.GenerateValues(2,600,1200) isoXY.SetImageRange(0,32,32,63,45,45) isoXY.SetLocator(myLocator) isoXYMapper = vtk.vtkPolyDataMapper() isoXYMapper.SetInputConnection(isoXY.GetOutputPort()) isoXYMapper.SetScalarRange(600,1200) isoXYActor = vtk.vtkActor() isoXYActor.SetMapper(isoXYMapper) isoYZ = vtk.vtkMarchingSquares() isoYZ.SetInputConnection(v16.GetOutputPort()) isoYZ.GenerateValues(2,600,1200) isoYZ.SetImageRange(32,32,32,63,46,92) isoYZMapper = vtk.vtkPolyDataMapper() isoYZMapper.SetInputConnection(isoYZ.GetOutputPort())
def __get_contour_data_vtk(self, x_grid, y_grid, z_grid, isovalue=[0]): """ wrapper for vtk marching squares function. Extracting contour information into bokeh compatible data type. Less comfortable (no text labels, no coloring) but faster then __get_contour_data_mpl :param x_grid: :param y_grid: :param z_grid: :param isovalue: :return: """ nx, ny = x_grid.shape xmin = self._plot.x_range.start xmax = self._plot.x_range.end ymin = self._plot.y_range.start ymax = self._plot.y_range.end hx = (xmax - xmin) / (nx - 1) hy = (ymax - ymin) / (ny - 1) image = vtk.vtkImageData() image.SetDimensions(nx, ny, 1) image.SetOrigin(xmin, ymin, 0) image.SetSpacing(hx, hy, 0) data = vtk.vtkDoubleArray() data.SetNumberOfComponents(1) data.SetNumberOfTuples(image.GetNumberOfPoints()) data.SetName("Values") # we load the z_data into vtk datatypes vtk_data_array = numpy_support.numpy_to_vtk(z_grid.ravel(), deep=True, array_type=vtk.VTK_DOUBLE) image.AllocateScalars(vtk.VTK_DOUBLE, 1) image.GetPointData().SetScalars(vtk_data_array) # apply marchign squares ms = vtk.vtkMarchingSquares() ms.SetInputData(image) for i in range(isovalue.__len__()): # set isovalues ms.SetValue(i, isovalue[i]) ms.SetImageRange(0, image.GetDimensions()[0], 0, image.GetDimensions()[1], 0, 0) ms.Update() # read output poly = ms.GetOutput() points = poly.GetPoints() lines = poly.GetLines() pts = numpy_support.vtk_to_numpy(points.GetData()) # get points line_idx = numpy_support.vtk_to_numpy(lines.GetData()) # get lines # lines are encoded as [nVerticesLine1, firstId, secondId, ... , nVerticesLine2...] # We always have 2 vertices per line. This justifies the stride 3 pattern below even_idx = line_idx[1::3] odd_idx = line_idx[2::3] x0 = pts[odd_idx, 0] y0 = pts[odd_idx, 1] x1 = pts[even_idx, 0] y1 = pts[even_idx, 1] data_contour = {'x0': x0, 'x1': x1, 'y0': y0, 'y1': y1} data_contour_label = {} return data_contour, data_contour_label
def main(): inputFileName, isoValue = get_program_parameters() reader = vtk.vtkPNGReader() if not reader.CanReadFile(inputFileName): print("Error: Could not read %s ." % (inputFileName)) reader.SetFileName(inputFileName) reader.Update() iso = vtk.vtkMarchingSquares() iso.SetInputConnection(reader.GetOutputPort()) iso.SetValue(0, isoValue) # Test smoothing here if 1: # Remove duplicate points and join up lines to form polylines pCleaner = vtk.vtkCleanPolyData() pCleaner.SetInputConnection(iso.GetOutputPort()) pStripper = vtk.vtkStripper() pStripper.SetInputConnection(pCleaner.GetOutputPort()) # Downsample and smooth the polyline pSpline = vtk.vtkSplineFilter() mSplineResamplingLength = 50.0 mReferenceLength = 40.0 pSpline.SetLength(mSplineResamplingLength / mReferenceLength) pSpline.SetSubdivideToLength() pSpline.SetInputConnection(pStripper.GetOutputPort()) pTriangle = vtk.vtkTriangleFilter() pTriangle.SetInputConnection(pSpline.GetOutputPort()) pTriangle.Update() pCleaned = pTriangle.GetOutput() newMapper = vtk.vtkDataSetMapper() newMapper.SetInputConnection(pTriangle.GetOutputPort()) newActor = vtk.vtkActor() newActor.SetMapper(newMapper) isoMapper = vtk.vtkDataSetMapper() isoMapper.SetInputConnection(iso.GetOutputPort()) isoMapper.ScalarVisibilityOff() isoActor = vtk.vtkActor() isoActor.SetMapper(isoMapper) isoActor.GetProperty().SetColor(0.8900, 0.8100, 0.3400) poly = vtk.vtkContourTriangulator() poly.SetInputConnection(iso.GetOutputPort()) polyMapper = vtk.vtkDataSetMapper() polyMapper.SetInputConnection(poly.GetOutputPort()) polyMapper.ScalarVisibilityOff() polyActor = vtk.vtkActor() polyActor.SetMapper(polyMapper) polyActor.GetProperty().SetColor(1.0000, 0.3882, 0.2784) # Standard rendering classes renderer = vtk.vtkRenderer() renWin = vtk.vtkRenderWindow() renWin.SetMultiSamples(0) renWin.AddRenderer(renderer) iren = vtk.vtkRenderWindowInteractor() iren.SetRenderWindow(renWin) renderer.AddActor(polyActor) # Important renderer.AddActor(isoActor) renderer.AddActor(newActor) # Standard testing code. renderer.SetBackground(0.5, 0.5, 0.5) renWin.SetSize(800, 800) camera = renderer.GetActiveCamera() renderer.ResetCamera() camera.Azimuth(180) renWin.Render() iren.Initialize() iren.Start()
for i in range(image.GetNumberOfPoints()): x,y,z = image.GetPoint(i) data.SetValue(i,x**2+y**2+z**2) image.GetPointData().AddArray(data) image.AllocateScalars(vtk.VTK_DOUBLE, 1) for z_id in range(image.GetDimensions()[2]): for y_id in range(image.GetDimensions()[1]): for x_id in range(image.GetDimensions()[0]): id = image.ComputePointId((x_id, y_id, z_id)) value = image.GetPointData().GetArray("Values").GetValue(id) image.SetScalarComponentFromDouble(x_id, y_id, z_id, 0, value) ms = vtk.vtkMarchingSquares() ms.SetInputData(image) ms.SetValue(0, .5) ms.SetValue(1, 1.0) ms.SetImageRange(0, image.GetDimensions()[0], 0, image.GetDimensions()[1], 0, 0) ms.Update() poly = ms.GetOutput() from matplotlib import pyplot as plt import numpy as np xx = np.zeros(poly.GetNumberOfPoints()) yy = np.zeros(poly.GetNumberOfPoints()) for i in range(poly.GetNumberOfPoints()): x,y,z = poly.GetPoint(i)