|
Hormodular 1.0
Locomotion gait discovery by using digital hormones on modular robots
|
Loads a gait table from a file:
{
//--Variables to store:
int rows = 0, cols = 0; //-- Matrix dimensions
static const int BUFFER_SIZE = 32; //-- Characters to store in number
char ch; //-- Current character read
char number[BUFFER_SIZE]; //-- Number stored as characters
std::vector<double> data_tmp; //-- Vector containing data
//-- Open the file
std::ifstream input_file( file_path.c_str() );
if (input_file.is_open())
{
//-- Erase the buffer:
for (int i = 0; i < BUFFER_SIZE; i++)
number[i] = '\0';
//-- Extracting data from file:
while (!input_file.eof())
{
input_file.get(ch); //-- Get a character
//-- Ignore comments
if (ch == '#')
{
//-- Ignore data until next line:
while( !input_file.eof() && ch != '\n')
input_file.get(ch);
continue;
}
//-- Check if it is a number:
if (ch == '-')
{
//-- Read next character:
input_file.get(ch);
//-- For Negative numbers:
if ( ch >= '0' && ch <= '9' )
{
number[0] = '-';
number[1] = ch;
int i = 2;
//-- Store the number in 'number'
while( !input_file.eof() && ch != '\n' && ch != ' ' && i < BUFFER_SIZE)
{
input_file.get(ch);
if ( ( ch >= '0' && ch <= '9' ) || ch == 'e' || ch == '.' || ch == '-')
number[i] = ch;
i++;
}
//-- Convert the string to a double and store it in vector 'data'
data_tmp.push_back( atof(number) );
//-- Erase the buffer:
for (int i = 0; i < BUFFER_SIZE; i++)
number[i] = '\0';
}
}
else
{
//-- For positive numbers:
if ( ch >= '0' && ch <= '9' )
{
number[0] = ch;
int i = 1;
//-- Store the number in 'number'
while( !input_file.eof() && ch != '\n' && ch != ' ' && i < BUFFER_SIZE)
{
input_file.get(ch);
if ( ( ch >= '0' && ch <= '9' ) || ch == 'e' || ch == '.' || ch == '-')
number[i] = ch;
i++;
}
//-- Convert the string to a double and store it in vector 'data'
data_tmp.push_back( atof(number) );
//-- Erase the buffer:
for (int i = 0; i < BUFFER_SIZE; i++)
number[i] = '\0';
}
}
//-- New line detection / number of columns
if (ch == '\n' && !input_file.eof())
{
//-- For first row
if ( rows == 0)
cols = data_tmp.size(); //-- Obtain the number of columns
rows++;
}
}
//-- Close the file:
input_file.close();
//-- Save data on table
num_parameters = cols-1;
//-- Add the data to the table
data.clear();
ids.clear();
for (int i = 0; i < rows; i ++)
{
ids.push_back( (unsigned long) data_tmp[i*cols]);
std::vector<float> newRow;
for (int j = 1; j < cols; j++)
newRow.push_back(data_tmp[i*cols+j]);
data.push_back(newRow);
}
//-- Just debug
// std::cout << "[Debug] Data loaded from table: " << std::endl;
// for (int i = 0; i < rows; i ++)
// {
// std::cout << ids[i] << " ";
// for (int j = 0; j < cols-1; j++)
// std::cout << data[i][j] << " ";
// std::cout << std::endl;
// }
}
else
{
//-- Show error message
std::cerr <<"[GaitTable] Error: File " << file_path << " could not be opened!" << std::endl;
return -1;
}
return 0;
}
|
1.7.4