forked from Kitware/VTK
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vtkEdgeCenters.cxx
195 lines (169 loc) · 5.2 KB
/
vtkEdgeCenters.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
/*=========================================================================
Program: Visualization Toolkit
Module: vtkEdgeCenters.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.
=========================================================================*/
#include "vtkEdgeCenters.h"
#include "vtkCellArray.h"
#include "vtkCellData.h"
#include "vtkDataSetAttributes.h"
#include "vtkEdgeListIterator.h"
#include "vtkGraph.h"
#include "vtkInformation.h"
#include "vtkInformationVector.h"
#include "vtkMath.h"
#include "vtkObjectFactory.h"
#include "vtkPointData.h"
#include "vtkPoints.h"
#include "vtkPolyData.h"
vtkStandardNewMacro(vtkEdgeCenters);
// Construct object with vertex cell generation turned off.
vtkEdgeCenters::vtkEdgeCenters()
{
this->VertexCells = 0;
}
// Generate points
int vtkEdgeCenters::RequestData(
vtkInformation *vtkNotUsed(request),
vtkInformationVector **inputVector,
vtkInformationVector *outputVector)
{
// get the info objects
vtkInformation *inInfo = inputVector[0]->GetInformationObject(0);
vtkInformation *outInfo = outputVector->GetInformationObject(0);
// get the input and output
vtkGraph *input = vtkGraph::SafeDownCast(
inInfo->Get(vtkDataObject::DATA_OBJECT()));
vtkPolyData *output = vtkPolyData::SafeDownCast(
outInfo->Get(vtkDataObject::DATA_OBJECT()));
vtkIdType numEdges;
vtkDataSetAttributes *inED;
vtkDataSetAttributes *outPD;
vtkPoints *newPts;
double x[3];
inED=input->GetEdgeData();
outPD=output->GetPointData();
if ( (numEdges = input->GetNumberOfEdges()) < 1 )
{
vtkDebugMacro(<<"No cells to generate center points for");
return 1;
}
newPts = vtkPoints::New();
newPts->SetNumberOfPoints(numEdges);
int abort=0;
vtkIdType progressInterval = numEdges/10 + 1;
vtkEdgeListIterator *edges = vtkEdgeListIterator::New();
input->GetEdges(edges);
vtkIdType processed = 0;
while (edges->HasNext() && !abort)
{
vtkEdgeType e = edges->Next();
if ( ! (processed % progressInterval) )
{
vtkDebugMacro(<<"Processing #" << processed);
this->UpdateProgress (0.5*processed/numEdges);
abort = this->GetAbortExecute();
}
double p1[3];
double p2[3];
input->GetPoint(e.Source, p1);
input->GetPoint(e.Target, p2);
vtkIdType npts = 0;
double* pts = 0;
input->GetEdgePoints(e.Id, npts, pts);
// Find the length of the edge
if (npts == 0)
{
for (int c = 0; c < 3; ++c)
{
x[c] = (p1[c] + p2[c]) / 2.0;
}
}
else
{
vtkIdType nptsFull = npts + 2;
double* ptsFull = new double[3*(nptsFull)];
ptsFull[0] = p1[0];
ptsFull[1] = p1[1];
ptsFull[2] = p1[2];
memcpy(ptsFull+3, pts, sizeof(double)*3*npts);
ptsFull[3*(nptsFull-1)+0] = p2[0];
ptsFull[3*(nptsFull-1)+1] = p2[1];
ptsFull[3*(nptsFull-1)+2] = p2[2];
double len = 0.0;
double* curPt = ptsFull;
for (vtkIdType i = 0; i < nptsFull-1; ++i, curPt += 3)
{
len += sqrt(vtkMath::Distance2BetweenPoints(curPt, curPt+3));
}
double half = len / 2;
len = 0.0;
curPt = ptsFull;
for (vtkIdType i = 0; i < nptsFull-1; ++i, curPt += 3)
{
double curDist = sqrt(vtkMath::Distance2BetweenPoints(curPt, curPt+3));
if (len + curDist > half)
{
double alpha = (half - len)/curDist;
for (int c = 0; c < 3; ++c)
{
x[c] = (1.0 - alpha)*curPt[c] + alpha*curPt[c+3];
}
break;
}
len += curDist;
}
delete [] ptsFull;
}
newPts->SetPoint(e.Id, x);
++processed;
}
edges->Delete();
if ( this->VertexCells )
{
vtkIdType pts[1];
vtkCellData *outCD = output->GetCellData();
vtkCellArray *verts = vtkCellArray::New();
verts->Allocate(verts->EstimateSize(numEdges, 2), 1);
processed = 0;
edges = vtkEdgeListIterator::New();
input->GetEdges(edges);
while (edges->HasNext() && !abort)
{
vtkEdgeType e = edges->Next();
if ( ! (processed % progressInterval) )
{
vtkDebugMacro(<<"Processing #" << processed);
this->UpdateProgress (0.5+0.5*processed/numEdges);
abort = this->GetAbortExecute();
}
pts[0] = e.Id;
verts->InsertNextCell(1, pts);
++processed;
}
edges->Delete();
output->SetVerts(verts);
verts->Delete();
outCD->PassData(inED); //only if verts are generated
}
// clean up and update output
output->SetPoints(newPts);
newPts->Delete();
outPD->PassData(inED); //because number of points = number of cells
return 1;
}
int vtkEdgeCenters::FillInputPortInformation(int, vtkInformation *info)
{
info->Set(vtkAlgorithm::INPUT_REQUIRED_DATA_TYPE(), "vtkGraph");
return 1;
}
void vtkEdgeCenters::PrintSelf(ostream& os, vtkIndent indent)
{
this->Superclass::PrintSelf(os,indent);
os << indent << "Vertex Cells: " << (this->VertexCells ? "On\n" : "Off\n");
}