iLab Neuromorphic Robotics Toolkit  0.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
PixelTraitsHelpers.H
Go to the documentation of this file.
1 /*! @file
2  @author David J. Berg
3  @copyright GNU Public License (GPL v3)
4  @section License
5  @verbatim
6  // ////////////////////////////////////////////////////////////////////////
7  // The iLab Neuromorphic Robotics Toolkit (NRT) //
8  // Copyright 2010-2012 by the University of Southern California (USC) //
9  // and the iLab at USC. //
10  // //
11  // iLab - University of Southern California //
12  // Hedco Neurociences Building, Room HNB-10 //
13  // Los Angeles, Ca 90089-2520 - USA //
14  // //
15  // See http://ilab.usc.edu for information about this project. //
16  // ////////////////////////////////////////////////////////////////////////
17  // This file is part of The iLab Neuromorphic Robotics Toolkit. //
18  // //
19  // The iLab Neuromorphic Robotics Toolkit is free software: you can //
20  // redistribute it and/or modify it under the terms of the GNU General //
21  // Public License as published by the Free Software Foundation, either //
22  // version 3 of the License, or (at your option) any later version. //
23  // //
24  // The iLab Neuromorphic Robotics Toolkit is distributed in the hope //
25  // that it will be useful, but WITHOUT ANY WARRANTY; without even the //
26  // implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR //
27  // PURPOSE. See the GNU General Public License for more details. //
28  // //
29  // You should have received a copy of the GNU General Public License //
30  // along with The iLab Neuromorphic Robotics Toolkit. If not, see //
31  // <http://www.gnu.org/licenses/>. //
32  // ////////////////////////////////////////////////////////////////////////
33  @endverbatim */
34 
35 
36 #ifndef INCLUDE_NRT_CORE_TYPING_DETAILS_PIXELTRAITSHELPERS_H
37 #define INCLUDE_NRT_CORE_TYPING_DETAILS_PIXELTRAITSHELPERS_H
38 
40 namespace nrt
41 {
42  //forward declarations
43  class PixelBaseCore;
44  template <class T> struct PixelWithAlpha;
45 
46  namespace pixel_helper
47  {
48 
49  //check to see if we are a pixel
50  template <class T, bool B = std::is_class<T>::value> struct is_pixel : std::false_type {};
51  template <class T> struct is_pixel<T,true> : std::is_base_of<PixelBaseCore,T> {};
52 
53  template<class PixType, bool B = !is_pixel<PixType>::value>
54  struct has_extra_data
55  { static const bool value = false; };
56 
57  template<class PixType>
58  struct has_extra_data<PixType, false>
59  { static const bool value = (sizeof(PixType) > sizeof(typename PixType::PODType)*PixType::numChannels) ? true : false; };
60 
61 
62  //check to see if we are the same or one derived from the other, irrespecitve of the inner pixel type
63  //you can make specializations of this class to enable operators for different classes.
64  template <class Pix1, class Pix2,
65  bool B = is_pixel<Pix1>::value && is_pixel<Pix2>::value,
66  bool C = has_extra_data<Pix1>::value && has_extra_data<Pix2>::value>
67  struct are_comparable : std::false_type { typedef NotATypeError type; };
68 
69  template <class Pix1, class Pix2>
70  class are_comparable<Pix1, Pix2, true, false>
71  {
72  typedef typename Pix1::PODType P1;
73  typedef typename Pix2::PODType P2;
74 
75  typedef typename nrt::promote<P1,P2>::type promo;
76 
77  typedef typename Pix1::template rebind<promo>::type promo1;
78  typedef typename Pix2::template rebind<promo>::type promo2;
79 
80  static const bool same = std::is_same<promo1, promo2>::value;
81  static const bool lbase = std::is_base_of<promo1, promo2>::value;
82  static const bool rbase = std::is_base_of<promo2, promo1>::value;
83 
84  template <class U1, class U2, bool, bool> struct get_type { typedef NotATypeError type; };
85  template <class U1, class U2> struct get_type<U1, U2, true, false> { typedef U1 type; };
86  template <class U1, class U2> struct get_type<U1, U2, false, true> { typedef U2 type; };
87  template <class U1, class U2> struct get_type<U1, U2, true, true> { typedef U1 type; };
88 
89  public:
90 
91  //if you want a specialization for your type, just implement these two:
92  typedef typename get_type<promo1, promo2, same || lbase, rbase>::type type;
93  static const bool value = same || lbase || rbase;
94  };
95 
96  template <class Pix1, class Pix2>
97  class are_comparable<Pix1, Pix2, true, true>
98  {
99  typedef typename Pix1::PODType P1;
100  typedef typename Pix2::PODType P2;
101 
102  typedef typename Pix1::ExtraPODType ExtraP1;
103  typedef typename Pix2::ExtraPODType ExtraP2;
104 
105  typedef typename nrt::promote<P1,P2>::type promo;
106  typedef typename nrt::promote<ExtraP1,ExtraP2>::type extrapromo;
107 
108  typedef typename Pix1::template rebind_extra<promo,extrapromo>::type promo1;
109  typedef typename Pix2::template rebind_extra<promo,extrapromo>::type promo2;
110 
111  static const bool same = std::is_same<promo1, promo2>::value;
112  static const bool lbase = std::is_base_of<promo1, promo2>::value;
113  static const bool rbase = std::is_base_of<promo2, promo1>::value;
114 
115  template <class U1, class U2, bool, bool> struct get_type { typedef NotATypeError type; };
116  template <class U1, class U2> struct get_type<U1, U2, true, false> { typedef typename Pix1::template rebind_extra<promo, ExtraP1>::type type; };
117  template <class U1, class U2> struct get_type<U1, U2, false, true> { typedef typename Pix2::template rebind_extra<promo, ExtraP2>::type type; };
118  template <class U1, class U2> struct get_type<U1, U2, true, true> { typedef typename Pix1::template rebind_extra<promo, ExtraP1>::type type; };
119 
120  public:
121 
122  //if you want a specialization for your type, just implement these two:
123  typedef typename get_type<promo1, promo2, same || lbase, rbase>::type type;
124  static const bool value = same || lbase || rbase;
125 
126  };
127 
128  /* example of a specialization to alow PixRGBA to operate and promote with a PixRGB */
129  /*
130  template <typemame> struct PixRGB;
131  template <typemame> struct PixRGBA;
132 
133  template <typename P1, typename P2>
134  class are_comparable<PixRGB, PixRGBA, P1, P2, true>
135  {
136  typedef typename nrt::promote<P1,P2>::type promo; //promote the pod type
137  typedef PixRGB<promo> type; //PixRGB is the promoted type
138  static const bool value = 1; //enable this specialization
139  };
140  */
141  }
142 }
143 
144 #endif // INCLUDE_NRT_CORE_TYPING_DETAILS_PIXELTRAITSHELPERS_H