def copy(self, deep=True): """Return a shallow or a deep copy of the light. The only mutable attribute of ``Light`` objects is the transformation matrix (if it exists). Thus asking for a shallow copy merely implies that the returned light and the original share the transformation matrix instance. Parameters ---------- deep : bool Whether to return a deep copy rather than a shallow one. Default ``True``. Examples -------- Create a light and check that it shares a transformation matrix with its shallow copy. >>> import pyvista as pv >>> light = pv.Light() >>> light.transform_matrix = [[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, 0], [0, 0, 0, 1]] >>> shallow_copied = light.copy(deep=False) >>> shallow_copied == light True >>> shallow_copied.transform_matrix is light.transform_matrix True """ immutable_attrs = [ 'light_type', 'position', 'focal_point', 'ambient_color', 'diffuse_color', 'specular_color', 'intensity', 'on', 'positional', 'exponent', 'cone_angle', 'attenuation_values', 'shadow_attenuation', ] new_light = Light() for attr in immutable_attrs: value = getattr(self, attr) setattr(new_light, attr, value) if deep and self.transform_matrix is not None: new_light.transform_matrix = vtkMatrix4x4() new_light.transform_matrix.DeepCopy(self.transform_matrix) else: new_light.transform_matrix = self.transform_matrix # light actors are private, always copy, but copy visibility state as well new_light.actor.SetVisibility(self.actor.GetVisibility()) return new_light
def init_view(self, orientation="axial", center=None): if center is not None: self.center = center # can make this more efficient by just having one enabled # need to change a couple other functions that use self.axial (and equivalents) self.axial = vtkMatrix4x4() self.axial.DeepCopy((1, 0, 0, self.center[0], 0, 1, 0, self.center[1], 0, 0, 1, self.center[2], 0, 0, 0, 1)) self.coronal = vtkMatrix4x4() self.coronal.DeepCopy( (1, 0, 0, self.center[0], 0, 0, 1, self.center[1], 0, -1, 0, self.center[2], 0, 0, 0, 1)) self.sagittal = vtkMatrix4x4() self.sagittal.DeepCopy( (0, 0, -1, self.center[0], 1, 0, 0, self.center[1], 0, -1, 0, self.center[2], 0, 0, 0, 1)) self.oblique = vtkMatrix4x4() self.oblique.DeepCopy( (1, 0, 0, self.center[0], 0, 0.866025, -0.5, self.center[1], 0, 0.5, 0.866025, self.center[2], 0, 0, 0, 1)) if orientation == "axial": self.set_orientation(self.axial) self.orientation_index = 2 elif orientation == "coronal": self.set_orientation(self.coronal) self.orientation_index = 1 elif orientation == "sagittal": self.set_orientation(self.sagittal) self.orientation_index = 0 else: raise ("Orientation not properly selected.") self.viewer_initialized = True
def numpy_to_vtkMatrix4x4(affine): """ Convert a numpy 4x4 array to a vtk 4x4 matrix :param affine: 4x4 array :return: vtkMatrix4x4 object representing the affine """ # test for type and shape of affine matrix # assert isinstance(affine, np.ndarray) assert affine.shape == (4, 4) affine_vtk = vtkMatrix4x4() for row in range(0, 4): for col in range(0, 4): affine_vtk.SetElement(row, col, affine[row, col]) return affine_vtk
(x0, y0, z0) = reader.GetOutput().GetOrigin() dirMatrix = reader.GetOutput().GetDirectionMatrix() dirMatrix2 = np.array(dirMatrix) pointIn = np.array((1, 1, 1)) pointOut = np.array((0, 0, 0)) pointIn2 = np.array((10, 20, 30, 1)) pointOut2 = np.array((0, 0, 0, 1)) dirMatrix.MultiplyPoint(pointIn, pointOut) print(pointIn) print(pointOut) print(type(pointOut)) directionMatrix = vtkMatrix4x4() for j, spacing in enumerate((xSpacing, ySpacing, zSpacing)): for i in range(3): directionMatrix.SetElement(i, j, dirMatrix.GetElement(i, j) * spacing) directionMatrix.SetElement(3, 3, 1) for i, v in enumerate((1, 2, 3, 1)): #for i, v in enumerate((x0, y0, z0, 1)): directionMatrix.SetElement(i, 3, v) print(directionMatrix) directionMatrix.MultiplyPoint(pointIn2, pointOut2) print(pointIn2) print(pointOut2)
def _do_surface_creation(self, mask, mask_sFormMatrix=None): if mask_sFormMatrix is None: mask_sFormMatrix = vtkMatrix4x4() value = np.mean(mask.GetScalarRange()) # Use the mask to create isosurface mc = vtkContourFilter() mc.SetInputData(mask) mc.SetValue(0, value) mc.ComputeNormalsOn() mc.Update() # Mask isosurface refSurface = mc.GetOutput() # Create a uniformly meshed surface tmpPeel = downsample(refSurface) # Standard space coordinates # Apply coordinate transform to the meshed mask mask_ijk2xyz = vtkTransform() mask_ijk2xyz.SetMatrix(mask_sFormMatrix) mask_ijk2xyz_filter = vtkTransformPolyDataFilter() mask_ijk2xyz_filter.SetInputData(tmpPeel) mask_ijk2xyz_filter.SetTransform(mask_ijk2xyz) mask_ijk2xyz_filter.Update() # Smooth the mesh tmpPeel = smooth(mask_ijk2xyz_filter.GetOutput()) # Configure calculation of normals tmpPeel = fixMesh(tmpPeel) # Remove duplicate points etc # tmpPeel = cleanMesh(tmpPeel) # Generate triangles tmpPeel = upsample(tmpPeel) tmpPeel = smooth(tmpPeel) tmpPeel = fixMesh(tmpPeel) tmpPeel = cleanMesh(tmpPeel) refImageSpace2_xyz_transform = vtkTransform() refImageSpace2_xyz_transform.SetMatrix(vtk_utils.numpy_to_vtkMatrix4x4(np.linalg.inv(self.affine))) self.refImageSpace2_xyz = vtkTransformPolyDataFilter() self.refImageSpace2_xyz.SetTransform(refImageSpace2_xyz_transform) xyz2_refImageSpace_transform = vtkTransform() xyz2_refImageSpace_transform.SetMatrix(vtk_utils.numpy_to_vtkMatrix4x4(self.affine)) self.xyz2_refImageSpace = vtkTransformPolyDataFilter() self.xyz2_refImageSpace.SetTransform(xyz2_refImageSpace_transform) currentPeel = tmpPeel self.currentPeelNo = 0 currentPeel= self.MapImageOnCurrentPeel(currentPeel) newPeel = vtkPolyData() newPeel.DeepCopy(currentPeel) newPeel.DeepCopy(currentPeel) self.peel_normals = vtkFloatArray() self.peel_centers = vtkFloatArray() self.peel.append(newPeel) self.currentPeelActor = vtkActor() if not np.all(np.equal(self.affine, np.eye(4))): affine_vtk = self.CreateTransformedVTKAffine() self.currentPeelActor.SetUserMatrix(affine_vtk) self.GetCurrentPeelActor(currentPeel) self.peelActors.append(self.currentPeelActor) # locator will later find the triangle on the peel surface where the coil's normal intersect self.locator = vtkCellLocator() self.PeelDown(currentPeel)
def __init__(self): VTK_DATA_ROOT = vtkGetDataRoot() self.reader = vtkDICOMImageReader() self.folder = "/Users/nandana/Downloads/image_ex" self.reader.SetDirectoryName(self.folder) self.reader.SetFilePrefix(VTK_DATA_ROOT + "/Data/headsq/quarter") self.reader.SetDataExtent(0, 63, 0, 63, 1, 93) self.reader.SetDataSpacing(3.2, 3.2, 1.5) self.reader.SetDataOrigin(0.0, 0.0, 0.0) self.reader.SetDataScalarTypeToUnsignedShort() self.reader.UpdateWholeExtent() self.reader.Update() self.center = self.calculate_center() self.axial = vtkMatrix4x4() self.axial.DeepCopy((1, 0, 0, self.center[0], 0, 1, 0, self.center[1], 0, 0, 1, self.center[2], 0, 0, 0, 1)) self.coronal = vtkMatrix4x4() self.coronal.DeepCopy( (1, 0, 0, self.center[0], 0, 0, 1, self.center[1], 0, -1, 0, self.center[2], 0, 0, 0, 1)) self.sagittal = vtkMatrix4x4() self.sagittal.DeepCopy( (0, 0, -1, self.center[0], 1, 0, 0, self.center[1], 0, -1, 0, self.center[2], 0, 0, 0, 1)) self.oblique = vtkMatrix4x4() self.oblique.DeepCopy( (1, 0, 0, self.center[0], 0, 0.866025, -0.5, self.center[1], 0, 0.5, 0.866025, self.center[2], 0, 0, 0, 1)) self.img_reslice = vtkImageReslice() self.roi_reslice = vtkImageReslice() self.img_reslice.SetInputConnection(0, self.reader.GetOutputPort()) self.img_reslice.SetOutputDimensionality(2) self.img_reslice.SetInterpolationModeToLinear() self.roi_reslice.SetInputConnection(0, self.reader.GetOutputPort()) self.roi_reslice.SetOutputDimensionality(2) self.roi_reslice.SetInterpolationModeToLinear() self.set_orientation(self.axial) self.img_table = vtkLookupTable() self.roi_table = vtkLookupTable() self.window_level = vtkImageMapToWindowLevelColors() self.img_color = vtkImageMapToColors() self.roi_color = vtkImageMapToColors() self.img = self.map_img() self.roi = self.map_roi() self.px_coord_text_prop = vtkTextProperty() self.px_coord_text_mapper = vtkTextMapper() self.px_coord_text_actor = vtkActor2D() self.world_coord_text_prop = vtkTextProperty() self.world_coord_text_mapper = vtkTextMapper() self.world_coord_text_actor = vtkActor2D() self.usage_text_prop = vtkTextProperty() self.usage_text_mapper = vtkTextMapper() self.usage_text_actor = vtkActor2D() self.renderer = vtkRenderer() self.add_text() self.renderer.AddActor(self.img) self.renderer.AddActor(self.roi) self.renderer.SetBackground(0.2, 0.3, 0.4) self.window = vtkRenderWindow() self.window.AddRenderer(self.renderer) self.window.SetSize(1000, 1000) self.interactor_style = vtkInteractorStyleImage() self.interactor_style.SetInteractionModeToImageSlicing() self.interactor = vtkRenderWindowInteractor() self.interactor.SetInteractorStyle(self.interactor_style) self.window.SetInteractor(self.interactor) self.window_level.SetWindow(1000) self.window_level.SetLevel(200) self.window_level.Update() self.window.Render() self.interactor_style.AddObserver("MouseWheelForwardEvent", self.scroll_forward_callback) self.interactor_style.AddObserver("MouseWheelBackwardEvent", self.scroll_backward_callback) self.interactor_style.AddObserver("MouseMoveEvent", self.mouse_move_callback) self.interactor_style.AddObserver("KeyPressEvent", self.key_press_callback) self.interactor_style.AddObserver("LeftButtonPressEvent", self.left_press_callback) self.window.AddObserver("ModifiedEvent", self.window_mod_callback) self.actions = { "Slicing": 0, "Cursor": 0, "CurrentPos": -1, "LastPos": -1, "DoubleClick": 0 }