core/vpdl/vpdt/vpdt_access.h
Go to the documentation of this file.
00001 // This is core/vpdl/vpdt/vpdt_access.h
00002 #ifndef vpdt_traits_h_
00003 #define vpdt_traits_h_
00004 //:
00005 // \file
00006 // \author Matthew Leotta
00007 // \brief Overloaded functions to allow uniform API access to various field types
00008 // \date March 5, 2009
00009 //
00010 // Since the same template code may apply to both scalars and vectors,
00011 // we need a standard set of functions to treat scalars as a 1-d vector.
00012 // Likewise, we need uniform access to variable and fixed size vectors.
00013 // This includes functions to access dimension, set dimension, access elements,
00014 // and more.
00015 //
00016 // \verbatim
00017 //  Modifications
00018 //   <None yet>
00019 // \endverbatim
00020 
00021 #include <vnl/vnl_vector.h>
00022 #include <vnl/vnl_matrix.h>
00023 #include <vnl/vnl_vector_fixed.h>
00024 #include <vnl/vnl_matrix_fixed.h>
00025 #include <vpdl/vpdt/vpdt_eigen_sym_matrix.h>
00026 #include <vcl_cassert.h>
00027 
00028 //==============================================================================
00029 // vpdt_size
00030 
00031 //: Access the size of a vnl_vector
00032 template <class T>
00033 inline unsigned int vpdt_size(const vnl_vector<T>& v) { return v.size(); }
00034 
00035 //: Access the size of a square vnl_matrix
00036 template <class T>
00037 inline unsigned int vpdt_size(const vnl_matrix<T>& m)
00038 {
00039   assert(m.cols() == m.rows());
00040   return m.cols();
00041 }
00042 
00043 //: Access the size of a vnl_vector_fixed
00044 template <class T, unsigned int n>
00045 inline unsigned int vpdt_size(const vnl_vector_fixed<T,n>& /*v*/) { return n; }
00046 
00047 //: Access the size of a square vnl_matrix_fixed
00048 template <class T, unsigned int n>
00049 inline unsigned int vpdt_size(const vnl_matrix_fixed<T,n,n>& /*m*/) { return n; }
00050 
00051 //: Access the size of a scalar
00052 inline unsigned int vpdt_size(float /*v*/) { return 1; }
00053 inline unsigned int vpdt_size(double /*v*/) { return 1; }
00054 
00055 //==============================================================================
00056 // vpdt_set_size
00057 
00058 //: Set the size of a vnl_vector
00059 template <class T>
00060 inline void vpdt_set_size(vnl_vector<T>& v, unsigned int s) { v.set_size(s); }
00061 
00062 //: Set the size of a square vnl_matrix
00063 template <class T>
00064 inline void vpdt_set_size(vnl_matrix<T>& m, unsigned int s) { m.set_size(s,s); }
00065 
00066 //: Default case, do nothing
00067 template <class T>
00068 inline void vpdt_set_size(T& /*v*/, unsigned int /*s*/) {}
00069 
00070 //==============================================================================
00071 // vpdt_fill
00072 
00073 //: Fill a vnl_vector
00074 template <class T>
00075 inline void vpdt_fill(vnl_vector<T>& v, const T& val) { v.fill(val); }
00076 
00077 //: Fill a square vnl_matrix
00078 template <class T>
00079 inline void vpdt_fill(vnl_matrix<T>& m, const T& val) { m.fill(val); }
00080 
00081 //: Fill a vnl_vector_fixed
00082 template <class T, unsigned int n>
00083 inline void vpdt_fill(vnl_vector_fixed<T,n>& v, const T& val) { v.fill(val); }
00084 
00085 //: Fill a square vnl_matrix_fixed
00086 template <class T, unsigned int n>
00087 inline void vpdt_fill(vnl_matrix_fixed<T,n,n>& m, const T& val) { m.fill(val); }
00088 
00089 //: Default case, assignment
00090 template <class T>
00091 inline void vpdt_fill(T& v, const T& val) { v = val; }
00092 
00093 //==============================================================================
00094 // vpdt_index (vector)
00095 
00096 //: Index into a vnl_vector
00097 template <class T>
00098 inline T& vpdt_index(vnl_vector<T>& v, unsigned int i) { assert(i < v.size()); return v[i]; }
00099 //: Index into a vnl_vector (const)
00100 template <class T>
00101 inline const T& vpdt_index(const vnl_vector<T>& v, unsigned int i) { assert(i < v.size()); return v[i]; }
00102 
00103 //: Index into a vnl_vector_fixed
00104 template <class T, unsigned int n>
00105 inline T& vpdt_index(vnl_vector_fixed<T,n>& v, unsigned int i) { assert(i < n); return v[i]; }
00106 //: Index into a vnl_vector_fixed (const)
00107 template <class T, unsigned int n>
00108 inline const T& vpdt_index(const vnl_vector_fixed<T,n>& v, unsigned int i) { assert(i < n); return v[i]; }
00109 
00110 //: Index into a scalar
00111 template <class T>
00112 inline T& vpdt_index(T& v, unsigned int /*i*/) { return v; }
00113 //: Index into a scalar (const)
00114 template <class T>
00115 inline const T& vpdt_index(const T& v, unsigned int /*i*/) { return v; }
00116 
00117 //==============================================================================
00118 // vpdt_index (matrix)
00119 
00120 //: Index into a vnl_matrix
00121 template <class T>
00122 inline T& vpdt_index(vnl_matrix<T>& v, unsigned int i, unsigned int j) { assert(i < v.rows() && j < v.columns()); return v(i,j); }
00123 //: Index into a vnl_matrix (const)
00124 template <class T>
00125 inline const T& vpdt_index(const vnl_matrix<T>& v, unsigned int i, unsigned int j) { assert(i < v.rows() && j < v.columns()); return v(i,j); }
00126 
00127 //: Index into a vnl_matrix_fixed
00128 template <class T, unsigned int n>
00129 inline T& vpdt_index(vnl_matrix_fixed<T,n,n>& v, unsigned int i, unsigned int j) { assert(i < n && j < n); return v(i,j); }
00130 //: Index into a vnl_matrix_fixed (const)
00131 template <class T, unsigned int n>
00132 inline const T& vpdt_index(const vnl_matrix_fixed<T,n,n>& v, unsigned int i, unsigned int j) { assert(i < n && j < n); return v(i,j); }
00133 
00134 //: Index into a scalar
00135 template <class T>
00136 inline T& vpdt_index(T& v, unsigned int /*i*/, unsigned int /*j*/) { return v; }
00137 //: Index into a scalar (const)
00138 template <class T>
00139 inline const T& vpdt_index(const T& v, unsigned int /*i*/, unsigned int /*j*/) { return v; }
00140 
00141 
00142 //==============================================================================
00143 // misc
00144 
00145 //: vnl defines outer_product for vectors but not scalars
00146 inline float outer_product(const float& v1, const float& v2) { return v1*v2; }
00147 inline double outer_product(const double& v1, const double& v2) { return v1*v2; }
00148 
00149 
00150 #endif // vpdt_traits_h_