1 /*
2  * Medical Image Registration ToolKit (MIRTK)
3  *
4  * Copyright 2013-2015 Imperial College London
5  * Copyright 2013-2015 Andreas Schuh
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *     http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */
19 
20 #include "mirtk/VelocityToDisplacementFieldEuler.h"
21 
22 #include "mirtk/Parallel.h"
23 #include "mirtk/NaryVoxelFunction.h"
24 
25 
26 namespace mirtk {
27 
28 
29 // ===========================================================================
30 // Construction/Destruction
31 // ===========================================================================
32 
33 // ---------------------------------------------------------------------------
34 template <class VoxelType>
VelocityToDisplacementFieldEuler()35 VelocityToDisplacementFieldEuler<VoxelType>::VelocityToDisplacementFieldEuler()
36 :
37   VelocityToDisplacementField<VoxelType>(),
38   _VelocityInterpolator(NULL)
39 {
40 }
41 
42 // ---------------------------------------------------------------------------
43 template <class VoxelType>
~VelocityToDisplacementFieldEuler()44 VelocityToDisplacementFieldEuler<VoxelType>::~VelocityToDisplacementFieldEuler()
45 {
46   delete _VelocityInterpolator;
47 }
48 
49 // ===========================================================================
50 // Filter implementation
51 // ===========================================================================
52 
53 // ---------------------------------------------------------------------------
54 template <class VoxelType>
Initialize()55 void VelocityToDisplacementFieldEuler<VoxelType>::Initialize()
56 {
57   // Initialize base class
58   VelocityToDisplacementField<VoxelType>::Initialize();
59 
60   // Initialize interpolator
61   if (_VelocityInterpolator) delete _VelocityInterpolator;
62   _VelocityInterpolator = InterpolateImageFunction::New(this->Interpolation(),
63                                                         this->Extrapolation(),
64                                                         this->Input());
65   _VelocityInterpolator->Input(this->Input());
66   _VelocityInterpolator->Initialize();
67 }
68 
69 // ---------------------------------------------------------------------------
70 template <class VoxelType>
Run()71 void VelocityToDisplacementFieldEuler<VoxelType>::Run()
72 {
73   this->Initialize();
74 
75   using NaryVoxelFunction::ExpVelocityFieldEuler2D;
76   using NaryVoxelFunction::ExpVelocityFieldEuler3D;
77   const ImageAttributes &grid = this->Output()->Attributes();
78 
79   if (this->Output()->Z() > 1) {
80     ExpVelocityFieldEuler3D<> exp(_VelocityInterpolator, this->NumberOfSteps(), this->UpperIntegrationLimit());
81     if (this->Input(1)) ParallelForEachVoxel(grid, this->Input(1), this->Output(), exp);
82     else                ParallelForEachVoxel(grid,                 this->Output(), exp);
83   } else {
84     ExpVelocityFieldEuler2D<> exp(_VelocityInterpolator, this->NumberOfSteps(), this->UpperIntegrationLimit());
85     if (this->Input(1)) ParallelForEachVoxel(grid, this->Input(1), this->Output(), exp);
86     else                ParallelForEachVoxel(grid,                 this->Output(), exp);
87   }
88 
89   this->Finalize();
90 }
91 
92 // ===========================================================================
93 // Explicit template instantiations
94 // ===========================================================================
95 
96 template class VelocityToDisplacementFieldEuler<float>;
97 template class VelocityToDisplacementFieldEuler<double>;
98 
99 
100 } // namespace mirtk
101