/usr/include/InsightToolkit/Common/itkLevelSetFunction.txx is in libinsighttoolkit3-dev 3.20.1-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 | /*=========================================================================
Program: Insight Segmentation & Registration Toolkit
Module: itkLevelSetFunction.txx
Language: C++
Date: $Date$
Version: $Revision$
Copyright (c) Insight Software Consortium. All rights reserved.
See ITKCopyright.txt or http://www.itk.org/HTML/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 notices for more information.
=========================================================================*/
#ifndef __itkLevelSetFunction_txx
#define __itkLevelSetFunction_txx
#include "itkLevelSetFunction.h"
#include "vnl/algo/vnl_symmetric_eigensystem.h"
namespace itk {
template <class TImageType>
typename LevelSetFunction<TImageType>::ScalarValueType
LevelSetFunction<TImageType>::ComputeCurvatureTerm(const NeighborhoodType &neighborhood,
const FloatOffsetType &offset, GlobalDataStruct *gd)
{
if ( m_UseMinimalCurvature == false )
{
return this->ComputeMeanCurvature(neighborhood, offset, gd);
}
else
{
if (ImageDimension == 3)
{
return this->ComputeMinimalCurvature(neighborhood, offset, gd);
}
else if (ImageDimension == 2)
{
return this->ComputeMeanCurvature(neighborhood, offset, gd);
}
else
{
return this->ComputeMinimalCurvature(neighborhood, offset, gd);
}
}
}
template< class TImageType>
typename LevelSetFunction< TImageType >::ScalarValueType
LevelSetFunction< TImageType >
::ComputeMinimalCurvature(
const NeighborhoodType &itkNotUsed(neighborhood),
const FloatOffsetType& itkNotUsed(offset), GlobalDataStruct *gd)
{
unsigned int i, j, n;
ScalarValueType gradMag = vcl_sqrt(gd->m_GradMagSqr);
ScalarValueType Pgrad[ImageDimension][ImageDimension];
ScalarValueType tmp_matrix[ImageDimension][ImageDimension];
const ScalarValueType ZERO = NumericTraits<ScalarValueType>::Zero;
vnl_matrix_fixed<ScalarValueType, ImageDimension, ImageDimension> Curve;
const ScalarValueType MIN_EIG = NumericTraits<ScalarValueType>::min();
ScalarValueType mincurve;
for (i = 0; i < ImageDimension; i++)
{
Pgrad[i][i] = 1.0 - gd->m_dx[i] * gd->m_dx[i]/gradMag;
for (j = i+1; j < ImageDimension; j++)
{
Pgrad[i][j]= gd->m_dx[i] * gd->m_dx[j]/gradMag;
Pgrad[j][i] = Pgrad[i][j];
}
}
//Compute Pgrad * Hessian * Pgrad
for (i = 0; i < ImageDimension; i++)
{
for (j = i; j < ImageDimension; j++)
{
tmp_matrix[i][j]= ZERO;
for (n = 0; n < ImageDimension; n++)
{
tmp_matrix[i][j] += Pgrad[i][n] * gd->m_dxy[n][j];
}
tmp_matrix[j][i]=tmp_matrix[i][j];
}
}
for (i = 0; i < ImageDimension; i++)
{
for (j = i; j < ImageDimension; j++)
{
Curve(i,j) = ZERO;
for (n = 0; n < ImageDimension; n++)
{
Curve(i,j) += tmp_matrix[i][n] * Pgrad[n][j];
}
Curve(j,i) = Curve(i,j);
}
}
//Eigensystem
vnl_symmetric_eigensystem<ScalarValueType> eig(Curve);
mincurve=vnl_math_abs(eig.get_eigenvalue(ImageDimension-1));
for (i = 0; i < ImageDimension; i++)
{
if(vnl_math_abs(eig.get_eigenvalue(i)) < mincurve &&
vnl_math_abs(eig.get_eigenvalue(i)) > MIN_EIG)
{
mincurve = vnl_math_abs(eig.get_eigenvalue(i));
}
}
return ( mincurve / gradMag );
}
template< class TImageType>
typename LevelSetFunction< TImageType >::ScalarValueType
LevelSetFunction< TImageType >
::Compute3DMinimalCurvature(const NeighborhoodType &neighborhood,
const FloatOffsetType& offset, GlobalDataStruct *gd)
{
ScalarValueType mean_curve = this->ComputeMeanCurvature(neighborhood, offset, gd);
int i0 = 0, i1 = 1, i2 = 2;
ScalarValueType gauss_curve =
(2*(gd->m_dx[i0]*gd->m_dx[i1]*(gd->m_dxy[i2][i0]
*gd->m_dxy[i1][i2]-gd->m_dxy[i0][i1]*gd->m_dxy[i2][i2]) +
gd->m_dx[i1]*gd->m_dx[i2]*(gd->m_dxy[i2][i0]
*gd->m_dxy[i0][i1]-gd->m_dxy[i1][i2]*gd->m_dxy[i0][i0]) +
gd->m_dx[i0]*gd->m_dx[i2]*(gd->m_dxy[i1][i2]
*gd->m_dxy[i0][i1]-gd->m_dxy[i2][i0]*gd->m_dxy[i1][i1])) +
gd->m_dx[i0]*gd->m_dx[i0]*(gd->m_dxy[i1][i1]
*gd->m_dxy[i2][i2]-gd->m_dxy[i1][i2]*gd->m_dxy[i1][i2]) +
gd->m_dx[i1]*gd->m_dx[i1]*(gd->m_dxy[i0][i0]
*gd->m_dxy[i2][i2]-gd->m_dxy[i2][i0]*gd->m_dxy[i2][i0]) +
gd->m_dx[i2]*gd->m_dx[i2]*(gd->m_dxy[i1][i1]
*gd->m_dxy[i0][i0]-gd->m_dxy[i0][i1]*gd->m_dxy[i0][i1]))/
(gd->m_dx[i0]*gd->m_dx[i0] + gd->m_dx[i1]*gd->m_dx[i1] + gd->m_dx[i2]*gd->m_dx[i2]);
ScalarValueType discriminant = mean_curve * mean_curve-gauss_curve;
if (discriminant < 0.0)
{
discriminant = 0.0;
}
discriminant = vcl_sqrt(discriminant);
return (mean_curve - discriminant);
}
template <class TImageType>
typename LevelSetFunction<TImageType>::ScalarValueType
LevelSetFunction<TImageType>::ComputeMeanCurvature(
const NeighborhoodType &itkNotUsed(neighborhood),
const FloatOffsetType &itkNotUsed(offset), GlobalDataStruct *gd)
{
// Calculate the mean curvature
ScalarValueType curvature_term = NumericTraits<ScalarValueType>::Zero;
unsigned int i, j;
for (i = 0; i < ImageDimension; i++)
{
for(j = 0; j < ImageDimension; j++)
{
if(j != i)
{
curvature_term -= gd->m_dx[i] * gd->m_dx[j] * gd->m_dxy[i][j];
curvature_term += gd->m_dxy[j][j] * gd->m_dx[i] * gd->m_dx[i];
}
}
}
return (curvature_term / gd->m_GradMagSqr );
}
template <class TImageType>
typename LevelSetFunction<TImageType>::VectorType
LevelSetFunction<TImageType>::InitializeZeroVectorConstant()
{
VectorType ans;
for (unsigned int i = 0; i < ImageDimension; ++i)
{
ans[i] = NumericTraits<ScalarValueType>::Zero;
}
return ans;
}
template <class TImageType>
typename LevelSetFunction<TImageType>::VectorType
LevelSetFunction<TImageType>::m_ZeroVectorConstant =
LevelSetFunction<TImageType>::InitializeZeroVectorConstant();
template <class TImageType>
void
LevelSetFunction<TImageType>::
PrintSelf(std::ostream& os, Indent indent) const
{
Superclass::PrintSelf(os, indent);
os << indent << "WaveDT: " << m_WaveDT << std::endl;
os << indent << "DT: " << m_DT << std::endl;
os << indent << "UseMinimalCurvature " << m_UseMinimalCurvature << std::endl;
os << indent << "EpsilonMagnitude: " << m_EpsilonMagnitude << std::endl;
os << indent << "AdvectionWeight: " << m_AdvectionWeight << std::endl;
os << indent << "PropagationWeight: " << m_PropagationWeight << std::endl;
os << indent << "CurvatureWeight: " << m_CurvatureWeight << std::endl;
os << indent << "LaplacianSmoothingWeight: " << m_LaplacianSmoothingWeight << std::endl;
}
template< class TImageType >
double LevelSetFunction<TImageType>::m_WaveDT = 1.0/(2.0 * ImageDimension);
template < class TImageType >
double LevelSetFunction<TImageType>::m_DT = 1.0/(2.0 * ImageDimension);
template< class TImageType >
typename LevelSetFunction< TImageType >::TimeStepType
LevelSetFunction<TImageType>
::ComputeGlobalTimeStep(void *GlobalData) const
{
TimeStepType dt;
GlobalDataStruct *d = (GlobalDataStruct *)GlobalData;
d->m_MaxAdvectionChange += d->m_MaxPropagationChange;
if (vnl_math_abs(d->m_MaxCurvatureChange) > 0.0)
{
if (d->m_MaxAdvectionChange > 0.0)
{
dt = vnl_math_min((m_WaveDT / d->m_MaxAdvectionChange),
( m_DT / d->m_MaxCurvatureChange ));
}
else
{
dt = m_DT / d->m_MaxCurvatureChange;
}
}
else
{
if (d->m_MaxAdvectionChange > 0.0)
{
dt = m_WaveDT / d->m_MaxAdvectionChange;
}
else
{
dt = 0.0;
}
}
double maxScaleCoefficient = 0.0;
for (unsigned int i=0; i<ImageDimension; i++)
{
maxScaleCoefficient = vnl_math_max(this->m_ScaleCoefficients[i],maxScaleCoefficient);
}
dt /= maxScaleCoefficient;
// reset the values
d->m_MaxAdvectionChange = NumericTraits<ScalarValueType>::Zero;
d->m_MaxPropagationChange = NumericTraits<ScalarValueType>::Zero;
d->m_MaxCurvatureChange = NumericTraits<ScalarValueType>::Zero;
return dt;
}
template< class TImageType >
void
LevelSetFunction< TImageType>
::Initialize(const RadiusType &r)
{
this->SetRadius(r);
// Dummy neighborhood.
NeighborhoodType it;
it.SetRadius( r );
// Find the center index of the neighborhood.
m_Center = it.Size() / 2;
// Get the stride length for each axis.
for(unsigned int i = 0; i < ImageDimension; i++)
{ m_xStride[i] = it.GetStride(i); }
}
template< class TImageType >
typename LevelSetFunction< TImageType >::PixelType
LevelSetFunction< TImageType >
::ComputeUpdate(const NeighborhoodType &it, void *globalData,
const FloatOffsetType& offset)
{
unsigned int i, j;
const ScalarValueType ZERO = NumericTraits<ScalarValueType>::Zero;
const ScalarValueType center_value = it.GetCenterPixel();
const NeighborhoodScalesType neighborhoodScales = this->ComputeNeighborhoodScales();
ScalarValueType laplacian, x_energy, laplacian_term, propagation_term,
curvature_term, advection_term, propagation_gradient;
VectorType advection_field;
// Global data structure
GlobalDataStruct *gd = (GlobalDataStruct *)globalData;
// Compute the Hessian matrix and various other derivatives. Some of these
// derivatives may be used by overloaded virtual functions.
gd->m_GradMagSqr = 1.0e-6;
for( i = 0; i < ImageDimension; i++)
{
const unsigned int positionA =
static_cast<unsigned int>( m_Center + m_xStride[i]);
const unsigned int positionB =
static_cast<unsigned int>( m_Center - m_xStride[i]);
gd->m_dx[i] = 0.5 * (it.GetPixel( positionA ) -
it.GetPixel( positionB ) ) * neighborhoodScales[i];
gd->m_dxy[i][i] = ( it.GetPixel( positionA )
+ it.GetPixel( positionB ) - 2.0 * center_value ) *
vnl_math_sqr(neighborhoodScales[i]);
gd->m_dx_forward[i] = ( it.GetPixel( positionA ) - center_value ) * neighborhoodScales[i];
gd->m_dx_backward[i] = ( center_value - it.GetPixel( positionB ) ) * neighborhoodScales[i];
gd->m_GradMagSqr += gd->m_dx[i] * gd->m_dx[i];
for( j = i+1; j < ImageDimension; j++ )
{
const unsigned int positionAa = static_cast<unsigned int>(
m_Center - m_xStride[i] - m_xStride[j] );
const unsigned int positionBa = static_cast<unsigned int>(
m_Center - m_xStride[i] + m_xStride[j] );
const unsigned int positionCa = static_cast<unsigned int>(
m_Center + m_xStride[i] - m_xStride[j] );
const unsigned int positionDa = static_cast<unsigned int>(
m_Center + m_xStride[i] + m_xStride[j] );
gd->m_dxy[i][j] = gd->m_dxy[j][i] = 0.25 * ( it.GetPixel( positionAa )
- it.GetPixel( positionBa )
- it.GetPixel( positionCa )
+ it.GetPixel( positionDa ) )
* neighborhoodScales[i] * neighborhoodScales[j];
}
}
if ( m_CurvatureWeight != ZERO )
{
curvature_term = this->ComputeCurvatureTerm(it, offset, gd) * m_CurvatureWeight
* this->CurvatureSpeed(it, offset);
gd->m_MaxCurvatureChange = vnl_math_max(gd->m_MaxCurvatureChange,
vnl_math_abs(curvature_term));
}
else
{
curvature_term = ZERO;
}
// Calculate the advection term.
// $\alpha \stackrel{\rightharpoonup}{F}(\mathbf{x})\cdot\nabla\phi $
//
// Here we can use a simple upwinding scheme since we know the
// sign of each directional component of the advective force.
//
if (m_AdvectionWeight != ZERO)
{
advection_field = this->AdvectionField(it, offset, gd);
advection_term = ZERO;
for(i = 0; i < ImageDimension; i++)
{
x_energy = m_AdvectionWeight * advection_field[i];
if (x_energy > ZERO)
{
advection_term += advection_field[i] * gd->m_dx_backward[i];
}
else
{
advection_term += advection_field[i] * gd->m_dx_forward[i];
}
gd->m_MaxAdvectionChange
= vnl_math_max(gd->m_MaxAdvectionChange, vnl_math_abs(x_energy));
}
advection_term *= m_AdvectionWeight;
}
else
{
advection_term = ZERO;
}
if (m_PropagationWeight != ZERO)
{
// Get the propagation speed
propagation_term = m_PropagationWeight * this->PropagationSpeed(it, offset, gd);
//
// Construct upwind gradient values for use in the propagation speed term:
// $\beta G(\mathbf{x})\mid\nabla\phi\mid$
//
// The following scheme for ``upwinding'' in the normal direction is taken
// from Sethian, Ch. 6 as referenced above.
//
propagation_gradient = ZERO;
if ( propagation_term > ZERO )
{
for(i = 0; i< ImageDimension; i++)
{
propagation_gradient += vnl_math_sqr( vnl_math_max(gd->m_dx_backward[i], ZERO) )
+ vnl_math_sqr( vnl_math_min(gd->m_dx_forward[i], ZERO) );
}
}
else
{
for(i = 0; i< ImageDimension; i++)
{
propagation_gradient += vnl_math_sqr( vnl_math_min(gd->m_dx_backward[i], ZERO) )
+ vnl_math_sqr( vnl_math_max(gd->m_dx_forward[i], ZERO) );
}
}
// Collect energy change from propagation term. This will be used in
// calculating the maximum time step that can be taken for this iteration.
gd->m_MaxPropagationChange =
vnl_math_max(gd->m_MaxPropagationChange,
vnl_math_abs(propagation_term));
propagation_term *= vcl_sqrt( propagation_gradient );
}
else propagation_term = ZERO;
if(m_LaplacianSmoothingWeight != ZERO)
{
laplacian = ZERO;
// Compute the laplacian using the existing second derivative values
for(i = 0;i < ImageDimension; i++)
{
laplacian += gd->m_dxy[i][i];
}
// Scale the laplacian by its speed and weight
laplacian_term =
laplacian * m_LaplacianSmoothingWeight * LaplacianSmoothingSpeed(it,offset, gd);
}
else
{
laplacian_term = ZERO;
}
// Return the combination of all the terms.
return ( PixelType ) ( curvature_term - propagation_term
- advection_term - laplacian_term );
}
} // end namespace itk
#endif
|