-
Notifications
You must be signed in to change notification settings - Fork 0
/
vtkElbowGraphToPolyData.cxx
224 lines (191 loc) · 6.66 KB
/
vtkElbowGraphToPolyData.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
// Copyright 2009 Sandia Corporation, Kitware Inc.
// See LICENSE.txt for details.
#include "vtkElbowGraphToPolyData.h"
#include "vtkCellArray.h"
#include "vtkCellData.h"
#include "vtkGraph.h"
#include "vtkIdTypeArray.h"
#include "vtkInformation.h"
#include "vtkInformationVector.h"
#include "vtkMath.h"
#include "vtkObjectFactory.h"
#include "vtkPointData.h"
#include "vtkStringArray.h"
#include <vtksys/stl/map>
vtkCxxRevisionMacro(vtkElbowGraphToPolyData, "$Revision$");
vtkStandardNewMacro(vtkElbowGraphToPolyData);
class vtkElbowGraphToPolyDataInternal
{
public:
typedef vtksys_stl::map<vtkIdType, vtkIdType> MapIdTypeToIdType;
MapIdTypeToIdType PointMapping;
vtkIdType AddEdge(vtkGraph* input, vtkPolyData* output, vtkIdType edgeId,
double factor);
vtkIdType StorePoint(double point[3], vtkIdType inIndex, vtkPoints* outputPoints,
vtkGraph* inputGraph, vtkPolyData* outputPD, vtkIdType copyPoint,
vtkIdType copyNames, int removeLabel);
void Initialize();
vtkStringArray* InputNamesArray;
vtkStringArray* OutputNamesArray;
};
vtkIdType vtkElbowGraphToPolyDataInternal::AddEdge(vtkGraph*
input, vtkPolyData* output, vtkIdType edgeId, double factor)
{
// Get input and output components
vtkPoints* inputPts = input->GetPoints();
vtkPoints* outputPts = output->GetPoints();
vtkCellArray* lines = output->GetLines();
// For the elbow
vtkIdType points[2];
points[0] = input->GetSourceVertex(edgeId);
points[1] = input->GetTargetVertex(edgeId);
// Calculate middle points
double pointSource[3];
double pointTarget[3];
double midPoint[3];
//double inPointSource[3];
//double inPointTarget[3];
inputPts->GetPoint(points[0], pointSource);
inputPts->GetPoint(points[1], pointTarget);
//double invFactor = 1 - factor;
double xFactor = 0;
double yFactor = 0;
#define myABS(x) (((x) < 0)?(-(x)):(x))
if ( factor >= 0 )
{
xFactor = factor;
yFactor = 1;
}
else
{
xFactor = 1;
yFactor = myABS(factor);
}
midPoint[0] = (pointSource[0]*xFactor + pointTarget[0]*(1-xFactor));
midPoint[1] = (pointSource[1]*yFactor + pointTarget[1]*(1-yFactor));
midPoint[2] = (pointSource[2] + pointTarget[2])/2;
int removeInLabel = 1;
int removeMidLabel = 0;
int removeOutLabel = 1;
// Copy the points to the target
vtkIdType outputPolyLine[4];
outputPolyLine[0] = this->StorePoint(pointSource, points[0], outputPts, input, output, points[0], points[0], removeInLabel);
outputPolyLine[1] = this->StorePoint(midPoint, -1 , outputPts, input, output, points[0], points[1], removeMidLabel);
outputPolyLine[2] = this->StorePoint(pointTarget, points[1], outputPts, input, output, points[1], points[1], removeOutLabel);
return lines->InsertNextCell(3, outputPolyLine);
}
vtkIdType vtkElbowGraphToPolyDataInternal::StorePoint(double point[3],
vtkIdType inIndex, vtkPoints* outputPoints, vtkGraph* inputGraph,
vtkPolyData* outputPD, vtkIdType copyPoint, vtkIdType copyNames, int removeLabel)
{
vtkElbowGraphToPolyDataInternal::MapIdTypeToIdType::iterator cpointIt
= this->PointMapping.find(inIndex);
if ( cpointIt != this->PointMapping.end() )
{
return cpointIt->second;
}
vtkIdType newPtIdx = outputPoints->InsertNextPoint(point);
if ( inIndex >= 0 )
{
this->PointMapping[inIndex] = newPtIdx;
}
outputPD->GetPointData()->CopyData(inputGraph->GetVertexData(),copyPoint,newPtIdx);
if ( this->OutputNamesArray )
{
if ( removeLabel )
{
this->OutputNamesArray->SetValue(newPtIdx, "");
}
else if ( this->InputNamesArray )
{
this->OutputNamesArray->SetValue(newPtIdx, this->InputNamesArray->GetValue(copyNames));
}
}
return newPtIdx;
}
void vtkElbowGraphToPolyDataInternal::Initialize()
{
this->PointMapping.erase(this->PointMapping.begin(), this->PointMapping.end());
this->InputNamesArray = 0;
this->OutputNamesArray = 0;
}
vtkElbowGraphToPolyData::vtkElbowGraphToPolyData()
{
this->Internals = new vtkElbowGraphToPolyDataInternal;
this->Factor = 0;
this->Elbow = 0;
}
vtkElbowGraphToPolyData::~vtkElbowGraphToPolyData()
{
delete this->Internals;
this->Internals = 0;
}
void vtkElbowGraphToPolyData::PrintSelf(ostream& os, vtkIndent indent)
{
this->Superclass::PrintSelf(os,indent);
os << indent << "Factor " << this->Factor << endl;
os << indent << "Elbow " << (this->Elbow?"ON":"OFF") << endl;
}
int vtkElbowGraphToPolyData::RequestData(
vtkInformation *request,
vtkInformationVector **inputVector,
vtkInformationVector *outputVector)
{
if ( !this->Elbow )
{
return this->Superclass::RequestData(request, inputVector, outputVector);
}
this->Internals->Initialize();
// get the info objects
vtkInformation *inInfo = inputVector[0]->GetInformationObject(0);
vtkInformation *outInfo = outputVector->GetInformationObject(0);
// get the input and ouptut
vtkGraph *input = vtkGraph::SafeDownCast(
inInfo->Get(vtkDataObject::DATA_OBJECT()));
vtkPolyData *output = vtkPolyData::SafeDownCast(
outInfo->Get(vtkDataObject::DATA_OBJECT()));
vtkIdType numPts = input->GetNumberOfVertices();
output->GetPointData()->CopyAllocate(input->GetVertexData(), 4*numPts);
vtkDataArray* edgeGhostLevels = vtkDataArray::SafeDownCast(
input->GetVertexData()->GetAbstractArray("vtkGhostLevels"));
vtkCellArray* newLines = vtkCellArray::New();
output->SetLines(newLines);
newLines->Delete();
vtkPoints* outputPoints = vtkPoints::New();
output->SetPoints(outputPoints);
outputPoints->Delete();
vtkIdType numEdges = input->GetNumberOfEdges();
vtkDataSetAttributes* inputEdgeData = input->GetEdgeData();
vtkCellData* outputCellData = output->GetCellData();
outputCellData->CopyAllocate(inputEdgeData);
newLines->Allocate(newLines->EstimateSize(numEdges, 2));
this->Internals->InputNamesArray = vtkStringArray::SafeDownCast(
input->GetVertexData()->GetAbstractArray("name"));
this->Internals->OutputNamesArray = vtkStringArray::SafeDownCast(
output->GetPointData()->GetAbstractArray("name"));
//vtkIdType points[2];
if (edgeGhostLevels == NULL)
{
// Send the data to output.
for (vtkIdType i = 0; i < numEdges; i++)
{
this->Internals->AddEdge(input, output, i, this->Factor);
}
// Cells correspond to edges, so pass the cell data along.
output->GetCellData()->PassData(input->GetEdgeData());
}
else
{
// Only create lines for non-ghost edges
for (vtkIdType i = 0; i < numEdges; i++)
{
if (edgeGhostLevels->GetComponent(i, 0) == 0)
{
vtkIdType ind = this->Internals->AddEdge(input, output, i, this->Factor);
outputCellData->CopyData(inputEdgeData, i, ind);
}
}
}
output->Squeeze();
return 1;
}