TernaryVoxelFunction.h
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 #ifndef MIRTK_TernaryVoxelFunction_H
21 #define MIRTK_TernaryVoxelFunction_H
22 
23 #include "mirtk/VoxelFunction.h"
24 
25 
26 namespace mirtk {
27 
28 
29 /**
30  * These basic ternary voxel functions can be used as VoxelFunc template parameter
31  * of the ternary ForEachVoxel function templates with three image arguments, e.g.:
32  *
33  * \code
34  * GreyImage input1(attr);
35  * GreyImage input2(attr);
36  * GreyImage ouptut(attr);
37  * // Compute voxel-wise sum of input1 and input2
38  * TernaryVoxelFunction::Sum sum;
39  * ForEachVoxel(input1, input2, output, sum);
40  * // Compute voxel-wise input1/input2 for each foreground voxel
41  * TernaryVoxelFunction::Div div;
42  * ParallelForEachVoxelInside(input1, input2, output, div);
43  * \endcode
44  */
45 namespace TernaryVoxelFunction {
46 
47 
48 // -----------------------------------------------------------------------------
49 /**
50  * Sums up the voxel values of two images
51  */
52 struct Sum : public VoxelFunction
53 {
54  template <class T1, class T2, class T3>
55  void operator ()(const T1 *in1, const T2 *in2, T3 *out)
56  {
57  *out = static_cast<T3>(static_cast<double>(*in1) + static_cast<double>(*in2));
58  }
59 
60  template <class TImage, class T1, class T2, class T3>
61  void operator ()(const TImage&, int, const T1 *in1, const T2 *in2, T3 *out)
62  {
63  this->operator ()(in1, in2, out);
64  }
65 
66  template <class T1, class T2, class T3>
67  void operator ()(int, int, int, int, const T1 *in1, const T2 *in2, T3 *out)
68  {
69  this->operator ()(in1, in2, out);
70  }
71 };
72 
73 // -----------------------------------------------------------------------------
74 /**
75  * Computes the voxel intensity difference of two images
76  */
77 struct Diff : public VoxelFunction
78 {
79  template <class T1, class T2, class T3>
80  void operator ()(const T1 *in1, const T2 *in2, T3 *out)
81  {
82  *out = static_cast<T3>(static_cast<double>(*in1) - static_cast<double>(*in2));
83  }
84 
85  template <class TImage, class T1, class T2, class T3>
86  void operator ()(const TImage&, int, const T1 *in1, const T2 *in2, T3 *out)
87  {
88  this->operator ()(in1, in2, out);
89  }
90 
91  template <class T1, class T2, class T3>
92  void operator ()(int, int, int, int, const T1 *in1, const T2 *in2, T3 *out)
93  {
94  this->operator ()(in1, in2, out);
95  }
96 };
97 
98 // -----------------------------------------------------------------------------
99 /**
100  * Calculates the product of the voxel values of two images
101  */
102 struct Mul : public VoxelFunction
103 {
104  template <class T1, class T2, class T3>
105  void operator ()(const T1 *in1, const T2 *in2, T3 *out)
106  {
107  *out = static_cast<T3>(static_cast<double>(*in1) * static_cast<double>(*in2));
108  }
109 
110  template <class TImage, class T1, class T2, class T3>
111  void operator ()(const TImage&, int, const T1 *in1, const T2 *in2, T3 *out)
112  {
113  this->operator ()(in1, in2, out);
114  }
115 
116  template <class T1, class T2, class T3>
117  void operator ()(int, int, int, int, const T1 *in1, const T2 *in2, T3 *out)
118  {
119  this->operator ()(in1, in2, out);
120  }
121 };
122 
123 /// Alternative name for Mul voxel function
124 typedef Mul Prod;
125 
126 // -----------------------------------------------------------------------------
127 /**
128  * Calculates the divison of the voxel values of two images
129  */
130 struct Div : public VoxelFunction
131 {
132  template <class T1, class T2, class T3>
133  void operator ()(const T1 *in1, const T2 *in2, T3 *out)
134  {
135  double divisor = static_cast<double>(*in2);
136  if (divisor == .0) {
137  *out = static_cast<T3>(0);
138  } else {
139  *out = static_cast<T3>(static_cast<double>(*in1) / divisor);
140  }
141  }
142 
143  template <class TImage, class T1, class T2, class T3>
144  void operator ()(const TImage&, int, const T1 *in1, const T2 *in2, T3 *out)
145  {
146  this->operator ()(in1, in2, out);
147  }
148 
149  template <class T1, class T2, class T3>
150  void operator ()(int, int, int, int, const T1 *in1, const T2 *in2, T3 *out)
151  {
152  this->operator ()(in1, in2, out);
153  }
154 };
155 
156 
157 } } // namespace mirtk::TernaryVoxelFunction
158 
159 #endif // MIRTK_TernaryVoxelFunction_H
Definition: IOConfig.h:41
Mul Prod
Alternative name for Mul voxel function.