File: itkNormalVectorDiffusionFunction.txx

package info (click to toggle)
insighttoolkit 3.6.0-3
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 94,956 kB
  • ctags: 74,981
  • sloc: cpp: 355,621; ansic: 195,070; fortran: 28,713; python: 3,802; tcl: 1,996; sh: 1,175; java: 583; makefile: 415; csh: 184; perl: 175
file content (211 lines) | stat: -rw-r--r-- 6,685 bytes parent folder | download
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
/*=========================================================================

  Program:   Insight Segmentation & Registration Toolkit
  Module:    $RCSfile: itkNormalVectorDiffusionFunction.txx,v $
  Language:  C++
  Date:      $Date: 2008-03-03 13:58:45 $
  Version:   $Revision: 1.8 $

  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 __itkNormalVectorDiffusionFunction_txx_
#define __itkNormalVectorDiffusionFunction_txx_

#include "itkNormalVectorDiffusionFunction.h"
#include "itkNumericTraits.h"
#include "itkVector.h"

namespace itk {

template <class TSparseImageType>
NormalVectorDiffusionFunction <TSparseImageType>
::NormalVectorDiffusionFunction()
{
  // check: should some of this be in Initialize?
  RadiusType r;
  for( unsigned int j = 0; j < ImageDimension; j++ )
    {
    r[j] = 1;
    } 
  
  this->SetRadius(r);
  this->SetTimeStep(static_cast<TimeStepType> (0.5/ImageDimension));
  m_NormalProcessType = 0;
  m_ConductanceParameter = NumericTraits<NodeValueType>::Zero;
  m_FluxStopConstant = NumericTraits<NodeValueType>::Zero;
}

template <class TSparseImageType>
void
NormalVectorDiffusionFunction <TSparseImageType>
::PrintSelf(std::ostream& os, Indent indent) const
{
  Superclass::PrintSelf(os, indent);
  os << indent << "NormalProcessType: " << m_NormalProcessType << std::endl;
  os << indent << "ConductanceParameter: "<< m_ConductanceParameter << std::endl;
  os << indent << "FluxStopConstant: "<< m_FluxStopConstant << std::endl;
}

template <class TSparseImageType>
void
NormalVectorDiffusionFunction <TSparseImageType>
::PrecomputeSparseUpdate( NeighborhoodType &it ) const 
{
  unsigned int i, j, k;
  NodeValueType DotProduct;
  
  NodeType* CenterNode = it.GetCenterPixel();
  const NormalVectorType CenterPixel = CenterNode->m_Data;

  NodeType *PreviousNode, *OtherNode;
  NormalVectorType PreviousPixel;
  Vector < NodeValueType, ImageDimension > gradient [ImageDimension];
  NormalVectorType PositiveSidePixel[2], NegativeSidePixel[2], flux;
  unsigned long stride [ImageDimension];
  unsigned long center;

  const NeighborhoodScalesType neighborhoodScales = this->ComputeNeighborhoodScales();

  for( j = 0; j < ImageDimension; j++ )
    {
    stride[j] = it.GetStride( (unsigned long) j);
    }
   center =  it.Size() / 2;

  for (i=0;i<ImageDimension;i++) // flux offset axis
    {
    PreviousNode = it.GetPrevious (i);
    if (PreviousNode == 0) 
      {
      for( j = 0; j < ImageDimension; j++ )
        {
        CenterNode->m_Flux[i][j] = NumericTraits<NodeValueType>::Zero;
        }
      }
    else 
      {
      PreviousPixel = PreviousNode->m_Data;
      for (j=0;j<ImageDimension;j++) // derivative axis
        {
        if (i!=j) // compute derivative on a plane
          {
          // compute differences (j-axis) in line with center pixel
          OtherNode = it.GetPrevious (j);
          if (OtherNode == 0)
            {
            NegativeSidePixel[0] = CenterPixel;
            }
          else 
            {
            NegativeSidePixel[0] = OtherNode->m_Data;
            }
          OtherNode = it.GetNext (j);
          if (OtherNode == 0)
            {
            PositiveSidePixel[0] = CenterPixel;
            }
          else 
            {
            PositiveSidePixel[0] = OtherNode->m_Data;
            }

          // compute derivative (j-axis) offset from center pixel on i-axis
          OtherNode = it.GetPixel (center - stride[i] - stride[j]);
          if (OtherNode == 0)
            {
            NegativeSidePixel[1] = PreviousPixel;
            }
          else 
            {
            NegativeSidePixel[1] = OtherNode->m_Data;
            }
          OtherNode = it.GetPixel (center - stride[i] + stride[j]);
          if (OtherNode == 0)
            {
            PositiveSidePixel[1] = PreviousPixel;
            }
          else 
            {
            PositiveSidePixel[1] = OtherNode->m_Data;
            }
          
          gradient[j] = ( ( PositiveSidePixel[0]+PositiveSidePixel[1] )- 
                          ( NegativeSidePixel[0]+NegativeSidePixel[1] ) )*
            static_cast<NodeValueType>(0.25) * neighborhoodScales[j];
          }
        else // compute derivative on a line
          {
          gradient[i] = ( CenterPixel-PreviousPixel ) * neighborhoodScales[i]; 
          }
        } // end derivative axis

      // now compute the intrinsic derivative
      for (j = 0; j < ImageDimension; j++) // component axis
        {
        DotProduct = NumericTraits<NodeValueType>::Zero;
        for (k = 0; k < ImageDimension; k++) // derivative axis
          {
          DotProduct += (gradient[k][j]*CenterNode->m_ManifoldNormal[i][k]);
          }
        flux[j] = gradient[i][j]-CenterNode->m_ManifoldNormal[i][i]*DotProduct;
        }
      // do following line for non-intrinsic derivative
      //flux = gradient[i];
      if (m_NormalProcessType == 1)
        {
        // anisotropic diffusion
        CenterNode->m_Flux[i] =
          flux * this->FluxStopFunction(flux.GetSquaredNorm());
        }
      else
        {
        // isotropic diffusion
        CenterNode->m_Flux[i] = flux;
        }
      } // end if-else PreviousNode==0
    } // end flux offset axis
}

template <class TSparseImageType>
typename NormalVectorDiffusionFunction <TSparseImageType>::NormalVectorType
NormalVectorDiffusionFunction <TSparseImageType>
::ComputeSparseUpdate( NeighborhoodType &it,
                       void*, const FloatOffsetType& ) const
{
  unsigned int i;
  NormalVectorType change;
  NodeValueType DotProduct;
  const NodeType* CenterNode = it.GetCenterPixel();
  const NormalVectorType CenterPixel = CenterNode->m_Data;
  NodeType* NextNode;

  const NeighborhoodScalesType neighborhoodScales = this->ComputeNeighborhoodScales();
  
  change = NumericTraits<NormalVectorType>::Zero;
  for (i=0;i<ImageDimension;i++) // flux offset axis
    {
    NextNode = it.GetNext (i);
    if (NextNode == 0)
      {
      change -= CenterNode->m_Flux[i] * neighborhoodScales[i];
      }
    else
      {
      change += ( NextNode->m_Flux[i] - CenterNode->m_Flux[i]) * neighborhoodScales[i];
      }
    } // end flux offset axis
  DotProduct = change*CenterPixel;
  change -= CenterPixel*DotProduct;

  return change;
}

} // end namespace itk

#endif