summaryrefslogtreecommitdiff
path: root/2004/i/nono/src/ovision/group.cc
blob: c998c5efcf4ce4b7d6f1292b5f7f1a3860093fcb (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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
// group.cc - Classe Group
// nono - Programme du robot Efrei Robotique I1-I2 2004
// Copyright (C) 2004 Olivier Gaillard

/// @file group.cc Cree une liste chainee de zones correspondant aux balles 

#include "group.h"
#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
using namespace std;

#define BORD 5

/// Constructeur

/// @param *img classe image
/// @param *segm classe segmNN
Group::Group (Img *img, SegmNN *segm) 
{
    // Sauvegarde des pointeurs
    Group::img = img;
    Group::segm = segm;
    oconfig = OConfig::GetInstance ();
    
    tabOut = NULL;
    zoneListBall = NULL;
    zoneListGoal = NULL;
}


/// Destructeur
Group::~Group () 
{
    FreeGroups ();
}


/// Supprime la liste des groupes
void 
Group::FreeGroups () 
{
    ZONE *pCur = zoneListBall;
    ZONE *pPrev;

    while (pCur)
      {
	pPrev = pCur;
	pCur = pCur->next;

	free(pPrev);
      }
    
    pCur = zoneListGoal;
    while (pCur)
      {
	pPrev = pCur;
	pCur = pCur->next;

	free(pPrev);
      }
    
    zoneListBall = NULL;
    zoneListGoal = NULL;
}


/// Ajoute une balle ou un poteau � la liste de groupes

/// @param type type du group � rechercher GOAL ou BALL
/// @param numColor num�ro de la couleur du group
/// @param xmin,xmax,ymin,ymax borne du group
/// @param centerx, centery centre du group
/// @param dernier �l�ment de la liste o� empiler
void
Group::AddGroup (int type, int numColor, int xmin, int xmax, int ymin, int ymax, int centerx, int centery, ZONE *pLast) 
{
    if (!pLast)
      {
	if (type == BALL)
	  {
	    zoneListBall = new ZONE;
	    pLast = zoneListBall;
	  }
	else if (type == GOAL)
	  {
	    zoneListGoal = new ZONE;
	    pLast = zoneListGoal;
	  }
      }
    else
      {
	pLast->next = new ZONE;
	pLast = pLast->next;
      }

    pLast->xmin = xmin;
    pLast->xmax = xmax;
    pLast->ymin = ymin;
    pLast->ymax = ymax;

    pLast->centerx = centerx;
    pLast->centery = centery;
    pLast->idColor = numColor;

    // test si la balle est vu partiellement ou completement
    if (pLast->ymax >= img->height - BORD)
      {
	pLast->bottom = true;
	if ((pLast->xmin <= 0) || (pLast->xmax >= img->width) || (pLast->ymin <= 0))
	    pLast->partial = true;
	else pLast->partial = false;
      }
    else 
      {
	pLast->bottom = false;
	pLast->partial = false;
      }

    pLast->next = NULL;
}



/// Construit la table des distances
void 
Group::DoDeltaTable ()
{
    
}


/// Retourne le delta utilis� pour la dissociation de 2 balles proches
/// @param type type du group � rechercher GOAL ou BALL
/// @param y coordonnees de la hauteur de la balle 
int 
Group::GetDelta (int type, int y)
{
    if (type == BALL)
	return (int)(25 + y*0.1);
    
    else return 3*(int)(25 + y*0.1);
}


/// Test si la zone trouv� est valide
/// @param type type du group � rechercher GOAL ou BALL
/// @param xmin,xmax,ymin,ymax borne du group � valider
bool
Group::IsValidZone (int type, int xmin, int xmax, int ymin, int ymax) 
{
    
   if (type == BALL)
     {
       // test si la zone trouve un trop petite -> parasite
       if ( (abs(xmin - xmax) < oconfig->minLengthZone) || (abs(ymin - ymax) < oconfig->minLengthZone) )
	   return 0;
     }
   else if (type == GOAL)
     {
       // test si la zone trouve un trop petite -> parasite
       if ( (abs(xmin - xmax) * (abs(ymin - ymax) < oconfig->minLengthZone*oconfig->minLengthZone/10)))
	   return 0;

       // test si la zone trouve touche le haut de l'image
       if (ymin > 0) return 0;
     }

   return 1;
}



/// Cherche l'objet complet a partir d'un pixel donne

/// @param type type du group � rechercher GOAL ou BALL
/// @param numColor numero de la couleur a chercher
/// @param x,y coordonnees de depart pour la recherche 
void Group::Plague (int type, unsigned char numColor, int x, int y) 
{
    int xmax = x;
    int xmin = x;
    int ymax = y;
    int ymin = y;

    // TODO ajouter une inertie ?
    
    // Parcours de l'objet trouve de haut en bas et de gauche � droite
    while ((xmax < img->width-1)&& (segm->GiveColor(img->tabData + ((++xmax)+y* img->width)*3, true, true) == numColor)) {}
    while ((xmin > 0) && (segm->GiveColor(img->tabData + ((--xmin)+y* img->width)*3, true, true) == numColor)) {}
    while ((ymax < img->height-1) && (segm->GiveColor(img->tabData + (x+(++ymax)* img->width)*3, true, true) == numColor)) {}
    while ((ymin > 0) && (segm->GiveColor(img->tabData + (x+(--ymin)* img->width)*3, true, true) == numColor)) {}
    
    // test si la zone trouve est une zone valide
    if (!IsValidZone (type, xmin, xmax, ymin, ymax))
	return;

    ZONE *pCur;
   
    if (type == BALL)
	pCur = zoneListBall;
    else if (type == GOAL)
	pCur = zoneListGoal;
	

    // Calcul du centre de l'image
    int centerx, centery;
    centerx = (xmax+xmin)/2;
    centery = (ymax+ymin)/2;

 
    ZONE *pLast=NULL;
    int imgY;
    int delta;
    while (pCur) {
	imgY = (pCur->centery + centery) / 2;
	delta = GetDelta(type, imgY); 
	    
	// si on a deja ce groupe on actualise les donnees du groupe
	if ((numColor == pCur->idColor) && (abs(pCur->centerx - centerx) <= delta) 
	 		   		&& (abs(pCur->centery - centery) <= delta)) 
	  {
	    if (xmin < pCur->xmin) pCur->xmin = xmin;
	    if (xmax > pCur->xmax) pCur->xmax = xmax;
	    if (ymin < pCur->ymin) pCur->ymin = ymin;
	    if (ymax > pCur->ymax) pCur->ymax = ymax;

	    pCur->centerx = (pCur->xmax+pCur->xmin)/2;
	    pCur->centery = (pCur->ymax+pCur->ymin)/2;

	    return;
	}

	pLast = pCur;
	pCur = pCur->next;
    }
 

    // Si il n'est pas presente on l'ajoute
    if (!pCur)
	AddGroup (type, numColor, xmin, xmax, ymin, ymax, centerx, centery, pLast);
}


/// Affiche les zones trouvees
void 
Group::ShowZones () 
{
    ZONE *pCur = zoneListBall;
    cout << "Groupes balles:" << endl;
    while(pCur) {
	printf("%u %i %i %i %i\n", pCur->idColor, pCur->xmin, pCur->xmax, pCur->ymin, pCur->ymax);
	pCur = pCur->next;
    }

    pCur = zoneListGoal;
    cout << "Groupes poteaux:" << endl;
    while(pCur) {
	printf("%u %i %i %i %i\n", pCur->idColor, pCur->xmin, pCur->xmax, pCur->ymin, pCur->ymax);
	pCur = pCur->next;
    }
}


/// Selectionne les points a tester dans l'image
/// @param numColor numero de la couleur a trouver
void Group::JumpPoints (unsigned char numColorBall, unsigned char numColorGoal) 
{
    FreeGroups ();

    // Initialisation de la couleur a chercher
    numColorBall = segm->index[numColorBall];
    if (numColorGoal != IS_SET) 
	numColorGoal = segm->index[numColorGoal];

    int curColor;
    
    // Parcours d'une partie des pixels de l'image
    for (int x=0; x<img->width; x+=oconfig->jumpPointDist) 
	for (int y=0; y<img->height; y+=oconfig->jumpPointDist) 
	  {
	    curColor = segm->GiveColor (img->tabData + ((y*img->width+x) * 3), true, true);
	    
	    if (curColor == numColorBall)
		Plague(BALL, numColorBall, x, y);
	    else if (curColor == numColorGoal)
		Plague(GOAL, numColorGoal, x, y);
	}
    
}


/// Creation du tableau de RGB pour faire une image 
void 
Group::TabOut (int type, bool init) 
{
    ZONE *pCur;
    if (type == BALL)
	pCur = zoneListBall;
    else
	pCur = zoneListGoal;

    // On verifie que des groupes ont ete trouve
    if (!pCur) {
	cerr << "Group::TabOut : No group defined" << endl;
	if (tabOut) delete [] tabOut;
	return;
    }

    if (init)
      {
	// Allocation de la memoire
	if (tabOut) delete [] tabOut;
	tabOut = new unsigned char[img->nbPixels];

	// On initialise le tableau pour une image noire
	for (unsigned int i=0; i<img->nbPixels; i++)
	    tabOut[i] = 0;
      }
    else
      {
	// Allocation de la memoire
	if (!tabOut)
	    tabOut = new unsigned char[img->nbPixels];
      }

    // Parcours de la liste des zones trouvees
    while (pCur) 
      {
	// Remplissage de la zone avec une couleur
	for(int i=pCur->xmin; i<pCur->xmax; i++)
	    for (int j=pCur->ymin; j<pCur->ymax; j++)
		tabOut[j* img->width+i] = pCur->idColor+1;

	pCur = pCur->next;
    }

    cout << endl;
}