forked from ANTsX/ANTs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
itkManifoldIntegrationAlgorithm.h
421 lines (347 loc) · 11.2 KB
/
itkManifoldIntegrationAlgorithm.h
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
/*=========================================================================
Program: Insight Segmentation & Registration Toolkit (ITK)
Copyright (c) 2001 Insight Consortium
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
* The name of the Insight Consortium, nor the names of any consortium members,
nor of any contributors, may be used to endorse or promote products derived
from this software without specific prior written permission.
* Modified source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS ``AS IS''
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
=========================================================================*/
#ifndef _itkManifoldIntegrationAlgorithm_h_
#define _itkManifoldIntegrationAlgorithm_h_
namespace itk
{
/**
* \class ManifoldIntegrationAlgorithm
* \brief General shortest path / greedy dynamic programming solver.
*/
template <typename TGraphSearchNode>
class ManifoldIntegrationAlgorithm : public itk::LightObject
{
public:
typedef ManifoldIntegrationAlgorithm Self;
typedef LightObject Superclass;
typedef SmartPointer<Self> Pointer;
typedef SmartPointer<const Self> ConstPointer;
itkTypeMacro(ManifoldIntegrationAlgorithm, LightObject);
itkNewMacro(Self);
// Computation Data
typedef TGraphSearchNode SearchNode; /** dimension of the graph */
typedef typename SearchNode::Pointer SearchNodePointer;
enum
{
GraphDimension = SearchNode::GraphDimension
}; /** dimension of the graph */
typedef typename SearchNode::PixelType PixelType; /** pixel type for the cost */
typedef typename SearchNode::CoordRep CoordRep; /** coordinate type */
typedef typename DijkstrasAlgorithmQueue<TGraphSearchNode>::Pointer QType;
typedef typename DijkstrasAlgorithmQueue<TGraphSearchNode>::NodeListType NodeListType;
typedef typename TGraphSearchNode::NodeLocationType NodeLocationType;
typedef vtkPolyData TriangulationType;
typedef vtkPolyData * TriangulationTypePointer;
typedef vector<SearchNodePointer> GraphType;
// FUNCTIONS
// estimate the metric tensor of the surface and also the (conjugate harmonic) function dstarU
void
GetSearchBoundary();
float
dstarUestimate(SearchNodePointer G);
void
InitializeGraph3();
void
InitializeGraph(); /** initializes all graph values appropriately */
void
InitializeGraph2(); /** initializes all graph values appropriately */
void
InitializeQueue(); /** initializes all queue values appropriately
call AFTER source and sink are set*/
inline void
EmptyQ()
{
m_QS->EmptyQ();
m_QS->EmptyPath();
}
/* adds a source to the source set */
void
SetSource(typename TGraphSearchNode::Pointer G)
{
G->SetTotalCost(0.0);
m_QS->m_SourceNodes.push_back(G);
};
// adds a sink to the sink set
void
SetSink(typename TGraphSearchNode::Pointer G)
{
m_QS->m_SinkNodes.push_back(G);
}
// Backtracks from the given node to its source node;
float
BackTrack(typename TGraphSearchNode::Pointer SinkNode)
{
m_QS->m_Path.clear();
typename TGraphSearchNode::Pointer G = SinkNode;
typename TGraphSearchNode::Pointer P = SinkNode->GetPredecessor();
if (!P)
{
std::cout << " ERROR NO PRED TO SINK " << std::endl;
return 1.;
}
m_QS->m_Path.push_back(G);
// if (P->GetAncestor() && G)
// {
// if (P->GetAncestor()->GetTotalCost() < G->GetTotalCost() ) P->SetAncestor(G);
// }
// else if (G)
P->SetAncestor(G);
// if (P->GetValue(1) < G->GetValue(1) ) P->SetValue(G->GetValue(1),1);
while (P && G != P)
{
// std::cout << " Backtrack " << G->GetValue(0) << std::endl;
G = P;
P = G->GetPredecessor();
// if (P->GetValue(1) < G->GetValue(1) ) P->SetValue(G->GetValue(1),1);
P->SetAncestor(G);
if (P)
{
m_QS->m_Path.push_back(P);
}
}
// m_QS->m_Path.push_back(P);
// std::cout << " final cost " << P->GetTotalCost() << " high " << highcost << std::endl;
if (!P)
{
cout << " null pred "; // else cout << " pred == self \n";
}
return P->GetValue(2);
}
// Backtracks from the given node to its source node performing integration
void
IntegrateBackward(typename TGraphSearchNode::Pointer SinkNode)
{
typename TGraphSearchNode::Pointer G = SinkNode;
typename TGraphSearchNode::Pointer P = SinkNode->GetPredecessor();
float intval = 0.0;
G->SetTotalCost(intval);
while (P && G != P)
{
// NodeLocationType dif=P->GetLocation()-G->GetLocation();
float dU = (P->GetValue() - G->GetValue());
intval += dU;
P->SetTotalCost(intval);
G = P;
P = G->GetPredecessor();
}
// std::cout << " intval " << intval << " at " << G->GetLocation() << std::endl;
if (!P)
{
cout << " null pred "; // else cout << " pred == self \n";
}
return;
}
// Backtracks from the given node to its source node performing integration
void
IntegrateForward(typename TGraphSearchNode::Pointer SinkNode)
{
typename TGraphSearchNode::Pointer G = SinkNode;
typename TGraphSearchNode::Pointer P = SinkNode->GetAncestor();
float intval = 0.0;
G->SetTotalCost(intval);
while (P && G != P)
{
// NodeLocationType dif=P->GetLocation()-G->GetLocation();
float dU = (P->GetValue() - G->GetValue());
intval += dU;
P->SetTotalCost(intval);
G = P;
P = G->GetAncestor();
}
// std::cout << " intval " << intval << " at " << G->GetLocation() << std::endl;
if (!P)
{
cout << " null pred "; // else cout << " pred == self \n";
}
return;
}
// Inverse of backtrack - from the given node to its sink node;
float
ForwardTrack(typename TGraphSearchNode::Pointer SinkNode)
{
typename TGraphSearchNode::Pointer G = SinkNode;
typename TGraphSearchNode::Pointer P = SinkNode->GetAncestor();
if (!P)
{
return G->GetValue(2);
}
// float highcost=G->GetTotalCost();
while (P && G != P && G)
{
G = P;
P = G->GetAncestor();
if (!P)
{
return G->GetValue(2);
}
// if (P->GetTotalCost() > highcost) highcost=P->GetTotalCost();
}
// SinkNode->SetValue(highcost);
// if (!P) cout << " null pred "; //else cout << " pred == self \n";
return P->GetValue(2);
}
bool ParameterizeBoundary(SearchNodePointer);
bool
TerminationCondition(); /** decides when the algorithm stops */
virtual void
SearchEdgeSet(); /** loops over the neighbors in the graph */
void
CheckNodeStatus(); /** checks if the node has been explored already, its cost, etc. */
virtual PixelType
MyLocalCost(); /* computes the local cost */
/* alternatively, we could pass the function as a template parameter
or set a function pointer. the latter method is used in dijkstrasegment. */
virtual void
FindPath(); /* runs the algorithm */
inline unsigned int
GetPathSize()
{
return m_QS->m_Path.size();
}
inline void
EmptyPath()
{
m_QS->m_Path.clear();
}
inline typename TGraphSearchNode::Pointer
GetPathAtIndex(unsigned int i)
{
return m_QS->m_Path[i];
}
inline typename TGraphSearchNode::Pointer
GetNeighborNode()
{
return m_NeighborNode;
}
inline typename TGraphSearchNode::Pointer
GetCurrentNode()
{
return m_CurrentNode;
}
PixelType
GetMaxCost()
{
return this->m_MaxCost;
}
void
SetMaxCost(PixelType m)
{
this->m_MaxCost = m;
}
void
ResetMaxCost()
{
this->m_MaxCost = vnl_huge_val(this->m_MaxCost);
}
inline void
SetDistanceCostWeight(float d)
{
this->m_DistanceCostWeight = d;
}
inline void
SetLabelCostWeight(float d)
{
this->m_LabelCostWeight = d;
}
inline void
SetWeights(float a, float b, float c)
{
this->m_MaxCost = a;
this->m_DistanceCostWeight = b;
this->m_LabelCostWeight = c;
}
inline void
PrintWeights()
{
std::cout << this->m_MaxCost << " " << this->m_DistanceCostWeight << " " << this->m_LabelCostWeight << std::endl;
}
void
SetSearchFinished(bool m)
{
m_SearchFinished = m;
}
/** sets the boolean that indicates if the algorithm is done */
void
SetSurfaceMesh(TriangulationTypePointer mesh)
{
m_SurfaceMesh = mesh;
}
TriangulationTypePointer
GetSurfaceMesh()
{
return m_SurfaceMesh;
}
SearchNodePointer
GetGraphNode(int i)
{
return m_GraphX[i];
}
int
GetGraphSize()
{
return m_GraphX.size();
}
// sanity check to see if mesh to graph conversion is ok
// see if genus is the same
void
ConvertGraphBackToMesh();
void
SetParamWhileSearching(bool b)
{
this->m_ParamWhileSearching = b;
}
std::vector<SearchNodePointer> m_BoundaryList;
protected:
QType m_QS;
vector<unsigned int> m_EdgeTemplate; /** defines neighborhood connectivity */
typename TGraphSearchNode::Pointer m_PredecessorNode; /** holds the predecessor node */
typename TGraphSearchNode::Pointer m_CurrentNode; /** holds the current node */
typename TGraphSearchNode::Pointer m_NeighborNode; /** holds the current neighbor node */
bool m_PureDist;
bool m_SearchFinished;
PixelType m_NewCost;
PixelType m_CurrentCost;
float m_MaxCost; // This creates an insurmountable barrier unless all costs are max
float m_DistanceCostWeight;
float m_LabelCostWeight;
GraphType m_GraphX;
unsigned long m_NumberSearched;
TriangulationTypePointer m_SurfaceMesh;
bool m_ParamWhileSearching;
ManifoldIntegrationAlgorithm();
~ManifoldIntegrationAlgorithm(){};
private:
ManifoldIntegrationAlgorithm(const Self &); // purposely not implemented
void
operator=(const Self &); // purposely not implemented
};
} // end namespace itk
#ifndef ITK_MANUAL_INSTANTIATION
# include "itkManifoldIntegrationAlgorithm.cxx"
#endif
#endif