summaryrefslogtreecommitdiff
path: root/2005/i/robert/src/ovision/see/oconfig.cc
blob: 41337a8613a434b9435a53e9ef7c5cec88d84e66 (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
// config.cc - Classe OConfig
// robert - Programme du robot APBteam
// Copyright (C) 2005 Olivier Gaillard

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

#include <iostream>
#include <fstream>
#include <stdlib.h>
#include <stdio.h>
#include <sstream>

#include "oconfig.hh"

OConfig *OConfig::instance = 0;

/// Constructor
/// @param *filename nom du fichier de config
OConfig::OConfig(const char *filename) 
    : node (0), color (0), index (0)
{
    instance = this;
    load (filename);
    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 (const char *var, const char *arg) 
{
    if (!arg) return;
    std::string varName (var);
    if (varName == "Cam_color")
      {
	std::string argName (arg);
	if 	(argName == "RGB") inputColor = Image::rgb;
	else if (argName == "BGR") inputColor = Image::bgr;
	else if (argName == "YUV") inputColor = Image::yuv;
	else if (argName == "HSI") inputColor = Image::hsi;
	return;
      }
    // Affecte la valeur de arg a la variable varName
    if 	    (varName ==		"NN_step_learning") 			nnSl = atof(arg);
    else if (varName == 	"NN_neighborhood_learning") 		nnNl = atof(arg);
    else if (varName == 	"NN_number_of_iteration_learning")	nnNil = atol(arg);
    else if (varName == 	"NN_threshold_output")	 		nnThresholdOutput = atoi(arg);
    else if (varName == 	"NN_lum_inosity_influence") 		nnInfluLum = atof(arg);
    else if (varName == 	"UI_img_path ") 			strcpy(imgPath, arg);
    else if (varName == 	"NN_lazy_threshold") 			nnLazyThreshold = atoi(arg);
//    else if (varName == 	"Map_error")				mapError = atoi(arg);
//    else if (varName == 	"Map_error_part")			mapErrorPart = atoi(arg);
//    else if (varName == 	"Map_angle_ball_weight")		angleBallWeight= atoi(arg);
//    else if (varName == 	"Map_distance_ball_robot_weight")	distanceBallRobotWeight = atoi(arg);
//    else if (varName == 	"Map_distance_ball_goal_weight")	distanceBallGoalWeight = atoi(arg);
//    else if (varName == 	"Map_ball_density_weight")		ballDensityWeight = atoi(arg);
//    else if (varName == 	"Map_ennemy_presence_weight")		ennemyPresenceWeight = atoi(arg);
//    else if (varName == 	"Map_ball_precision_weight")		ballPrecisionWeight = atoi(arg);
//    else if (varName == 	"Map_skepticism_weight")		skepticismWeight = atoi(arg);
//    else if (varName == 	"Map_skepticism_max")			skepticismMax = atoi(arg);
//    else if (varName == 	"Map_ball_lost_weight")			ballLostWeight = atoi(arg);
//    else if (varName == 	"Map_ball_bottom_time_out") 		ballBottomTimeOut = atoi(arg);
    else if (varName == 	"Group_minimum_length_zone") 		minLengthZone = atoi(arg);
    else if (varName == 	"Group_jump_point_distance") 		jumpPointDist = atoi(arg);
    else if (varName == 	"UI_group_to_display") 			uiGroupToDisplay = atoi(arg);
    else if (varName == 	"Skittle_border") 			skittleBorder = atoi(arg);
    else if (varName == 	"Skittle_grow") 			skittleGrow = atoi(arg);
    else if (varName == 	"Skittle_div_jump") 			skittleDivJump = atoi(arg);
    else if (varName == 	"Skittle_vertical_ratio") 		skittleVerticalRatio = atof(arg);
    else if (varName == 	"Skittle_score_bend_ratio")		skittleScoreBendRatio = atof(arg);
}


/// Charge les variables du fichier de configuration (par d�faut rc/vision.conf)
void 
OConfig::load (const char *filename)
{
    const int NBARG = 3;
    char *cut[NBARG] = {0};
    FILE *file;
    char ligne[50];
    int i;
    // Ouverture du fichier de conf    
    file = fopen(filename, "r");
    if (!file) throw "OConfig::OConfig : Error during config file opening";
    else std::cout << "Lecture du ficher de configuration" << std::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] != 0) && (i<(NBARG-1))) 
	  {
	    i++;
	    cut[i] = strtok( 0, " \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 (const char *filePath) 
{
    const int maxChar = 50;
    // Ouverture du fichier de conf
    std::ifstream file (filePath);
    if (!file) 
      {
	throw "OConfig::LoadNNFile : Error during poids file opening";
	return;
      }
    // Chargement des couleurs
    char buf [maxChar];
    groupColor.clear ();
    while (!file.eof ())
      {
	file.getline (buf, maxChar);
	std::string line (buf);
	if (line.find("#index") != std::string::npos)
	    break;
	if (buf[0] == '\0' || buf[0] == ' ' || buf[0] == '\n' 
	    || buf[0] == '\t' || buf[0] == '#') continue;
    	std::istringstream iss (buf);
	ObjectColor tmp;
	iss >> tmp.label >> tmp.color;
	groupColor.push_back (tmp);
      }
    // Chargement des poids
    std::vector<int> tmpNode, tmpIndex;
    int tmp;
    while (!file.eof ())
      {
	file.getline (buf, maxChar);
    	std::istringstream iss (buf);
	if (buf[0] == '\0' || buf[0] == ' ' || buf[0] == '\n' 
	    || buf[0] == '\t' || buf[0] == '#') continue;
	iss >> tmp;
	tmpIndex.push_back (tmp);
	for (int i=0; i<3; i++)
	  {
	    iss >> tmp;
	    tmpNode.push_back (tmp);
	  }
      }
    file.close ();
    // Recopie dans des tableaux
    delete [] node;
    node = new unsigned char[tmpNode.size ()];
    for (unsigned int i=0; i<tmpNode.size (); ++i)
	node[i] = (unsigned char)tmpNode[i];
    delete [] index;
    index = new int[tmpIndex.size ()];
    for (unsigned int i=0; i<tmpIndex.size (); ++i)
	index[i] = (unsigned char)tmpIndex[i];
    nnNbNodeMax = tmpIndex.size ();
    nnNbColor = nnNbNodeMax;
}

/// 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, const int nbOutput) const
{
    if (!node) 
      {
	throw "OConfig::CreateNNFile : NN non initialis�";
	return;
      }
    // Ecriture dans un fichier
    std::ofstream file (filename);
    // Couleurs
    for (std::vector<ObjectColor>::const_iterator i = groupColor.begin (); i < groupColor.end (); ++i)
	file << i->label << " " << i->color << "\n";
    // Poids
    file << "#index\t#x1\tx2\tx3\n";
    for (int i=0; i<nbOutput; i++) 
      {
	file << index[i] << "\t";
	for (int j=0; j<3; j++)
	    file << (int)node[i*3+j] << "\t";
	file << "\n";
      }
    file.close ();
}

/// Chargement de la liste des points pour les distances
void
OConfig::loadDistFile (const char *filename) 
{
    // Ouverture du fichier de distance
    std::ifstream file (filename); 
    if (!file) {
	throw "OConfig::LoadDistFile : Error during poids file opening";
	return;
    }
    // Parcours des lignes et analyse
    char buf[50];
    int point[4];
    while (!file.eof () && file.getline (buf, 50))
	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]);
	    spaceTabPoint.push_back(point[0]);
	    spaceTabPoint.push_back(point[1]);
	    spaceTabPoint.push_back(point[2]);
	    spaceTabPoint.push_back(point[3]);
	  }
    file.close ();
    // Nombre de points venant d'�tre charg�s
    spaceNbDistPoint = spaceTabPoint.size () / 4;
}

/// Creation d'un fichier pour la liste des points pour les distances
void
OConfig::createDistFile (const char *filename, const int numPoint) const
{
    if (spaceTabPoint.size () == 0) 
      {
	throw "OConfig::CreateDistFile : spaceTabPoint vide";
	return;
      }
    // Ecriture dans un fichier
    std::ofstream file (filename); 
    // Remplissage du fichier
    file << "#imgX\timgY\tdistX\tdistY\n";
    for (int i=0; i<numPoint; i++)
    	file << spaceTabPoint[i*4+0] << "\t" << spaceTabPoint[i*4+1] << "\t"
	     << spaceTabPoint[i*4+2] << "\t" << spaceTabPoint[i*4+3] << "\n";
    file.close ();
}