1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321
|
/*=========================================================================
Program: Visualization Toolkit
Module: $RCSfile: vtkExtractSelectedGraph.cxx,v $
Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
All rights reserved.
See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notice for more information.
=========================================================================*/
/*----------------------------------------------------------------------------
Copyright (c) Sandia Corporation
See Copyright.txt or http://www.paraview.org/HTML/Copyright.html for details.
----------------------------------------------------------------------------*/
#include "vtkExtractSelectedGraph.h"
#include "vtkCellData.h"
#include "vtkCommand.h"
#include "vtkDataArray.h"
#include "vtkEventForwarderCommand.h"
#include "vtkExtractSelection.h"
#include "vtkGraph.h"
#include "vtkGraphIdList.h"
#include "vtkIdTypeArray.h"
#include "vtkInformation.h"
#include "vtkInformationVector.h"
#include "vtkObjectFactory.h"
#include "vtkPointData.h"
#include "vtkSelection.h"
#include "vtkSignedCharArray.h"
#include "vtkSmartPointer.h"
#include "vtkStringArray.h"
#include <vtksys/stl/map>
using vtksys_stl::map;
vtkCxxRevisionMacro(vtkExtractSelectedGraph, "$Revision: 1.10 $");
vtkStandardNewMacro(vtkExtractSelectedGraph);
vtkExtractSelectedGraph::vtkExtractSelectedGraph()
{
this->SetNumberOfInputPorts(2);
this->RemoveIsolatedVertices = true;
}
vtkExtractSelectedGraph::~vtkExtractSelectedGraph()
{
}
int vtkExtractSelectedGraph::FillInputPortInformation(int port, vtkInformation* info)
{
if (port == 0)
{
info->Set(vtkAlgorithm::INPUT_REQUIRED_DATA_TYPE(), "vtkAbstractGraph");
return 1;
}
else if (port == 1)
{
info->Set(vtkAlgorithm::INPUT_REQUIRED_DATA_TYPE(), "vtkSelection");
return 1;
}
return 0;
}
void vtkExtractSelectedGraph::SetSelectionConnection(vtkAlgorithmOutput* in)
{
this->SetInputConnection(1, in);
}
int vtkExtractSelectedGraph::ConvertToIndexSelection(
vtkSelection* selection,
vtkDataSet* input,
vtkSelection* outputSelection)
{
// Change the selection to preserve topology
vtkSelection* selTemp = vtkSelection::New();
selTemp->ShallowCopy(selection);
selTemp->GetProperties()->Set(vtkSelection::PRESERVE_TOPOLOGY(), true);
// Use the extraction filter to create an insidedness array.
vtkExtractSelection* const extract = vtkExtractSelection::New();
extract->SetInput(0, input);
extract->SetInput(1, selTemp);
extract->Update();
vtkDataSet* const extracted = extract->GetOutput();
selTemp->Delete();
outputSelection->GetProperties()->Set(vtkSelection::CONTENT_TYPE(), vtkSelection::INDICES);
int type = selection->GetProperties()->Get(vtkSelection::FIELD_TYPE());
outputSelection->GetProperties()->Set(vtkSelection::FIELD_TYPE(), type);
vtkSignedCharArray* insidedness = 0;
if (type == vtkSelection::CELL)
{
insidedness = vtkSignedCharArray::SafeDownCast(
extracted->GetCellData()->GetAbstractArray("vtkInsidedness"));
}
else if (type == vtkSelection::POINT)
{
insidedness = vtkSignedCharArray::SafeDownCast(
extracted->GetPointData()->GetAbstractArray("vtkInsidedness"));
}
else
{
vtkErrorMacro("Unknown field type");
extract->Delete();
return 0;
}
if (!insidedness)
{
vtkErrorMacro("Did not find expected vtkInsidedness array.");
extract->Delete();
return 0;
}
// Convert the insidedness array into an index selection.
vtkIdTypeArray* indexArray = vtkIdTypeArray::New();
for (vtkIdType i = 0; i < insidedness->GetNumberOfTuples(); i++)
{
if (insidedness->GetValue(i) == 1)
{
indexArray->InsertNextValue(i);
}
}
outputSelection->SetSelectionList(indexArray);
indexArray->Delete();
extract->Delete();
return 1;
}
int vtkExtractSelectedGraph::RequestData(
vtkInformation* vtkNotUsed(request),
vtkInformationVector** inputVector,
vtkInformationVector* outputVector)
{
vtkAbstractGraph* input = vtkAbstractGraph::GetData(inputVector[0]);
vtkSelection* selection = vtkSelection::GetData(inputVector[1]);
// If there is nothing in the list, there is nothing to select.
vtkAbstractArray* list = selection->GetSelectionList();
if (!list || list->GetNumberOfTuples() == 0)
{
return 1;
}
vtkSelection* indexSelection = vtkSelection::New();
indexSelection->ShallowCopy(selection);
int content = selection->GetProperties()->Get(selection->CONTENT_TYPE());
if (content != vtkSelection::INDICES)
{
// Convert the selection to an INDICES selection
int ret = this->ConvertToIndexSelection(selection, input, indexSelection);
if (ret != 1)
{
vtkErrorMacro("Selection conversion to INDICES failed.");
indexSelection->Delete();
return 0;
}
}
vtkAbstractArray* arr = indexSelection->GetSelectionList();
if (arr == NULL)
{
vtkErrorMacro("Selection list not found.");
return 0;
}
vtkIdTypeArray* selectArr = vtkIdTypeArray::SafeDownCast(arr);
if (selectArr == NULL)
{
vtkErrorMacro("Selection list must be of type vtkIdTypeArray.");
return 0;
}
vtkIdType selectSize = selectArr->GetNumberOfTuples();
vtkGraph* output = vtkGraph::GetData(outputVector);
output->SetDirected(input->GetDirected());
output->GetFieldData()->PassData(input->GetFieldData());
if (selection->GetProperties()->Has(vtkSelection::FIELD_TYPE()) &&
selection->GetProperties()->Get(vtkSelection::FIELD_TYPE()) == vtkSelection::CELL)
{
//
// Edge selection
//
// Copy all vertices
output->SetNumberOfVertices(input->GetNumberOfVertices());
// Copy selected edges
vtkCellData* inputEdgeData = input->GetEdgeData();
vtkCellData* outputEdgeData = output->GetEdgeData();
outputEdgeData->CopyAllocate(inputEdgeData);
for (vtkIdType i = 0; i < selectSize; i++)
{
vtkIdType inputEdge = selectArr->GetValue(i);
if (inputEdge < input->GetNumberOfEdges())
{
vtkIdType source = input->GetSourceVertex(inputEdge);
vtkIdType target = input->GetTargetVertex(inputEdge);
vtkIdType outputEdge = output->AddEdge(source, target);
outputEdgeData->CopyData(inputEdgeData, inputEdge, outputEdge);
}
}
// Remove isolated vertices
if (this->RemoveIsolatedVertices)
{
output->GetVertexData()->DeepCopy(input->GetVertexData());
output->GetPoints()->DeepCopy(input->GetPoints());
vtkIdTypeArray* isolated = vtkIdTypeArray::New();
for (vtkIdType i = 0; i < output->GetNumberOfVertices(); i++)
{
if (output->GetDegree(i) == 0)
{
isolated->InsertNextValue(i);
}
}
output->RemoveVertices(isolated->GetPointer(0), isolated->GetNumberOfTuples());
isolated->Delete();
}
else
{
output->GetVertexData()->PassData(input->GetVertexData());
output->GetPoints()->ShallowCopy(input->GetPoints());
}
}
else
{
//
// Vertex selection
//
// Copy selected vertices
double pt[3];
vtkPoints* inputPoints = input->GetPoints();
vtkPoints* outputPoints = vtkPoints::New();
vtkPointData* inputVertexData = input->GetVertexData();
vtkPointData* outputVertexData = output->GetVertexData();
outputVertexData->CopyAllocate(inputVertexData);
map<vtkIdType, vtkIdType> idMap;
for (vtkIdType i = 0; i < selectSize; i++)
{
vtkIdType inputVertex = selectArr->GetValue(i);
if (inputVertex < input->GetNumberOfVertices())
{
vtkIdType outputVertex = output->AddVertex();
outputVertexData->CopyData(inputVertexData, inputVertex, outputVertex);
idMap[inputVertex] = outputVertex;
inputPoints->GetPoint(inputVertex, pt);
outputPoints->InsertNextPoint(pt);
}
}
output->SetPoints(outputPoints);
outputPoints->Delete();
// Make a directed shallow copy of the graph so GetOutEdges()
// returns every edge no more than once.
vtkAbstractGraph* copy = input;
bool madeCopy = false;
if (vtkGraph::SafeDownCast(input) && !input->GetDirected())
{
copy = vtkGraph::New();
copy->ShallowCopy(input);
vtkGraph::SafeDownCast(copy)->SetDirected(true);
madeCopy = true;
}
// Copy any edges that connect selected vertices
vtkCellData* inputEdgeData = input->GetEdgeData();
vtkCellData* outputEdgeData = output->GetEdgeData();
outputEdgeData->CopyAllocate(inputEdgeData);
vtkGraphIdList* edgeList = vtkGraphIdList::New();
for (vtkIdType i = 0; i < selectSize; i++)
{
vtkIdType inputVertex = selectArr->GetValue(i);
if (idMap.count(inputVertex) > 0)
{
vtkIdType outputVertex = idMap[inputVertex];
copy->GetOutEdges(inputVertex, edgeList);
for (vtkIdType j = 0; j < edgeList->GetNumberOfIds(); j++)
{
vtkIdType inputEdge = edgeList->GetId(j);
vtkIdType oppInputVertex = input->GetOppositeVertex(inputEdge, inputVertex);
if (idMap.count(oppInputVertex) > 0)
{
vtkIdType oppOutputVertex = idMap[oppInputVertex];
vtkIdType outputEdge = output->AddEdge(outputVertex, oppOutputVertex);
outputEdgeData->CopyData(inputEdgeData, inputEdge, outputEdge);
}
}
}
}
edgeList->Delete();
// Clean up
if (madeCopy)
{
copy->Delete();
}
}
// Clean up
output->Squeeze();
indexSelection->Delete();
return 1;
}
void vtkExtractSelectedGraph::PrintSelf(ostream& os, vtkIndent indent)
{
this->Superclass::PrintSelf(os, indent);
os << indent << "RemoveIsolatedVertices: "
<< (this->RemoveIsolatedVertices ? "on" : "off") << endl;
}
|