36 #ifndef __itkCascadedTransform_h
37 #define __itkCascadedTransform_h
46 #include <itkTransform.h>
49 #include <Core/Utils/Log.h>
58 template <
class TScalar,
unsigned int Dimension>
60 public ITK_EXPORT Transform<TScalar, Dimension, Dimension>
65 typedef Transform<TScalar, Dimension, Dimension> Superclass;
66 typedef SmartPointer<Self> Pointer;
67 typedef SmartPointer<const Self> ConstPointer;
73 typedef typename InverseTransformBaseType::Pointer InverseTransformBasePointer;
75 typedef SmartPointer<Superclass> TransformPointer;
76 typedef SmartPointer<const Superclass> ConstTransformPointer;
92 typedef typename Superclass::ParametersType ParametersType;
93 typedef typename Superclass::JacobianType JacobianType;
95 typedef typename Superclass::InputPointType PointType;
96 typedef PointType InputPointType;
97 typedef PointType OutputPointType;
99 typedef typename Superclass::InputVectorType VectorType;
100 typedef VectorType InputVectorType;
101 typedef VectorType OutputVectorType;
103 typedef typename Superclass::InputVnlVectorType VnlVectorType;
104 typedef VnlVectorType InputVnlVectorType;
105 typedef VnlVectorType OutputVnlVectorType;
107 typedef typename Superclass::InputCovariantVectorType CovariantVectorType;
108 typedef CovariantVectorType InputCovariantVectorType;
109 typedef CovariantVectorType OutputCovariantVectorType;
115 virtual PointType TransformPoint(
const PointType & x)
const
120 for (
unsigned int i = active_start_; i <= active_finish_; i++)
122 PointType tx = transform_[i]->TransformPoint(y);
123 if (tx[0] == std::numeric_limits<double>::max())
135 virtual VectorType TransformVector(
const VectorType & x)
const
140 for (
unsigned int i = active_start_; i <= active_finish_; i++)
142 y = transform_[i]->TransformVector(y);
149 virtual VnlVectorType TransformVector(
const VnlVectorType & x)
const
154 for (
unsigned int i = active_start_; i <= active_finish_; i++)
156 y = transform_[i]->TransformVector(y);
163 virtual CovariantVectorType
164 TransformCovariantVector(
const CovariantVectorType & x)
const
166 CovariantVectorType y(x);
169 for (
unsigned int i = active_start_; i < active_finish_; i++)
171 y = transform_[i]->TransformCovariantVector(y);
180 bool GetInverse(Self* inverse)
const
182 if (! inverse)
return false;
184 const unsigned int cascade_length = transform_.size();
187 inverse->transform_.resize(cascade_length);
189 for (
unsigned int i = 0; i < cascade_length; i++)
191 unsigned int idx = (cascade_length - 1) - i;
193 TransformPointer inverseTransform =
194 dynamic_cast<Superclass*
>( transform_[idx]->GetInverseTransform().GetPointer() );
195 if (inverseTransform)
197 inverse->transform_[i] = inverseTransform;
201 std::ostringstream oss;
202 oss <<
"Could not convert inverse tranform obtained from transform "
203 << i <<
" from itkTransformBase to itkTransform. Skipping transform.";
204 CORE_LOG_DEBUG(oss.str());
208 inverse->active_start_ = active_start_;
209 inverse->active_finish_ = active_finish_;
210 inverse->active_params_ = (cascade_length - 1) - active_params_;
217 virtual InverseTransformBasePointer GetInverseTransform()
const
219 Pointer inv = Self::New();
220 return this->GetInverse(inv) ? inv.GetPointer() : 0;
224 virtual void SetParameters(
const ParametersType & params)
225 { transform_[active_params_]->SetParameters(params); }
227 virtual void SetFixedParameters(
const ParametersType &)
229 itkExceptionMacro(<<
"SetFixedParameters is not implemented for CascadedTransform");
233 virtual const ParametersType & GetParameters()
const
234 {
return transform_[active_params_]->GetParameters(); }
237 virtual NumberOfParametersType GetNumberOfParameters()
const
238 {
return transform_[active_params_]->GetNumberOfParameters(); }
241 virtual const JacobianType & GetJacobian(
const PointType & pt)
const
242 {
return transform_[active_params_]->GetJacobian(pt); }
245 void append(TransformPointer transform)
247 const unsigned int old_sz = transform_.size();
248 std::vector<TransformPointer> new_array(old_sz + 1);
250 for (
unsigned int i = 0; i < old_sz; i++)
252 new_array[i] = transform_[i];
255 new_array[old_sz] = transform;
256 transform_ = new_array;
257 active_params_ = transform_.size() - 1;
258 active_finish_ = transform_.size() - 1;
262 template <
typename container_t>
263 void setup(
const container_t & transforms,
264 const unsigned int & cascade_length)
266 transform_.resize(cascade_length);
268 for (
unsigned int i = 0; i < cascade_length; i++)
270 transform_[i] = transforms[i];
274 active_finish_ = cascade_length - 1;
275 active_params_ = active_finish_;
279 template <
class transform_t> transform_t *
280 GetTransform(
const unsigned int & index)
const
281 {
return dynamic_cast<transform_t *
>(transform_[index].GetPointer()); }
284 inline const std::vector<TransformPointer> & GetTransforms()
const
285 {
return transform_; }
288 inline unsigned int GetParamsIndex()
const
289 {
return active_params_; }
291 inline void SetParamsIndex(
const unsigned int & index)
294 if (index >= transform_.size())
296 itkExceptionMacro(<<
"parameter index out of bounds");
298 active_params_ = index;
300 if (active_params_ > active_finish_)
302 itkExceptionMacro(<<
"active parameter index out of bounds");
306 inline unsigned int GetActiveIndex()
const
307 {
return active_finish_; }
309 inline void SetActiveIndex(
const unsigned int & index)
312 if (index >= transform_.size())
314 itkExceptionMacro(<<
"parameter index out of bounds");
316 active_finish_ = index;
317 active_params_ = index;
330 virtual void PrintSelf(std::ostream & os, Indent indent)
const
332 Superclass::PrintSelf(os, indent);
334 const unsigned int sz = transform_.size();
335 for (
unsigned int i = 0; i < sz; i++)
337 os << indent <<
"transform_[" << i <<
"] = "
338 << transform_[i] << std::endl;
341 os << indent <<
"active_params_ = " << active_params_ << std::endl
342 << indent <<
"active_start_ = " << active_start_ << std::endl
343 << indent <<
"active_finish_ = " << active_finish_ << std::endl;
348 CascadedTransform(
const Self & other);
349 const Self & operator = (
const Self & t);
355 std::vector<TransformPointer> transform_;
361 unsigned int active_params_;
366 unsigned int active_start_;
367 unsigned int active_finish_;
373 #endif // __itkCascadedTransform_h
Definition: itkNormalizeImageFilterWithMask.h:48