-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathvtkSlicerDijkstraGraphGeodesicPath.cxx
191 lines (167 loc) · 5.95 KB
/
vtkSlicerDijkstraGraphGeodesicPath.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
/*==============================================================================
Copyright (c) Laboratory for Percutaneous Surgery (PerkLab)
Queen's University, Kingston, ON, Canada. All Rights Reserved.
See COPYRIGHT.txt
or http://www.slicer.org/copyright/copyright.txt for details.
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
This file was originally developed by Kyle Sunderland, PerkLab, Queen's University
and was supported through CANARIE's Research Software Program, Cancer
Care Ontario, OpenAnatomy, and Brigham and Women's Hospital through NIH grant R01MH112748.
==============================================================================*/
// Markups MRML includes
#include "vtkSlicerDijkstraGraphGeodesicPath.h"
// VTK includes
#include <vtkFloatArray.h>
#include <vtkPointData.h>
#include <vtkInformation.h>
#include <vtkInformationVector.h>
//------------------------------------------------------------------------------
vtkStandardNewMacro(vtkSlicerDijkstraGraphGeodesicPath);
//------------------------------------------------------------------------------
vtkSlicerDijkstraGraphGeodesicPath::vtkSlicerDijkstraGraphGeodesicPath()
{
this->UseScalarWeights = true;
this->PreviousUseScalarWeights = this->UseScalarWeights;
this->CostFunctionType = COST_FUNCTION_TYPE_DISTANCE;
this->PreviousCostFunctionType = this->CostFunctionType;
}
//------------------------------------------------------------------------------
vtkSlicerDijkstraGraphGeodesicPath::~vtkSlicerDijkstraGraphGeodesicPath() = default;
//------------------------------------------------------------------------------
void vtkSlicerDijkstraGraphGeodesicPath::PrintSelf(std::ostream &os, vtkIndent indent)
{
Superclass::PrintSelf(os, indent);
os << indent << "CostFunction: " << this->GetCostFunctionTypeAsString(this->CostFunctionType) << std::endl;
}
//------------------------------------------------------------------------------
const char* vtkSlicerDijkstraGraphGeodesicPath::GetCostFunctionTypeAsString(int costFunction)
{
switch (costFunction)
{
case COST_FUNCTION_TYPE_DISTANCE:
{
return "distance";
}
case COST_FUNCTION_TYPE_ADDITIVE:
{
return "additive";
}
case COST_FUNCTION_TYPE_MULTIPLICATIVE:
{
return "multiplicative";
}
case COST_FUNCTION_TYPE_INVERSE_SQUARED:
{
return "inverseSquared";
}
default:
{
return "";
}
}
}
//------------------------------------------------------------------------------
int vtkSlicerDijkstraGraphGeodesicPath::GetCostFunctionTypeFromString(const char* costFunctionType)
{
if (costFunctionType == nullptr)
{
// invalid name
vtkGenericWarningMacro("Invalid sorting method name");
return -1;
}
for (int i = 0; i < vtkSlicerDijkstraGraphGeodesicPath::COST_FUNCTION_TYPE_LAST; i++)
{
if (strcmp(costFunctionType, vtkSlicerDijkstraGraphGeodesicPath::GetCostFunctionTypeAsString(i)) == 0)
{
// found a matching name
return i;
}
}
// name not found
vtkGenericWarningMacro("Unknown cost function type: " << costFunctionType);
return -1;
}
//----------------------------------------------------------------------------
int vtkSlicerDijkstraGraphGeodesicPath::RequestData(
vtkInformation* vtkNotUsed(request),
vtkInformationVector** inputVector,
vtkInformationVector* outputVector)
{
/// Reimplemented to rebuild the adjacency info if either CostFunctionType or UseScalarWeights are changed.
vtkInformation* inInfo = inputVector[0]->GetInformationObject(0);
vtkInformation* outInfo = outputVector->GetInformationObject(0);
vtkPolyData* input = vtkPolyData::SafeDownCast(
inInfo->Get(vtkDataObject::DATA_OBJECT()));
if (!input)
{
return 0;
}
vtkPolyData* output = vtkPolyData::SafeDownCast(
outInfo->Get(vtkDataObject::DATA_OBJECT()));
if (!output)
{
return 0;
}
if (this->AdjacencyBuildTime.GetMTime() < input->GetMTime() ||
this->CostFunctionType != this->PreviousCostFunctionType ||
static_cast<bool>(this->UseScalarWeights) != this->PreviousUseScalarWeights)
{
this->Initialize(input);
}
else
{
this->Reset();
}
this->PreviousUseScalarWeights = this->UseScalarWeights;
this->PreviousCostFunctionType= this->CostFunctionType;
if (this->NumberOfVertices == 0)
{
return 0;
}
this->ShortestPath(input, this->StartVertex, this->EndVertex);
this->TraceShortestPath(input, output, this->StartVertex, this->EndVertex);
return 1;
}
//------------------------------------------------------------------------------
double vtkSlicerDijkstraGraphGeodesicPath::CalculateStaticEdgeCost(vtkDataSet* inData, vtkIdType u, vtkIdType v)
{
// Parent implementation is inverse squared
if (this->CostFunctionType == COST_FUNCTION_TYPE_INVERSE_SQUARED)
{
return Superclass::CalculateStaticEdgeCost(inData, u, v);
}
double p1[3];
inData->GetPoint(u,p1);
double p2[3];
inData->GetPoint(v,p2);
double distance = sqrt(vtkMath::Distance2BetweenPoints(p1, p2));
double cost = distance;
if (this->UseScalarWeights && this->CostFunctionType != COST_FUNCTION_TYPE_DISTANCE)
{
double scalarV = 0.0;
// Note this edge cost is not symmetric!
if (inData->GetPointData())
{
vtkFloatArray* scalars = vtkFloatArray::SafeDownCast(inData->GetPointData()->GetScalars());
if (scalars)
{
scalarV = static_cast<double>(scalars->GetValue(v));
}
}
switch (this->CostFunctionType)
{
case COST_FUNCTION_TYPE_ADDITIVE:
cost += scalarV;
break;
case COST_FUNCTION_TYPE_MULTIPLICATIVE:
cost *= scalarV;
default:
break;
}
}
return cost;
}