forked from Kitware/VTK
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathvtkTree.cxx
219 lines (193 loc) · 5.57 KB
/
vtkTree.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
/*=========================================================================
Program: Visualization Toolkit
Module: vtkTree.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 "vtkTree.h"
#include "vtkInformation.h"
#include "vtkInformationVector.h"
#include "vtkObjectFactory.h"
#include "vtkOutEdgeIterator.h"
#include "vtkSmartPointer.h"
#include <vtksys/stl/vector>
using vtksys_stl::vector;
vtkStandardNewMacro(vtkTree);
//----------------------------------------------------------------------------
vtkTree::vtkTree()
{
this->Root = -1;
}
//----------------------------------------------------------------------------
vtkTree::~vtkTree()
{
}
//----------------------------------------------------------------------------
vtkIdType vtkTree::GetChild(vtkIdType v, vtkIdType i)
{
const vtkOutEdgeType *edges;
vtkIdType nedges;
this->GetOutEdges(v, edges, nedges);
if (i < nedges)
{
return edges[i].Target;
}
return -1;
}
//----------------------------------------------------------------------------
vtkIdType vtkTree::GetParent(vtkIdType v)
{
const vtkInEdgeType *edges;
vtkIdType nedges;
this->GetInEdges(v, edges, nedges);
if (nedges > 0)
{
return edges[0].Source;
}
return -1;
}
//----------------------------------------------------------------------------
vtkEdgeType vtkTree::GetParentEdge(vtkIdType v)
{
const vtkInEdgeType *edges;
vtkIdType nedges;
this->GetInEdges(v, edges, nedges);
if (nedges > 0)
{
return vtkEdgeType(edges[0].Source, v, edges[0].Id);
}
return vtkEdgeType();
}
//----------------------------------------------------------------------------
vtkIdType vtkTree::GetLevel(vtkIdType vertex)
{
if (vertex < 0 || vertex >= this->GetNumberOfVertices())
{
return -1;
}
vtkIdType level = 0;
while (vertex != this->Root)
{
vertex = this->GetParent(vertex);
level++;
}
return level;
}
//----------------------------------------------------------------------------
bool vtkTree::IsLeaf(vtkIdType vertex)
{
return (this->GetNumberOfChildren(vertex) == 0);
}
//----------------------------------------------------------------------------
vtkTree *vtkTree::GetData(vtkInformation *info)
{
return info? vtkTree::SafeDownCast(info->Get(DATA_OBJECT())) : 0;
}
//----------------------------------------------------------------------------
vtkTree *vtkTree::GetData(vtkInformationVector *v, int i)
{
return vtkTree::GetData(v->GetInformationObject(i));
}
//----------------------------------------------------------------------------
bool vtkTree::IsStructureValid(vtkGraph *g)
{
if (vtkTree::SafeDownCast(g))
{
// Since a tree has the additional root propery, we need
// to set that here.
this->Root = (vtkTree::SafeDownCast(g))->Root;
return true;
}
// Empty graph is a valid tree.
if (g->GetNumberOfVertices() == 0)
{
this->Root = -1;
return true;
}
// A tree must have one more vertex than its number of edges.
if (g->GetNumberOfEdges() != g->GetNumberOfVertices() - 1)
{
return false;
}
// Find the root and fail if there is more than one.
vtkIdType root = -1;
for (vtkIdType v = 0; v < g->GetNumberOfVertices(); ++v)
{
vtkIdType indeg = g->GetInDegree(v);
if (indeg > 1)
{
// No tree vertex should have in degree > 1, so fail.
return false;
}
else if (indeg == 0 && root == -1)
{
// We found our first root.
root = v;
}
else if (indeg == 0)
{
// We already found a root, so fail.
return false;
}
}
if (root < 0)
{
return false;
}
// Make sure the tree is connected with no cycles.
vector<bool> visited(g->GetNumberOfVertices(), false);
vector<vtkIdType> stack;
stack.push_back(root);
vtkSmartPointer<vtkOutEdgeIterator> outIter =
vtkSmartPointer<vtkOutEdgeIterator>::New();
while (!stack.empty())
{
vtkIdType v = stack.back();
stack.pop_back();
visited[v] = true;
g->GetOutEdges(v, outIter);
while (outIter->HasNext())
{
vtkIdType id = outIter->Next().Target;
if (!visited[id])
{
stack.push_back(id);
}
else
{
return false;
}
}
}
for (vtkIdType v = 0; v < g->GetNumberOfVertices(); ++v)
{
if (!visited[v])
{
return false;
}
}
// Since a tree has the additional root propery, we need
// to set that here.
this->Root = root;
return true;
}
//----------------------------------------------------------------------------
void vtkTree::ReorderChildren(vtkIdType parent, vtkIdTypeArray *children)
{
this->ReorderOutVertices(parent, children);
}
//----------------------------------------------------------------------------
void vtkTree::PrintSelf(ostream& os, vtkIndent indent)
{
this->Superclass::PrintSelf(os,indent);
os << indent << "Root: " << this->Root << endl;
}