Shapeworks Studio  2.1
Shape analysis software suite
itkPSMImageDomainWithGradients.h
1 /*=========================================================================
2  *
3  * Copyright Insight Software Consortium
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0.txt
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  *=========================================================================*/
18 #ifndef __itkPSMImageDomainWithGradients_h
19 #define __itkPSMImageDomainWithGradients_h
20 
21 #include "itkImage.h"
22 #include "itkImageDuplicator.h"
23 #include "itkPSMImageDomain.h"
24 #include "itkVectorLinearInterpolateImageFunction.h"
25 #include "itkGradientImageFilter.h"
26 #include "itkFixedArray.h"
27 
28 namespace itk
29 {
40 template <class T, unsigned int VDimension>
41 class ITK_EXPORT PSMImageDomainWithGradients : public PSMImageDomain<T, VDimension>
42 {
43 public:
46  typedef PSMImageDomain<T, VDimension> Superclass;
47  typedef SmartPointer<Self> Pointer;
48  typedef SmartPointer<const Self> ConstPointer;
49  typedef WeakPointer<const Self> ConstWeakPointer;
50 
52  typedef typename Superclass::PointType PointType;
53 
54  typedef typename Superclass::ImageType ImageType;
55  typedef typename Superclass::ScalarInterpolatorType ScalarInterpolatorType;
56  typedef GradientImageFilter<ImageType> GradientImageFilterType;
57  typedef typename GradientImageFilterType::OutputImageType GradientImageType;
58  typedef VectorLinearInterpolateImageFunction<GradientImageType, typename PointType::CoordRepType>
59  GradientInterpolatorType;
60 
61  typedef FixedArray<T, VDimension> VectorType;
62  typedef vnl_vector_fixed<T, VDimension> VnlVectorType;
63 
65  itkNewMacro(Self);
66 
69 
71  itkStaticConstMacro(Dimension, unsigned int, VDimension);
72 
75  void SetImage(ImageType *I)
76  {
77  Superclass::SetImage(I);
78 
79  // Compute gradient image and set up gradient interpolation.
80  typename GradientImageFilterType::Pointer filter = GradientImageFilterType::New();
81  filter->SetInput(I);
82  filter->SetUseImageSpacingOn();
83  filter->Update();
84  m_GradientImage = filter->GetOutput();
85 
86  m_GradientInterpolator->SetInputImage(m_GradientImage);
87  }
88  itkGetObjectMacro(GradientImage, GradientImageType);
89 
90 
95  inline VectorType SampleGradient(const PointType &p) const
96  { return m_GradientInterpolator->Evaluate(p); }
97  inline VnlVectorType SampleGradientVnl(const PointType &p) const
98  { return VnlVectorType( this->SampleGradient(p).GetDataPointer() ); }
99  inline VnlVectorType SampleNormalVnl(const PointType &p, T epsilon = 1.0e-5) const
100  {
101  VnlVectorType grad = this->SampleGradientVnl(p);
102  double q = 1.0 / (grad.magnitude() + epsilon);
103  for (unsigned int i = 0; i < VDimension; i++) { grad[i] *= q; }
104  return grad;
105  }
106 
108  itkGetObjectMacro(GradientInterpolator, GradientInterpolatorType);
109 
110 
114  virtual bool ApplyVectorConstraints(vnl_vector_fixed<double, VDimension> &gradE,
115  const PointType &pos,
116  double) const
117 
118  {
119  if (this->m_ConstraintsEnabled == true)
120  {
121  const double epsilon = 1.0e-10;
122 
123  double dotprod = 0.0;
124  VnlVectorType normal = this->SampleNormalVnl(pos, epsilon);
125  for (unsigned int i = 0; i < VDimension; i++) { dotprod += normal[i] * gradE[i]; }
126  for (unsigned int i = 0; i < VDimension; i++) { gradE[i] -= normal[i] * dotprod; }
127 
128  return true;
129  }
130  else return false;
131  }
132 
133 protected:
135  {
136  m_GradientInterpolator = GradientInterpolatorType::New();
137  }
138 
139  void PrintSelf(std::ostream& os, Indent indent) const
140  {
141  Superclass::PrintSelf(os, indent);
142  os << indent << "m_GradientImage = " << m_GradientImage << std::endl;
143  os << indent << "m_GradientInterpolator = " << m_GradientInterpolator << std::endl;
144  }
145  virtual ~PSMImageDomainWithGradients() {};
146 
147 private:
148  PSMImageDomainWithGradients(const Self&); //purposely not implemented
149  void operator=(const Self&); //purposely not implemented
150 
151  typename GradientImageType::Pointer m_GradientImage;
152  typename GradientInterpolatorType::Pointer m_GradientInterpolator;
153 };
154 
155 } // end namespace itk
156 
157 #endif
Image< T, VDimension > ImageType
virtual bool ApplyVectorConstraints(vnl_vector_fixed< double, VDimension > &gradE, const PointType &pos, double) const
VectorType SampleGradient(const PointType &p) const