private void vtkPolyDataConnectivityFilter_LargestRegion()
        {
            // Small sphere
            vtkSphereSource sphereSource1 = vtkSphereSource.New();

            sphereSource1.Update();

            // Large sphere
            vtkSphereSource sphereSource2 = vtkSphereSource.New();

            sphereSource2.SetRadius(10);
            sphereSource2.SetCenter(25, 0, 0);
            sphereSource2.SetThetaResolution(10);
            sphereSource2.SetPhiResolution(10);
            sphereSource2.Update();

            vtkAppendPolyData appendFilter = vtkAppendPolyData.New();

            appendFilter.AddInputConnection(sphereSource1.GetOutputPort());
            appendFilter.AddInputConnection(sphereSource2.GetOutputPort());
            appendFilter.Update();

            vtkPolyDataConnectivityFilter connectivityFilter = vtkPolyDataConnectivityFilter.New();

            connectivityFilter.SetInputConnection(appendFilter.GetOutputPort());
            connectivityFilter.SetExtractionModeToLargestRegion();
            connectivityFilter.Update();

            // Create a mapper and actor for original data
            vtkPolyDataMapper originalMapper = vtkPolyDataMapper.New();

            originalMapper.SetInputConnection(appendFilter.GetOutputPort());
            originalMapper.Update();

            vtkActor originalActor = vtkActor.New();

            originalActor.SetMapper(originalMapper);

            // Create a mapper and actor for extracted data
            vtkPolyDataMapper extractedMapper = vtkPolyDataMapper.New();

            extractedMapper.SetInputConnection(connectivityFilter.GetOutputPort());
            extractedMapper.Update();

            vtkActor extractedActor = vtkActor.New();

            extractedActor.GetProperty().SetColor(1, 0, 0);
            extractedActor.SetMapper(extractedMapper);
            // 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(originalActor);
            renderer.AddActor(extractedActor);
        }
Example #2
0
        public IVtkPreprocess SetMapper()
        {
            polyDataMapper = vtkPolyDataMapper.New();
            polyDataMapper.SetInputConnection(reader.GetOutputPort());
            polyDataMapper.Update();

            return(this);
        }
Example #3
0
        public XmlPolyDataPackage(string vtpFile, vtkRenderer renderer)
        {
            //Console.WriteLine(string.Format("VTP文件 = {0}", vtpFile));

            if (File.Exists(vtpFile))
            {
                if (vtpFile.EndsWith(".vtp"))
                {
                    //Console.WriteLine(string.Format("VTP文件 = {0}已存在", vtpFile));
                    reader = vtkXMLPolyDataReader.New();
                    reader.SetFileName(vtpFile);
                    reader.Update();

                    PolyData = reader.GetOutput();
                }
                else if (vtpFile.EndsWith(".stl"))
                {
                    PolyData = VTKUtil.ReadSTLPolyData(vtpFile);
                }

                vtkCamera camera = vtkCamera.New();

                var sortPolydata = new vtkDepthSortPolyData();
                sortPolydata.SetInput(PolyData);
                sortPolydata.SetCamera(camera);
                sortPolydata.SetDirectionToFrontToBack();
                sortPolydata.SetVector(1, 1, 1);
                sortPolydata.SetSortScalars(1);
                sortPolydata.Update();

                _polyDataMapper = vtkPolyDataMapper.New();
                _polyDataMapper.ScalarVisibilityOff();
                _polyDataMapper.SetInput(sortPolydata.GetOutput());
                _polyDataMapper.Update();

                actor = vtkActor.New();
                actor.SetMapper(_polyDataMapper);
                actor.GetProperty().SetColor(1, 1, 1);

                //marchingCubeActor.GetProperty().BackfaceCullingOn();
                //marchingCubeActor.GetProperty().FrontfaceCullingOn();
                //marchingCubeActor.GetProperty().BackfaceCullingOff();

                // now, tell the renderer our actors
                renderer.AddActor(actor);

                this.renderer = renderer;
            }
            else
            {
                throw new Exception(string.Format("VTP文件 = {0}不存在。", vtpFile));
            }

            //正面观();
        }
Example #4
0
        public void SetMapper(vtkPolyData data)
        {
            vtkVertexGlyphFilter filter = vtkVertexGlyphFilter.New();

            filter.SetInput(data);
            filter.Update();


            polyDataMapper = vtkPolyDataMapper.New();
            polyDataMapper.SetInputConnection(filter.GetOutputPort());
            polyDataMapper.Update();
        }
Example #5
0
        public XmlPolyDataPackage(vtkPolyData polyData, vtkRenderer renderer)
        {
            this.PolyData   = polyData;
            _polyDataMapper = vtkPolyDataMapper.New();
            _polyDataMapper.ScalarVisibilityOff();
            _polyDataMapper.SetInput(PolyData);
            _polyDataMapper.Update();

            actor = vtkActor.New();
            actor.SetMapper(_polyDataMapper);

            //marchingCubeActor.GetProperty().BackfaceCullingOn();
            //marchingCubeActor.GetProperty().FrontfaceCullingOn();
            //marchingCubeActor.GetProperty().BackfaceCullingOff();

            // now, tell the renderer our actors
            renderer.AddActor(actor);

            this.renderer = renderer;
        }
Example #6
0
        private void VectorFieldNonZeroExtraction()
        {
            // Create an image
            vtkImageData image = vtkImageData.New();

            CreateVectorField(ref image);

            // This filter produces a vtkImageData with an array named "Magnitude"
            vtkImageMagnitude magnitudeFilter = vtkImageMagnitude.New();

            magnitudeFilter.SetInputConnection(image.GetProducerPort());
            magnitudeFilter.Update();

            image.GetPointData().AddArray(magnitudeFilter.GetOutput().GetPointData().GetScalars());
            image.GetPointData().SetActiveScalars("Magnitude");

            vtkThresholdPoints thresholdVector = vtkThresholdPoints.New();

            thresholdVector.SetInput(image);
            thresholdVector.SetInputArrayToProcess(
                0,
                0,
                (int)vtkDataObject.FieldAssociations.FIELD_ASSOCIATION_POINTS,
                (int)vtkDataSetAttributes.AttributeTypes.SCALARS,
                "Magnitude");
            thresholdVector.ThresholdByUpper(0.00001);
            thresholdVector.Update();

            // in case you want to save imageData
            //vtkXMLPolyDataWriter writer = vtkXMLPolyDataWriter.New();
            //writer.SetFileName("output.vtp");
            //writer.SetInputConnection(thresholdPoints.GetOutputPort());
            //writer.Write();

            // repesents the pixels
            vtkCubeSource cubeSource = vtkCubeSource.New();

            cubeSource.SetXLength(2.0);
            cubeSource.SetYLength(2.0);
            cubeSource.SetZLength(2.0);
            vtkGlyph3D glyph = vtkGlyph3D.New();

            glyph.SetInput(image);
            glyph.SetSourceConnection(cubeSource.GetOutputPort());
            // don't scale glyphs according to any scalar data
            glyph.SetScaleModeToDataScalingOff();

            vtkPolyDataMapper glyphMapper = vtkPolyDataMapper.New();

            glyphMapper.SetInputConnection(glyph.GetOutputPort());
            // don't color glyphs according to scalar data
            glyphMapper.ScalarVisibilityOff();
            glyphMapper.SetScalarModeToDefault();

            vtkActor actor = vtkActor.New();

            actor.SetMapper(glyphMapper);

            // represent vector field
            vtkGlyph3D        vectorGlyph       = vtkGlyph3D.New();
            vtkArrowSource    arrowSource       = vtkArrowSource.New();
            vtkPolyDataMapper vectorGlyphMapper = vtkPolyDataMapper.New();

            int n = image.GetPointData().GetNumberOfArrays();

            for (int i = 0; i < n; i++)
            {
                Debug.WriteLine("name of array[" + i + "]: " + image.GetPointData().GetArrayName(i));
            }

            vtkPolyData tmp = thresholdVector.GetOutput();

            Debug.WriteLine("number of thresholded points: " + tmp.GetNumberOfPoints());
            vectorGlyph.SetInputConnection(thresholdVector.GetOutputPort());

            // in case you want the point glyphs to be oriented according to
            // scalar values in array "ImageScalars" uncomment the following line
            image.GetPointData().SetActiveVectors("ImageScalars");

            vectorGlyph.SetSourceConnection(arrowSource.GetOutputPort());
            vectorGlyph.SetScaleModeToScaleByVector();
            vectorGlyph.SetVectorModeToUseVector();
            vectorGlyph.ScalingOn();
            vectorGlyph.OrientOn();
            vectorGlyph.SetInputArrayToProcess(
                1,
                0,
                (int)vtkDataObject.FieldAssociations.FIELD_ASSOCIATION_POINTS,
                (int)vtkDataSetAttributes.AttributeTypes.SCALARS,
                "ImageScalars");

            vectorGlyph.Update();

            vectorGlyphMapper.SetInputConnection(vectorGlyph.GetOutputPort());
            vectorGlyphMapper.Update();

            vtkActor vectorActor = vtkActor.New();

            vectorActor.SetMapper(vectorGlyphMapper);


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

            // set background color
            renderer.SetBackground(.2, .6, .3);
            //Add the actors to the renderer, set the background and size
            renderer.AddActor(actor);
            renderer.AddActor(vectorActor);
        }
Example #7
0
        static public vtkProp3D genRayActor(List <RayLineCluster> data)
        {
            vtkPoints    points    = vtkPoints.New();
            vtkCellArray pLineCell = vtkCellArray.New();

            vtkLine p1   = vtkLine.New();
            int     cout = 0;

            System.Windows.Forms.MessageBox.Show("cluster.start_radius:" + data.Count.ToString());
            for (int i = 0; i < data.Count; i++)
            {
                RayLineCluster cluster = data[i];
                if (i != data.Count - 1)
                {
                    // 第一个的起点连第二次的起点
                    List <RayLine> rays_start = cluster.ray_cluster;
                    List <RayLine> rays_end   = data[i + 1].ray_cluster;
                    for (int j = 0; j < rays_start.Count; j++)
                    {
                        points.InsertNextPoint(rays_start[j].start_point.x,
                                               rays_start[j].start_point.y, rays_start[j].start_point.z);
                        points.InsertNextPoint(rays_end[j].start_point.x,
                                               rays_end[j].start_point.y, rays_end[j].start_point.z);

                        p1.GetPointIds().SetId(0, cout++);
                        p1.GetPointIds().SetId(1, cout++);
                        pLineCell.InsertNextCell(p1);
                    }
                }
                else
                {
                    foreach (var line in cluster.ray_cluster)
                    {
                        points.InsertNextPoint(line.start_point.x,
                                               line.start_point.y, line.start_point.z);
                        points.InsertNextPoint(
                            line.start_point.x + 0.5 * line.normal_line.x,
                            line.start_point.y + 0.5 * line.normal_line.y,
                            line.start_point.z + 0.5 * line.normal_line.z);
                        p1.GetPointIds().SetId(0, cout++);
                        p1.GetPointIds().SetId(1, cout++);
                        pLineCell.InsertNextCell(p1);
                    }
                }
            }

            vtkPolyData pointsData = vtkPolyData.New();

            pointsData.SetPoints(points); //获得网格模型中的几何数据:点集
            pointsData.SetLines(pLineCell);

            vtkPolyDataMapper pointMapper = vtkPolyDataMapper.New();

            pointMapper.SetInput(pointsData);
            pointMapper.Update();

            vtkActor actor = vtkActor.New();

            actor.SetMapper(pointMapper);
            actor.GetProperty().SetColor(1, 0, 0);
            return(actor);
        }
    /// <summary>
    /// The main entry method called by the CSharp driver
    /// </summary>
    /// <param name="argv"></param>
    public static void AVTestPDataSetReaderGrid(String [] argv)
    {
        //Prefix Content is: ""

        // Create the RenderWindow, Renderer and both Actors[]
        //[]
        ren1 = vtkRenderer.New();
        renWin = vtkRenderWindow.New();
        renWin.AddRenderer((vtkRenderer)ren1);
        iren = new vtkRenderWindowInteractor();
        iren.SetRenderWindow((vtkRenderWindow)renWin);
        //[]
        // If the current directory is writable, then test the witers[]
        //[]
        try
          {
          channel = new StreamWriter("test.tmp");
          tryCatchError = "NOERROR";
          }
        catch(Exception)
          {
          tryCatchError = "ERROR";
          }
        if(tryCatchError.Equals("NOERROR"))
          {
          channel.Close();
          File.Delete("test.tmp");
          // ====== Structured Grid ======[]
          // First save out a grid in parallel form.[]
          reader = new vtkMultiBlockPLOT3DReader();
          reader.SetXYZFileName((string)"" + (VTK_DATA_ROOT.ToString()) + "/Data/combxyz.bin");
          reader.SetQFileName((string)"" + (VTK_DATA_ROOT.ToString()) + "/Data/combq.bin");
          writer = new vtkPDataSetWriter();
          writer.SetFileName((string)"comb.pvtk");
          writer.SetInputConnection((vtkAlgorithmOutput)reader.GetOutputPort());
          writer.SetNumberOfPieces((int)4);
          writer.Write();
          pReader = new vtkPDataSetReader();
          pReader.SetFileName((string)"comb.pvtk");
          surface = new vtkDataSetSurfaceFilter();
          surface.SetInputConnection((vtkAlgorithmOutput)pReader.GetOutputPort());
          mapper = vtkPolyDataMapper.New();
          mapper.SetInputConnection((vtkAlgorithmOutput)surface.GetOutputPort());
          mapper.SetNumberOfPieces((int)2);
          mapper.SetPiece((int)0);
          mapper.SetGhostLevel((int)1);
          mapper.Update();
          File.Delete("comb.pvtk");
          File.Delete("comb.0.vtk");
          File.Delete("comb.1.vtk");
          File.Delete("comb.2.vtk");
          File.Delete("comb.3.vtk");
          actor = new vtkActor();
          actor.SetMapper((vtkMapper)mapper);
          actor.SetPosition((double)-5,(double)0,(double)-29);
          // Add the actors to the renderer, set the background and size[]
          //[]
          ren1.AddActor((vtkProp)actor);
          // ====== ImageData ======[]
          // First save out a grid in parallel form.[]
          fractal = new vtkImageMandelbrotSource();
          fractal.SetWholeExtent((int)0,(int)9,(int)0,(int)9,(int)0,(int)9);
          fractal.SetSampleCX((double)0.1,(double)0.1,(double)0.1,(double)0.1);
          fractal.SetMaximumNumberOfIterations((ushort)10);
          writer2 = new vtkPDataSetWriter();
          writer.SetFileName((string)"fractal.pvtk");
          writer.SetInputConnection((vtkAlgorithmOutput)fractal.GetOutputPort());
          writer.SetNumberOfPieces((int)4);
          writer.Write();
          pReader2 = new vtkPDataSetReader();
          pReader2.SetFileName((string)"fractal.pvtk");
          iso = new vtkContourFilter();
          iso.SetInputConnection((vtkAlgorithmOutput)pReader2.GetOutputPort());
          iso.SetValue((int)0,(double)4);
          mapper2 = vtkPolyDataMapper.New();
          mapper2.SetInputConnection((vtkAlgorithmOutput)iso.GetOutputPort());
          mapper2.SetNumberOfPieces((int)3);
          mapper2.SetPiece((int)0);
          mapper2.SetGhostLevel((int)0);
          mapper2.Update();
          File.Delete("fractal.pvtk");
          File.Delete("fractal.0.vtk");
          File.Delete("fractal.1.vtk");
          File.Delete("fractal.2.vtk");
          File.Delete("fractal.3.vtk");
          actor2 = new vtkActor();
          actor2.SetMapper((vtkMapper)mapper2);
          actor2.SetScale((double)5,(double)5,(double)5);
          actor2.SetPosition((double)6,(double)6,(double)6);
          // Add the actors to the renderer, set the background and size[]
          //[]
          ren1.AddActor((vtkProp)actor2);
          // ====== PolyData ======[]
          // First save out a grid in parallel form.[]
          sphere = new vtkSphereSource();
          sphere.SetRadius((double)2);
          writer3 = new vtkPDataSetWriter();
          writer3.SetFileName((string)"sphere.pvtk");
          writer3.SetInputConnection((vtkAlgorithmOutput)sphere.GetOutputPort());
          writer3.SetNumberOfPieces((int)4);
          writer3.Write();
          pReader3 = new vtkPDataSetReader();
          pReader3.SetFileName((string)"sphere.pvtk");
          mapper3 = vtkPolyDataMapper.New();
          mapper3.SetInputConnection((vtkAlgorithmOutput)pReader3.GetOutputPort());
          mapper3.SetNumberOfPieces((int)2);
          mapper3.SetPiece((int)0);
          mapper3.SetGhostLevel((int)1);
          mapper3.Update();
          File.Delete("sphere.pvtk");
          File.Delete("sphere.0.vtk");
          File.Delete("sphere.1.vtk");
          File.Delete("sphere.2.vtk");
          File.Delete("sphere.3.vtk");
          actor3 = new vtkActor();
          actor3.SetMapper((vtkMapper)mapper3);
          actor3.SetPosition((double)6,(double)6,(double)6);
          // Add the actors to the renderer, set the background and size[]
          //[]
          ren1.AddActor((vtkProp)actor3);
          }

        ren1.SetBackground((double)0.1,(double)0.2,(double)0.4);
        renWin.SetSize((int)300,(int)300);
        // render the image[]
        //[]
        cam1 = ren1.GetActiveCamera();
        cam1.Azimuth((double)20);
        cam1.Elevation((double)40);
        ren1.ResetCamera();
        cam1.Zoom((double)1.2);
        iren.Initialize();
        // prevent the tk window from showing up then start the event loop[]

        //deleteAllVTKObjects();
    }
        public CrossPackage(double size, vtkRenderer renderer)
        {
            //Console.WriteLine(string.Format("VTP文件 = {0}", vtpFile));
            vtkPolyData cursorPolyData = vtkPolyData.New();

            vtkPoints cursorPts = vtkPoints.New();

            cursorPts.SetNumberOfPoints(12);

            cursorPts.SetPoint(0, -size, -0, 0);
            cursorPts.SetPoint(1, size, -0, 0);
            cursorPts.SetPoint(2, size, 0, 0);
            cursorPts.SetPoint(3, -size, 0, 0);

            cursorPts.SetPoint(4, -0, size, 0);
            cursorPts.SetPoint(5, 0, size, 0);
            cursorPts.SetPoint(6, 0, -size, 0);
            cursorPts.SetPoint(7, -0, -size, 0);

            cursorPts.SetPoint(8, -0, 0, size);
            cursorPts.SetPoint(9, 0, 0, size);
            cursorPts.SetPoint(10, 0, 0, -size);
            cursorPts.SetPoint(11, -0, 0, -size);

            vtkCellArray cells = vtkCellArray.New();

            cells.InsertNextCell(3, VTKUtil.ConvertIntPtr(new long[] { 0, 1, 2 }));
            cells.InsertNextCell(3, VTKUtil.ConvertIntPtr(new long[] { 1, 2, 3 }));
            cells.InsertNextCell(3, VTKUtil.ConvertIntPtr(new long[] { 4, 5, 6 }));
            cells.InsertNextCell(3, VTKUtil.ConvertIntPtr(new long[] { 5, 6, 7 }));
            cells.InsertNextCell(3, VTKUtil.ConvertIntPtr(new long[] { 8, 9, 10 }));
            cells.InsertNextCell(3, VTKUtil.ConvertIntPtr(new long[] { 9, 10, 11 }));

            vtkCellArray lines = vtkCellArray.New();

            lines.InsertNextCell(2, VTKUtil.ConvertIntPtr(new long[] { 0, 1 }));
            lines.InsertNextCell(2, VTKUtil.ConvertIntPtr(new long[] { 6, 5 }));
            lines.InsertNextCell(2, VTKUtil.ConvertIntPtr(new long[] { 10, 9 }));

            cursorPolyData.SetPoints(cursorPts);
            cursorPolyData.SetVerts(cells);
            //CursorPolyData.SetPolys(cells);
            cursorPolyData.SetLines(lines);
            cursorPolyData.Update();

            PolyData = cursorPolyData;

            _polyDataMapper = vtkPolyDataMapper.New();
            _polyDataMapper.ScalarVisibilityOff();
            _polyDataMapper.SetInput(PolyData);
            _polyDataMapper.Update();

            actor = vtkActor.New();
            actor.SetMapper(_polyDataMapper);

            //marchingCubeActor.GetProperty().BackfaceCullingOn();
            //marchingCubeActor.GetProperty().FrontfaceCullingOn();
            //marchingCubeActor.GetProperty().BackfaceCullingOff();

            // now, tell the renderer our actors
            renderer.AddActor(actor);

            this._renderer = renderer;
        }