vpdt_access.h
Go to the documentation of this file.
1 // This is core/vpdl/vpdt/vpdt_access.h
2 #ifndef vpdt_traits_h_
3 #define vpdt_traits_h_
4 //:
5 // \file
6 // \author Matthew Leotta
7 // \brief Overloaded functions to allow uniform API access to various field types
8 // \date March 5, 2009
9 //
10 // Since the same template code may apply to both scalars and vectors,
11 // we need a standard set of functions to treat scalars as a 1-d vector.
12 // Likewise, we need uniform access to variable and fixed size vectors.
13 // This includes functions to access dimension, set dimension, access elements,
14 // and more.
15 //
16 // \verbatim
17 // Modifications
18 // <None yet>
19 // \endverbatim
20 
21 #include <vnl/vnl_vector.h>
22 #include <vnl/vnl_matrix.h>
23 #include <vnl/vnl_vector_fixed.h>
24 #include <vnl/vnl_matrix_fixed.h>
26 #include <cassert>
27 #ifdef _MSC_VER
28 # include <vcl_msvc_warnings.h>
29 #endif
30 
31 //==============================================================================
32 // vpdt_size
33 
34 //: Access the size of a vnl_vector
35 template <class T>
36 inline unsigned int vpdt_size(const vnl_vector<T>& v) { return v.size(); }
37 
38 //: Access the size of a square vnl_matrix
39 template <class T>
40 inline unsigned int vpdt_size(const vnl_matrix<T>& m)
41 {
42  assert(m.cols() == m.rows());
43  return m.cols();
44 }
45 
46 //: Access the size of a vnl_vector_fixed
47 template <class T, unsigned int n>
48 inline unsigned int vpdt_size(const vnl_vector_fixed<T,n>& /*v*/) { return n; }
49 
50 //: Access the size of a square vnl_matrix_fixed
51 template <class T, unsigned int n>
52 inline unsigned int vpdt_size(const vnl_matrix_fixed<T,n,n>& /*m*/) { return n; }
53 
54 //: Access the size of a scalar
55 inline unsigned int vpdt_size(float /*v*/) { return 1; }
56 inline unsigned int vpdt_size(double /*v*/) { return 1; }
57 
58 //==============================================================================
59 // vpdt_set_size
60 
61 //: Set the size of a vnl_vector
62 template <class T>
63 inline void vpdt_set_size(vnl_vector<T>& v, unsigned int s) { v.set_size(s); }
64 
65 //: Set the size of a square vnl_matrix
66 template <class T>
67 inline void vpdt_set_size(vnl_matrix<T>& m, unsigned int s) { m.set_size(s,s); }
68 
69 //: Default case, do nothing
70 template <class T>
71 inline void vpdt_set_size(T& /*v*/, unsigned int /*s*/) {}
72 
73 //==============================================================================
74 // vpdt_fill
75 
76 //: Fill a vnl_vector
77 template <class T>
78 inline void vpdt_fill(vnl_vector<T>& v, const T& val) { v.fill(val); }
79 
80 //: Fill a square vnl_matrix
81 template <class T>
82 inline void vpdt_fill(vnl_matrix<T>& m, const T& val) { m.fill(val); }
83 
84 //: Fill a vnl_vector_fixed
85 template <class T, unsigned int n>
86 inline void vpdt_fill(vnl_vector_fixed<T,n>& v, const T& val) { v.fill(val); }
87 
88 //: Fill a square vnl_matrix_fixed
89 template <class T, unsigned int n>
90 inline void vpdt_fill(vnl_matrix_fixed<T,n,n>& m, const T& val) { m.fill(val); }
91 
92 //: Default case, assignment
93 template <class T>
94 inline void vpdt_fill(T& v, const T& val) { v = val; }
95 
96 //==============================================================================
97 // vpdt_index (vector)
98 
99 //: Index into a vnl_vector
100 template <class T>
101 inline T& vpdt_index(vnl_vector<T>& v, unsigned int i) { assert(i < v.size()); return v[i]; }
102 //: Index into a vnl_vector (const)
103 template <class T>
104 inline const T& vpdt_index(const vnl_vector<T>& v, unsigned int i) { assert(i < v.size()); return v[i]; }
105 
106 //: Index into a vnl_vector_fixed
107 template <class T, unsigned int n>
108 inline T& vpdt_index(vnl_vector_fixed<T,n>& v, unsigned int i) { assert(i < n); return v[i]; }
109 //: Index into a vnl_vector_fixed (const)
110 template <class T, unsigned int n>
111 inline const T& vpdt_index(const vnl_vector_fixed<T,n>& v, unsigned int i) { assert(i < n); return v[i]; }
112 
113 //: Index into a scalar
114 template <class T>
115 inline T& vpdt_index(T& v, unsigned int /*i*/) { return v; }
116 //: Index into a scalar (const)
117 template <class T>
118 inline const T& vpdt_index(const T& v, unsigned int /*i*/) { return v; }
119 
120 //==============================================================================
121 // vpdt_index (matrix)
122 
123 //: Index into a vnl_matrix
124 template <class T>
125 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); }
126 //: Index into a vnl_matrix (const)
127 template <class T>
128 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); }
129 
130 //: Index into a vnl_matrix_fixed
131 template <class T, unsigned int n>
132 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); }
133 //: Index into a vnl_matrix_fixed (const)
134 template <class T, unsigned int n>
135 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); }
136 
137 //: Index into a scalar
138 template <class T>
139 inline T& vpdt_index(T& v, unsigned int /*i*/, unsigned int /*j*/) { return v; }
140 //: Index into a scalar (const)
141 template <class T>
142 inline const T& vpdt_index(const T& v, unsigned int /*i*/, unsigned int /*j*/) { return v; }
143 
144 
145 //==============================================================================
146 // misc
147 
148 //: vnl defines outer_product for vectors but not scalars
149 inline float outer_product(const float& v1, const float& v2) { return v1*v2; }
150 inline double outer_product(const double& v1, const double& v2) { return v1*v2; }
151 
152 
153 #endif // vpdt_traits_h_
#define m
#define v
T & vpdt_index(vnl_vector< T > &v, unsigned int i)
Index into a vnl_vector.
Definition: vpdt_access.h:101
void vpdt_set_size(vnl_vector< T > &v, unsigned int s)
Set the size of a vnl_vector.
Definition: vpdt_access.h:63
A symmetric matrix represented in eigenvalue decomposition.
unsigned int vpdt_size(const vnl_vector< T > &v)
Access the size of a vnl_vector.
Definition: vpdt_access.h:36
void vpdt_fill(vnl_vector< T > &v, const T &val)
Fill a vnl_vector.
Definition: vpdt_access.h:78
float outer_product(const float &v1, const float &v2)
vnl defines outer_product for vectors but not scalars.
Definition: vpdt_access.h:149