Commit d235b31e authored by James Frink's avatar James Frink

add start of Bitalino example

parent 3b678428
This diff is collapsed.
//
// GVFGesture.h
// gvf
//
// Created by Baptiste Caramiaux on 22/01/16.
//
//
#ifndef GVFGesture_h
#define GVFGesture_h
#ifndef MAX
#define MAX(a,b) (((a) > (b)) ? (a) : (b))
#endif
#ifndef MIN
#define MIN(a,b) (((a) < (b)) ? (a) : (b))
#endif
class GVFGesture
{
public:
GVFGesture()
{
inputDimensions = 2;
setAutoAdjustRanges(true);
templatesRaw = vector<vector<vector<float > > >();
templatesNormal = vector<vector<vector<float > > >();
clear();
}
GVFGesture(int inputDimension){
inputDimensions = inputDimension;
setAutoAdjustRanges(true);
templatesRaw = vector<vector<vector<float > > >();
templatesNormal = vector<vector<vector<float > > >();
clear();
}
~GVFGesture(){
clear();
}
void setNumberDimensions(int dimensions){
assert(dimensions > 0);
inputDimensions = dimensions;
}
void setAutoAdjustRanges(bool b){
// if(b) bIsRangeMinSet = bIsRangeMaxSet = false;
bAutoAdjustNormalRange = b;
}
void setMax(float x, float y){
assert(inputDimensions == 2);
vector<float> r(2);
r[0] = x; r[1] = y;
setMaxRange(r);
}
void setMin(float x, float y){
assert(inputDimensions == 2);
vector<float> r(2);
r[0] = x; r[1] = y;
setMinRange(r);
}
void setMax(float x, float y, float z){
assert(inputDimensions == 3);
vector<float> r(3);
r[0] = x; r[1] = y; r[2] = z;
setMaxRange(r);
}
void setMin(float x, float y, float z){
assert(inputDimensions == 3);
vector<float> r(3);
r[0] = x; r[1] = y; r[2] = z;
setMinRange(r);
}
void setMaxRange(vector<float> observationRangeMax){
this->observationRangeMax = observationRangeMax;
// bIsRangeMaxSet = true;
normalise();
}
void setMinRange(vector<float> observationRangeMin){
this->observationRangeMin = observationRangeMin;
// bIsRangeMinSet = true;
normalise();
}
vector<float>& getMaxRange(){
return observationRangeMax;
}
vector<float>& getMinRange(){
return observationRangeMin;
}
void autoAdjustMinMax(vector<float> & observation){
if(observationRangeMax.size() < inputDimensions){
observationRangeMax.assign(inputDimensions, -INFINITY);
observationRangeMin.assign(inputDimensions, INFINITY);
}
for(int i = 0; i < inputDimensions; i++){
observationRangeMax[i] = MAX(observationRangeMax[i], observation[i]);
observationRangeMin[i] = MIN(observationRangeMin[i], observation[i]);
}
}
void addObservation(vector<float> observation, int templateIndex = 0){
if (observation.size() != inputDimensions)
inputDimensions = int(observation.size());
// check we have a valid templateIndex and correct number of input dimensions
assert(templateIndex <= templatesRaw.size());
assert(observation.size() == inputDimensions);
// if the template index is same as the number of temlates make a new template
if(templateIndex == templatesRaw.size()){ // make a new template
// reserve space in raw and normal template storage
templatesRaw.resize(templatesRaw.size() + 1);
templatesNormal.resize(templatesNormal.size() + 1);
}
if(templatesRaw[templateIndex].size() == 0)
{
templateInitialObservation = observation;
templateInitialNormal = observation;
}
for(int j = 0; j < observation.size(); j++)
observation[j] = observation[j] - templateInitialObservation[j];
// store the raw observation
templatesRaw[templateIndex].push_back(observation);
autoAdjustMinMax(observation);
normalise();
}
void normalise()
{
templatesNormal.resize(templatesRaw.size());
for(int t = 0; t < templatesRaw.size(); t++)
{
templatesNormal[t].resize(templatesRaw[t].size());
for(int o = 0; o < templatesRaw[t].size(); o++)
{
templatesNormal[t][o].resize(inputDimensions);
for(int d = 0; d < inputDimensions; d++)
{
templatesNormal[t][o][d] = templatesRaw[t][o][d] / (observationRangeMax[d] - observationRangeMin[d]);
templateInitialNormal[d] = templateInitialObservation[d] / (observationRangeMax[d] - observationRangeMin[d]);
}
}
}
}
void setTemplate(vector< vector<float> > & observations, int templateIndex = 0){
for(int i = 0; i < observations.size(); i++){
addObservation(observations[i], templateIndex);
}
}
vector< vector<float> > & getTemplate(int templateIndex = 0){
assert(templateIndex < templatesRaw.size());
return templatesRaw[templateIndex];
}
int getNumberOfTemplates(){
return int(templatesRaw.size());
}
int getNumberDimensions(){
return inputDimensions;
}
int getTemplateLength(int templateIndex = 0){
return int(templatesRaw[templateIndex].size());
}
int getTemplateDimension(int templateIndex = 0){
return int(templatesRaw[templateIndex][0].size());
}
vector<float>& getLastObservation(int templateIndex = 0){
return templatesRaw[templateIndex][templatesRaw[templateIndex].size() - 1];
}
vector< vector< vector<float> > >& getTemplates(){
return templatesRaw;
}
vector<float>& getInitialObservation(){
return templateInitialObservation;
}
void deleteTemplate(int templateIndex = 0)
{
assert(templateIndex < templatesRaw.size());
templatesRaw[templateIndex].clear();
templatesNormal[templateIndex].clear();
}
void clear()
{
templatesRaw.clear();
templatesNormal.clear();
observationRangeMax.assign(inputDimensions, -INFINITY);
observationRangeMin.assign(inputDimensions, INFINITY);
}
private:
int inputDimensions;
bool bAutoAdjustNormalRange;
vector<float> observationRangeMax;
vector<float> observationRangeMin;
vector<float> templateInitialObservation;
vector<float> templateInitialNormal;
vector< vector< vector<float> > > templatesRaw;
vector< vector< vector<float> > > templatesNormal;
vector<vector<float> > gestureDataFromFile;
};
#endif /* GVFGesture_h */
//
// GVFTypesAndUtils.h
//
//
//
#ifndef __H_GVFTYPES
#define __H_GVFTYPES
#include <map>
#include <vector>
#include <iostream>
#include <random>
#include <iostream>
#include <math.h>
#include <assert.h>
using namespace std;
/**
* Configuration structure
*/
typedef struct
{
int inputDimensions; /**< input dimesnion */
bool translate; /**< translate flag */
bool segmentation; /**< segmentation flag */
} GVFConfig;
/**
* Parameters structure
*/
typedef struct
{
float tolerance; /**< input dimesnion */
float distribution;
int numberParticles;
int resamplingThreshold;
float alignmentVariance;
float speedVariance;
vector<float> scaleVariance;
vector<float> dynamicsVariance;
vector<float> scalingsVariance;
vector<float> rotationsVariance;
// spreadings
float alignmentSpreadingCenter;
float alignmentSpreadingRange;
float dynamicsSpreadingCenter;
float dynamicsSpreadingRange;
float scalingsSpreadingCenter;
float scalingsSpreadingRange;
float rotationsSpreadingCenter;
float rotationsSpreadingRange;
int predictionSteps;
vector<float> dimWeights;
} GVFParameters;
// Outcomes structure
typedef struct
{
int likeliestGesture;
vector<float> likelihoods;
vector<float> alignments;
vector<vector<float> > dynamics;
vector<vector<float> > scalings;
vector<vector<float> > rotations;
} GVFOutcomes;
//--------------------------------------------------------------
// init matrix by allocating memory
template <typename T>
inline void initMat(vector< vector<T> > & M, int rows, int cols){
M.resize(rows);
for (int n=0; n<rows; n++){
M[n].resize(cols);
}
}
//--------------------------------------------------------------
// init matrix and copy values from another matrix
template <typename T>
inline void setMat(vector< vector<T> > & C, vector< vector<float> > & M){
int rows = int(M.size());
int cols = int(M[0].size());
//C.resize(rows);
C = vector<vector<T> >(rows);
for (int n=0; n<rows; n++){
//C[n].resize(cols);
C[n] = vector<T>(cols);
for (int m=0;m<cols;m++){
C[n][m] = M[n][m];
}
}
}
//--------------------------------------------------------------
// init matrix by allocating memory and fill with T value
template <typename T>
inline void setMat(vector< vector<T> > & M, T value, int rows, int cols){
M.resize(rows);
for (int n=0; n<rows; n++){
M[n].resize(cols);
for (int m=0; m<cols; m++){
M[n][m] = value;
}
}
}
//--------------------------------------------------------------
// set matrix filled with T value
template <typename T>
inline void setMat(vector< vector<T> > & M, T value){
for (int n=0; n<M.size(); n++){
for (int m=0; m<M[n].size(); m++){
M[n][m] = value;
}
}
}
//--------------------------------------------------------------
template <typename T>
inline void printMat(vector< vector<T> > & M){
for (int k=0; k<M.size(); k++){
cout << k << ": ";
for (int l=0; l<M[0].size(); l++){
cout << M[k][l] << " ";
}
cout << endl;
}
cout << endl;
}
//--------------------------------------------------------------
template <typename T>
inline void printVec(vector<T> & V){
for (int k=0; k<V.size(); k++){
cout << k << ": " << V[k] << (k == V.size() - 1 ? "" : " ,");
}
cout << endl;
}
//--------------------------------------------------------------
template <typename T>
inline void initVec(vector<T> & V, int rows){
V.resize(rows);
}
//--------------------------------------------------------------
template <typename T>
inline void setVec(vector<T> & C, vector<int> &V){
int rows = int(V.size());
C = vector<T>(rows);
//C.resize(rows);
for (int n=0; n<rows; n++){
C[n] = V[n];
}
}
//--------------------------------------------------------------
template <typename T>
inline void setVec(vector<T> & C, vector<float> & V){
int rows = int(V.size());
C.resize(rows);
for (int n=0; n<rows; n++){
C[n] = V[n];
}
}
//--------------------------------------------------------------
template <typename T>
inline void setVec(vector<T> & V, T value){
for (int n=0; n<V.size(); n++){
V[n] = value;
}
}
//--------------------------------------------------------------
template <typename T>
inline void setVec(vector<T> & V, T value, int rows){
V.resize(rows);
setVec(V, value);
}
//--------------------------------------------------------------
template <typename T>
inline vector< vector<T> > dotMat(vector< vector<T> > & M1, vector< vector<T> > & M2){
// TODO(Baptiste)
}
//--------------------------------------------------------------
template <typename T>
inline vector< vector<T> > multiplyMatf(vector< vector<T> > & M1, T v){
vector< vector<T> > multiply;
initMat(multiply, M1.size(), M1[0].size());
for (int i=0; i<M1.size(); i++){
for (int j=0; j<M1[i].size(); j++){
multiply[i][j] = M1[i][j] * v;
}
}
return multiply;
}
//--------------------------------------------------------------
template <typename T>
inline vector< vector<T> > multiplyMatf(vector< vector<T> > & M1, vector< vector<T> > & M2){
assert(M1[0].size() == M2.size()); // columns in M1 == rows in M2
vector< vector<T> > multiply;
initMat(multiply, M1.size(), M2[0].size()); // rows in M1 x cols in M2
for (int i=0; i<M1.size(); i++){
for (int j=0; j<M2[i].size(); j++){
multiply[i][j] = 0.0f;
for(int k=0; k<M1[0].size(); k++){
multiply[i][j] += M1[i][k] * M2[k][j];
}
}
}
return multiply;
}
//--------------------------------------------------------------
template <typename T>
inline vector<T> multiplyMat(vector< vector<T> > & M1, vector< T> & Vect){
assert(Vect.size() == M1[0].size()); // columns in M1 == rows in Vect
vector<T> multiply;
initVec(multiply, Vect.size());
for (int i=0; i<M1.size(); i++){
multiply[i] = 0.0f;
for (int j=0; j<M1[i].size(); j++){
multiply[i] += M1[i][j] * Vect[j];
}
}
return multiply;
}
//--------------------------------------------------------------
template <typename T>
inline float getMeanVec(vector<T>& V){
float tSum = 0.0f;
for (int n=0; n<V.size(); n++){
tSum += V[n];
}