private void RenderVTP() { // reader // Read all the data from the file vtkXMLPolyDataReader reader = vtkXMLPolyDataReader.New(); reader.SetFileName(m_FileName); reader.Update(); // here we read the file actually // mapper vtkDataSetMapper mapper = vtkDataSetMapper.New(); mapper.SetInputConnection(reader.GetOutputPort()); // actor vtkActor actor = vtkActor.New(); actor.SetMapper(mapper); // add our actor to the renderer imgPropList.Add(actor); m_Renderer.AddActor(actor); m_Renderer.ResetCamera(); //Rerender the screen m_RenderWindow.Render(); m_Renderer.Render(); }
private void Curvature() { // 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(); // note: one should use a coarse surface mesh to achieve a good visual effect string filePath = System.IO.Path.Combine(root, @"Data\bunny.vtp"); // Create a polydata vtkXMLPolyDataReader reader = vtkXMLPolyDataReader.New(); reader.SetFileName(filePath); vtkCurvatures curvaturesFilter = vtkCurvatures.New(); curvaturesFilter.SetInputConnection(reader.GetOutputPort()); //curvaturesFilter.SetCurvatureTypeToGaussian(); //curvaturesFilter.SetCurvatureTypeToMean(); //curvaturesFilter.SetCurvatureTypeToMaximum(); curvaturesFilter.SetCurvatureTypeToMinimum(); // To inspect more closely, if required vtkXMLPolyDataWriter writer = vtkXMLPolyDataWriter.New(); writer.SetInputConnection(curvaturesFilter.GetOutputPort()); writer.SetFileName(System.IO.Path.Combine(root, @"Data\gauss.vtp")); writer.Write(); // Create a mapper and actor vtkPolyDataMapper mapper = vtkPolyDataMapper.New(); mapper.SetInputConnection(curvaturesFilter.GetOutputPort()); double[] range = curvaturesFilter.GetOutput().GetScalarRange(); mapper.SetScalarRange(range[0], range[1]); 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.0, 0.0, 0.0); // add our actor to the renderer renderer.AddActor(actor); }
private void ReadPolyData() { // 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(); // string filePath = System.IO.Path.Combine(root, @"Data\SyntheticPolyline.vtp"); // string filePath = System.IO.Path.Combine(root, @"Data\uniform-001371-5x5x5.vtp"); // string filePath = System.IO.Path.Combine(root, @"Data\political.vtp"); // string filePath = System.IO.Path.Combine(root, @"Data\filledContours.vtp"); // string filePath = System.IO.Path.Combine(root, @"Data\disk_out_ref_surface.vtp"); string filePath = System.IO.Path.Combine(root, @"Data\cow.vtp"); // reader // Read all the data from the file vtkXMLPolyDataReader reader = vtkXMLPolyDataReader.New(); if (reader.CanReadFile(filePath) == 0) { MessageBox.Show("Cannot read file \"" + filePath + "\"", "Error", MessageBoxButtons.OK); return; } reader.SetFileName(filePath); reader.Update(); // here we read the file actually // mapper vtkDataSetMapper mapper = vtkDataSetMapper.New(); mapper.SetInputConnection(reader.GetOutputPort()); // actor 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); }
private void Subdivision(string filePath) { vtkPolyData originalMesh; if (filePath != null) { vtkXMLPolyDataReader reader = vtkXMLPolyDataReader.New(); reader.SetFileName(filePath); // Subdivision filters only work on triangles vtkTriangleFilter triangles = vtkTriangleFilter.New(); triangles.SetInputConnection(reader.GetOutputPort()); triangles.Update(); originalMesh = triangles.GetOutput(); } else { vtkSphereSource sphereSource = vtkSphereSource.New(); sphereSource.Update(); originalMesh = sphereSource.GetOutput(); } Debug.WriteLine("Before subdivision"); Debug.WriteLine(" There are " + originalMesh.GetNumberOfPoints() + " points."); Debug.WriteLine(" There are " + originalMesh.GetNumberOfPolys() + " triangles."); int numberOfViewports = 3; vtkRenderWindow renderWindow = renderWindowControl1.RenderWindow; this.Size = new System.Drawing.Size(200 * numberOfViewports + 12, 252); this.Text += " - Subdivision"; Random rnd = new Random(2); int numberOfSubdivisions = 2; // Create one text property for all vtkTextProperty textProperty = vtkTextProperty.New(); textProperty.SetFontSize(14); textProperty.SetJustificationToCentered(); for (int i = 0; i < numberOfViewports; i++) { // Note: Here we create a superclass pointer (vtkPolyDataAlgorithm) so that we can easily instantiate different // types of subdivision filters. Typically you would not want to do this, but rather create the pointer to be the type // filter you will actually use, e.g. // <vtkLinearSubdivisionFilter> subdivisionFilter = <vtkLinearSubdivisionFilter>.New(); vtkPolyDataAlgorithm subdivisionFilter; switch (i) { case 0: subdivisionFilter = vtkLinearSubdivisionFilter.New(); ((vtkLinearSubdivisionFilter)subdivisionFilter).SetNumberOfSubdivisions(numberOfSubdivisions); break; case 1: subdivisionFilter = vtkLoopSubdivisionFilter.New(); ((vtkLoopSubdivisionFilter)subdivisionFilter).SetNumberOfSubdivisions(numberOfSubdivisions); break; case 2: subdivisionFilter = vtkButterflySubdivisionFilter.New(); ((vtkButterflySubdivisionFilter)subdivisionFilter).SetNumberOfSubdivisions(numberOfSubdivisions); break; default: subdivisionFilter = vtkLinearSubdivisionFilter.New(); ((vtkLinearSubdivisionFilter)subdivisionFilter).SetNumberOfSubdivisions(numberOfSubdivisions); break; } #if VTK_MAJOR_VERSION_5 subdivisionFilter.SetInputConnection(originalMesh.GetProducerPort()); #else subdivisionFilter.SetInputData(originalMesh); #endif subdivisionFilter.Update(); vtkRenderer renderer = vtkRenderer.New(); renderWindow.AddRenderer(renderer); renderer.SetViewport((float)i / numberOfViewports, 0, (float)(i + 1) / numberOfViewports, 1); renderer.SetBackground(.2 + rnd.NextDouble() / 8, .3 + rnd.NextDouble() / 8, .4 + rnd.NextDouble() / 8); vtkTextMapper textMapper = vtkTextMapper.New(); vtkActor2D textActor = vtkActor2D.New(); textMapper.SetInput(subdivisionFilter.GetClassName()); textMapper.SetTextProperty(textProperty); textActor.SetMapper(textMapper); textActor.SetPosition(100, 16); //Create a mapper and actor vtkPolyDataMapper mapper = vtkPolyDataMapper.New(); mapper.SetInputConnection(subdivisionFilter.GetOutputPort()); vtkActor actor = vtkActor.New(); actor.SetMapper(mapper); renderer.AddActor(actor); renderer.AddActor(textActor); renderer.ResetCamera(); } renderWindow.Render(); }
private void WritePolyData() { // 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(); string filePath = System.IO.Path.Combine(root, @"Data\poly_test.vtp"); // Create 4 points for a tetrahedron vtkPoints points = vtkPoints.New(); points.InsertNextPoint(0, 0, 0); points.InsertNextPoint(0, 0, 1); points.InsertNextPoint(1, 0, 0); points.InsertNextPoint(0, 1, 0); // Create a polydata object and add the points to it. vtkPolyData polydata = vtkPolyData.New(); polydata.SetPoints(points); // it's not enough only to define points // we need to define faces too // (must be defined in counter clockwise order as viewed from the outside) vtkTriangle face0 = vtkTriangle.New(); face0.GetPointIds().SetId(0, 0); face0.GetPointIds().SetId(1, 2); face0.GetPointIds().SetId(2, 1); vtkTriangle face1 = vtkTriangle.New(); face1.GetPointIds().SetId(0, 0); face1.GetPointIds().SetId(1, 3); face1.GetPointIds().SetId(2, 2); vtkTriangle face2 = vtkTriangle.New(); face2.GetPointIds().SetId(0, 0); face2.GetPointIds().SetId(1, 1); face2.GetPointIds().SetId(2, 3); vtkTriangle face3 = vtkTriangle.New(); face3.GetPointIds().SetId(0, 1); face3.GetPointIds().SetId(1, 2); face3.GetPointIds().SetId(2, 3); vtkCellArray faces = vtkCellArray.New(); faces.InsertNextCell(face0); faces.InsertNextCell(face1); faces.InsertNextCell(face2); faces.InsertNextCell(face3); polydata.SetPolys(faces); // Write the file vtkXMLPolyDataWriter writer = vtkXMLPolyDataWriter.New(); writer.SetFileName(filePath); writer.SetInput(polydata); // Optional - set the mode. The default is binary. //writer.SetDataModeToBinary(); writer.SetDataModeToAscii(); writer.Write(); // Read and display file for verification that it was written correctly vtkXMLPolyDataReader reader = vtkXMLPolyDataReader.New(); if (reader.CanReadFile(filePath) == 0) { MessageBox.Show("Cannot read file \"" + filePath + "\"", "Error", MessageBoxButtons.OK); return; } reader.SetFileName(filePath); reader.Update(); vtkDataSetMapper mapper = vtkDataSetMapper.New(); mapper.SetInputConnection(reader.GetOutputPort()); // actor 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); }
private void FilledContours(string filePath, int numberOfContours) { // Read the file vtkXMLPolyDataReader reader = vtkXMLPolyDataReader.New(); reader.SetFileName(filePath); reader.Update(); // Update so that we can get the scalar range double[] scalarRange = reader.GetOutput().GetPointData().GetScalars().GetRange(); vtkAppendPolyData appendFilledContours = vtkAppendPolyData.New(); double delta = (scalarRange[1] - scalarRange[0]) / (numberOfContours - 1); // Keep the clippers alive List <vtkClipPolyData> clippersLo = new List <vtkClipPolyData>(); List <vtkClipPolyData> clippersHi = new List <vtkClipPolyData>(); for (int i = 0; i < numberOfContours; i++) { double valueLo = scalarRange[0] + i * delta; double valueHi = scalarRange[0] + (i + 1) * delta; clippersLo.Add(vtkClipPolyData.New()); clippersLo[i].SetValue(valueLo); if (i == 0) { clippersLo[i].SetInputConnection(reader.GetOutputPort()); } else { clippersLo[i].SetInputConnection(clippersHi[i - 1].GetOutputPort(1)); } clippersLo[i].InsideOutOff(); clippersLo[i].Update(); clippersHi.Add(vtkClipPolyData.New()); clippersHi[i].SetValue(valueHi); clippersHi[i].SetInputConnection(clippersLo[i].GetOutputPort()); clippersHi[i].GenerateClippedOutputOn(); clippersHi[i].InsideOutOn(); clippersHi[i].Update(); if (clippersHi[i].GetOutput().GetNumberOfCells() == 0) { continue; } vtkFloatArray cd = vtkFloatArray.New(); cd.SetNumberOfComponents(1); cd.SetNumberOfTuples(clippersHi[i].GetOutput().GetNumberOfCells()); cd.FillComponent(0, valueLo); clippersHi[i].GetOutput().GetCellData().SetScalars(cd); appendFilledContours.AddInputConnection(clippersHi[i].GetOutputPort()); } vtkCleanPolyData filledContours = vtkCleanPolyData.New(); filledContours.SetInputConnection(appendFilledContours.GetOutputPort()); vtkLookupTable lut = vtkLookupTable.New(); lut.SetNumberOfTableValues(numberOfContours + 1); lut.Build(); vtkPolyDataMapper contourMapper = vtkPolyDataMapper.New(); contourMapper.SetInputConnection(filledContours.GetOutputPort()); contourMapper.SetScalarRange(scalarRange[0], scalarRange[1]); contourMapper.SetScalarModeToUseCellData(); contourMapper.SetLookupTable(lut); vtkActor contourActor = vtkActor.New(); contourActor.SetMapper(contourMapper); contourActor.GetProperty().SetInterpolationToFlat(); vtkContourFilter contours = vtkContourFilter.New(); contours.SetInputConnection(filledContours.GetOutputPort()); contours.GenerateValues(numberOfContours, scalarRange[0], scalarRange[1]); vtkPolyDataMapper contourLineMapperer = vtkPolyDataMapper.New(); contourLineMapperer.SetInputConnection(contours.GetOutputPort()); contourLineMapperer.SetScalarRange(scalarRange[0], scalarRange[1]); contourLineMapperer.ScalarVisibilityOff(); vtkActor contourLineActor = vtkActor.New(); contourLineActor.SetMapper(contourLineMapperer); contourLineActor.GetProperty().SetLineWidth(2); // 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(contourActor); renderer.AddActor(contourLineActor); }