// SPDX-FileCopyrightText: Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen // SPDX-FileCopyrightText: Copyright 2008 Sandia Corporation // SPDX-License-Identifier: LicenseRef-BSD-3-Clause-Sandia-USGov #include "vtkRemoveIsolatedVertices.h" #include "vtkDataSetAttributes.h" #include "vtkEdgeListIterator.h" #include "vtkGraph.h" #include "vtkInformation.h" #include "vtkInformationVector.h" #include "vtkMutableDirectedGraph.h" #include "vtkMutableGraphHelper.h" #include "vtkMutableUndirectedGraph.h" #include "vtkObjectFactory.h" #include "vtkPoints.h" #include "vtkSmartPointer.h" #include VTK_ABI_NAMESPACE_BEGIN vtkStandardNewMacro(vtkRemoveIsolatedVertices); //------------------------------------------------------------------------------ vtkRemoveIsolatedVertices::vtkRemoveIsolatedVertices() = default; //------------------------------------------------------------------------------ vtkRemoveIsolatedVertices::~vtkRemoveIsolatedVertices() = default; //------------------------------------------------------------------------------ int vtkRemoveIsolatedVertices::RequestData(vtkInformation* vtkNotUsed(request), vtkInformationVector** inputVector, vtkInformationVector* outputVector) { vtkGraph* input = vtkGraph::GetData(inputVector[0]); // Set up our mutable graph helper. vtkSmartPointer builder = vtkSmartPointer::New(); if (vtkDirectedGraph::SafeDownCast(input)) { vtkSmartPointer dir = vtkSmartPointer::New(); builder->SetGraph(dir); } else { vtkSmartPointer undir = vtkSmartPointer::New(); builder->SetGraph(undir); } // Initialize edge data, vertex data, and points. vtkDataSetAttributes* inputEdgeData = input->GetEdgeData(); vtkDataSetAttributes* builderEdgeData = builder->GetGraph()->GetEdgeData(); builderEdgeData->CopyAllocate(inputEdgeData); vtkDataSetAttributes* inputVertData = input->GetVertexData(); vtkDataSetAttributes* builderVertData = builder->GetGraph()->GetVertexData(); builderVertData->CopyAllocate(inputVertData); vtkPoints* inputPoints = input->GetPoints(); vtkSmartPointer builderPoints = vtkSmartPointer::New(); builder->GetGraph()->SetPoints(builderPoints); // Vector keeps track of mapping of input vertex ids to // output vertex ids. vtkIdType numVert = input->GetNumberOfVertices(); std::vector outputVertex(numVert, -1); vtkSmartPointer edgeIter = vtkSmartPointer::New(); input->GetEdges(edgeIter); while (edgeIter->HasNext()) { vtkEdgeType e = edgeIter->Next(); vtkIdType source = outputVertex[e.Source]; if (source < 0) { source = builder->AddVertex(); outputVertex[e.Source] = source; builderVertData->CopyData(inputVertData, e.Source, source); builderPoints->InsertNextPoint(inputPoints->GetPoint(e.Source)); } vtkIdType target = outputVertex[e.Target]; if (target < 0) { target = builder->AddVertex(); outputVertex[e.Target] = target; builderVertData->CopyData(inputVertData, e.Target, target); builderPoints->InsertNextPoint(inputPoints->GetPoint(e.Target)); } vtkEdgeType outputEdge = builder->AddEdge(source, target); builderEdgeData->CopyData(inputEdgeData, e.Id, outputEdge.Id); } // Pass constructed graph to output. vtkGraph* output = vtkGraph::GetData(outputVector); output->ShallowCopy(builder->GetGraph()); output->GetFieldData()->PassData(input->GetFieldData()); // Clean up output->Squeeze(); return 1; } //------------------------------------------------------------------------------ void vtkRemoveIsolatedVertices::PrintSelf(ostream& os, vtkIndent indent) { this->Superclass::PrintSelf(os, indent); } VTK_ABI_NAMESPACE_END