public static List <List <double[]> > ReadPolyDataList(this string vtpFileName) { vtkPolyData polyData = ReadPolyData(vtpFileName); List <List <double[]> > ret = new List <List <double[]> >(); for (int cellIdx = 0; cellIdx < polyData.GetNumberOfCells(); ++cellIdx) { List <double[]> list = new List <double[]>(); ret.Add(list); vtkCell cell = polyData.GetCell(cellIdx); for (int i = 0; i < cell.GetNumberOfPoints(); ++i) { list.Add(cell.GetPoints().GetPoint(i)); } } polyData.Dispose(); return(ret); }
private void HighLightBadCells() { vtkSphereSource sphereSource = vtkSphereSource.New(); sphereSource.Update(); vtkTriangleFilter triangleFilter = vtkTriangleFilter.New(); triangleFilter.SetInputConnection(sphereSource.GetOutputPort()); triangleFilter.Update(); //Create a mapper and actor vtkDataSetMapper sphereMapper = vtkDataSetMapper.New(); sphereMapper.SetInputConnection(triangleFilter.GetOutputPort()); vtkActor sphereActor = vtkActor.New(); sphereActor.SetMapper(sphereMapper); vtkPolyData mesh = triangleFilter.GetOutput(); Debug.WriteLine("There are " + mesh.GetNumberOfCells() + " cells."); vtkMeshQuality qualityFilter = vtkMeshQuality.New(); #if VTK_MAJOR_VERSION_5 qualityFilter.SetInput(mesh); #else qualityFilter.SetInputData(mesh); #endif qualityFilter.SetTriangleQualityMeasureToArea(); qualityFilter.Update(); vtkDataSet qualityMesh = qualityFilter.GetOutput(); vtkDoubleArray qualityArray = vtkDoubleArray.SafeDownCast(qualityMesh.GetCellData().GetArray("Quality")); Debug.WriteLine("There are " + qualityArray.GetNumberOfTuples() + " values."); for (int i = 0; i < qualityArray.GetNumberOfTuples(); i++) { double val = qualityArray.GetValue(i); Debug.WriteLine("value " + i + ": " + val); } vtkThreshold selectCells = vtkThreshold.New(); selectCells.ThresholdByLower(.02); selectCells.SetInputArrayToProcess( 0, 0, 0, 1, // POINTS = 0, CELLS = 1, NONE = 2, POINTS_THEN_CELLS = 3, VERTICES = 4, EDGES = 5, ROWS = 6 0 // SCALARS = 0, VECTORS = 1, NORMALS = 2, TCOORDS = 3, TENSORS = 4, GLOBALIDS = 5, PEDIGREEIDS = 6, EDGEFLAG = 7 ); #if VTK_MAJOR_VERSION_5 selectCells.SetInput(qualityMesh); #else selectCells.SetInputData(qualityMesh); #endif selectCells.Update(); vtkUnstructuredGrid ug = selectCells.GetOutput(); // Create a mapper and actor vtkDataSetMapper mapper = vtkDataSetMapper.New(); #if VTK_MAJOR_VERSION_5 mapper.SetInput(ug); #else mapper.SetInputData(ug); #endif vtkActor actor = vtkActor.New(); actor.SetMapper(mapper); actor.GetProperty().SetColor(1.0, 0.0, 0.0); actor.GetProperty().SetRepresentationToWireframe(); actor.GetProperty().SetLineWidth(5); // get a reference to the renderwindow of our renderWindowControl1 vtkRenderWindow renderWindow = renderWindowControl1.RenderWindow; // renderer vtkRenderer renderer = renderWindow.GetRenderers().GetFirstRenderer(); // set background color renderer.SetBackground(1.0, 1.0, 1.0); // add our actors to the renderer renderer.AddActor(actor); renderer.AddActor(sphereActor); }
private void UpdateVtk(VtkAlgorithm algorithm, tkDefaultContext context) { if (!IsInitialized()) return; if (_input) { _algorithm.SetInputConnection(_input.Algorithm.GetOutputPort()); _input.UpdateVtk(_input, null); } if (_triangleFilter == null || _algorithm == null || _vtkMesh == null || _gameObject == null) return; _algorithm.Update(); _output = (vtkDataSet)_algorithm.GetOutputDataObject(0); // Input connection has to be set here because _algorithm address changes somehow // because of FullInspector serialization if (OutputDataDataType != DataType.vtkPolyData) { if (_geometryFilter == null) _geometryFilter = vtkGeometryFilter.New(); //_geometryFilter.MergingOff(); _geometryFilter.SetInputConnection(_algorithm.GetOutputPort()); _triangleFilter.SetInputConnection(_geometryFilter.GetOutputPort()); } else _triangleFilter.SetInputConnection(_algorithm.GetOutputPort()); _triangleFilter.PassVertsOn(); _triangleFilter.PassLinesOn(); _triangleFilter.Update(); _polyDataOutput = _triangleFilter.GetOutput(); if (_polyDataOutput == null || _polyDataOutput.GetNumberOfPoints() == 0 || _polyDataOutput.GetNumberOfCells() == 0) { // Debug.Log("Polydata output empty!"); return; } if (GenerateNormals && !VtkNormalsHelper.GetPointNormals(_polyDataOutput)) { if (_normalsFilter == null) _normalsFilter = vtkPolyDataNormals.New(); _normalsFilter.SetInputConnection(_triangleFilter.GetOutputPort()); _normalsFilter.ComputePointNormalsOn(); _normalsFilter.ComputeCellNormalsOff(); _normalsFilter.Update(); _polyDataOutput = _normalsFilter.GetOutput(); } _arrayNames = GetArrayNames(_polyDataOutput); _arrayLabels = _arrayNames.Select(t => new GUIContent(t)).ToArray(); if(!GenerateMesh) return; _vtkMesh.Update(_polyDataOutput); UpdateMeshColors(_selectedArrayIndex); DestroyImmediate(_gameObject.GetComponent<MeshRenderer>()); DestroyImmediate(_gameObject.GetComponent<MeshFilter>()); if (_vtkMesh.Meshes.Count == 1) { _gameObject.AddComponent<MeshFilter>().sharedMesh = _vtkMesh.Meshes[0]; var meshRenderer = _gameObject.AddComponent<MeshRenderer>(); meshRenderer.material = new Material(Shader.Find("Diffuse")) { color = Color.gray }; for(var i = 0; i < _gameObject.transform.childCount; i++) DestroyImmediate(_gameObject.transform.GetChild(i)); } else { for (var i = 0; i < _vtkMesh.Meshes.Count; i++) { var currentName = Name + "-" + i; GameObject child; var childTransform = _gameObject.transform.FindChild(currentName); if (childTransform == null) { child = new GameObject(currentName); child.transform.parent = _gameObject.transform; child.transform.localPosition = new Vector3(); child.AddComponent<MeshFilter>(); var meshRenderer = child.AddComponent<MeshRenderer>(); meshRenderer.material = new Material(Shader.Find("Diffuse")) {color = Color.gray}; } else child = childTransform.gameObject; child.GetComponent<MeshFilter>().sharedMesh = _vtkMesh.Meshes[i]; } while (_vtkMesh.Meshes.Count < _gameObject.transform.childCount) DestroyImmediate(_gameObject.transform.GetChild( _gameObject.transform.childCount - 1)); } if(MaterialProperties == null) MaterialProperties = _gameObject.AddComponent<MaterialProperties>(); }
public bool Read_Poly_Data_File(string filename) { //Initalize VTK Reader vtkXMLPolyDataReader reader = new vtkXMLPolyDataReader(); reader.SetFileName(filename); reader.Update(); vtkPolyData polydata = reader.GetOutput(); if (polydata == null) { Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine("Invalid Poly data Input"); return(false); } // Read Point Coordinates int numPoints = (int)polydata.GetNumberOfPoints(); List <Vector3d> point_dat = new List <Vector3d>(); if (numPoints != 0) { double[] pt; for (int i = 0; i < numPoints; i++) { pt = polydata.GetPoint(i); point_dat.Add(new Vector3d((float)pt[0], (float)pt[1], (float)pt[2])); } if (this.vertex_data.ContainsKey("vertices")) { this.vertex_data["vertices"] = point_dat; } else { this.vertex_data.Add("vertices", point_dat); } Console.WriteLine("All points read in correctly!"); } else { Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine("---------------No Points existent"); } // Read Point Indices int numpolydatacells = (int)polydata.GetNumberOfCells(); vtkCell polydataCell; vtkIdList pts; if (numpolydatacells != 0) { int counter = 0; cells.SetNumberOfCells(numpolydatacells); for (int i = 0; i < numpolydatacells; i++) { polydataCell = polydata.GetCell(i); int numCellPoints = (int)polydataCell.GetNumberOfPoints(); cells.InsertNextCell(polydataCell); Vector3 trianglePoints = new Vector3(); if (numCellPoints == 3) { pts = polydataCell.GetPointIds(); int one = (int)pts.GetId(0); int two = (int)pts.GetId(1); int three = (int)pts.GetId(2); //this.Get_Triangle(counter, pts); trianglePoints = new Vector3(one, two, three); counter++; } triangleList.Add(trianglePoints); } } else { Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine("---------------No Triangles existent"); } // Read point data vtkPointData pointData = polydata.GetPointData(); // Load point attributes this.Load_Point_Attributes(pointData); return(true); }