private void Window_Activated(object sender, EventArgs e)
        {
            vtkPolyData   cube    = new vtkPolyData();
            vtkPoints     points  = new vtkPoints();
            vtkCellArray  polys   = new vtkCellArray();
            vtkFloatArray scalars = new vtkFloatArray();

            Kitware.VTK.RenderWindowControl vtkControl = new Kitware.VTK.RenderWindowControl();
            vtkControl.AddTestActors = false;
            vtkControl.Location      = new System.Drawing.Point(10, 10);
            vtkControl.Name          = "_renwin";
            vtkControl.Size          = new System.Drawing.Size(100, 100);
            vtkControl.TabIndex      = 0;
            vtkControl.TestText      = null;
            vtkControl.Dock          = System.Windows.Forms.DockStyle.Fill;
            vtkformhost.Child        = vtkControl;
            vtkformhost.Visibility   = System.Windows.Visibility.Visible;


            int i;

            float[][] x = new float[8][]
            {
                new float[] { 0, 0, 0 }, //第0个点的坐标
                new float[] { 1, 0, 0 }, //第1个点的坐标
                new float[] { 1, 1, 0 }, //第2个点的坐标
                new float[] { 0, 1, 0 }, //3
                new float[] { 0, 0, 1 }, //4
                new float[] { 1, 0, 1 }, //5
                new float[] { 1, 1, 1 }, //6
                new float[] { 0, 1, 1 } //7
            };
            for (i = 0; i < 8; i++)
            {
                points.InsertPoint(i, x[i][0], x[i][1], x[i][2]);                    //加载点,创建数据结构的几何
            }
            List <int[]> temp = new List <int[]>();

            int[] temparray0 = new int[4] {
                0, 1, 2, 3
            };                                           //第0,1,2,3个点连接在一起,成为一个单元
            int[] temparray1 = new int[4] {
                4, 5, 6, 7
            };                                           //第4,5,6,7个点连接在一起,成为一个单元
            int[] temparray2 = new int[4] {
                0, 1, 5, 4
            };
            int[] temparray3 = new int[4] {
                1, 2, 6, 5
            };
            int[] temparray4 = new int[4] {
                2, 3, 7, 6
            };
            int[] temparray5 = new int[4] {
                3, 0, 4, 7
            };
            temp.Add(temparray0);
            temp.Add(temparray1);
            temp.Add(temparray2);
            temp.Add(temparray3);
            temp.Add(temparray4);
            temp.Add(temparray5);
            //因为在activiz中没有vtkIdType这个类,所以用了其他的方法代替C++代码中的实现。
            for (int j = 0; j < temp.Count; j++)
            {
                IntPtr pP = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(int)) * 4);
                Marshal.Copy(temp[j], 0, pP, 4);
                polys.InsertNextCell(4, pP);//加载单元,定义数据集的拓扑
                Marshal.FreeHGlobal(pP);
            }
            for (i = 0; i < 8; i++)
            {
                scalars.InsertTuple1(i, i);                    //为每一个点设置点属性。
            }
            cube.SetPoints(points);
            cube.SetPolys(polys);
            cube.GetPointData().SetScalars(scalars);

            vtkPolyDataMapper cubemapper = new vtkPainterPolyDataMapper();

            cubemapper.SetInput(cube);
            cubemapper.SetScalarRange(0, 7);

            vtkActor cubeactor = new vtkActor();

            cubeactor.SetMapper(cubemapper);

            // Create components of the rendering subsystem
            //
            vtkRenderWindow _renwin = vtkControl.RenderWindow;
            vtkRenderer     ren1    = _renwin.GetRenderers().GetFirstRenderer();


            // Add the actors to the renderer, set the window size
            //
            ren1.AddViewProp(cubeactor);
            _renwin.SetSize(250, 250);
            _renwin.Render();
            ren1.ResetCamera();
        }
        //private void InitializeVTK()
        //{
        //    Kitware.VTK.RenderWindowControl vtkControl = new Kitware.VTK.RenderWindowControl();
        //    vtkControl.AddTestActors = false;
        //    vtkControl.Location = new System.Drawing.Point(10, 10);
        //    vtkControl.Name = "_renwin";
        //    vtkControl.Size = new System.Drawing.Size(100, 100);
        //    vtkControl.TabIndex = 0;
        //    vtkControl.TestText = null;
        //    vtkControl.Dock = System.Windows.Forms.DockStyle.Fill;
        //    vtkformhost.Child = vtkControl;
        //    vtkformhost.Visibility = System.Windows.Visibility.Visible;

        //    vtkSphereSource sphere = vtkSphereSource.New();
        //    sphere.SetThetaResolution(8);
        //    sphere.SetPhiResolution(16);

        //    vtkShrinkPolyData shrink = vtkShrinkPolyData.New();
        //    shrink.SetInputConnection(sphere.GetOutputPort());
        //    shrink.SetShrinkFactor(0.9);

        //    vtkPolyDataMapper mapper = vtkPolyDataMapper.New();
        //    mapper.SetInputConnection(shrink.GetOutputPort());

        //    // The actor links the data pipeline to the rendering subsystem
        //    vtkActor actor = vtkActor.New();
        //    actor.SetMapper(mapper);
        //    actor.GetProperty().SetColor(1, 0, 0);

        //    // Create components of the rendering subsystem
        //    //
        //    Kitware.VTK.vtkRendererCollection rs = vtkControl.RenderWindow.GetRenderers();
        //    vtkRenderer ren1 = vtkControl.RenderWindow.GetRenderers().GetFirstRenderer();
        //    vtkRenderWindow renWin = vtkControl.RenderWindow;

        //    // Add the actors to the renderer, set the window size
        //    //
        //    ren1.AddViewProp(actor);
        //    renWin.SetSize(500, 500);
        //    renWin.Render();
        //    vtkCamera camera = ren1.GetActiveCamera();
        //    camera.Zoom(1.5);

        //}


        private void InitializeVTK()
        {
            Kitware.VTK.RenderWindowControl rw = new Kitware.VTK.RenderWindowControl();

            rw.AddTestActors       = false;
            rw.Dock                = System.Windows.Forms.DockStyle.Fill;
            vtkformhost.Child      = rw;
            vtkformhost.Visibility = System.Windows.Visibility.Visible;
            Kitware.VTK.vtkRendererCollection rs = rw.RenderWindow.GetRenderers();
            int rsc = rs.GetNumberOfItems();

            Console.WriteLine(rsc + " renderers");
            Kitware.VTK.vtkRenderer r = rs.GetFirstRenderer();
            r.SetBackground(0.1, 0.3, 0.7);
            r.SetBackground2(0.7, 0.8, 1.0);
            r.SetGradientBackground(true);

            int   i, j, k, kOffset, jOffset, offset;
            float s, sp, x, y, z;

            //创建结构化点数据集,(创建点和单元)
            vtkStructuredPoints vol = new vtkStructuredPoints();

            vol.SetDimensions(26, 26, 26);   //x,y,z三个坐标轴方向上各有26个点
            vol.SetOrigin(-0.5, -0.5, -0.5); //设置数据集在坐标空间内的起点
            sp = (float)(1.0 / 25.0);
            vol.SetSpacing(sp, sp, sp);      //设置坐标轴上每个点的间距

            //创建标量数据(作为结构化点数据集的属性数据)
            vtkFloatArray scalars = new vtkFloatArray();

            scalars.SetNumberOfTuples(26 * 26 * 26);//设置标量个数,因为是点属性所以和点的个数相同

            for (k = 0; k < 26; k++)
            {
                z       = (float)(-0.5 + k * sp);
                kOffset = k * 26 * 26;
                for (j = 0; j < 26; j++)
                {
                    y       = (float)(-0.5 + j * sp);
                    jOffset = j * 26;
                    for (i = 0; i < 26; i++)
                    {
                        x = (float)(-0.5 + i * sp);
                        s = (float)(x * x + y * y + z * z - (0.4 * 0.4));
                        //计算标量值,该方程为球体方程,位于球体上的点标量值为0
                        offset = i + jOffset + kOffset;  //计算id
                        scalars.InsertTuple1(offset, s); //插入标量值
                    }
                }
            }

            vol.GetPointData().SetScalars(scalars); //将标量值与点关联


            //抽取标量值为0的点所形成的面
            vtkContourFilter contour = new vtkContourFilter();

            contour.SetInput(vol);
            contour.SetValue(0, 0);

            vtkPolyDataMapper volmapper = new vtkPainterPolyDataMapper();

            volmapper.SetInput(contour.GetOutput());

            vtkActor actor = new vtkActor();

            actor.GetProperty().SetRepresentationToWireframe();
            // actor.GetProperty().SetRepresentationToSurface();
            // actor.GetProperty().SetRepresentationToPoints();
            actor.GetProperty().SetColor(0, 0, 0);
            actor.SetMapper(volmapper);

            vtkRenderWindow _renwin = rw.RenderWindow;
            vtkRenderer     _render = _renwin.GetRenderers().GetFirstRenderer();

            _render.AddActor(actor);

            _renwin.Render();

            _render.ResetCamera();
        }
Esempio n. 3
0
        //private void CommonInit(Sequence BinarySubImageSeq)
        //{
        //    vtkImageData ImageData1 = new vtkImageData();
        //    ImageData1.SetDimensions(BinarySubImageSeq.Width, BinarySubImageSeq.Height, BinarySubImageSeq.Depth);
        //    ImageData1.SetNumberOfScalarComponents(1);
        //    ImageData1.SetSpacing(BinarySubImageSeq.XResolution, BinarySubImageSeq.YResolution, BinarySubImageSeq.ZResolution);
        //    ImageData1.SetScalarTypeToFloat();
        //    vtkFloatArray array1 = new vtkFloatArray();
        //    for (int i = 0; i < BinarySubImageSeq.ImageSize; i++)
        //        array1.InsertTuple1(i, BinarySubImageSeq[0].Data[0][i]);
        //    ImageData1.GetPointData().SetScalars(array1);
        //    vtkExtractVOI VOI = new vtkExtractVOI();
        //    VOI.SetInput(ImageData1);
        //    VOI.SetSampleRate(1, 1, 1);
        //    vtkMarchingCubes ContourObject = vtkMarchingCubes.New();
        //    vtk_PolyDataMapper = vtkPolyDataMapper.New();
        //    //ContourActor = new vtkActor();
        //    VOI.SetVOI(0, BinarySubImageSeq.Width - 1, 0, BinarySubImageSeq.Height - 1, 0, BinarySubImageSeq.Depth - 1);
        //    ContourObject.SetInput(VOI.GetOutput());
        //    ContourObject.SetValue(0, 0.5);
        //    vtk_PolyDataMapper.SetInput(ContourObject.GetOutput());
        //    vtk_PolyDataMapper.ScalarVisibilityOn();
        //    vtk_PolyDataMapper.SetScalarModeToUseFieldData();
        //}
        ///// <summary>
        ///// Generate a 3D mesh using marching-cubes algorithm. If voxel value is lower than 1 it is consider as background, else as object
        ///// </summary>
        ///// <param name="BinarySubImageSeq">The binary image</param>
        ///// <param name="Colour">Mesh color</param>
        ///// <param name="Pos">Postion of the object in the world</param>
        //public void Generate(Sequence BinarySubImageSeq, Color Colour, cPoint3D Pos)
        //{
        //    vtkImageData ImageData1 = new vtkImageData();
        //    ImageData1.SetDimensions(BinarySubImageSeq.Width, BinarySubImageSeq.Height, BinarySubImageSeq.Depth);
        //    ImageData1.SetNumberOfScalarComponents(1);
        //    ImageData1.SetSpacing(BinarySubImageSeq.XResolution, BinarySubImageSeq.YResolution, BinarySubImageSeq.ZResolution);
        //    ImageData1.SetScalarTypeToFloat();
        //    vtkFloatArray array1 = new vtkFloatArray();
        //    for (int i = 0; i < BinarySubImageSeq.ImageSize; i++)
        //        array1.InsertTuple1(i, BinarySubImageSeq[0].Data[0][i]);
        //    ImageData1.GetPointData().SetScalars(array1);
        //    vtkExtractVOI VOI = new vtkExtractVOI();
        //    VOI.SetInput(ImageData1);
        //    VOI.SetSampleRate(1, 1, 1);
        //    vtkMarchingCubes ContourObject = vtkMarchingCubes.New();
        //    vtk_PolyDataMapper = vtkPolyDataMapper.New();
        //    //ContourActor = new vtkActor();
        //    VOI.SetVOI(0, BinarySubImageSeq.Width - 1, 0, BinarySubImageSeq.Height - 1, 0, BinarySubImageSeq.Depth - 1);
        //    // perform the amrching cubes
        //    ContourObject.SetInput(VOI.GetOutput());
        //    ContourObject.SetValue(0, 0.5);
        //    //vtkDecimatePro deci
        //    //deci SetInputConnection [fran GetOutputPort]
        //    //deci SetTargetReduction 0.9
        //    //deci PreserveTopologyOn
        //    if (MeshSmoother!=null)
        //    {
        //        vtkSmoothPolyDataFilter smoother = new vtkSmoothPolyDataFilter();
        //        smoother.SetInputConnection(ContourObject.GetOutputPort());// [deci GetOutputPort]
        //        smoother.SetNumberOfIterations(50);
        //        vtk_PolyData = smoother.GetOutput();
        //    }
        //    else
        //    {
        //        vtk_PolyData = ContourObject.GetOutput();
        //    }
        //    vtk_PolyDataMapper.SetInput(vtk_PolyData);
        //    vtk_PolyDataMapper.ScalarVisibilityOn();
        //    vtk_PolyDataMapper.SetScalarModeToUseFieldData();
        //    this.Position = new cPoint3D(Pos.X, Pos.Y, Pos.Z);
        //    this.Colour = Colour;
        //    CreateVTK3DObject(1);
        //    vtk_PolyData = ContourObject.GetOutput();
        //    // compute convex hull
        //    hullFilter = vtkHull.New();
        //    hullFilter.SetInputConnection(ContourObject.GetOutputPort());
        //    hullFilter.AddRecursiveSpherePlanes(1);
        //    hullFilter.Update();
        //    //  this.BackfaceCulling(false);
        //    Information = new cInformation(ContourObject, this, hullFilter);
        //}
        public void Generate(cImage BinarySubImageSeq, Color Colour, cPoint3D Pos/*, List<cBiological3DObject> Containers, int ContainerMode*/)
        {
            vtkImageData ImageData1 = new vtkImageData();

            ImageData1.SetDimensions(BinarySubImageSeq.Width, BinarySubImageSeq.Height, BinarySubImageSeq.Depth);
            ImageData1.SetNumberOfScalarComponents(1);
            ImageData1.SetSpacing(BinarySubImageSeq.Resolution.X, BinarySubImageSeq.Resolution.Y, BinarySubImageSeq.Resolution.Z);
            ImageData1.SetScalarTypeToFloat();

            vtkFloatArray array1 = new vtkFloatArray();
            for (int i = 0; i < BinarySubImageSeq.ImageSize; i++)
                array1.InsertTuple1(i, BinarySubImageSeq.SingleChannelImage[0].Data[i]);
            ImageData1.GetPointData().SetScalars(array1);

            vtkExtractVOI VOI = new vtkExtractVOI();
            VOI.SetInput(ImageData1);
            VOI.SetSampleRate(1, 1, 1);

            vtkMarchingCubes ContourObject = vtkMarchingCubes.New();
            vtk_PolyDataMapper = vtkPolyDataMapper.New();
            //ContourActor = new vtkActor();

            VOI.SetVOI(0, BinarySubImageSeq.Width - 1, 0, BinarySubImageSeq.Height - 1, 0, BinarySubImageSeq.Depth - 1);
            ContourObject.SetInput(VOI.GetOutput());
            ContourObject.SetValue(0, 0.5);

            vtkAlgorithmOutput AlgoOutPut = null;

            if (MeshSmoother != null)
            {
                //vtkSmoothPolyDataFilter smoother = new vtkSmoothPolyDataFilter();
                vtkWindowedSincPolyDataFilter smoother = new vtkWindowedSincPolyDataFilter();
                smoother.SetInputConnection(ContourObject.GetOutputPort());// [deci GetOutputPort]
                smoother.SetNumberOfIterations(MeshSmoother.NumberOfIterations);
                vtk_PolyData = smoother.GetOutput();
                //smoother.GetOutputPort();
                AlgoOutPut = smoother.GetOutputPort();
            }
            else
            {
                vtk_PolyData = ContourObject.GetOutput();
                AlgoOutPut = ContourObject.GetOutputPort();
            }

            vtk_PolyDataMapper.SetInput(vtk_PolyData);
            vtk_PolyDataMapper.ScalarVisibilityOn();
            vtk_PolyDataMapper.SetScalarModeToUseFieldData();

            vtkActor TmpActor = vtkActor.New();
            TmpActor.SetMapper(vtk_PolyDataMapper);
            TmpActor.SetPosition(Pos.X, Pos.Y, Pos.Z);

            //Console.WriteLine("PosX"+Pos.X+" PosY"+Pos.Y+" PosZ"+Pos.Z);

            #region deal with the containers

            if (this.Containers != null)
            {
                if (this.Containers.ContainerMode == 0)
                {
                    cPoint3D Centroid = new cPoint3D((float)TmpActor.GetCenter()[0], (float)TmpActor.GetCenter()[1], (float)TmpActor.GetCenter()[2]);
                    bool IsInside = false;

                    for (int Idx = 0; Idx < Containers.Containers.Count; Idx++)
                    {
                        cBiological3DVolume CurrentContainer = (cBiological3DVolume)(Containers.Containers[Idx]);

                        if (CurrentContainer.IsPointInside(Centroid))
                        {
                            IsInside = true;
                            ContainerIdx = Idx;
                            break;
                        }
                    }
                    if (IsInside)
                    {
                        this.SetPosition(new cPoint3D(Pos.X, Pos.Y, Pos.Z));
                        this.Colour = Colour;
                        CreateVTK3DObject(1);
                        //   vtk_PolyData = ContourObject.GetOutput();

                        // compute convex hull
                        hullFilter = vtkHull.New();
                        hullFilter.SetInputConnection(AlgoOutPut);
                        hullFilter.AddRecursiveSpherePlanes(0);
                        hullFilter.Update();

                        Information = new cInformation(AlgoOutPut, this, hullFilter);
                        this.Detected = true;
                    }
                    else
                    {
                        this.Detected = false;
                    }
                }
                else if (Containers.ContainerMode == 1)
                {
                    this.Detected = true;
                    //bool IsInside = false;
                    for (int Idx = 0; Idx < Containers.Containers.Count; Idx++)
                    {
                        cBiological3DVolume CurrentContainer = (cBiological3DVolume)(Containers.Containers[Idx]);

                        if (CurrentContainer.IsPointInside(Pos))
                        {
                            //IsInside = false;
                            this.Detected = false;
                            return;
                        }
                    }
                    this.SetPosition(new cPoint3D(Pos.X, Pos.Y, Pos.Z));
                    this.Colour = Colour;

                    CreateVTK3DObject(1);
                    // vtk_PolyData = ContourObject.GetOutput();

                    // compute convex hull
                    hullFilter = vtkHull.New();
                    hullFilter.SetInputConnection(AlgoOutPut);
                    hullFilter.AddRecursiveSpherePlanes(0);
                    hullFilter.Update();

                    Information = new cInformation(AlgoOutPut, this, hullFilter);
                    this.Detected = true;
                }
            }
            else
            {
                this.SetPosition(new cPoint3D(Pos.X, Pos.Y, Pos.Z));
                this.Colour = Colour;

                CreateVTK3DObject(1);

                //  vtk_PolyData = ContourObject.GetOutput();

                // compute convex hull
                hullFilter = vtkHull.New();
                hullFilter.SetInputConnection(AlgoOutPut);
                hullFilter.AddRecursiveSpherePlanes(1);
                hullFilter.Update();

                //  this.BackfaceCulling(false);
                Information = new cInformation(AlgoOutPut, this, hullFilter);

            }

            #endregion
        }
Esempio n. 4
0
        public void Generate(CImage3D BinarySubImageSeq, Color Colour, cPoint3D Pos)
        {
            vtkImageData ImageData1 = new vtkImageData();

            ImageData1.SetDimensions(BinarySubImageSeq.Width, BinarySubImageSeq.Height, BinarySubImageSeq.Depth);
            ImageData1.SetNumberOfScalarComponents(1);
            //ImageData1.SetSpacing(BinarySubImageSeq.XResolution, BinarySubImageSeq.YResolution, BinarySubImageSeq.ZResolution);
            ImageData1.SetScalarTypeToFloat();

            vtkFloatArray array1 = new vtkFloatArray();
            for (int i = 0; i < BinarySubImageSeq.ImageSize; i++)
                array1.InsertTuple1(i, BinarySubImageSeq.Data[i]);
            ImageData1.GetPointData().SetScalars(array1);

            vtkExtractVOI VOI = new vtkExtractVOI();
            VOI.SetInput(ImageData1);
            VOI.SetSampleRate(1, 1, 1);

            vtkMarchingCubes ContourObject = vtkMarchingCubes.New();
            vtk_PolyDataMapper = vtkPolyDataMapper.New();
            //ContourActor = new vtkActor();

            VOI.SetVOI(0, BinarySubImageSeq.Width - 1, 0, BinarySubImageSeq.Height - 1, 0, BinarySubImageSeq.Depth - 1);
            ContourObject.SetInput(VOI.GetOutput());
            ContourObject.SetValue(0, 0.5);

            vtkAlgorithmOutput AlgoOutPut = null;

            vtk_PolyData = ContourObject.GetOutput();
            AlgoOutPut = ContourObject.GetOutputPort();

            vtk_PolyDataMapper.SetInput(vtk_PolyData);
            vtk_PolyDataMapper.ScalarVisibilityOn();
            vtk_PolyDataMapper.SetScalarModeToUseFieldData();

            vtkActor TmpActor = vtkActor.New();
            TmpActor.SetMapper(vtk_PolyDataMapper);
            TmpActor.SetPosition(Pos.X, Pos.Y, Pos.Z);

            //Console.WriteLine("PosX"+Pos.X+" PosY"+Pos.Y+" PosZ"+Pos.Z);

            #region deal with the containers

            this.Position = new cPoint3D(Pos.X, Pos.Y, Pos.Z);
            this.Colour = Colour;

            CreateVTK3DObject(1);

            //  vtk_PolyData = ContourObject.GetOutput();

            // compute convex hull
            hullFilter = vtkHull.New();
            hullFilter.SetInputConnection(AlgoOutPut);
            hullFilter.AddRecursiveSpherePlanes(1);
            hullFilter.Update();

            //  this.BackfaceCulling(false);
            Information = new cInformation(AlgoOutPut, this, hullFilter);

            #endregion
        }