Commit 787f18ae authored by Kasper Marstal's avatar Kasper Marstal
Browse files

ENH: Use itkSharedPointerDataObjectDecorator

parent e6409934
/*=========================================================================
*
* 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 selxSharedPointerDataObjectDecorator_h
#define selxSharedPointerDataObjectDecorator_h
#include <memory>
#include "itkDataObject.h"
#include "itkObjectFactory.h"
namespace itk
{
/** \class AutoPointerDataObjectDecorator
* \brief Decorates any pointer to a simple object with a DataObject API using
* AutoPointer semantics.
*
* AutoPointerDataObjectDecorator decorates a pointer to an object
* with a DataObject API. This allows a pointer to an object to be
* encapsulated in a DataObject and passed through the pipeline. This
* object differs from SimpleDataObjectDecorator in that the decorator
* takes control of deleting the pointer upon destruction.
*
* The decorator provides two methods Set() and Get() to access the
* decorated object (referred internally as the component).
*
* Note that when an instance of SimpleDataObjectDecorator is created,
* the component is initialized with its default constructor (i.e. a
* null pointer).
*
* \sa SimpleDataObjectDecorator
* \sa DataObjectDecorator
* \ingroup ITKSystemObjects
*
* \ingroup ITKCommon
*/
template< typename T >
class SharedPointerDataObjectDecorator:public DataObject
{
public:
/** Standard typedefs. */
typedef SharedPointerDataObjectDecorator Self;
typedef DataObject Superclass;
typedef SmartPointer< Self > Pointer;
typedef SmartPointer< const Self > ConstPointer;
/** Typedef for the component type (object being decorated) */
typedef T ComponentType;
typedef std::shared_ptr< T > ComponentPointer;
/** Method for creation through the object factory. */
itkNewMacro(Self);
/** Run-time type information (and related methods). */
itkTypeMacro(SharedPointerDataObjectDecorator, DataObject);
/** Set the contained object */
virtual void Set(std::shared_ptr< T > val);
/** Get the contained object */
virtual std::shared_ptr< T > Get() { return m_Component; }
virtual const std::shared_ptr< T > Get() const { return m_Component; }
protected:
SharedPointerDataObjectDecorator();
~SharedPointerDataObjectDecorator();
virtual void PrintSelf(std::ostream & os, Indent indent) const ITK_OVERRIDE;
protected:
private:
// ITK_DISALLOW_COPY_AND_ASSIGN(SharedPointerDataObjectDecorator);
ComponentPointer m_Component;
};
} // end namespace itk
#ifndef ITK_MANUAL_INSTANTIATION
#include "itkSharedPointerDataObjectDecorator.hxx"
#endif
#endif
\ No newline at end of file
/*=========================================================================
*
* 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 selxSharedPointerDataObjectDecorator_hxx
#define selxSharedPointerDataObjectDecorator_hxx
#include "itkSharedPointerDataObjectDecorator.h"
namespace itk
{
/** Constructor */
template< typename T >
SharedPointerDataObjectDecorator< T >
::SharedPointerDataObjectDecorator():m_Component()
{}
/** Destructor */
template< typename T >
SharedPointerDataObjectDecorator< T >
::~SharedPointerDataObjectDecorator()
{}
/** Set value */
template< typename T >
void
SharedPointerDataObjectDecorator< T >
::Set(std::shared_ptr< T > val)
{
if ( m_Component != val )
{
// store the pointer and take ownership of the memory
ComponentPointer aPointer(val);
m_Component = aPointer;
this->Modified();
}
}
/** PrintSelf method */
template< typename T >
void
SharedPointerDataObjectDecorator< T >
::PrintSelf(std::ostream & os, Indent indent) const
{
Superclass::PrintSelf(os, indent);
os << indent << "Component: " << typeid( m_Component ).name() << std::endl;
}
} // end namespace itk
#endif
\ No newline at end of file
......@@ -25,7 +25,7 @@
#include "selxOverlord.h"
#include "selxAnyFileReader.h"
#include "selxAnyFileWriter.h"
#include "itkAutoPointerDataObjectDecorator.h"
#include "itkSharedPointerDataObjectDecorator.h"
/**
* \class SuperElastixFilter
......@@ -59,9 +59,9 @@ public:
typedef std::unique_ptr< Overlord > OverlordPointer;
typedef typename itk::AutoPointerDataObjectDecorator< Blueprint > BlueprintType;
typedef BlueprintType::Pointer BlueprintPointer;
typedef BlueprintType::ConstPointer BlueprintConstPointer;
typedef typename itk::SharedPointerDataObjectDecorator< Blueprint > BlueprintType;
typedef BlueprintType::Pointer BlueprintPointer;
typedef BlueprintType::ConstPointer BlueprintConstPointer;
// TODO: Make const-correct
itkSetObjectMacro( Blueprint, BlueprintType )
......@@ -93,7 +93,7 @@ protected:
private:
//TODO make const correct
BlueprintType::Pointer m_Blueprint;
BlueprintType::Pointer m_Blueprint;
OverlordPointer m_Overlord;
bool m_InputConnectionModified;
bool m_OutputConnectionModified;
......
......@@ -18,6 +18,7 @@
*=========================================================================*/
#include "selxSuperElastixFilter.h"
#include "itkSharedPointerDataObjectDecorator.h"
#include "selxItkSmoothingRecursiveGaussianImageFilterComponent.h"
#include "selxItkImageSink.h"
......@@ -65,7 +66,7 @@ public:
ItkMeshSinkComponent< 2, float >,
ItkMeshSourceComponent< 2, float > > RegisterComponents;
typedef std::unique_ptr< Blueprint > BlueprintPointer;
typedef std::shared_ptr< Blueprint > BlueprintPointer;
typedef SuperElastixFilter< RegisterComponents > SuperElastixFilterType;
typedef SuperElastixFilterType::Pointer SuperElastixFilterPointer;
......@@ -110,13 +111,13 @@ TEST_F( SuperElastixFilterTest, ImageOnly )
EXPECT_NO_THROW( superElastix = SuperElastixFilterType::New() );
SuperElastixFilterBlueprintPointer superElastixBlueprint = SuperElastixFilterBlueprintType::New();
superElastixBlueprint->Set( blueprint.get() );
superElastixBlueprint->Set( blueprint );
superElastix->SetBlueprint( superElastixBlueprint );
superElastix->SetInput( "InputImage", imageReader3D->GetOutput() );
imageWriter3D->SetInput( superElastix->GetOutput< Image3DType >( "OutputImage" ) );
EXPECT_NO_THROW( imageWriter3D->Update() );
imageWriter3D->Update();
}
TEST_F( SuperElastixFilterTest, ImageAndMesh )
{
......@@ -150,7 +151,7 @@ TEST_F( SuperElastixFilterTest, ImageAndMesh )
EXPECT_NO_THROW( superElastix = SuperElastixFilterType::New() );
SuperElastixFilterBlueprintPointer superElastixFilterBlueprint = SuperElastixFilterBlueprintType::New();
superElastixFilterBlueprint->Set( blueprint.get() );
superElastixFilterBlueprint->Set( blueprint );
superElastix->SetBlueprint( superElastixFilterBlueprint );
superElastix->SetInput( "InputMesh", meshReader->GetOutput() );
......@@ -172,19 +173,23 @@ TEST_F( SuperElastixFilterTest, TooManyInputs )
imageReader3D_A->SetFileName( dataManager->GetInputFile( "sphereA3d.mhd" ) );
imageReader3D_B->SetFileName( dataManager->GetInputFile( "sphereA3d.mhd" ) );
std::cout << "Create blueprint " << std::endl;
BlueprintPointer blueprint = BlueprintPointer( new Blueprint() );
blueprint->SetComponent( "Source_A", { { "NameOfClass", { "ItkImageSourceComponent" } } } );
SuperElastixFilterType::Pointer superElastix;
EXPECT_NO_THROW( superElastix = SuperElastixFilterType::New() );
SuperElastixFilterBlueprintPointer superElastixFilterBlueprint = SuperElastixFilterBlueprintType::New();
superElastixFilterBlueprint->Set( blueprint.get() );
superElastixFilterBlueprint->Set( blueprint );
superElastix->SetBlueprint( superElastixFilterBlueprint );
std::cout << "Set inputs " << std::endl;
superElastix->SetInput( "Source_A", imageReader3D_A->GetOutput() );
superElastix->SetInput( "Source_B", imageReader3D_B->GetOutput() );
std::cout << "Update " << std::endl;
EXPECT_THROW( superElastix->Update(), itk::ExceptionObject );
}
TEST_F( SuperElastixFilterTest, TooManySources )
......@@ -203,8 +208,8 @@ TEST_F( SuperElastixFilterTest, TooManySources )
SuperElastixFilterType::Pointer superElastixFilter;
EXPECT_NO_THROW( superElastixFilter = SuperElastixFilterType::New() );
itk::AutoPointerDataObjectDecorator< Blueprint >::Pointer superElastixFilterBlueprint = itk::AutoPointerDataObjectDecorator< Blueprint >::New();
superElastixFilterBlueprint->Set( blueprint.get() );
SuperElastixFilterBlueprintPointer superElastixFilterBlueprint = SuperElastixFilterBlueprintType::New();
superElastixFilterBlueprint->Set( blueprint );
EXPECT_NO_THROW( superElastixFilter->SetBlueprint( superElastixFilterBlueprint ) );
superElastixFilter->SetInput( "Source_A", imageReader3D_A->GetOutput() );
......@@ -226,7 +231,7 @@ TEST_F( SuperElastixFilterTest, TooManyOutputs )
SuperElastixFilterPointer superElastixFilter = SuperElastixFilterType::New();
SuperElastixFilterBlueprintPointer superElastixFilterBlueprint = SuperElastixFilterBlueprintType::New();
superElastixFilterBlueprint->Set( blueprint.get() );
superElastixFilterBlueprint->Set( blueprint );
superElastixFilter->SetBlueprint( superElastixFilterBlueprint );
imageWriter3D_A->SetInput( superElastixFilter->GetOutput< Image3DType >( "Sink_A" ) );
......@@ -250,7 +255,7 @@ TEST_F( SuperElastixFilterTest, TooManySinks )
EXPECT_NO_THROW( superElastixFilter = SuperElastixFilterType::New() );
SuperElastixFilterBlueprintPointer superElastixFilterBlueprint = SuperElastixFilterBlueprintType::New();
superElastixFilterBlueprint->Set( blueprint.get() );
superElastixFilterBlueprint->Set( blueprint );
superElastixFilter->SetBlueprint( superElastixFilterBlueprint );
imageWriter3D_A->SetInput( superElastixFilter->GetOutput< Image3DType >( "Sink_A" ) );
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment