stereo
store_vector.hpp
Go to the documentation of this file.
1 #ifndef __STORE_VECTOR_HPP__
2 #define __STORE_VECTOR_HPP__
3 
4 #include <opencv2/opencv.hpp>
5 
6 /******
7 simple templace functions for storing/loading stereo vectors. supports the class version as well as the POD version and stream version of target template storage, e.g. if target template is a basic type, use POD, if target template is a class which has the storeKeyPoint and loadKeyPoint functions implemented use class
8 ******/
9 template<typename T>
10 void storeKeyPoint(const T& kp, std::ostream& os)
11 {
12  os << kp.angle << "\n";
13  os << kp.class_id << "\n";
14  os << kp.octave << "\n";
15  os << kp.pt.x << " " << kp.pt.y << "\n";
16  os << kp.response << "\n";
17  os << kp.size << "\n";
18 };
19 
20 template<typename T>
21 void loadKeyPoint(T& kp, std::istream& is)
22 {
23  is >> kp.angle;
24  is.ignore(10, '\n');
25 
26  is >> kp.class_id;
27  is.ignore(10, '\n');
28 
29  is >> kp.octave;
30  is.ignore(10, '\n');
31 
32  float d1, d2;
33  is >> d1;
34  is >> d2;
35  is.ignore(10, '\n');
36  kp.pt = cv::Point2f(d1, d2);
37 
38  is >> kp.response;
39  is.ignore(10, '\n');
40 
41  is >> kp.size;
42  is.ignore(10, '\n');
43 };
44 
45 
46 template<typename T>
47 void StoreClassVector(const std::vector<T>& Tvector, std::ostream& os)
48 {
49  typename std::vector<T>::size_type size(Tvector.size());
50  os.write(reinterpret_cast<const char*>(&size), sizeof(typename std::vector<T>::size_type));
51  for(typename std::vector<T>::size_type i =0; i < size; ++i)
52  {
53  storeKeyPoint(Tvector[i], os);
54  }
55 };
56 
57 template<typename T>
58 void LoadClassVector(std::vector<T>& Tvector, std::istream& is, int max_load_indices = -1)
59 {
60  typename std::vector<T>::size_type to_load_size(0);
61  is.read(reinterpret_cast<char*>(&to_load_size), sizeof(typename std::vector<T>::size_type));
62  // check, if the whole vector should be loaded or only the indices until max_load_indices is reached
63  if(max_load_indices > -1)
64  to_load_size = ((int)to_load_size < max_load_indices)?to_load_size:max_load_indices;
65  Tvector.resize(to_load_size);
66  for(typename std::vector<T>::size_type i =0; i < to_load_size; ++i)
67  {
68  loadKeyPoint(Tvector[i], is);
69  }
70 };
71 
72 template<typename T>
73 void StorePODVector(const std::vector<T>& Tvector, std::ostream& os)
74 {
75  typename std::vector<T>::size_type size(Tvector.size());
76  os.write(reinterpret_cast<const char*>(&size), sizeof(typename std::vector<T>::size_type));
77  for(typename std::vector<T>::size_type i = 0; i < size; i++)
78  os << Tvector[i] << " ";
79 };
80 
81 template<typename T>
82 void LoadPODVector(std::vector<T>& Tvector, std::istream& is)
83 {
84  typename std::vector<T>::size_type to_load_size(0);
85  is.read(reinterpret_cast<char*>(&to_load_size), sizeof(typename std::vector<T>::size_type));
86  T temp;
87  for(typename std::vector<T>::size_type i = 0; i < to_load_size; i++)
88  {
89  is >> temp;
90  Tvector.push_back(temp);
91  }
92 };
93 
94 template<typename T>
95 void StoreEigenMatrix3d(const T &matrix, std::ostream& os)
96 {
97  for(int i = 0; i < 3; ++i)
98  {
99  for(int j = 0; j < 3; ++j)
100  {
101  os << matrix(i, j) << " ";
102  }
103  }
104  os << "\n";
105 };
106 
107 template<typename T>
108 void LoadEigenMatrix3d(T &matrix, std::istream& is)
109 {
110  double a;
111  for(int i = 0; i < 3; ++i)
112  {
113  for(int j = 0; j < 3; ++j)
114  {
115  is >> a;
116  matrix(i, j) = a;
117  }
118  }
119  is.ignore(10, '\n');
120 };
121 
122 template<typename T>
123 void StoreEigenMatrix4d(const T &matrix, std::ostream& os)
124 {
125  for(int i = 0; i < 4; ++i)
126  {
127  for(int j = 0; j < 4; ++j)
128  {
129  os << matrix(i, j) << " ";
130  }
131  }
132  os << "\n";
133 };
134 
135 template<typename T>
136 void LoadEigenMatrix4d(T &matrix, std::istream& is)
137 {
138  double a;
139  for(int i = 0; i < 4; ++i)
140  {
141  for(int j = 0; j < 4; ++j)
142  {
143  is >> a;
144  matrix(i, j) = a;
145  }
146  }
147  is.ignore(10, '\n');
148 };
149 
150 #endif
void LoadPODVector(std::vector< T > &Tvector, std::istream &is)
Definition: store_vector.hpp:82
void LoadEigenMatrix3d(T &matrix, std::istream &is)
Definition: store_vector.hpp:108
void StoreEigenMatrix4d(const T &matrix, std::ostream &os)
Definition: store_vector.hpp:123
void loadKeyPoint(T &kp, std::istream &is)
Definition: store_vector.hpp:21
void StoreEigenMatrix3d(const T &matrix, std::ostream &os)
Definition: store_vector.hpp:95
void storeKeyPoint(const T &kp, std::ostream &os)
Definition: store_vector.hpp:10
void StoreClassVector(const std::vector< T > &Tvector, std::ostream &os)
Definition: store_vector.hpp:47
void LoadEigenMatrix4d(T &matrix, std::istream &is)
Definition: store_vector.hpp:136
void StorePODVector(const std::vector< T > &Tvector, std::ostream &os)
Definition: store_vector.hpp:73
void LoadClassVector(std::vector< T > &Tvector, std::istream &is, int max_load_indices=-1)
Definition: store_vector.hpp:58