File: itkOrientedImage.h

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 (317 lines) | stat: -rw-r--r-- 10,857 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
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
/*=========================================================================

  Program:   Insight Segmentation & Registration Toolkit
  Module:    $RCSfile: itkOrientedImage.h,v $
  Language:  C++
  Date:      $Date: 2008-02-04 12:34:11 $
  Version:   $Revision: 1.21 $

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

#include "itkImage.h"
#include "itkImageTransformHelper.h"

namespace itk
{

/** \class OrientedImage
 *  \brief Templated n-dimensional oriented image class.
 *
 * \note
 * This work is part of the National Alliance for Medical Image Computing 
 * (NAMIC), funded by the National Institutes of Health through the NIH Roadmap
 * for Medical Research, Grant U54 EB005149.
 *
 * \ingroup ImageObjects */
template <class TPixel, unsigned int VImageDimension>
class ITK_EXPORT OrientedImage : public Image<TPixel, VImageDimension>
{
public:
  /** Standard class typedefs */
  typedef OrientedImage               Self;
  typedef Image<TPixel, VImageDimension>  Superclass;
  typedef SmartPointer<Self>  Pointer;
  typedef SmartPointer<const Self>  ConstPointer;
  typedef WeakPointer<const Self>  ConstWeakPointer;

  /** Method for creation through the object factory. */
  itkNewMacro(Self);

  /** Run-time type information (and related methods). */
  itkTypeMacro(OrientedImage, Image);

  /** Index typedef support. An index is used to access pixel values. */
  typedef typename Superclass::IndexType  IndexType;

  /** Direction typedef support. The direction cosines of the image. */
  typedef typename Superclass::DirectionType  DirectionType;

  /** Spacing typedef support.  Spacing holds the size of a pixel.  The
   * spacing is the geometric distance between image samples. */
  typedef typename Superclass::SpacingType SpacingType;

  typedef typename Superclass::AccessorType        AccessorType;
  typedef typename Superclass::AccessorFunctorType AccessorFunctorType;
  typedef typename Superclass::IOPixelType         IOPixelType;

  /** Tyepdef for the functor used to access a neighborhood of pixel pointers.*/
  typedef NeighborhoodAccessorFunctor< Self > 
                                            NeighborhoodAccessorFunctorType;

  /** Return the NeighborhoodAccessor functor. This method is called by the 
   * neighborhood iterators. */
  NeighborhoodAccessorFunctorType GetNeighborhoodAccessor() 
    { return NeighborhoodAccessorFunctorType(); }
  
  /** Return the NeighborhoodAccessor functor. This method is called by the 
   * neighborhood iterators. */
  const NeighborhoodAccessorFunctorType GetNeighborhoodAccessor() const
    { return NeighborhoodAccessorFunctorType(); }
  
   /** Set the spacing of the image and precompute the transforms for
   * the image. */
  virtual void SetSpacing (const SpacingType spacing)
    {
    Superclass::SetSpacing(spacing);

    DirectionType scale;
    for (unsigned int i=0; i < VImageDimension; i++)
      {
      scale[i][i] = this->m_Spacing[i];
      }
    m_IndexToPhysicalPoint = this->m_Direction * scale;
    m_PhysicalPointToIndex = m_IndexToPhysicalPoint.GetInverse();
    }

  virtual void SetSpacing (const double spacing[VImageDimension])
    {
    Superclass::SetSpacing(spacing);

    DirectionType scale;
    for (unsigned int i=0; i < VImageDimension; i++)
      {
      scale[i][i] = this->m_Spacing[i];
      }
    m_IndexToPhysicalPoint = this->m_Direction * scale;
    m_PhysicalPointToIndex = m_IndexToPhysicalPoint.GetInverse();
    }

  virtual void SetSpacing (const float spacing[VImageDimension])
    {
    Superclass::SetSpacing(spacing);

    DirectionType scale;
    for (unsigned int i=0; i < VImageDimension; i++)
      {
      scale[i][i] = this->m_Spacing[i];
      }
    m_IndexToPhysicalPoint = this->m_Direction * scale;
    m_PhysicalPointToIndex = m_IndexToPhysicalPoint.GetInverse();
    }

  /** Set the direction of the image and precompute the transforms for
   * the image. */
  virtual void SetDirection (const DirectionType direction)
    {
    Superclass::SetDirection(direction);

    DirectionType scale;
    for (unsigned int i=0; i < VImageDimension; i++)
      {
      scale[i][i] = this->m_Spacing[i];
      }
    m_IndexToPhysicalPoint = this->m_Direction * scale;
    m_PhysicalPointToIndex = m_IndexToPhysicalPoint.GetInverse();
    }

  /** \brief Get the continuous index from a physical point
   *
   * Returns true if the resulting index is within the image, false otherwise.
   * \sa Transform */
  template<class TCoordRep>
  bool TransformPhysicalPointToContinuousIndex(
              const Point<TCoordRep, VImageDimension>& point,
              ContinuousIndex<TCoordRep, VImageDimension>& index   ) const
    {
    Vector<double, VImageDimension> cvector;

    cvector = m_PhysicalPointToIndex * (point - this->m_Origin);
    for (unsigned int i = 0 ; i < VImageDimension ; i++)
      {
      index[i] = static_cast<TCoordRep>(cvector[i]);
      }

    // Now, check to see if the index is within allowed bounds
    const bool isInside =
      this->GetLargestPossibleRegion().IsInside( index );

    return isInside;
    }

  /** Get the index (discrete) from a physical point.
   * Floating point index results are truncated to integers.
   * Returns true if the resulting index is within the image, false otherwise
   * \sa Transform */
#if 1
  template<class TCoordRep>
  bool TransformPhysicalPointToIndex(
    const Point<TCoordRep, VImageDimension>& point,
    IndexType & index ) const
    {
      ImageTransformHelper<VImageDimension,VImageDimension-1,VImageDimension-1>::TransformPhysicalPointToIndex(
        this->m_PhysicalPointToIndex, this->m_Origin, point, index);

    // Now, check to see if the index is within allowed bounds
    const bool isInside =
      this->GetLargestPossibleRegion().IsInside( index );
    return isInside;
    }
#else
  template<class TCoordRep>
  bool TransformPhysicalPointToIndex(
            const Point<TCoordRep, VImageDimension>& point,
            IndexType & index                                ) const
    {
    typedef typename IndexType::IndexValueType IndexValueType;
    for (unsigned int i = 0; i < VImageDimension; i++)
      {
      index[i] = 0.0;
      for (unsigned int j = 0; j < VImageDimension; j++)
        {
        index[i] += 
          m_PhysicalPointToIndex[i][j] * (point[j] - this->m_Origin[j]);
        }
      }

    // Now, check to see if the index is within allowed bounds
    const bool isInside =
      this->GetLargestPossibleRegion().IsInside( index );

    return isInside;
    }
#endif
  /** Get a physical point (in the space which
   * the origin and spacing infomation comes from)
   * from a continuous index (in the index space)
   * \sa Transform */
  template<class TCoordRep>
  void TransformContinuousIndexToPhysicalPoint(
            const ContinuousIndex<TCoordRep, VImageDimension>& index,
            Point<TCoordRep, VImageDimension>& point        ) const
    {
    Vector<double,VImageDimension> cvector;
    for (unsigned int i = 0 ; i < VImageDimension ; i++)
      {
      cvector[i] = index[i];
      }

    point = this->m_Origin + m_IndexToPhysicalPoint * cvector;
    }

  /** Get a physical point (in the space which
   * the origin and spacing infomation comes from)
   * from a discrete index (in the index space)
   *
   * \sa Transform */
#if 1
  template<class TCoordRep>
  void TransformIndexToPhysicalPoint(
                      const IndexType & index,
                      Point<TCoordRep, VImageDimension>& point ) const
    {
      ImageTransformHelper<VImageDimension,VImageDimension-1,VImageDimension-1>::TransformIndexToPhysicalPoint(
        this->m_IndexToPhysicalPoint, this->m_Origin, index, point);
    }
#else
  template<class TCoordRep>
  void TransformIndexToPhysicalPoint(
                      const IndexType & index,
                      Point<TCoordRep, VImageDimension>& point ) const
    {
    for (unsigned int i = 0; i < VImageDimension; i++)
      {
      point[i] = this->m_Origin[i];
      for (unsigned int j = 0; j < VImageDimension; j++)
        {
        point[i] += m_IndexToPhysicalPoint[i][j] * index[j];
        }
      }
    }
#endif

  /** Take a vector or covariant vector that has been computed in the
   * coordinate system parallel to the image grid and rotate it by the
   * direction cosines in order to get it in terms of the coordinate system of
   * the image acquisition device.  This implementation in the OrientedImage
   * multiply the array (vector or covariant vector) by the matrix of Direction
   * Cosines. The arguments of the method are of type FixedArray to make
   * possible to use this method with both Vector and CovariantVector.
   * The Method is implemented differently in the itk::Image.
   *
   * \sa Image
   */ 
  template<class TCoordRep>
  void TransformLocalVectorToPhysicalVector(
    const FixedArray<TCoordRep, VImageDimension> & inputGradient,
          FixedArray<TCoordRep, VImageDimension> & outputGradient ) const
    {
    //
    // This temporary implementation should be replaced with Template MetaProgramming.
    // 
#ifdef ITK_USE_ORIENTED_IMAGE_DIRECTION
    const DirectionType & direction = this->GetDirection();
    for (unsigned int i = 0 ; i < VImageDimension ; i++)
      {
      typedef typename NumericTraits<TCoordRep>::AccumulateType CoordSumType;
      CoordSumType sum = NumericTraits<CoordSumType>::Zero;
      for (unsigned int j = 0; j < VImageDimension; j++)
        {
        sum += direction[i][j] * inputGradient[j];
        }
      outputGradient[i] = static_cast<TCoordRep>( sum );
      }
#else
    for (unsigned int i = 0 ; i < VImageDimension ; i++)
      {
      outputGradient[i] = inputGradient[i];
      }
#endif
    }

protected:
  OrientedImage();
  virtual ~OrientedImage() {};

private:
  OrientedImage(const Self&); //purposely not implemented
  void operator=(const Self&); //purposely not implemented

  DirectionType m_IndexToPhysicalPoint;
  DirectionType m_PhysicalPointToIndex;
};
} // end namespace itk

// Define instantiation macro for this template.
#define ITK_TEMPLATE_OrientedImage(_, EXPORT, x, y) namespace itk { \
  _(2(class EXPORT OrientedImage< ITK_TEMPLATE_2 x >)) \
  namespace Templates { typedef Image< ITK_TEMPLATE_2 x > OrientedImage##y; } \
  }

#if ITK_TEMPLATE_EXPLICIT
# include "Templates/itkOrientedImage+-.h"
#endif

#if ITK_TEMPLATE_TXX
# include "itkOrientedImage.txx"
#endif

#endif