Beispiel #1
0
        private void WritePNG()
        {
            // Path to vtk data must be set as an environment variable
            // VTK_DATA_ROOT = "C:\VTK\vtkdata-5.8.0"
            vtkTesting test     = vtkTesting.New();
            string     root     = test.GetDataRoot();
            string     filePath = System.IO.Path.Combine(root, @"Data\test_png.png");

            int[] extent = new int[] { 0, 99, 0, 99, 0, 0 };
            vtkImageCanvasSource2D imageSource = vtkImageCanvasSource2D.New();

            imageSource.SetExtent(extent[0], extent[1], extent[2], extent[3], extent[4], extent[5]);
            imageSource.SetScalarTypeToUnsignedChar();
            imageSource.SetNumberOfScalarComponents(3);
            imageSource.SetDrawColor(127, 45, 255);
            imageSource.FillBox(0, 99, 0, 99);
            imageSource.SetDrawColor(255, 255, 255);
            imageSource.FillBox(40, 70, 20, 50);
            imageSource.Update();

            vtkImageCast castFilter = vtkImageCast.New();

            castFilter.SetOutputScalarTypeToUnsignedChar();
            castFilter.SetInputConnection(imageSource.GetOutputPort());
            castFilter.Update();

            vtkPNGWriter writer = vtkPNGWriter.New();

            writer.SetFileName(filePath);
            writer.SetInputConnection(castFilter.GetOutputPort());
            writer.Write();
            // Read and display file for verification that it was written correctly
            vtkPNGReader reader = vtkPNGReader.New();

            if (reader.CanReadFile(filePath) == 0)
            {
                MessageBox.Show("Cannot read file \"" + filePath + "\"", "Error", MessageBoxButtons.OK);
                return;
            }
            reader.SetFileName(filePath);
            reader.Update();

            // Visualize
            vtkImageViewer2 imageViewer = vtkImageViewer2.New();

            imageViewer.SetInputConnection(reader.GetOutputPort());
            // get a reference to the renderwindow of our renderWindowControl1
            vtkRenderWindow renderWindow = renderWindowControl1.RenderWindow;
            // renderer
            vtkRenderer renderer = renderWindow.GetRenderers().GetFirstRenderer();

            // set background color
            renderer.SetBackground(0.2, 0.3, 0.4);
            imageViewer.SetRenderer(renderer);
            renderer.ResetCamera();
        }
        //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();
        }