VMTK
Loading...
Searching...
No Matches
vtkvmtkTopologicalSeamFilter.cxx
Go to the documentation of this file.
1/*=========================================================================
2
3 Program: VMTK
4 Module: $RCSfile: vtkvmtkTopologicalSeamFilter.cxx,v $
5 Language: C++
6 Date: $Date: 2015/12/01 12:26:27 $
7 Version: $Revision: 1.1 $
8
9 Copyright (c) Luca Antiga, David Steinman. All rights reserved.
10 See LICENSE file for details.
11
12 Portions of this code are covered under the VTK copyright.
13 See VTKCopyright.txt or http://www.kitware.com/VTKCopyright.htm
14 for details.
15
16 This software is distributed WITHOUT ANY WARRANTY; without even
17 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
18 PURPOSE. See the above copyright notices for more information.
19
20 =========================================================================*/
21
22#include <queue>
23
25
26#include "vtkvmtkConstants.h"
27
28#include "vtkCell.h"
29#include "vtkDataArray.h"
30#include "vtkDoubleArray.h"
31#include "vtkMath.h"
32#include "vtkPointData.h"
33#include "vtkPolyData.h"
34#include "vtkUnsignedCharArray.h"
35
36#include "vtkInformation.h"
37#include "vtkInformationVector.h"
38#include "vtkObjectFactory.h"
39#include "vtkVersion.h"
40
41
43
45{
46 this->ClosestPoint[0] = this->ClosestPoint[1] = this->ClosestPoint[2] = 0.0;
47 this->SeamScalarsArrayName = NULL;
48 this->SeamFunction = NULL;
49}
50
52{
53 if (this->SeamScalarsArrayName)
54 {
55 delete[] this->SeamScalarsArrayName;
56 this->SeamScalarsArrayName = NULL;
57 }
58
59 if (this->SeamFunction)
60 {
61 this->SeamFunction->Delete();
62 this->SeamFunction = NULL;
63 }
64}
65
66int vtkvmtkTopologicalSeamFilter::RequestData(vtkInformation *vtkNotUsed(request),
67 vtkInformationVector **inputVector,
68 vtkInformationVector *outputVector)
69{
70 vtkInformation *inInfo = inputVector[0]->GetInformationObject(0);
71 vtkInformation *outInfo = outputVector->GetInformationObject(0);
72
73 vtkPolyData *input = vtkPolyData::SafeDownCast(inInfo->Get(vtkDataObject::DATA_OBJECT()));
74 vtkPolyData *output = vtkPolyData::SafeDownCast(outInfo->Get(vtkDataObject::DATA_OBJECT()));
75
76 if (this->SeamScalarsArrayName == NULL)
77 {
78 vtkErrorMacro("Name of seam scalars array not specified.");
79 return 1;
80 }
81
82 vtkDoubleArray* sourceArray = vtkDoubleArray::New();
83
84 vtkIdType numberOfPoints = input->GetNumberOfPoints();
85
86 if (this->SeamFunction == NULL)
87 {
88 if (input->GetPointData()->GetArray(this->SeamScalarsArrayName))
89 {
90 sourceArray->DeepCopy(input->GetPointData()->GetArray(this->SeamScalarsArrayName));
91 }
92 else
93 {
94 vtkErrorMacro("Seam scalars with name specified does not exist and seam function not specified.");
95 return 1;
96 }
97 }
98 else
99 {
100 sourceArray->SetNumberOfTuples(input->GetNumberOfPoints());
101 double point[3], value;
102 for (vtkIdType i=0; i<numberOfPoints; i++)
103 {
104 input->GetPoint(i,point);
105 value = this->SeamFunction->FunctionValue(point);
106 sourceArray->SetValue(i,value);
107 }
108 }
109
110 vtkIdType numberOfCells = input->GetNumberOfCells();
111
112 vtkUnsignedCharArray* visitedArray = vtkUnsignedCharArray::New();
113 visitedArray->SetNumberOfTuples(numberOfPoints);
114 visitedArray->FillComponent(0,0.0);
115
116 vtkDoubleArray* seamArray = vtkDoubleArray::New();
117 seamArray->SetName(this->SeamScalarsArrayName);
118 seamArray->SetNumberOfTuples(numberOfPoints);
119 seamArray->FillComponent(0,0.0);
120
121 input->BuildCells();
122 input->BuildLinks();
123
124 vtkIdType firstSeamPointId = -1;
125 double minDist = VTK_VMTK_LARGE_DOUBLE;
126
127 vtkIdType npts;
128 const vtkIdType *pts;
129 // get the point that is closest to the implicit plane
130 for (vtkIdType i=0; i<numberOfCells; i++)
131 {
132 input->GetCellPoints(i,npts,pts);
133 for (vtkIdType j=0; j<npts-1; j++)
134 {
135 vtkIdType point0Id = pts[j];
136 vtkIdType point1Id = pts[j+1];
137 double point0Value = sourceArray->GetTuple1(point0Id);
138 double point1Value = sourceArray->GetTuple1(point1Id);
139 if (point0Value * point1Value < 0.0)
140 {
141 double point0[3], point1[3];
142 input->GetPoint(point0Id,point0);
143 input->GetPoint(point1Id,point1);
144 double dist0 = vtkMath::Distance2BetweenPoints(point0,this->ClosestPoint);
145 double dist1 = vtkMath::Distance2BetweenPoints(point1,this->ClosestPoint);
146 if (dist0 < minDist)
147 {
148 minDist = dist0;
149 firstSeamPointId = point0Id;
150 }
151 if (dist1 < minDist)
152 {
153 minDist = dist1;
154 firstSeamPointId = point1Id;
155 }
156 continue;
157 }
158 }
159 }
160
161 std::queue<vtkIdType> seamQueue;
162
163 seamQueue.push(firstSeamPointId);
164
165 std::queue<vtkIdType> leftQueue;
166 std::queue<vtkIdType> rightQueue;
167
168 vtkIdType *cells;
169 vtkIdType ncells;
170
171 while (!seamQueue.empty())
172 {
173 vtkIdType point0Id = seamQueue.front();
174 seamQueue.pop();
175 double point0Value = sourceArray->GetTuple1(point0Id);
176 visitedArray->SetValue(point0Id,1);
177 seamArray->SetValue(point0Id,point0Value);
178 input->GetPointCells(point0Id,ncells,cells);
179 for (vtkIdType i=0; i<ncells; i++)
180 {
181 vtkIdType cellId = cells[i];
182 input->GetCellPoints(cellId,npts,pts);
183 for (vtkIdType j=0; j<npts; j++)
184 {
185 vtkIdType point1Id = pts[j];
186 if (visitedArray->GetValue(point1Id) == 1)
187 {
188 continue;
189 }
190 double point1Value = sourceArray->GetTuple1(point1Id);
191 if (point0Value * point1Value > 0.0)
192 {
193 seamArray->SetValue(point1Id,point1Value);
194 if (point1Value > 0.0)
195 {
196 rightQueue.push(point1Id);
197 }
198 else
199 {
200 leftQueue.push(point1Id);
201 }
202 continue;
203 }
204 visitedArray->SetValue(point1Id,1);
205 seamArray->SetValue(point1Id,point1Value);
206 seamQueue.push(point1Id);
207 }
208 }
209 }
210
211 const double leftValue = -1.0;
212 const double rightValue = 1.0;
213
214 while(!leftQueue.empty())
215 {
216 vtkIdType point0Id = leftQueue.front();
217 leftQueue.pop();
218 visitedArray->SetValue(point0Id,1);
219 input->GetPointCells(point0Id,ncells,cells);
220 for (vtkIdType i=0; i<ncells; i++)
221 {
222 vtkIdType cellId = cells[i];
223 input->GetCellPoints(cellId,npts,pts);
224 for (vtkIdType j=0; j<npts; j++)
225 {
226 vtkIdType point1Id = pts[j];
227 if (visitedArray->GetValue(point1Id) == 1)
228 {
229 continue;
230 }
231 visitedArray->SetValue(point1Id,1);
232 seamArray->SetValue(point1Id,leftValue);
233 leftQueue.push(point1Id);
234 }
235 }
236 }
237
238 while(!rightQueue.empty())
239 {
240 vtkIdType point0Id = rightQueue.front();
241 rightQueue.pop();
242 visitedArray->SetValue(point0Id,1);
243 input->GetPointCells(point0Id,ncells,cells);
244 for (vtkIdType i=0; i<ncells; i++)
245 {
246 vtkIdType cellId = cells[i];
247 input->GetCellPoints(cellId,npts,pts);
248 for (vtkIdType j=0; j<npts; j++)
249 {
250 vtkIdType point1Id = pts[j];
251 if (visitedArray->GetValue(point1Id) == 1)
252 {
253 continue;
254 }
255 visitedArray->SetValue(point1Id,1);
256 seamArray->SetValue(point1Id,rightValue);
257 rightQueue.push(point1Id);
258 }
259 }
260 }
261
262 output->DeepCopy(input);
263 output->GetPointData()->AddArray(seamArray);
264 output->GetPointData()->SetActiveScalars(this->SeamScalarsArrayName);
265
266 visitedArray->Delete();
267 sourceArray->Delete();
268 seamArray->Delete();
269
270 return 1;
271}
272
273void vtkvmtkTopologicalSeamFilter::PrintSelf(std::ostream& os, vtkIndent indent)
274{
275 this->Superclass::PrintSelf(os,indent);
276}
Create a feature edge on a surface from a point and implicit plane function.
void PrintSelf(std::ostream &os, vtkIndent indent) override
virtual int RequestData(vtkInformation *, vtkInformationVector **, vtkInformationVector *) override
#define VTK_VMTK_LARGE_DOUBLE
vtkStandardNewMacro(vtkvmtkTopologicalSeamFilter)