summaryrefslogtreecommitdiff
path: root/2004/i/nono/src/ovision/group.cc
blob: 9c9c15884be48e24a953fe824fc0b81b3c24215c (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
// 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 ();
    
    width = img->width;
    height = img->height;

    tabOut = NULL;
    zoneListBall = NULL;
    zoneListGoal = NULL;
}


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


/// 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;
}



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


/// Retourne le delta utilis� pour la dissociation de 2 balles proches
int Group::GetDelta (int type, int y)
{
    //    return 50;

    if (type == BALL)
	return (int)(25 + y*0.1);
    else return 3*(int)(25 + y*0.1);
}


/// Test si la zone trouv� est valide
bool Group::IsValidZone (int type, int xmin, int xmax, int ymin, int ymax) {


    // test si la zone trouve un trop petite -> parasite
    if ( (abs(xmin - xmax) < oconfig->minLengthZone) || (abs(ymin - ymax) < oconfig->minLengthZone) )
	return 0;
    
   if (type == BALL)
     {
     }
   else if (type == GOAL)
     {
       // 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 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
    while ((xmax < img->width-1)&& (segm->FindColorNN(img->tabData + ((++xmax)+y* img->width)*3, 1) == numColor)) {}
    while ((xmin > 0) && (segm->FindColorNN(img->tabData + ((--xmin)+y* img->width)*3, 1) == numColor)) {}
    while ((ymax < img->height-1) && (segm->FindColorNN(img->tabData + (x+(++ymax)* img->width)*3, 1) == numColor)) {}
    while ((ymin > 0) && (segm->FindColorNN(img->tabData + (x+(--ymin)* img->width)*3, 1) == 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;
    while (pCur) {
	imgY = (pCur->centery + centery) / 2;
	// si on a deja ce groupe on actualise les donnees du groupe
	if ((numColor == pCur->idColor) && (abs(pCur->centerx - centerx) <= GetDelta(type, imgY)) && (abs(pCur->centery - centery) <= GetDelta(type, imgY))) {
	    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) {
	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 = 1;
	    if ((pLast->xmin <= 0) || (pLast->xmax >= img->width) || (pLast->ymin <= 0))
	      pLast->partial = 1;
	    else pLast->partial = 0;
	  }
	else 
	  {
	    pLast->bottom = 0;
	    pLast->partial = 0;
	  }

	pLast->next = NULL;
    }
}


/// 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 != 255) 
	numColorGoal = segm->index[numColorGoal];

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

    ZONE *pCur;
    for (int k=0; k<2; k++)
      {
	if (k == 0) pCur = zoneListBall;
	else if (k == 1) pCur = zoneListGoal;

	while (pCur)
	  {
	    pCur->ymax = img->height - pCur->ymax;
	    pCur = pCur->next;
	  }
      }
    


}


/// Creation du tableau de RGB pour faire une image 
void Group::TabOut() {
    ZONE *pCur = zoneListBall;

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