def clear_points(self): """ Remove alll points """ self.pts = vtk.vtkPoints() self.ca = vtk.vtkCellArray() self.obj = vtk.vtkDoubleArray() self.obj.SetName("Obj") self.pd.SetPoints(self.pts) self.pd.SetVerts(self.ca) self.pd.GetPointData().SetScalars(self.obj)
def __init__(self, pos, size, color): # POINT self.points = all.vtkPoints() poly = all.vtkPolyData() poly.SetPoints(self.points) mapper = all.vtkPointGaussianMapper() mapper.SetInputData(poly) mapper.EmissiveOff() mapper.SetScaleFactor(0.0) self.actor = all.vtkActor() self.actor.SetMapper(mapper) self.actor.GetProperty().SetPointSize(size) self.actor.GetProperty().SetColor(color)
def __init__(self, color, width): self._color = color # LINE self._points = all.vtkPoints() self._lines = all.vtkCellArray() self._line = all.vtkLine() poly = all.vtkPolyData() poly.SetPoints(self._points) poly.SetLines(self._lines) mapper = all.vtkPolyDataMapper() mapper.SetInputData(poly) self.actor = all.vtkActor() self.actor.SetMapper(mapper) self.actor.GetProperty().SetLineWidth(width) self.actor.GetProperty().SetColor(color)
def __init__(self, size=3, opacity=1): # POINTS self._points = all.vtkPoints() self._poly = all.vtkPolyData() self._poly.SetPoints(self._points) mapper = all.vtkPointGaussianMapper() mapper.SetInputData(self._poly) mapper.EmissiveOff() mapper.SetScaleFactor(0.0) self.actor = all.vtkActor() self.actor.SetMapper(mapper) self.actor.GetProperty().SetPointSize(size) self.actor.GetProperty().SetOpacity(opacity) # COLOR self._color_array = all.vtkUnsignedCharArray() self._color_array.SetNumberOfComponents(3)
def numpy_to_vtp(position: np.array, array_dict: dict): vtk_position = numpy_support.numpy_to_vtk(position) points = vtk.vtkPoints() points.SetData(vtk_position) data_save = vtk.vtkPolyData() vertices = vtk.vtkPolyVertex() length = len(position) vertices.GetPointIds().SetNumberOfIds(length) for i in range(0, length): vertices.GetPointIds().SetId(i, i) vert_cell = vtk.vtkCellArray() vert_cell.InsertNextCell(vertices) data_save.SetVerts(vert_cell) data_save.SetPoints(points) pd = data_save.GetPointData() for k, v in array_dict.items(): vtk_array = numpy_support.numpy_to_vtk(v) vtk_array.SetName(k) pd.AddArray(vtk_array) return data_save
vtkUnstructuredGridReader, vtkDataSetMapper, vtkGlyph3D, vtkArrowSource, vtkPolyDataMapper, vtkSphereSource) import numpy as np from vis2_3 import UGVisualiser from util.window_renderer import WindowRenderer if __name__ == '__main__': # Read Files __vectors = np.loadtxt("util/waarden.txt") __coordinaten = np.loadtxt("util/coordinaten.txt") cells = nptovtk(__vectors) #Create Grid and Points __unstructured_grid = vtkUnstructuredGrid() __points = vtkPoints() # Set Data in points __points.SetData(nptovtk(__coordinaten)) #__points.SetVectors(nptovtk(__vectors)) # set Points in Grid __unstructured_grid.SetPoints(__points) print(__points) #add array __unstructured_grid.GetPointData().SetVectors(nptovtk(__vectors)) __actor = vtkActor() __reader = vtkUnstructuredGridReader() __mapper = vtkDataSetMapper()
from vtkmodules import all points = all.vtkPoints() poly = all.vtkPolyData() poly.SetPoints(points) mapper = all.vtkPointGaussianMapper() mapper.SetInputData(poly) mapper.EmissiveOff() mapper.SetScaleFactor(0.0) actor = all.vtkActor() actor.SetMapper(mapper) colors_array = all.vtkUnsignedCharArray() colors_array.SetNumberOfComponents(3) for i in range(1000): color = (i * 0.1, 0, 0) colors_array.InsertNextTuple3(*color) points.InsertNextPoint([0.1 * i, 0.1 * i, 0]) points.Modified() poly.GetPointData().SetScalars(colors_array) render = all.vtkRenderer() render.AddActor(actor) ren_win = all.vtkRenderWindow() ren_win.AddRenderer(render) ren_win.SetSize(500, 500) # colors_array.Reset() # for i in range(0, 1000): # colors_array.InsertNextTuple3(0, 255, 0) # colors_array.Modified()
def Poly2Label2D(poly, dim, origin, spacing): ''' Input: poly: numpy array (n*2), poly to convert dim: dimention of target label origin: origin of target label spacing: spacing of target label Output: numpy array, result 2D label Description: Convert a 2D contour to 2D label ''' # 1 构建poly NumOfPoints = len(poly) Points = vtk.vtkPoints() polydata = vtk.vtkPolyData() Points.Reset() i = 0 while i < NumOfPoints: Points.InsertPoint(i, (poly[i][0], poly[i][1], 0)) i += 1 polydata.Modified() lines = vtk.vtkCellArray() lines.InsertNextCell(NumOfPoints+1) i = 0 while i < NumOfPoints: lines.InsertCellPoint(i) i += 1 lines.InsertCellPoint(0) polydata.SetPoints(Points) polydata.SetLines(lines) polydata.Modified() # 2 建立空白图像 Image = vtk.vtkImageData() Image.SetDimensions(dim[0], dim[1], 1) Image.SetOrigin(origin[0], origin[1], 0) Image.SetSpacing(spacing[0], spacing[1], 0) Image.AllocateScalars(vtk.VTK_INT, 1) count = Image.GetNumberOfPoints() # Fill every entry of the image data with "0" for i in range(count): Image.GetPointData().GetScalars().SetTuple1(i, 0) # 3 LinearExtrusionFilter extrudeFilter = vtk.vtkLinearExtrusionFilter() extrudeFilter.SetInputData(polydata) extrudeFilter.SetScaleFactor(1) extrudeFilter.SetExtrusionTypeToNormalExtrusion() extrudeFilter.SetVector( 0, 0, 1) # 4 poly沿z轴平移-0.5 m1 = vtk.vtkMatrix4x4() m1.SetElement(2, 3, -0.5) linearTransform = vtk.vtkMatrixToLinearTransform() linearTransform.GetMatrix().DeepCopy(m1) transformPolyData = vtk.vtkTransformPolyDataFilter() transformPolyData.SetInputConnection(extrudeFilter.GetOutputPort()) transformPolyData.SetTransform(linearTransform) # 5 转换 dataToStencil = vtk.vtkPolyDataToImageStencil() dataToStencil.SetInputConnection(transformPolyData.GetOutputPort()) dataToStencil.SetOutputSpacing(spacing[0], spacing[1], 0) dataToStencil.SetOutputOrigin(origin[0], origin[1], 0) stencil = vtk.vtkImageStencil() stencil.SetStencilConnection(dataToStencil.GetOutputPort()) stencil.SetInputData(Image) stencil.ReverseStencilOn() stencil.SetBackgroundValue(1) stencil.Update() # 6 vtk 2 numpy dataObj = stencil.GetOutput() vtkArray = dataObj.GetPointData().GetArray(0) numpyarray = numpy_support.vtk_to_numpy(vtkArray) Output = numpy.reshape(numpyarray, dim, order='F') return Output