def test_get_bounds(): size = (15, 15) test_bounds = [0.0, 15, 0.0, 15, 0.0, 0.0] points = Points() points.InsertNextPoint(0, 0, 0) points.InsertNextPoint(size[0], 0, 0) points.InsertNextPoint(size[0], size[1], 0) points.InsertNextPoint(0, size[1], 0) # Create the polygon polygon = Polygon() polygon.GetPointIds().SetNumberOfIds(4) # make a quad polygon.GetPointIds().SetId(0, 0) polygon.GetPointIds().SetId(1, 1) polygon.GetPointIds().SetId(2, 2) polygon.GetPointIds().SetId(3, 3) # Add the polygon to a list of polygons polygons = CellArray() polygons.InsertNextCell(polygon) # Create a PolyData polygonPolyData = PolyData() polygonPolyData.SetPoints(points) polygonPolyData.SetPolys(polygons) # Create a mapper and actor mapper = PolyDataMapper2D() mapper = set_input(mapper, polygonPolyData) actor = Actor2D() actor.SetMapper(mapper) compute_bounds(actor) npt.assert_equal(get_bounds(actor), test_bounds)
def generate_points(): centers = np.array([[1, 0, 0], [0, 1, 0], [0, 0, 1]]) colors = np.array([[1, 0, 0], [0, 1, 0], [0, 0, 1]]) * 255 vtk_vertices = Points() # Create the topology of the point (a vertex) vtk_faces = CellArray() # Add points for i in range(len(centers)): p = centers[i] id = vtk_vertices.InsertNextPoint(p) vtk_faces.InsertNextCell(1) vtk_faces.InsertCellPoint(id) # Create a polydata object polydata = PolyData() # Set the vertices and faces we created as the geometry and topology of the # polydata polydata.SetPoints(vtk_vertices) polydata.SetVerts(vtk_faces) set_polydata_colors(polydata, colors) mapper = PolyDataMapper() mapper.SetInputData(polydata) mapper.SetVBOShiftScaleMethod(False) point_actor = Actor() point_actor.SetMapper(mapper) return point_actor
def numpy_to_vtk_cells(data, is_coords=True): """Convert numpy array to a vtk cell array. Parameters ---------- data : ndarray points coordinate or connectivity array (e.g triangles). is_coords : ndarray Select the type of array. default: True. Returns ------- vtk_cell : vtkCellArray connectivity + offset information """ data = np.array(data, dtype=object) nb_cells = len(data) # Get lines_array in vtk input format connectivity = data.flatten() if not is_coords else [] offset = [0, ] current_position = 0 cell_array = CellArray() if VTK_9_PLUS: for i in range(nb_cells): current_len = len(data[i]) offset.append(offset[-1] + current_len) if is_coords: end_position = current_position + current_len connectivity += list(range(current_position, end_position)) current_position = end_position connectivity = np.array(connectivity, np.intp) offset = np.array(offset, dtype=connectivity.dtype) vtk_array_type = numpy_support.get_vtk_array_type(connectivity.dtype) cell_array.SetData( numpy_support.numpy_to_vtk(offset, deep=True, array_type=vtk_array_type), numpy_support.numpy_to_vtk(connectivity, deep=True, array_type=vtk_array_type)) else: for i in range(nb_cells): current_len = len(data[i]) end_position = current_position + current_len connectivity += [current_len] connectivity += list(range(current_position, end_position)) current_position = end_position connectivity = np.array(connectivity) cell_array.GetData().DeepCopy(numpy_support.numpy_to_vtk(connectivity)) cell_array.SetNumberOfCells(nb_cells) return cell_array
def _setup(self): """Setup this UI Component. Return an image as a 2D actor with a specific position. Returns ------- :class:`vtkTexturedActor2D` """ self.texture_polydata = PolyData() self.texture_points = Points() self.texture_points.SetNumberOfPoints(4) polys = CellArray() polys.InsertNextCell(4) polys.InsertCellPoint(0) polys.InsertCellPoint(1) polys.InsertCellPoint(2) polys.InsertCellPoint(3) self.texture_polydata.SetPolys(polys) tc = FloatArray() tc.SetNumberOfComponents(2) tc.SetNumberOfTuples(4) tc.InsertComponent(0, 0, 0.0) tc.InsertComponent(0, 1, 0.0) tc.InsertComponent(1, 0, 1.0) tc.InsertComponent(1, 1, 0.0) tc.InsertComponent(2, 0, 1.0) tc.InsertComponent(2, 1, 1.0) tc.InsertComponent(3, 0, 0.0) tc.InsertComponent(3, 1, 1.0) self.texture_polydata.GetPointData().SetTCoords(tc) texture_mapper = PolyDataMapper2D() texture_mapper = set_input(texture_mapper, self.texture_polydata) image = TexturedActor2D() image.SetMapper(texture_mapper) self.texture = Texture() image.SetTexture(self.texture) image_property = Property2D() image_property.SetOpacity(1.0) image.SetProperty(image_property) self.actor = image # Add default events listener to the VTK actor. self.handle_events(self.actor)
def _setup(self): """Set up this UI component. Creating the button actor used internally. """ # This is highly inspired by # https://github.com/Kitware/VTK/blob/c3ec2495b183e3327820e927af7f8f90d34c3474/Interaction/Widgets/vtkBalloonRepresentation.cxx#L47 self.texture_polydata = PolyData() self.texture_points = Points() self.texture_points.SetNumberOfPoints(4) polys = CellArray() polys.InsertNextCell(4) polys.InsertCellPoint(0) polys.InsertCellPoint(1) polys.InsertCellPoint(2) polys.InsertCellPoint(3) self.texture_polydata.SetPolys(polys) tc = FloatArray() tc.SetNumberOfComponents(2) tc.SetNumberOfTuples(4) tc.InsertComponent(0, 0, 0.0) tc.InsertComponent(0, 1, 0.0) tc.InsertComponent(1, 0, 1.0) tc.InsertComponent(1, 1, 0.0) tc.InsertComponent(2, 0, 1.0) tc.InsertComponent(2, 1, 1.0) tc.InsertComponent(3, 0, 0.0) tc.InsertComponent(3, 1, 1.0) self.texture_polydata.GetPointData().SetTCoords(tc) texture_mapper = PolyDataMapper2D() texture_mapper = set_input(texture_mapper, self.texture_polydata) button = TexturedActor2D() button.SetMapper(texture_mapper) self.texture = Texture() button.SetTexture(self.texture) button_property = Property2D() button_property.SetOpacity(1.0) button.SetProperty(button_property) self.actor = button # Add default events listener to the VTK actor. self.handle_events(self.actor)
def _points_to_vtk_cells(points, points_per_line=2): """ Returns the VTK cell array for the peaks given the set of points coordinates. Parameters ---------- points : (N, 3) array or ndarray points coordinates array. points_per_line : int (1 or 2), optional number of points per peak direction. Returns ------- cell_array : vtkCellArray connectivity + offset information. """ num_pnts = len(points) num_cells = num_pnts // points_per_line cell_array = CellArray() """ Connectivity is an array that contains the indices of the points that need to be connected in the visualization. The indices start from 0. """ connectivity = np.asarray(list(range(0, num_pnts)), dtype=int) """ Offset is an array that contains the indices of the first point of each line. The indices start from 0 and given the known geometry of this actor the creation of this array requires a 2 points padding between indices. """ offset = np.asarray(list(range(0, num_pnts + 1, points_per_line)), dtype=int) vtk_array_type = numpy_support.get_vtk_array_type(connectivity.dtype) cell_array.SetData( numpy_support.numpy_to_vtk(offset, deep=True, array_type=vtk_array_type), numpy_support.numpy_to_vtk(connectivity, deep=True, array_type=vtk_array_type)) cell_array.SetNumberOfCells(num_cells) return cell_array
def _setup(self): """Set up this UI component. Creating the polygon actor used internally. """ # Setup four points size = (1, 1) self._points = Points() self._points.InsertNextPoint(0, 0, 0) self._points.InsertNextPoint(size[0], 0, 0) self._points.InsertNextPoint(size[0], size[1], 0) self._points.InsertNextPoint(0, size[1], 0) # Create the polygon polygon = Polygon() polygon.GetPointIds().SetNumberOfIds(4) # make a quad polygon.GetPointIds().SetId(0, 0) polygon.GetPointIds().SetId(1, 1) polygon.GetPointIds().SetId(2, 2) polygon.GetPointIds().SetId(3, 3) # Add the polygon to a list of polygons polygons = CellArray() polygons.InsertNextCell(polygon) # Create a PolyData self._polygonPolyData = PolyData() self._polygonPolyData.SetPoints(self._points) self._polygonPolyData.SetPolys(polygons) # Create a mapper and actor mapper = PolyDataMapper2D() mapper = set_input(mapper, self._polygonPolyData) self.actor = Actor2D() self.actor.SetMapper(mapper) # Add default events listener to the VTK actor. self.handle_events(self.actor)
def set_polydata_triangles(polydata, triangles): """Set polydata triangles with a numpy array (ndarrays Nx3 int). Parameters ---------- polydata : vtkPolyData triangles : array (N, 3) triangles, represented as 2D ndarrays (Nx3) """ vtk_cells = CellArray() vtk_cells = numpy_to_vtk_cells(triangles, is_coords=False) polydata.SetPolys(vtk_cells) return polydata
def set_polydata_triangles(polydata, triangles): """Set polydata triangles with a numpy array (ndarrays Nx3 int). Parameters ---------- polydata : vtkPolyData triangles : array (N, 3) triangles, represented as 2D ndarrays (Nx3) """ vtk_cells = CellArray() if VTK_9_PLUS: vtk_cells = numpy_to_vtk_cells(triangles, is_coords=False) else: isize = IdTypeArray().GetDataTypeSize() req_dtype = np.int32 if isize == 4 else np.int64 all_triangles =\ np.insert(triangles, 0, 3, axis=1).astype(req_dtype).flatten() vtk_triangles = numpy_support.numpy_to_vtkIdTypeArray(all_triangles, deep=True) vtk_cells.SetCells(len(triangles), vtk_triangles) polydata.SetPolys(vtk_cells) return polydata
def _points_to_vtk_cells(points, points_per_line=2): """ Returns the VTK cell array for the peaks given the set of points coordinates. Parameters ---------- points : (N, 3) array or ndarray points coordinates array. points_per_line : int (1 or 2), optional number of points per peak direction. Returns ------- cell_array : vtkCellArray connectivity + offset information. """ num_pnts = len(points) num_cells = num_pnts // points_per_line cell_array = CellArray() if VTK_9_PLUS: """ Connectivity is an array that contains the indices of the points that need to be connected in the visualization. The indices start from 0. """ connectivity = np.asarray(list(range(0, num_pnts)), dtype=int) """ Offset is an array that contains the indices of the first point of each line. The indices start from 0 and given the known geometry of this actor the creation of this array requires a 2 points padding between indices. """ offset = np.asarray(list(range(0, num_pnts + 1, points_per_line)), dtype=int) vtk_array_type = numpy_support.get_vtk_array_type(connectivity.dtype) cell_array.SetData( numpy_support.numpy_to_vtk(offset, deep=True, array_type=vtk_array_type), numpy_support.numpy_to_vtk(connectivity, deep=True, array_type=vtk_array_type)) else: connectivity = np.array([], dtype=int) i_pos = 0 while i_pos < num_pnts: e_pos = i_pos + points_per_line """ In old versions of VTK (<9.0) the connectivity array should include the length of each line and immediately after the indices of the points in each line. """ connectivity = np.append(connectivity, [points_per_line, i_pos, e_pos - 1]) i_pos = e_pos cell_array.GetData().DeepCopy(numpy_support.numpy_to_vtk(connectivity)) cell_array.SetNumberOfCells(num_cells) return cell_array