Skip to content
Snippets Groups Projects
Commit fa65c221 authored by Floris Berendsen's avatar Floris Berendsen
Browse files

WIP: initial ModuleFactory based on TransformIOFactory

parent 9c25b2e1
No related branches found
No related tags found
No related merge requests found
#ifndef itkMetricModule1_h
#define itkMetricModule1_h
#include "itkModuleBase.h"
namespace itk
{
template <typename TScalar,
unsigned int NInputDimensions = 3,
unsigned int NOutputDimensions = 3>
class MetricModule1 : public ModuleBaseTemplate< TScalar >
{
public:
/** Standard class typedefs. */
typedef MetricModule1 Self;
typedef ModuleBaseTemplate< TScalar > Superclass;
typedef SmartPointer< Self > Pointer;
typedef SmartPointer< const Self > ConstPointer;
/** New macro for creation of through the object factory. */
itkNewMacro(Self);
/** Run-time type information (and related methods). */
itkTypeMacro(MetricModule1, ModuleBaseTemplate);
/** define the Clone method */
// itkCloneMacro(Self);
/** Type of the scalar representing coordinate and vector elements. */
typedef TScalar ScalarType;
std::string GetModuleTypeAsString() const;
protected:
/**
* Clone the current Metric.
* This does a complete copy of the Metric
* state to the new Metric
*/
virtual typename LightObject::Pointer InternalClone() const ITK_OVERRIDE;
MetricModule1();
virtual ~MetricModule1()
{
}
private:
MetricModule1(const Self &); // purposely not implemented
void operator=(const Self &); // purposely not implemented
template <typename TType>
std::string GetModuleTypeAsString(TType *) const
{
std::string rval("other");
return rval;
}
std::string GetModuleTypeAsString(float *) const
{
std::string rval("float");
return rval;
}
std::string GetModuleTypeAsString(double *) const
{
std::string rval("double");
return rval;
}
};
} // end namespace itk
#ifndef ITK_MANUAL_INSTANTIATION
#include "itkMetricModule1.hxx"
#endif
#endif
/*=========================================================================
*
* 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 itkMetricModule1_hxx
#define itkMetricModule1_hxx
#include "itkMetricModule1.h"
namespace itk
{
template <typename TScalar,
unsigned int NInputDimensions,
unsigned int NOutputDimensions>
MetricModule1<TScalar, NInputDimensions, NOutputDimensions>
::MetricModule1()
{
}
template <typename TScalar,
unsigned int NInputDimensions,
unsigned int NOutputDimensions>
std::string MetricModule1<TScalar, NInputDimensions, NOutputDimensions>
::GetModuleTypeAsString() const
{
std::ostringstream n;
n << GetNameOfClass();
n << "_";
n << this->GetModuleTypeAsString(static_cast<TScalar *>(ITK_NULLPTR));
//n << "_" << this->GetInputSpaceDimension() << "_" << this->GetOutputSpaceDimension();
return n.str();
}
template <typename TScalar,
unsigned int NInputDimensions,
unsigned int NOutputDimensions>
typename LightObject::Pointer
MetricModule1<TScalar, NInputDimensions, NOutputDimensions>
::InternalClone() const
{
// Default implementation just copies the parameters from
// this to new Metric.
typename LightObject::Pointer loPtr = Superclass::InternalClone();
typename Self::Pointer rval =
dynamic_cast<Self *>(loPtr.GetPointer());
if(rval.IsNull())
{
itkExceptionMacro(<< "downcast to type "
<< this->GetNameOfClass()
<< " failed.");
}
//rval->SetFixedParameters(this->GetFixedParameters());
//rval->SetParameters(this->GetParameters());
return loPtr;
}
} // end namespace itk
#endif
/*=========================================================================
*
* 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 itkModuleBase_h
#define itkModuleBase_h
#include "itkObject.h"
#include "itkObjectFactory.h"
#include "itkIntTypes.h"
namespace itk
{
/** \class itkModuleBaseTemplate
*
* This class is an abstract class to represent a spatial Module.
*
* This class is templated over the scalar type used to store the Module's
* parameters.
*
* \ingroup ITKModule
*/
template< typename TScalar >
class ModuleBaseTemplate :public Object
{
public:
/** Standard class typedefs. */
typedef ModuleBaseTemplate Self;
typedef Object Superclass;
typedef SmartPointer< Self > Pointer;
typedef SmartPointer< const Self > ConstPointer;
/** Type of the input parameters. */
typedef TScalar ParametersValueType;
//typedef OptimizerParameters< ParametersValueType > ParametersType;
/** Run-time type information (and related methods). */
itkTypeMacro(ModuleBaseTemplate, Object);
/** Generate a platform independent name */
virtual std::string GetModuleTypeAsString() const = 0;
/*
typedef enum {
UnknownModuleCategory=0,
Linear=1,
BSpline=2,
Spline=3,
DisplacementField=4,
VelocityField=5
} ModuleCategoryType;
*/
/** Get Module category */
//virtual ModuleCategoryType GetModuleCategory() const = 0;
protected:
ModuleBaseTemplate(){}
virtual ~ModuleBaseTemplate() {}
private:
ModuleBaseTemplate(const Self &); //purposely not implemented
void operator=(const Self &); //purposely not implemented
};
} // end namespace itk
#endif
/*=========================================================================
*
* 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.
*
*=========================================================================*/
/*=========================================================================
*
* Portions of this file are subject to the VTK Toolkit Version 3 copyright.
*
* Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
*
* For complete copyright, license and disclaimer of warranty information
* please refer to the NOTICE file at the top of the ITK source tree.
*
*=========================================================================*/
#ifndef itkModuleFactory_h
#define itkModuleFactory_h
#include "itkModuleFactoryBase.h"
namespace itk
{
/** \class ModuleFactory
* \brief Create instances of Transforms
* \ingroup ITKIOTransformBase
*/
template< typename T >
class ModuleFactory:public ModuleFactoryBase
{
public:
static void RegisterModule()
{
typename T::Pointer t = T::New();
ModuleFactoryBase::Pointer f = ModuleFactoryBase::GetFactory();
f->RegisterModule ( t->GetModuleTypeAsString().c_str(),
t->GetModuleTypeAsString().c_str(),
t->GetModuleTypeAsString().c_str(),
1,
CreateObjectFunction< T >::New() );
}
};
} // end namespace itk
#endif
/*=========================================================================
*
* 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.
*
*=========================================================================*/
#include "itkModuleFactory.h"
#include "itkVersion.h"
#include "itkTransformModule1.h"
#include "itkMetricModule1.h"
namespace itk
{
ModuleFactoryBase *ModuleFactoryBase:: m_Factory = ITK_NULLPTR;
namespace ModuleFactoryBasePrivate
{
bool DefaultModulesRegistered = false;
}
ModuleFactoryBase::ModuleFactoryBase()
{}
ModuleFactoryBase::~ModuleFactoryBase()
{}
void ModuleFactoryBase::RegisterDefaultModules()
{
//
// make sure that the the factory instance has
// been created. All normal paths to this method
// already do this but this makes certain sure it's done
(void)ModuleFactoryBase::GetFactory();
if ( !ModuleFactoryBasePrivate::DefaultModulesRegistered )
{
//
// double instances (in alphabetical order)
//
ModuleFactory< TransformModule1< double, 2 > >::RegisterModule ();
ModuleFactory< MetricModule1< double, 2 > >::RegisterModule ();
}
ModuleFactoryBasePrivate::DefaultModulesRegistered = true;
}
const char *
ModuleFactoryBase::GetITKSourceVersion(void) const
{
return ITK_SOURCE_VERSION;
}
const char *
ModuleFactoryBase::GetDescription() const
{
return "Module FactoryBase";
}
} // end namespace itk
/*=========================================================================
*
* 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.
*
*=========================================================================*/
/*=========================================================================
*
* Portions of this file are subject to the VTK Toolkit Version 3 copyright.
*
* Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
*
* For complete copyright, license and disclaimer of warranty information
* please refer to the NOTICE file at the top of the ITK source tree.
*
*=========================================================================*/
#ifndef itkModuleFactoryBase_h
#define itkModuleFactoryBase_h
#include "itkObjectFactoryBase.h"
namespace itk
{
/** \class ModuleFactoryBase
* \brief Create instances of Modules
* \ingroup ITKIOModuleBase
*/
class ModuleFactoryBase :public ObjectFactoryBase
{
public:
/** Standard class typedefs. */
typedef ModuleFactoryBase Self;
typedef ObjectFactoryBase Superclass;
typedef SmartPointer< Self > Pointer;
typedef SmartPointer< const Self > ConstPointer;
/** Class methods used to interface with the registered factories. */
virtual const char * GetITKSourceVersion(void) const ITK_OVERRIDE;
virtual const char * GetDescription(void) const ITK_OVERRIDE;
/** Run-time type information (and related methods). */
itkTypeMacro(ModuleFactoryBase, ObjectFactoryBase);
/** Method for class instantiation. */
itkFactorylessNewMacro(Self);
/** Register all builtin Modules */
static void RegisterDefaultModules();
/** Register this Module */
static ModuleFactoryBase * GetFactory()
{
if (m_Factory == ITK_NULLPTR)
{
// Make and register the factory
Pointer p = New();
m_Factory = p.GetPointer();
ObjectFactoryBase::RegisterFactory(p);
p->RegisterDefaultModules();
}
return m_Factory;
}
void RegisterModule(const char *classOverride,
const char *overrideClassName,
const char *description,
bool enableFlag,
CreateObjectFunctionBase *createFunction)
{
this->RegisterOverride(classOverride, overrideClassName, description, enableFlag, createFunction);
}
protected:
ModuleFactoryBase();
virtual ~ModuleFactoryBase();
private:
ModuleFactoryBase(const Self &); //purposely not implemented
void operator=(const Self &); //purposely not implemented
static ModuleFactoryBase *m_Factory;
};
} // end namespace itk
#endif
#ifndef itkTransformModule1_h
#define itkTransformModule1_h
#include "itkModuleBase.h"
namespace itk
{
template <typename TScalar,
unsigned int NInputDimensions = 3,
unsigned int NOutputDimensions = 3>
class TransformModule1 : public ModuleBaseTemplate< TScalar >
{
public:
/** Standard class typedefs. */
typedef TransformModule1 Self;
typedef ModuleBaseTemplate< TScalar > Superclass;
typedef SmartPointer< Self > Pointer;
typedef SmartPointer< const Self > ConstPointer;
/** New macro for creation of through the object factory. */
itkNewMacro(Self);
/** Run-time type information (and related methods). */
itkTypeMacro(TransformModule1, ModuleBaseTemplate);
/** define the Clone method */
//itkCloneMacro(Self);
/** Type of the scalar representing coordinate and vector elements. */
typedef TScalar ScalarType;
/** Dimension of the domain space. */
itkStaticConstMacro(InputSpaceDimension, unsigned int, NInputDimensions);
itkStaticConstMacro(OutputSpaceDimension, unsigned int, NOutputDimensions);
/** Get the size of the input space */
unsigned int GetInputSpaceDimension(void) const ITK_OVERRIDE
{
return NInputDimensions;
}
/** Get the size of the output space */
unsigned int GetOutputSpaceDimension(void) const ITK_OVERRIDE
{
return NOutputDimensions;
}
/** Standard coordinate point type for this class */
//typedef Point<TScalar, NInputDimensions> InputPointType;
//typedef Point<TScalar, NOutputDimensions> OutputPointType;
//virtual OutputPointType TransformPoint(const InputPointType &) const = 0;
virtual std::string GetModuleTypeAsString() const;
protected:
/**
* Clone the current transform.
* This does a complete copy of the transform
* state to the new transform
*/
virtual typename LightObject::Pointer InternalClone() const ITK_OVERRIDE;
TransformModule1();
virtual ~TransformModule1()
{
}
private:
TransformModule1(const Self &); // purposely not implemented
void operator=(const Self &); // purposely not implemented
template <typename TType>
std::string GetModuleTypeAsString(TType *) const
{
std::string rval("other");
return rval;
}
std::string GetModuleTypeAsString(float *) const
{
std::string rval("float");
return rval;
}
std::string GetModuleTypeAsString(double *) const
{
std::string rval("double");
return rval;
}
};
} // end namespace itk
#ifndef ITK_MANUAL_INSTANTIATION
#include "itkTransformModule1.hxx"
#endif
#endif
/*=========================================================================
*
* 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 itkTransformModule1_hxx
#define itkTransformModule1_hxx
#include "itkTransformModule1.h"
namespace itk
{
template <typename TScalar,
unsigned int NInputDimensions,
unsigned int NOutputDimensions>
TransformModule1<TScalar, NInputDimensions, NOutputDimensions>
::TransformModule1()
{
}
template <typename TScalar,
unsigned int NInputDimensions,
unsigned int NOutputDimensions>
std::string TransformModule1<TScalar, NInputDimensions, NOutputDimensions>
::GetModuleTypeAsString() const
{
std::ostringstream n;
n << GetNameOfClass();
n << "_";
n << this->GetModuleTypeAsString(static_cast<TScalar *>(ITK_NULLPTR) );
n << "_" << this->GetInputSpaceDimension() << "_" << this->GetOutputSpaceDimension();
return n.str();
}
template <typename TScalar,
unsigned int NInputDimensions,
unsigned int NOutputDimensions>
typename LightObject::Pointer
TransformModule1<TScalar, NInputDimensions, NOutputDimensions>
::InternalClone() const
{
// Default implementation just copies the parameters from
// this to new transform.
typename LightObject::Pointer loPtr = Superclass::InternalClone();
typename Self::Pointer rval =
dynamic_cast<Self *>(loPtr.GetPointer());
if(rval.IsNull())
{
itkExceptionMacro(<< "downcast to type "
<< this->GetNameOfClass()
<< " failed.");
}
//rval->SetFixedParameters(this->GetFixedParameters());
//rval->SetParameters(this->GetParameters());
return loPtr;
}
} // end namespace itk
#endif
/*=========================================================================
*
* 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.
*
*=========================================================================*/
#include "itkImageFileReader.h"
#include "itkImageFileWriter.h"
#include "itkDisplacementFieldTransform.h"
#include "itkComposeDisplacementFieldsImageFilter.h"
#include "itkCompositeTransform.h"
#include "itkVector.h"
#include "itkPoint.h"
#include "itkTransformFileReader.h"
#include "itkTransformFactoryBase.h"
#include "itkTransformFactory.h"
#include "itkMatrixOffsetTransformBase.h"
//Floris: The module factory is based on the transformIO factory.
// By default it creates 2 dummy modules: itkTransformModule1 and itkMetricModule1
#include "itkModuleFactoryBase.h"
#include "itkModuleFactory.h"
int main(int argc, char *argv[])
{
std::string fileName;
if (argc == 1) // No arguments were provided
{
fileName = "test.tfm";
}
else
{
fileName = argv[1];
}
typedef itk::MatrixOffsetTransformBase< double, 3, 3 > MatrixOffsetTransformType;
itk::TransformFactory<MatrixOffsetTransformType>::RegisterTransform();
#if (ITK_VERSION_MAJOR == 4 && ITK_VERSION_MINOR >= 5) || ITK_VERSION_MAJOR > 4
itk::TransformFileReaderTemplate<float>::Pointer reader =
itk::TransformFileReaderTemplate<float>::New();
#else
itk::TransformFileReader::Pointer reader = itk::TransformFileReader::New();
#endif
reader->SetFileName(fileName);
reader->Update();
std::cout << *(reader->GetTransformList()->begin()) << std::endl;
return EXIT_SUCCESS;
}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment