Beispiel #1
0
        void CreateData(ref vtkImageData data)
        {
            data.SetExtent(-25, 25, -25, 25, 0, 0);
#if VTK_MAJOR_VERSION_5
            data.SetNumberOfScalarComponents(1);
            data.SetScalarTypeToDouble();
#else
            data.AllocateScalars(VTK_DOUBLE, 1);
#endif
            int[] extent = data.GetExtent();

            for (int y = extent[2]; y <= extent[3]; y++)
            {
                for (int x = extent[0]; x <= extent[1]; x++)
                {
                    IntPtr   ptr   = data.GetScalarPointer(x, y, 0);
                    double[] pixel = new double[] { Math.Sqrt(Math.Pow(x, 2.0) + Math.Pow(y, 2.0)) };
                    Marshal.Copy(pixel, 0, ptr, 1);
                }
            }

            vtkXMLImageDataWriter writer = vtkXMLImageDataWriter.New();
            writer.SetFileName(@"c:\vtk\vtkdata-5.8.0\Data\testIsoContours.vti");
#if VTK_MAJOR_VERSION_5
            writer.SetInputConnection(data.GetProducerPort());
#else
            writer.SetInputData(data);
#endif
            writer.Write();
        }
Beispiel #2
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);
        }