forked from Kitware/VTK
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vtkPBGLMinimumSpanningTree.cxx
271 lines (234 loc) · 8.88 KB
/
vtkPBGLMinimumSpanningTree.cxx
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
/*=========================================================================
Program: Visualization Toolkit
Module: vtkPBGLMinimumSpanningTree.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.
-------------------------------------------------------------------------*/
/*
* Copyright (C) 2008 The Trustees of Indiana University.
* Use, modification and distribution is subject to the Boost Software
* License, Version 1.0. (See http://www.boost.org/LICENSE_1_0.txt)
*/
#include "vtkPBGLMinimumSpanningTree.h"
#include "vtkBoostGraphAdapter.h"
#include "vtkCellArray.h"
#include "vtkCellData.h"
#include "vtkDataArray.h"
#include "vtkDirectedGraph.h"
#include "vtkDistributedGraphHelper.h"
#include "vtkFloatArray.h"
#include "vtkInformation.h"
#include "vtkInformationVector.h"
#include "vtkMath.h"
#include "vtkObjectFactory.h"
#include "vtkPBGLDistributedGraphHelper.h"
#include "vtkPBGLGraphAdapter.h"
#include "vtkPointData.h"
#include "vtkSelection.h"
#include "vtkSelectionNode.h"
#include "vtkSmartPointer.h"
#include "vtkStreamingDemandDrivenPipeline.h"
#include "vtkStringArray.h"
#include "vtkUndirectedGraph.h"
#include <boost/graph/use_mpi.hpp>
#include <boost/graph/distributed/dehne_gotz_min_spanning_tree.hpp>
#include <boost/graph/distributed/vertex_list_adaptor.hpp>
#include <boost/property_map/parallel/global_index_map.hpp>
#include <boost/property_map/property_map.hpp>
#include <boost/property_map/vector_property_map.hpp>
#include <boost/pending/queue.hpp>
#include <vtksys/stl/utility> // for pair
#include <vtksys/stl/iterator> // for back_inserter
using namespace boost;
vtkStandardNewMacro(vtkPBGLMinimumSpanningTree);
// Constructor/Destructor
vtkPBGLMinimumSpanningTree::vtkPBGLMinimumSpanningTree()
{
// Default values for the origin vertex
this->EdgeWeightArrayName = 0;
this->OutputSelectionType = 0;
this->SetOutputSelectionType("MINIMUM_SPANNING_TREE_EDGES");
this->SetNumberOfInputPorts(1);
this->SetNumberOfOutputPorts(2);
}
vtkPBGLMinimumSpanningTree::~vtkPBGLMinimumSpanningTree()
{
this->SetEdgeWeightArrayName(0);
}
int vtkPBGLMinimumSpanningTree::RequestData(
vtkInformation *vtkNotUsed(request),
vtkInformationVector **inputVector,
vtkInformationVector *outputVector)
{
// get the info objects
vtkInformation *inInfo = inputVector[0]->GetInformationObject(0);
vtkInformation *outInfo = outputVector->GetInformationObject(0);
// get the input and output
vtkGraph *input = vtkGraph::SafeDownCast(
inInfo->Get(vtkDataObject::DATA_OBJECT()));
vtkGraph *output = vtkGraph::SafeDownCast(
outInfo->Get(vtkDataObject::DATA_OBJECT()));
// Send the data to output.
output->ShallowCopy(input);
vtkDistributedGraphHelper *helper = output->GetDistributedGraphHelper();
if (!helper)
{
vtkErrorMacro("Distributed vtkGraph is required.");
return 1;
}
// We can only deal with Parallel BGL-distributed graphs.
vtkPBGLDistributedGraphHelper *pbglHelper
= vtkPBGLDistributedGraphHelper::SafeDownCast(helper);
if (!pbglHelper)
{
vtkErrorMacro("Can only perform parallel breadth-first-search on a Parallel BGL distributed graph");
return 1;
}
// Retrieve the edge-weight array.
if (!this->EdgeWeightArrayName)
{
vtkErrorMacro("Edge-weight array name is required");
return 1;
}
vtkAbstractArray* abstractEdgeWeightArray
= input->GetEdgeData()->GetAbstractArray(this->EdgeWeightArrayName);
// Does the edge-weight array exist at all?
if (abstractEdgeWeightArray == NULL)
{
vtkErrorMacro("Could not find edge-weight array named "
<< this->EdgeWeightArrayName);
return 1;
}
// Does the edge-weight array have enough values in it?
if (abstractEdgeWeightArray->GetNumberOfTuples() < output->GetNumberOfEdges())
{
vtkErrorMacro("Edge-weight array named " << this->EdgeWeightArrayName
<< " has too few values in it.");
return 1;
}
bool edgeWeightArrayIsTemporary = false;
vtkDoubleArray *edgeWeightArray
= vtkDoubleArray::SafeDownCast(abstractEdgeWeightArray);
if (edgeWeightArray == 0)
{
// Edge-weight array does not contain "double" values. We will
// need to build a temporary array, or fail.
if (abstractEdgeWeightArray->IsNumeric())
{
// Allocate a new edge-weight array.
edgeWeightArray = vtkDoubleArray::New();
edgeWeightArray->SetNumberOfTuples(output->GetNumberOfEdges());
edgeWeightArrayIsTemporary = true;
// Convert the values in the given array into doubles.
for (vtkIdType i = 0; i < output->GetNumberOfEdges(); ++i)
{
vtkVariant value = abstractEdgeWeightArray->GetVariantValue(i);
edgeWeightArray->SetTuple1(i, value.ToDouble());
}
}
else
{
vtkErrorMacro("Edge-weight array named " << this->EdgeWeightArrayName
<< " does not contain numeric values.")
return 1;
}
}
// Execute the algorithm
if (vtkUndirectedGraph::SafeDownCast(output))
{
vtkUndirectedGraph *g = vtkUndirectedGraph::SafeDownCast(output);
// Distributed edge-weight map
typedef vtkDistributedEdgePropertyMapType<vtkDoubleArray>::type
DistributedEdgeWeightMap;
DistributedEdgeWeightMap edgeWeightMap
= MakeDistributedEdgePropertyMap(g, edgeWeightArray);
// Create a global index map, which takes vertex descriptors and
// turns them into numbers in the range [0, N), where N is the
// total number of vertices in the distributed graph.
typedef boost::parallel::global_index_map<vtkGraphDistributedVertexIndexMap,
vtkVertexGlobalMap>
GlobalIndexMap;
GlobalIndexMap globalIndexMap(pbglHelper->GetProcessGroup(),
g->GetNumberOfVertices(),
MakeDistributedVertexIndexMap(g),
get(boost::vertex_global, g));
// Run the minimum spanning tree algorithm
std::vector<vtkEdgeType> mstEdges;
boost::graph::distributed::boruvka_mixed_merge
(boost::graph::make_vertex_list_adaptor(g, globalIndexMap),
edgeWeightMap,
std::back_inserter(mstEdges),
globalIndexMap);
// Select the minimum spanning tree edges.
if (!strcmp(OutputSelectionType,"MINIMUM_SPANNING_TREE_EDGES"))
{
vtkSelection* sel = vtkSelection::GetData(outputVector, 1);
vtkSmartPointer<vtkIdTypeArray> ids =
vtkSmartPointer<vtkIdTypeArray>::New();
vtkSmartPointer<vtkSelectionNode> node =
vtkSmartPointer<vtkSelectionNode>::New();
// Add the ids of each MST edge.
for (std::vector<vtkEdgeType>::iterator i = mstEdges.begin();
i != mstEdges.end(); ++i)
{
ids->InsertNextValue(i->Id);
}
node->SetSelectionList(ids);
node->GetProperties()->Set(vtkSelectionNode::CONTENT_TYPE(),
vtkSelectionNode::INDICES);
node->GetProperties()->Set(vtkSelectionNode::FIELD_TYPE(),
vtkSelectionNode::EDGE);
sel->AddNode(node);
}
}
else
{
vtkErrorMacro("Minimum spanning tree can only be computed on an undirected vtkGraph");
return 1;
}
return 1;
}
void vtkPBGLMinimumSpanningTree::PrintSelf(ostream& os, vtkIndent indent)
{
this->Superclass::PrintSelf(os, indent);
os << indent << "EdgeWeightArrayName: "
<< (this->EdgeWeightArrayName ? this->EdgeWeightArrayName : "(none)")
<< endl;
os << indent << "OutputSelectionType: "
<< (this->OutputSelectionType ? this->OutputSelectionType : "(none)") << endl;
}
//----------------------------------------------------------------------------
int vtkPBGLMinimumSpanningTree::FillInputPortInformation(
int port, vtkInformation* info)
{
// now add our info
if (port == 0)
{
info->Set(vtkAlgorithm::INPUT_REQUIRED_DATA_TYPE(), "vtkGraph");
}
return 1;
}
//----------------------------------------------------------------------------
int vtkPBGLMinimumSpanningTree::FillOutputPortInformation(
int port, vtkInformation* info)
{
// now add our info
if (port == 0)
{
info->Set(vtkDataObject::DATA_TYPE_NAME(), "vtkGraph");
}
else if (port == 1)
{
info->Set(vtkDataObject::DATA_TYPE_NAME(), "vtkSelection");
}
return 1;
}