forked from Kitware/VTK
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vtkStrahlerMetric.cxx
159 lines (131 loc) · 4.57 KB
/
vtkStrahlerMetric.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
/*=========================================================================
Program: Visualization Toolkit
Module: vtkStrahlerMetric.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 "vtkStrahlerMetric.h"
#include "vtkFloatArray.h"
#include "vtkObjectFactory.h"
#include "vtkOutEdgeIterator.h"
#include "vtkInformation.h"
#include "vtkInformationVector.h"
#include "vtkDataSetAttributes.h"
#include "vtkSmartPointer.h"
//--------------------------------------------------------------------------
vtkStandardNewMacro(vtkStrahlerMetric);
vtkStrahlerMetric::vtkStrahlerMetric()
{
this->MaxStrahler = 0;
this->Normalize = 0;
this->MetricArrayName = 0;
this->SetMetricArrayName( "Strahler" );
}
vtkStrahlerMetric::~vtkStrahlerMetric()
{
this->SetMetricArrayName(0);
}
//----------------------------------------------------------------------------
float vtkStrahlerMetric::CalculateStrahler(
vtkIdType root,
vtkFloatArray *metric,
vtkTree *tree)
{
float strahler, maxStrahler, *childStrahler;
bool same;
vtkSmartPointer<vtkOutEdgeIterator> children
= vtkSmartPointer<vtkOutEdgeIterator>::New();
vtkIdType nrChildren = tree->GetNumberOfChildren(root);
// A leaf node has a Strahler value of 1.
if (nrChildren == 0)
{
strahler = 1.0;
}
else
{
// Non-leaf node: find the Strahler values of the children.
childStrahler = new float[nrChildren];
tree->GetOutEdges(root, children);
for (vtkIdType i = 0; i < nrChildren; i++)
{
childStrahler[i] = this->CalculateStrahler(
children->Next().Target,
metric,
tree);
}
// Determine if the children have the same strahler values
same = true;
maxStrahler = childStrahler[0];
for (vtkIdType j = 1; j < nrChildren; j++)
{
same = same && (maxStrahler == childStrahler[j]);
if (maxStrahler < childStrahler[j])
{
maxStrahler = childStrahler[j];
}
}
// Calculate the strahler value for this node
strahler = (same) ? maxStrahler + nrChildren - 1
: maxStrahler + nrChildren - 2;
delete [] childStrahler;
}
// Record the strahler value within the array.
metric->InsertValue(root, strahler);
if (strahler > this->MaxStrahler)
{
this->MaxStrahler = strahler;
}
return strahler;
}
int vtkStrahlerMetric::RequestData(vtkInformation *vtkNotUsed(request),
vtkInformationVector **inputVector,
vtkInformationVector *outputVector)
{
// get the info objects
vtkInformation *inInfo = inputVector[0]->GetInformationObject(0);
vtkInformation *outInfo = outputVector->GetInformationObject(0);
vtkDebugMacro(<<"StrahlerMetric executing." );
// get the input and output
vtkTree *input = vtkTree::SafeDownCast(
inInfo->Get(vtkDataObject::DATA_OBJECT()));
vtkTree *output = vtkTree::SafeDownCast(
outInfo->Get(vtkDataObject::DATA_OBJECT()));
// The output model should share the data of the input.
output->ShallowCopy(input);
// Create a new array to hold the metric.
vtkFloatArray *metric = vtkFloatArray::New();
metric->SetName(this->MetricArrayName);
metric->SetNumberOfValues(input->GetNumberOfVertices());
this->MaxStrahler = 1.0;
this->CalculateStrahler(
input->GetRoot(),
metric,
input);
if (this->Normalize)
{
for (vtkIdType i = 0; i < input->GetNumberOfVertices(); i++)
{
metric->SetValue(i, metric->GetValue(i)/this->MaxStrahler);
}
}
output->GetVertexData()->AddArray(metric);
metric->Delete();
vtkDebugMacro(<<"StrahlerMetric done.");
return 1;
}
void vtkStrahlerMetric::PrintSelf(ostream& os, vtkIndent indent)
{
this->Superclass::PrintSelf(os,indent);
os << indent << "Normalize: " << this->Normalize << endl;
os << indent << "MaxStrahler: " << this->MaxStrahler << endl;
os << indent << "MetricArrayName: " << (this->MetricArrayName ? this->MetricArrayName : "(none)") << endl;
}