core/vbl/vbl_attributes.txx
Go to the documentation of this file.
00001 #ifndef vbl_attributes_txx_
00002 #define vbl_attributes_txx_
00003 #include "vbl_attributes.h"
00004 //:
00005 // \file
00006 
00007 #include <vcl_algorithm.h> // for std::sort()
00008 #include <vcl_cassert.h>
00009 
00010 // ------------ 1d array ----------
00011 
00012 //: minimum element value
00013 template <class T>
00014 T minval(vbl_array_1d<T> const& in)
00015 {
00016   assert(in.size()>0);
00017   T mval = *(in.begin());
00018   for (typename vbl_array_1d<T>::const_iterator it = in.begin();
00019        it != in.end(); ++it) {
00020     T v = *it;
00021     if (v<mval) mval = v;
00022   }
00023   return mval;
00024 }
00025 
00026 //: maximum element value
00027 template <class T>
00028 T maxval(vbl_array_1d<T> const& in)
00029 {
00030   assert(in.size()>0);
00031   T mval = *(in.begin());
00032   for (typename vbl_array_1d<T>::const_iterator it = in.begin();
00033        it != in.end(); ++it) {
00034     T v = *it;
00035     if (v>mval) mval = v;
00036   }
00037   return mval;
00038 }
00039 
00040 //: min max bounds
00041 template <class T>
00042 void bounds(vbl_array_1d<T> const& in, T& min, T& max)
00043 {
00044   assert(in.size()>0);
00045   max = min = *(in.begin());
00046   for (typename vbl_array_1d<T>::const_iterator it = in.begin();
00047        it != in.end(); ++it) {
00048     T v = *it;
00049     if (v>max) max = v;
00050     if (v<min) min = v;
00051   }
00052 }
00053 
00054 //: mean of element values
00055 template <class T>
00056 T mean(vbl_array_1d<T> const& in)
00057 {
00058   T sum = T(0);
00059   vcl_size_t n = in.size();
00060   assert(n>0);
00061   for (typename vbl_array_1d<T>::const_iterator it = in.begin();
00062        it != in.end(); ++it)
00063     sum += *it;
00064   return sum/static_cast<T>(n);
00065 }
00066 
00067 // ----------------- 2d array ------------
00068 
00069 //: minimum element value
00070 template <class T>
00071 T minval(vbl_array_2d<T> const& in)
00072 {
00073   assert(in.size()>0);
00074   T mval = *(in.begin());
00075   for (typename vbl_array_2d<T>::const_iterator it = in.begin();
00076        it != in.end(); ++it) {
00077     T v = *it;
00078     if (v<mval) mval = v;
00079   }
00080   return mval;
00081 }
00082 
00083 //: maximum element value
00084 template <class T>
00085 T maxval(vbl_array_2d<T> const& in)
00086 {
00087   assert(in.size()>0);
00088   T mval = *(in.begin());
00089   for (typename vbl_array_2d<T>::const_iterator it = in.begin();
00090        it != in.end(); ++it) {
00091     T v = *it;
00092     if (v>mval) mval = v;
00093   }
00094   return mval;
00095 }
00096 
00097 //: min max bounds
00098 template <class T>
00099 void bounds(vbl_array_2d<T> const& in, T& min, T& max)
00100 {
00101   assert(in.size()>0);
00102   max = min = *(in.begin());
00103   for (typename vbl_array_2d<T>::const_iterator it = in.begin();
00104        it != in.end(); ++it) {
00105     T v = *it;
00106     if (v>max) max = v;
00107     if (v<min) min = v;
00108   }
00109 }
00110 
00111 //: mean of element values
00112 template <class T>
00113 T mean(vbl_array_2d<T> const& in)
00114 {
00115   T sum = T(0);
00116   vcl_size_t n = in.size();
00117   assert(n>0);
00118   for (typename vbl_array_2d<T>::const_iterator it = in.begin();
00119        it != in.end(); ++it)
00120     sum += *it;
00121   return sum/static_cast<T>(n);
00122 }
00123 
00124 // ----------------- 3d array ------------
00125 
00126 //: minimum element value
00127 template <class T>
00128 T minval(vbl_array_3d<T> const& in)
00129 {
00130   assert(in.size()>0);
00131   T mval = *(in.begin());
00132   for (typename vbl_array_3d<T>::const_iterator it = in.begin();
00133        it != in.end(); ++it) {
00134     T v = *it;
00135     if (v<mval) mval = v;
00136   }
00137   return mval;
00138 }
00139 
00140 //: maximum element value
00141 template <class T>
00142 T maxval(vbl_array_3d<T> const& in)
00143 {
00144   assert(in.size()>0);
00145   T mval = *(in.begin());
00146   for (typename vbl_array_3d<T>::const_iterator it = in.begin();
00147        it != in.end(); ++it) {
00148     T v = *it;
00149     if (v>mval) mval = v;
00150   }
00151   return mval;
00152 }
00153 
00154 //: min max bounds
00155 template <class T>
00156 void bounds(vbl_array_3d<T> const& in, T& min, T& max)
00157 {
00158   assert(in.size()>0);
00159   max = min = *(in.begin());
00160   for (typename vbl_array_3d<T>::const_iterator it = in.begin();
00161        it != in.end(); ++it) {
00162     T v = *it;
00163     if (v>max) max = v;
00164     if (v<min) min = v;
00165   }
00166 }
00167 
00168 //: mean of element values
00169 template <class T>
00170 T mean(vbl_array_3d<T> const& in)
00171 {
00172   T sum = T(0);
00173   vcl_size_t n = in.size();
00174   assert(n>0);
00175   for (typename vbl_array_3d<T>::const_iterator it = in.begin();
00176        it != in.end(); ++it)
00177     sum += *it;
00178   return sum/static_cast<T>(n);
00179 }
00180 
00181 //: median (actually: left-median) of element values
00182 template <class T>
00183 T median(vbl_array_1d<T> const& in)
00184 {
00185   assert(in.size()>0);
00186   vbl_array_1d<T> t = in;
00187   vcl_sort(t.begin(), t.end());
00188   return *(t.begin() + (in.size()/2));
00189 }
00190 
00191 //: median (actually: left-median) of element values
00192 template <class T>
00193 T median(vbl_array_2d<T> const& in)
00194 {
00195   assert(in.size()>0);
00196   vbl_array_2d<T> t = in;
00197   vcl_sort(t.begin(), t.end());
00198   return *(t.begin() + (in.size()/2));
00199 }
00200 
00201 //: median (actually: left-median) of element values
00202 template <class T>
00203 T median(vbl_array_3d<T> const& in)
00204 {
00205   assert(in.size()>0);
00206   vbl_array_3d<T> t = in;
00207   vcl_sort(t.begin(), t.end());
00208   return *(t.begin() + (in.size()/2));
00209 }
00210 
00211 #define VBL_ATTRIBUTES_INSTANTIATE(T) \
00212 template T minval(vbl_array_1d<T >const&); \
00213 template T maxval(vbl_array_1d<T >const&); \
00214 template void bounds(vbl_array_1d<T >const&, T&, T&); \
00215 template T mean(vbl_array_1d<T >const&);   \
00216 template T median(vbl_array_1d<T >const&); \
00217 template T minval(vbl_array_2d<T >const&); \
00218 template T maxval(vbl_array_2d<T >const&); \
00219 template void bounds(vbl_array_2d<T >const&, T&, T&); \
00220 template T mean(vbl_array_2d<T >const&);   \
00221 template T median(vbl_array_2d<T >const&); \
00222 template T minval(vbl_array_3d<T >const&); \
00223 template T maxval(vbl_array_3d<T >const&); \
00224 template void bounds(vbl_array_3d<T >const&, T&, T&); \
00225 template T mean(vbl_array_3d<T >const&); \
00226 template T median(vbl_array_3d<T >const&)
00227 
00228 #endif // vbl_attributes_txx_