iRoCS Toolbox  1.1.0
SvmH5Type.hh
Go to the documentation of this file.
1 /**************************************************************************
2  *
3  * Copyright (C) 2004-2015 Olaf Ronneberger, Florian Pigorsch, Jörg Mechnich,
4  * Thorsten Falk
5  *
6  * Image Analysis Lab, University of Freiburg, Germany
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 3 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software Foundation,
20  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21  *
22  **************************************************************************/
23 
24 /**************************************************************************
25 ** Title:
26 ** $RCSfile: $
27 ** $Revision: $$Name: $
28 ** $Date: $
29 ** Copyright: GPL $Author: $
30 ** Description:
31 **
32 **
33 **
34 **-------------------------------------------------------------------------
35 **
36 ** $Log: $
37 **
38 **
39 **************************************************************************/
40 
41 #ifndef SVMHDF5TYPE_HH
42 #define SVMHDF5TYPE_HH
43 
44 #ifdef HAVE_CONFIG_H
45 #include <config.hh>
46 #endif
47 
48 #include <vector>
49 #include <limits>
50 
51 namespace svt
52 {
69 };
70 
71 /*======================================================================*/
77 /*======================================================================*/
78 template< typename T>
79 struct BlitzH5Type {
81  static H5::DataType h5DataType()
82  {
83  SVMError err;
84  err << "BlitzH5Type::NO_TYPE: Data type not supported.";
85  //throw(err);
86  return H5::DataType();
87 
88  }
89  static hid_t h5DataTypeId()
90  {
91  SVMError err;
92  err << "BlitzH5Type::NO_TYPE: Data type not supported.";
93  //throw(err);
94  return H5::DataType();
95  }
96  static bool isVectorial()
97  {
98  SVMError err;
99  err << "BlitzH5Type::NO_TYPE: Data type not supported.";
100  //throw(err);
101  return H5::DataType();
102  }
103 };
104 
106 template<>
107 struct BlitzH5Type<bool>
108 {
110  static H5::DataType h5DataType()
111  {
112  return H5::PredType::NATIVE_UCHAR;
113  }
114  static hid_t h5DataTypeId()
115  {
116  return H5Tcopy(H5T_NATIVE_UCHAR);
117  }
118  static bool isVectorial()
119  {
120  return false;
121  }
122 };
123 
125 template<>
126 struct BlitzH5Type<signed char>
127 {
129  static H5::DataType h5DataType()
130  {
131  return H5::PredType::NATIVE_CHAR;
132  }
133  static hid_t h5DataTypeId()
134  {
135  return H5Tcopy(H5T_NATIVE_CHAR);
136  }
137  static bool isVectorial()
138  {
139  return false;
140  }
141 };
142 
143 template<>
144 struct BlitzH5Type<char>
145 {
147  static H5::DataType h5DataType()
148  {
149  return H5::PredType::NATIVE_CHAR;
150  }
151  static hid_t h5DataTypeId()
152  {
153  return H5Tcopy(H5T_NATIVE_CHAR);
154  }
155  static bool isVectorial()
156  {
157  return false;
158  }
159 };
160 
162 template<>
163 struct BlitzH5Type<unsigned char>
164 {
166  static H5::DataType h5DataType()
167  {
168  return H5::PredType::NATIVE_UCHAR;
169  }
170  static hid_t h5DataTypeId()
171  {
172  return H5Tcopy(H5T_NATIVE_UCHAR);
173  }
174  static bool isVectorial()
175  {
176  return false;
177  }
178 };
179 
181 template<>
182 struct BlitzH5Type<short>
183 {
185  static H5::DataType h5DataType()
186  {
187  return H5::PredType::NATIVE_SHORT;
188  }
189  static hid_t h5DataTypeId()
190  {
191  return H5Tcopy(H5T_NATIVE_SHORT);
192  }
193  static bool isVectorial()
194  {
195  return false;
196  }
197 };
198 
200 template<>
201 struct BlitzH5Type<unsigned short>
202 {
204  static H5::DataType h5DataType()
205  {
206  return H5::PredType::NATIVE_USHORT;
207  }
208  static hid_t h5DataTypeId()
209  {
210  return H5Tcopy(H5T_NATIVE_USHORT);
211  }
212  static bool isVectorial()
213  {
214  return false;
215  }
216 };
217 
219 template<>
220 struct BlitzH5Type<int>
221 {
223  static H5::DataType h5DataType()
224  {
225  return H5::PredType::NATIVE_INT;
226  }
227  static hid_t h5DataTypeId()
228  {
229  return H5Tcopy(H5T_NATIVE_INT);
230  }
231  static bool isVectorial()
232  {
233  return false;
234  }
235 };
236 
238 template<>
239 struct BlitzH5Type<unsigned int>
240 {
242  static H5::DataType h5DataType()
243  {
244  return H5::PredType::NATIVE_UINT;
245  }
246  static hid_t h5DataTypeId()
247  {
248  return H5Tcopy(H5T_NATIVE_UINT);
249  }
250  static bool isVectorial()
251  {
252  return false;
253  }
254 };
255 
257 template<>
258 struct BlitzH5Type<long>
259 {
261  static H5::DataType h5DataType()
262  {
263  return H5::PredType::NATIVE_LONG;
264  }
265  static hid_t h5DataTypeId()
266  {
267  return H5Tcopy(H5T_NATIVE_LONG);
268  }
269  static bool isVectorial()
270  {
271  return false;
272  }
273 };
274 
276 template<>
277 struct BlitzH5Type<unsigned long>
278 {
280  static H5::DataType h5DataType()
281  {
282  return H5::PredType::NATIVE_ULONG;
283  }
284  static hid_t h5DataTypeId()
285  {
286  return H5Tcopy(H5T_NATIVE_ULONG);
287  }
288  static bool isVectorial()
289  {
290  return false;
291  }
292 };
293 
295 template<>
296 struct BlitzH5Type<long long>
297 {
299  static H5::DataType h5DataType()
300  {
301  return H5::PredType::NATIVE_LLONG;
302  }
303  static hid_t h5DataTypeId()
304  {
305  return H5Tcopy(H5T_NATIVE_LLONG);
306  }
307  static bool isVectorial()
308  {
309  return false;
310  }
311 };
312 
314 template<>
315 struct BlitzH5Type<unsigned long long>
316 {
318  static H5::DataType h5DataType()
319  {
320  return H5::PredType::NATIVE_ULLONG;
321  }
322  static hid_t h5DataTypeId()
323  {
324  return H5Tcopy(H5T_NATIVE_ULLONG);
325  }
326  static bool isVectorial()
327  {
328  return false;
329  }
330 };
331 
333 template<>
334 struct BlitzH5Type<float>
335 {
337  static H5::DataType h5DataType()
338  {
339  return H5::PredType::NATIVE_FLOAT;
340  }
341  static hid_t h5DataTypeId()
342  {
343  return H5Tcopy(H5T_NATIVE_FLOAT);
344  }
345  static bool isVectorial()
346  {
347  return false;
348  }
349 };
350 
352 template<>
353 struct BlitzH5Type<double>
354 {
356  static H5::DataType h5DataType()
357  {
358  return H5::PredType::NATIVE_DOUBLE;
359  }
360  static hid_t h5DataTypeId()
361  {
362  return H5Tcopy(H5T_NATIVE_DOUBLE);
363  }
364  static bool isVectorial()
365  {
366  return false;
367  }
368 };
369 
370 // //! complex version
371 // template<typename T>
372 // struct BlitzH5Type< std::complex<T> >
373 // {
374 // static const TypeIdentifier dataType = BlitzH5Type<T>::dataType;
375 // static H5::DataType h5DataType()
376 // {
377 // return BlitzH5Type<T>::h5DataType();
378 // }
379 // static hid_t h5DataTypeId()
380 // {
381 // return BlitzH5Type<T>::h5DataTypeId();
382 // }
383 // static bool isVectorial()
384 // {
385 // return true;
386 // }
387 // };
388 
389 // //! TinyVector version
390 // template<typename T, int Dim>
391 // struct BlitzH5Type<blitz::TinyVector<T,Dim> >
392 // {
393 // static const TypeIdentifier dataType = BlitzH5Type<T>::dataType;
394 // static H5::DataType h5DataType()
395 // {
396 // return BlitzH5Type<T>::h5DataType();
397 // }
398 // static hid_t h5DataTypeId()
399 // {
400 // return BlitzH5Type<T>::h5DataTypeId();
401 // }
402 // static bool isVectorial()
403 // {
404 // return true;
405 // }
406 // };
407 
408 // //! TinyMatrix version
409 // template<typename T, int N, int M>
410 // struct BlitzH5Type<blitz::TinyMatrix<T,N,M> >
411 // {
412 // static const TypeIdentifier dataType = BlitzH5Type<T>::dataType;
413 // static H5::DataType h5DataType()
414 // {
415 // return BlitzH5Type<T>::h5DataType();
416 // }
417 // static hid_t h5DataTypeId()
418 // {
419 // return BlitzH5Type<T>::h5DataTypeId();
420 // }
421 // static bool isVectorial()
422 // {
423 // return true;
424 // }
425 // };
426 
428 template<typename T>
429 struct BlitzH5Type<std::vector<T> >
430 {
432  static H5::DataType h5DataType()
433  {
435  }
436  static hid_t h5DataTypeId()
437  {
439  }
440  static bool isVectorial()
441  {
442  return true;
443  }
444 };
445 
447 template<typename T>
448 struct BlitzH5Type<std::valarray<T> >
449 {
451  static H5::DataType h5DataType()
452  {
454  }
455  static hid_t h5DataTypeId()
456  {
458  }
459  static bool isVectorial()
460  {
461  return true;
462  }
463 };
464 
466 // From http://www.zib.de/benger/hdf5/Datatypes.html
467 // 3.6 Character and String Datatype Issues
468 // it seems that a good way to store strings is as an array
469 // of single H5T_C_S1 characters.
470 template<>
471 struct BlitzH5Type<std::string>
472 {
474  static H5::DataType h5DataType()
475  {
476  // old style 'variable length' strings where:
477  //H5::StrType datatype(H5::PredType::C_S1, H5T_VARIABLE);
478  H5::StrType datatype(H5::PredType::C_S1);
479  datatype.setStrpad(H5T_STR_NULLTERM);
480  //datatype.setStrpad(H5T_STR_NULLPAD);
481  return datatype;
482  }
483  static hid_t h5DataTypeId()
484  {
485  hid_t datatype = H5Tcopy(H5T_C_S1);
486  // old style 'variable length' strings where:
487  //H5Tset_size(datatype, H5T_VARIABLE);
488  H5Tset_strpad(datatype, H5T_STR_NULLTERM);
489  //H5Tset_strpad(datatype, H5T_STR_NULLPAD);
490  return datatype;
491  }
492  static bool isVectorial()
493  {
494  return true;
495  }
496 };
497 /*
498 template<>
499 struct BlitzH5Type<char[]>
500 {
501  static const TypeIdentifier dataType = TYPE_STRING;
502  static H5::DataType h5DataType()
503  {
504  H5::StrType datatype(H5::PredType::C_S1);
505  datatype.setStrpad(H5T_STR_NULLPAD);
506  return datatype;
507  }
508  static hid_t h5DataTypeId()
509  {
510  hid_t datatype = H5Tcopy(H5T_C_S1);
511  H5Tset_strpad(datatype, H5T_STR_NULLPAD);
512  return datatype;
513  }
514  static bool isVectorial()
515  {
516  return true;
517  }
518 };
519 */
521 template<>
522 struct BlitzH5Type<std::vector<std::string> >
523 {
525  static H5::DataType h5DataType()
526  {
527  SVMError err;
528  err << "std::vector<std::string> not supported yet.";
529  throw(err);
530  }
531  static hid_t h5DataTypeId()
532  {
533  SVMError err;
534  err << "std::vector<std::string> not supported yet.";
535  throw(err);
536  }
537  static bool isVectorial()
538  {
539  SVMError err;
540  err << "std::vector<std::string> not supported yet.";
541  throw(err);
542  }
543 };
544 
545 
546 /*-------------------------------------------------------------------------
547  * some common type comparison operators, for the getAndConvert method
548  *-------------------------------------------------------------------------*/
549 template< typename WantedType, typename DataFileType >
551 {
552  static bool exists()
553  {
554  return sizeof(WantedType) >= sizeof(DataFileType);
555  }
556  static bool sameType()
557  {
558  return false;
559  }
560  static bool sameSign()
561  {
562  // ToDo: Ugly workaround: Without the cast the compiler throws warnings
563  // for unsigned types, the cast to double remedies that, but
564  // might have bad side effects
565  return
566  ((static_cast<double>(std::numeric_limits<DataFileType>::min()) < 0 &&
567  static_cast<double>(std::numeric_limits<WantedType>::min()) < 0) ||
568  (static_cast<double>(std::numeric_limits<DataFileType>::min()) >= 0 &&
569  static_cast<double>(std::numeric_limits<WantedType>::min()) >= 0));
570  }
571  static WantedType scaleFactor()
572  {
573  WantedType maxWanted = std::numeric_limits<WantedType>::max();
574  DataFileType maxDataFile = std::numeric_limits<DataFileType>::max();
575  if( maxWanted < maxDataFile)
576  {
577  return std::numeric_limits<WantedType>::max();
578  }
579  return WantedType(1);
580  }
581  static WantedType wantedTypeMin()
582  {
583  return std::numeric_limits<WantedType>::min();
584  }
585 
586  static WantedType wantedTypeMax()
587  {
588  return std::numeric_limits<WantedType>::max();
589  }
590 };
591 
592 
593 /*-------------------------------------------------------------------------
594  * some common type comparison operators, specialization for equal types
595  *-------------------------------------------------------------------------*/
596 template< typename DataType >
597 struct BlitzH5TypeConversion<DataType,DataType>
598 {
599  static bool exists()
600  {
601  return true;
602  }
603  static bool sameType()
604  {
605  return true;
606  }
607  static bool sameSign()
608  {
609  return true;
610  }
611  static DataType scaleFactor()
612  {
613  return DataType(1);
614  }
615  static DataType wantedTypeMin()
616  {
617  return std::numeric_limits<DataType>::min();
618  }
619 
620  static DataType wantedTypeMax()
621  {
622  return std::numeric_limits<DataType>::max();
623  }
624 };
625 
626 
627 // /*-------------------------------------------------------------------------
628 // * some common type comparison operators, specialization for TinyVector
629 // *-------------------------------------------------------------------------*/
630 // template< typename WantedType, typename DataFileType, int N>
631 // struct BlitzH5TypeConversion<WantedType, blitz::TinyVector<DataFileType,N> >
632 // {
633 // static bool exists()
634 // {
635 // return BlitzH5TypeConversion<WantedType, DataFileType>::exists();
636 // }
637 // static bool sameType()
638 // {
639 // return BlitzH5TypeConversion<WantedType, DataFileType>::sameType();
640 // }
641 // static bool sameSign()
642 // {
643 // return BlitzH5TypeConversion<WantedType, DataFileType>::sameSign();
644 // }
645 // static WantedType scaleFactor()
646 // {
647 // return BlitzH5TypeConversion<WantedType, DataFileType>::scaleFactor();
648 // }
649 // static WantedType wantedTypeMin()
650 // {
651 // return BlitzH5TypeConversion<WantedType, DataFileType>::wantedTypeMin();
652 // }
653 
654 // static WantedType wantedTypeMax()
655 // {
656 // return BlitzH5TypeConversion<WantedType, DataFileType>::wantedTypeMax();
657 // }
658 // };
659 
660 
661 // /*-------------------------------------------------------------------------
662 // * some common type comparison operators, specialization for TinyVector
663 // *-------------------------------------------------------------------------*/
664 // template< typename WantedType, typename DataFileType, int N>
665 // struct BlitzH5TypeConversion<blitz::TinyVector<WantedType,N>, DataFileType>
666 // {
667 // static bool exists()
668 // {
669 // return BlitzH5TypeConversion<WantedType, DataFileType>::exists();
670 // }
671 // static bool sameType()
672 // {
673 // return BlitzH5TypeConversion<WantedType, DataFileType>::sameType();
674 // }
675 // static bool sameSign()
676 // {
677 // return BlitzH5TypeConversion<WantedType, DataFileType>::sameSign();
678 // }
679 // static WantedType scaleFactor()
680 // {
681 // return BlitzH5TypeConversion<WantedType, DataFileType>::scaleFactor();
682 // }
683 // static WantedType wantedTypeMin()
684 // {
685 // return BlitzH5TypeConversion<WantedType, DataFileType>::wantedTypeMin();
686 // }
687 
688 // static WantedType wantedTypeMax()
689 // {
690 // return BlitzH5TypeConversion<WantedType, DataFileType>::wantedTypeMax();
691 // }
692 // };
693 
694 
695 // /*-------------------------------------------------------------------------
696 // * some common type comparison operators, specialization for std::complex
697 // *-------------------------------------------------------------------------*/
698 // template< typename WantedType, typename DataFileType>
699 // struct BlitzH5TypeConversion<WantedType, std::complex<DataFileType> >
700 // {
701 // static bool exists()
702 // {
703 // return BlitzH5TypeConversion<WantedType, DataFileType>::exists();
704 // }
705 // static bool sameType()
706 // {
707 // return BlitzH5TypeConversion<WantedType, DataFileType>::sameType();
708 // }
709 // static bool sameSign()
710 // {
711 // return BlitzH5TypeConversion<WantedType, DataFileType>::sameSign();
712 // }
713 // static WantedType scaleFactor()
714 // {
715 // return BlitzH5TypeConversion<WantedType, DataFileType>::scaleFactor();
716 // }
717 // static WantedType wantedTypeMin()
718 // {
719 // return BlitzH5TypeConversion<WantedType, DataFileType>::wantedTypeMin();
720 // }
721 // static WantedType wantedTypeMax()
722 // {
723 // return BlitzH5TypeConversion<WantedType, DataFileType>::wantedTypeMax();
724 // }
725 // };
726 
727 
728 // /*-------------------------------------------------------------------------
729 // * some common type comparison operators, specialization for std::complex
730 // *-------------------------------------------------------------------------*/
731 // template< typename WantedType, typename DataFileType>
732 // struct BlitzH5TypeConversion<std::complex<DataFileType>, WantedType>
733 // {
734 // static bool exists()
735 // {
736 // return BlitzH5TypeConversion<WantedType, DataFileType>::exists();
737 // }
738 // static bool sameType()
739 // {
740 // return BlitzH5TypeConversion<WantedType, DataFileType>::sameType();
741 // }
742 // static bool sameSign()
743 // {
744 // return BlitzH5TypeConversion<WantedType, DataFileType>::sameSign();
745 // }
746 // static WantedType scaleFactor()
747 // {
748 // return BlitzH5TypeConversion<WantedType, DataFileType>::scaleFactor();
749 // }
750 // static WantedType wantedTypeMin()
751 // {
752 // return BlitzH5TypeConversion<WantedType, DataFileType>::wantedTypeMin();
753 // }
754 // static WantedType wantedTypeMax()
755 // {
756 // return BlitzH5TypeConversion<WantedType, DataFileType>::wantedTypeMax();
757 // }
758 // };
759 }
760 #endif
761 
static hid_t h5DataTypeId()
Definition: SvmH5Type.hh:227
static H5::DataType h5DataType()
Definition: SvmH5Type.hh:451
static const TypeIdentifier dataType
Definition: SvmH5Type.hh:80
static H5::DataType h5DataType()
Definition: SvmH5Type.hh:147
static bool isVectorial()
Definition: SvmH5Type.hh:118
static hid_t h5DataTypeId()
Definition: SvmH5Type.hh:360
static H5::DataType h5DataType()
Definition: SvmH5Type.hh:261
static WantedType scaleFactor()
Definition: SvmH5Type.hh:571
static H5::DataType h5DataType()
Definition: SvmH5Type.hh:299
static WantedType wantedTypeMax()
Definition: SvmH5Type.hh:586
STL namespace.
static hid_t h5DataTypeId()
Definition: SvmH5Type.hh:114
static hid_t h5DataTypeId()
Definition: SvmH5Type.hh:189
static bool isVectorial()
Definition: SvmH5Type.hh:96
static H5::DataType h5DataType()
Definition: SvmH5Type.hh:242
static WantedType wantedTypeMin()
Definition: SvmH5Type.hh:581
The SVMError class is the parent class for all errors that are thrown by the LIBSVMTL.
Definition: SVMError.hh:90
static H5::DataType h5DataType()
Definition: SvmH5Type.hh:337
static H5::DataType h5DataType()
Definition: SvmH5Type.hh:223
static bool isVectorial()
Definition: SvmH5Type.hh:193
static bool isVectorial()
Definition: SvmH5Type.hh:155
static H5::DataType h5DataType()
Definition: SvmH5Type.hh:318
static H5::DataType h5DataType()
Definition: SvmH5Type.hh:185
static H5::DataType h5DataType()
Definition: SvmH5Type.hh:432
static hid_t h5DataTypeId()
Definition: SvmH5Type.hh:89
static H5::DataType h5DataType()
Definition: SvmH5Type.hh:204
Default type trait.
Definition: SvmH5Type.hh:79
static H5::DataType h5DataType()
Definition: SvmH5Type.hh:166
static hid_t h5DataTypeId()
Definition: SvmH5Type.hh:341
static bool isVectorial()
Definition: SvmH5Type.hh:269
static hid_t h5DataTypeId()
Definition: SvmH5Type.hh:151
static H5::DataType h5DataType()
Definition: SvmH5Type.hh:110
static bool isVectorial()
Definition: SvmH5Type.hh:231
static bool isVectorial()
Definition: SvmH5Type.hh:345
static H5::DataType h5DataType()
Definition: SvmH5Type.hh:129
static H5::DataType h5DataType()
Definition: SvmH5Type.hh:356
static H5::DataType h5DataType()
Definition: SvmH5Type.hh:81
TypeIdentifier
Type identifiers.
Definition: SvmH5Type.hh:54
static H5::DataType h5DataType()
Definition: SvmH5Type.hh:474
static H5::DataType h5DataType()
Definition: SvmH5Type.hh:280
static bool isVectorial()
Definition: SvmH5Type.hh:364
static hid_t h5DataTypeId()
Definition: SvmH5Type.hh:265