Exemplo n.º 1
0
 public BoxUnit(string name) : base(name)
 {
     UpdateBox();
     mapper.SetInputData(polyData);
     actor.SetMapper(mapper);
     Center.OnPropertyChanged += OnCenterChanged;
 }
Exemplo n.º 2
0
        private void ImplicitBoolean()
        {
            vtkSphere sphere1 = vtkSphere.New();

            sphere1.SetCenter(.9, 0, 0);
            vtkSphere sphere2 = vtkSphere.New();

            sphere2.SetCenter(-.9, 0, 0);

            vtkImplicitBoolean implicitBoolean = vtkImplicitBoolean.New();

            implicitBoolean.AddFunction(sphere1);
            implicitBoolean.AddFunction(sphere2);
            implicitBoolean.SetOperationTypeToUnion();
            //implicitBoolean.SetOperationTypeToIntersection();

            // Sample the function
            vtkSampleFunction sample = vtkSampleFunction.New();

            sample.SetSampleDimensions(50, 50, 50);
            sample.SetImplicitFunction(implicitBoolean);
            double value = 3.0;
            double xmin = -value, xmax = value,
                   ymin = -value, ymax = value,
                   zmin = -value, zmax = value;

            sample.SetModelBounds(xmin, xmax, ymin, ymax, zmin, zmax);

            // Create the 0 isosurface
            vtkContourFilter contours       = vtkContourFilter.New();

#if VTK_MAJOR_VERSION_5
            contours.SetInputConnection(sample.GetOutputPort());
#else
            contours.SetInputData(sample);
#endif
            contours.GenerateValues(1, 1, 1);

            // Map the contours to graphical primitives
            vtkPolyDataMapper contourMapper = vtkPolyDataMapper.New();
#if VTK_MAJOR_VERSION_5
            contourMapper.SetInputConnection(contours.GetOutputPort());
#else
            contourMapper.SetInputData(contours);
#endif
            contourMapper.ScalarVisibilityOff();

            // Create an actor for the contours
            vtkActor contourActor           = vtkActor.New();
            contourActor.SetMapper(contourMapper);
            // 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, .3, .4);
            renderer.AddActor(contourActor);
        }
Exemplo n.º 3
0
        private void DrawTriangle()
        {
            //创建点数据
            vtkPoints points = vtkPoints.New();

            points.InsertNextPoint(1.0, 0.0, 0.0);
            points.InsertNextPoint(0.0, 1.0, 0.0);
            points.InsertNextPoint(0.0, 0.0, 0.0);

            //每两个坐标之间分别创建一条线
            //SetId()的第一个参数是线段的端点ID,第二参数是连接的的点的ID
            vtkLine line0 = vtkLine.New();

            line0.GetPointIds().SetId(0, 0);
            line0.GetPointIds().SetId(1, 1);

            vtkLine line1 = vtkLine.New();

            line1.GetPointIds().SetId(0, 1);
            line1.GetPointIds().SetId(1, 2);

            vtkLine line2 = vtkLine.New();

            line2.GetPointIds().SetId(0, 2);
            line2.GetPointIds().SetId(1, 0);

            //创建单元数组,用于存储以上创建的线段
            vtkCellArray lines = vtkCellArray.New();

            lines.InsertNextCell(line0);
            lines.InsertNextCell(line1);
            lines.InsertNextCell(line2);

            //将点和线加入数据集中,前者定义数据集的几何结构,后者定义拓扑结构
            //创建vtkPolyData类型的数据,是一种数据集
            vtkPolyData polyData = vtkPolyData.New();

            //将创建的点数据加入vtkPolyData数据里
            polyData.SetPoints(points); //点数据定义了polydata数据集的几何结构。
            polyData.SetLines(lines);   //定义拓扑结构

            //显示数据
            vtkPolyDataMapper mapper = vtkPolyDataMapper.New();

            mapper.SetInputData(polyData);
            vtkActor actor = vtkActor.New();

            actor.SetMapper(mapper);
            actor.GetProperty().SetColor(1.0, 0.0, 0.0);
            vtkRenderWindow renWin   = myRenderWindowControl.RenderWindow;
            vtkRenderer     renderer = renWin.GetRenderers().GetFirstRenderer();

            renderer.SetBackground(1.0, 1.0, 1.0);
            renderer.AddActor(actor);
        }
Exemplo n.º 4
0
        private void MarchingCubes()
        {
            vtkSphereSource sphereSource = vtkSphereSource.New();

            sphereSource.SetPhiResolution(20);
            sphereSource.SetThetaResolution(20);
            sphereSource.Update();

            double[] bounds = sphereSource.GetOutput().GetBounds();
            for (int i = 0; i < 6; i += 2)
            {
                double range = bounds[i + 1] - bounds[i];
                bounds[i]     = bounds[i] - .1 * range;
                bounds[i + 1] = bounds[i + 1] + .1 * range;
            }
            vtkVoxelModeller voxelModeller = vtkVoxelModeller.New();

            voxelModeller.SetSampleDimensions(50, 50, 50);
            voxelModeller.SetModelBounds(bounds[0], bounds[1], bounds[2], bounds[3], bounds[4], bounds[5]);
            voxelModeller.SetScalarTypeToFloat();
            voxelModeller.SetMaximumDistance(.1);

#if VTK_MAJOR_VERSION_5
            voxelModeller.SetInputConnection(sphereSource.GetOutputPort());
#else
            voxelModeller.SetInputData(sphereSource);
#endif
            vtkMarchingCubes surface = vtkMarchingCubes.New();

#if VTK_MAJOR_VERSION_5
            surface.SetInputConnection(voxelModeller.GetOutputPort());
#else
            surface.SetInputData(voxelModeller);
#endif
            surface.ComputeNormalsOn();
            surface.SetValue(0, 0.5);
            vtkPolyDataMapper mapper = vtkPolyDataMapper.New();
#if VTK_MAJOR_VERSION_5
            mapper.SetInputConnection(surface.GetOutputPort());
#else
            mapper.SetInputData(surface);
#endif
            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(.2, .3, .4);
            // add our actor to the renderer
            renderer.AddActor(actor);
        }
Exemplo n.º 5
0
        private void ExtractEdges()
        {
            vtkSphereSource sphereSource = vtkSphereSource.New();

            sphereSource.Update();

            Debug.WriteLine("Sphere" + Environment.NewLine + "----------");
            Debug.WriteLine("There are " + sphereSource.GetOutput().GetNumberOfCells() + " cells.");
            Debug.WriteLine("There are " + sphereSource.GetOutput().GetNumberOfPoints() + " points.");

            vtkExtractEdges extractEdges = vtkExtractEdges.New();

#if VTK_MAJOR_VERSION_5
            extractEdges.SetInputConnection(sphereSource.GetOutputPort());
#else
            extractEdges.SetInputData(sphereSource);
#endif
            extractEdges.Update();

            vtkCellArray lines  = extractEdges.GetOutput().GetLines();
            vtkPoints    points = extractEdges.GetOutput().GetPoints();

            Debug.WriteLine(Environment.NewLine + "Edges" + Environment.NewLine + "----------");
            Debug.WriteLine("There are " + lines.GetNumberOfCells() + " cells.");
            Debug.WriteLine("There are " + points.GetNumberOfPoints() + " points.");

            // Traverse all of the edges
            for (int i = 0; i < extractEdges.GetOutput().GetNumberOfCells(); i++)
            {
                //Debug.WriteLine("Type: " + extractEdges.GetOutput().GetCell(i).GetClassName() );
                vtkLine line = vtkLine.SafeDownCast(extractEdges.GetOutput().GetCell(i));
                Debug.WriteLine("Line " + i + " : " + line);
            }

            // Visualize the edges

            // Create a mapper and actor
            vtkPolyDataMapper mapper = vtkPolyDataMapper.New();
#if VTK_MAJOR_VERSION_5
            mapper.SetInputConnection(extractEdges.GetOutputPort());
#else
            mapper.SetInputData(extractEdges);
#endif
            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(1, 1, 1);
            // add our actor to the renderer
            renderer.AddActor(actor);
        }
Exemplo n.º 6
0
        private void DrawPoint()
        {
            // Create the geometry of the points (the coordinate)
            vtkPoints points = vtkPoints.New();

            double[,] p = new double[, ]
            {
                { 1.0, 2.0, 3.0 },
                { 3.0, 1.0, 2.0 },
                { 2.0, 3.0, 1.0 },
                { 1.0, 3.0, 3.0 }
            };

            // Create topology of the points (a vertex per point)
            vtkCellArray vertices = vtkCellArray.New();
            int          nPts     = 4;

            int[] ids = new int[nPts];
            for (int i = 0; i < nPts; i++)
            {
                ids[i] = (int)points.InsertNextPoint(p[i, 0], p[i, 1], p[i, 2]);
            }

            int    size = Marshal.SizeOf(typeof(int)) * nPts;
            IntPtr pIds = Marshal.AllocHGlobal(size);

            Marshal.Copy(ids, 0, pIds, nPts);
            vertices.InsertNextCell(nPts, pIds);
            Marshal.FreeHGlobal(pIds);

            // Create a polydata object
            vtkPolyData pointPoly = vtkPolyData.New();

            // Set the points and vertices we created as the geometry and topology of the polydata
            pointPoly.SetPoints(points);
            pointPoly.SetVerts(vertices);

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

            mapper.SetInputData(pointPoly);

            vtkActor actor = vtkActor.New();

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

            renderer.SetBackground(0.3, 0.2, 0.1);
            renderer.AddActor(actor);
        }
Exemplo n.º 7
0
        private void SolidClip()
        {
            // Create a superquadric
            vtkSuperquadricSource superquadricSource = vtkSuperquadricSource.New();

            superquadricSource.SetPhiRoundness(3.1);
            superquadricSource.SetThetaRoundness(2.2);

            // Define a clipping plane
            vtkPlane clipPlane = vtkPlane.New();

            clipPlane.SetNormal(1.0, -1.0, -1.0);
            clipPlane.SetOrigin(0.0, 0.0, 0.0);

            // Clip the source with the plane
            vtkClipPolyData clipper = vtkClipPolyData.New();

#if VTK_MAJOR_VERSION_5
            clipper.SetInputConnection(superquadricSource.GetOutputPort());
#else
            clipper.SetInputData(superquadricSource);
#endif
            clipper.SetClipFunction(clipPlane);

            //Create a mapper and actor
            vtkPolyDataMapper superquadricMapper = vtkPolyDataMapper.New();
#if VTK_MAJOR_VERSION_5
            superquadricMapper.SetInputConnection(clipper.GetOutputPort());
#else
            superquadricMapper.SetInputData(clipper);
#endif
            vtkActor superquadricActor = vtkActor.New();
            superquadricActor.SetMapper(superquadricMapper);

            // Create a property to be used for the back faces. Turn off all
            // shading by specifying 0 weights for specular and diffuse. Max the
            // ambient.
            vtkProperty backFaces = vtkProperty.New();
            backFaces.SetSpecular(0.0);
            backFaces.SetDiffuse(0.0);
            backFaces.SetAmbient(1.0);
            backFaces.SetAmbientColor(1.0000, 0.3882, 0.2784);

            superquadricActor.SetBackfaceProperty(backFaces);
            // get a reference to the renderwindow of our renderWindowControl1
            vtkRenderWindow renderWindow = renderWindowControl1.RenderWindow;
            // renderer
            vtkRenderer renderer = renderWindow.GetRenderers().GetFirstRenderer();
            // add our actor to the renderer
            renderer.AddActor(superquadricActor);
        }
Exemplo n.º 8
0
        private void BoundaryEdges()
        {
            vtkDiskSource diskSource = vtkDiskSource.New();

            diskSource.Update();

            vtkFeatureEdges featureEdges = vtkFeatureEdges.New();

#if VTK_MAJOR_VERSION_5
            featureEdges.SetInputConnection(diskSource.GetOutputPort());
#else
            featureEdges.SetInputData(diskSource);
#endif
            featureEdges.BoundaryEdgesOn();
            featureEdges.FeatureEdgesOff();
            featureEdges.ManifoldEdgesOff();
            featureEdges.NonManifoldEdgesOff();
            featureEdges.Update();

            // Visualize
            vtkPolyDataMapper edgeMapper = vtkPolyDataMapper.New();
#if VTK_MAJOR_VERSION_5
            edgeMapper.SetInputConnection(featureEdges.GetOutputPort());
#else
            edgeMapper.SetInputData(featureEdges);
#endif
            vtkActor edgeActor = vtkActor.New();
            edgeActor.GetProperty().SetLineWidth(3);
            edgeActor.SetMapper(edgeMapper);

            vtkPolyDataMapper diskMapper = vtkPolyDataMapper.New();
#if VTK_MAJOR_VERSION_5
            diskMapper.SetInputConnection(diskSource.GetOutputPort());
#else
            diskMapper.SetInputData(diskSource);
#endif
            vtkActor diskActor = vtkActor.New();
            diskActor.SetMapper(diskMapper);

            // 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(diskActor);
            renderer.AddActor(edgeActor);
        }
Exemplo n.º 9
0
        private void IsoContours()
        {
            vtkImageData data = vtkImageData.New();

            CreateData(ref data);

            // Create an isosurface
            _ContourFilter = vtkContourFilter.New();
#if VTK_MAJOR_VERSION_5
            _ContourFilter.SetInput(data);
#else
            contourFilter.SetInputData(data);
#endif
            _ContourFilter.GenerateValues(1, 10, 10); // (numContours, rangeStart, rangeEnd)

            // Map the contours to graphical primitives
            vtkPolyDataMapper contourMapper = vtkPolyDataMapper.New();
#if VTK_MAJOR_VERSION_5
            contourMapper.SetInputConnection(_ContourFilter.GetOutputPort());
#else
            contourMapper.SetInputData(contourFilter);
#endif
            // Create an actor for the contours
            vtkActor contourActor = vtkActor.New();
            contourActor.SetMapper(contourMapper);

            // Create the outline
            vtkOutlineFilter  outlineFilter = vtkOutlineFilter.New();
            vtkPolyDataMapper outlineMapper = vtkPolyDataMapper.New();
#if VTK_MAJOR_VERSION_5
            outlineFilter.SetInput(data);
            outlineMapper.SetInputConnection(outlineFilter.GetOutputPort());
#else
            outlineFilter.SetInputData(data);
            outlineMapper.SetInputData(outlineFilter);
#endif
            vtkActor outlineActor = vtkActor.New();
            outlineActor.SetMapper(outlineMapper);

            // get a reference to the renderwindow of our renderWindowControl1
            _RenderWindow = renderWindowControl1.RenderWindow;
            // renderer
            vtkRenderer renderer = _RenderWindow.GetRenderers().GetFirstRenderer();
            // set background color
            renderer.SetBackground(.2, .3, .4);
            // add our actor to the renderer
            renderer.AddActor(contourActor);
            renderer.AddActor(outlineActor);
        }
Exemplo n.º 10
0
    /// <summary>
    /// The main entry method called by the CSharp driver
    /// </summary>
    /// <param name="argv"></param>
    public static void AVmergeFilter(String [] argv)
    {
        //Prefix Content is: ""

          // Create the RenderWindow, Renderer and both Actors[]
          //[]
          ren1 = vtkRenderer.New();
          ren2 = vtkRenderer.New();
          renWin = vtkRenderWindow.New();
          renWin.SetMultiSamples(0);
          renWin.AddRenderer((vtkRenderer)ren1);
          renWin.AddRenderer((vtkRenderer)ren2);
          iren = new vtkRenderWindowInteractor();
          iren.SetRenderWindow((vtkRenderWindow)renWin);
          // create pipeline[]
          //[]
          pl3d = new vtkMultiBlockPLOT3DReader();
          pl3d.SetXYZFileName((string)"" + (VTK_DATA_ROOT.ToString()) + "/Data/combxyz.bin");
          pl3d.SetQFileName((string)"" + (VTK_DATA_ROOT.ToString()) + "/Data/combq.bin");
          pl3d.SetScalarFunctionNumber((int)110);
          pl3d.SetVectorFunctionNumber((int)202);
          pl3d.Update();
          probeLine = new vtkLineSource();
          probeLine.SetPoint1((double)1,(double)1,(double)29);
          probeLine.SetPoint2((double)16.5,(double)5,(double)31.7693);
          probeLine.SetResolution((int)500);
          probe = new vtkProbeFilter();
          probe.SetInputConnection((vtkAlgorithmOutput)probeLine.GetOutputPort());
          probe.SetSourceData((vtkDataObject)pl3d.GetOutput().GetBlock(0));
          probe.Update();
          probeTube = new vtkTubeFilter();
          probeTube.SetInputData((vtkDataObject)probe.GetPolyDataOutput());
          probeTube.SetNumberOfSides((int)5);
          probeTube.SetRadius((double).05);
          probeMapper = vtkPolyDataMapper.New();
          probeMapper.SetInputConnection((vtkAlgorithmOutput)probeTube.GetOutputPort());
          probeMapper.SetScalarRange((double)((vtkDataSet)pl3d.GetOutput().GetBlock(0)).GetScalarRange()[0],
          (double)((vtkDataSet)pl3d.GetOutput().GetBlock(0)).GetScalarRange()[1]);
          probeActor = new vtkActor();
          probeActor.SetMapper((vtkMapper)probeMapper);
          displayLine = new vtkLineSource();
          displayLine.SetPoint1((double)0,(double)0,(double)0);
          displayLine.SetPoint2((double)1,(double)0,(double)0);
          displayLine.SetResolution((int)probeLine.GetResolution());
          displayMerge = new vtkMergeFilter();
          displayMerge.SetGeometryConnection(displayLine.GetOutputPort());
          displayMerge.SetScalarsData((vtkDataSet)probe.GetPolyDataOutput());
          displayMerge.Update();

          displayWarp = new vtkWarpScalar();
          displayWarp.SetInputData((vtkDataObject)displayMerge.GetPolyDataOutput());
          displayWarp.SetNormal((double)0,(double)1,(double)0);
          displayWarp.SetScaleFactor((double).000001);
          displayWarp.Update();

          displayMapper = vtkPolyDataMapper.New();
          displayMapper.SetInputData((vtkPolyData)displayWarp.GetPolyDataOutput());
          displayMapper.SetScalarRange((double)((vtkDataSet)pl3d.GetOutput().GetBlock(0)).GetScalarRange()[0],
          (double)((vtkDataSet)pl3d.GetOutput().GetBlock(0)).GetScalarRange()[1]);
          displayActor = new vtkActor();
          displayActor.SetMapper((vtkMapper)displayMapper);
          outline = new vtkStructuredGridOutlineFilter();
          outline.SetInputData(pl3d.GetOutput().GetBlock(0));
          outlineMapper = vtkPolyDataMapper.New();
          outlineMapper.SetInputConnection((vtkAlgorithmOutput)outline.GetOutputPort());
          outlineActor = new vtkActor();
          outlineActor.SetMapper((vtkMapper)outlineMapper);
          outlineActor.GetProperty().SetColor((double)0,(double)0,(double)0);
          ren1.AddActor((vtkProp)outlineActor);
          ren1.AddActor((vtkProp)probeActor);
          ren1.SetBackground((double)1,(double)1,(double)1);
          ren1.SetViewport((double)0,(double).25,(double)1,(double)1);
          ren2.AddActor((vtkProp)displayActor);
          ren2.SetBackground((double)0,(double)0,(double)0);
          ren2.SetViewport((double)0,(double)0,(double)1,(double).25);
          renWin.SetSize((int)300,(int)300);
          ren1.ResetCamera();
          cam1 = ren1.GetActiveCamera();
          cam1.SetClippingRange((double)3.95297,(double)50);
          cam1.SetFocalPoint((double)8.88908,(double)0.595038,(double)29.3342);
          cam1.SetPosition((double)9.9,(double)-26,(double)41);
          cam1.SetViewUp((double)0.060772,(double)-0.319905,(double)0.945498);
          ren2.ResetCamera();
          cam2 = ren2.GetActiveCamera();
          cam2.ParallelProjectionOn();
          cam2.SetParallelScale((double).15);
          iren.Initialize();
          // render the image[]
          //[]
          // prevent the tk window from showing up then start the event loop[]

        //deleteAllVTKObjects();
    }
Exemplo n.º 11
0
        private void WarpVector()
        {
            vtkPoints points = vtkPoints.New();

            points.InsertNextPoint(0.0, 0.0, 0.0);
            points.InsertNextPoint(1.0, 0.0, 0.0);
            points.InsertNextPoint(2.0, 0.0, 0.0);
            points.InsertNextPoint(3.0, 0.0, 0.0);
            points.InsertNextPoint(4.0, 0.0, 0.0);

            vtkCellArray lines = vtkCellArray.New();
            vtkLine      line  = vtkLine.New();

            line.GetPointIds().SetId(0, 0);
            line.GetPointIds().SetId(1, 1);
            lines.InsertNextCell(line);
            line.GetPointIds().SetId(0, 1);
            line.GetPointIds().SetId(1, 2);
            lines.InsertNextCell(line);
            line.GetPointIds().SetId(0, 2);
            line.GetPointIds().SetId(1, 3);
            lines.InsertNextCell(line);
            line.GetPointIds().SetId(0, 3);
            line.GetPointIds().SetId(1, 4);
            lines.InsertNextCell(line);

            vtkDoubleArray warpData = vtkDoubleArray.New();

            warpData.SetNumberOfComponents(3);
            warpData.SetName("warpData");
            double[] warp = new double[] { 0.0, 0.0, 0.0 };
            warp[1] = 0.0;
            warpData.InsertNextTuple3(warp[0], warp[1], warp[2]);
            warp[1] = 0.1;
            warpData.InsertNextTuple3(warp[0], warp[1], warp[2]);
            warp[1] = 0.3;
            warpData.InsertNextTuple3(warp[0], warp[1], warp[2]);
            warp[1] = 0.0;
            warpData.InsertNextTuple3(warp[0], warp[1], warp[2]);
            warp[1] = 0.1;
            warpData.InsertNextTuple3(warp[0], warp[1], warp[2]);

            vtkPolyData polydata = vtkPolyData.New();

            polydata.SetPoints(points);
            polydata.SetLines(lines);
            polydata.GetPointData().AddArray(warpData);
            polydata.GetPointData().SetActiveVectors(warpData.GetName());

            //WarpVector will use the array marked as active vector in polydata
            //it has to be a 3 component array
            //with the same number of tuples as points in polydata
            vtkWarpVector warpVector = vtkWarpVector.New();

#if VTK_MAJOR_VERSION_5
            warpVector.SetInput(polydata);
#else
            warpVector.SetInputData(polydata);
#endif
            warpVector.Update();

            vtkPolyDataMapper mapper = vtkPolyDataMapper.New();
#if VTK_MAJOR_VERSION_5
            mapper.SetInput(warpVector.GetPolyDataOutput());
#else
            mapper.SetInputData(warpVector.GetPolyDataOutput());
#endif
            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(.2, .6, .3);
            renderer.AddActor(actor);
        }
Exemplo n.º 12
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.º 13
0
        private void QuadricDecimation()
        {
            vtkSphereSource sphereSource = vtkSphereSource.New();

            sphereSource.Update();

            vtkPolyData input = vtkPolyData.New();

            input.ShallowCopy(sphereSource.GetOutput());

            Debug.WriteLine("Before decimation" + Environment.NewLine + "------------");
            Debug.WriteLine("There are " + input.GetNumberOfPoints() + " points.");
            Debug.WriteLine("There are " + input.GetNumberOfPolys() + " polygons.");

            vtkQuadricDecimation decimate = vtkQuadricDecimation.New();

#if VTK_MAJOR_VERSION_5
            decimate.SetInputConnection(input.GetProducerPort());
#else
            decimate.SetInputData(input);
#endif
            decimate.Update();

            vtkPolyData decimated = vtkPolyData.New();
            decimated.ShallowCopy(decimate.GetOutput());

            Debug.WriteLine("After decimation" + Environment.NewLine + "------------");

            Debug.WriteLine("There are " + decimated.GetNumberOfPoints() + " points.");
            Debug.WriteLine("There are " + decimated.GetNumberOfPolys() + " polygons.");

            vtkPolyDataMapper inputMapper = vtkPolyDataMapper.New();
#if VTK_MAJOR_VERSION_5
            inputMapper.SetInputConnection(input.GetProducerPort());
#else
            inputMapper.SetInputData(input);
#endif
            vtkActor inputActor = vtkActor.New();
            inputActor.SetMapper(inputMapper);

            vtkPolyDataMapper decimatedMapper = vtkPolyDataMapper.New();
#if VTK_MAJOR_VERSION_5
            decimatedMapper.SetInputConnection(decimated.GetProducerPort());
#else
            decimatedMapper.SetInputData(decimated);
#endif
            vtkActor decimatedActor = vtkActor.New();
            decimatedActor.SetMapper(decimatedMapper);

            vtkRenderWindow renderWindow = renderWindowControl1.RenderWindow;
            this.Size = new System.Drawing.Size(612, 352);

            // Define viewport ranges
            // (xmin, ymin, xmax, ymax)
            double[] leftViewport  = new double[] { 0.0, 0.0, 0.5, 1.0 };
            double[] rightViewport = new double[] { 0.5, 0.0, 1.0, 1.0 };

            // Setup both renderers
            vtkRenderer leftRenderer = vtkRenderer.New();
            renderWindow.AddRenderer(leftRenderer);
            leftRenderer.SetViewport(leftViewport[0], leftViewport[1], leftViewport[2], leftViewport[3]);
            leftRenderer.SetBackground(.6, .5, .4);

            vtkRenderer rightRenderer = vtkRenderer.New();
            renderWindow.AddRenderer(rightRenderer);
            rightRenderer.SetViewport(rightViewport[0], rightViewport[1], rightViewport[2], rightViewport[3]);
            rightRenderer.SetBackground(.4, .5, .6);

            // Add the sphere to the left and the cube to the right
            leftRenderer.AddActor(inputActor);
            rightRenderer.AddActor(decimatedActor);
            leftRenderer.ResetCamera();
            rightRenderer.ResetCamera();
            renderWindow.Render();
        }
Exemplo n.º 14
0
        private void ElevationFilter()
        {
            // Created a grid of points (heigh/terrian map)
            vtkPoints points = vtkPoints.New();

            uint GridSize = 10;

            for (uint x = 0; x < GridSize; x++)
            {
                for (uint y = 0; y < GridSize; y++)
                {
                    points.InsertNextPoint(x, y, (x + y) / (y + 1));
                }
            }
            double[] bounds = points.GetBounds();

            // Add the grid points to a polydata object
            vtkPolyData inputPolyData = vtkPolyData.New();

            inputPolyData.SetPoints(points);

            // Triangulate the grid points
            vtkDelaunay2D delaunay = vtkDelaunay2D.New();

#if VTK_MAJOR_VERSION_5
            delaunay.SetInput(inputPolyData);
#else
            delaunay.SetInputData(inputPolyData);
#endif
            delaunay.Update();

            vtkElevationFilter elevationFilter = vtkElevationFilter.New();
            elevationFilter.SetInputConnection(delaunay.GetOutputPort());
            elevationFilter.SetLowPoint(0.0, 0.0, bounds[4]);
            elevationFilter.SetHighPoint(0.0, 0.0, bounds[5]);
            elevationFilter.Update();

            vtkPolyData output = vtkPolyData.New();
            output.ShallowCopy(vtkPolyData.SafeDownCast(elevationFilter.GetOutput()));

            vtkFloatArray elevation =
                vtkFloatArray.SafeDownCast(output.GetPointData().GetArray("Elevation"));

            // Create the color map
            vtkLookupTable colorLookupTable = vtkLookupTable.New();
            colorLookupTable.SetTableRange(bounds[4], bounds[5]);
            colorLookupTable.Build();

            // Generate the colors for each point based on the color map
            vtkUnsignedCharArray colors = vtkUnsignedCharArray.New();
            colors.SetNumberOfComponents(3);
            colors.SetName("Colors");

            for (int i = 0; i < output.GetNumberOfPoints(); i++)
            {
                double val = elevation.GetValue(i);
                Debug.WriteLine("val: " + val);

                double[] dcolor = colorLookupTable.GetColor(val);
                //Debug.WriteLine("dcolor: "
                //          + dcolor[0] + " "
                //          + dcolor[1] + " "
                //          + dcolor[2]);
                byte[] color = new byte[3];
                for (int j = 0; j < 3; j++)
                {
                    color[j] = (byte)(255 * dcolor[j]);
                }
                //Debug.WriteLine("color: "
                //          + color[0] + " "
                //          + color[1] + " "
                //          + color[2]);

                colors.InsertNextTuple3(color[0], color[1], color[2]);
            }

            output.GetPointData().AddArray(colors);

            // Visualize
            vtkPolyDataMapper mapper = vtkPolyDataMapper.New();
#if VTK_MAJOR_VERSION_5
            mapper.SetInputConnection(output.GetProducerPort());
#else
            mapper.SetInputData(output);
#endif

            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.º 15
0
        private void Triangulate()
        {
            vtkRegularPolygonSource polygonSource = vtkRegularPolygonSource.New();

            polygonSource.Update();

            vtkTriangleFilter triangleFilter = vtkTriangleFilter.New();

#if VTK_MAJOR_VERSION_5
            triangleFilter.SetInputConnection(polygonSource.GetOutputPort());
#else
            triangleFilter.SetInputData(polygonSource);
#endif
            triangleFilter.Update();

            vtkPolyDataMapper inputMapper = vtkPolyDataMapper.New();
#if VTK_MAJOR_VERSION_5
            inputMapper.SetInputConnection(polygonSource.GetOutputPort());
#else
            inputMapper.SetInputData(polygonSource);
#endif
            vtkActor inputActor = vtkActor.New();
            inputActor.SetMapper(inputMapper);
            inputActor.GetProperty().SetRepresentationToWireframe();

            vtkPolyDataMapper triangleMapper = vtkPolyDataMapper.New();
#if VTK_MAJOR_VERSION_5
            triangleMapper.SetInputConnection(triangleFilter.GetOutputPort());
#else
            triangleMapper.SetInputData(triangleFilter);
#endif
            vtkActor triangleActor = vtkActor.New();
            triangleActor.SetMapper(triangleMapper);
            triangleActor.GetProperty().SetRepresentationToWireframe();

            vtkRenderWindow renderWindow = renderWindowControl1.RenderWindow;
            this.Size = new System.Drawing.Size(612, 352);

            // Define viewport ranges
            // (xmin, ymin, xmax, ymax)
            double[] leftViewport  = new double[] { 0.0, 0.0, 0.5, 1.0 };
            double[] rightViewport = new double[] { 0.5, 0.0, 1.0, 1.0 };

            // Setup both renderers
            vtkRenderer leftRenderer = vtkRenderer.New();
            renderWindow.AddRenderer(leftRenderer);
            leftRenderer.SetViewport(leftViewport[0], leftViewport[1], leftViewport[2], leftViewport[3]);
            leftRenderer.SetBackground(.6, .5, .4);

            vtkRenderer rightRenderer = vtkRenderer.New();
            renderWindow.AddRenderer(rightRenderer);
            rightRenderer.SetViewport(rightViewport[0], rightViewport[1], rightViewport[2], rightViewport[3]);
            rightRenderer.SetBackground(.4, .5, .6);

            // Add the sphere to the left and the cube to the right
            leftRenderer.AddActor(inputActor);
            rightRenderer.AddActor(triangleActor);
            leftRenderer.ResetCamera();
            rightRenderer.ResetCamera();
            renderWindow.Render();
        }
Exemplo n.º 16
0
        private void CapClip(string filePath)
        {
            // PolyData to process
            vtkPolyData polyData;

            if (filePath != null)
            {
                vtkXMLPolyDataReader reader = vtkXMLPolyDataReader.New();
                reader.SetFileName(filePath);
                reader.Update();
                polyData = reader.GetOutput();
            }
            else
            {
                // Create a sphere
                vtkSphereSource sphereSource = vtkSphereSource.New();
                sphereSource.SetThetaResolution(20);
                sphereSource.SetPhiResolution(11);

                vtkPlane plane = vtkPlane.New();
                plane.SetOrigin(0, 0, 0);
                plane.SetNormal(1.0, -1.0, -1.0);

                vtkClipPolyData clipper = vtkClipPolyData.New();
                clipper.SetInputConnection(sphereSource.GetOutputPort());
                clipper.SetClipFunction(plane);
                clipper.SetValue(0);
                clipper.Update();

                polyData = clipper.GetOutput();
            }

            vtkDataSetMapper clipMapper = vtkDataSetMapper.New();

#if VTK_MAJOR_VERSION_5
            clipMapper.SetInput(polyData);
#else
            clipMapper.SetInputData(polyData);
#endif

            vtkActor clipActor = vtkActor.New();
            clipActor.SetMapper(clipMapper);
            clipActor.GetProperty().SetColor(1.0000, 0.3882, 0.2784);
            clipActor.GetProperty().SetInterpolationToFlat();

            // Now extract feature edges
            vtkFeatureEdges boundaryEdges = vtkFeatureEdges.New();
#if VTK_MAJOR_VERSION_5
            boundaryEdges.SetInput(polyData);
#else
            boundaryEdges.SetInputData(polyData);
#endif
            boundaryEdges.BoundaryEdgesOn();
            boundaryEdges.FeatureEdgesOff();
            boundaryEdges.NonManifoldEdgesOff();
            boundaryEdges.ManifoldEdgesOff();

            vtkStripper boundaryStrips = vtkStripper.New();
            boundaryStrips.SetInputConnection(boundaryEdges.GetOutputPort());
            boundaryStrips.Update();

            // Change the polylines into polygons
            vtkPolyData boundaryPoly = vtkPolyData.New();
            boundaryPoly.SetPoints(boundaryStrips.GetOutput().GetPoints());
            boundaryPoly.SetPolys(boundaryStrips.GetOutput().GetLines());

            vtkPolyDataMapper boundaryMapper = vtkPolyDataMapper.New();
#if VTK_MAJOR_VERSION_5
            boundaryMapper.SetInput(boundaryPoly);
#else
            boundaryMapper.SetInputData(boundaryPoly);
#endif

            vtkActor boundaryActor = vtkActor.New();
            boundaryActor.SetMapper(boundaryMapper);
            boundaryActor.GetProperty().SetColor(0.8900, 0.8100, 0.3400);
            // 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, .3, .4);
            // add our actor to the renderer
            renderer.AddActor(clipActor);
            renderer.AddActor(boundaryActor);
            // Generate an interesting view
            //
            renderer.ResetCamera();
            renderer.GetActiveCamera().Azimuth(30);
            renderer.GetActiveCamera().Elevation(30);
            renderer.GetActiveCamera().Dolly(1.2);
            renderer.ResetCameraClippingRange();
        }
Exemplo n.º 17
0
        private void SampleFunction()
        {
            vtkSphere sphere = vtkSphere.New();

            // Sample the function
            vtkSampleFunction sample = vtkSampleFunction.New();

            sample.SetSampleDimensions(50, 50, 50);
            sample.SetImplicitFunction(sphere);
            double value = 2.0;
            double xmin = -value, xmax = value, ymin = -value, ymax = value, zmin = -value, zmax = value;

            sample.SetModelBounds(xmin, xmax, ymin, ymax, zmin, zmax);

            // Create the 0 isosurface
            vtkContourFilter contours       = vtkContourFilter.New();

#if VTK_MAJOR_VERSION_5
            contours.SetInputConnection(sample.GetOutputPort());
#else
            contours.SetInputData(sample);
#endif
            contours.GenerateValues(1, 1, 1);

            // Map the contours to graphical primitives
            vtkPolyDataMapper contourMapper = vtkPolyDataMapper.New();
#if VTK_MAJOR_VERSION_5
            contourMapper.SetInputConnection(contours.GetOutputPort());
#else
            contourMapper.SetInputData(contours);
#endif
            contourMapper.SetScalarRange(0.0, 1.2);

            // Create an actor for the contours
            vtkActor contourActor           = vtkActor.New();
            contourActor.SetMapper(contourMapper);

            // -- create a box around the function to indicate the sampling volume --

            // Create outline
            vtkOutlineFilter outline = vtkOutlineFilter.New();
#if VTK_MAJOR_VERSION_5
            outline.SetInputConnection(sample.GetOutputPort());
#else
            outline.SetInputData(sample);
#endif

            // Map it to graphics primitives
            vtkPolyDataMapper outlineMapper = vtkPolyDataMapper.New();
#if VTK_MAJOR_VERSION_5
            outlineMapper.SetInputConnection(outline.GetOutputPort());
#else
            outlineMapper.SetInputData(outline);
#endif

            // Create an actor for it
            vtkActor outlineActor = vtkActor.New();

            outlineActor.SetMapper(outlineMapper);
            outlineActor.GetProperty().SetColor(0, 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(1.0, 1.0, 1.0);
            // add our actor to the renderer
            renderer.AddActor(contourActor);
            renderer.AddActor(outlineActor);
        }
        private void WindowedSincPolyDataFilter()
        {
            vtkSphereSource sphereSource = vtkSphereSource.New();

            sphereSource.Update();

            vtkWindowedSincPolyDataFilter smoother = vtkWindowedSincPolyDataFilter.New();

            smoother.SetInputConnection(sphereSource.GetOutputPort());
            smoother.SetNumberOfIterations(15);
            smoother.BoundarySmoothingOff();
            smoother.FeatureEdgeSmoothingOff();
            smoother.SetFeatureAngle(120.0);
            smoother.SetPassBand(.001);
            smoother.NonManifoldSmoothingOn();
            smoother.NormalizeCoordinatesOn();
            smoother.Update();

            vtkPolyDataMapper smoothedMapper = vtkPolyDataMapper.New();

#if VTK_MAJOR_VERSION_5
            smoothedMapper.SetInputConnection(smoother.GetOutputPort());
#else
            smoothedMapper.SetInputData(smoother);
#endif
            vtkActor smoothedActor = vtkActor.New();
            smoothedActor.SetMapper(smoothedMapper);

            vtkPolyDataMapper inputMapper = vtkPolyDataMapper.New();
#if VTK_MAJOR_VERSION_5
            inputMapper.SetInputConnection(sphereSource.GetOutputPort());
#else
            inputMapper.SetInputData(sphereSource);
#endif
            vtkActor inputActor = vtkActor.New();
            inputActor.SetMapper(inputMapper);

            vtkRenderWindow renderWindow = renderWindowControl1.RenderWindow;
            this.Size = new System.Drawing.Size(612, 352);

            // Define viewport ranges
            // (xmin, ymin, xmax, ymax)
            double[] leftViewport  = new double[] { 0.0, 0.0, 0.5, 1.0 };
            double[] rightViewport = new double[] { 0.5, 0.0, 1.0, 1.0 };

            // Setup both renderers
            vtkRenderer leftRenderer = vtkRenderer.New();
            renderWindow.AddRenderer(leftRenderer);
            leftRenderer.SetViewport(leftViewport[0], leftViewport[1], leftViewport[2], leftViewport[3]);
            leftRenderer.SetBackground(.6, .5, .4);

            vtkRenderer rightRenderer = vtkRenderer.New();
            renderWindow.AddRenderer(rightRenderer);
            rightRenderer.SetViewport(rightViewport[0], rightViewport[1], rightViewport[2], rightViewport[3]);
            rightRenderer.SetBackground(.4, .5, .6);

            // Add the sphere to the left and the cube to the right
            leftRenderer.AddActor(inputActor);
            rightRenderer.AddActor(smoothedActor);
            leftRenderer.ResetCamera();
            rightRenderer.ResetCamera();
            renderWindow.Render();
        }
Exemplo n.º 19
0
        private void ColoredElevationMap()
        {
            // Create a grid of points (height/terrian map)
            vtkPoints points = vtkPoints.New();

            uint   GridSize = 20;
            double xx, yy, zz;

            for (uint x = 0; x < GridSize; x++)
            {
                for (uint y = 0; y < GridSize; y++)
                {
                    xx = x + vtkMath.Random(-.2, .2);
                    yy = y + vtkMath.Random(-.2, .2);
                    zz = vtkMath.Random(-.5, .5);
                    points.InsertNextPoint(xx, yy, zz);
                }
            }

            // Add the grid points to a polydata object
            vtkPolyData inputPolyData = vtkPolyData.New();

            inputPolyData.SetPoints(points);

            // Triangulate the grid points
            vtkDelaunay2D delaunay = vtkDelaunay2D.New();

#if VTK_MAJOR_VERSION_5
            delaunay.SetInput(inputPolyData);
#else
            delaunay.SetInputData(inputPolyData);
#endif
            delaunay.Update();
            vtkPolyData outputPolyData = delaunay.GetOutput();

            double[] bounds = outputPolyData.GetBounds();

            // Find min and max z
            double minz = bounds[4];
            double maxz = bounds[5];

            Debug.WriteLine("minz: " + minz);
            Debug.WriteLine("maxz: " + maxz);

            // Create the color map
            vtkLookupTable colorLookupTable = vtkLookupTable.New();
            colorLookupTable.SetTableRange(minz, maxz);
            colorLookupTable.Build();

            // Generate the colors for each point based on the color map
            vtkUnsignedCharArray colors = vtkUnsignedCharArray.New();
            colors.SetNumberOfComponents(3);
            colors.SetName("Colors");

            Debug.WriteLine("There are " + outputPolyData.GetNumberOfPoints()
                            + " points.");


#if UNSAFE // fastest way to fill color array
            colors.SetNumberOfTuples(outputPolyData.GetNumberOfPoints());
            unsafe {
                byte *pColor = (byte *)colors.GetPointer(0).ToPointer();

                for (int i = 0; i < outputPolyData.GetNumberOfPoints(); i++)
                {
                    double[] p = outputPolyData.GetPoint(i);

                    double[] dcolor = colorLookupTable.GetColor(p[2]);
                    Debug.WriteLine("dcolor: "
                                    + dcolor[0] + " "
                                    + dcolor[1] + " "
                                    + dcolor[2]);

                    byte[] color = new byte[3];
                    for (uint j = 0; j < 3; j++)
                    {
                        color[j] = (byte)(255 * dcolor[j]);
                    }
                    Debug.WriteLine("color: "
                                    + color[0] + " "
                                    + color[1] + " "
                                    + color[2]);

                    *(pColor + 3 * i)     = color[0];
                    *(pColor + 3 * i + 1) = color[1];
                    *(pColor + 3 * i + 2) = color[2];
                }
            }
#else
            for (int i = 0; i < outputPolyData.GetNumberOfPoints(); i++)
            {
                double[] p = outputPolyData.GetPoint(i);

                double[] dcolor = colorLookupTable.GetColor(p[2]);
                Debug.WriteLine("dcolor: "
                                + dcolor[0] + " "
                                + dcolor[1] + " "
                                + dcolor[2]);

                byte[] color = new byte[3];
                for (uint j = 0; j < 3; j++)
                {
                    color[j] = (byte)(255 * dcolor[j]);
                }
                Debug.WriteLine("color: "
                                + color[0] + " "
                                + color[1] + " "
                                + color[2]);
                colors.InsertNextTuple3(color[0], color[1], color[2]);
                //IntPtr pColor = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(byte)) * 3);
                //Marshal.Copy(color, 0, pColor, 3);
                //colors.InsertNextTupleValue(pColor);
                //Marshal.FreeHGlobal(pColor);
            }
#endif

            outputPolyData.GetPointData().SetScalars(colors);

            // Create a mapper and actor
            vtkPolyDataMapper mapper = vtkPolyDataMapper.New();
#if VTK_MAJOR_VERSION_5
            mapper.SetInputConnection(outputPolyData.GetProducerPort());
#else
            mapper.SetInputData(outputPolyData);
#endif

            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 ContoursFromPolyData(string filePath)
        {
            vtkPolyData inputPolyData;

            if (filePath != null)
            {
                vtkXMLPolyDataReader reader = vtkXMLPolyDataReader.New();
                reader.SetFileName(filePath);
                reader.Update();
                inputPolyData = reader.GetOutput();
            }
            else
            {
                vtkSphereSource sphereSource = vtkSphereSource.New();
                sphereSource.SetThetaResolution(30);
                sphereSource.SetPhiResolution(15);
                sphereSource.Update();
                inputPolyData = sphereSource.GetOutput();
            }

            vtkPolyDataMapper inputMapper = vtkPolyDataMapper.New();

#if VTK_MAJOR_VERSION_5
            inputMapper.SetInput(inputPolyData);
#else
            inputMapper.SetInputData(inputPolyData);
#endif

            // Create a plane to cut
            vtkPlane plane  = vtkPlane.New();
            double[] center = inputPolyData.GetCenter();
            double[] bounds = inputPolyData.GetBounds();
            plane.SetOrigin(center[0], center[1], center[2]);
            plane.SetNormal(1, 1, 1);


            float[] centerf   = new float[] { (float)center[0], (float)center[1], (float)center[2] };
            float[] minBoundf = new float[] { (float)bounds[0], (float)bounds[2], (float)bounds[4] };
            float[] maxBoundf = new float[] { (float)bounds[1], (float)bounds[3], (float)bounds[5] };
            IntPtr  pCenter   = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(float)) * 3);
            IntPtr  pMinBound = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(float)) * 3);
            IntPtr  pMaxBound = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(float)) * 3);
            Marshal.Copy(centerf, 0, pCenter, 3);
            Marshal.Copy(minBoundf, 0, pMinBound, 3);
            Marshal.Copy(maxBoundf, 0, pMaxBound, 3);

            // vtkMath.Distance2BetweenPoints accepts floats only
            double distanceMin = Math.Sqrt(vtkMath.Distance2BetweenPoints(pMinBound, pCenter));
            double distanceMax = Math.Sqrt(vtkMath.Distance2BetweenPoints(pMaxBound, pCenter));

            Marshal.FreeHGlobal(pCenter);
            Marshal.FreeHGlobal(pMinBound);
            Marshal.FreeHGlobal(pMaxBound);
            // Create cutter
            vtkCutter cutter = vtkCutter.New();
            cutter.SetCutFunction(plane);
#if VTK_MAJOR_VERSION_5
            cutter.SetInput(inputPolyData);
#else
            cutter.SetInputData(inputPolyData);
#endif
            cutter.GenerateValues(20, -distanceMin, distanceMax);
            vtkPolyDataMapper cutterMapper = vtkPolyDataMapper.New();
            cutterMapper.SetInputConnection(cutter.GetOutputPort());
            cutterMapper.ScalarVisibilityOff();

            // Create plane actor
            vtkActor planeActor = vtkActor.New();
            planeActor.GetProperty().SetColor(1.0, 0.0, 0.0);
            planeActor.GetProperty().SetLineWidth(3);
            planeActor.SetMapper(cutterMapper);

            // Create input actor
            vtkActor inputActor = vtkActor.New();
            inputActor.GetProperty().SetColor(1.0, 0.8941, 0.7686); // bisque
            inputActor.SetMapper(inputMapper);

            // 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, .3, .4);
            renderer.AddActor(inputActor);
            renderer.AddActor(planeActor); //display the contour lines
        }
Exemplo n.º 21
0
        private void FillHoles(string filePath)
        {
            vtkPolyData input = vtkPolyData.New();

            if (filePath == null)
            {
                GenerateData(ref input);
            }
            else
            {
                vtkXMLPolyDataReader reader = vtkXMLPolyDataReader.New();
                reader.SetFileName(filePath);
                reader.Update();

                input.ShallowCopy(reader.GetOutput());
            }

            vtkFillHolesFilter fillHolesFilter = vtkFillHolesFilter.New();

#if VTK_MAJOR_VERSION_5
            fillHolesFilter.SetInputConnection(input.GetProducerPort());
#else
            fillHolesFilter.SetInputData(input);
#endif

            fillHolesFilter.Update();

            // Create a mapper and actor
            vtkPolyDataMapper originalMapper = vtkPolyDataMapper.New();
#if VTK_MAJOR_VERSION_5
            originalMapper.SetInputConnection(input.GetProducerPort());
#else
            originalMapper.SetInputData(input);
#endif

            vtkActor originalActor = vtkActor.New();
            originalActor.SetMapper(originalMapper);

            vtkPolyDataMapper filledMapper = vtkPolyDataMapper.New();
            filledMapper.SetInputConnection(fillHolesFilter.GetOutputPort());

            vtkActor filledActor = vtkActor.New();
            filledActor.SetMapper(filledMapper);

            vtkRenderWindow renderWindow = renderWindowControl1.RenderWindow;
            this.Size = new System.Drawing.Size(612, 352);

            // Define viewport ranges
            // (xmin, ymin, xmax, ymax)
            double[] leftViewport  = new double[] { 0.0, 0.0, 0.5, 1.0 };
            double[] rightViewport = new double[] { 0.5, 0.0, 1.0, 1.0 };

            // Setup both renderers
            vtkRenderer leftRenderer = vtkRenderer.New();
            renderWindow.AddRenderer(leftRenderer);
            leftRenderer.SetViewport(leftViewport[0], leftViewport[1], leftViewport[2], leftViewport[3]);
            leftRenderer.SetBackground(.6, .5, .4);

            vtkRenderer rightRenderer = vtkRenderer.New();
            renderWindow.AddRenderer(rightRenderer);
            rightRenderer.SetViewport(rightViewport[0], rightViewport[1], rightViewport[2], rightViewport[3]);
            rightRenderer.SetBackground(.4, .5, .6);

            // Add the sphere to the left and the cube to the right
            leftRenderer.AddActor(originalActor);
            rightRenderer.AddActor(filledActor);
            leftRenderer.ResetCamera();
            rightRenderer.ResetCamera();
            renderWindow.Render();
        }