///<summary> A Set Method for Static Variables </summary>
 public static void Setsp(vtkStructuredPoints toSet)
 {
     sp = toSet;
 }
    /// <summary>
    /// The main entry method called by the CSharp driver
    /// </summary>
    /// <param name="argv"></param>
    public static void AVTestImageWriters(String [] argv)
    {
        //Prefix Content is: ""

        // Image pipeline[]
        image1 = new vtkTIFFReader();
        image1.SetFileName((string)"" + (VTK_DATA_ROOT.ToString()) + "/Data/beach.tif");
        // "beach.tif" image contains ORIENTATION tag which is []
        // ORIENTATION_TOPLEFT (row 0 top, col 0 lhs) type. The TIFF []
        // reader parses this tag and sets the internal TIFF image []
        // orientation accordingly.  To overwrite this orientation with a vtk[]
        // convention of ORIENTATION_BOTLEFT (row 0 bottom, col 0 lhs ), invoke[]
        // SetOrientationType method with parameter value of 4.[]
        image1.SetOrientationType((uint)4);
        image1.Update();
        sp = new vtkStructuredPoints();
        sp.SetDimensions(image1.GetOutput().GetDimensions()[0], image1.GetOutput().GetDimensions()[1], image1.GetOutput().GetDimensions()[2]);
        sp.SetExtent(
            image1.GetOutput().GetExtent()[0],
            image1.GetOutput().GetExtent()[1],
            image1.GetOutput().GetExtent()[2],
            image1.GetOutput().GetExtent()[3],
            image1.GetOutput().GetExtent()[4],
            image1.GetOutput().GetExtent()[5]);
        //sp.SetScalarType((int)image1.GetOutput().GetScalarType());
        //sp.SetNumberOfScalarComponents((int)image1.GetOutput().GetNumberOfScalarComponents());
        vtkDataObject.SetPointDataActiveScalarInfo(sp.GetInformation(), (int)image1.GetOutput().GetScalarType(), (int)image1.GetOutput().GetNumberOfScalarComponents());

        sp.GetPointData().SetScalars((vtkDataArray)image1.GetOutput().GetPointData().GetScalars());
        luminance = new vtkImageLuminance();
        luminance.SetInputData((vtkDataObject)sp);
        //[]
        // write to the temp directory if possible, otherwise use .[]
        //[]
        dir = ".";
        dir = TclToCsScriptTestDriver.GetTempDirectory();


        // make sure it is writeable first[]
        try
        {
            channel       = new StreamWriter("" + (dir.ToString()) + "/test.tmp");
            tryCatchError = "NOERROR";
        }
        catch (Exception)
        { tryCatchError = "ERROR"; }

        if (tryCatchError.Equals("NOERROR"))
        {
            channel.Close();
            File.Delete("" + (dir.ToString()) + "/test.tmp");
            tiff1 = new vtkTIFFWriter();
            tiff1.SetInputConnection((vtkAlgorithmOutput)image1.GetOutputPort());
            tiff1.SetFileName((string)"" + (dir.ToString()) + "/tiff1.tif");
            tiff2 = new vtkTIFFWriter();
            tiff2.SetInputConnection((vtkAlgorithmOutput)luminance.GetOutputPort());
            tiff2.SetFileName((string)"" + (dir.ToString()) + "/tiff2.tif");
            bmp1 = new vtkBMPWriter();
            bmp1.SetInputConnection((vtkAlgorithmOutput)image1.GetOutputPort());
            bmp1.SetFileName((string)"" + (dir.ToString()) + "/bmp1.bmp");
            bmp2 = new vtkBMPWriter();
            bmp2.SetInputConnection((vtkAlgorithmOutput)luminance.GetOutputPort());
            bmp2.SetFileName((string)"" + (dir.ToString()) + "/bmp2.bmp");
            pnm1 = new vtkPNMWriter();
            pnm1.SetInputConnection((vtkAlgorithmOutput)image1.GetOutputPort());
            pnm1.SetFileName((string)"" + (dir.ToString()) + "/pnm1.pnm");
            pnm2 = new vtkPNMWriter();
            pnm2.SetInputConnection((vtkAlgorithmOutput)luminance.GetOutputPort());
            pnm2.SetFileName((string)"" + (dir.ToString()) + "/pnm2.pnm");
            psw1 = new vtkPostScriptWriter();
            psw1.SetInputConnection((vtkAlgorithmOutput)image1.GetOutputPort());
            psw1.SetFileName((string)"" + (dir.ToString()) + "/psw1.ps");
            psw2 = new vtkPostScriptWriter();
            psw2.SetInputConnection((vtkAlgorithmOutput)luminance.GetOutputPort());
            psw2.SetFileName((string)"" + (dir.ToString()) + "/psw2.ps");
            pngw1 = new vtkPNGWriter();
            pngw1.SetInputConnection((vtkAlgorithmOutput)image1.GetOutputPort());
            pngw1.SetFileName((string)"" + (dir.ToString()) + "/pngw1.png");
            pngw2 = new vtkPNGWriter();
            pngw2.SetInputConnection((vtkAlgorithmOutput)luminance.GetOutputPort());
            pngw2.SetFileName((string)"" + (dir.ToString()) + "/pngw2.png");
            jpgw1 = new vtkJPEGWriter();
            jpgw1.SetInputConnection((vtkAlgorithmOutput)image1.GetOutputPort());
            jpgw1.SetFileName((string)"" + (dir.ToString()) + "/jpgw1.jpg");
            jpgw2 = new vtkJPEGWriter();
            jpgw2.SetInputConnection((vtkAlgorithmOutput)luminance.GetOutputPort());
            jpgw2.SetFileName((string)"" + (dir.ToString()) + "/jpgw2.jpg");
            tiff1.Write();
            tiff2.Write();
            bmp1.Write();
            bmp2.Write();
            pnm1.Write();
            pnm2.Write();
            psw1.Write();
            psw2.Write();
            pngw1.Write();
            pngw2.Write();
            jpgw1.Write();
            jpgw2.Write();
            File.Delete("" + (dir.ToString()) + "/tiff1.tif");
            File.Delete("" + (dir.ToString()) + "/tiff2.tif");
            File.Delete("" + (dir.ToString()) + "/bmp1.bmp");
            File.Delete("" + (dir.ToString()) + "/bmp2.bmp");
            File.Delete("" + (dir.ToString()) + "/pnm1.pnm");
            File.Delete("" + (dir.ToString()) + "/pnm2.pnm");
            File.Delete("" + (dir.ToString()) + "/psw1.ps");
            File.Delete("" + (dir.ToString()) + "/psw2.ps");
            File.Delete("" + (dir.ToString()) + "/pngw1.png");
            File.Delete("" + (dir.ToString()) + "/pngw2.png");
            File.Delete("" + (dir.ToString()) + "/jpgw1.jpg");
            File.Delete("" + (dir.ToString()) + "/jpgw2.jpg");
        }


        viewer = new vtkImageViewer();
        viewer.SetInputConnection((vtkAlgorithmOutput)luminance.GetOutputPort());
        viewer.SetColorWindow((double)255);
        viewer.SetColorLevel((double)127.5);
        viewer.Render();

//deleteAllVTKObjects();
    }
 ///<summary> A Set Method for Static Variables </summary>
 public static void Setsp(vtkStructuredPoints toSet)
 {
     sp = toSet;
 }
    /// <summary>
    /// The main entry method called by the CSharp driver
    /// </summary>
    /// <param name="argv"></param>
    public static void AVTestImageWriters(String [] argv)
    {
        //Prefix Content is: ""

          // Image pipeline[]
          image1 = new vtkTIFFReader();
          image1.SetFileName((string)"" + (VTK_DATA_ROOT.ToString()) + "/Data/beach.tif");
          // "beach.tif" image contains ORIENTATION tag which is []
          // ORIENTATION_TOPLEFT (row 0 top, col 0 lhs) type. The TIFF []
          // reader parses this tag and sets the internal TIFF image []
          // orientation accordingly.  To overwrite this orientation with a vtk[]
          // convention of ORIENTATION_BOTLEFT (row 0 bottom, col 0 lhs ), invoke[]
          // SetOrientationType method with parameter value of 4.[]
          image1.SetOrientationType((uint)4);
          image1.Update();
          sp = new vtkStructuredPoints();
          sp.SetDimensions(image1.GetOutput().GetDimensions()[0],image1.GetOutput().GetDimensions()[1],image1.GetOutput().GetDimensions()[2]);
          sp.SetExtent(
          image1.GetOutput().GetExtent()[0],
          image1.GetOutput().GetExtent()[1],
          image1.GetOutput().GetExtent()[2],
          image1.GetOutput().GetExtent()[3],
          image1.GetOutput().GetExtent()[4],
          image1.GetOutput().GetExtent()[5]);
          //sp.SetScalarType((int)image1.GetOutput().GetScalarType());
          //sp.SetNumberOfScalarComponents((int)image1.GetOutput().GetNumberOfScalarComponents());
          vtkDataObject.SetPointDataActiveScalarInfo(sp.GetInformation(), (int)image1.GetOutput().GetScalarType(), (int)image1.GetOutput().GetNumberOfScalarComponents());

          sp.GetPointData().SetScalars((vtkDataArray)image1.GetOutput().GetPointData().GetScalars());
          luminance = new vtkImageLuminance();
          luminance.SetInputData((vtkDataObject)sp);
          //[]
          // write to the temp directory if possible, otherwise use .[]
          //[]
          dir = ".";
          dir = TclToCsScriptTestDriver.GetTempDirectory();

          // make sure it is writeable first[]
          try
          {
         channel = new StreamWriter("" + (dir.ToString()) + "/test.tmp");
          tryCatchError = "NOERROR";
          }
          catch(Exception)
          {tryCatchError = "ERROR";}

        if(tryCatchError.Equals("NOERROR"))
          {
          channel.Close();
          File.Delete("" + (dir.ToString()) + "/test.tmp");
          tiff1 = new vtkTIFFWriter();
          tiff1.SetInputConnection((vtkAlgorithmOutput)image1.GetOutputPort());
          tiff1.SetFileName((string)"" + (dir.ToString()) + "/tiff1.tif");
          tiff2 = new vtkTIFFWriter();
          tiff2.SetInputConnection((vtkAlgorithmOutput)luminance.GetOutputPort());
          tiff2.SetFileName((string)"" + (dir.ToString()) + "/tiff2.tif");
          bmp1 = new vtkBMPWriter();
          bmp1.SetInputConnection((vtkAlgorithmOutput)image1.GetOutputPort());
          bmp1.SetFileName((string)"" + (dir.ToString()) + "/bmp1.bmp");
          bmp2 = new vtkBMPWriter();
          bmp2.SetInputConnection((vtkAlgorithmOutput)luminance.GetOutputPort());
          bmp2.SetFileName((string)"" + (dir.ToString()) + "/bmp2.bmp");
          pnm1 = new vtkPNMWriter();
          pnm1.SetInputConnection((vtkAlgorithmOutput)image1.GetOutputPort());
          pnm1.SetFileName((string)"" + (dir.ToString()) + "/pnm1.pnm");
          pnm2 = new vtkPNMWriter();
          pnm2.SetInputConnection((vtkAlgorithmOutput)luminance.GetOutputPort());
          pnm2.SetFileName((string)"" + (dir.ToString()) + "/pnm2.pnm");
          psw1 = new vtkPostScriptWriter();
          psw1.SetInputConnection((vtkAlgorithmOutput)image1.GetOutputPort());
          psw1.SetFileName((string)"" + (dir.ToString()) + "/psw1.ps");
          psw2 = new vtkPostScriptWriter();
          psw2.SetInputConnection((vtkAlgorithmOutput)luminance.GetOutputPort());
          psw2.SetFileName((string)"" + (dir.ToString()) + "/psw2.ps");
          pngw1 = new vtkPNGWriter();
          pngw1.SetInputConnection((vtkAlgorithmOutput)image1.GetOutputPort());
          pngw1.SetFileName((string)"" + (dir.ToString()) + "/pngw1.png");
          pngw2 = new vtkPNGWriter();
          pngw2.SetInputConnection((vtkAlgorithmOutput)luminance.GetOutputPort());
          pngw2.SetFileName((string)"" + (dir.ToString()) + "/pngw2.png");
          jpgw1 = new vtkJPEGWriter();
          jpgw1.SetInputConnection((vtkAlgorithmOutput)image1.GetOutputPort());
          jpgw1.SetFileName((string)"" + (dir.ToString()) + "/jpgw1.jpg");
          jpgw2 = new vtkJPEGWriter();
          jpgw2.SetInputConnection((vtkAlgorithmOutput)luminance.GetOutputPort());
          jpgw2.SetFileName((string)"" + (dir.ToString()) + "/jpgw2.jpg");
          tiff1.Write();
          tiff2.Write();
          bmp1.Write();
          bmp2.Write();
          pnm1.Write();
          pnm2.Write();
          psw1.Write();
          psw2.Write();
          pngw1.Write();
          pngw2.Write();
          jpgw1.Write();
          jpgw2.Write();
          File.Delete("" + (dir.ToString()) + "/tiff1.tif");
          File.Delete("" + (dir.ToString()) + "/tiff2.tif");
          File.Delete("" + (dir.ToString()) + "/bmp1.bmp");
          File.Delete("" + (dir.ToString()) + "/bmp2.bmp");
          File.Delete("" + (dir.ToString()) + "/pnm1.pnm");
          File.Delete("" + (dir.ToString()) + "/pnm2.pnm");
          File.Delete("" + (dir.ToString()) + "/psw1.ps");
          File.Delete("" + (dir.ToString()) + "/psw2.ps");
          File.Delete("" + (dir.ToString()) + "/pngw1.png");
          File.Delete("" + (dir.ToString()) + "/pngw2.png");
          File.Delete("" + (dir.ToString()) + "/jpgw1.jpg");
          File.Delete("" + (dir.ToString()) + "/jpgw2.jpg");
        }

          viewer = new vtkImageViewer();
          viewer.SetInputConnection((vtkAlgorithmOutput)luminance.GetOutputPort());
          viewer.SetColorWindow((double)255);
          viewer.SetColorLevel((double)127.5);
          viewer.Render();

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