1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324
|
/*=========================================================================
*
* Copyright Insight Software Consortium
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0.txt
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*=========================================================================*/
#ifndef itkMINCTransformAdapter_h
#define itkMINCTransformAdapter_h
#include "itkObject.h"
#include "itkPoint.h"
#include "itkVector.h"
#include "itkCovariantVector.h"
#include "vnl/vnl_matrix_fixed.h"
#include "vnl/vnl_vector_fixed.h"
#include "vnl/vnl_det.h"
#include "vnl/vnl_vector_fixed_ref.h"
#include "vnl/vnl_vector.h"
#include "itkTransform.h"
#include "itkObjectFactory.h"
//minc header
#include "itk_minc2.h"
namespace itk
{
/** \class MINCTransformAdapter
* \ingroup ITKIOTransformMINC
* \brief ITK wrapper around MINC general transform functions, supports all the transformations that MINC XFM supports
*
* \author Vladimir S. FONOV
* Brain Imaging Center, Montreal Neurological Institute, McGill University, Montreal Canada 2012
* \ingroup ITKIOTransformMINC
*/
template<typename TParametersValueType=double, unsigned int NInputDimensions=3,unsigned int NOutputDimensions=3>
class MINCTransformAdapter : public Transform<TParametersValueType, NInputDimensions, NOutputDimensions>
{
public:
/** Standard class typedefs. */
typedef MINCTransformAdapter Self;
typedef Transform<TParametersValueType, NInputDimensions, NOutputDimensions> Superclass;
typedef SmartPointer<Self> Pointer;
typedef SmartPointer<const Self> ConstPointer;
typedef typename Superclass::NumberOfParametersType NumberOfParametersType;
/** New method for creating an object using a factory. */
itkNewMacro(Self);
/** Run-time type information (and related methods). */
itkTypeMacro( MINCTransformAdapter, Transform );
/** Dimension of the domain space. */
itkStaticConstMacro(InputSpaceDimension, unsigned int, NInputDimensions);
itkStaticConstMacro(OutputSpaceDimension, unsigned int, NOutputDimensions);
/** Type of the input parameters. */
typedef double ScalarType;
/** Type of the input parameters. */
typedef typename Superclass::ParametersType ParametersType;
typedef typename Superclass::FixedParametersType FixedParametersType;
/** Type of the Jacobian matrix. */
typedef typename Superclass::JacobianType JacobianType;
/** Standard vector type for this class. */
typedef Vector<TParametersValueType, itkGetStaticConstMacro(InputSpaceDimension)> InputVectorType;
typedef Vector<TParametersValueType, itkGetStaticConstMacro(OutputSpaceDimension)> OutputVectorType;
/** Standard variable length vector type for this class
* this provides an interface for the VectorImage class */
typedef VariableLengthVector<TParametersValueType> InputVectorPixelType;
typedef VariableLengthVector<TParametersValueType> OutputVectorPixelType;
/** Standard covariant vector type for this class */
typedef CovariantVector<TParametersValueType, itkGetStaticConstMacro(InputSpaceDimension)> InputCovariantVectorType;
typedef CovariantVector<TParametersValueType, itkGetStaticConstMacro(OutputSpaceDimension)> OutputCovariantVectorType;
/** Standard coordinate point type for this class */
typedef Point<TParametersValueType,NInputDimensions > InputPointType;
typedef Point<TParametersValueType,NInputDimensions > OutputPointType;
/** Standard vnl_vector type for this class. */
typedef vnl_vector_fixed<TParametersValueType, NInputDimensions> InputVnlVectorType;
typedef vnl_vector_fixed<TParametersValueType, NOutputDimensions> OutputVnlVectorType;
/** Method to transform a point. */
virtual OutputPointType TransformPoint(const InputPointType &point ) const ITK_OVERRIDE
{
if(!m_Initialized)
{
return point;
}
if(m_Invert && !m_Initialized_invert)
{
return point;
}
OutputPointType pnt;
//works only for 3D->3D transforms
general_transform_point((m_Invert ? &m_Xfm_inv : &m_Xfm), point[0], point[1], point[2], &pnt[0], &pnt[1], &pnt[2]);
return pnt;
}
//! use finate element difference to estimate local jacobian
void estimate_local_jacobian(const InputPointType &orig, vnl_matrix_fixed< double, 3, 3 > &m)
{
double u1,v1,w1;
double u2,v2,w2;
const double delta=1e-4;
general_transform_point((m_Invert ? &m_Xfm_inv : &m_Xfm) , orig[0]-delta, orig[1], orig[2],&u1, &v1, &w1);
general_transform_point((m_Invert ? &m_Xfm_inv : &m_Xfm) , orig[0]+delta, orig[1], orig[2],&u2, &v2, &w2);
m(0,0)=(u2-u1)/(2*delta);
m(0,1)=(v2-v1)/(2*delta);
m(0,2)=(w2-w1)/(2*delta);
general_transform_point((m_Invert ? &m_Xfm_inv : &m_Xfm) , orig[0], orig[1]-delta, orig[2],&u1, &v1, &w1);
general_transform_point((m_Invert ? &m_Xfm_inv : &m_Xfm) , orig[0], orig[1]+delta, orig[2],&u2, &v2, &w2);
m(1,0)=(u2-u1)/(2*delta);
m(1,1)=(v2-v1)/(2*delta);
m(1,2)=(w2-w1)/(2*delta);
general_transform_point((m_Invert ? &m_Xfm_inv : &m_Xfm), orig[0], orig[1], orig[2]-delta,&u1, &v1, &w1);
general_transform_point((m_Invert ? &m_Xfm_inv : &m_Xfm), orig[0], orig[1], orig[2]+delta,&u2, &v2, &w2);
m(2,0)=(u2-u1)/(2*delta);
m(2,1)=(v2-v1)/(2*delta);
m(2,2)=(w2-w1)/(2*delta);
}
/** Method to transform a vector. */
OutputVectorType TransformVector( const InputVectorType& vector, const InputPointType & ) const ITK_OVERRIDE
{
itkExceptionMacro( << "Not Implemented" );
return vector;
}
/** Method to transform a vector. */
OutputVnlVectorType TransformVector( const InputVnlVectorType& vector, const InputPointType & ) const ITK_OVERRIDE
{
itkExceptionMacro( << "Not Implemented" );
return vector;
}
/** Method to transform a vector. */
OutputVectorType TransformVector( const InputVectorType& vector) const ITK_OVERRIDE
{
return Superclass::TransformVector(vector);
}
/** Method to transform a vector. */
OutputVnlVectorType TransformVector( const InputVnlVectorType& vector) const ITK_OVERRIDE
{
return Superclass::TransformVector(vector);
}
/** Method to transform a vector. */
OutputVectorPixelType TransformVector( const InputVectorPixelType& vector) const ITK_OVERRIDE
{
return Superclass::TransformVector(vector);
}
/** Method to transform a vector. */
OutputVectorPixelType TransformVector(
const InputVectorPixelType& vector,
const InputPointType & ) const ITK_OVERRIDE
{
itkExceptionMacro( << "Not Implemented" );
return vector;
}
/** Method to transform a CovariantVector. */
virtual OutputCovariantVectorType TransformCovariantVector(
const InputCovariantVectorType &vector
, const InputPointType & ) const ITK_OVERRIDE
{
itkExceptionMacro( << "Not Implemented" );
return vector;
}
/** Method to transform a CovariantVector. */
virtual OutputCovariantVectorType TransformCovariantVector(
const InputCovariantVectorType &vector) const ITK_OVERRIDE
{
return Superclass::TransformCovariantVector(vector);
}
/** Method to transform a CovariantVector. */
virtual OutputVectorPixelType TransformCovariantVector(
const InputVectorPixelType &vector) const ITK_OVERRIDE
{
return Superclass::TransformCovariantVector(vector);
}
/** Method to transform a CovariantVector. */
virtual OutputVectorPixelType TransformCovariantVector(
const InputVectorPixelType &vector, const InputPointType & ) const ITK_OVERRIDE
{
itkExceptionMacro( << "Not Implemented" );
return vector;
}
/** Set the transformation to an Identity
*/
virtual void SetIdentity( void )
{
cleanup();
}
virtual void SetFixedParameters(const FixedParametersType &) ITK_OVERRIDE
{
itkExceptionMacro( << "Not Implemented" );
}
virtual void ComputeJacobianWithRespectToParameters(
const InputPointType &,
JacobianType &) const ITK_OVERRIDE
{
itkExceptionMacro( << "Not Implemented" );
}
virtual NumberOfParametersType GetNumberOfParameters(void) const ITK_OVERRIDE
{
//this transform is defined by XFM file
itkExceptionMacro( << "Not Defined" );
return 0;
}
/** Set the Transformation Parameters
* and update the internal transformation. */
virtual void SetParameters(const ParametersType &) ITK_OVERRIDE
{
itkExceptionMacro( << "Not Implemented" );
}
virtual const ParametersType & GetParameters(void) const ITK_OVERRIDE
{
itkExceptionMacro( << "Not Implemented" );
return m_Parameters;
}
void OpenXfm(const char *xfm)
{
cleanup();
if(input_transform_file(xfm, &m_Xfm) != VIO_OK)
itkExceptionMacro( << "Error reading XFM:" << xfm );
m_Initialized=true;
}
void Invert(void)
{
if(!m_Initialized)
itkExceptionMacro( << "XFM not initialized" );
if(!m_Initialized_invert)
{
create_inverse_general_transform(&m_Xfm,&m_Xfm_inv);
m_Initialized_invert=true;
}
m_Invert= !m_Invert;
}
protected:
MINCTransformAdapter():
Transform<TParametersValueType, NInputDimensions, NOutputDimensions>(0),
m_Invert(false),
m_Initialized(false),
m_Initialized_invert(false)
{
if(NInputDimensions!=3 || NOutputDimensions!=3)
itkExceptionMacro(<< "Sorry, only 3D to 3d minc xfm transform is currently implemented");
}
virtual ~MINCTransformAdapter() ITK_OVERRIDE
{
cleanup();
}
void cleanup(void)
{
if(m_Initialized)
{
delete_general_transform(&m_Xfm);
}
if(m_Initialized_invert)
{
delete_general_transform(&m_Xfm_inv);
}
m_Initialized=false;
m_Initialized_invert=false;
}
ParametersType m_Parameters;
mutable VIO_General_transform m_Xfm;
mutable VIO_General_transform m_Xfm_inv;
bool m_Invert;
bool m_Initialized;
bool m_Initialized_invert;
private:
ITK_DISALLOW_COPY_AND_ASSIGN(MINCTransformAdapter );
};
}
#endif //itkMINCTransformAdapter_h
|