selxNiftyregf3dComponent.hxx 3.55 KB
Newer Older
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
/*=========================================================================
 *
 *  Copyright Leiden University Medical Center, Erasmus University Medical
 *  Center and contributors
 *
 *  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 "selxNiftyregf3dComponent.h"
#include "selxCheckTemplateProperties.h"

namespace selx
{
template< class TPixel >
Kasper Marstal's avatar
Kasper Marstal committed
26
Niftyregf3dComponent< TPixel >::Niftyregf3dComponent( const std::string & name ) : Superclass( name )
27
{
Floris Berendsen's avatar
Floris Berendsen committed
28
  m_reg_f3d = new reg_f3d< TPixel >( 1, 1 );
29
30
31
32
33
34
}


template< class TPixel >
Niftyregf3dComponent< TPixel >::~Niftyregf3dComponent()
{
Floris Berendsen's avatar
Floris Berendsen committed
35
  delete m_reg_f3d;
36
37
38
39
40
41
}


template< class TPixel >
int
Niftyregf3dComponent< TPixel >
42
::Set( typename NiftyregReferenceImageInterface< TPixel >::Pointer component )
43
{
44
45
  // store the shared_ptr to the data, otherwise it gets freed
  this->m_reference_image = component->GetReferenceNiftiImage();
46
  // connect the itk pipeline
47
  this->m_reg_f3d->SetReferenceImage(this->m_reference_image.get());
48
49
50
  return 0;
}

Floris Berendsen's avatar
Floris Berendsen committed
51

52
53
54
template< class TPixel >
int
Niftyregf3dComponent< TPixel >
Floris Berendsen's avatar
Floris Berendsen committed
55
::Set( typename NiftyregFloatingImageInterface< TPixel >::Pointer component )
56
{
57
58
  // store the shared_ptr to the data, otherwise it gets freed
  this->m_floating_image = component->GetFloatingNiftiImage();
59
  // connect the itk pipeline
60
  this->m_reg_f3d->SetFloatingImage(this->m_floating_image.get());
61
62
63
  return 0;
}

64
65
66
67
68
69
70
71
template< class TPixel >
std::shared_ptr<nifti_image>
Niftyregf3dComponent< TPixel >
::GetWarpedNiftiImage()
{
  return (*(this->m_warped_images.get()))[0];
}

Floris Berendsen's avatar
Floris Berendsen committed
72
73
74
75
76
77

template< class TPixel >
void
Niftyregf3dComponent<  TPixel >
::RunRegistration()
{
78
79
  //this->m_reg_f3d->UseSSD( 0, true );
  //this->m_reg_f3d->UseCubicSplineInterpolation();
80

Floris Berendsen's avatar
Floris Berendsen committed
81
  this->m_reg_f3d->Run();
82
  nifti_image** outputWarpedImage = m_reg_f3d->GetWarpedImage();
83
84
85
86
87
88
89
90
  memset(outputWarpedImage[0]->descrip, 0, 80);
  strcpy(outputWarpedImage[0]->descrip, "Warped image using NiftyReg (reg_f3d)");

  //encapsulate malloc-ed pointer in a smartpointer for proper memory ownership
  this->m_warped_images = std::unique_ptr< std::array<std::shared_ptr<nifti_image>, 2>>(new std::array<std::shared_ptr<nifti_image>, 2>);
  (*(this->m_warped_images.get()))[0] = std::shared_ptr<nifti_image>(outputWarpedImage[0], nifti_image_free);
  (*(this->m_warped_images.get()))[1] = std::shared_ptr<nifti_image>(outputWarpedImage[1], nifti_image_free);

91
  // m_reg_f3d->GetWarpedImage() malloc-ed the container which we must free ourselves.
92
93
94
  free(outputWarpedImage);
  outputWarpedImage = NULL;

Floris Berendsen's avatar
Floris Berendsen committed
95
}
Floris Berendsen's avatar
Floris Berendsen committed
96
97


98
99
100
101
102
template< class TPixel >
bool
Niftyregf3dComponent<  TPixel >
::MeetsCriterion( const ComponentBase::CriterionType & criterion )
{
Floris Berendsen's avatar
Floris Berendsen committed
103
104
105
106
  bool hasUndefinedCriteria( false );
  bool meetsCriteria( false );
  auto status = CheckTemplateProperties( this->TemplateProperties(), criterion );
  if( status == CriterionStatus::Satisfied )
107
108
109
  {
    return true;
  }
Floris Berendsen's avatar
Floris Berendsen committed
110
  else if( status == CriterionStatus::Failed )
111
112
113
114
115
116
117
  {
    return false;
  } // else: CriterionStatus::Unknown

  return meetsCriteria;
}
} //end namespace selx