Example #1
0
    def set_matrix(self, registration_mat):
        print "VTKSurface.set_matrix(", registration_mat, ")!!"
        def vtkmatrix4x4_to_array(vtkmat):
            scipy_array = zeros((4,4), 'd')
            for i in range(0,4):
                for j in range(0,4):
                    scipy_array[i][j] = mat.GetElement(i,j)
            return scipy_array 

        def array_to_vtkmatrix4x4(scipy_array):
            mat = vtk.vtkMatrix4x4()
            for i in range(0,4):
                for j in range(0,4):
                    mat.SetElement(i,j, scipy_array[i][j])
            return mat

        #print "calling SetUserMatrix(", array_to_vtkmatrix4x4(registration_mat) , ")"
        mat = array_to_vtkmatrix4x4(registration_mat)
        mat.Modified()

        mat2xform = vtk.vtkMatrixToLinearTransform()
        mat2xform.SetInput(mat)
        
        print "calling SetUserTransform(", mat2xform, ")"
        self.SetUserTransform(mat2xform) # see vtk Prop3d docs
        self.Modified()
        # how do we like update the render tree or somethin..
        self.renderer.Render()
Example #2
0
    def Execute(self):

        if (self.Mesh == None):
            self.PrintError('Error: no Mesh.')

        if not self.Matrix4x4:
            self.Matrix4x4 = vtk.vtkMatrix4x4()
            if self.MatrixCoefficients != []:
                self.PrintLog('Setting up transform matrix using specified coefficients')
                self.Matrix4x4.DeepCopy(self.MatrixCoefficients)
            elif self.Translation != [0.0,0.0,0.0] or self.Rotation != [0.0,0.0,0.0] or self.Scaling != [1.0,1.0,1.0]:
                self.PrintLog('Setting up transform matrix using specified translation, rotation and/or scaling')
                transform = vtk.vtkTransform()
                transform.RotateX(self.Rotation[0])
                transform.RotateY(self.Rotation[1])
                transform.RotateZ(self.Rotation[2])                       
                transform.Translate(self.Translation[0], self.Translation[1], self.Translation[2])
                transform.Scale(self.Scaling[0], self.Scaling[1], self.Scaling[2])
                self.Matrix4x4.DeepCopy(transform.GetMatrix())

        if self.InvertMatrix:
            self.Matrix4x4.Invert()

        transform = vtk.vtkMatrixToLinearTransform()
        transform.SetInput(self.Matrix4x4)

        transformFilter = vtk.vtkTransformFilter()
        transformFilter.SetInputData(self.Mesh)
        transformFilter.SetTransform(transform)
        transformFilter.Update()

        self.Mesh = transformFilter.GetOutput()
 def __init__(self):
     '''
     Constructor
     '''
     
     self.__OrientationMatrix = vtk.vtkMatrix4x4()
     self.__CornerAnnotation = vtk.vtkCornerAnnotation()
     self.__TextProperty = vtk.vtkTextProperty()
     self.__LookupTable = vtk.vtkLookupTable()
     self.__ScalarBarActor = vtk.vtkScalarBarActor()
     self.__Prop3DCollection = vtk.vtkProp3DCollection()
     self.__DataSetCollection = vtk.vtkDataSetCollection()
     self.__OrientationTransform = vtk.vtkMatrixToLinearTransform()
     
     self.__OrientationMatrix.Identity()
     self.__CornerAnnotation.SetNonlinearFontScaleFactor(0.30)
     self.__CornerAnnotation.SetText(0, "Jolly - (c) summit 2009 ref vtkINRIA3D")
     self.__CornerAnnotation.SetMaximumFontSize(46)
     
     self.__ScalarBarActor.SetLabelTextProperty(self.__TextProperty)
     
     self.__ScalarBarActor.GetLabelTextProperty().BoldOff()
     self.__ScalarBarActor.GetLabelTextProperty().ItalicOff()
     self.__ScalarBarActor.SetNumberOfLabels(3)
     self.__ScalarBarActor.SetWidth(0.1)
     self.__ScalarBarActor.SetHeight(0.5)
     self.__ScalarBarActor.SetPosition(0.9, 0.3)
     self.__LookupTable.SetTableRange(0, 1)
     self.__LookupTable.SetSaturationRange(0, 0)
     self.__LookupTable.SetHueRange(0, 0)
     self.__LookupTable.SetValueRange(0, 1)
     self.__LookupTable.Build()
     
     self.__ShowAnnotations = True
     self.__ShowScalarBar = True
     
     self.__OrientationTransform.SetInput(self.__OrientationMatrix)
     
     self.__WindowLevel = self.GetWindowLevel()
     self.__WindowLevel.SetLookupTable( self.__LookupTable )
     self.__ScalarBarActor.SetLookupTable(self.__LookupTable)
     
     self.__Renderer = self.GetRenderer()
     self.__Renderer.AddViewProp(self.__CornerAnnotation)
     self.__Renderer.AddViewProp(self.__ScalarBarActor)
     
     self.__ImageActor = self.GetImageActor()
     self.__RenderWindow = self.GetRenderWindow ()
     self.__InteractorStyle = self.GetInteractorStyle()
     self.__Interactor = None
     
     self.__CornerAnnotation.SetWindowLevel(self.__WindowLevel)
     self.__CornerAnnotation.SetImageActor(self.__ImageActor)
     self.__CornerAnnotation.ShowSliceAndImageOn()
     
     # Sometime we would want to set the default window/level value instead
     # of the ImageData's ScalarRange
     self.__RefWindow = None
     self.__RefLevel = None
Example #4
0
    def Update(self):
        if shared.debug: print "Loading ", self.__filename
        self.__nim=load(self.__filename)
        if shared.debug: print self.__nim
        self.__data=self.__nim.get_data().astype("f").swapaxes(0,2)
        #self.__vtkimport.SetDataExtent(0,self.__data.shape[2]-1,0,self.__data.shape[1]-1,0,self.__data.shape[0]-1)
        self.__vtkimport.SetWholeExtent(0,self.__data.shape[2]-1,0,self.__data.shape[1]-1,0,self.__data.shape[0]-1)
        self.__vtkimport.SetDataExtentToWholeExtent()
        voxdim = self.__nim.get_header()['pixdim'][:3].copy()
        #Export data as string
        self.__data_string = self.__data.tostring()
        if shared.debug: print voxdim
        self.__vtkimport.SetDataSpacing((1.,1.,1.))#to reverse: [::-1]
        self.__vtkimport.CopyImportVoidPointer(self.__data_string,len(self.__data_string))
        self.__vtkimport.UpdateWholeExtent()

        imgData1 = self.__vtkimport.GetOutput()
        imgData1.SetExtent(self.__vtkimport.GetDataExtent())
        imgData1.SetOrigin((0,0,0))
        imgData1.SetSpacing(1.,1.,1.)
        #print imgData1
        #print self._irs
        #self._irs.SetInputConnection(self.__vtkimport.GetOutputPort())
        self._irs.SetInput(imgData1)
        #print self._irs
        self._irs.SetInterpolationModeToCubic()
        #self._irs.AutoCropOutputOn()
        
        
        affine = array_to_vtkmatrix4x4(self.__nim.get_affine())
        if shared.debug: print self._irs.GetResliceAxesOrigin()
        self._irs.SetResliceAxes(affine)
        if shared.debug: print self._irs.GetResliceAxesOrigin()
        m2t = vtk.vtkMatrixToLinearTransform()
        m2t.SetInput(affine.Invert())
        self._irs.TransformInputSamplingOff()
        self._irs.SetResliceTransform(m2t.MakeTransform())
        #self._irs.SetResliceAxesOrigin((0.,0.,0.)) #self.__vtkimport.GetOutput().GetOrigin())
        #print self.__vtkimport.GetOutput().GetBounds()
        #print self._irs.GetOutput().GetBounds()

        if shared.debug: print voxdim, self._irs.GetOutputSpacing()
        self._irs.SetOutputSpacing(abs(voxdim))
        if shared.debug: print self._irs.GetOutputSpacing()
        #print self._irs.GetOutputOrigin()
        #self._irs.SetOutputOrigin((0,0,0))
        # print self._irs.GetOutputOrigin()

        #m2t_i.DeepCopy(m2t);
        #m2t_i = affine_i.Invert();
        #m2t_i.MultiplyPoint(); 
        #self._irs.SetOutputOrigin(self.__nim.get_affine()[:3,-1])
        
        #self._irs.SetOutputExtent(self.__vtkimport.GetDataExtent())
        self._irs.AutoCropOutputOn()

        self._irs.Update()
Example #5
0
    def set_matrix(self, registration_mat):
        print "VTKSurface.set_matrix(", registration_mat, ")!!"

        #print "calling SetUserMatrix(", array_to_vtkmatrix4x4(registration_mat) , ")"
        mat = array_to_vtkmatrix4x4(registration_mat)
        mat.Modified()

        mat2xform = vtk.vtkMatrixToLinearTransform()
        mat2xform.SetInput(mat)
        
        print "calling SetUserTransform(", mat2xform, ")"
        self.SetUserTransform(mat2xform) # see vtk Prop3d docs
        self.Modified()
        # how do we like update the render tree or somethin..
        self.renderer.Render()
Example #6
0
    def Execute(self):

        if len(self.Points) % 3 != 0:
            self.PrintError('Error: Points not made up of triplets.')
            return

        matrix = vtk.vtkMatrix4x4()
        matrix.DeepCopy(self.MatrixCoefficients)
        if self.InvertMatrix:
            matrix.Invert()

        transform = vtk.vtkMatrixToLinearTransform()
        transform.SetInput(matrix)

        outputPoints = []
        for i in range(len(self.Points)/3):
            point = [self.Points[3*i+0],self.Points[3*i+1],self.Points[3*i+2]]
            outputPoint = transform.TransformPoint(point)
            outputPoints.append(outputPoint)

        self.Points = outputPoints
Example #7
0
    def Execute(self):

        if self.Image == None:
            self.PrintError('Error: No input image.')

        if self.Cast:
            cast = vtk.vtkImageCast()
            cast.SetInputData(self.Image)
            cast.SetOutputScalarTypeToFloat()
            cast.Update()
            self.Image = cast.GetOutput()

        resliceFilter = vtk.vtkImageReslice()
        resliceFilter.SetInputData(self.Image)
        if self.ReferenceImage:
            resliceFilter.SetInformationInput(self.ReferenceImage)
        else:
            if self.OutputSpacing:
                resliceFilter.SetOutputSpacing(self.OutputSpacing)
            if self.OutputOrigin:
                resliceFilter.SetOutputOrigin(self.OutputOrigin)
            if self.OutputExtent:
                resliceFilter.SetOutputExtent(self.OutputExtent)
        if self.Interpolation == 'nearestneighbor':
            resliceFilter.SetInterpolationModeToNearestNeighbor()
        elif self.Interpolation == 'linear':
            resliceFilter.SetInterpolationModeToLinear()
        elif self.Interpolation == 'cubic':
            resliceFilter.SetInterpolationModeToCubic()
        else:
            self.PrintError('Error: unsupported interpolation mode')
        resliceFilter.SetBackgroundLevel(self.BackgroundLevel)

        if self.TransformInputSampling:
            resliceFilter.TransformInputSamplingOn()
        else:
            resliceFilter.TransformInputSamplingOff()
 
        if not self.Matrix4x4:
            if self.MatrixCoefficients != []:
                self.PrintLog('Setting up transform matrix using specified coefficients')
                self.Matrix4x4 = vtk.vtkMatrix4x4()
                self.Matrix4x4.DeepCopy(self.MatrixCoefficients)
            elif self.Translation != [0.0,0.0,0.0] or self.Rotation != [0.0,0.0,0.0] or self.Scaling != [1.0,1.0,1.0]:
                self.PrintLog('Setting up transform matrix using specified translation, rotation and/or scaling')
                transform = vtk.vtkTransform()
                transform.RotateX(self.Rotation[0])
                transform.RotateY(self.Rotation[1])
                transform.RotateZ(self.Rotation[2])                       
                transform.Translate(self.Translation[0], self.Translation[1], self.Translation[2])
                transform.Scale(self.Scaling[0], self.Scaling[1], self.Scaling[2])
                self.Matrix4x4 = vtk.vtkMatrix4x4()
                self.Matrix4x4.DeepCopy(transform.GetMatrix())

        if self.InvertMatrix and self.Matrix4x4:
            self.Matrix4x4.Invert()
       
        if self.Matrix4x4:
            transform = vtk.vtkMatrixToLinearTransform()
            transform.SetInput(self.Matrix4x4)
            resliceFilter.SetResliceTransform(transform)

        resliceFilter.Update()

        self.Image = resliceFilter.GetOutput()
p6.SetOrigin(0.5,0.5,0.508)
p6.SetPoint1(-0.5,0.5,0.508)
p6.SetPoint2(0.5,-0.5,0.508)
p6.SetXResolution(5)
p6.SetYResolution(5)
# append together
ap = vtk.vtkAppendPolyData()
ap.AddInputConnection(p1.GetOutputPort())
ap.AddInputConnection(p2.GetOutputPort())
ap.AddInputConnection(p3.GetOutputPort())
ap.AddInputConnection(p4.GetOutputPort())
ap.AddInputConnection(p5.GetOutputPort())
ap.AddInputConnection(p6.GetOutputPort())
#--------------------------
# linear transform matrix
t1 = vtk.vtkMatrixToLinearTransform()
m1 = vtk.vtkMatrix4x4()
t1.SetInput(m1)
m1.SetElement(0,0,1.127631)
m1.SetElement(0,1,0.205212)
m1.SetElement(0,2,-0.355438)
m1.SetElement(1,0,0.000000)
m1.SetElement(1,1,0.692820)
m1.SetElement(1,2,0.400000)
m1.SetElement(2,0,0.200000)
m1.SetElement(2,1,-0.469846)
m1.SetElement(2,2,0.813798)
f11 = vtk.vtkTransformPolyDataFilter()
f11.SetInputConnection(ap.GetOutputPort())
f11.SetTransform(t1)
m11 = vtk.vtkDataSetMapper()