ITK  4.6.0
Insight Segmentation and Registration Toolkit
itkMultivariateLegendrePolynomial.h
Go to the documentation of this file.
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 __itkMultivariateLegendrePolynomial_h
19 #define __itkMultivariateLegendrePolynomial_h
20 
21 #include "itkIntTypes.h"
22 #include "itkIndent.h"
23 #include <vector>
24 #include "itkArray.h"
25 
26 namespace itk
27 {
75 {
76 public:
78 
79  typedef std::vector< double > DoubleArrayType;
80  typedef std::vector< unsigned long > ULongArrayType;
81  typedef std::vector< long > LongArrayType;
82 
85 
89 
93 
95  MultivariateLegendrePolynomial(unsigned int dimension,
96  unsigned int degree,
97  const DomainSizeType & domainSize);
98 
101 
103  unsigned int GetDimension(void) const
104  { return m_Dimension; }
105 
107  unsigned int GetDegree(void) const
108  { return m_Degree; }
109 
116  unsigned int GetNumberOfCoefficients(void) const
117  { return m_NumberOfCoefficients; }
118 
120  const DomainSizeType & GetDomainSize(void) const
121  { return m_DomainSize; }
122 
128  {
129 public:
130  CoefficientVectorSizeMismatch(int given, int required)
131  {
132  m_Required = required;
133  m_Given = given;
134  }
135 
137  int m_Given;
138  };
139 
144  void SetCoefficients(const CoefficientArrayType & coef)
146 
147  void SetCoefficients(const ParametersType & coef)
149 
151  const CoefficientArrayType & GetCoefficients(void) const;
152 
155  double Evaluate(IndexType & index)
156  {
157  if ( m_Dimension == 2 )
158  {
159  if ( index[1] != m_PrevY )
160  {
161  // normalized y [-1, 1]
162  double norm_y = m_NormFactor[1]
163  * static_cast< double >( index[1] - 1 );
164  this->CalculateXCoef(norm_y, m_CoefficientArray);
165  m_PrevY = index[1];
166  }
168 
169  // normalized x [-1, 1]
170  double norm_x = m_NormFactor[0]
171  * static_cast< double >( index[0] - 1 );
172 
173  return LegendreSum(norm_x, m_Degree, m_CachedXCoef);
174  }
175  else if ( m_Dimension == 3 )
176  {
177  if ( index[2] != m_PrevZ )
178  {
179  // normalized z [-1, 1]
180  double norm_z = m_NormFactor[2]
181  * static_cast< double >( index[2] - 1 );
182  this->CalculateYCoef(norm_z, m_CoefficientArray);
183  m_PrevZ = index[2];
184  }
185 
186  if ( index[1] != m_PrevY )
187  {
188  // normalized y [-1, 1]
189  double norm_y = m_NormFactor[1]
190  * static_cast< double >( index[1] - 1 );
191  this->CalculateXCoef(norm_y, m_CachedYCoef);
192  m_PrevY = index[1];
193  }
194 
195  // normalized x [-1, 1]
196  double norm_x = m_NormFactor[0]
197  * static_cast< double >( index[0] - 1 );
198  return this->LegendreSum(norm_x, m_Degree, m_CachedXCoef);
199  }
200  return 0;
201  }
202 
204  unsigned int GetNumberOfCoefficients();
205 
207  unsigned int GetNumberOfCoefficients(unsigned int dimension, unsigned int degree);
208 
216  {
217 public:
222  m_IsAtEnd(false)
223  {
224  m_Index.resize(m_Dimension);
225  std::fill(m_Index.begin(), m_Index.end(), 0);
226  }
227 
228  void Begin(void)
229  {
230  m_IsAtEnd = false;
231  for ( unsigned int dim = 0; dim < m_Dimension; dim++ )
232  {
233  m_Index[dim] = 0;
234  }
235  }
236 
237  bool IsAtEnd()
238  { return m_IsAtEnd; }
239 
241  {
242  for ( unsigned int dim = 0; dim < m_Dimension; dim++ )
243  {
244  if ( m_Index[dim] < static_cast< int >( m_DomainSize[dim] - 1 ) )
245  {
246  m_Index[dim] += 1;
247  return *this;
248  }
249  else
250  {
251  if ( dim == m_Dimension - 1 )
252  {
253  m_IsAtEnd = true;
254  break;
255  }
256  else
257  {
258  m_Index[dim] = 0;
259  }
260  }
261  }
262  return *this;
263  }
264 
265  double Get()
267 
268 private:
270  unsigned int m_Dimension;
273  bool m_IsAtEnd;
274  }; // end of class Iterator
275 
276  void Print(std::ostream & os);
277 
278 protected:
279  void PrintSelf(std::ostream & os, Indent indent) const;
280 
281  double LegendreSum(const double x, int n,
282  const CoefficientArrayType & coef,
283  int offset = 0);
284 
285  void CalculateXCoef(double norm_y, const CoefficientArrayType & coef);
286 
287  void CalculateYCoef(double norm_z, const CoefficientArrayType & coef);
288 
289 private:
291  unsigned int m_Dimension;
292  unsigned int m_Degree;
294 
299 
303 }; // end of class
304 
305  std::ostream & operator<<(std::ostream & os,
307 } // end of namespace itk
308 #endif
MultivariateLegendrePolynomial(unsigned int dimension, unsigned int degree, const DomainSizeType &domainSize)
signed long IndexValueType
Definition: itkIntTypes.h:150
std::ostream & operator<<(std::ostream &os, const Array< TValue > &arr)
Definition: itkArray.h:184
void PrintSelf(std::ostream &os, Indent indent) const
double LegendreSum(const double x, int n, const CoefficientArrayType &coef, int offset=0)
const DomainSizeType & GetDomainSize(void) const
2D and 3D multivariate Legendre Polynomial
void CalculateYCoef(double norm_z, const CoefficientArrayType &coef)
void CalculateXCoef(double norm_y, const CoefficientArrayType &coef)
const CoefficientArrayType & GetCoefficients(void) const
Gets Legendre polynomials&#39; coefficients.
Control indentation during Print() invocation.
Definition: itkIndent.h:49
void SetCoefficients(const CoefficientArrayType &coef)
Sets the Legendre polynomials&#39; parameters.
Iterator which only supports forward iteration and Begin(), IsAtEnd(), and Get() method which work ju...
void Print(std::ostream &os)