forked from Kitware/VTK
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vtkBoostBetweennessClustering.cxx
397 lines (336 loc) · 12.2 KB
/
vtkBoostBetweennessClustering.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
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
/*=========================================================================
Program: Visualization Toolkit
Module: vtkBoostGraphAdapter.h
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.
=========================================================================*/
#include "vtkBoostBetweennessClustering.h"
#include "vtkBoostConnectedComponents.h"
#include "vtkBoostGraphAdapter.h"
#include "vtkDataSetAttributes.h"
#include "vtkDirectedGraph.h"
#include "vtkFloatArray.h"
#include "vtkInformation.h"
#include "vtkInformationVector.h"
#include "vtkMutableDirectedGraph.h"
#include "vtkMutableUndirectedGraph.h"
#include "vtkObjectFactory.h"
#include "vtkSmartPointer.h"
#include "vtkUndirectedGraph.h"
#include <boost/graph/bc_clustering.hpp>
// @note: This piece of code is modification of algorithm from boost graph
// library. This modified version allows the user to pass edge weight map
namespace boost
{
// Graph clustering based on edge betweenness centrality.
//
// This algorithm implements graph clustering based on edge
// betweenness centrality. It is an iterative algorithm, where in each
// step it compute the edge betweenness centrality (via @ref
// brandes_betweenness_centrality) and removes the edge with the
// maximum betweenness centrality. The @p done function object
// determines when the algorithm terminates (the edge found when the
// algorithm terminates will not be removed).
//
// @param g The graph on which clustering will be performed. The type
// of this parameter (@c MutableGraph) must be a model of the
// VertexListGraph, IncidenceGraph, EdgeListGraph, and Mutable Graph
// concepts.
//
// @param done The function object that indicates termination of the
// algorithm. It must be a ternary function object thats accepts the
// maximum centrality, the descriptor of the edge that will be
// removed, and the graph @p g.
//
// @param edge_centrality (UTIL/out2) The property map that will store
// the betweenness centrality for each edge. When the algorithm
// terminates, it will contain the edge centralities for the
// graph. The type of this property map must model the
// ReadWritePropertyMap concept. Defaults to an @c
// iterator_property_map whose value type is
// @c Done::centrality_type and using @c get(edge_index, g) for the
// index map.
//
// @param vertex_index (IN) The property map that maps vertices to
// indices in the range @c [0, num_vertices(g)). This type of this
// property map must model the ReadablePropertyMap concept and its
// value type must be an integral type. Defaults to
// @c get(vertex_index, g).
template<typename MutableGraph, typename Done, typename EdgeCentralityMap,
typename EdgeWeightMap, typename VertexIndexMap>
void
betweenness_centrality_clustering(MutableGraph& g, Done done,
EdgeCentralityMap edge_centrality,
EdgeWeightMap edge_weight_map,
VertexIndexMap vertex_index)
{
typedef typename property_traits<EdgeCentralityMap>::value_type
centrality_type;
typedef typename graph_traits<MutableGraph>::edge_iterator edge_iterator;
typedef typename graph_traits<MutableGraph>::edge_descriptor edge_descriptor;
if (has_no_edges(g)) return;
// Function object that compares the centrality of edges
indirect_cmp<EdgeCentralityMap, std::less<centrality_type> >
cmp(edge_centrality);
bool is_done;
do {
brandes_betweenness_centrality(g,edge_centrality_map(edge_centrality)
.vertex_index_map(vertex_index)
.weight_map(edge_weight_map));
std::pair<edge_iterator, edge_iterator> edges_iters = edges(g);
edge_descriptor e = *max_element(edges_iters.first, edges_iters.second,
cmp);
is_done = done(get(edge_centrality, e), e, g);
if (!is_done) remove_edge(e, g);
} while (!is_done && !has_no_edges(g));
}
}
vtkStandardNewMacro(vtkBoostBetweennessClustering);
//-----------------------------------------------------------------------------
vtkBoostBetweennessClustering::vtkBoostBetweennessClustering() :
vtkGraphAlgorithm (),
Threshold (0),
UseEdgeWeightArray (false),
InvertEdgeWeightArray (false),
EdgeWeightArrayName (0),
EdgeCentralityArrayName (0)
{
this->SetNumberOfOutputPorts(2);
}
//-----------------------------------------------------------------------------
vtkBoostBetweennessClustering::~vtkBoostBetweennessClustering()
{
this->SetEdgeWeightArrayName(0);
this->SetEdgeCentralityArrayName(0);
}
//-----------------------------------------------------------------------------
void vtkBoostBetweennessClustering::PrintSelf(ostream &os, vtkIndent indent)
{
this->Superclass::PrintSelf(os, indent);
os << indent << "Threshold: " << this->Threshold << endl;
os << indent << "UseEdgeWeightArray: " << this->UseEdgeWeightArray << endl;
os << indent << "InvertEdgeWeightArray: " << this->InvertEdgeWeightArray
<< endl;
(EdgeWeightArrayName) ?
os << indent << "EdgeWeightArrayName: " << this->EdgeWeightArrayName
<< endl :
os << indent << "EdgeWeightArrayName: NULL" << endl;
(EdgeCentralityArrayName) ?
os << indent << "EdgeCentralityArrayName: " << this->EdgeCentralityArrayName
<< endl :
os << indent << "EdgeCentralityArrayName: NULL" << endl;
}
//-----------------------------------------------------------------------------
int vtkBoostBetweennessClustering::RequestData(
vtkInformation* vtkNotUsed(request),
vtkInformationVector** inputVector,
vtkInformationVector* outputVector)
{
// Helpful vars.
bool isDirectedGraph (false);
// Get the info objects
vtkInformation *inInfo = inputVector[0]->GetInformationObject(0);
if(!inInfo)
{
vtkErrorMacro("Failed to get input information.")
return 1;
}
vtkInformation* outInfo1 = outputVector->GetInformationObject(0);
if(!outInfo1)
{
vtkErrorMacro("Failed get output1 on information first port.")
}
vtkInformation* outInfo2 = outputVector->GetInformationObject(1);
if(!outInfo2)
{
vtkErrorMacro("Failed to get output2 information on second port.")
return 1;
}
// Get the input, output1 and output2.
vtkGraph* input = vtkGraph::SafeDownCast(inInfo->Get(
vtkDataObject::DATA_OBJECT()));
if(!input)
{
vtkErrorMacro("Failed to get input graph.")
return 1;
}
if(vtkDirectedGraph::SafeDownCast(input))
{
isDirectedGraph = true;
}
vtkGraph* output1 = vtkGraph::SafeDownCast(
outInfo1->Get(vtkDataObject::DATA_OBJECT()));
if(!output1)
{
vtkErrorMacro("Failed to get output1 graph.")
return 1;
}
vtkGraph* output2 = vtkGraph::SafeDownCast(
outInfo2->Get(vtkDataObject::DATA_OBJECT()));
if(!output2)
{
vtkErrorMacro("Failed to get output2 graph.")
return 1;
}
vtkSmartPointer<vtkFloatArray> edgeCM = vtkSmartPointer<vtkFloatArray>::New();
if(this->EdgeCentralityArrayName)
{
edgeCM->SetName(this->EdgeCentralityArrayName);
}
else
{
edgeCM->SetName("edge_centrality");
}
boost::vtkGraphEdgePropertyMapHelper<vtkFloatArray*> helper(edgeCM);
vtkSmartPointer<vtkDataArray> edgeWeight (0);
if(this->UseEdgeWeightArray && this->EdgeWeightArrayName)
{
if(!this->InvertEdgeWeightArray)
{
edgeWeight = input->GetEdgeData()->GetArray(this->EdgeWeightArrayName);
}
else
{
vtkDataArray* weights =
input->GetEdgeData()->GetArray(this->EdgeWeightArrayName);
if(!weights)
{
vtkErrorMacro(<<"Error: Edge weight array " << this->EdgeWeightArrayName
<< " is set but not found or not a data array.\n");
return 1;
}
edgeWeight.TakeReference(
vtkDataArray::CreateDataArray(weights->GetDataType()));
double range[2];
weights->GetRange(range);
if(weights->GetNumberOfComponents() > 1)
{
vtkErrorMacro("Expecting single component array.");
return 1;
}
for(int i=0; i < weights->GetDataSize(); ++i)
{
edgeWeight->InsertNextTuple1(range[1] - weights->GetTuple1(i));
}
}
if(!edgeWeight)
{
vtkErrorMacro(<<"Error: Edge weight array " << this->EdgeWeightArrayName
<< " is set but not found or not a data array.\n");
return 1;
}
}
// First compute the second output and the result will be used
// as input for the first output.
if(isDirectedGraph)
{
vtkMutableDirectedGraph* out2 = vtkMutableDirectedGraph::New();
// Copy the data to the second output (as this algorithm most likely
// going to removed edges (and hence modifies the graph).
out2->DeepCopy(input);
if(edgeWeight)
{
boost::vtkGraphEdgePropertyMapHelper<vtkDataArray*> helper2(edgeWeight);
boost::betweenness_centrality_clustering(out2,
boost::bc_clustering_threshold<double>(this->Threshold, out2, false),
helper, helper2, boost::get(boost::vertex_index, out2));
}
else
{
boost::betweenness_centrality_clustering(out2,
boost::bc_clustering_threshold<double>(
this->Threshold, out2, false), helper);
}
out2->GetEdgeData()->AddArray(edgeCM);
// Finally copy the results to the output.
output2->ShallowCopy(out2);
out2->Delete();
}
else
{
vtkMutableUndirectedGraph* out2 = vtkMutableUndirectedGraph::New();
// Send the data to output2.
out2->DeepCopy(input);
if(edgeWeight)
{
boost::vtkGraphEdgePropertyMapHelper<vtkDataArray*> helper2(edgeWeight);
boost::betweenness_centrality_clustering(out2,
boost::bc_clustering_threshold<double>(this->Threshold, out2,false),
helper, helper2, boost::get(boost::vertex_index, out2));
}
else
{
boost::betweenness_centrality_clustering(out2,
boost::bc_clustering_threshold<double>(this->Threshold, out2,false),
helper);
}
out2->GetEdgeData()->AddArray(edgeCM);
// Finally copy the results to the output.
output2->ShallowCopy(out2);
out2->Delete();
}
// Now take care of the first output.
vtkSmartPointer<vtkBoostConnectedComponents> bcc (
vtkSmartPointer<vtkBoostConnectedComponents>::New());
vtkSmartPointer<vtkGraph> output2Copy(0);
if(isDirectedGraph)
{
output2Copy = vtkSmartPointer<vtkDirectedGraph>::New();
}
else
{
output2Copy = vtkSmartPointer<vtkUndirectedGraph>::New();
}
output2Copy->ShallowCopy(output2);
bcc->SetInputData(0, output2Copy);
bcc->Update();
vtkSmartPointer<vtkGraph> bccOut = bcc->GetOutput(0);
vtkSmartPointer<vtkAbstractArray> compArray (0);
if(isDirectedGraph)
{
vtkSmartPointer<vtkDirectedGraph> out1
(vtkSmartPointer<vtkDirectedGraph>::New());
out1->ShallowCopy(input);
compArray = bccOut->GetVertexData()->GetAbstractArray("component");
if(!compArray)
{
vtkErrorMacro("Unable to get component array.")
return 1;
}
out1->GetVertexData()->AddArray(compArray);
// Finally copy the output to the algorithm output.
output1->ShallowCopy(out1);
}
else
{
vtkSmartPointer<vtkUndirectedGraph> out1
(vtkSmartPointer<vtkUndirectedGraph>::New());
out1->ShallowCopy(input);
compArray = bccOut->GetVertexData()->GetAbstractArray("component");
if(!compArray)
{
vtkErrorMacro("Unable to get component array.")
return 1;
}
out1->GetVertexData()->AddArray(compArray);
// Finally copy the output to the algorithm output.
output1->ShallowCopy(out1);
}
// Also add the components array to the second output.
output2->GetVertexData()->AddArray(compArray);
return 1;
}
//-----------------------------------------------------------------------------
int vtkBoostBetweennessClustering::FillOutputPortInformation(
int port, vtkInformation* info)
{
if(port == 0 || port == 1)
{
info->Set(vtkDataObject::DATA_TYPE_NAME(), "vtkGraph");
}
return 1;
}