public void SetMapper(vtkPolyData data) { vtkVertexGlyphFilter filter = vtkVertexGlyphFilter.New(); filter.SetInput(data); filter.Update(); polyDataMapper = vtkPolyDataMapper.New(); polyDataMapper.SetInputConnection(filter.GetOutputPort()); polyDataMapper.Update(); }
public void ReadPointIntoObject(RenderWindowControl renderWindowControl, List <nvmPointModel> listPointModel) { vtkUnsignedCharArray colors = vtkUnsignedCharArray.New(); colors.SetNumberOfComponents(3); colors.SetName("Colors"); vtkPoints points = vtkPoints.New(); foreach (var point in listPointModel) { colors.InsertNextValue(byte.Parse(point.color.X.ToString(), CultureInfo.InvariantCulture)); colors.InsertNextValue(byte.Parse(point.color.Y.ToString(), CultureInfo.InvariantCulture)); colors.InsertNextValue(byte.Parse(point.color.Z.ToString(), CultureInfo.InvariantCulture)); points.InsertNextPoint( double.Parse(point.position.X.ToString(), CultureInfo.InvariantCulture), double.Parse(point.position.Y.ToString(), CultureInfo.InvariantCulture), double.Parse(point.position.Z.ToString(), CultureInfo.InvariantCulture)); } vtkPolyData polydata = vtkPolyData.New(); polydata.SetPoints(points); polydata.GetPointData().SetScalars(colors); vtkVertexGlyphFilter glyphFilter = vtkVertexGlyphFilter.New(); glyphFilter.SetInputConnection(polydata.GetProducerPort()); // Visualize vtkPolyDataMapper mapper = vtkPolyDataMapper.New(); mapper.SetInputConnection(glyphFilter.GetOutputPort()); vtkActor actor = vtkActor.New(); actor.SetMapper(mapper); actor.GetProperty().SetPointSize(2); // get a reference to the renderwindow of our renderWindowControl1 vtkRenderWindow renderWindow = renderWindowControl.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); renderer.ResetCamera(); }
private void ReadPlainText() { // 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\teapot.xyz"); FileStream fs = null; StreamReader sr = null; String sLineBuffer; String[] sXYZ; char[] chDelimiter = new char[] { ' ', '\t', ';' }; double[] xyz = new double[3]; vtkPoints points = vtkPoints.New(); int cnt = 0; try { // in case file must be open in another application too use "FileShare.ReadWrite" fs = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite); sr = new StreamReader(fs); while (!sr.EndOfStream) { sLineBuffer = sr.ReadLine(); cnt++; sXYZ = sLineBuffer.Split(chDelimiter, StringSplitOptions.RemoveEmptyEntries); if (sXYZ == null || sXYZ.Length != 3) { MessageBox.Show("data seems to be in wrong format at line " + cnt, "Format Exception", MessageBoxButtons.OK); return; } xyz[0] = double.Parse(sXYZ[0], CultureInfo.InvariantCulture); xyz[1] = double.Parse(sXYZ[1], CultureInfo.InvariantCulture); xyz[2] = double.Parse(sXYZ[2], CultureInfo.InvariantCulture); points.InsertNextPoint(xyz[0], xyz[1], xyz[2]); } vtkPolyData polydata = vtkPolyData.New(); polydata.SetPoints(points); vtkVertexGlyphFilter glyphFilter = vtkVertexGlyphFilter.New(); glyphFilter.SetInputConnection(polydata.GetProducerPort()); // Visualize vtkPolyDataMapper mapper = vtkPolyDataMapper.New(); mapper.SetInputConnection(glyphFilter.GetOutputPort()); vtkActor actor = vtkActor.New(); actor.SetMapper(mapper); actor.GetProperty().SetPointSize(4); actor.GetProperty().SetColor(1, 0.5, 0); // 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); } catch (IOException ex) { MessageBox.Show(ex.Message, "IOException", MessageBoxButtons.OK); } finally { if (sr != null) { sr.Close(); sr.Dispose(); sr = null; } } }
private void RenderXYZColor() { FileStream fs = null; StreamReader sr = null; String sLineBuffer; String[] sXYZ; char[] chDelimiter = new char[] { ' ', '\t', ';' }; double[] xyz = new double[3]; double[] rgb = new double[3]; vtkPoints points = vtkPoints.New(); vtkPoints colors = vtkPoints.New(); int cnt = 0; try { // in case file must be open in another application too use "FileShare.ReadWrite" fs = new FileStream(m_FileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite); sr = new StreamReader(fs); vtkDoubleArray colorScalor = new vtkDoubleArray(); int n = 1; while (!sr.EndOfStream) { sLineBuffer = sr.ReadLine(); cnt++; sXYZ = sLineBuffer.Split(chDelimiter, StringSplitOptions.RemoveEmptyEntries); if (sXYZ == null || sXYZ.Length != 6) { MessageBox.Show("data seems to be in wrong format at line " + cnt, "Format Exception", MessageBoxButtons.OK); return; } xyz[0] = double.Parse(sXYZ[0], CultureInfo.InvariantCulture) * 11100; xyz[1] = double.Parse(sXYZ[1], CultureInfo.InvariantCulture) * 11100; xyz[2] = double.Parse(sXYZ[2], CultureInfo.InvariantCulture); rgb[0] = double.Parse(sXYZ[0], CultureInfo.InvariantCulture); rgb[1] = double.Parse(sXYZ[1], CultureInfo.InvariantCulture); rgb[2] = double.Parse(sXYZ[2], CultureInfo.InvariantCulture); points.InsertNextPoint(xyz[0], xyz[1], xyz[2]); colors.InsertNextPoint(rgb[0], rgb[1], rgb[2]); colorScalor.InsertNextTuple1(n++); } vtkPolyData polydata = vtkPolyData.New(); polydata.SetPoints(points); polydata.GetPointData().SetScalars(colorScalor); //设置点的Scalar(标量)属性 vtkVertexGlyphFilter glyphFilter = vtkVertexGlyphFilter.New(); glyphFilter.SetInputConnection(polydata.GetProducerPort()); vtkLookupTable lookupTable = new vtkLookupTable(); lookupTable.SetNumberOfColors(n); // SetSetTableValue(vtkIdType indx, double r, double g, double b, double a); Random random = new Random(); for (int i = 0; i < n; i++) { double[] tmp = colors.GetPoint(i); double r = tmp[0]; double g = tmp[1]; double b = tmp[2]; lookupTable.SetTableValue(i, r, g, b, 1); } // Visualize vtkPolyDataMapper mapper = vtkPolyDataMapper.New(); mapper.SetInputConnection(glyphFilter.GetOutputPort()); mapper.SetLookupTable(lookupTable); mapper.SetScalarRange(1, n); vtkActor actor = vtkActor.New(); actor.SetMapper(mapper); actor.GetProperty().SetPointSize(1); //actor.GetProperty().SetColor(1, 0.5, 0); // add our actor to the renderer m_Renderer.AddActor(actor); imgPropList.Add(actor); m_Renderer.ResetCamera(); //Rerender the screen m_RenderWindow.Render(); m_Renderer.Render(); } catch (IOException ex) { MessageBox.Show(ex.Message, "IOException", MessageBoxButtons.OK); } finally { if (sr != null) { sr.Close(); sr.Dispose(); sr = null; } } }