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(); }
/// <summary> /// The main entry method called by the CSharp driver /// </summary> /// <param name="argv"></param> public static void AVTestPolygonWriters(String [] argv) { //Prefix Content is: "" // Create the RenderWindow, Renderer and both Actors[] //[] ren1 = vtkRenderer.New(); renWin = vtkRenderWindow.New(); renWin.AddRenderer((vtkRenderer)ren1); iren = new vtkRenderWindowInteractor(); iren.SetRenderWindow((vtkRenderWindow)renWin); // read data[] //[] input = new vtkPolyDataReader(); input.SetFileName((string)"" + (VTK_DATA_ROOT.ToString()) + "/Data/brainImageSmooth.vtk"); //[] // generate vectors[] clean = new vtkCleanPolyData(); clean.SetInputConnection((vtkAlgorithmOutput)input.GetOutputPort()); smooth = new vtkWindowedSincPolyDataFilter(); smooth.SetInputConnection((vtkAlgorithmOutput)clean.GetOutputPort()); smooth.GenerateErrorVectorsOn(); smooth.GenerateErrorScalarsOn(); smooth.Update(); mapper = vtkPolyDataMapper.New(); mapper.SetInputConnection((vtkAlgorithmOutput)smooth.GetOutputPort()); mapper.SetScalarRange((double)((vtkDataSet)smooth.GetOutput()).GetScalarRange()[0], (double)((vtkDataSet)smooth.GetOutput()).GetScalarRange()[1]); brain = new vtkActor(); brain.SetMapper((vtkMapper)mapper); // Add the actors to the renderer, set the background and size[] //[] ren1.AddActor((vtkProp)brain); renWin.SetSize((int)320,(int)240); ren1.GetActiveCamera().SetPosition((double)149.653,(double)-65.3464,(double)96.0401); ren1.GetActiveCamera().SetFocalPoint((double)146.003,(double)22.3839,(double)0.260541); ren1.GetActiveCamera().SetViewAngle((double)30); ren1.GetActiveCamera().SetViewUp((double)-0.255578,(double)-0.717754,(double)-0.647695); ren1.GetActiveCamera().SetClippingRange((double)79.2526,(double)194.052); iren.Initialize(); renWin.Render(); // render the image[] //[] // prevent the tk window from showing up then start the event loop[] //[] // If the current directory is writable, then test the witers[] //[] try { channel = new StreamWriter("test.tmp"); tryCatchError = "NOERROR"; } catch(Exception) {tryCatchError = "ERROR";} if(tryCatchError.Equals("NOERROR")) { channel.Close(); File.Delete("test.tmp"); //[] //[] // test the writers[] dsw = new vtkDataSetWriter(); dsw.SetInputConnection((vtkAlgorithmOutput)smooth.GetOutputPort()); dsw.SetFileName((string)"brain.dsw"); dsw.Write(); File.Delete("brain.dsw"); pdw = new vtkPolyDataWriter(); pdw.SetInputConnection((vtkAlgorithmOutput)smooth.GetOutputPort()); pdw.SetFileName((string)"brain.pdw"); pdw.Write(); File.Delete("brain.pdw"); iv = new vtkIVWriter(); iv.SetInputConnection((vtkAlgorithmOutput)smooth.GetOutputPort()); iv.SetFileName((string)"brain.iv"); iv.Write(); File.Delete("brain.iv"); //[] // the next writers only handle triangles[] triangles = new vtkTriangleFilter(); triangles.SetInputConnection((vtkAlgorithmOutput)smooth.GetOutputPort()); iv2 = new vtkIVWriter(); iv2.SetInputConnection((vtkAlgorithmOutput)triangles.GetOutputPort()); iv2.SetFileName((string)"brain2.iv"); iv2.Write(); File.Delete("brain2.iv"); edges = new vtkExtractEdges(); edges.SetInputConnection((vtkAlgorithmOutput)triangles.GetOutputPort()); iv3 = new vtkIVWriter(); iv3.SetInputConnection((vtkAlgorithmOutput)edges.GetOutputPort()); iv3.SetFileName((string)"brain3.iv"); iv3.Write(); File.Delete("brain3.iv"); byu = new vtkBYUWriter(); byu.SetGeometryFileName((string)"brain.g"); byu.SetScalarFileName((string)"brain.s"); byu.SetDisplacementFileName((string)"brain.d"); byu.SetInputConnection((vtkAlgorithmOutput)triangles.GetOutputPort()); byu.Write(); File.Delete("brain.g"); File.Delete("brain.s"); File.Delete("brain.d"); mcubes = new vtkMCubesWriter(); mcubes.SetInputConnection((vtkAlgorithmOutput)triangles.GetOutputPort()); mcubes.SetFileName((string)"brain.tri"); mcubes.SetLimitsFileName((string)"brain.lim"); mcubes.Write(); File.Delete("brain.lim"); File.Delete("brain.tri"); stl = new vtkSTLWriter(); stl.SetInputConnection((vtkAlgorithmOutput)triangles.GetOutputPort()); stl.SetFileName((string)"brain.stl"); stl.Write(); File.Delete("brain.stl"); stlBinary = new vtkSTLWriter(); stlBinary.SetInputConnection((vtkAlgorithmOutput)triangles.GetOutputPort()); stlBinary.SetFileName((string)"brainBinary.stl"); stlBinary.SetFileType((int)2); stlBinary.Write(); File.Delete("brainBinary.stl"); } //deleteAllVTKObjects(); }
///<summary> A Set Method for Static Variables </summary> public static void Setsmooth(vtkWindowedSincPolyDataFilter toSet) { smooth = toSet; }
///<summary> A Set Method for Static Variables </summary> public static void Setsmooth(vtkWindowedSincPolyDataFilter toSet) { smooth = toSet; }
/// <summary> /// The main entry method called by the CSharp driver /// </summary> /// <param name="argv"></param> public static void AVTestPolygonWriters(String [] argv) { //Prefix Content is: "" // Create the RenderWindow, Renderer and both Actors[] //[] ren1 = vtkRenderer.New(); renWin = vtkRenderWindow.New(); renWin.AddRenderer((vtkRenderer)ren1); iren = new vtkRenderWindowInteractor(); iren.SetRenderWindow((vtkRenderWindow)renWin); // read data[] //[] input = new vtkPolyDataReader(); input.SetFileName((string)"" + (VTK_DATA_ROOT.ToString()) + "/Data/brainImageSmooth.vtk"); //[] // generate vectors[] clean = new vtkCleanPolyData(); clean.SetInputConnection((vtkAlgorithmOutput)input.GetOutputPort()); smooth = new vtkWindowedSincPolyDataFilter(); smooth.SetInputConnection((vtkAlgorithmOutput)clean.GetOutputPort()); smooth.GenerateErrorVectorsOn(); smooth.GenerateErrorScalarsOn(); smooth.Update(); mapper = vtkPolyDataMapper.New(); mapper.SetInputConnection((vtkAlgorithmOutput)smooth.GetOutputPort()); mapper.SetScalarRange((double)((vtkDataSet)smooth.GetOutput()).GetScalarRange()[0], (double)((vtkDataSet)smooth.GetOutput()).GetScalarRange()[1]); brain = new vtkActor(); brain.SetMapper((vtkMapper)mapper); // Add the actors to the renderer, set the background and size[] //[] ren1.AddActor((vtkProp)brain); renWin.SetSize((int)320, (int)240); ren1.GetActiveCamera().SetPosition((double)149.653, (double)-65.3464, (double)96.0401); ren1.GetActiveCamera().SetFocalPoint((double)146.003, (double)22.3839, (double)0.260541); ren1.GetActiveCamera().SetViewAngle((double)30); ren1.GetActiveCamera().SetViewUp((double)-0.255578, (double)-0.717754, (double)-0.647695); ren1.GetActiveCamera().SetClippingRange((double)79.2526, (double)194.052); iren.Initialize(); renWin.Render(); // render the image[] //[] // prevent the tk window from showing up then start the event loop[] //[] // If the current directory is writable, then test the witers[] //[] try { channel = new StreamWriter("test.tmp"); tryCatchError = "NOERROR"; } catch (Exception) { tryCatchError = "ERROR"; } if (tryCatchError.Equals("NOERROR")) { channel.Close(); File.Delete("test.tmp"); //[] //[] // test the writers[] dsw = new vtkDataSetWriter(); dsw.SetInputConnection((vtkAlgorithmOutput)smooth.GetOutputPort()); dsw.SetFileName((string)"brain.dsw"); dsw.Write(); File.Delete("brain.dsw"); pdw = new vtkPolyDataWriter(); pdw.SetInputConnection((vtkAlgorithmOutput)smooth.GetOutputPort()); pdw.SetFileName((string)"brain.pdw"); pdw.Write(); File.Delete("brain.pdw"); iv = new vtkIVWriter(); iv.SetInputConnection((vtkAlgorithmOutput)smooth.GetOutputPort()); iv.SetFileName((string)"brain.iv"); iv.Write(); File.Delete("brain.iv"); //[] // the next writers only handle triangles[] triangles = new vtkTriangleFilter(); triangles.SetInputConnection((vtkAlgorithmOutput)smooth.GetOutputPort()); iv2 = new vtkIVWriter(); iv2.SetInputConnection((vtkAlgorithmOutput)triangles.GetOutputPort()); iv2.SetFileName((string)"brain2.iv"); iv2.Write(); File.Delete("brain2.iv"); edges = new vtkExtractEdges(); edges.SetInputConnection((vtkAlgorithmOutput)triangles.GetOutputPort()); iv3 = new vtkIVWriter(); iv3.SetInputConnection((vtkAlgorithmOutput)edges.GetOutputPort()); iv3.SetFileName((string)"brain3.iv"); iv3.Write(); File.Delete("brain3.iv"); byu = new vtkBYUWriter(); byu.SetGeometryFileName((string)"brain.g"); byu.SetScalarFileName((string)"brain.s"); byu.SetDisplacementFileName((string)"brain.d"); byu.SetInputConnection((vtkAlgorithmOutput)triangles.GetOutputPort()); byu.Write(); File.Delete("brain.g"); File.Delete("brain.s"); File.Delete("brain.d"); mcubes = new vtkMCubesWriter(); mcubes.SetInputConnection((vtkAlgorithmOutput)triangles.GetOutputPort()); mcubes.SetFileName((string)"brain.tri"); mcubes.SetLimitsFileName((string)"brain.lim"); mcubes.Write(); File.Delete("brain.lim"); File.Delete("brain.tri"); stl = new vtkSTLWriter(); stl.SetInputConnection((vtkAlgorithmOutput)triangles.GetOutputPort()); stl.SetFileName((string)"brain.stl"); stl.Write(); File.Delete("brain.stl"); stlBinary = new vtkSTLWriter(); stlBinary.SetInputConnection((vtkAlgorithmOutput)triangles.GetOutputPort()); stlBinary.SetFileName((string)"brainBinary.stl"); stlBinary.SetFileType((int)2); stlBinary.Write(); File.Delete("brainBinary.stl"); } //deleteAllVTKObjects(); }
//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 }