summaryrefslogtreecommitdiff
path: root/2004/i/nono/src/ovision/oconfig.cc
blob: 98f8ad91bfe2524687029fb4fbc59632faf9b81a (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
// config.cc - Classe OConfig
// nono - Programme du robot Efrei Robotique I1-I2 2004
// Copyright (C) 2004 Olivier Gaillard

/// @file config.cc Charge le fichier config et distribue les variables

#include "oconfig.h"
#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include <string>
using namespace std;

OConfig *OConfig::instance = 0;


/// Constructor
/// @param *filename nom du fichier de config
OConfig::OConfig(char *filename) 
{
    instance = this;
    
    Load (filename);

    colorMode = 0;
    color = NULL;
    node = NULL;
    index = NULL;
    LoadNNFile("rc/poids");
    LoadDistFile("rc/dist");
}

/// Destructor
OConfig::~OConfig() 
{
}

/// Parse une ligne du fichier de config
/// @param *var nom de la variable a fixer
/// @param *arg valeur de la variable
void 
OConfig::Parse(char *var, char *arg) 
{
    char argu[20];

    if (!arg) cerr << "OConfig::Parse : Error during config file parsing" << endl;

    strcpy(argu, arg);

    // Verifie si l'argument est un nombre ou un nom de fichier
    if (((argu[0]>'9') || (argu[0] < '0')) 
	&& strcmp(var, "Source") && strcmp(var, "imgPath")) 
      {
	FILE *file;

	file=fopen(arg, "r");
	if (!file) cerr << "OConfig::Parse : Bad pathfile for " << arg << endl;

	fgets(argu, 20,file);

	fclose(file);
      }

    // Affecte la valeur de argu a la variable var
    if (!strcmp (var,"NN_step_learning")) 			nn_sl = atof(argu);
    else if (!strcmp (var,"NN_neighborhood_learning")) 		nn_nl = atof(argu);
    else if (!strcmp (var,"NN_number_of_iteration_learning"))	nn_nil = atol(argu);
    else if (!strcmp (var,"NN_number_of_color_to_segment")) 	nn_NbCouleurs = atoi(argu);
    else if (!strcmp (var,"NN_threshold_output"))	 	nn_threshold_output = atoi(argu);
    else if (!strcmp (var,"NN_luminosity_influence")) 		nn_influ_lum = atof(argu);
    else if (!strcmp (var,"UI_img_path ")) 			strcpy(imgPath, argu);
    else if (!strcmp (var,"NN_lazy_threshold")) 		nn_lazy_threshold = atoi(argu);
    else if (!strcmp (var,"Map_error"))				map_error = atoi(argu);
    else if (!strcmp (var,"Map_error_part"))			map_error_part = atoi(argu);
    else if (!strcmp (var,"Map_angle_ball_weight"))		angle_ball_weight= atoi(argu);
    else if (!strcmp (var,"Map_distance_ball_robot_weight"))	distance_ball_robot_weight = atoi(argu);
    else if (!strcmp (var,"Map_distance_ball_goal_weight"))	distance_ball_goal_weight = atoi(argu);
    else if (!strcmp (var,"Map_ball_density_weight"))		ball_density_weight = atoi(argu);
    else if (!strcmp (var,"Map_ennemy_presence_weight"))	ennemy_presence_weight = atoi(argu);
    else if (!strcmp (var,"Map_ball_precision_weight"))		ball_precision_weight = atoi(argu);
    else if (!strcmp (var,"Map_skepticism_weight"))		skepticism_weight = atoi(argu);
    else if (!strcmp (var,"Map_skepticism_max"))		skepticism_max = atoi(argu);
    else if (!strcmp (var,"Map_ball_lost_weight"))		ball_lost_weight = atoi(argu);
    else if (!strcmp (var,"Map_ball_bottom_time_out")) 		ball_bottom_time_out = atoi(argu);
    else if (!strcmp (var,"Group_minimum_length_zone")) 	minLengthZone = atoi(argu);
    else if (!strcmp (var,"Group_jump_point_distance")) 	jumpPointDist = atoi(argu);
}


void 
OConfig::Load (char *filename)
{
    const int NBARG = 3;
    char *cut[NBARG] = {NULL};
    FILE *file;
    char ligne[50];
    int i;

    // Ouverture du fichier de conf    
    file = fopen(filename, "r");
    if (!file) cerr << "OConfig::OConfig : Error during config file opening" << endl;
    else cout << "Lecture du ficher de configuration" << endl;


    // Parcours des lignes et analyse
    while(fgets(ligne, 50, file))
      {
	if (ligne[0] == '#') continue;

	// Division du string
	cut[0] = strtok(ligne, " \n");
	if (!cut[0]) continue;
	i=0;
	while ((cut[i] != NULL) && (i<(NBARG-1))) 
	  {
	    i++;
	    cut[i] = strtok( NULL, " \t\n");

	  }

	Parse(cut[0], cut[2]);
      }

}


/// Chargement des poids d'un reseau de neurones
/// @param *filePath nom du fichier de poids a charger
void
OConfig::LoadNNFile (char *filePath) 
{
    const int NBARG = 4;
    char *cut[NBARG] = {NULL};
    FILE *file;
    char ligne[50];
    int i;
    unsigned char *pNode;

    // Ouverture du fichier de conf
    file = fopen(filePath, "r");
    if (!file) {
	cerr << "OConfig::LoadNNFile : Error during poids file opening" << endl;
	index = new int[nn_NbCouleurs];
	for (int i=0; i<nn_NbCouleurs; i++)
	    index[i] = i;
	return;
    }

    // Nombre de couleurs contenu dans le fichier
    int numNode=-3;
    while(fgets(ligne, 50,file))
	numNode++;

    if (numNode <= 0) {
	cerr << "OConfig::LoadNNFile : Corrupt poids file" << endl;
	index = new int[nn_NbCouleurs];
	for (int i=0; i<nn_NbCouleurs; i++)
	    index[i] = i;
	return;
    }

    
    if (node) delete [] node;
    node = new unsigned char[numNode*3];
    if (index) delete [] index;
    index = new int[numNode];

    // Parcours des lignes et analyse
    numNode=0;

    // Recherche mode de couleur
    rewind(file);

    if (fgets(ligne, 50, file)) {
	switch (ligne[0]) {
	    case 'Y': colorMode = YUV;
		break;

	    case 'R': colorMode = RGB;
		break;

	    case 'H': colorMode = HSI;
		break;
	}
    }

    // Recherche couleur des balles
    if (fgets(ligne, 50, file)) {
	cut[0] = strtok(ligne, " \t\n");
	groupColor = atoi(cut[0]);
    }
    
    // Recherche couleur des poteaux
    if (fgets(ligne, 50, file)) {
	cut[0] = strtok(ligne, " \t\n");
	goalColor = atoi(cut[0]);
    }

    while(fgets(ligne, 50, file)) {
	if (ligne[0] == '#') continue;

	// Division du string
	cut[0] = strtok(ligne, " \t\n");
	if (!cut[0]) continue;
	i=0;
	pNode = &node[numNode*3];
	index[numNode] = atoi(cut[0]);
	while ((cut[i] != NULL) && (i<(NBARG-1))) {
	    i++;
	    cut[i] = strtok( NULL, " \t\n");
	    pNode[i-1] = (unsigned char)atoi(cut[i]);
	}
	numNode++;
    }

    nbNodeMax = numNode;

    fclose(file);
}


/// Creation d'un fichier de poids pour le reseau de neurones
/// @param *filename nom du fichier a creer
/// @param mode mode de l'espace de couleur
/// @param nbOutput nombre de couleurs a detecter du reseau de neurones
void 
OConfig::CreateNNFile (const char *filename, int mode, int nbOutput) 
{

    if (!node) {
	cerr << "OConfig::CreateNNFile : NN non initialis�\n";
	return;
    }

    // Ecriture dans un fichier
    FILE *file;
    file = fopen(filename, "w+");

    // Espace de couleur (RGB, YUV, HSI)
    char buf[50];
    if (colorMode == RGB) strcpy(buf, "RGB\n");
    else if (colorMode == YUV) strcpy(buf, "YUV\n");
    else if (colorMode == HSI) strcpy(buf, "HSI\n");
    
    fprintf(file, buf);

    // Couleur des balles
    sprintf(buf, "%i // Index de la couleur des balles\n", groupColor);
    fprintf(file, buf);
    
    // Couleur des poteaux
    sprintf(buf, "%i // Index de la couleur des poteaux\n", goalColor);
    fprintf(file, buf);

    fprintf(file, "#index\t#x1\tx2\tx3\n");
    for (int i=0; i<nbOutput; i++) {
	fprintf(file, "%i\t", index[i]);
	for (int j=0; j<3; j++)
	    fprintf(file, "%u\t", node[i*3+j]);
	fprintf(file, "\n");
    }

    fclose(file);
}


/// Chargement de la liste des points pour les distances
void
OConfig::LoadDistFile (char *filename) 
{
    FILE *file;
    char buf[50];
    
    // Ouverture du fichier de distance
    file = fopen(filename, "r");
    if (!file) {
	cerr << "OConfig::LoadDistFile : Error during poids file opening" << endl;
	return;
    }
    
    // Parcours des lignes et analyse
    int point[4];
    while((fgets(buf, 50, file)) && (!feof(file)))
      {
	if ((buf[0] >= '0') && (buf[0] <= '9'))
	  {
	    sscanf(buf, "%i\t%i\t%i\t%i\n", &point[0], &point[1], &point[2], &point[3]);
	    tabPoint.push_back(point[0]);
	    tabPoint.push_back(point[1]);
	    tabPoint.push_back(point[2]);
	    tabPoint.push_back(point[3]);
	  }
      }
   
    fclose(file);

    // Nombre de points venant d'�tre charg�
    nbDistPoint = tabPoint.size () / 4;
}


/// Creation d'un fichier pour la liste des points pour les distances
void
OConfig::CreateDistFile (char *filename, int numPoint) 
{
    if (tabPoint.size () == 0) 
      {
	cerr << "OConfig::CreateDistFile : tabPoint vide" << endl;
	return;
      }

    // Ecriture dans un fichier
    FILE *file;
    file = fopen(filename, "w+");

    // Remplissage du fichier
    fprintf(file, "#imgX\timgY\tdistX\tdistY\n");

    // for (int i=0; i<numPoint; i++)
    //	fprintf(file, "%i\t%i\t%i\t%i\n", tabPoint[i*4+0], tabPoint[i*4+1], tabPoint[i*4+2], tabPoint[i*4+3]);

    fclose(file);
}