Exemplo n.º 1
0
        void CreateVectorField(ref vtkImageData image)
        {
            // Specify the size of the image data
            image.SetDimensions(3, 3, 3);
            image.SetNumberOfScalarComponents(3);
            image.SetScalarTypeToFloat();
            image.AllocateScalars();
            image.SetSpacing(10.0, 10.0, 10.0);
            int[] dims = image.GetDimensions();

            float[] pixel = new float[] { 0.0f, 0.0f, 0.0f };
            IntPtr  pPixel;

            // Zero the vectors
            for (int z = 0; z < dims[2]; z++)
            {
                for (int y = 0; y < dims[1]; y++)
                {
                    for (int x = 0; x < dims[0]; x++)
                    {
                        pPixel = image.GetScalarPointer(x, y, 0);
                        Marshal.Copy(pixel, 0, pPixel, 3);
                    }
                }
            }

            // Set two of the pixels to non zero values
            pixel[0] = 8.0f;
            pixel[1] = 8.0f;
            pixel[2] = -8.0f;
            pPixel   = image.GetScalarPointer(0, 2, 0);
            Marshal.Copy(pixel, 0, pPixel, 3);

            pixel[0] = 8.0f;
            pixel[1] = -8.0f;
            pixel[2] = 8.0f;
            pPixel   = image.GetScalarPointer(2, 0, 2);
            Marshal.Copy(pixel, 0, pPixel, 3);
        }
        void CreateVectorField(ref vtkImageData image)
        {
            // Specify the size of the image data
             image.SetDimensions(3, 3, 3);
             image.SetNumberOfScalarComponents(3);
             image.SetScalarTypeToFloat();
             image.AllocateScalars();
             image.SetSpacing(10.0, 10.0, 10.0);
             int[]  dims = image.GetDimensions();

             float[] pixel = new float[] {0.0f, 0.0f, 0.0f};
             IntPtr pPixel;

             // Zero the vectors
             for(int z = 0; z < dims[2]; z++) {
            for(int y = 0; y < dims[1]; y++) {
               for(int x = 0; x < dims[0]; x++) {
                  pPixel = image.GetScalarPointer(x, y, 0);
                  Marshal.Copy(pixel, 0, pPixel, 3);
               }
            }
             }

             // Set two of the pixels to non zero values
             pixel[0] = 8.0f;
             pixel[1] = 8.0f;
             pixel[2] = -8.0f;
             pPixel = image.GetScalarPointer(0, 2, 0);
             Marshal.Copy(pixel, 0, pPixel, 3);

             pixel[0] = 8.0f;
             pixel[1] = -8.0f;
             pixel[2] = 8.0f;
             pPixel = image.GetScalarPointer(2, 0, 2);
             Marshal.Copy(pixel, 0, pPixel, 3);
        }
Exemplo n.º 3
0
    /// <summary>
    /// The main entry method called by the CSharp driver
    /// </summary>
    /// <param name="argv"></param>
    public static void AVTestCutMaterial(String [] argv)
    {
        //Prefix Content is: ""

          // Lets create a data set.[]
          data = new vtkImageData();
          data.SetExtent((int)0,(int)31,(int)0,(int)31,(int)0,(int)31);
          data.SetScalarTypeToFloat();
          // First the data array:[]
          gauss = new vtkImageGaussianSource();
          gauss.SetWholeExtent((int)0,(int)30,(int)0,(int)30,(int)0,(int)30);
          gauss.SetCenter((double)18,(double)12,(double)20);
          gauss.SetMaximum((double)1.0);
          gauss.SetStandardDeviation((double)10.0);
          gauss.Update();
          a = gauss.GetOutput().GetPointData().GetScalars();
          a.SetName((string)"Gauss");
          data.GetCellData().SetScalars((vtkDataArray)a);
          //skipping Delete gauss
          // Now the material array:[]
          ellipse = new vtkImageEllipsoidSource();
          ellipse.SetWholeExtent((int)0,(int)30,(int)0,(int)30,(int)0,(int)30);
          ellipse.SetCenter((double)11,(double)12,(double)13);
          ellipse.SetRadius((double)5,(double)9,(double)13);
          ellipse.SetInValue((double)1);
          ellipse.SetOutValue((double)0);
          ellipse.SetOutputScalarTypeToInt();
          ellipse.Update();
          m = ellipse.GetOutput().GetPointData().GetScalars();
          m.SetName((string)"Material");
          data.GetCellData().AddArray((vtkAbstractArray)m);
          //skipping Delete ellipse
          cut = new vtkCutMaterial();
          cut.SetInput((vtkDataObject)data);
          cut.SetMaterialArrayName((string)"Material");
          cut.SetMaterial((int)1);
          cut.SetArrayName((string)"Gauss");
          cut.SetUpVector((double)1,(double)0,(double)0);
          cut.Update();
          mapper2 = vtkPolyDataMapper.New();
          mapper2.SetInputConnection((vtkAlgorithmOutput)cut.GetOutputPort());
          mapper2.SetScalarRange((double)0,(double)1);
          //apper2 SetScalarModeToUseCellFieldData[]
          //apper2 SetColorModeToMapScalars []
          //apper2 ColorByArrayComponent "vtkGhostLevels" 0[]
          actor2 = new vtkActor();
          actor2.SetMapper((vtkMapper)mapper2);
          actor2.SetPosition((double)1.5,(double)0,(double)0);
          ren = vtkRenderer.New();
          ren.AddActor((vtkProp)actor2);
          renWin = vtkRenderWindow.New();
          renWin.AddRenderer((vtkRenderer)ren);
          p = cut.GetCenterPoint();
          n = cut.GetNormal();
          cam = ren.GetActiveCamera();
          cam.SetFocalPoint(p[0],p[1],p[2]);
          cam.SetViewUp(cut.GetUpVector()[0], cut.GetUpVector()[1], cut.GetUpVector()[2]);
          cam.SetPosition(
          (double)(lindex(n,0))+(double)(lindex(p,0)),
          (double)(lindex(n,1))+(double)(lindex(p,1)),
          (double)(lindex(n,2))+(double)(lindex(p,2)));
          ren.ResetCamera();
          iren = vtkRenderWindowInteractor.New();
          iren.SetRenderWindow(renWin);
          iren.Initialize();

        //deleteAllVTKObjects();
    }
Exemplo n.º 4
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
        }
Exemplo n.º 5
0
    /// <summary>
    /// The main entry method called by the CSharp driver
    /// </summary>
    /// <param name="argv"></param>
    public static void AVTestCutMaterial(String [] argv)
    {
        //Prefix Content is: ""

        // Lets create a data set.[]
        data = new vtkImageData();
        data.SetExtent((int)0, (int)31, (int)0, (int)31, (int)0, (int)31);
        data.SetScalarTypeToFloat();
        // First the data array:[]
        gauss = new vtkImageGaussianSource();
        gauss.SetWholeExtent((int)0, (int)30, (int)0, (int)30, (int)0, (int)30);
        gauss.SetCenter((double)18, (double)12, (double)20);
        gauss.SetMaximum((double)1.0);
        gauss.SetStandardDeviation((double)10.0);
        gauss.Update();
        a = gauss.GetOutput().GetPointData().GetScalars();
        a.SetName((string)"Gauss");
        data.GetCellData().SetScalars((vtkDataArray)a);
        //skipping Delete gauss
        // Now the material array:[]
        ellipse = new vtkImageEllipsoidSource();
        ellipse.SetWholeExtent((int)0, (int)30, (int)0, (int)30, (int)0, (int)30);
        ellipse.SetCenter((double)11, (double)12, (double)13);
        ellipse.SetRadius((double)5, (double)9, (double)13);
        ellipse.SetInValue((double)1);
        ellipse.SetOutValue((double)0);
        ellipse.SetOutputScalarTypeToInt();
        ellipse.Update();
        m = ellipse.GetOutput().GetPointData().GetScalars();
        m.SetName((string)"Material");
        data.GetCellData().AddArray((vtkAbstractArray)m);
        //skipping Delete ellipse
        cut = new vtkCutMaterial();
        cut.SetInput((vtkDataObject)data);
        cut.SetMaterialArrayName((string)"Material");
        cut.SetMaterial((int)1);
        cut.SetArrayName((string)"Gauss");
        cut.SetUpVector((double)1, (double)0, (double)0);
        cut.Update();
        mapper2 = vtkPolyDataMapper.New();
        mapper2.SetInputConnection((vtkAlgorithmOutput)cut.GetOutputPort());
        mapper2.SetScalarRange((double)0, (double)1);
        //apper2 SetScalarModeToUseCellFieldData[]
        //apper2 SetColorModeToMapScalars []
        //apper2 ColorByArrayComponent "vtkGhostLevels" 0[]
        actor2 = new vtkActor();
        actor2.SetMapper((vtkMapper)mapper2);
        actor2.SetPosition((double)1.5, (double)0, (double)0);
        ren = vtkRenderer.New();
        ren.AddActor((vtkProp)actor2);
        renWin = vtkRenderWindow.New();
        renWin.AddRenderer((vtkRenderer)ren);
        p   = cut.GetCenterPoint();
        n   = cut.GetNormal();
        cam = ren.GetActiveCamera();
        cam.SetFocalPoint(p[0], p[1], p[2]);
        cam.SetViewUp(cut.GetUpVector()[0], cut.GetUpVector()[1], cut.GetUpVector()[2]);
        cam.SetPosition(
            (double)(lindex(n, 0)) + (double)(lindex(p, 0)),
            (double)(lindex(n, 1)) + (double)(lindex(p, 1)),
            (double)(lindex(n, 2)) + (double)(lindex(p, 2)));
        ren.ResetCamera();
        iren = vtkRenderWindowInteractor.New();
        iren.SetRenderWindow(renWin);
        iren.Initialize();

//deleteAllVTKObjects();
    }
Exemplo n.º 6
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
        }