def __init__(self): floor_mapper = vtk.vtkPolyDataMapper() floor_mapper.SetInputData(vtk.vtkPolyData()) self.floor_actor = vtk.vtkActor() self.floor_actor.SetMapper(floor_mapper) car_mapper = vtk.vtkPolyDataMapper() car_mapper.SetInputData(vtk.vtkPolyData()) self.car_actor = vtk.vtkActor() self.car_actor.SetMapper(car_mapper)
def __init__(self, max_num_pts=1e24): self.max_num_pts = max_num_pts self.pd = vtk.vtkPolyData() self.clear_points() mapper = vtk.vtkPolyDataMapper() self.actor = vtk.vtkActor() self.actor.SetMapper(mapper)
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 halo_writer(center, Rvir, outputname): haloData = vtk.vtkAppendPolyData() for i in range(len(center)): print(i, "/", len(center), end='\r') s = vtk.vtkSphereSource() s.SetCenter(*center[i]) s.SetRadius(Rvir[i]) s.Update() input1 = vtk.vtkPolyData() input1.ShallowCopy(s.GetOutput()) haloData.AddInputData(input1) haloData.Update() writer = vtk.vtkXMLPolyDataWriter() writer.SetInputConnection(haloData.GetOutputPort()) writer.SetFileName(outputname) writer.Write()
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
def __init__(self): stl_writer = vtk.vtkSTLWriter() append_poly = vtk.vtkAppendPolyData() reader1 = vtk.vtkSTLReader() reader1.SetFileName(file1) reader1.Update() reader2 = vtk.vtkSTLReader() reader2.SetFileName(file2) reader2.Update() mapper1 = vtk.vtkPolyDataMapper() mapper1.SetInputData(vtk.vtkPolyData()) mapper2 = vtk.vtkPolyDataMapper() mapper2.SetInputData(vtk.vtkPolyData()) actor1 = vtk.vtkActor() actor1.SetMapper(mapper1) actor2 = vtk.vtkActor() actor2.SetMapper(mapper2) actor1.GetMapper().SetInputData(reader1.GetOutput()) actor2.GetMapper().SetInputData(reader2.GetOutput()) h_center = actor2.GetCenter() c_center = actor1.GetCenter() c_h = actor1.GetBounds()[2] x = h_center[0] - c_center[0] y = 0.07 - c_h z = h_center[2] - c_center[2] actor1.SetPosition(x, y, z) # append_poly.AddInputData(reader1.GetOutput()) # append_poly.AddInputData(reader2.GetOutput()) reader1.FastDelete() reader2.FastDelete() render = vtk.vtkRenderer() ren_win = vtk.vtkRenderWindow() ren_win.AddRenderer(render) ren_win.SetSize(2000, 2000) i_ren = vtk.vtkRenderWindowInteractor() i_ren.SetInteractorStyle(vtk.vtkInteractorStyleMultiTouchCamera()) i_ren.SetRenderWindow(ren_win) render.AddActor(actor1) render.AddActor(actor2) ren_win.Render() actors = render.GetActors() actor_items = actors.GetNumberOfItems() actors.InitTraversal() for i in range(actor_items): actor = actors.GetNextActor() position = list(actor.GetPosition()) actor.SetPosition(position[0], position[1], position[2]) mapper = actor.GetMapper() poly = mapper.GetInput() if position != (0, 0, 0): # 如果发生平移则把每个点的坐标都做一下平移 points = poly.GetPoints() sum_pts = points.GetNumberOfPoints() for x in range(sum_pts): pos_point = list(points.GetPoint(x)) for y in range(3): pos_point[y] += position[y] points.SetPoint(x, pos_point) append_poly.AddInputData(poly) append_poly.Update() filter = vtk.vtkTriangleFilter() filter.SetInputConnection(append_poly.GetOutputPort()) stl_writer.SetFileName('STL/1.stl') stl_writer.SetInputConnection(filter.GetOutputPort()) stl_writer.Update() stl_writer.Write() i_ren.Start()
def __init__(self): append_poly = vtk.vtkAppendPolyData() reader1 = vtk.vtkSTLReader() reader1.SetFileName(file1) reader1.Update() reader2 = vtk.vtkSTLReader() reader2.SetFileName(file2) reader2.Update() mapper1 = vtk.vtkPolyDataMapper() mapper1.SetInputData(vtk.vtkPolyData()) mapper2 = vtk.vtkPolyDataMapper() mapper2.SetInputData(vtk.vtkPolyData()) actor1 = vtk.vtkActor() actor1.SetMapper(mapper1) actor2 = vtk.vtkActor() actor2.SetMapper(mapper2) actor1.GetMapper().SetInputData(reader1.GetOutput()) actor2.GetMapper().SetInputData(reader2.GetOutput()) h_center = actor2.GetCenter() c_center = actor1.GetCenter() c_h = actor1.GetBounds()[2] x = h_center[0] - c_center[0] y = 0.07 - c_h z = h_center[2] - c_center[2] actor1.SetPosition(x, y, z) # append_poly.AddInputData(reader1.GetOutput()) # append_poly.AddInputData(reader2.GetOutput()) # reader1.FastDelete() # reader2.FastDelete() render = vtk.vtkRenderer() ren_win = vtk.vtkRenderWindow() ren_win.AddRenderer(render) ren_win.SetSize(2000, 2000) i_ren = vtk.vtkRenderWindowInteractor() i_ren.SetInteractorStyle(vtk.vtkInteractorStyleMultiTouchCamera()) i_ren.SetRenderWindow(ren_win) actor_list = [actor1, actor2] for actor in actor_list: position = list(actor.GetPosition()) actor.SetPosition(position[0], position[1], position[2]) mapper = actor.GetMapper() poly = mapper.GetInput() if position != (0, 0, 0): points = poly.GetPoints() sum_pts = points.GetNumberOfPoints() for x in range(sum_pts): pos_point = list(points.GetPoint(x)) for y in range(3): pos_point[y] += position[y] points.SetPoint(x, pos_point) append_poly.AddInputData(poly) append_poly.Update() mapper = vtk.vtkPolyDataMapper() mapper.SetInputData(append_poly.GetOutput()) actor = vtk.vtkActor() actor.SetMapper(mapper) render.AddActor(actor) ren_win.Render() i_ren.Start()
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