forked from Kitware/VTK
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vtkGraphToPolyData.cxx
227 lines (204 loc) · 7.31 KB
/
vtkGraphToPolyData.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
/*=========================================================================
Program: Visualization Toolkit
Module: vtkGraphToPolyData.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 "vtkGraphToPolyData.h"
#include "vtkCellArray.h"
#include "vtkCellData.h"
#include "vtkDirectedGraph.h"
#include "vtkDoubleArray.h"
#include "vtkEdgeListIterator.h"
#include "vtkGlyph3D.h"
#include "vtkGlyphSource2D.h"
#include "vtkGraph.h"
#include "vtkIdTypeArray.h"
#include "vtkInformation.h"
#include "vtkInformationVector.h"
#include "vtkMath.h"
#include "vtkObjectFactory.h"
#include "vtkPointData.h"
#include "vtkPoints.h"
#include "vtkPolyData.h"
#include "vtkSmartPointer.h"
#include <map>
#include <utility>
#include <vector>
vtkStandardNewMacro(vtkGraphToPolyData);
vtkGraphToPolyData::vtkGraphToPolyData()
{
this->EdgeGlyphOutput = false;
this->EdgeGlyphPosition = 1.0;
this->SetNumberOfOutputPorts(2);
}
int vtkGraphToPolyData::FillInputPortInformation(int vtkNotUsed(port), vtkInformation* info)
{
info->Set(vtkAlgorithm::INPUT_REQUIRED_DATA_TYPE(), "vtkGraph");
return 1;
}
int vtkGraphToPolyData::RequestData(
vtkInformation *vtkNotUsed(request),
vtkInformationVector **inputVector,
vtkInformationVector *outputVector)
{
// get the info objects
vtkInformation *inInfo = inputVector[0]->GetInformationObject(0);
vtkInformation *outInfo = outputVector->GetInformationObject(0);
vtkInformation *arrowInfo = outputVector->GetInformationObject(1);
// get the input and output
vtkGraph *input = vtkGraph::SafeDownCast(
inInfo->Get(vtkDataObject::DATA_OBJECT()));
vtkPolyData *output = vtkPolyData::SafeDownCast(
outInfo->Get(vtkDataObject::DATA_OBJECT()));
vtkPolyData *arrowOutput = vtkPolyData::SafeDownCast(
arrowInfo->Get(vtkDataObject::DATA_OBJECT()));
vtkDataArray* edgeGhostLevels = vtkArrayDownCast<vtkDataArray>(
input->GetEdgeData()->GetAbstractArray(vtkDataSetAttributes::GhostArrayName()));
if (edgeGhostLevels == NULL)
{
vtkSmartPointer<vtkIdTypeArray> cells =
vtkSmartPointer<vtkIdTypeArray>::New();
vtkSmartPointer<vtkEdgeListIterator> it =
vtkSmartPointer<vtkEdgeListIterator>::New();
input->GetEdges(it);
vtkSmartPointer<vtkPoints> newPoints =
vtkSmartPointer<vtkPoints>::New();
newPoints->DeepCopy(input->GetPoints());
output->SetPoints(newPoints);
vtkIdType numEdges = input->GetNumberOfEdges();
bool noExtraPoints = true;
for (vtkIdType e = 0; e < numEdges; ++e)
{
vtkIdType npts;
double* pts;
input->GetEdgePoints(e, npts, pts);
vtkIdType source = input->GetSourceVertex(e);
vtkIdType target = input->GetTargetVertex(e);
if (npts == 0)
{
cells->InsertNextValue(2);
cells->InsertNextValue(source);
cells->InsertNextValue(target);
}
else
{
cells->InsertNextValue(2+npts);
cells->InsertNextValue(source);
for (vtkIdType i = 0; i < npts; ++i, pts += 3)
{
noExtraPoints = false;
vtkIdType pt = output->GetPoints()->InsertNextPoint(pts);
cells->InsertNextValue(pt);
}
cells->InsertNextValue(target);
}
}
vtkSmartPointer<vtkCellArray> newLines =
vtkSmartPointer<vtkCellArray>::New();
newLines->SetCells(numEdges, cells);
// Send the data to output.
output->SetLines(newLines);
// Points only correspond to vertices if we didn't add extra points.
if (noExtraPoints)
{
output->GetPointData()->PassData(input->GetVertexData());
}
// Cells correspond to edges, so pass the cell data along.
output->GetCellData()->PassData(input->GetEdgeData());
}
else
{
vtkIdType numEdges = input->GetNumberOfEdges();
vtkDataSetAttributes *inputCellData = input->GetEdgeData();
vtkCellData *outputCellData = output->GetCellData();
outputCellData->CopyAllocate(inputCellData);
vtkSmartPointer<vtkCellArray> newLines =
vtkSmartPointer<vtkCellArray>::New();
newLines->Allocate(newLines->EstimateSize(numEdges, 2));
vtkIdType points[2];
// Only create lines for non-ghost edges
vtkSmartPointer<vtkEdgeListIterator> it =
vtkSmartPointer<vtkEdgeListIterator>::New();
input->GetEdges(it);
while (it->HasNext())
{
vtkEdgeType e = it->Next();
if (edgeGhostLevels->GetComponent(e.Id, 0) == 0)
{
points[0] = e.Source;
points[1] = e.Target;
vtkIdType ind = newLines->InsertNextCell(2, points);
outputCellData->CopyData(inputCellData, e.Id, ind);
}
}
// Send data to output
output->SetPoints(input->GetPoints());
output->SetLines(newLines);
output->GetPointData()->PassData(input->GetVertexData());
// Clean up
output->Squeeze();
}
if (this->EdgeGlyphOutput)
{
vtkDataSetAttributes *inputCellData = input->GetEdgeData();
vtkPointData* arrowPointData = arrowOutput->GetPointData();
arrowPointData->CopyAllocate(inputCellData);
vtkPoints* newPoints = vtkPoints::New();
arrowOutput->SetPoints(newPoints);
newPoints->Delete();
vtkDoubleArray* orientArr = vtkDoubleArray::New();
orientArr->SetNumberOfComponents(3);
orientArr->SetName("orientation");
arrowPointData->AddArray(orientArr);
arrowPointData->SetVectors(orientArr);
orientArr->Delete();
double sourcePt[3] = {0, 0, 0};
double targetPt[3] = {0, 0, 0};
double pt[3] = {0, 0, 0};
double orient[3] = {0, 0, 0};
vtkSmartPointer<vtkEdgeListIterator> it =
vtkSmartPointer<vtkEdgeListIterator>::New();
input->GetEdges(it);
while (it->HasNext())
{
vtkEdgeType e = it->Next();
if (!edgeGhostLevels || edgeGhostLevels->GetComponent(e.Id, 0) == 0)
{
vtkIdType source = e.Source;
vtkIdType target = e.Target;
// Do not render arrows for self loops.
if (source != target)
{
input->GetPoint(source, sourcePt);
input->GetPoint(target, targetPt);
for (int j = 0; j < 3; j++)
{
pt[j] = (1 - this->EdgeGlyphPosition)*sourcePt[j] + this->EdgeGlyphPosition*targetPt[j];
orient[j] = targetPt[j] - sourcePt[j];
}
vtkIdType ind = newPoints->InsertNextPoint(pt);
orientArr->InsertNextTuple(orient);
arrowPointData->CopyData(inputCellData, e.Id, ind);
}
}
}
}
return 1;
}
void vtkGraphToPolyData::PrintSelf(ostream& os, vtkIndent indent)
{
this->Superclass::PrintSelf(os,indent);
os << indent << "EdgeGlyphOutput: "
<< (this->EdgeGlyphOutput ? "on" : "off") << endl;
os << indent << "EdgeGlyphPosition: " << this->EdgeGlyphPosition << endl;
}