Statismo  0.10.1
 All Classes Namespaces Functions Typedefs
itkPixelConversionTraits.h
1 /*
2  * This file is part of the statismo library.
3  *
4  * Author: Marcel Luethi (marcel.luethi@unibas.ch)
5  *
6  * Copyright (c) 2011 University of Basel
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  *
13  * Redistributions of source code must retain the above copyright notice,
14  * this list of conditions and the following disclaimer.
15  *
16  * Redistributions in binary form must reproduce the above copyright
17  * notice, this list of conditions and the following disclaimer in the
18  * documentation and/or other materials provided with the distribution.
19  *
20  * Neither the name of the project's author nor the names of its
21  * contributors may be used to endorse or promote products derived from
22  * this software without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
27  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
28  * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
29  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
30  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
31  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
32  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
33  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
34  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35  *
36  */
37 
38 
39 #ifndef __ITK_TYPE_CONVERSION_TRAIT
40 #define __ITK_TYPE_CONVERSION_TRAIT
41 
42 #include <itkVector.h>
43 
44 #include "Exceptions.h"
45 #include "CommonTypes.h"
46 
47 namespace itk {
48 
49 // these traits are used to allow a conversion from the generic pixel type to a statismo vector.
50 // Currently only scalar types are supported.
51 
52 template <typename T> struct PixelConversionTrait {
53  static statismo::VectorType ToVector(const T& pixel) {
54  throw statismo::StatisticalModelException("Unsupported PixelType (PixelTraits::ToVector not implemented)");
55  }
56  static T FromVector(const statismo::VectorType& v) {
57  throw statismo::StatisticalModelException("Unsupported PixelType (PixelTraits::ToVector not implemented)");
58  }
59  static unsigned GetDataType() {
60  throw statismo::StatisticalModelException("Unsupported PixelType (PixelTraits::ToVector not implemented)");
61  }
62  static unsigned GetPixelDimension() {
63  throw statismo::StatisticalModelException("Unsupported PixelType (PixelTraits::ToVector not implemented)");
64  }
65 };
66 
67 template <> struct PixelConversionTrait<double> {
68  static statismo::VectorType ToVector(const double& pixel) {
69  statismo::VectorType v(1);
70  v << pixel;
71  return v;
72  }
73  static double FromVector(const statismo::VectorType& v) {
74  assert(v.size() == 1);
75  return v(0);
76  }
77  static unsigned GetDataType() {
78  return statismo::DOUBLE;
79  }
80  static unsigned GetPixelDimension() {
81  return 1;
82  }
83 };
84 template <> struct PixelConversionTrait<float> {
85  static statismo::VectorType ToVector(const float& pixel) {
86  statismo::VectorType v(1);
87  v << pixel;
88  return v;
89  }
90  static float FromVector(const statismo::VectorType& v) {
91  assert(v.size() == 1);
92  return v(0);
93  }
94  static unsigned GetDataType() {
95  return statismo::FLOAT;
96  }
97  static unsigned GetPixelDimension() {
98  return 1;
99  }
100 };
101 template <> struct PixelConversionTrait<short> {
102  static statismo::VectorType ToVector(const short& pixel) {
103  statismo::VectorType v(1);
104  v << pixel;
105  return v;
106  }
107  static short FromVector(const statismo::VectorType& v) {
108  assert(v.size() == 1);
109  return v(0);
110  }
111  static unsigned GetDataType() {
112  return statismo::SIGNED_SHORT;
113  }
114  static unsigned GetPixelDimension() {
115  return 1;
116  }
117 };
118 template <> struct PixelConversionTrait<unsigned short> {
119  static statismo::VectorType ToVector(const unsigned short& pixel) {
120  statismo::VectorType v(1);
121  v << pixel;
122  return v;
123  }
124  static unsigned short FromVector(const statismo::VectorType& v) {
125  assert(v.size() == 1);
126  return v(0);
127  }
128  static unsigned GetDataType() {
129  return statismo::UNSIGNED_SHORT;
130  }
131  static unsigned GetPixelDimension() {
132  return 1;
133  }
134 };
135 template <> struct PixelConversionTrait<int> {
136  static statismo::VectorType ToVector(const int& pixel) {
137  statismo::VectorType v(1);
138  v << pixel;
139  return v;
140  }
141  static int FromVector(const statismo::VectorType& v) {
142  assert(v.size() == 1);
143  return v(0);
144  }
145  static unsigned GetDataType() {
146  return statismo::SIGNED_INT;
147  }
148  static unsigned GetPixelDimension() {
149  return 1;
150  }
151 };
152 template <> struct PixelConversionTrait<unsigned int> {
153  static statismo::VectorType ToVector(const unsigned int& pixel) {
154  statismo::VectorType v(1);
155  v << pixel;
156  return v;
157  }
158  static unsigned int FromVector(const statismo::VectorType& v) {
159  assert(v.size() == 1);
160  return v(0);
161  }
162  static unsigned GetDataType() {
163  return statismo::UNSIGNED_SHORT;
164  }
165  static unsigned GetPixelDimension() {
166  return 1;
167  }
168 };
169 template <> struct PixelConversionTrait<char> {
170  static statismo::VectorType ToVector(const char& pixel) {
171  statismo::VectorType v(1);
172  v << pixel;
173  return v;
174  }
175  static char FromVector(const statismo::VectorType& v) {
176  assert(v.size() == 1);
177  return v(0);
178  }
179  static unsigned GetDataType() {
180  return statismo::SIGNED_CHAR;
181  }
182  static unsigned GetPixelDimension() {
183  return 1;
184  }
185 };
186 template <> struct PixelConversionTrait<unsigned char> {
187  static statismo::VectorType ToVector(const unsigned char& pixel) {
188  statismo::VectorType v(1);
189  v << pixel;
190  return v;
191  }
192  static unsigned char FromVector(const statismo::VectorType& v) {
193  assert(v.size() == 1);
194  return v(0);
195  }
196  static unsigned GetDataType() {
197  return statismo::UNSIGNED_CHAR;
198  }
199  static unsigned GetPixelDimension() {
200  return 1;
201  }
202 };
203 
204 template <> struct PixelConversionTrait<long> {
205  static statismo::VectorType ToVector(const long& pixel) {
206  statismo::VectorType v(1);
207  v << pixel;
208  return v;
209  }
210  static long FromVector(const statismo::VectorType& v) {
211  assert(v.size() == 1);
212  return v(0);
213  }
214  static unsigned GetDataType() {
215  return statismo::SIGNED_LONG;
216  }
217  static unsigned GetPixelDimension() {
218  return 1;
219  }
220 };
221 template <> struct PixelConversionTrait<unsigned long> {
222  static statismo::VectorType ToVector(const unsigned long& pixel) {
223  statismo::VectorType v(1);
224  v << pixel;
225  return v;
226  }
227  static unsigned long FromVector(const statismo::VectorType& v) {
228  assert(v.size() == 1);
229  return v(0);
230  }
231  static unsigned GetDataType() {
232  return statismo::UNSIGNED_LONG;
233  }
234  static unsigned GetPixelDimension() {
235  return 1;
236  }
237 };
238 
239 template <> struct PixelConversionTrait<itk::Vector<float, 2> > {
240  static statismo::VectorType ToVector(const itk::Vector<float, 2>& pixel) {
241  statismo::VectorType v(2);
242  v << pixel[0] , pixel[1];
243  return v;
244  }
245  static itk::Vector<float, 2> FromVector(const statismo::VectorType& v) {
246  assert(v.size() == 2);
247  itk::Vector<double, 2> itkVec;
248  itkVec[0] = v(0);
249  itkVec[1] = v(1);
250  return itkVec;
251  }
252  static unsigned GetDataType() {
253  return statismo::FLOAT;
254  }
255  static unsigned GetPixelDimension() {
256  return 2;
257  }
258 };
259 
260 template <> struct PixelConversionTrait<itk::Vector<float, 3> > {
261  static statismo::VectorType ToVector(const itk::Vector<float, 3>& pixel) {
262  statismo::VectorType v(3);
263  v << pixel[0] , pixel[1], pixel[2];
264  return v;
265  }
266  static itk::Vector<float, 3> FromVector(const statismo::VectorType& v) {
267  assert(v.size() == 3);
268  itk::Vector<double, 3> itkVec;
269  itkVec[0] = v(0);
270  itkVec[1] = v(1);
271  itkVec[2] = v(2);
272  return itkVec;
273  }
274  static unsigned GetDataType() {
275  return statismo::FLOAT;
276  }
277  static unsigned GetPixelDimension() {
278  return 3;
279  }
280 };
281 
282 
283 } // namespace itk
284 
285 #endif
Generic Exception class for the statismo Library.
Definition: Exceptions.h:68