GVFUtils.h
1 //
2 // GVFTypesAndUtils.h
3 //
4 //
5 //
6 
7 #ifndef __H_GVFTYPES
8 #define __H_GVFTYPES
9 
10 #include <map>
11 #include <vector>
12 #include <iostream>
13 #include <random>
14 #include <iostream>
15 #include <math.h>
16 #include <assert.h>
17 
18 using namespace std;
19 
23 typedef struct
24 {
26  bool translate;
27  bool segmentation;
28 } GVFConfig;
29 
33 typedef struct
34 {
35  float tolerance;
36  float distribution;
37  int numberParticles;
38  int resamplingThreshold;
39  float alignmentVariance;
40  float speedVariance;
41  vector<float> scaleVariance;
42  vector<float> dynamicsVariance;
43  vector<float> scalingsVariance;
44  vector<float> rotationsVariance;
45  // spreadings
46  float alignmentSpreadingCenter;
47  float alignmentSpreadingRange;
48  float dynamicsSpreadingCenter;
49  float dynamicsSpreadingRange;
50  float scalingsSpreadingCenter;
51  float scalingsSpreadingRange;
52  float rotationsSpreadingCenter;
53  float rotationsSpreadingRange;
54 
55  int predictionSteps;
56  vector<float> dimWeights;
58 
59 // Outcomes structure
60 typedef struct
61 {
62  int likeliestGesture;
63  vector<float> likelihoods;
64  vector<float> alignments;
65  vector<vector<float> > dynamics;
66  vector<vector<float> > scalings;
67  vector<vector<float> > rotations;
68 } GVFOutcomes;
69 
70 
71 //--------------------------------------------------------------
72 // init matrix by allocating memory
73 template <typename T>
74 inline void initMat(vector< vector<T> > & M, int rows, int cols){
75  M.resize(rows);
76  for (int n=0; n<rows; n++){
77  M[n].resize(cols);
78  }
79 }
80 
81 //--------------------------------------------------------------
82 // init matrix and copy values from another matrix
83 template <typename T>
84 inline void setMat(vector< vector<T> > & C, vector< vector<float> > & M){
85  int rows = M.size();
86  int cols = M[0].size();
87  //C.resize(rows);
88  C = vector<vector<T> >(rows);
89  for (int n=0; n<rows; n++){
90  //C[n].resize(cols);
91  C[n] = vector<T>(cols);
92  for (int m=0;m<cols;m++){
93  C[n][m] = M[n][m];
94  }
95  }
96 }
97 
98 //--------------------------------------------------------------
99 // init matrix by allocating memory and fill with T value
100 template <typename T>
101 inline void setMat(vector< vector<T> > & M, T value, int rows, int cols){
102  M.resize(rows);
103  for (int n=0; n<rows; n++){
104  M[n].resize(cols);
105  for (int m=0; m<cols; m++){
106  M[n][m] = value;
107  }
108  }
109 }
110 
111 //--------------------------------------------------------------
112 // set matrix filled with T value
113 template <typename T>
114 inline void setMat(vector< vector<T> > & M, T value){
115  for (int n=0; n<M.size(); n++){
116  for (int m=0; m<M[n].size(); m++){
117  M[n][m] = value;
118  }
119  }
120 }
121 
122 //--------------------------------------------------------------
123 template <typename T>
124 inline void printMat(vector< vector<T> > & M){
125  for (int k=0; k<M.size(); k++){
126  cout << k << ": ";
127  for (int l=0; l<M[0].size(); l++){
128  cout << M[k][l] << " ";
129  }
130  cout << endl;
131  }
132  cout << endl;
133 }
134 
135 //--------------------------------------------------------------
136 template <typename T>
137 inline void printVec(vector<T> & V){
138  for (int k=0; k<V.size(); k++){
139  cout << k << ": " << V[k] << (k == V.size() - 1 ? "" : " ,");
140  }
141  cout << endl;
142 }
143 
144 //--------------------------------------------------------------
145 template <typename T>
146 inline void initVec(vector<T> & V, int rows){
147  V.resize(rows);
148 }
149 
150 //--------------------------------------------------------------
151 template <typename T>
152 inline void setVec(vector<T> & C, vector<int> &V){
153  int rows = V.size();
154  C = vector<T>(rows);
155  //C.resize(rows);
156  for (int n=0; n<rows; n++){
157  C[n] = V[n];
158  }
159 }
160 
161 //--------------------------------------------------------------
162 template <typename T>
163 inline void setVec(vector<T> & C, vector<float> & V){
164  int rows = V.size();
165  C.resize(rows);
166  for (int n=0; n<rows; n++){
167  C[n] = V[n];
168  }
169 }
170 
171 //--------------------------------------------------------------
172 template <typename T>
173 inline void setVec(vector<T> & V, T value){
174  for (int n=0; n<V.size(); n++){
175  V[n] = value;
176  }
177 }
178 
179 //--------------------------------------------------------------
180 template <typename T>
181 inline void setVec(vector<T> & V, T value, int rows){
182  V.resize(rows);
183  setVec(V, value);
184 }
185 
186 //--------------------------------------------------------------
187 template <typename T>
188 inline vector< vector<T> > dotMat(vector< vector<T> > & M1, vector< vector<T> > & M2){
189  // TODO(Baptiste)
190 }
191 
192 //--------------------------------------------------------------
193 template <typename T>
194 inline vector< vector<T> > multiplyMatf(vector< vector<T> > & M1, T v){
195  vector< vector<T> > multiply;
196  initMat(multiply, M1.size(), M1[0].size());
197  for (int i=0; i<M1.size(); i++){
198  for (int j=0; j<M1[i].size(); j++){
199  multiply[i][j] = M1[i][j] * v;
200  }
201  }
202  return multiply;
203 }
204 
205 //--------------------------------------------------------------
206 template <typename T>
207 inline vector< vector<T> > multiplyMatf(vector< vector<T> > & M1, vector< vector<T> > & M2){
208  assert(M1[0].size() == M2.size()); // columns in M1 == rows in M2
209  vector< vector<T> > multiply;
210  initMat(multiply, M1.size(), M2[0].size()); // rows in M1 x cols in M2
211  for (int i=0; i<M1.size(); i++){
212  for (int j=0; j<M2[i].size(); j++){
213  multiply[i][j] = 0.0f;
214  for(int k=0; k<M1[0].size(); k++){
215  multiply[i][j] += M1[i][k] * M2[k][j];
216  }
217 
218  }
219  }
220  return multiply;
221 }
222 
223 //--------------------------------------------------------------
224 template <typename T>
225 inline vector<T> multiplyMat(vector< vector<T> > & M1, vector< T> & Vect){
226  assert(Vect.size() == M1[0].size()); // columns in M1 == rows in Vect
227  vector<T> multiply;
228  initVec(multiply, Vect.size());
229  for (int i=0; i<M1.size(); i++){
230  multiply[i] = 0.0f;
231  for (int j=0; j<M1[i].size(); j++){
232  multiply[i] += M1[i][j] * Vect[j];
233  }
234  }
235  return multiply;
236 }
237 
238 //--------------------------------------------------------------
239 template <typename T>
240 inline float getMeanVec(vector<T>& V){
241  float tSum = 0.0f;
242  for (int n=0; n<V.size(); n++){
243  tSum += V[n];
244  }
245  return tSum / (float)V.size();
246 }
247 
248 template <typename T>
249 inline vector<vector<float> > getRotationMatrix3d(T phi, T theta, T psi)
250 {
251  vector< vector<float> > M;
252  initMat(M,3,3);
253 
254  M[0][0] = cos(theta)*cos(psi);
255  M[0][1] = -cos(phi)*sin(psi)+sin(phi)*sin(theta)*cos(psi);
256  M[0][2] = sin(phi)*sin(psi)+cos(phi)*sin(theta)*cos(psi);
257 
258  M[1][0] = cos(theta)*sin(psi);
259  M[1][1] = cos(phi)*cos(psi)+sin(phi)*sin(theta)*sin(psi);
260  M[1][2] = -sin(phi)*cos(psi)+cos(phi)*sin(theta)*sin(psi);
261 
262  M[2][0] = -sin(theta);
263  M[2][1] = sin(phi)*cos(theta);
264  M[2][2] = cos(phi)*cos(theta);
265 
266  return M;
267 }
268 
269 template <typename T>
270 float distance_weightedEuclidean(vector<T> x, vector<T> y, vector<T> w)
271 {
272  int count = x.size();
273  if (count <= 0) return 0;
274  float dist = 0.0;
275  for(int k = 0; k < count; k++) dist += w[k] * pow((x[k] - y[k]), 2);
276  return dist;
277 }
278 
280 //vector<vector<float> > getRotationMatrix3d(float phi, float theta, float psi)
281 //{
282 // vector< vector<float> > M;
283 // initMat(M,3,3);
284 //
285 // M[0][0] = cos(theta)*cos(psi);
286 // M[0][1] = -cos(phi)*sin(psi)+sin(phi)*sin(theta)*cos(psi);
287 // M[0][2] = sin(phi)*sin(psi)+cos(phi)*sin(theta)*cos(psi);
288 //
289 // M[1][0] = cos(theta)*sin(psi);
290 // M[1][1] = cos(phi)*cos(psi)+sin(phi)*sin(theta)*sin(psi);
291 // M[1][2] = -sin(phi)*cos(psi)+cos(phi)*sin(theta)*sin(psi);
292 //
293 // M[2][0] = -sin(theta);
294 // M[2][1] = sin(phi)*cos(theta);
295 // M[2][2] = cos(phi)*cos(theta);
296 //
297 // return M;
298 //}
299 
300 //float distance_weightedEuclidean(vector<float> x, vector<float> y, vector<float> w)
301 //{
302 // int count = x.size();
303 // if (count <= 0) return 0;
304 // float dist = 0.0;
305 // for(int k = 0; k < count; k++) dist += w[k] * pow((x[k] - y[k]), 2);
306 // return dist;
307 //}
308 
309 #endif
float tolerance
Definition: GVFUtils.h:35
int inputDimensions
Definition: GVFUtils.h:25
bool segmentation
Definition: GVFUtils.h:27
bool translate
Definition: GVFUtils.h:26
Definition: GVFUtils.h:33
Definition: GVFUtils.h:60
Definition: GVFUtils.h:23