summaryrefslogtreecommitdiff
path: root/2004/i/nono/src/ovision/group.cc
blob: 9228d7be686a1a3e4260fc6b9beaf1d9e683a95d (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
// 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 BALL 1
#define GOAL 2


/// Constructeur

/// @param *img classe image
/// @param *segm classe segmNN
Group::Group(Img *img, SegmNN *segm) {
    // Sauvegarde des pointeurs
    Group::img = img;
    Group::segm = segm;
    
    width = img->width;
    height = img->height;

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


/// Destructeur
Group::~Group() {
    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);
      }
}


/// 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 < width-1)&& (tabSegm[(++xmax)+y*width] == numColor)) {}
    while ((xmin > 0) && (tabSegm[(--xmin)+y*width] == numColor)) {}
    while ((ymax < height-1) && (tabSegm[x+(++ymax)*width] == numColor)) {}
    while ((ymin > 0) && (tabSegm[x+(--ymin)*width] == numColor)) {}

    // Verification la validite des entrees
/*    if ((xmin < 0) || (xmin > width)) 
	xmin = 0;
    if ((xmax > img->width) || (xmax < 0)) 
	xmax = img->width;
    if ((ymin < 0) || (ymin > height)) 
	ymin = 0;
    if ((ymax > img->height) || (ymax < 0)) 
	ymax = img->height;
*/    
    // Calcul du centre de l'image
    int centerx, centery;
    centerx = (xmax+xmin)/2;
    centery = (ymax+ymin)/2;

    ZONE *pCur;

    if (type == BALL)	
	pCur = zoneListBall;
    else if (type == GOAL)
      {
	    if (!((pCur->ymin <= 0) && (pCur->ymax >= img->width)))
		return;
	pCur = zoneListGoal;
      }

    ZONE *pLast=NULL;
    while (pCur) {
	// 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) {
	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->xmin <= 0) || (pLast->ymin <= 0) || (pLast->xmax >= img->width) || (pLast->ymax >= img->height))
	    pLast->partial = 1;
	else 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 balles:" << 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 numColor) {

    tabSegm = segm->tabSegm;

    // Initialisation de la couleur a chercher
    numColor = segm->index[numColor];
    
    // Parcours d'une partie des pixels de l'image
    for (int x=0; x<width; x+=10) {
	for (int y=0; y<height; y+=10) {
	    if (tabSegm[y*width+x] == numColor)
	      {
		Plague(BALL, numColor, x, y);
	      }
	}
    }

}


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