C# vtkimagerslice三维旋转问题

C# vtkimagerslice三维旋转问题,c#,3d,vtk,C#,3d,Vtk,我试图在旋转旋钮控件时使用VTK旋转3D对象。下面是我是如何实现它的(用C#)。但是,当我旋转控制旋钮时,屏幕上的3D对象消失了。我不知道发生了什么,但在添加vtkImageReslice之前,一切都很好,我想是罪魁祸首 我认为其中一些代码是相关的: private vtkAxesActor axes; private vtkCamera camera; private List<vtkImageChangeInformation> changeF

我试图在旋转旋钮控件时使用VTK旋转3D对象。下面是我是如何实现它的(用C#)。但是,当我旋转控制旋钮时,屏幕上的3D对象消失了。我不知道发生了什么,但在添加vtkImageReslice之前,一切都很好,我想是罪魁祸首

我认为其中一些代码是相关的:

private vtkAxesActor axes; 
        private vtkCamera camera; 
        private List<vtkImageChangeInformation> changeFilters; 

        //private vtkTIFFReader reader; 
        private vtkImageAppendComponents componentAdaptor; 
        private List<vtkStringArray> fileNameArrays; 
        private List<vtkImageFlip> flippers; 
        private vtkRenderWindowInteractor iren; 

        // these two arrays specify the color range for the components specified by the array indexes 
        private int[] lowerThreshold = { 0, 0, 0, 0 }; 
        private List<vtkTIFFReader> readers; 
        private vtkRenderer renderer; 
        private vtkRenderWindow renderWindow; 
        private RenderWindowControl renWindowControl; 
        private int[] upperThreshold = { MaxGrayScaleLevel, MaxGrayScaleLevel, MaxGrayScaleLevel, MaxGrayScaleLevel}; 
        private vtkVolumeProperty volProperty; 
        private vtkVolume volume; 
        private vtkFixedPointVolumeRayCastMapper volumeMapper; 
        private vtkOrientationMarkerWidget widget; 


        public void setDefaultColorMapping() 
        { 
            if (volume == null) 
            { 
                MessageBox.Show("No volume exist"); 
                return; 
            } 
               // vtkVolumeProperty volProperty = volume.GetProperty(); 

            vtkColorTransferFunction colorFunctionA = vtkColorTransferFunction.New(); 
            colorFunctionA.AddRGBSegment(0, 0, 0, 0, MaxGrayScaleLevel, 1, 0, 0); 
            volProperty.SetColor(0, colorFunctionA); 

            vtkColorTransferFunction colorFunctionB = vtkColorTransferFunction.New(); 
            colorFunctionB.AddRGBSegment(0, 0, 0, 0, MaxGrayScaleLevel, 0, 1, 0); 
            volProperty.SetColor(1, colorFunctionB); 

            vtkColorTransferFunction colorFunctionC = vtkColorTransferFunction.New(); 
            colorFunctionC.AddRGBSegment(0, 0, 0, 0, MaxGrayScaleLevel, 0, 0, 1); 
            volProperty.SetColor(2, colorFunctionC); 

            vtkColorTransferFunction colorFunctionD = vtkColorTransferFunction.New(); 
            colorFunctionD.AddRGBSegment(0, 0, 0, 0, MaxGrayScaleLevel, 0.5, 0.5, 0); 
            volProperty.SetColor(3, colorFunctionD); 

            //volProperty.SetInterpolationTypeToNearest(); 
            //renWindowControl.RenderWindow.Render(); 
        } 

        public void setToDefault() 
        { 
            if (null == renWindowControl.RenderWindow) 
                return; 

            volumeMapper.SetBlendModeToMaximumIntensity(); 
            volumeMapper.SetCropping(1); 
            volumeMapper.SetCroppingRegionFlagsToSubVolume(); 

            //setDefaultColorMapping(); 
            //setDefaultOpacityfunction(); 

            widget.SetOutlineColor(0.93, 0.57, 0.13); 
            widget.SetOrientationMarker(axes); 
            widget.SetInteractor(renWindowControl.RenderWindow.GetInteractor()); 
            widget.SetEnabled(1); 

            double[] rotX = { 1.0, 0.0, 0.0 }; 
            double[] rotY = { 0.0, 1.0, 0.0 }; 
            double[] rotZ = { 0.0, 0.0, 1.0 }; 
            double[] center = { 0.0, 0.0, 0.0 }; 

            _reslicer.SetResliceAxesDirectionCosines(DoubleArrayToIntPtr(rotX), DoubleArrayToIntPtr(rotY), DoubleArrayToIntPtr(rotZ)); 
            _reslicer.SetResliceAxesOrigin(DoubleArrayToIntPtr(center)); 
            _reslicer.SetInterpolationModeToLinear(); 
            _reslicer.SetOutputDimensionality(3); 

            iren = renWindowControl.RenderWindow.GetInteractor(); 
        } 

        private void SetupScene() 
        { 
            renderer = renWindowControl.RenderWindow.GetRenderers().GetFirstRenderer(); 
            renderer.RemoveAllViewProps(); 

            if (_isZStackDataExist == true) 
            { 
                // volumeMapper.SetInputConnection(componentAdaptor.GetOutputPort());         
                _reslicer.SetInputConnection(componentAdaptor.GetOutputPort());               

                volumeMapper.SetInputConnection(_reslicer.GetOutputPort()); 
                //volumeMapper.Update(); 

                volume.SetMapper(volumeMapper); 
                //volume.SetOrigin(DataExtentX / 2, DataExtentY / 2, DataExtentZ / 2); 

                renderer.AddVolume(volume); 
                renderer.ResetCamera(); 
                camera = renderer.GetActiveCamera(); 
            } 

            renderer.SetBackground(0, 0, 0); 
            //renderer.GetActiveCamera().Zoom(3); 
            //deleteAllVTKObjects(); 
        } 

        //--------------------------------------------------------------------------------------------------------------- 
        public void updateXRotation() 
        { 
            if (false == IsVolumeRendererReady) 
                return; 

            if (null != _reslicer) 
            { 
                double[] rotX = { 1.0, 0.0, 0.0};   
                double[] rotY = { 0.0, Math.Cos(DataXRotationDegrees), -Math.Sin(DataXRotationDegrees)};   
                double[] rotZ = { 0.0, Math.Sin(DataXRotationDegrees), Math.Cos(DataXRotationDegrees)}; 
                double[] center = { 0.0, 0.0, 0.0 }; 

                _reslicer.SetResliceAxesDirectionCosines(DoubleArrayToIntPtr(rotX), DoubleArrayToIntPtr(rotY), DoubleArrayToIntPtr(rotZ)); 
                _reslicer.SetResliceAxesOrigin(DoubleArrayToIntPtr(center)); 
                _reslicer.SetInterpolationModeToLinear(); 
                _reslicer.Update(); 
            } 

        } 

        static IntPtr DoubleArrayToIntPtr(double[] rotDoubleArr) 
        { 
            // Initialize unmanaged memory to hold the array. 
            int size = Marshal.SizeOf(rotDoubleArr[0]) * rotDoubleArr.Length; 

            IntPtr rotIntPtr = Marshal.AllocHGlobal(size); 

            try 
            { 
                // Copy the array to unmanaged memory. 
                Marshal.Copy(rotDoubleArr, 0, rotIntPtr, rotDoubleArr.Length); 
            } 
            catch 
            { 
            } 

            return rotIntPtr; 

            //finally 
            //{ 
            //    // Free the unmanaged memory. 
            //    Marshal.FreeHGlobal(rotIntPtr); 
            //} 
        } 
专用vtkAxesActor轴;
私人vtkCamera摄像机;
私有列表过滤器;
//私人vtktiff阅读器;
专用vtkimageappendmentcomponents组件适配器;
私有列表文件名数组;
私有列表翻转器;
私人vtkRenderWindowInteractor iren;
//这两个数组为数组索引指定的组件指定颜色范围
private int[]lowerThreshold={0,0,0,0};
私人列表阅读器;
私人vtkrender渲染器;
私人vtcrenderwindowrenderwindow;
私有RenderWindowControl;
private int[]upperThreshold={MaxGrayScaleLevel,MaxGrayScaleLevel,MaxGrayScaleLevel,MaxGrayScaleLevel};
私有vtkVolumeProperty和volProperty;
私有vtkVolume卷;
私有vtkFixedPointVolumeRayCastMapper volumeMapper;
私有vtKorientiationMarkerWidget小部件;
public void setDefaultColorMapping()
{ 
如果(卷==null)
{ 
MessageBox.Show(“不存在卷”);
返回;
} 
//vtkVolumeProperty volProperty=volume.GetProperty();
vtkColorTransferFunction colorFunction=vtkColorTransferFunction.New();
ColorFunction.AddRGB段(0,0,0,0,MaxGrayScaleLevel,1,0,0);
SetColor(0,colorFunction);
vtkColorTransferFunction colorFunctionB=vtkColorTransferFunction.New();
颜色函数B.AddRGB段(0,0,0,0,MaxGrayScaleLevel,0,1,0);
SetColor(1,colorFunctionB);
vtkColorTransferFunction colorFunctionC=vtkColorTransferFunction.New();
颜色函数c.addRGB段(0,0,0,0,MaxGrayScaleLevel,0,0,1);
SetColor(2,colorFunctionC);
vtkColorTransferFunction colorFunctionD=vtkColorTransferFunction.New();
添加RGB段(0,0,0,0,MaxGrayScaleLevel,0.5,0.5,0);
SetColor(3,colorFunctionD);
//SetInterpolationTypeToNearest();
//renWindowControl.RenderWindow.Render();
} 
public void setToDefault()
{ 
if(null==renWindowControl.RenderWindow)
返回;
volumeMapper.SetblendMode到最大亮度();
体积上限设定裁剪(1);
volumeMapper.SetCroppingRegionFlagsToSubVolume();
//setDefaultColorMapping();
//setDefaultOpacityfunction();
SetOutlineColor(0.93,0.57,0.13);
setOrientionMarker(轴);
SetInteractor(renWindowControl.RenderWindow.GetInteractor());
widget.SetEnabled(1);
double[]rotX={1.0,0.0,0.0};
double[]rotY={0.0,1.0,0.0};
double[]rotZ={0.0,0.0,1.0};
双[]中心={0.0,0.0,0.0};
_reslicer.setresliceasdirectioncosines(DoubleArrayToIntPtr(rotX)、DoubleArrayToIntPtr(rotY)、DoubleArrayToIntPtr(rotZ));
_reslicer.SetResliceAxesOrigin(doubleArrayIntptr(中));
_reslicer.SetInterpolationModeToLinear();
_reslicer.setOutputDimensional(3);
iren=renWindowControl.RenderWindow.GetInteractor();
} 
私有场景()
{ 
renderer=renWindowControl.RenderWindow.GetRenderers().GetFirstRenderer();
renderer.RemoveAllViewProps();
如果(_isZStackDataExist==true)
{ 
//volumeMapper.SetInputConnection(ComponentAdapter.GetOutputPort());
_reslicer.SetInputConnection(componentAdapter.GetOutputPort());
volumeMapper.SetInputConnection(_reslicer.GetOutputPort());
//volumeMapper.Update();
volume.SetMapper(volumeMapper);
//卷.SetOrigin(DataExtentX/2、DataExtentY/2、DataExtentZ/2);
渲染器.AddVolume(volume);
ResetCamera();
camera=renderer.GetActiveCamera();
} 
设置背景(0,0,0);
//renderer.GetActiveCamera().Zoom(3);
//删除所有vtkobjects();
} 
//--------------------------------------------------------------------------------------------------------------- 
公共void updateXRotation()
{ 
if(false==isVolumerenderReady)
返回;
如果(空!=\u重新许可)
{ 
double[]rotX={1.0,0.0,0.0};
double[]rotY={0.0,Math.Cos(DataXRotationDegrees),-Math.Sin(DataXRotationDegrees)};
double[]rotZ={0.0,Math.Sin(DataXRotationDegrees),Math.Cos(DataXRotationDegrees)};
双[]中心={0.0,0.0,0.0};
_reslicer.setresliceasdirectioncosines(DoubleArrayToIntPtr(rotX)、DoubleArrayToIntPtr(rotY)、DoubleArrayToIntPtr(rotZ));
_reslicer.SetResliceAxesOrigin(doubleArrayIntptr(中));
_reslicer.SetInterpolationModeToLinear();
_reslicer.Update();
} 
} 
静态IntPtr双阵列IntPtr(双[]旋转双阵列)
{ 
//初始化非托管内存以保存阵列。
int size=Marshal.SizeOf(rotDoubleArr[0])
public void RotateToRight()
{           
       double cos = Math.Cos(angle);
       double sin = Math.Sin(angle);

       double[] rot = { cos, 0, -sin, 0,
                          0, 1,    0, 0,
                        sin, 0,  cos, 0,
                          0, 0,    0, 1};

       transform.SetMatrix(DoubleArrayToIntPtr(rot));
       camera.ApplyTransform(transform);
       renderer.ResetCamera();
       renderer.GetActiveCamera();
       ForceWindowToRender();
}
private void rotate(double angle, int axis) {
    vtkTransform t = new vtkTransform();
    bw.GetTransform(t);

    switch(axis) {
        case X:
            t.RotateX(-angle); 
            break;
        case Y:
            t.RotateY(-angle);
            break;
        case Z:
            t.RotateZ(-angle);
            break;
    }

    ren.GetActiveCamera().ApplyTransform(t);
}