private void RotateImage(RotationOperation operation)
        {
            vtkImageReslice reslice   = vtkImageReslice.New();
            vtkTransform    transform = vtkTransform.New();

            transform.PostMultiply();

            double[] center = { 75, 100, 0 };
            transform.Translate(-center[0], -center[1], -center[2]);
            if (operation == RotationOperation.Forward)
            {
                transform.RotateZ(90);
            }
            else if (operation == RotationOperation.Back)
            {
                transform.RotateZ(-90);
            }
            transform.Translate(+center[0], +center[1], +center[2]);

            transform.Update();
            reslice.SetInput(_viewer.GetInput());
            reslice.SetResliceTransform(transform);
            reslice.Update();

            _viewer.SetInput(reslice.GetOutput());

            UpdateViewer();
        }
        /// <summary>
        /// Update the 2D visualization when the plane moved.
        /// </summary>
        /// <param name="plane">PlaneWidget which changed the coordinates.</param>
        public void PlaneMoved(vtkImagePlaneWidget plane)
        {
            vtkImageReslice reslice   = vtkImageReslice.New();
            vtkTransform    transform = vtkTransform.New();

            transform.PostMultiply();

            //TODO wyznaczenie centrum okna
            double[] center = { 75, 100, 0 };
            transform.Translate(-center[0], -center[1], -center[2]);
            transform.RotateZ(_orientation);
            transform.Translate(+center[0], +center[1], +center[2]);

            transform.Update();
            reslice.SetInput(plane.GetResliceOutput());
            reslice.SetResliceTransform(transform);
            reslice.Update();
            //
            vtkImageData viewerInputData;

            viewerInputData = reslice.GetOutput();

            _lastData = viewerInputData;

            _viewer.SetInput(viewerInputData);
            //
            //_viewer.SetInput(reslice.GetOutput());
            UpdateViewer();
        }
예제 #3
0
        /// <summary>
        /// Rotates volume along given axis.
        /// </summary>
        /// <param name="volume"></param>
        /// <param name="angles"></param>
        public static void RotateData(ref vtkImageData volume, double[] angles)
        {
            // Rotation along image center
            double[] center = volume.GetExtent().Divide(2);
            // Dimensions of rotated image
            int[] outExtent = volume.GetExtent().Multiply(1.1).Round().ToInt32();

            // Rotation parameters
            var rotate = new vtkTransform();

            rotate.Translate(center[1], center[3], center[5]);
            rotate.RotateX(angles[0]);
            rotate.RotateY(angles[1]);
            rotate.RotateZ(angles[2]); // z angle should be 0
            rotate.Translate(-center[1], -center[3], -center[5]);

            // Perform rotation
            var slice = new vtkImageReslice();

            slice.SetInput(volume);
            slice.SetResliceTransform(rotate);
            slice.SetInterpolationModeToCubic();
            slice.SetOutputSpacing(volume.GetSpacing()[0], volume.GetSpacing()[1], volume.GetSpacing()[2]);
            slice.SetOutputOrigin(volume.GetOrigin()[0], volume.GetOrigin()[1], volume.GetOrigin()[2]);
            slice.SetOutputExtent(outExtent[0], outExtent[1], outExtent[2], outExtent[3], outExtent[4], outExtent[5]);
        }
        //TODO

        public void DrawingModeRepaint()
        {
            if (_lastData == null)
            {
                return;
            }

            vtkImageReslice reslice   = vtkImageReslice.New();
            vtkTransform    transform = vtkTransform.New();

            transform.PostMultiply();

            //TODO wyznaczenie centrum okna
            double[] center = { 75, 100, 0 };
            transform.Translate(-center[0], -center[1], -center[2]);
            transform.RotateZ(_orientation);
            transform.Translate(+center[0], +center[1], +center[2]);

            transform.Update();

            reslice.SetInput(_lastData);
            reslice.SetResliceTransform(transform);
            reslice.Update();

            vtkImageData viewerInputData;

            if (!DrawingEnabled)
            {
                viewerInputData = reslice.GetOutput();
            }
            else
            {
                var resliceOutput = reslice.GetOutput();
                var sizeArray     = resliceOutput.GetDimensions();

                vtkImageCanvasSource2D imageCanvas = vtkImageCanvasSource2D.New();
                imageCanvas.SetScalarTypeToUnsignedChar();
                imageCanvas.SetExtent(0, sizeArray[0], 0, sizeArray[1], 0, 0);
                imageCanvas.DrawImage(0, 0, resliceOutput);

                DrawingUtils.Draw(imageCanvas, DrawingInfo);

                imageCanvas.Update();
                viewerInputData = imageCanvas.GetOutput();
            }
            _viewer.SetInput(viewerInputData);

            UpdateViewer();
        }
예제 #5
0
        /// <summary>
        /// Rotates 3D vtk volume around x and y axes.
        /// </summary>
        /// <param name="input"></param>
        /// <param name="angle"></param>
        /// <param name="axis"></param>
        /// <param name="out_extent"></param>
        /// <returns></returns>
        public static vtkImageData rotate_sample(vtkImageData input, double angle, int axis, int out_extent = 0)
        {
            //get input data dimensions
            int[] dims = input.GetExtent();
            //Compute centers
            int[] centers = new int[] { (dims[1] + dims[0]) / 2, (dims[3] + dims[2]) / 2, (dims[5] + dims[4]) / 2 };

            //Set rotation axis
            int[] axes = new int[3];
            axes[axis] = 1;

            int[] new_dims    = new int[] { dims[0], dims[1], dims[2], dims[3], dims[4], dims[5] };
            int[] new_centers = new int[] { centers[0], centers[1], centers[2] };

            //Compute new sample dimensions
            if (axis == 0)
            {
                new_dims[3] = (int)(Math.Cos(Math.Abs(angle / 180) * Math.PI) * new_dims[3] + Math.Sin(Math.Abs(angle / 180) * Math.PI) * new_dims[5]);
                new_dims[5] = (int)(Math.Sin(Math.Abs(angle / 180) * Math.PI) * new_dims[3] + Math.Cos(Math.Abs(angle / 180) * Math.PI) * new_dims[5]);

                new_centers[1] = (Math.Abs(new_dims[3]) + Math.Abs(new_dims[2])) / 2;
                new_centers[2] = (Math.Abs(new_dims[5]) + Math.Abs(new_dims[4])) / 2;
            }
            if (axis == 1)
            {
                new_dims[1] = (int)(Math.Cos(Math.Abs(angle / 180) * Math.PI) * new_dims[1] + Math.Sin(Math.Abs(angle / 180) * Math.PI) * new_dims[5]);
                new_dims[5] = (int)(Math.Sin(Math.Abs(angle / 180) * Math.PI) * new_dims[1] + Math.Cos(Math.Abs(angle / 180) * Math.PI) * new_dims[5]);

                new_centers[0] = (Math.Abs(new_dims[0]) + Math.Abs(new_dims[1])) / 2;
                new_centers[2] = (Math.Abs(new_dims[5]) + Math.Abs(new_dims[4])) / 2;
            }


            //Image transformation
            vtkTransform transform = vtkTransform.New();

            transform.Translate(centers[0], centers[1], centers[2]);
            transform.RotateWXYZ(angle, axes[0], axes[1], axes[2]);
            if (out_extent == 0)
            {
                transform.Translate(-centers[0], -centers[1], -centers[2]);
            }
            else
            {
                transform.Translate(-new_centers[0], -new_centers[1], -new_centers[2]);
            }

            //Console.ReadKey();

            transform.Update();

            //Compute new data extent
            int[] diff = new int[] { new_dims[1] - dims[1], new_dims[3] - dims[3], new_dims[5] - dims[5] };
            new_dims[0] += diff[0] / 2; new_dims[1] -= diff[0] / 2;
            new_dims[2] += diff[1] / 2; new_dims[3] -= diff[1] / 2;
            new_dims[4] += diff[2] / 2; new_dims[5] -= diff[2] / 2;



            //Image reslicing
            vtkImageReslice rotater = vtkImageReslice.New();

            rotater.SetInput(input);
            rotater.SetInformationInput(input);
            rotater.SetResliceTransform(transform);
            rotater.SetInterpolationModeToCubic();
            //rotater.SetInterpolationModeToLinear();
            if (out_extent == 1)
            {
                rotater.SetOutputSpacing(input.GetSpacing()[0], input.GetSpacing()[1], input.GetSpacing()[2]);
                rotater.SetOutputOrigin(input.GetOrigin()[0], input.GetOrigin()[1], input.GetOrigin()[2]);
                rotater.SetOutputExtent(new_dims[0], new_dims[1], new_dims[2], new_dims[3], new_dims[4], new_dims[5]);
            }
            rotater.Update();

            vtkImageData output = vtkImageData.New();

            output.DeepCopy(rotater.GetOutput());

            rotater.Dispose();
            transform.Dispose();

            return(output);
        }