ITK  4.2.0
Insight Segmentation and Registration Toolkit
itkMeasurementVectorTraits.h
Go to the documentation of this file.
1 /*=========================================================================
2  *
3  * Copyright Insight Software Consortium
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0.txt
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  *=========================================================================*/
18 #ifndef __itkMeasurementVectorTraits_h
19 #define __itkMeasurementVectorTraits_h
20 
22 #include "vnl/vnl_vector_fixed.h"
23 #include "itkRGBPixel.h"
24 #include "itkMatrix.h"
25 #include "itkVariableSizeMatrix.h"
26 #include "itkNumericTraits.h"
28 #include "itkSize.h"
29 #include <vector>
30 
31 namespace itk
32 {
33 namespace Statistics
34 {
42 {
43 public:
44 
52 
58 
59  typedef unsigned int MeasurementVectorLength;
60 
61  template< class TVectorType >
62  static bool IsResizable(const TVectorType &)
63  {
64  // Test whether the vector type is resizable or not
65  //
66  // If the default constructor creates a vector of
67  // length zero, we assume that it is resizable,
68  // otherwise that is a pretty useless measurement vector.
69  TVectorType m;
71 
72  return ( len == 0 );
73  }
74 
75  template< class TValueType1, unsigned int VLength, class TValueType2, unsigned int VLength2 >
78  const char *errMsg = "Length Mismatch")
79  {
80  if ( VLength != VLength2 )
81  {
82  itkGenericExceptionMacro(<< errMsg);
83  }
84  return 0;
85  }
86 
87  template< class TValueType1, unsigned int VLength, class TValueType2, unsigned int VLength2 >
90  const char *errMsg = "Length Mismatch")
91  {
92  if ( VLength != VLength2 )
93  {
94  itkGenericExceptionMacro(<< errMsg);
95  }
96  return 0;
97  }
98 
99  template< class TValueType1, class TValueType2 >
101  const Array< TValueType2 > & b, const char *errMsg = "Length Mismatch")
102  {
103  if ( b.Size() != a.Size() )
104  {
105  itkGenericExceptionMacro(<< errMsg);
106  }
107  return 0;
108  }
109 
110  template< class TValueType1, class TValueType2 >
112  const Array< TValueType2 > *b, const char *errMsg = "Length Mismatch")
113  {
114  if ( b->Size() != a->Size() )
115  {
116  itkGenericExceptionMacro(<< errMsg);
117  }
118  return 0;
119  }
120 
121  template< class TValueType1, class TValueType2 >
124  const char *errMsg = "Length Mismatch")
125  {
126  if ( b.Size() != a.Size() )
127  {
128  itkGenericExceptionMacro(<< errMsg);
129  }
130  return 0;
131  }
132 
133  template< class TValueType1, class TValueType2 >
136  const char *errMsg = "Length Mismatch")
137  {
138  if ( b->Size() != a->Size() )
139  {
140  itkGenericExceptionMacro(<< errMsg);
141  }
142  return 0;
143  }
144 
145  template< class TValueType1, class TValueType2 >
146  static MeasurementVectorLength Assert(const std::vector< TValueType1 > & a,
147  const std::vector< TValueType2 > & b, const char *errMsg = "Length Mismatch")
148  {
149  if ( b.size() != a.size() )
150  {
151  itkGenericExceptionMacro(<< errMsg);
152  }
153  return 0;
154  }
155 
156  template< class TValueType1, class TValueType2 >
157  static MeasurementVectorLength Assert(const std::vector< TValueType1 > *a,
158  const std::vector< TValueType2 > *b, const char *errMsg = "Length Mismatch")
159  {
160  if ( b->size() != a->size() )
161  {
162  itkGenericExceptionMacro(<< errMsg);
163  }
164  return 0;
165  }
166 
167  template< class TValueType1, unsigned int VLength, class TValueType2 >
169  const Array< TValueType2 > & b, const char *errMsg = "Length Mismatch")
170  {
171  if ( b.Size() == 0 )
172  {
173  return VLength;
174  }
175  if ( b.Size() != 0 )
176  {
177  if ( b.Size() != VLength )
178  {
179  itkGenericExceptionMacro(<< errMsg);
180  }
181  }
182  return 0;
183  }
184 
185  template< class TValueType1, unsigned int VLength, class TValueType2 >
187  const Array< TValueType2 > *b, const char *errMsg = "Length Mismatch")
188  {
189  if ( b->Size() == 0 )
190  {
191  return VLength;
192  }
193  else if ( b->Size() != VLength )
194  {
195  itkGenericExceptionMacro(<< errMsg);
196  }
197  return 0;
198  }
199 
200  template< class TValueType1, unsigned int VLength, class TValueType2 >
203  const char *errMsg = "Length Mismatch")
204  {
205  if ( b.Size() == 0 )
206  {
207  return VLength;
208  }
209  if ( b.Size() != 0 )
210  {
211  if ( b.Size() != VLength )
212  {
213  itkGenericExceptionMacro(<< errMsg);
214  }
215  }
216  return 0;
217  }
218 
219  template< class TValueType1, unsigned int VLength, class TValueType2 >
222  const char *errMsg = "Length Mismatch")
223  {
224  if ( b->Size() == 0 )
225  {
226  return VLength;
227  }
228  else if ( b->Size() != VLength )
229  {
230  itkGenericExceptionMacro(<< errMsg);
231  }
232  return 0;
233  }
234 
235  template< class TValueType1, unsigned int VLength, class TValueType2 >
237  const std::vector< TValueType2 > & b, const char *errMsg = "Length Mismatch")
238  {
239  if ( b.size() == 0 )
240  {
241  return VLength;
242  }
243  if ( b.size() != 0 )
244  {
245  if ( b.size() != VLength )
246  {
247  itkGenericExceptionMacro(<< errMsg);
248  }
249  }
250  return 0;
251  }
252 
253  template< class TValueType1, unsigned int VLength, class TValueType2 >
255  const std::vector< TValueType2 > *b, const char *errMsg = "Length Mismatch")
256  {
257  if ( b->size() == 0 )
258  {
259  return VLength;
260  }
261  else if ( b->size() != VLength )
262  {
263  itkGenericExceptionMacro(<< errMsg);
264  }
265  return 0;
266  }
267 
268  template< class TValueType1, unsigned int VLength >
270  const MeasurementVectorLength l, const char *errMsg = "Length Mismatch")
271  {
272  if ( l == 0 )
273  {
274  return VLength;
275  }
276  else if ( l != VLength )
277  {
278  itkGenericExceptionMacro(<< errMsg);
279  }
280  return 0;
281  }
282 
283  template< class TValueType1, unsigned int VLength >
285  const MeasurementVectorLength l, const char *errMsg = "Length Mismatch")
286  {
287  if ( l == 0 )
288  {
289  return VLength;
290  }
291  else if ( l != VLength )
292  {
293  itkGenericExceptionMacro(<< errMsg);
294  }
295  return 0;
296  }
297 
298  template< class TValueType >
300  const MeasurementVectorLength l, const char *errMsg = "Length Mismatch")
301  {
302  if ( ( ( l != 0 ) && ( a.Size() != l ) ) || ( a.Size() == 0 ) )
303  {
304  itkGenericExceptionMacro(<< errMsg);
305  }
306  else if ( l == 0 )
307  {
308  return a.Size();
309  }
310  return 0;
311  }
312 
313  template< class TValueType >
315  const MeasurementVectorLength l, const char *errMsg = "Length Mismatch")
316  {
317  if ( ( ( l != 0 ) && ( a->Size() != l ) ) || ( a->Size() == 0 ) )
318  {
319  itkGenericExceptionMacro(<< errMsg);
320  }
321  else if ( l == 0 )
322  {
323  return a->Size();
324  }
325  return 0;
326  }
327 
328  template< class TValueType >
330  const MeasurementVectorLength l, const char *errMsg = "Length Mismatch")
331  {
332  if ( ( ( l != 0 ) && ( a.Size() != l ) ) || ( a.Size() == 0 ) )
333  {
334  itkGenericExceptionMacro(<< errMsg);
335  }
336  else if ( l == 0 )
337  {
338  return a.Size();
339  }
340  return 0;
341  }
342 
343  template< class TValueType >
345  const MeasurementVectorLength l, const char *errMsg = "Length Mismatch")
346  {
347  if ( ( ( l != 0 ) && ( a->Size() != l ) ) || ( a->Size() == 0 ) )
348  {
349  itkGenericExceptionMacro(<< errMsg);
350  }
351  else if ( l == 0 )
352  {
353  return a->Size();
354  }
355  return 0;
356  }
357 
358  template< class TValueType >
359  static MeasurementVectorLength Assert(const std::vector< TValueType > & a,
360  const MeasurementVectorLength l, const char *errMsg = "Length Mismatch")
361  {
362  if ( ( ( l != 0 ) && ( a.size() != l ) ) || ( a.size() == 0 ) )
363  {
364  itkGenericExceptionMacro(<< errMsg);
365  }
366  else if ( l == 0 )
367  {
368  return a.size();
369  }
370  return 0;
371  }
372 
373  template< class TValueType >
374  static MeasurementVectorLength Assert(const std::vector< TValueType > *a,
375  const MeasurementVectorLength l, const char *errMsg = "Length Mismatch")
376  {
377  if ( ( ( l != 0 ) && ( a->size() != l ) ) || ( a->size() == 0 ) )
378  {
379  itkGenericExceptionMacro(<< errMsg);
380  }
381  else if ( l == 0 )
382  {
383  return a->size();
384  }
385  return 0;
386  }
387 
388  template< class TArrayType >
389  static void Assign(TArrayType & m, const TArrayType & v)
390  {
391  m = v;
392  }
393 
394  template< class TValueType, unsigned int VLength >
395  static void Assign(FixedArray< TValueType, VLength > & m, const TValueType & v)
396  {
397  m[0] = v;
398  }
399 };
400 
407 template< class TMeasurementVector >
409 {
410 public:
411  typedef typename TMeasurementVector::ValueType ValueType;
412 };
413 
414 template< class T >
415 class MeasurementVectorTraitsTypes< std::vector< T > >
416 {
417 public:
418  typedef T ValueType;
419 };
420 
424 template< class TPixelType >
426 {
427 public:
428  /* type of the vector that matches this pixel type */
429  typedef TPixelType MeasurementVectorType;
430 };
431 
437 template< >
438 class MeasurementVectorPixelTraits< char >
439 {
440 public:
442 };
443 
444 template< >
445 class MeasurementVectorPixelTraits< unsigned char >
446 {
447 public:
449 };
450 
451 template< >
452 class MeasurementVectorPixelTraits< signed char >
453 {
454 public:
455  typedef FixedArray< signed char, 1 > MeasurementVectorType;
456 };
457 
458 template< >
459 class MeasurementVectorPixelTraits< unsigned short >
460 {
461 public:
462  typedef FixedArray< unsigned short, 1 > MeasurementVectorType;
463 };
464 
465 template< >
466 class MeasurementVectorPixelTraits< signed short >
467 {
468 public:
469  typedef FixedArray< signed short, 1 > MeasurementVectorType;
470 };
471 
472 template< >
473 class MeasurementVectorPixelTraits< unsigned int >
474 {
475 public:
476  typedef FixedArray< unsigned int, 1 > MeasurementVectorType;
477 };
478 
479 template< >
480 class MeasurementVectorPixelTraits< signed int >
481 {
482 public:
483  typedef FixedArray< signed int, 1 > MeasurementVectorType;
484 };
485 
486 template< >
487 class MeasurementVectorPixelTraits< unsigned long >
488 {
489 public:
490  typedef FixedArray< unsigned long, 1 > MeasurementVectorType;
491 };
492 
493 template< >
494 class MeasurementVectorPixelTraits< signed long >
495 {
496 public:
497  typedef FixedArray< signed long, 1 > MeasurementVectorType;
498 };
499 
500 template< >
501 class MeasurementVectorPixelTraits< float >
502 {
503 public:
504  typedef FixedArray< float, 1 > MeasurementVectorType;
505 };
506 
507 template< >
508 class MeasurementVectorPixelTraits< double >
509 {
510 public:
511  typedef FixedArray< double, 1 > MeasurementVectorType;
512 };
513 
516 } // namespace Statistics
517 } // namespace itk
518 
519 #endif // __itkMeasurementVectorTraits_h
520