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 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477
|
/*=========================================================================
Program: Visualization Toolkit
Module: vtkExtractSelectedGraph.cxx
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 2008 Sandia Corporation.
Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
the U.S. Government retains certain rights in this software.
-------------------------------------------------------------------------*/
#include "vtkExtractSelectedGraph.h"
#include "vtkAnnotation.h"
#include "vtkAnnotationLayers.h"
#include "vtkCellArray.h"
#include "vtkCellData.h"
#include "vtkCommand.h"
#include "vtkConvertSelection.h"
#include "vtkDataArray.h"
#include "vtkEdgeListIterator.h"
#include "vtkEventForwarderCommand.h"
#include "vtkExtractSelection.h"
#include "vtkGraph.h"
#include "vtkIdTypeArray.h"
#include "vtkInformation.h"
#include "vtkInformationVector.h"
#include "vtkMutableDirectedGraph.h"
#include "vtkMutableUndirectedGraph.h"
#include "vtkObjectFactory.h"
#include "vtkPointData.h"
#include "vtkPoints.h"
#include "vtkSelection.h"
#include "vtkSelectionNode.h"
#include "vtkSignedCharArray.h"
#include "vtkSmartPointer.h"
#include "vtkStringArray.h"
#include "vtkTree.h"
#include "vtkVertexListIterator.h"
#include <vtksys/stl/map>
#include <vector>
vtkStandardNewMacro(vtkExtractSelectedGraph);
//----------------------------------------------------------------------------
vtkExtractSelectedGraph::vtkExtractSelectedGraph()
{
this->SetNumberOfInputPorts(3);
this->RemoveIsolatedVertices = false;
}
//----------------------------------------------------------------------------
vtkExtractSelectedGraph::~vtkExtractSelectedGraph()
{
}
//----------------------------------------------------------------------------
int vtkExtractSelectedGraph::FillInputPortInformation(int port, vtkInformation* info)
{
if (port == 0)
{
info->Set(vtkAlgorithm::INPUT_REQUIRED_DATA_TYPE(), "vtkGraph");
return 1;
}
else if (port == 1)
{
info->Set(vtkAlgorithm::INPUT_IS_OPTIONAL(), 1);
info->Set(vtkAlgorithm::INPUT_REQUIRED_DATA_TYPE(), "vtkSelection");
return 1;
}
else if (port == 2)
{
info->Set(vtkAlgorithm::INPUT_IS_OPTIONAL(), 1);
info->Set(vtkAlgorithm::INPUT_REQUIRED_DATA_TYPE(), "vtkAnnotationLayers");
return 1;
}
return 0;
}
//----------------------------------------------------------------------------
void vtkExtractSelectedGraph::SetSelectionConnection(vtkAlgorithmOutput* in)
{
this->SetInputConnection(1, in);
}
//----------------------------------------------------------------------------
void vtkExtractSelectedGraph::SetAnnotationLayersConnection(vtkAlgorithmOutput* in)
{
this->SetInputConnection(2, in);
}
//----------------------------------------------------------------------------
int vtkExtractSelectedGraph::RequestDataObject(
vtkInformation*,
vtkInformationVector** inputVector ,
vtkInformationVector* outputVector)
{
vtkInformation* inInfo = inputVector[0]->GetInformationObject(0);
if (!inInfo)
{
return 0;
}
vtkGraph *input = vtkGraph::SafeDownCast(
inInfo->Get(vtkDataObject::DATA_OBJECT()));
if (input)
{
vtkInformation* info = outputVector->GetInformationObject(0);
vtkGraph *output = vtkGraph::SafeDownCast(
info->Get(vtkDataObject::DATA_OBJECT()));
// Output a vtkDirectedGraph if the input is a tree.
if (!output
|| (vtkTree::SafeDownCast(input) && !vtkDirectedGraph::SafeDownCast(output))
|| (!vtkTree::SafeDownCast(input) && !output->IsA(input->GetClassName())))
{
if (vtkTree::SafeDownCast(input))
{
output = vtkDirectedGraph::New();
}
else
{
output = input->NewInstance();
}
info->Set(vtkDataObject::DATA_OBJECT(), output);
output->Delete();
}
return 1;
}
return 0;
}
//----------------------------------------------------------------------------
int vtkExtractSelectedGraph::RequestData(
vtkInformation* vtkNotUsed(request),
vtkInformationVector** inputVector,
vtkInformationVector* outputVector)
{
vtkGraph* input = vtkGraph::GetData(inputVector[0]);
vtkSelection* inputSelection = vtkSelection::GetData(inputVector[1]);
vtkAnnotationLayers* inputAnnotations = vtkAnnotationLayers::GetData(inputVector[2]);
vtkGraph* output = vtkGraph::GetData(outputVector);
if(!inputSelection && !inputAnnotations)
{
vtkErrorMacro("No vtkSelection or vtkAnnotationLayers provided as input.");
return 0;
}
vtkSmartPointer<vtkSelection> selection = vtkSmartPointer<vtkSelection>::New();
int numSelections = 0;
if(inputSelection)
{
selection->DeepCopy(inputSelection);
numSelections++;
}
// If input annotations are provided, extract their selections only if
// they are enabled and not hidden.
if(inputAnnotations)
{
for(unsigned int i=0; i<inputAnnotations->GetNumberOfAnnotations(); ++i)
{
vtkAnnotation* a = inputAnnotations->GetAnnotation(i);
if ((a->GetInformation()->Has(vtkAnnotation::ENABLE()) &&
a->GetInformation()->Get(vtkAnnotation::ENABLE())==0) ||
(a->GetInformation()->Has(vtkAnnotation::ENABLE()) &&
a->GetInformation()->Get(vtkAnnotation::ENABLE())==1 &&
a->GetInformation()->Has(vtkAnnotation::HIDE()) &&
a->GetInformation()->Get(vtkAnnotation::HIDE())==1))
{
continue;
}
selection->Union(a->GetSelection());
numSelections++;
}
}
// Handle case where there was no input selection and no enabled, non-hidden
// annotations
if(numSelections == 0)
{
output->ShallowCopy(input);
return 1;
}
// Convert the selection to an INDICES selection
vtkSmartPointer<vtkSelection> converted;
converted.TakeReference(vtkConvertSelection::ToIndexSelection(selection, input));
if (!converted.GetPointer())
{
vtkErrorMacro("Selection conversion to INDICES failed.");
return 0;
}
// Collect vertex and edge selections.
vtkSmartPointer<vtkIdTypeArray> edgeList = vtkSmartPointer<vtkIdTypeArray>::New();
bool hasEdges = false;
vtkSmartPointer<vtkIdTypeArray> vertexList = vtkSmartPointer<vtkIdTypeArray>::New();
bool hasVertices = false;
for (unsigned int i = 0; i < converted->GetNumberOfNodes(); ++i)
{
vtkSelectionNode* node = converted->GetNode(i);
vtkIdTypeArray* list = 0;
if (node->GetFieldType() == vtkSelectionNode::VERTEX)
{
list = vertexList;
hasVertices = true;
}
else if (node->GetFieldType() == vtkSelectionNode::EDGE)
{
list = edgeList;
hasEdges = true;
}
if (list)
{
// Append the selection list to the selection
vtkIdTypeArray* curList = vtkIdTypeArray::SafeDownCast(node->GetSelectionList());
if (curList)
{
int inverse = node->GetProperties()->Get(vtkSelectionNode::INVERSE());
if (inverse)
{
vtkIdType num =
(node->GetFieldType() == vtkSelectionNode::VERTEX) ?
input->GetNumberOfVertices() : input->GetNumberOfEdges();
for (vtkIdType j = 0; j < num; ++j)
{
if (curList->LookupValue(j) < 0 && list->LookupValue(j) < 0)
{
list->InsertNextValue(j);
}
}
}
else
{
vtkIdType numTuples = curList->GetNumberOfTuples();
for (vtkIdType j = 0; j < numTuples; ++j)
{
vtkIdType curValue = curList->GetValue(j);
if (list->LookupValue(curValue) < 0)
{
list->InsertNextValue(curValue);
}
}
}
} // end if (curList)
} // end if (list)
} // end for each child
// If there is no selection list, return an empty graph
if (vertexList->GetNumberOfTuples() == 0 && edgeList->GetNumberOfTuples() == 0)
{
return 1;
}
vtkSmartPointer<vtkMutableDirectedGraph> dirBuilder =
vtkSmartPointer<vtkMutableDirectedGraph>::New();
vtkSmartPointer<vtkMutableUndirectedGraph> undirBuilder =
vtkSmartPointer<vtkMutableUndirectedGraph>::New();
bool directed;
vtkGraph* builder = 0;
if (vtkDirectedGraph::SafeDownCast(input))
{
directed = true;
builder = dirBuilder;
}
else
{
directed = false;
builder = undirBuilder;
}
// There are three cases to handle:
// 1. Selecting vertices only: Select the vertices along with any edges
// connecting two selected vertices.
// 2. Selecting edges only: Select the edges along with all vertices
// adjacent to a selected edge.
// 3. Selecting vertices and edges: Select the edges along with all vertices
// adjacent to a selected edge, plus any additional vertex specified
// in the vertex selection.
vtkDataSetAttributes* vdIn = input->GetVertexData();
vtkDataSetAttributes* edIn = input->GetEdgeData();
vtkDataSetAttributes* vdOut = builder->GetVertexData();
vtkDataSetAttributes* edOut = builder->GetEdgeData();
vtkPoints* ptsIn = input->GetPoints();
vtkPoints* ptsOut = builder->GetPoints();
vdOut->CopyAllocate(vdIn);
edOut->CopyAllocate(edIn);
vtksys_stl::map<vtkIdType, vtkIdType> vertexMap;
// Step 1: Add the vertices.
// If the user has specified a vertex selection, add them.
// Else if only an edge selection and RemoveIsolatedVertices is off,
// add all vertices to the output.
// Otherwise, let the edge selection determine the vertices to add.
if (hasVertices)
{
// Add selected vertices
vtkIdType numSelectedVerts = vertexList->GetNumberOfTuples();
for (vtkIdType i = 0; i < numSelectedVerts; ++i)
{
vtkIdType inVert = vertexList->GetValue(i);
vtkIdType outVert = 0;
if (directed)
{
outVert = dirBuilder->AddVertex();
}
else
{
outVert = undirBuilder->AddVertex();
}
vdOut->CopyData(vdIn, inVert, outVert);
ptsOut->InsertNextPoint(ptsIn->GetPoint(inVert));
vertexMap[inVert] = outVert;
}
}
else if (!this->RemoveIsolatedVertices)
{
// In the special case where there is only an edge selection, the user may
// specify that they want all vertices in the output.
vtkIdType numVert = input->GetNumberOfVertices();
for (vtkIdType inVert = 0; inVert < numVert; ++inVert)
{
vtkIdType outVert = 0;
if (directed)
{
outVert = dirBuilder->AddVertex();
}
else
{
outVert = undirBuilder->AddVertex();
}
vdOut->CopyData(vdIn, inVert, outVert);
ptsOut->InsertNextPoint(ptsIn->GetPoint(inVert));
vertexMap[inVert] = outVert;
}
}
// Step 2: Add the edges
// If there is an edge selection, add those edges.
// Otherwise, add all edges connecting selected vertices.
if (hasEdges)
{
// Add selected edges
vtkIdType numSelectedEdges = edgeList->GetNumberOfTuples();
for (vtkIdType i = 0; i < numSelectedEdges; ++i)
{
vtkEdgeType e;
e.Id = edgeList->GetValue(i);
e.Source = input->GetSourceVertex(e.Id);
e.Target = input->GetTargetVertex(e.Id);
// Add source and target vertices if they are not yet in output
vtkIdType addVert[2];
int numAddVert = 0;
if (vertexMap.find(e.Source) == vertexMap.end())
{
addVert[numAddVert] = e.Source;
++numAddVert;
}
if (vertexMap.find(e.Target) == vertexMap.end())
{
addVert[numAddVert] = e.Target;
++numAddVert;
}
for (int j = 0; j < numAddVert; ++j)
{
vtkIdType outVert = 0;
if (directed)
{
outVert = dirBuilder->AddVertex();
}
else
{
outVert = undirBuilder->AddVertex();
}
vdOut->CopyData(vdIn, addVert[j], outVert);
ptsOut->InsertNextPoint(ptsIn->GetPoint(addVert[j]));
vertexMap[addVert[j]] = outVert;
}
// Add the selected edge
vtkIdType source = vertexMap[e.Source];
vtkIdType target = vertexMap[e.Target];
vtkEdgeType f;
if (directed)
{
f = dirBuilder->AddEdge(source, target);
}
else
{
f = undirBuilder->AddEdge(source, target);
}
edOut->CopyData(edIn, e.Id, f.Id);
// Copy edge layout to the output.
vtkIdType npts;
double* pts;
input->GetEdgePoints(e.Id, npts, pts);
builder->SetEdgePoints(f.Id, npts, pts);
}
}
else
{
// Add edges between selected vertices
vtkSmartPointer<vtkEdgeListIterator> edges = vtkSmartPointer<vtkEdgeListIterator>::New();
input->GetEdges(edges);
while (edges->HasNext())
{
vtkEdgeType e = edges->Next();
if (vertexMap.find(e.Source) != vertexMap.end() &&
vertexMap.find(e.Target) != vertexMap.end())
{
vtkIdType source = vertexMap[e.Source];
vtkIdType target = vertexMap[e.Target];
vtkEdgeType f;
if (directed)
{
f = dirBuilder->AddEdge(source, target);
}
else
{
f = undirBuilder->AddEdge(source, target);
}
edOut->CopyData(edIn, e.Id, f.Id);
// Copy edge layout to the output.
vtkIdType npts;
double* pts;
input->GetEdgePoints(e.Id, npts, pts);
builder->SetEdgePoints(f.Id, npts, pts);
}
}
}
// Pass constructed graph to output.
if (directed)
{
if (!output->CheckedShallowCopy(dirBuilder))
{
vtkErrorMacro(<<"Invalid graph structure.");
return 0;
}
}
else
{
if (!output->CheckedShallowCopy(undirBuilder))
{
vtkErrorMacro(<<"Invalid graph structure.");
return 0;
}
}
output->GetFieldData()->PassData(input->GetFieldData());
// Clean up
output->Squeeze();
return 1;
}
//----------------------------------------------------------------------------
void vtkExtractSelectedGraph::PrintSelf(ostream& os, vtkIndent indent)
{
this->Superclass::PrintSelf(os, indent);
os << indent << "RemoveIsolatedVertices: "
<< (this->RemoveIsolatedVertices ? "on" : "off") << endl;
}
|