Exemplo n.º 1
0
        private void ReadMetaImage()
        {
            // 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\foot\foot.mha");

            vtkMetaImageReader reader = vtkMetaImageReader.New();

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

            actor.SetInput(reader.GetOutput());

            // Visualize
            vtkRenderWindow renderWindow = renderWindowControl1.RenderWindow;
            // renderer
            vtkRenderer renderer = renderWindow.GetRenderers().GetFirstRenderer();

            // set background color
            renderer.SetBackground(0.2, 0.3, 0.4);
            renderer.AddActor(actor);
        }
Exemplo n.º 2
0
        private void ReadSTL()
        {
            // 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\42400-IDGH.stl");
            vtkSTLReader reader   = vtkSTLReader.New();

            reader.SetFileName(filePath);
            reader.Update();
            vtkPolyDataMapper mapper = vtkPolyDataMapper.New();

            mapper.SetInputConnection(reader.GetOutputPort());

            vtkActor actor = vtkActor.New();

            actor.SetMapper(mapper);
            // 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);
        }
Exemplo n.º 3
0
        private void ReadPNM()
        {
            // 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\earth.ppm");
            //Read the image
            vtkPNMReader reader = vtkPNMReader.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);
        }
Exemplo n.º 4
0
        private void SimplePointsReader()
        {
            // 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\points.txt");

            vtkSimplePointsReader reader = vtkSimplePointsReader.New();

            reader.SetFileName(filePath);
            reader.Update();
            // Visualize
            vtkPolyDataMapper mapper = vtkPolyDataMapper.New();

            mapper.SetInputConnection(reader.GetOutputPort());
            vtkActor actor = vtkActor.New();

            actor.SetMapper(mapper);
            actor.GetProperty().SetPointSize(4);
            vtkRenderWindow renderWindow = renderWindowControl1.RenderWindow;
            vtkRenderer     renderer     = renderWindow.GetRenderers().GetFirstRenderer();

            renderer.SetBackground(0.2, 0.3, 0.4);
            renderer.AddActor(actor);
            //renderer.ResetCamera();
        }
Exemplo n.º 5
0
        private void VRML()
        {
            // 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\bot2.wrl");
            // reader
            vtkVRMLImporter importer = vtkVRMLImporter.New();

            importer.SetFileName(filePath);
            importer.Update();

            // get a reference to the renderwindow of our renderWindowControl1
            vtkRenderWindow renderWindow = renderWindowControl1.RenderWindow;

            renderWindow.AddRenderer(importer.GetRenderer());
            renderWindow.Render();

            //vtkActorCollection actors = importer.GetRenderer().GetActors();
            //actors.InitTraversal();
            //vtkActor tmp;
            //while(( tmp = actors.GetNextActor()) != null) {
            //}
        }
Exemplo n.º 6
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();
        }
Exemplo n.º 7
0
     private void ReadPLOT3D() {
         // 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 filePathXYZ = System.IO.Path.Combine(root, @"Data\bluntfinxyz.bin");
         string filePathQ = System.IO.Path.Combine(root, @"Data\bluntfinq.bin");
         //string filePathXYZ = System.IO.Path.Combine(root, @"Data\combxyz.bin");
         //string filePathQ = System.IO.Path.Combine(root, @"Data\combq.bin");

         vtkMultiBlockPLOT3DReader reader = vtkMultiBlockPLOT3DReader.New();
         reader.SetXYZFileName(filePathXYZ);
         reader.SetQFileName(filePathQ);

         // Specify the scalar function to extract. If ==(-1), then no scalar function is extracted. 
         int scalarFctNo = reader.GetScalarFunctionNumber();
         int vectorFctNo = reader.GetVectorFunctionNumber();
         if(scalarFctNo != -1)
            reader.SetScalarFunctionNumber(scalarFctNo);
         // Specify the vector function to extract. If ==(-1), then no vector function is extracted. 
         if(vectorFctNo != -1)
            reader.SetVectorFunctionNumber(vectorFctNo);
         reader.Update();

         //// geometry filter
         //// This filter is multi-block aware and will request blocks from the
         //// input. These blocks will be processed by simple processes as if they
         //// are the whole dataset
         //vtkCompositeDataGeometryFilter geom1 = vtkCompositeDataGeometryFilter.New();
         //geom1.SetInputConnection(0, reader.GetOutputPort(0));

         vtkStructuredGridGeometryFilter geometryFilter = vtkStructuredGridGeometryFilter.New();
         geometryFilter.SetInput(reader.GetOutput().GetBlock(0));
         //geometryFilter.SetInputConnection(geom1.GetOutputPort(0));
         geometryFilter.Update();

         // Visualize
         vtkPolyDataMapper mapper = vtkPolyDataMapper.New();
         mapper.SetInputConnection(geometryFilter.GetOutputPort());
         //mapper.SetInputConnection(geom1.GetOutputPort());
         mapper.ScalarVisibilityOn();
         vtkActor actor = vtkActor.New();
         actor.SetMapper(mapper);
         // 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);
      }
        private void ReadUnknownTypeXMLFile()
        {
            // 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\quadraticTetra01.vtu");
            //string filePath = System.IO.Path.Combine(root, @"Data\SyntheticPolyline.vtp");

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

            // caution: you cannot use CanReadFile with the generic reader
            //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

            // All of the standard data types can be checked and obtained like this:
            if (vtkPolyData.SafeDownCast(reader.GetOutput()) != null)
            {
                Debug.WriteLine("File is a polydata");
            }
            else if (vtkUnstructuredGrid.SafeDownCast(reader.GetOutput()) != null)
            {
                Debug.WriteLine("File is an unstructured grid");
            }

            // 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;
            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);
        }
Exemplo n.º 9
0
        private void WriteMetaImage()
        {
            // 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_mha.mhd");
            string     filePathRaw = System.IO.Path.Combine(root, @"Data\test_mha.raw");
            // Create an image
            vtkImageMandelbrotSource source = vtkImageMandelbrotSource.New();

            source.Update();

            vtkImageCast castFilter = vtkImageCast.New();

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

            vtkMetaImageWriter writer = vtkMetaImageWriter.New();

            writer.SetInputConnection(castFilter.GetOutputPort());
            writer.SetFileName(filePath);
            writer.SetRAWFileName(filePathRaw);
            writer.Write();

            // Read and display file for verification that it was written correctly
            vtkMetaImageReader reader = vtkMetaImageReader.New();

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

            actor.SetInput(reader.GetOutput());

            // Visualize
            vtkRenderWindow renderWindow = renderWindowControl1.RenderWindow;
            // renderer
            vtkRenderer renderer = renderWindow.GetRenderers().GetFirstRenderer();

            // set background color
            renderer.SetBackground(0.2, 0.3, 0.4);
            renderer.AddActor(actor);
        }
Exemplo n.º 10
0
        private void MatrixMathFilter()
        {
            // 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\tensors.vtk");
            vtkUnstructuredGridReader reader = vtkUnstructuredGridReader.New();

            reader.SetFileName(filePath);
            reader.Update();

            vtkDataSetSurfaceFilter surfaceFilter = vtkDataSetSurfaceFilter.New();

            surfaceFilter.SetInputConnection(reader.GetOutputPort());
            surfaceFilter.Update();

            vtkMatrixMathFilter matrixMathFilter = vtkMatrixMathFilter.New();

            //matrixMathFilter.SetOperationToDeterminant();
            matrixMathFilter.SetOperationToEigenvalue();
            matrixMathFilter.SetInputConnection(surfaceFilter.GetOutputPort());
            matrixMathFilter.Update();
            matrixMathFilter.GetOutput().GetPointData().SetActiveScalars("Eigenvalue");

            vtkXMLPolyDataWriter writer = vtkXMLPolyDataWriter.New();

            writer.SetInputConnection(matrixMathFilter.GetOutputPort());
            writer.SetFileName(System.IO.Path.Combine(root, @"Data\output.vtp"));
            writer.Write();

            vtkPolyDataMapper mapper = vtkPolyDataMapper.New();

            mapper.SetInputConnection(matrixMathFilter.GetOutputPort());

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

            actor.SetMapper(mapper);
            // 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);
        }
Exemplo n.º 11
0
        private void ReadStructuredGrid()
        {
            // 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\multicomb_0.vts");

            // reader
            vtkXMLStructuredGridReader reader = vtkXMLStructuredGridReader.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

            vtkStructuredGridGeometryFilter geometryFilter = vtkStructuredGridGeometryFilter.New();

            geometryFilter.SetInputConnection(reader.GetOutputPort());
            geometryFilter.Update();

            // Visualize
            vtkPolyDataMapper mapper = vtkPolyDataMapper.New();

            mapper.SetInputConnection(geometryFilter.GetOutputPort());

            //// mapper
            //vtkDataSetMapper mapper = vtkDataSetMapper.New();
            //mapper.SetInputConnection(reader.GetOutputPort());

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

            actor.SetMapper(mapper);
            // 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);
        }
Exemplo n.º 12
0
        private void Curvature()
        {
            // 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();
            // note: one should use a coarse surface mesh to achieve a good visual effect
            string filePath = System.IO.Path.Combine(root, @"Data\bunny.vtp");
            // Create a polydata
            vtkXMLPolyDataReader reader = vtkXMLPolyDataReader.New();

            reader.SetFileName(filePath);

            vtkCurvatures curvaturesFilter = vtkCurvatures.New();

            curvaturesFilter.SetInputConnection(reader.GetOutputPort());
            //curvaturesFilter.SetCurvatureTypeToGaussian();
            //curvaturesFilter.SetCurvatureTypeToMean();
            //curvaturesFilter.SetCurvatureTypeToMaximum();
            curvaturesFilter.SetCurvatureTypeToMinimum();

            // To inspect more closely, if required
            vtkXMLPolyDataWriter writer = vtkXMLPolyDataWriter.New();

            writer.SetInputConnection(curvaturesFilter.GetOutputPort());
            writer.SetFileName(System.IO.Path.Combine(root, @"Data\gauss.vtp"));
            writer.Write();

            // Create a mapper and actor
            vtkPolyDataMapper mapper = vtkPolyDataMapper.New();

            mapper.SetInputConnection(curvaturesFilter.GetOutputPort());
            double[] range = curvaturesFilter.GetOutput().GetScalarRange();
            mapper.SetScalarRange(range[0], range[1]);
            vtkActor actor = vtkActor.New();

            actor.SetMapper(mapper);
            // 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.0, 0.0, 0.0);
            // add our actor to the renderer
            renderer.AddActor(actor);
        }
Exemplo n.º 13
0
        private void ReadPolyData()
        {
            // 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\SyntheticPolyline.vtp");
            // string filePath = System.IO.Path.Combine(root, @"Data\uniform-001371-5x5x5.vtp");
            // string filePath = System.IO.Path.Combine(root, @"Data\political.vtp");
            // string filePath = System.IO.Path.Combine(root, @"Data\filledContours.vtp");
            // string filePath = System.IO.Path.Combine(root, @"Data\disk_out_ref_surface.vtp");
            string filePath = System.IO.Path.Combine(root, @"Data\cow.vtp");
            // reader
            // Read all the data from the file
            vtkXMLPolyDataReader reader = vtkXMLPolyDataReader.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);

            // 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);
        }
Exemplo n.º 14
0
        private void ReadDEM()
        {
            // 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\SainteHelens.dem");

            vtkDEMReader reader = vtkDEMReader.New();

            reader.SetFileName(filePath);
            reader.Update();

            vtkLookupTable lut = vtkLookupTable.New();

            lut.SetHueRange(0.6, 0);
            lut.SetSaturationRange(1.0, 0);
            lut.SetValueRange(0.5, 1.0);
            double[] range = reader.GetOutput().GetScalarRange();
            lut.SetTableRange(range[0], range[1]);

            // Visualize
            vtkImageMapToColors mapColors = vtkImageMapToColors.New();

            mapColors.SetLookupTable(lut);
            mapColors.SetInputConnection(reader.GetOutputPort());

            // Create an actor
            vtkImageActor actor = vtkImageActor.New();

            actor.SetInput(mapColors.GetOutput());
            // 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);
        }
Exemplo n.º 15
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);
        }
Exemplo n.º 16
0
        private void ParticleReader()
        {
            // 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\Particles.raw");

            // Read the file
            vtkParticleReader reader = vtkParticleReader.New();

            reader.SetFileName(filePath);
            reader.SetDataByteOrderToBigEndian();
            reader.Update();
            Debug.WriteLine("NumberOfPieces: " + reader.GetOutput().GetNumberOfPieces());

            // Visualize
            vtkPolyDataMapper mapper = vtkPolyDataMapper.New();

            mapper.SetInputConnection(reader.GetOutputPort());
            mapper.SetScalarRange(4, 9);
            mapper.SetPiece(1);

            vtkActor actor = vtkActor.New();

            actor.SetMapper(mapper);
            actor.GetProperty().SetPointSize(4);
            actor.GetProperty().SetColor(1, 0, 0);
            // 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);
        }
Exemplo n.º 17
0
        private void XGMLReader()
        {
            // 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\Infovis\fsm.gml");

            vtkXGMLReader reader = vtkXGMLReader.New();

            reader.SetFileName(filePath);
            reader.Update();

            vtkUndirectedGraph g = reader.GetOutput();

            vtkGraphLayoutView graphLayoutView = vtkGraphLayoutView.New();

            graphLayoutView.SetRenderWindow(renderWindowControl1.RenderWindow);
            graphLayoutView.AddRepresentationFromInput(g);
            graphLayoutView.SetLayoutStrategy("Simple 2D");
            graphLayoutView.ResetCamera();
            graphLayoutView.Render();
        }
Exemplo n.º 18
0
        private void ReadOBJ()
        {
            // 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();
            // caution: vtkdata-5.8.0\Data\mni-surface-mesh.obj is no valid obj file!
            //string filePath = System.IO.Path.Combine(root, @"Data\mni-surface-mesh.obj");
            string filePath = System.IO.Path.Combine(root, @"Data\ViewPoint\cow.obj");

            vtkOBJReader reader = vtkOBJReader.New();

            if (!File.Exists(filePath))
            {
                MessageBox.Show("Cannot read file \"" + filePath + "\"", "Error", MessageBoxButtons.OK);
                return;
            }
            reader.SetFileName(filePath);
            reader.Update();
            // Visualize
            vtkPolyDataMapper mapper = vtkPolyDataMapper.New();

            mapper.SetInputConnection(reader.GetOutputPort());
            vtkActor actor = vtkActor.New();

            actor.SetMapper(mapper);
            // 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.3, 0.6, 0.3);
            // add our actor to the renderer
            renderer.AddActor(actor);
            renderWindow.Render();
        }
Exemplo n.º 19
0
        private void WritePolyData()
        {
            // 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\poly_test.vtp");
            // Create 4 points for a tetrahedron
            vtkPoints points = vtkPoints.New();

            points.InsertNextPoint(0, 0, 0);
            points.InsertNextPoint(0, 0, 1);
            points.InsertNextPoint(1, 0, 0);
            points.InsertNextPoint(0, 1, 0);

            // Create a polydata object and add the points to it.
            vtkPolyData polydata = vtkPolyData.New();

            polydata.SetPoints(points);

            // it's not enough only to define points
            // we need to define faces too
            // (must be defined in counter clockwise order as viewed from the outside)
            vtkTriangle face0 = vtkTriangle.New();

            face0.GetPointIds().SetId(0, 0);
            face0.GetPointIds().SetId(1, 2);
            face0.GetPointIds().SetId(2, 1);
            vtkTriangle face1 = vtkTriangle.New();

            face1.GetPointIds().SetId(0, 0);
            face1.GetPointIds().SetId(1, 3);
            face1.GetPointIds().SetId(2, 2);
            vtkTriangle face2 = vtkTriangle.New();

            face2.GetPointIds().SetId(0, 0);
            face2.GetPointIds().SetId(1, 1);
            face2.GetPointIds().SetId(2, 3);
            vtkTriangle face3 = vtkTriangle.New();

            face3.GetPointIds().SetId(0, 1);
            face3.GetPointIds().SetId(1, 2);
            face3.GetPointIds().SetId(2, 3);

            vtkCellArray faces = vtkCellArray.New();

            faces.InsertNextCell(face0);
            faces.InsertNextCell(face1);
            faces.InsertNextCell(face2);
            faces.InsertNextCell(face3);

            polydata.SetPolys(faces);

            // Write the file
            vtkXMLPolyDataWriter writer = vtkXMLPolyDataWriter.New();

            writer.SetFileName(filePath);
            writer.SetInput(polydata);

            // Optional - set the mode. The default is binary.
            //writer.SetDataModeToBinary();
            writer.SetDataModeToAscii();
            writer.Write();

            // Read and display file for verification that it was written correctly
            vtkXMLPolyDataReader reader = vtkXMLPolyDataReader.New();

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

            vtkDataSetMapper mapper = vtkDataSetMapper.New();

            mapper.SetInputConnection(reader.GetOutputPort());

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

            actor.SetMapper(mapper);
            // 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);
        }
Exemplo n.º 20
0
        private void ReadPDB()
        {
            // 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\caffeine.pdb");

            vtkPDBReader pdb = vtkPDBReader.New();

            pdb.SetFileName(filePath);
            pdb.SetHBScale(1.0);
            pdb.SetBScale(1.0);
            pdb.Update();
            Debug.WriteLine("# of atoms is: " + pdb.GetNumberOfAtoms());
            // if molecule contains a lot of atoms, reduce the resolution of the sphere (represents an atom) for faster rendering
            int resolution = (int)Math.Floor(Math.Sqrt(300000.0 / pdb.GetNumberOfAtoms())); // 300000.0 is an empriric value

            if (resolution > 20)
            {
                resolution = 20;
            }
            else if (resolution < 4)
            {
                resolution = 4;
            }

            Debug.WriteLine("Resolution is: " + resolution);
            vtkSphereSource sphere = vtkSphereSource.New();

            sphere.SetCenter(0, 0, 0);
            sphere.SetRadius(1);
            sphere.SetThetaResolution(resolution);
            sphere.SetPhiResolution(resolution);

            vtkGlyph3D glyph = vtkGlyph3D.New();

            glyph.SetInputConnection(pdb.GetOutputPort());
            glyph.SetOrient(1);
            glyph.SetColorMode(1);
            // glyph.ScalingOn();
            glyph.SetScaleMode(2);
            glyph.SetScaleFactor(.25);
            glyph.SetSourceConnection(sphere.GetOutputPort());

            vtkPolyDataMapper atomMapper = vtkPolyDataMapper.New();

            atomMapper.SetInputConnection(glyph.GetOutputPort());
            atomMapper.UseLookupTableScalarRangeOff();
            atomMapper.ScalarVisibilityOn();
            atomMapper.SetScalarModeToDefault();

            vtkLODActor atom = vtkLODActor.New();

            atom.SetMapper(atomMapper);
            atom.GetProperty().SetRepresentationToSurface();
            atom.GetProperty().SetInterpolationToGouraud();
            atom.GetProperty().SetAmbient(0.15);
            atom.GetProperty().SetDiffuse(0.85);
            atom.GetProperty().SetSpecular(0.1);
            atom.GetProperty().SetSpecularPower(30);
            atom.GetProperty().SetSpecularColor(1, 1, 1);
            atom.SetNumberOfCloudPoints(30000);


            vtkTubeFilter tube = vtkTubeFilter.New();

            tube.SetInputConnection(pdb.GetOutputPort());
            tube.SetNumberOfSides(resolution);
            tube.CappingOff();
            tube.SetRadius(0.2);
            // turn off variation of tube radius with scalar values
            tube.SetVaryRadius(0);
            tube.SetRadiusFactor(10);

            vtkPolyDataMapper bondMapper = vtkPolyDataMapper.New();

            bondMapper.SetInputConnection(tube.GetOutputPort());
            bondMapper.UseLookupTableScalarRangeOff();
            bondMapper.ScalarVisibilityOff();
            bondMapper.SetScalarModeToDefault();

            vtkLODActor bond = vtkLODActor.New();

            bond.SetMapper(bondMapper);
            bond.GetProperty().SetRepresentationToSurface();
            bond.GetProperty().SetInterpolationToGouraud();
            bond.GetProperty().SetAmbient(0.15);
            bond.GetProperty().SetDiffuse(0.85);
            bond.GetProperty().SetSpecular(0.1);
            bond.GetProperty().SetSpecularPower(30);
            bond.GetProperty().SetSpecularColor(1, 1, 1);
            bond.GetProperty().SetDiffuseColor(1.0000, 0.8941, 0.70981);


            // 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(atom);
            renderer.AddActor(bond);
        }
Exemplo n.º 21
0
        private void MarchingSquares()
        {
            // 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();

            vtkVolume16Reader v16 = vtkVolume16Reader.New();

            v16.SetDataDimensions(64, 64);

            v16.GetOutput().SetOrigin(0.0, 0.0, 0.0);
            v16.SetDataByteOrderToLittleEndian();
            v16.SetFilePrefix(System.IO.Path.Combine(root, @"Data\headsq\quarter"));
            v16.SetImageRange(1, 93);
            v16.SetDataSpacing(3.2, 3.2, 1.5);
            v16.Update();

            vtkMergePoints myLocator = vtkMergePoints.New();

            vtkMarchingSquares isoXY = vtkMarchingSquares.New();

#if VTK_MAJOR_VERSION_5
            isoXY.SetInputConnection(v16.GetOutputPort());
#else
            isoXY.SetInputData(v16);
#endif
            isoXY.GenerateValues(2, 600, 1200);
            isoXY.SetImageRange(0, 32, 32, 63, 45, 45);
            isoXY.SetLocator(myLocator);

            vtkPolyDataMapper isoXYMapper = vtkPolyDataMapper.New();
#if VTK_MAJOR_VERSION_5
            isoXYMapper.SetInputConnection(isoXY.GetOutputPort());
#else
            isoXYMapper.SetInputData(isoXY);
#endif
            isoXYMapper.SetScalarRange(600, 1200);

            vtkActor isoXYActor = vtkActor.New();
            isoXYActor.SetMapper(isoXYMapper);

            vtkMarchingSquares isoYZ = vtkMarchingSquares.New();
#if VTK_MAJOR_VERSION_5
            isoYZ.SetInputConnection(v16.GetOutputPort());
#else
            isoYZ.SetInputData(v16);
#endif
            isoYZ.GenerateValues(2, 600, 1200);
            isoYZ.SetImageRange(32, 32, 32, 63, 46, 92);

            vtkPolyDataMapper isoYZMapper = vtkPolyDataMapper.New();
#if VTK_MAJOR_VERSION_5
            isoYZMapper.SetInputConnection(isoYZ.GetOutputPort());
#else
            isoYZMapper.SetInputData(isoYZ);
#endif
            isoYZMapper.SetScalarRange(600, 1200);

            vtkActor isoYZActor = vtkActor.New();
            isoYZActor.SetMapper(isoYZMapper);

            vtkMarchingSquares isoXZ = vtkMarchingSquares.New();
#if VTK_MAJOR_VERSION_5
            isoXZ.SetInputConnection(v16.GetOutputPort());
#else
            isoXZ.SetInputData(v16);
#endif
            isoXZ.GenerateValues(2, 600, 1200);
            isoXZ.SetImageRange(0, 32, 32, 32, 0, 46);

            vtkPolyDataMapper isoXZMapper = vtkPolyDataMapper.New();
#if VTK_MAJOR_VERSION_5
            isoXZMapper.SetInputConnection(isoXZ.GetOutputPort());
#else
            isoXZMapper.SetInputData(isoXZ);
#endif
            isoXZMapper.SetScalarRange(600, 1200);

            vtkActor isoXZActor = vtkActor.New();
            isoXZActor.SetMapper(isoXZMapper);

            vtkOutlineFilter outline = vtkOutlineFilter.New();
#if VTK_MAJOR_VERSION_5
            outline.SetInputConnection(v16.GetOutputPort());
#else
            outline.SetInputData(v16);
#endif
            vtkPolyDataMapper outlineMapper = vtkPolyDataMapper.New();
#if VTK_MAJOR_VERSION_5
            outlineMapper.SetInputConnection(outline.GetOutputPort());
#else
            outlineMapper.SetInputData(outline);
#endif
            vtkActor outlineActor = vtkActor.New();
            outlineActor.SetMapper(outlineMapper);
            outlineActor.VisibilityOff();

            // get a reference to the renderwindow of our renderWindowControl1
            vtkRenderWindow renderWindow = renderWindowControl1.RenderWindow;
            // renderer
            vtkRenderer renderer = renderWindow.GetRenderers().GetFirstRenderer();
            // set background color
            renderer.SetBackground(1.0, 1.0, 1.0);
            //Add the actors to the renderer, set the background and size
            renderer.AddActor(outlineActor);
            renderer.AddActor(isoXYActor);
            renderer.AddActor(isoYZActor);
            renderer.AddActor(isoXZActor);
            renderer.GetActiveCamera().SetPosition(324.368, 284.266, -19.3293);
            renderer.GetActiveCamera().SetFocalPoint(73.5683, 120.903, 70.7309);
            renderer.GetActiveCamera().SetViewAngle(30);
            renderer.GetActiveCamera().SetViewUp(-0.304692, -0.0563843, -0.950781);
        }
Exemplo n.º 22
0
    /// <summary>
    /// The main entry method called by the CSharp driver
    /// </summary>
    /// <param name="argv"></param>
    public static void Main(String[] argv)
    {
        try
        {
            bool fail_on_image_diff = false;

            //Prefix Content is: ""
            int argc = 0;
            if (argv != null)
            {
                argc = argv.Length;
            }

            // setup some common things for testing[]
            vtkMath.RandomSeed(6);
            // create the testing class to do the work[]
            rtTester = new vtkTesting();
            for (int i = 1; i < argc; i++)
            {
                rtTester.AddArgument(argv[i]);

                if (argv[i] == "--fail-on-image-diff")
                {
                    fail_on_image_diff = true;
                }
            }

            // string auto_path = "";
            //
            VTK_DATA_ROOT = rtTester.GetDataRoot();

            // load in the script[]
            if (0 == argv.Length)
            {
                test = GetTestNameInteractively();
            }
            else
            {
                test = argv[0];
            }

            //The class that we are about to execute the test in
            System.Type t = System.Type.GetType(test + "Class");
            if (null == t)
            {
                throw new System.ArgumentException(System.String.Format(
                                                       "error: could not create a Type object for '{0}'...\n\n{1}\n{2}\n{3}\n{4}\n\n{5}\n\n",
                                                       test + "Class",
                                                       "Typo?",
                                                       "Did you follow the C# test driver naming convention?",
                                                       "Did you add the test to the CMakeLists.txt file?",
                                                       "Did you reconfigure/rebuild after adding the test?",
                                                       "Test 'method' name should equal 'file name without extension'... Test 'public class' name should be the same but with 'Class' appended..."
                                                       ));
            }


            // set the default threshold, the Tcl script may change this[]
            threshold = -1;

            executeMethod(t, "Setthreshold", new object[] { threshold });
            executeMethod(t, "SetVTK_DATA_ROOT", new object[] { VTK_DATA_ROOT });

            //run the test
            executeMethod(t, test, new object[] { argv });

            tempRenderWindowInteractor = (vtkRenderWindowInteractor)executeMethod(t, "Getiren", new object[] { });
            tempRenderWindow           = (vtkRenderWindow)executeMethod(t, "GetrenWin", new object[] { });
            tempViewer = (vtkObject)executeMethod(t, "Getviewer", new object[] { });
            tempw2i    = (vtkWindowToImageFilter)executeMethod(t, "Getw2i", new object[] { });

            //update the threshold from what the test made it
            threshold = (int)executeMethod(t, "Getthreshold", new object[] { });
            if (tempRenderWindowInteractor != null)
            {
                tempRenderWindow.Render();
            }

            // run the event loop quickly to map any tkwidget windows[]
            // current directory[]
            rtResult = 0;
            if (fail_on_image_diff && rtTester.IsValidImageSpecified() != 0)
            {
                // look for a renderWindow ImageWindow or ImageViewer[]
                // first check for some common names[]
                if (tempRenderWindow != null)
                {
                    rtTester.SetRenderWindow(tempRenderWindow);
                    if ((threshold) == -1)
                    {
                        threshold = 10;
                    }
                }
                else
                {
                    if ((threshold) == -1)
                    {
                        threshold = 5;
                    }
                    if (tempViewer != null)
                    {
                        if (tempViewer.IsA("vtkImageViewer") != 0)
                        {
                            tempRenderWindow = ((vtkImageViewer)tempViewer).GetRenderWindow();
                        }
                        else if (tempViewer.IsA("vtkImageViewer2") != 0)
                        {
                            tempRenderWindow = ((vtkImageViewer2)tempViewer).GetRenderWindow();
                        }
                        else
                        {
                            throw new System.Exception("");
                        }
                        rtTester.SetRenderWindow(tempRenderWindow);

                        if (tempViewer.IsA("vtkImageViewer") != 0)
                        {
                            ((vtkImageViewer)tempViewer).Render();
                        }
                        else if (tempViewer.IsA("vtkImageViewer2") != 0)
                        {
                            ((vtkImageViewer2)tempViewer).Render();
                        }
                    }
                    else
                    {
                        tempRenderWindow = (vtkRenderWindow)executeMethod(t, "GetimgWin", new object[] { });
                        if (tempRenderWindow != null)
                        {
                            rtTester.SetRenderWindow(tempRenderWindow);
                            tempRenderWindow.Render();
                        }
                    }
                }

                if (tempRenderWindow == null)
                {
                    throw new System.Exception("tempRenderWindow cannot be null for IsValidImageSpecified case...");
                }

                rtResult = rtTester.RegressionTest(threshold);
            }

            if (rtTester.IsInteractiveModeSpecified() != 0)
            {
                if (tempRenderWindowInteractor != null)
                {
                    tempRenderWindowInteractor.Start();
                }
            }

            // Force other objects that may have holds on the render window
            // to let go:
            //
            rtTester.SetRenderWindow(null);
            if (null != tempw2i)
            {
                tempw2i.SetInput(null);
            }

            executeMethod(t, "deleteAllVTKObjects", new object[] { });
            deleteAllVTKObjects();

            // Force a garbage collection prior to exiting the test
            // so that any memory leaks reported are likely to be
            // *actual* leaks of some sort rather than false reports:
            //
            System.GC.Collect();
            System.GC.WaitForPendingFinalizers();

            // Fail the tests that have image diffs if fail_on_image_diff is on:
            //
            if (fail_on_image_diff && 0 == rtResult)
            {
                throw new System.Exception("error: image RegressionTest failed");
            }

            // Test finished without throwing any exceptions...
            // Therefore, it passed. Exit with a zero ExitCode.
            //
            System.Environment.ExitCode = 0;
        }
        catch (System.Exception exc)
        {
            // Catch anything, spit it out to the console so it can be captured
            // by ctest. Exit with a non-zero ExitCode.
            //
            System.Console.Error.WriteLine("================================================================================");
            System.Console.Error.WriteLine("");
            System.Console.Error.WriteLine("TclToCsScript C# test driver caught System.Exception:");
            System.Console.Error.WriteLine("");
            System.Console.Error.WriteLine("{0}", exc.ToString());
            System.Console.Error.WriteLine("");
            System.Console.Error.WriteLine("================================================================================");
            System.Console.Error.WriteLine("");
            System.Environment.ExitCode = 2345;
        }
    }
Exemplo n.º 23
0
    public static int Main(string[] args)
    {
        vtkTesting testHelper = vtkTesting.New();

        for (int cc = 0; cc < args.Length; cc++)
        {
            //testHelper.AddArguments(argc,const_cast<const char **>(argv));
            //System.Console.Write( "args: " + args[cc] + "\n" );
            testHelper.AddArgument(args[cc]);
        }
        if (testHelper.IsFlagSpecified("-D") != 0)
        {
            string VTK_DATA_ROOT = vtkGDCMTesting.GetVTKDataRoot();
            if (VTK_DATA_ROOT != null)
            {
                //System.Console.Write( "VTK_DATA_ROOT: " + VTK_DATA_ROOT  + "\n" );
                testHelper.SetDataRoot(VTK_DATA_ROOT);
                testHelper.AddArgument("-D");
                testHelper.AddArgument(VTK_DATA_ROOT);
            }
        }

        string dataRoot = testHelper.GetDataRoot();
        string filename = dataRoot;

        filename += "/Data/mr.001";

        vtkDirectory dir = vtkDirectory.New();

        if (dir.FileIsDirectory(dataRoot) == 0)
        {
            filename = vtkGDCMTesting.GetGDCMDataRoot() + "/test.acr";
        }
        //System.Console.Write( "dataRoot: " + dataRoot + "\n" );
        System.Console.Write("filename being used is: " + filename + "\n");

        vtkGDCMImageReader reader = vtkGDCMImageReader.New();
        vtkStringArray     array  = vtkStringArray.New();

        array.InsertNextValue(filename);
        reader.SetFileNames(array);
        reader.Update();

        System.Console.Write(reader.GetOutput());

        vtkRenderWindowInteractor iren = vtkRenderWindowInteractor.New();

        vtkRenderer     ren1   = vtkRenderer.New();
        vtkRenderWindow renWin = vtkRenderWindow.New();

        renWin.AddRenderer(ren1);

        vtkImageActor actor = vtkImageActor.New();

        vtkImageMapToWindowLevelColors coronalColors = vtkImageMapToWindowLevelColors.New();

        coronalColors.SetInput(reader.GetOutput());

        actor.SetInput(coronalColors.GetOutput());

        ren1.AddActor(actor);
        iren.SetRenderWindow(renWin);

        iren.Initialize();

        renWin.Render();

        int retVal = testHelper.IsInteractiveModeSpecified();

        if (retVal != 0)
        {
            iren.Start();
        }

        return(0);
    }
Exemplo n.º 24
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);
        }
        private void XMLStructuredGridWriter()
        {
            // 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\structuredgrid_test.vts");
            // Create a grid
            vtkStructuredGrid structuredGrid = vtkStructuredGrid.New();
            vtkPoints         points         = vtkPoints.New();

            points.InsertNextPoint(0, 0, 0);
            points.InsertNextPoint(1, 0, 0);
            points.InsertNextPoint(0, 1, 0);
            points.InsertNextPoint(1, 1, 0);
            points.InsertNextPoint(0, 2, 0);
            points.InsertNextPoint(1, 2, 1);

            // Specify the dimensions of the grid
            structuredGrid.SetDimensions(2, 3, 1);
            structuredGrid.SetPoints(points);

            // Write file
            vtkXMLStructuredGridWriter writer = vtkXMLStructuredGridWriter.New();

            writer.SetFileName(filePath);
            writer.SetInput(structuredGrid);
            writer.Write();

            // Read and display file for verification that it was written correctly
            vtkXMLStructuredGridReader reader = vtkXMLStructuredGridReader.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

            vtkStructuredGridGeometryFilter geometryFilter = vtkStructuredGridGeometryFilter.New();

            geometryFilter.SetInputConnection(reader.GetOutputPort());
            geometryFilter.Update();

            // Visualize
            vtkPolyDataMapper mapper = vtkPolyDataMapper.New();

            mapper.SetInputConnection(geometryFilter.GetOutputPort());

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

            actor.SetMapper(mapper);
            // 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);
        }
Exemplo n.º 26
0
        private void ReadDICOMSeries()
        {
            // 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();
            // Read all the DICOM files in the specified directory.
            // Caution: folder "DicomTestImages" don't exists by default in the standard vtk data folder
            // sample data are available at http://www.vtk.org/Wiki/images/1/12/VTK_Examples_StandardFormats_Input_DicomTestImages.zip

            //string folder = Path.Combine(root, @"Data\DicomTestImages");
            string folder = @"provaDicom/" + studyUID;

            Console.WriteLine(folder);
            vtkDICOMImageReader reader = vtkDICOMImageReader.New();

            reader.SetDirectoryName(folder);
            reader.Update();
            // Visualize
            _ImageViewer = vtkImageViewer2.New();
            _ImageViewer.SetInputConnection(reader.GetOutputPort());
            // get range of slices (min is the first index, max is the last index)
            _ImageViewer.GetSliceRange(ref _MinSlice, ref _MaxSlice);
            Debug.WriteLine("slices range from : " + _MinSlice.ToString() + " to " + _MaxSlice.ToString());

            // slice status message
            vtkTextProperty sliceTextProp = vtkTextProperty.New();

            sliceTextProp.SetFontFamilyToCourier();
            sliceTextProp.SetFontSize(20);
            sliceTextProp.SetVerticalJustificationToBottom();
            sliceTextProp.SetJustificationToLeft();

            _SliceStatusMapper = vtkTextMapper.New();
            _SliceStatusMapper.SetInput("Slice No " + (_Slice + 1).ToString() + "/" + (_MaxSlice + 1).ToString());
            _SliceStatusMapper.SetTextProperty(sliceTextProp);

            vtkActor2D sliceStatusActor = vtkActor2D.New();

            sliceStatusActor.SetMapper(_SliceStatusMapper);
            sliceStatusActor.SetPosition(15, 10);
            // usage hint message
            vtkTextProperty usageTextProp = vtkTextProperty.New();

            usageTextProp.SetFontFamilyToCourier();
            usageTextProp.SetFontSize(14);
            usageTextProp.SetVerticalJustificationToTop();
            usageTextProp.SetJustificationToLeft();

            vtkTextMapper usageTextMapper = vtkTextMapper.New();

            usageTextMapper.SetInput("Slice with mouse wheel\nor Up/Down-Key");
            usageTextMapper.SetTextProperty(usageTextProp);

            vtkActor2D usageTextActor = vtkActor2D.New();

            usageTextActor.SetMapper(usageTextMapper);
            usageTextActor.GetPositionCoordinate().SetCoordinateSystemToNormalizedDisplay();
            usageTextActor.GetPositionCoordinate().SetValue(0.05, 0.95);

            vtkRenderWindow renderWindow = renderWindowControl1.RenderWindow;

            vtkInteractorStyleImage interactorStyle = vtkInteractorStyleImage.New();

            // NOTA:non funziona la rotellina del mouse per cambiare slice <--------------------------------------
            // l'errore è causato dalla funzione DicomCFindRequest(della sorgente di FellowOak)
            //in QueryFellowOak.cs, in particolare dal costruttore
            // DicomCFindRequest(DicomQueryRetrieveLevel level)

            // interactorStyle.MouseWheelForwardEvt += new vtkObject.vtkObjectEventHandler(interactor_MouseWheelForwardEvt);
            //  interactorStyle.MouseWheelBackwardEvt += new vtkObject.vtkObjectEventHandler(interactor_MouseWheelBackwardEvt);

            renderWindow.GetInteractor().SetInteractorStyle(interactorStyle);
            renderWindow.GetRenderers().InitTraversal();
            vtkRenderer ren;

            while ((ren = renderWindow.GetRenderers().GetNextItem()) != null)
            {
                ren.SetBackground(0.0, 0.0, 0.0);
            }

            _ImageViewer.SetRenderWindow(renderWindow);
            _ImageViewer.GetRenderer().AddActor2D(sliceStatusActor);
            _ImageViewer.GetRenderer().AddActor2D(usageTextActor);
            _ImageViewer.SetSlice(_MinSlice);
            _ImageViewer.Render();
        }
Exemplo n.º 27
0
        private void ReadPlainText()
        {
            // 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\teapot.xyz");

            FileStream   fs = null;
            StreamReader sr = null;
            String       sLineBuffer;

            String[]  sXYZ;
            char[]    chDelimiter = new char[] { ' ', '\t', ';' };
            double[]  xyz         = new double[3];
            vtkPoints points      = vtkPoints.New();
            int       cnt         = 0;

            try {
                // in case file must be open in another application too use "FileShare.ReadWrite"
                fs = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
                sr = new StreamReader(fs);
                while (!sr.EndOfStream)
                {
                    sLineBuffer = sr.ReadLine();
                    cnt++;
                    sXYZ = sLineBuffer.Split(chDelimiter, StringSplitOptions.RemoveEmptyEntries);
                    if (sXYZ == null || sXYZ.Length != 3)
                    {
                        MessageBox.Show("data seems to be in wrong format at line " + cnt, "Format Exception", MessageBoxButtons.OK);
                        return;
                    }
                    xyz[0] = double.Parse(sXYZ[0], CultureInfo.InvariantCulture);
                    xyz[1] = double.Parse(sXYZ[1], CultureInfo.InvariantCulture);
                    xyz[2] = double.Parse(sXYZ[2], CultureInfo.InvariantCulture);
                    points.InsertNextPoint(xyz[0], xyz[1], xyz[2]);
                }
                vtkPolyData polydata = vtkPolyData.New();
                polydata.SetPoints(points);
                vtkVertexGlyphFilter glyphFilter = vtkVertexGlyphFilter.New();
                glyphFilter.SetInputConnection(polydata.GetProducerPort());

                // Visualize
                vtkPolyDataMapper mapper = vtkPolyDataMapper.New();
                mapper.SetInputConnection(glyphFilter.GetOutputPort());

                vtkActor actor = vtkActor.New();
                actor.SetMapper(mapper);
                actor.GetProperty().SetPointSize(4);
                actor.GetProperty().SetColor(1, 0.5, 0);
                // 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);
            }
            catch (IOException ex) {
                MessageBox.Show(ex.Message, "IOException", MessageBoxButtons.OK);
            }
            finally {
                if (sr != null)
                {
                    sr.Close();
                    sr.Dispose();
                    sr = null;
                }
            }
        }
Exemplo n.º 28
0
    /// <summary>
    /// The main entry method called by the CSharp driver
    /// </summary>
    /// <param name="argv"></param>
    public static void Main(String[] argv)
    {
        try
        {
          bool fail_on_image_diff = false;

          //Prefix Content is: ""
          int argc = 0;
          if (argv != null)
          {
        argc = argv.Length;
          }

          // setup some common things for testing[]
          vtkMath.RandomSeed(6);
          // create the testing class to do the work[]
          rtTester = new vtkTesting();
          for (int i = 1; i < argc; i++)
          {
        rtTester.AddArgument(argv[i]);

        if (argv[i] == "--fail-on-image-diff")
        {
          fail_on_image_diff = true;
        }
          }

          // string auto_path = "";
          //
          VTK_DATA_ROOT = rtTester.GetDataRoot();

          // load in the script[]
          if (0 == argv.Length)
          {
        test = GetTestNameInteractively();
          }
          else
          {
        test = argv[0];
          }

          //The class that we are about to execute the test in
          System.Type t = System.Type.GetType(test + "Class");
          if (null == t)
          {
        throw new System.ArgumentException(System.String.Format(
              "error: could not create a Type object for '{0}'...\n\n{1}\n{2}\n{3}\n{4}\n\n{5}\n\n",
              test + "Class",
              "Typo?",
              "Did you follow the C# test driver naming convention?",
              "Did you add the test to the CMakeLists.txt file?",
              "Did you reconfigure/rebuild after adding the test?",
              "Test 'method' name should equal 'file name without extension'... Test 'public class' name should be the same but with 'Class' appended..."
              ));
          }

          // set the default threshold, the Tcl script may change this[]
          threshold = -1;

          executeMethod(t, "Setthreshold", new object[] { threshold });
          executeMethod(t, "SetVTK_DATA_ROOT", new object[] { VTK_DATA_ROOT });

          //run the test
          executeMethod(t, test, new object[] { argv });

          tempRenderWindowInteractor = (vtkRenderWindowInteractor)executeMethod(t, "Getiren", new object[] { });
          tempRenderWindow = (vtkRenderWindow)executeMethod(t, "GetrenWin", new object[] { });
          tempViewer = (vtkObject)executeMethod(t, "Getviewer", new object[] { });
          tempw2i = (vtkWindowToImageFilter)executeMethod(t, "Getw2i", new object[] { });

          //update the threshold from what the test made it
          threshold = (int)executeMethod(t, "Getthreshold", new object[] { });
          if (tempRenderWindowInteractor != null)
          {
        tempRenderWindow.Render();
          }

          // run the event loop quickly to map any tkwidget windows[]
          // current directory[]
          rtResult = 0;
          if (fail_on_image_diff && rtTester.IsValidImageSpecified() != 0)
          {
        // look for a renderWindow ImageWindow or ImageViewer[]
        // first check for some common names[]
        if (tempRenderWindow != null)
        {
          rtTester.SetRenderWindow(tempRenderWindow);
          if ((threshold) == -1)
          {
            threshold = 10;
          }
        }
        else
        {
          if ((threshold) == -1)
          {
            threshold = 5;
          }
          if (tempViewer != null)
          {
            if (tempViewer.IsA("vtkImageViewer") != 0)
            {
              tempRenderWindow = ((vtkImageViewer)tempViewer).GetRenderWindow();
            }
            else if (tempViewer.IsA("vtkImageViewer2") != 0)
            {
              tempRenderWindow = ((vtkImageViewer2)tempViewer).GetRenderWindow();
            }
            else
            {
              throw new System.Exception("");
            }
            rtTester.SetRenderWindow(tempRenderWindow);

            if (tempViewer.IsA("vtkImageViewer") != 0)
            {
              ((vtkImageViewer)tempViewer).Render();
            }
            else if (tempViewer.IsA("vtkImageViewer2") != 0)
            {
              ((vtkImageViewer2)tempViewer).Render();
            }
          }
          else
          {
            tempRenderWindow = (vtkRenderWindow)executeMethod(t, "GetimgWin", new object[] { });
            if (tempRenderWindow != null)
            {
              rtTester.SetRenderWindow(tempRenderWindow);
              tempRenderWindow.Render();
            }
          }
        }

        if (tempRenderWindow == null)
        {
          throw new System.Exception("tempRenderWindow cannot be null for IsValidImageSpecified case...");
        }

        rtResult = rtTester.RegressionTest(threshold);
          }

          if (rtTester.IsInteractiveModeSpecified() != 0)
          {
        if (tempRenderWindowInteractor != null)
        {
          tempRenderWindowInteractor.Start();
        }
          }

          // Force other objects that may have holds on the render window
          // to let go:
          //
          rtTester.SetRenderWindow(null);
          if (null != tempw2i)
          {
        tempw2i.SetInput(null);
          }

          executeMethod(t, "deleteAllVTKObjects", new object[] { });
          deleteAllVTKObjects();

          // Force a garbage collection prior to exiting the test
          // so that any memory leaks reported are likely to be
          // *actual* leaks of some sort rather than false reports:
          //
          System.GC.Collect();
          System.GC.WaitForPendingFinalizers();

          // Fail the tests that have image diffs if fail_on_image_diff is on:
          //
          if (fail_on_image_diff && 0 == rtResult)
          {
        throw new System.Exception("error: image RegressionTest failed");
          }

          // Test finished without throwing any exceptions...
          // Therefore, it passed. Exit with a zero ExitCode.
          //
          System.Environment.ExitCode = 0;
        }
        catch (System.Exception exc)
        {
          // Catch anything, spit it out to the console so it can be captured
          // by ctest. Exit with a non-zero ExitCode.
          //
          System.Console.Error.WriteLine("================================================================================");
          System.Console.Error.WriteLine("");
          System.Console.Error.WriteLine("TclToCsScript C# test driver caught System.Exception:");
          System.Console.Error.WriteLine("");
          System.Console.Error.WriteLine("{0}", exc.ToString());
          System.Console.Error.WriteLine("");
          System.Console.Error.WriteLine("================================================================================");
          System.Console.Error.WriteLine("");
          System.Environment.ExitCode = 2345;
        }
    }
Exemplo n.º 29
0
        private void WriteVTUFile()
        {
            // 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\tetra_test.vtu");

            vtkPoints points = vtkPoints.New();

            points.InsertNextPoint(0, 0, 0);
            points.InsertNextPoint(1, 0, 0);
            points.InsertNextPoint(1, 1, 0);
            points.InsertNextPoint(0, 1, 1);

            vtkTetra tetra = vtkTetra.New();

            tetra.GetPointIds().SetId(0, 0);
            tetra.GetPointIds().SetId(1, 1);
            tetra.GetPointIds().SetId(2, 2);
            tetra.GetPointIds().SetId(3, 3);

            vtkCellArray cellArray = vtkCellArray.New();

            cellArray.InsertNextCell(tetra);

            vtkUnstructuredGrid unstructuredGrid = vtkUnstructuredGrid.New();

            unstructuredGrid.SetPoints(points);
            const int VTK_TETRA = 10;

            unstructuredGrid.SetCells(VTK_TETRA, cellArray);

            // Write file
            vtkXMLUnstructuredGridWriter writer = vtkXMLUnstructuredGridWriter.New();

            writer.SetFileName(filePath);
            writer.SetInput(unstructuredGrid);
            writer.Write();

            // Read and display file for verification that it was written correctly
            vtkXMLUnstructuredGridReader reader = vtkXMLUnstructuredGridReader.New();

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

            vtkDataSetMapper mapper = vtkDataSetMapper.New();

            mapper.SetInputConnection(reader.GetOutputPort());

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

            actor.SetMapper(mapper);

            // 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);
        }