ITK  4.0.0
Insight Segmentation and Registration Toolkit
itkCompositeTransform.h
Go to the documentation of this file.
00001 /*=========================================================================
00002  *
00003  *  Copyright Insight Software Consortium
00004  *
00005  *  Licensed under the Apache License, Version 2.0 (the "License");
00006  *  you may not use this file except in compliance with the License.
00007  *  You may obtain a copy of the License at
00008  *
00009  *         http://www.apache.org/licenses/LICENSE-2.0.txt
00010  *
00011  *  Unless required by applicable law or agreed to in writing, software
00012  *  distributed under the License is distributed on an "AS IS" BASIS,
00013  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00014  *  See the License for the specific language governing permissions and
00015  *  limitations under the License.
00016  *
00017  *=========================================================================*/
00018 #ifndef __itkCompositeTransform_h
00019 #define __itkCompositeTransform_h
00020 
00021 #include "itkTransform.h"
00022 
00023 #include <deque>
00024 
00025 namespace itk
00026 {
00027 
00100 template
00101 <class TScalar = double, unsigned int NDimensions = 3>
00102 class ITK_EXPORT CompositeTransform :
00103   public Transform<TScalar, NDimensions, NDimensions>
00104 {
00105 public:
00107   typedef CompositeTransform                           Self;
00108   typedef Transform<TScalar, NDimensions, NDimensions> Superclass;
00109   typedef SmartPointer<Self>                           Pointer;
00110   typedef SmartPointer<const Self>                     ConstPointer;
00111 
00113   itkTypeMacro( CompositeTransform, Transform );
00114 
00116   itkSimpleNewMacro( Self );
00117 
00122   virtual::itk::LightObject::Pointer CreateAnother(void) const
00123   {
00124     itkExceptionMacro("CreateAnother unimplemented. See source comments.");
00125   }
00126 
00128   typedef Superclass                   TransformType;
00129   typedef typename Superclass::Pointer TransformTypePointer;
00130 
00132   typedef typename Superclass::InverseTransformBasePointer
00133   InverseTransformBasePointer;
00134 
00136   typedef typename Superclass::ScalarType ScalarType;
00137 
00139   typedef typename Superclass::ParametersType      ParametersType;
00140   typedef typename Superclass::ParametersValueType ParametersValueType;
00141 
00143   typedef typename Superclass::DerivativeType DerivativeType;
00144 
00146   typedef typename Superclass::JacobianType JacobianType;
00147 
00149   typedef typename Superclass::InputPointType  InputPointType;
00150   typedef typename Superclass::OutputPointType OutputPointType;
00151 
00153   typedef typename Superclass::InputVectorType  InputVectorType;
00154   typedef typename Superclass::OutputVectorType OutputVectorType;
00155 
00157   typedef typename Superclass::InputCovariantVectorType
00158   InputCovariantVectorType;
00159   typedef typename Superclass::OutputCovariantVectorType
00160   OutputCovariantVectorType;
00161 
00163   typedef typename Superclass::InputVnlVectorType  InputVnlVectorType;
00164   typedef typename Superclass::OutputVnlVectorType OutputVnlVectorType;
00165 
00167   typedef typename Superclass::InputVectorPixelType  InputVectorPixelType;
00168   typedef typename Superclass::OutputVectorPixelType OutputVectorPixelType;
00169 
00171   typedef typename Superclass::InputDiffusionTensor3DType  InputDiffusionTensor3DType;
00172   typedef typename Superclass::OutputDiffusionTensor3DType OutputDiffusionTensor3DType;
00173 
00175   typedef typename Superclass::InputSymmetricSecondRankTensorType
00176     InputSymmetricSecondRankTensorType;
00177   typedef typename Superclass::OutputSymmetricSecondRankTensorType
00178     OutputSymmetricSecondRankTensorType;
00179 
00181   typedef std::deque<TransformTypePointer> TransformQueueType;
00182 
00184   typedef std::deque<bool> TransformsToOptimizeFlagsType;
00185 
00187   typedef typename Superclass::NumberOfParametersType NumberOfParametersType;
00188 
00190   itkStaticConstMacro( InputDimension, unsigned int, NDimensions );
00191   itkStaticConstMacro( OutputDimension, unsigned int, NDimensions );
00193 
00199   void AddTransform( TransformType *t  )
00200   {
00201     this->PushBackTransform( t ); /* Also adds to TransformsToOptimize list */
00202   }
00203 
00204   void RemoveTransform()
00205   {
00206     this->PopBackTransform(); /* Also removes to TransformsToOptimize list */
00207   }
00208 
00210   const
00211   TransformTypePointer GetFrontTransform()
00212   {
00213     return this->m_TransformQueue.front();
00214   }
00215 
00216   const
00217   TransformTypePointer GetBackTransform()
00218   {
00219     return this->m_TransformQueue.back();
00220   }
00221 
00222   const
00223   TransformTypePointer GetNthTransform( size_t n ) const
00224   {
00225     return this->m_TransformQueue[n];
00226   }
00227 
00230   void SetNthTransformToOptimize( size_t i, bool state )
00231   {
00232     this->m_TransformsToOptimizeFlags.at(i) = state;
00233     this->Modified();
00234   }
00235 
00236   void SetNthTransformToOptimizeOn( size_t i )
00237   {
00238     this->SetNthTransformToOptimize( i, true );
00239   }
00240 
00241   void SetNthTransformToOptimizeOff( size_t i )
00242   {
00243     this->SetNthTransformToOptimize( i, false );
00244   }
00245 
00246   void SetAllTransformsToOptimize( bool state )
00247   {
00248     this->m_TransformsToOptimizeFlags.assign(
00249       this->m_TransformsToOptimizeFlags.size(), state );
00250     this->Modified();
00251   }
00252 
00253   void SetAllTransformsToOptimizeOn()
00254   {
00255     this->SetAllTransformsToOptimize( true );
00256   }
00257 
00258   void SetAllTransformsToOptimizeOff()
00259   {
00260     this->SetAllTransformsToOptimize( false );
00261   }
00262 
00263   /* With AddTransform() as the only way to add a transform, we
00264    * can have this method to easily allow user to optimize only
00265    * the transform added most recenlty. */
00266   void SetOnlyMostRecentTransformToOptimizeOn()
00267   {
00268     this->SetAllTransformsToOptimize( false );
00269     this->SetNthTransformToOptimizeOn( this->GetNumberOfTransforms() - 1 );
00270   }
00271 
00272   /* Get whether the Nth transform is set to be optimzied */
00273   /* NOTE: ambiguous function name here - are we getting if the Nth transform
00274       is set to be optimized, or the Nth of the transforms that are set to be
00275       optimized? */
00276   bool GetNthTransformToOptimize( size_t i ) const
00277   {
00278     return this->m_TransformsToOptimizeFlags.at(i);
00279   }
00280 
00282   const TransformQueueType & GetTransformQueue() const
00283   {
00284     return this->m_TransformQueue;
00285   }
00286 
00288   const TransformsToOptimizeFlagsType & GetTransformsToOptimizeFlags() const
00289   {
00290     return this->m_TransformsToOptimizeFlags;
00291   }
00292 
00294   bool IsTransformQueueEmpty()
00295   {
00296     return this->m_TransformQueue.empty();
00297   }
00298 
00299   size_t GetNumberOfTransforms() const
00300   {
00301     return this->m_TransformQueue.size();
00302   }
00303 
00304   void ClearTransformQueue()
00305   {
00306     this->m_TransformQueue.clear();
00307     this->m_TransformsToOptimizeFlags.clear();
00308     this->Modified();
00309   }
00310 
00312   bool GetInverse( Self *inverse ) const;
00313 
00314   virtual InverseTransformBasePointer GetInverseTransform() const;
00315 
00330   virtual OutputPointType TransformPoint( const InputPointType & inputPoint ) const;
00331 
00332   /* Note: why was the 'isInsideTransformRegion' flag used below?
00333   {
00334     bool isInside = true;
00335 
00336     return this->TransformPoint( inputPoint, isInside );
00337   }
00338   virtual OutputPointType TransformPoint( const InputPointType& thisPoint,
00339                                           bool &isInsideTransformRegion ) const;
00340   */
00342   using Superclass::TransformVector;
00343   virtual OutputVectorType TransformVector(const InputVectorType &) const;
00344 
00345   virtual OutputVnlVectorType TransformVector(const InputVnlVectorType & inputVector) const;
00346 
00347   virtual OutputVectorPixelType TransformVector(const InputVectorPixelType & inputVector ) const;
00348 
00349   virtual OutputVectorType TransformVector(const InputVectorType & inputVector,
00350                                            const InputPointType & inputPoint ) const;
00351 
00352   virtual OutputVnlVectorType TransformVector(const InputVnlVectorType & inputVector,
00353                                               const InputPointType & inputPoint ) const;
00354 
00355   virtual OutputVectorPixelType TransformVector(const InputVectorPixelType & inputVector,
00356                                                 const InputPointType & inputPoint ) const;
00357 
00359   using Superclass::TransformCovariantVector;
00360   virtual OutputCovariantVectorType TransformCovariantVector(const InputCovariantVectorType &) const;
00361 
00362   virtual OutputVectorPixelType TransformCovariantVector(const InputVectorPixelType &) const;
00363 
00364   virtual OutputCovariantVectorType TransformCovariantVector(const InputCovariantVectorType & inputVector,
00365                                                              const InputPointType & inputPoint ) const;
00366 
00367   virtual OutputVectorPixelType TransformCovariantVector(const InputVectorPixelType & inputVector,
00368                                                          const InputPointType & inputPoint ) const;
00369 
00371   using Superclass::TransformDiffusionTensor3D;
00372   virtual OutputDiffusionTensor3DType TransformDiffusionTensor3D(
00373     const InputDiffusionTensor3DType & inputTensor) const;
00374 
00375   virtual OutputVectorPixelType TransformDiffusionTensor3D(
00376     const InputVectorPixelType & inputTensor) const;
00377 
00378   virtual OutputDiffusionTensor3DType TransformDiffusionTensor3D(
00379     const InputDiffusionTensor3DType & inputTensor,
00380     const InputPointType & inputPoint ) const;
00381 
00382   virtual OutputVectorPixelType TransformDiffusionTensor3D(
00383     const InputVectorPixelType & inputTensor,
00384     const InputPointType & inputPoint ) const;
00385 
00387   using Superclass::TransformSymmetricSecondRankTensor;
00388   virtual OutputSymmetricSecondRankTensorType TransformSymmetricSecondRankTensor(
00389     const InputSymmetricSecondRankTensorType & inputTensor) const;
00390 
00391   virtual OutputVectorPixelType TransformSymmetricSecondRankTensor(
00392     const InputVectorPixelType & inputTensor) const;
00393 
00394   virtual OutputSymmetricSecondRankTensorType TransformSymmetricSecondRankTensor(
00395     const InputSymmetricSecondRankTensorType & inputTensor,
00396     const InputPointType & inputPoint ) const;
00397 
00398   virtual OutputVectorPixelType TransformSymmetricSecondRankTensor(
00399     const InputVectorPixelType & inputTensor,
00400     const InputPointType & inputPoint ) const;
00401 
00402   virtual bool IsLinear() const;
00403 
00409   virtual const ParametersType & GetParameters(void) const;
00410 
00411   /* SetParameters only for transforms that are set to be optimized */
00412   virtual void  SetParameters(const ParametersType & p);
00413 
00414   /* GetFixedParameters only for transforms that are set to be optimized */
00415   virtual const ParametersType & GetFixedParameters(void) const;
00416 
00417   /* SetFixedParameters only for transforms that are set to be optimized */
00418   virtual void  SetFixedParameters(const ParametersType & fixedParameters);
00419 
00420   /* Get total number of parameters for transforms that are set to be
00421    * optimized */
00422   virtual NumberOfParametersType GetNumberOfParameters(void) const;
00423 
00424   /* Get total number of local parameters for transforms that are set
00425    * to be optimized */
00426   virtual NumberOfParametersType GetNumberOfLocalParameters(void) const;
00427 
00428   /* Get total number of fixed parameters for transforms that are set
00429    * to be optimized */
00430   virtual NumberOfParametersType GetNumberOfFixedParameters(void) const;
00431 
00432   /* Prepare the transform for use, e.g. in registration framework.
00433    * Must be called before registration to optimize parameter storage
00434    * for more efficient operation, particularly with high-dimensionality
00435    * sub-transforms. */
00436   // virtual void PrepareForUse(void);
00437 
00446   virtual void UpdateTransformParameters( DerivativeType & update, ScalarType  factor = 1.0 );
00447 
00453   virtual bool HasLocalSupport() const;
00454 
00459   virtual void ComputeJacobianWithRespectToParameters(const InputPointType  & p, JacobianType & j) const;
00460 
00461   virtual void ComputeJacobianWithRespectToPosition(const InputPointType &,
00462                                                     JacobianType &) const
00463   {
00464     itkExceptionMacro( "ComputeJacobianWithRespectToPosition not yet implemented "
00465                        "for " << this->GetNameOfClass() );
00466   }
00467 
00468 protected:
00469   CompositeTransform();
00470   virtual ~CompositeTransform();
00471   void PrintSelf( std::ostream& os, Indent indent ) const;
00472 
00473   void PushFrontTransform( TransformTypePointer t  )
00474   {
00475     this->m_TransformQueue.push_front( t );
00476     /* Add element to list of flags, and set true by default */
00477     this->m_TransformsToOptimizeFlags.push_front( true );
00478     this->Modified();
00479   }
00480 
00481   void PushBackTransform( TransformTypePointer t  )
00482   {
00483     this->m_TransformQueue.push_back( t );
00484     /* Add element to list of flags, and set true by default */
00485     this->m_TransformsToOptimizeFlags.push_back( true );
00486     this->Modified();
00487   }
00488 
00489   void PopFrontTransform()
00490   {
00491     this->m_TransformQueue.pop_front();
00492     this->m_TransformsToOptimizeFlags.pop_front();
00493     this->Modified();
00494   }
00495 
00496   void PopBackTransform()
00497   {
00498     this->m_TransformQueue.pop_back();
00499     this->m_TransformsToOptimizeFlags.pop_back();
00500     this->Modified();
00501   }
00502 
00508   // void UnifyParameterMemory(void);
00509 
00511   mutable TransformQueueType m_TransformQueue;
00512 
00514   TransformQueueType & GetTransformsToOptimizeQueue() const;
00515 
00516   mutable TransformQueueType            m_TransformsToOptimizeQueue;
00517   mutable TransformsToOptimizeFlagsType m_TransformsToOptimizeFlags;
00518 private:
00519   CompositeTransform( const Self & ); // purposely not implemented
00520   void operator=( const Self & );     // purposely not implemented
00521 
00522   mutable unsigned long m_PreviousTransformsToOptimizeUpdateTime;
00523 };
00524 
00525 } // end namespace itk
00526 
00527 #if ITK_TEMPLATE_EXPLICIT
00528 #include "Templates/itkCompositeTransform+-.h"
00529 #endif
00530 
00531 #if ITK_TEMPLATE_TXX
00532 #include "itkCompositeTransform.hxx"
00533 #endif
00534 
00535 #endif // __itkCompositeTransform_h
00536