summaryrefslogtreecommitdiff
path: root/2004/i/nono/src/ovision/space.cc
blob: 8b41521a13a8aa07ecb120e121563a7ddfa0870e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
// space.cc - Classe Space
// nono - Programme du robot Efrei Robotique I1-I2 2004
// Copyright (C) 2004 Olivier Gaillard

/// @file space.cc Etalonnage des distances et localisation de la balle

#include "space.h"

#include "group.h"


using namespace std;
	
/// Constructeur
/// @param imgHeight hauteur de l'image
/// @param imgWidth largeur de l'image
Space::Space (int imgHeight, int imgWidth)
{
    Space::imgHeight = imgHeight;
    Space::imgWidth = imgWidth;

    Space::oconfig = oconfig;

    for (int i=0; i<NB_POINTS; i++)
	coefX[i] = coefY[i] = i;
}


/// Destructeur
Space::~Space ()
{
}


/// Ajoute un point pour l'etalonnage
void Space::AddSetupPoint(int x, int y, int distx, int disty)
{
    if (setupTab.size() > NB_POINTS)
      {
	std::vector<SETUP_POINT>::iterator iter;
	iter = setupTab.begin();
	setupTab.erase(iter);
      }
    
    SETUP_POINT setupPoint;

    setupPoint.x = x;
    setupPoint.y = y;
    setupPoint.distx = distx;
    setupPoint.disty = disty;

    setupTab.push_back(setupPoint);
}


/// Chargement des points a partir d'un fichier
void Space::LoadFromFile()
{
    for (int i=0; i<NB_POINTS; i++)
	AddSetupPoint(oconfig->tabPoint[i][0], oconfig->tabPoint[i][1], oconfig->tabPoint[i][2], oconfig->tabPoint[i][3]); 
}



double Space::CoeffDeterminant(double *matrix, int i, int n)
{
    return (std::pow ((double) -1,(double) i))*matrix[i*n]; 
}

/// Calcul du determinant d'une matrice
double Space::Determinant(double *matrix, int n)
{
    if (n == 3)
	return Determinant33(matrix);

    double subDet = 0;
    double coeff;
    double *mat = new double[(n-1)*(n-1)];
    for (int m=0; m<n; m++)
      {
	coeff = CoeffDeterminant(matrix, m, n);  

	int k = 0;
	for (int l=n; l<n*n; l++)
	  {
	    if (l%n != m)
	      {
		mat[k] = matrix[l];
		k++;
	      }
	  }
	cout << "o\n";

	subDet += coeff *  Determinant(mat, n-1);
      }
    
    delete [] mat;
    return subDet;
}

/// Calcul du determinant d'une matrice
double Space::Determinant33(double *matrix)
{
    double det = 0;

    det += matrix[0*3+2] * (matrix[1*3+0] * matrix[2*3+1] - matrix[2*3+0] * matrix[1*3+1]);
    det -= matrix[1*3+2] * (matrix[0*3+0] * matrix[2*3+1] - matrix[2*3+0] * matrix[0*3+1]);
    det += matrix[2*3+2] * (matrix[0*3+0] * matrix[1*3+1] - matrix[1*3+0] * matrix[0*3+1]);

    return det;
}

/// Calcul du d�terminant caract�ristique d'une matrice
double Space::CaracteristicDeterminant(double *matrix, double *endMat, int numCol)
{
    double save[3];

    // Deplacement de la i�me colonne et sauvegarde
    // pour le calcul du determinant caracteristique
    for (int i=0; i<3; i++)
      {
	save[i] =  matrix[i*3+numCol];
	matrix[i*3+numCol] = endMat[i];
      }

    // Calcul du determinant caracteristique
    double carDet = Space::Determinant(matrix, 3);

    // Restauration de la matrice d'origine
    for (int i=0; i<3; i++)
	matrix[i*3+numCol] = save[i];

    return carDet;
}



int Space::BestFitDoTab(double *mat, double *endMat)
{
    for (int i=0; i<3; i++)
	endMat[i] = 0;

    for (int i=0; i<9; i++)
	mat[i] = 0;
    
    // distance
    for(int i=0; i<NB_POINTS; i++)
      {
	endMat[0] += setupTab[i].disty;
	endMat[1] += setupTab[i].y * setupTab[i].disty;
	endMat[2] += setupTab[i].y * setupTab[i].y * setupTab[i].disty;

	mat[0] += 1;
	mat[1] += setupTab[i].y;
	mat[2] += setupTab[i].y * setupTab[i].y;
	mat[3] += setupTab[i].y; 
	mat[4] += setupTab[i].y * setupTab[i].y;
	mat[5] += setupTab[i].y * setupTab[i].y * setupTab[i].y;
	mat[6] += setupTab[i].y * setupTab[i].y;
	mat[7] += setupTab[i].y * setupTab[i].y * setupTab[i].y;
	mat[8] +=  setupTab[i].y * setupTab[i].y * setupTab[i].y * setupTab[i].y;
      }

}


void Space::Setup(double aX, double bX, double cX, double aY, double bY, double cY)
{
    coefX[0] = cX;
    coefX[1] = bX;
    coefX[2] = aX;
    coefY[0] = cY;
    coefY[1] = bY;
    coefY[2] = aY;
}


/// Etalonnage des distances
int Space::Setup()
{
    if (setupTab.size() < NB_POINTS)
      {
	cerr << "Space::Setup : Pas assez de points donn�s, utilis� AddSetupPoint\n";
	cerr << "Il y a " << setupTab.size () << " et il faut " << NB_POINTS << endl;
	return 0;
      }

    std::cout << "nbPoints: " << NB_POINTS << endl;

    // Calcul des coeff pour l'approximation de la profondeur
    double mat[NB_POINTS*NB_POINTS];
    double endMat[NB_POINTS];

    double det = Space::Determinant(mat, 3);

    if (det != 0)
      {
	for (int i=0; i<3; i++)
	  {
	    coefY[i] = (double)Space::CaracteristicDeterminant(mat, endMat, i) / det;
	    cout << coefY[i] << endl;
	  }
      }
    else
      {
	cerr << "Space::Setup : Determinant egal a zero, donner d'autres points\n";
	return 0;
      }

    // Calcul des coeff pour l'approximation de l'horizontal
/*    for (int i=0; i<NB_POINTS; i++)
      {
	endMat[i] = setupTab[i].distx;
	for (int j=0; j<NB_POINTS-1; j++)
	    mat[i*NB_POINTS+j] = pow((double)setupTab[i].x, (double)(NB_POINTS - j -1));

	mat[i*(NB_POINTS+1) -1] = 1;
      }


    det = Space::Determinant(mat, NB_POINTS);

    if (det != 0)
      {
	for (int i=0; i<NB_POINTS; i++)
	  {
	    coefX[NB_POINTS-i-1] = (double)Space::CaracteristicDeterminant(mat, endMat, i) / det;
	  }

      }
    else
      {
	cerr << "Space::Setup : Coordonnees X egales a zero, donner d'autres points\n";
	return 0;
      }


    /*    double matrix2[NB_POINTS][4] = {{setupTab[0].y * setupTab[0].x, setupTab[0].x, 1, setupTab[0].distx},
	  {setupTab[1].y * setupTab[1].x, setupTab[1].x, 1, setupTab[1].distx},
	  {setupTab[2].y * setupTab[2].x, setupTab[2].x, 1, setupTab[2].distx}};

	  det = Space::Determinant(matrix2);
	  if (det != 0)
	  {
	  aX = (double)Space::CaracteristicDeterminant(matrix2, 0) / det;
	  bX = (double)Space::CaracteristicDeterminant(matrix2, 1) / det;
	  cX = (double)Space::CaracteristicDeterminant(matrix2, 2) / det;
	  cout << "aX: " << aX << " bX: " << bX << " cX: " << cX << endl;
	  }
	  else
	  {
	  cerr << "Space::Setup : Coordonnees X egales a zero, donner d'autres points\n";
	  return 0;
	  }
	  */
    return 1;
}

/// Position d'un objet dans le referentiel du robot
void Space::GetLoc(int locImgX, int locImgY, double &locX, double &locY)
{
    // Calcul de locX
    locX = (locImgY * locImgX * coefX[2] + coefX[1]*locImgX + coefX[0]);

    // Calcul de locY
    locY = (locImgY * locImgY * coefY[2] + locImgY * coefY[1] + coefY[0]);
}

void Space::GetLoc(int locImgX, int locImgY, int &locX, int &locY)
{
    double x,y;
    GetLoc(locImgX, locImgY, x, y);
    locX = (int)x;
    locY = (int)y;
}

/// Donne la position reelle sur la table de la balle
/// @param locX, locY : position de la balle dans le r�f�rentiel du robot
/// @param posRobotX, posRobotY, angleRobot : position du robot sur la table
/// @param posX, posY : variable de retour pour la position de la balle sur la table
void Space::GetPos(int locX, int locY, int posRobotX, int posRobotY, double angleRobot, double &posX, double &posY)
{
    double sinus = sin(angleRobot);
    double cosinus = cos(angleRobot); 
    
    // Calcul des coordonnes avec le changement de repere
    posX = posRobotX + locX*cosinus - locY*sinus;
    posY = posRobotY + locX*sinus + locY*cosinus;
}