Ejemplo n.º 1
0
        private void RenderVTI()
        {
            // reader
            // Read all the data from the file
            vtkXMLImageDataReader reader = vtkXMLImageDataReader.New();

            if (reader.CanReadFile(m_FileName) == 0)
            {
                MessageBox.Show("Cannot read file \"" + m_FileName + "\"", "Error", MessageBoxButtons.OK);
                return;
            }
            vtkVolume vol = vtkVolume.New();
            vtkColorTransferFunction ctf  = vtkColorTransferFunction.New();
            vtkPiecewiseFunction     spwf = vtkPiecewiseFunction.New();
            vtkPiecewiseFunction     gpwf = vtkPiecewiseFunction.New();

            reader.SetFileName(m_FileName);
            reader.Update(); // here we read the file actually

            // mapper
            vtkFixedPointVolumeRayCastMapper mapper = vtkFixedPointVolumeRayCastMapper.New();

            mapper.SetInputConnection(reader.GetOutputPort());

            // actor
            vtkActor actor = vtkActor.New();

            //actor.SetMapper(mapper);
            actor.GetProperty().SetRepresentationToWireframe();

            // add our actor to the renderer
            //Set the color curve for the volume
            ctf.AddHSVPoint(0, .67, .07, 1);
            ctf.AddHSVPoint(94, .67, .07, 1);
            ctf.AddHSVPoint(139, 0, 0, 0);
            ctf.AddHSVPoint(160, .28, .047, 1);
            ctf.AddHSVPoint(254, .38, .013, 1);

            //Set the opacity curve for the volume
            spwf.AddPoint(84, 0);
            spwf.AddPoint(151, .1);
            spwf.AddPoint(255, 1);

            //Set the gradient curve for the volume
            gpwf.AddPoint(0, .2);
            gpwf.AddPoint(10, .2);
            gpwf.AddPoint(25, 1);

            vol.GetProperty().SetColor(ctf);
            vol.GetProperty().SetScalarOpacity(spwf);
            vol.GetProperty().SetGradientOpacity(gpwf);

            vol.SetMapper(mapper);

            //Go through the Graphics Pipeline
            m_Renderer.AddVolume(vol);
            m_Renderer.ResetCamera();
            //renderer.AddActor(actor);
            RenderSlicer();
        }
Ejemplo n.º 2
0
        private void ReadImageData()
        {
            // 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\vase_1comp.vti");

            // reader
            // Read all the data from the file
            vtkXMLImageDataReader reader = vtkXMLImageDataReader.New();

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

            // mapper
            vtkDataSetMapper mapper = vtkDataSetMapper.New();

            mapper.SetInputConnection(reader.GetOutputPort());

            // actor
            vtkActor actor = vtkActor.New();

            actor.SetMapper(mapper);
            actor.GetProperty().SetRepresentationToWireframe();

            // 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);
            // add our actor to the renderer
            renderer.AddActor(actor);
        }
Ejemplo n.º 3
0
        private void WriteVTI()
        {
            // 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_vti.vti");
            vtkImageData imageData = vtkImageData.New();

            imageData.SetDimensions(3, 4, 5);
            imageData.SetNumberOfScalarComponents(1);
            imageData.SetScalarTypeToDouble();
            int[] dims = imageData.GetDimensions();

            // Fill every entry of the image data with "2.0"

            /* we can do this in unsafe mode which looks pretty similar to the c++ version
             * but then you must declare at the very top of your file the "preprocessor" directive
             *
             #define UNSAFE
             *
             * or whatever name you choose for the following preprocessor #if statement
             */
#if UNSAFE
            unsafe {
                for (int z = 0; z < dims[2]; z++)
                {
                    for (int y = 0; y < dims[1]; y++)
                    {
                        for (int x = 0; x < dims[0]; x++)
                        {
                            double *pixel = (double *)imageData.GetScalarPointer(x, y, z).ToPointer();
                            // c++ version:
                            // double* pixel = static_cast<double*>(imageData->GetScalarPointer(x,y,z));
                            pixel[0] = 2.0;
                        }
                    }
                }
            }
#else
            /* or we can do it in managed mode */
            int    size = imageData.GetScalarSize();
            IntPtr ptr  = Marshal.AllocHGlobal(size);

            for (int z = 0; z < dims[2]; z++)
            {
                for (int y = 0; y < dims[1]; y++)
                {
                    for (int x = 0; x < dims[0]; x++)
                    {
                        ptr = imageData.GetScalarPointer(x, y, z);
                        Marshal.Copy(new double[] { 2.0 }, 0, ptr, 1);
                    }
                }
            }
            Marshal.FreeHGlobal(ptr);
#endif

            vtkXMLImageDataWriter writer = vtkXMLImageDataWriter.New();
            writer.SetFileName(filePath);
            writer.SetInputConnection(imageData.GetProducerPort());
            writer.Write();

            // Read and display file for verification that it was written correctly
            vtkXMLImageDataReader reader = vtkXMLImageDataReader.New();
            if (reader.CanReadFile(filePath) == 0)
            {
                MessageBox.Show("Cannot read file \"" + filePath + "\"", "Error", MessageBoxButtons.OK);
                return;
            }
            reader.SetFileName(filePath);
            reader.Update();

            // Convert the image to a polydata
            vtkImageDataGeometryFilter imageDataGeometryFilter = vtkImageDataGeometryFilter.New();
            imageDataGeometryFilter.SetInputConnection(reader.GetOutputPort());
            imageDataGeometryFilter.Update();

            vtkDataSetMapper mapper = vtkDataSetMapper.New();
            mapper.SetInputConnection(imageDataGeometryFilter.GetOutputPort());
            // actor
            vtkActor actor = vtkActor.New();
            actor.SetMapper(mapper);
            actor.GetProperty().SetPointSize(4);
            // 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);
            // add our actor to the renderer
            renderer.AddActor(actor);
        }