KWStyle - itkMatrixOffsetTransformBase.txx
 
Matrix View
Description

1 /*=========================================================================
2
3   Program:   Insight Segmentation & Registration Toolkit
4   Module:    $RCSfile: itkMatrixOffsetTransformBase.txx.html,v $
5   Language:  C++
6   Date:      $Date: 2006/01/17 19:15:41 $
7   Version:   $Revision: 1.4 $
8
9   Copyright (c) Insight Software Consortium. All rights reserved.
10   See ITKCopyright.txt or http://www.itk.org/HTML/Copyright.htm for details.
11
12      This software is distributed WITHOUT ANY WARRANTY; without even 
13      the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
14      PURPOSE.  See the above copyright notices for more information.
15
16 =========================================================================*/
17 DEF #ifndef _itkMatrixOffsetTransformBase_txx
18 DEF #define _itkMatrixOffsetTransformBase_txx
19
20 #include "itkNumericTraits.h"
21 #include "itkMatrixOffsetTransformBase.h"
22 #include "vnl/algo/vnl_matrix_inverse.h"
23
24
25 namespace itk
26 {
27
28 // Constructor with default arguments
29 template<class TScalarType, unsigned int NInputDimensions,
30                             unsigned int NOutputDimensions>
31 MatrixOffsetTransformBase<TScalarType, NInputDimensions, NOutputDimensions>
32 ::MatrixOffsetTransformBase()
33 IND **: Superclass(OutputSpaceDimension, ParametersDimension)
34 {
35   m_Matrix.SetIdentity();
36   m_MatrixMTime.Modified();
37   m_Offset.Fill( 0 );
38   m_Center.Fill( 0 );
39   m_Translation.Fill( 0 );
40   m_Singular = false;
41   m_InverseMatrix.SetIdentity();
42   m_InverseMatrixMTime = m_MatrixMTime;
43   this->m_FixedParameters.SetSize ( NInputDimensions );
44   this->m_FixedParameters.Fill ( 0.0 );
45 }
46
47
48 // Constructor with default arguments
49 template<class TScalarType, unsigned int NInputDimensions,
50                             unsigned int NOutputDimensions>
51 MatrixOffsetTransformBase<TScalarType, NInputDimensions, NOutputDimensions>
52 ::MatrixOffsetTransformBase( unsigned int outputDims, 
53                              unsigned int paramDims   )
54 IND **: Superclass(outputDims, paramDims)
55 {
56   m_Matrix.SetIdentity();
57   m_MatrixMTime.Modified();
58   m_Offset.Fill( 0 );
59   m_Center.Fill( 0 );
60   m_Translation.Fill( 0 );
61   m_Singular = false;
62   m_InverseMatrix.SetIdentity();
63   m_InverseMatrixMTime = m_MatrixMTime;
64 }
65
66
67 EML
68 EML
69 // Constructor with explicit arguments
70 template<class TScalarType, unsigned int NInputDimensions,
71                             unsigned int NOutputDimensions>
72 MatrixOffsetTransformBase<TScalarType, NInputDimensions, NOutputDimensions>
73 ::MatrixOffsetTransformBase(const MatrixType &matrix,
74                             const OutputVectorType &offset)
75 {
76   m_Matrix = matrix;
77   m_MatrixMTime.Modified();
78   m_Offset = offset;
79   m_Center.Fill( 0 );
80   m_Translation.Fill(0);
81   for(unsigned int i=0; i<NOutputDimensions; i++)
82     {
83     m_Translation[i] = offset[i];
84     }
85   this->ComputeMatrixParameters();
86 }
87
88
89 EML
90 EML
91 // Destructor
92 template<class TScalarType, unsigned int NInputDimensions,
93                             unsigned int NOutputDimensions>
94 MatrixOffsetTransformBase<TScalarType, NInputDimensions, NOutputDimensions>
95 ::~MatrixOffsetTransformBase()
96 {
97   return;
98 }
99
100
101 EML
102 // Print self
103 template<class TScalarType, unsigned int NInputDimensions,
104                             unsigned int NOutputDimensions>
105 void
106 MatrixOffsetTransformBase<TScalarType, NInputDimensions, NOutputDimensions>
107 ::PrintSelf(std::ostream &os, Indent indent) const
108 {
109   Superclass::PrintSelf(os,indent);
110
111   unsigned int i, j;
112   
113   os << indent << "Matrix: " << std::endl;
114   for (i = 0; i < NInputDimensions; i++) 
115     {
116     os << indent.GetNextIndent();
117     for (j = 0; j < NOutputDimensions; j++)
118       {
119       os << m_Matrix[i][j] << " ";
120       }
121     os << std::endl;
122     }
123
124   os << indent << "Offset: " << m_Offset << std::endl;
125   os << indent << "Center: " << m_Center << std::endl;
126   os << indent << "Translation: " << m_Translation << std::endl;
127
128   os << indent << "Inverse: " << std::endl;
129   for (i = 0; i < NInputDimensions; i++) 
130     {
131     os << indent.GetNextIndent();
132     for (j = 0; j < NOutputDimensions; j++)
133       {
134       os << this->GetInverseMatrix()[i][j] << " ";
135       }
136     os << std::endl;
137     }
138   os << indent << "Singular: " << m_Singular << std::endl;
139 }
140
141 // Constructor with explicit arguments
142 template<class TScalarType, unsigned int NInputDimensions,
143                             unsigned int NOutputDimensions>
144 void
145 MatrixOffsetTransformBase<TScalarType, NInputDimensions, NOutputDimensions>
146 ::SetIdentity( void )
147 {
148   m_Matrix.SetIdentity();
149   m_MatrixMTime.Modified();
150   m_Offset.Fill( 0.0 );
151   m_Translation.Fill( 0.0 );
152   m_Center.Fill( 0.0 );
153   m_Singular = false;
154   m_InverseMatrix.SetIdentity();
155   m_InverseMatrixMTime = m_MatrixMTime;
156   this->Modified();  
157 }
158
159
160 // Compose with another affine transformation
161 template<class TScalarType, unsigned int NInputDimensions,
162                             unsigned int NOutputDimensions>
163 void
164 MatrixOffsetTransformBase<TScalarType, NInputDimensions, NOutputDimensions>
165 ::Compose(const Self * other, bool pre)
166 {
167   if (pre) 
168     {
169     m_Offset = m_Matrix * other->m_Offset + m_Offset;
170     m_Matrix = m_Matrix * other->m_Matrix;
171     }
172   else 
173     {
174     m_Offset = other->m_Matrix * m_Offset + other->m_Offset;
175     m_Matrix = other->m_Matrix * m_Matrix;
176     }
177
178   this->ComputeTranslation();
179   this->ComputeMatrixParameters();
180
181   m_MatrixMTime.Modified();
182   this->Modified();
183
184   return;
185 }
186
187
188 EML
189 // Transform a point
190 template<class TScalarType, unsigned int NInputDimensions,
191                             unsigned int NOutputDimensions>
192 typename MatrixOffsetTransformBase<TScalarType,
193                                NInputDimensions,
194                                NOutputDimensions>::OutputPointType
195 MatrixOffsetTransformBase<TScalarType, NInputDimensions, NOutputDimensions>
196 ::TransformPoint(const InputPointType &point) const 
197 {
198   return m_Matrix * point + m_Offset;
199 }
200
201
202 // Transform a vector
203 template<class TScalarType, unsigned int NInputDimensions,
204                             unsigned int NOutputDimensions>
205 typename MatrixOffsetTransformBase<TScalarType,
206                                NInputDimensions,
207                                NOutputDimensions>::OutputVectorType
208 MatrixOffsetTransformBase<TScalarType, NInputDimensions, NOutputDimensions>
209 ::TransformVector(const InputVectorType &vect) const 
210 {
211   return m_Matrix * vect;
212 }
213
214
215 // Transform a vnl_vector_fixed
216 template<class TScalarType, unsigned int NInputDimensions,
217                             unsigned int NOutputDimensions>
218 typename MatrixOffsetTransformBase<TScalarType,
219                                NInputDimensions,
220                                NOutputDimensions>::OutputVnlVectorType
221 MatrixOffsetTransformBase<TScalarType, NInputDimensions, NOutputDimensions>
222 ::TransformVector(const InputVnlVectorType &vect) const 
223 {
224   return m_Matrix * vect;
225 }
226
227
228 // Transform a CovariantVector
229 template<class TScalarType, unsigned int NInputDimensions,
230                             unsigned int NOutputDimensions>
231 typename MatrixOffsetTransformBase<TScalarType,
232                                NInputDimensions,
233                                NOutputDimensions>::OutputCovariantVectorType
234 MatrixOffsetTransformBase<TScalarType, NInputDimensions, NOutputDimensions>
235 ::TransformCovariantVector(const InputCovariantVectorType &vec) const 
236 {
237   OutputCovariantVectorType  result;    // Converted vector
238
239   for (unsigned int i = 0; i < NOutputDimensions; i++) 
240     {
241     result[i] = NumericTraits<ScalarType>::Zero;
242     for (unsigned int j = 0; j < NInputDimensions; j++) 
243       {
244       result[i] += this->GetInverseMatrix()[j][i]*vec[j]; // Inverse transposed
245       }
246     }
247   return result;
248 }
249
250 // Recompute the inverse matrix (internal)
251 template<class TScalarType, unsigned int NInputDimensions,
252                             unsigned int NOutputDimensions>
253 const typename MatrixOffsetTransformBase<TScalarType,
254                                NInputDimensions,
255                                NOutputDimensions>::InverseMatrixType &
256 MatrixOffsetTransformBase<TScalarType, NInputDimensions, NOutputDimensions>
257 ::GetInverseMatrix( void ) const
258 {
259   // If the transform has been modified we recompute the inverse
260   if(m_InverseMatrixMTime != m_MatrixMTime)
261     {
262     m_Singular = false;
263     try 
264       {
265       m_InverseMatrix  = m_Matrix.GetInverse();
266       }
267     catch(...) 
268       {
269       m_Singular = true;
270       }
271 IND *****m_InverseMatrixMTime = m_MatrixMTime;
272     }
273
274   return m_InverseMatrix;
275 }
276
277 // return an inverse transformation
278 template<class TScalarType, unsigned int NInputDimensions,
279                             unsigned int NOutputDimensions>
280 bool
281 MatrixOffsetTransformBase<TScalarType, NInputDimensions, NOutputDimensions>
282 ::GetInverse( Self * inverse) const
283 {
284   if(!inverse)
285     {
286     return false;
287     }
288
289   this->GetInverseMatrix();
290   if(m_Singular)
291     {
292     return false;
293     }
294
295   inverse->m_Matrix         = this->GetInverseMatrix();
296   inverse->m_InverseMatrix  = m_Matrix;
297   inverse->m_Offset         = -(this->GetInverseMatrix() * m_Offset);
298   inverse->ComputeTranslation();
299   inverse->ComputeMatrixParameters();
300
301   return true;
302 }
303
304
305 // Get fixed parameters
306 template<class TScalarType, unsigned int NInputDimensions,
307                             unsigned int NOutputDimensions>
308 void
309 MatrixOffsetTransformBase<TScalarType, NInputDimensions, NOutputDimensions>
310 IND **::SetFixedParameters( const ParametersType & fp )
311 {
312   this->m_FixedParameters = fp;
313   InputPointType c;
314   for ( unsigned int i = 0; i < NInputDimensions; i++ )
315     {
316     c[i] = this->m_FixedParameters[i];
317     }
318   this->SetCenter ( c );
319 }
320
321 /** Get the Fixed Parameters. */
322 template<class TScalarType, unsigned int NInputDimensions,
323                             unsigned int NOutputDimensions>
324 const typename MatrixOffsetTransformBase<TScalarType,
325                                      NInputDimensions,
326                                      NOutputDimensions>::ParametersType &
327 MatrixOffsetTransformBase<TScalarType, NInputDimensions, NOutputDimensions>
328 IND **::GetFixedParameters(void) const
329 {
330   this->m_FixedParameters.SetSize ( NInputDimensions );
331   for ( unsigned int i = 0; i < NInputDimensions; i++ )
332     {
333     this->m_FixedParameters[i] = this->m_Center[i];
334     }
335   return this->m_FixedParameters;
336 }
337
338
339 EML
340 // Get parameters
341 template<class TScalarType, unsigned int NInputDimensions,
342                             unsigned int NOutputDimensions>
343 const typename MatrixOffsetTransformBase<TScalarType,
344                                      NInputDimensions,
345                                      NOutputDimensions>::ParametersType &
346 MatrixOffsetTransformBase<TScalarType, NInputDimensions, NOutputDimensions>
347 ::GetParameters( void ) const
348 {
349   // Transfer the linear part
350   unsigned int par = 0;
351
352   for(unsigned int row=0; row<NOutputDimensions; row++) 
353     {
354     for(unsigned int col=0; col<NInputDimensions; col++) 
355       {
356       this->m_Parameters[par] = m_Matrix[row][col];
357       ++par;
358       }
359     }
360
361   // Transfer the constant part
362   for(unsigned int i=0; i<NOutputDimensions; i++) 
363     {
364     this->m_Parameters[par] = m_Translation[i];
365     ++par;
366     }
367
368   return this->m_Parameters;
369 }
370
371
372 // Set parameters
373 template<class TScalarType, unsigned int NInputDimensions,
374                             unsigned int NOutputDimensions>
375 void
376 MatrixOffsetTransformBase<TScalarType, NInputDimensions, NOutputDimensions>
377 ::SetParameters( const ParametersType & parameters )
378 {
379   unsigned int par = 0;
380
381   this->m_Parameters = parameters;
382
383   for(unsigned int row=0; row<NOutputDimensions; row++) 
384     {
385     for(unsigned int col=0; col<NInputDimensions; col++) 
386       {
387       m_Matrix[row][col] = this->m_Parameters[par];
388       ++par;
389       }
390     }
391
392   // Transfer the constant part
393   for(unsigned int i=0; i<NOutputDimensions; i++) 
394     {
395     m_Translation[i] = this->m_Parameters[par];
396     ++par;
397     }
398
399   m_MatrixMTime.Modified(); 
400
401   this->ComputeMatrix();  // Not necessary since parameters explicitly define
402 IND **************************//    the matrix
403   this->ComputeOffset();
404
405   this->Modified();
406  
407 }
408
409
410 // Compute the Jacobian in one position 
411 template<class TScalarType, unsigned int NInputDimensions,
412                             unsigned int NOutputDimensions>
413 LEN const typename MatrixOffsetTransformBase<TScalarType, NInputDimensions, NOutputDimensions>::JacobianType & 
414 MatrixOffsetTransformBase<TScalarType, NInputDimensions, NOutputDimensions>
415 ::GetJacobian( const InputPointType & p ) const
416 {
417   // The Jacobian of the affine transform is composed of
418   // subblocks of diagonal matrices, each one of them having
419   // a constant value in the diagonal.
420
421   this->m_Jacobian.Fill( 0.0 );
422
423   unsigned int blockOffset = 0;
424   
425   for(unsigned int block=0; block < NInputDimensions; block++) 
426     {
427     for(unsigned int dim=0; dim < NOutputDimensions; dim++ ) 
428       {
429       this->m_Jacobian( block , blockOffset + dim ) = p[dim];
430       }
431
432     blockOffset += NInputDimensions;
433
434     }
435
436   for(unsigned int dim=0; dim < NOutputDimensions; dim++ ) 
437     {
438     this->m_Jacobian( dim , blockOffset + dim ) = 1.0;
439     }
440
441   return this->m_Jacobian;
442
443 }
444
445 // Computes offset based on center, matrix, and translation variables
446 template<class TScalarType, unsigned int NInputDimensions,
447                             unsigned int NOutputDimensions>
448 void
449 MatrixOffsetTransformBase<TScalarType, NInputDimensions, NOutputDimensions>
450 ::ComputeOffset( void ) 
451 {
452   const MatrixType & matrix = this->GetMatrix();
453   
454   OffsetType offset;
455   for(unsigned int i=0; i<NOutputDimensions; i++)
456     {
457     offset[i] = m_Translation[i] + m_Center[i];
458     for(unsigned int j=0; j<NInputDimensions; j++)
459       {
460       offset[i] -= matrix[i][j] * m_Center[j];
461       }
462     }
463
464 SEM   m_Offset = offset ;
465 }
466
467 // Computes translation based on offset, matrix, and center
468 template<class TScalarType, unsigned int NInputDimensions,
469                             unsigned int NOutputDimensions>
470 void
471 MatrixOffsetTransformBase<TScalarType, NInputDimensions, NOutputDimensions>
472 ::ComputeTranslation( void ) 
473 {
474   const MatrixType & matrix = this->GetMatrix();
475   
476   OffsetType translation;
477   for(unsigned int i=0; i<NOutputDimensions; i++)
478     {
479     translation[i] = m_Offset[i] - m_Center[i];
480     for(unsigned int j=0; j<NInputDimensions; j++)
481       {
482       translation[i] += matrix[i][j] * m_Center[j];
483       }
484     }
485
486 SEM   m_Translation = translation ;
487 }
488
489
490 // Computes matrix - base class does nothing.  In derived classes is
491 //    used to convert, for example, versor into a matrix
492 template<class TScalarType, unsigned int NInputDimensions,
493                             unsigned int NOutputDimensions>
494 void
495 MatrixOffsetTransformBase<TScalarType, NInputDimensions, NOutputDimensions>
496 ::ComputeMatrix( void ) 
497 {
498   // Since parameters explicitly define the matrix in this base class, this
499   // function does nothing.  Normally used to compute a matrix when
500   // its parameterization (e.g., the class' versor) is modified.
501 }
502
503
504 // Computes parameters - base class does nothing.  In derived classes is
505 //    used to convert, for example, matrix into a versor
506 template<class TScalarType, unsigned int NInputDimensions,
507                             unsigned int NOutputDimensions>
508 void
509 MatrixOffsetTransformBase<TScalarType, NInputDimensions, NOutputDimensions>
510 ::ComputeMatrixParameters( void ) 
511 {
512   // Since parameters explicitly define the matrix in this base class, this
513   // function does nothing.  Normally used to update the parameterization
514   // of the matrix (e.g., the class' versor) when the matrix is explicitly
515   // set.
516 }
517
518 // namespace
519
520 #endif
521
522 EOF

Generated by KWStyle 1.0b on Tuesday January,17 at 02:14:21PM
© Kitware Inc.