31 #ifndef RTABMAP_FLANN_SIMPLEX_DOWNHILL_H_ 32 #define RTABMAP_FLANN_SIMPLEX_DOWNHILL_H_ 41 void addValue(
int pos,
float val,
float* vals, T* point, T* points,
int n)
44 for (
int i=0; i<n; ++i) {
45 points[pos*n+i] = point[i];
50 while (j>0 && vals[j]<vals[j-1]) {
51 swap(vals[j],vals[j-1]);
52 for (
int i=0; i<n; ++i) {
53 swap(points[j*n+i],points[(j-1)*n+i]);
68 template <
typename T,
typename F>
71 const int MAX_ITERATIONS = 10;
86 vals =
new float[n+1];
87 for (
int i=0; i<n+1; ++i) {
88 float val =
func(points+i*n);
89 addValue(i, val, vals, points+i*n, points, n);
96 if (iterations++ > MAX_ITERATIONS)
break;
99 for (
int j=0; j<n; ++j) {
101 for (
int i=0; i<n; ++i) {
102 p_o[i] += points[j*n+i];
105 for (
int i=0; i<n; ++i) {
109 bool converged =
true;
110 for (
int i=0; i<n; ++i) {
111 if (p_o[i] != points[nn+i]) {
115 if (converged)
break;
118 for (
int i=0; i<n; ++i) {
119 p_r[i] = p_o[i] + alpha*(p_o[i]-points[nn+i]);
121 float val_r =
func(p_r);
123 if ((val_r>=vals[0])&&(val_r<vals[n])) {
126 Logger::info(
"Choosing reflection\n");
127 addValue(n, val_r,vals, p_r, points, n);
135 for (
int i=0; i<n; ++i) {
136 p_e[i] = 2*p_r[i]-p_o[i];
138 float val_e =
func(p_e);
141 Logger::info(
"Choosing reflection and expansion\n");
142 addValue(n, val_e,vals,p_e,points,n);
145 Logger::info(
"Choosing reflection\n");
146 addValue(n, val_r,vals,p_r,points,n);
150 if (val_r>=vals[n]) {
151 for (
int i=0; i<n; ++i) {
152 p_e[i] = (p_o[i]+points[nn+i])/2;
154 float val_e =
func(p_e);
157 Logger::info(
"Choosing contraction\n");
158 addValue(n,val_e,vals,p_e,points,n);
163 Logger::info(
"Full contraction\n");
164 for (
int j=1; j<=n; ++j) {
165 for (
int i=0; i<n; ++i) {
166 points[j*n+i] = (points[j*n+i]+points[i])/2;
168 float val =
func(points+j*n);
169 addValue(j,val,vals,points+j*n,points,n);
174 float bestVal = vals[0];
179 if (ownVals)
delete[] vals;
186 #endif //FLANN_SIMPLEX_DOWNHILL_H_
void func(GLuint LocationMVP, float Translate, glm::vec2 const &Rotate)
void addValue(int pos, float val, float *vals, T *point, T *points, int n)
float optimizeSimplexDownhill(T *points, int n, F func, float *vals=NULL)