[Positioning] Code cleaning around CSV data reading
CSVFileReader: Add function read_point3d(). Point3D: Add function set_coordinates(float[3]). posexcept: Rename malformed_topology -> malformed_input_data. TopologyReaderCSV: - Use CSVFileReader::read_point3d() instead of the custom read_point(). - Rename occurrences of malformed_topology.
This commit is contained in:
parent
2347657d08
commit
338e4f7dcd
|
@ -1,4 +1,5 @@
|
|||
#include "csvfilereader.hh"
|
||||
#include "point3d.hh"
|
||||
|
||||
#include <iostream>
|
||||
|
||||
|
@ -44,6 +45,34 @@ bool CSVFileReader::next_line()
|
|||
}
|
||||
|
||||
|
||||
bool CSVFileReader::read_point3d(Point3D &p)
|
||||
{
|
||||
float coord[3] ;
|
||||
|
||||
for (unsigned int i = 0 ; i < 3 ; ++i)
|
||||
{
|
||||
if (token_iterator == current_token->end())
|
||||
return false ;
|
||||
++current_field_nb ;
|
||||
|
||||
try
|
||||
{
|
||||
coord[i] = boost::lexical_cast<float>(*token_iterator) ;
|
||||
}
|
||||
catch (boost::bad_lexical_cast &e)
|
||||
{
|
||||
print_error_cast() ;
|
||||
return false ;
|
||||
}
|
||||
|
||||
++token_iterator ;
|
||||
}
|
||||
|
||||
p.set_coordinates(coord) ;
|
||||
return true ;
|
||||
}
|
||||
|
||||
|
||||
void CSVFileReader::print_error_cast()
|
||||
{
|
||||
cerr
|
||||
|
|
|
@ -1,12 +1,13 @@
|
|||
#ifndef _OWLPS_POSITIONING_CSVFILEREADER_HH_
|
||||
#define _OWLPS_POSITIONING_CSVFILEREADER_HH_
|
||||
|
||||
class Point3D ;
|
||||
|
||||
#include "textfilereader.hh"
|
||||
|
||||
#include <boost/tokenizer.hpp>
|
||||
#include <boost/lexical_cast.hpp>
|
||||
|
||||
|
||||
/// Reads a CSV file, line by line, field by field
|
||||
class CSVFileReader: public TextFileReader
|
||||
{
|
||||
|
@ -35,6 +36,8 @@ public:
|
|||
bool next_line(void) ;
|
||||
/// Read the next field of the current line
|
||||
template<class T> bool read_field(T &field) ;
|
||||
/// Read the next 3 fields that should form a Point3D
|
||||
bool read_point3d(Point3D &p) ;
|
||||
//@}
|
||||
} ;
|
||||
|
||||
|
|
|
@ -37,6 +37,7 @@ public:
|
|||
void set_y(const float _y) ;
|
||||
void set_z(const float _z) ;
|
||||
void set_coordinates(const float _x, const float _y, const float _z) ;
|
||||
void set_coordinates(const float source[3]) ;
|
||||
void set_coordinates(const Point3D &source) ;
|
||||
//@}
|
||||
|
||||
|
@ -132,6 +133,15 @@ set_coordinates(const float _x, const float _y, const float _z)
|
|||
}
|
||||
|
||||
|
||||
inline void Point3D::
|
||||
set_coordinates(const float source[3])
|
||||
{
|
||||
x = source[0] ;
|
||||
y = source[1] ;
|
||||
z = source[2] ;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Updates x, y, z by passing a Point3D.
|
||||
* This is useful for derivated classes, and different than a direct
|
||||
|
|
|
@ -37,9 +37,9 @@ bad_configuration(const string &_explanation) throw():
|
|||
/* *** Input *** */
|
||||
|
||||
|
||||
malformed_topology::
|
||||
malformed_topology(const string &_explanation) throw():
|
||||
posexcept("Error reading the topology: " + _explanation) {}
|
||||
malformed_input_data::
|
||||
malformed_input_data(const string &_explanation) throw():
|
||||
posexcept("Read error: " + _explanation) {}
|
||||
|
||||
|
||||
bad_direction::bad_direction(const char direction) throw()
|
||||
|
|
|
@ -65,10 +65,10 @@ public:
|
|||
/* *** Input *** */
|
||||
|
||||
|
||||
class malformed_topology: public posexcept
|
||||
class malformed_input_data: public posexcept
|
||||
{
|
||||
public:
|
||||
malformed_topology(const std::string &_explanation) throw() ;
|
||||
malformed_input_data(const std::string &_explanation) throw() ;
|
||||
} ;
|
||||
|
||||
|
||||
|
|
|
@ -37,16 +37,21 @@ void TopologyReaderCSV::process_area_line()
|
|||
{
|
||||
string building_name ;
|
||||
if (! areas_file.read_field(building_name))
|
||||
throw malformed_topology("Cannot read building name!") ;
|
||||
throw malformed_input_data("Cannot read building name!") ;
|
||||
Building &building = const_cast<Building&>(
|
||||
Stock::find_create_building(building_name)) ;
|
||||
|
||||
string name ;
|
||||
if (! areas_file.read_field(name))
|
||||
throw malformed_topology("Cannot read area name!") ;
|
||||
throw malformed_input_data("Cannot read area name!") ;
|
||||
|
||||
Point3D p_min(read_point(areas_file)) ;
|
||||
Point3D p_max(read_point(areas_file)) ;
|
||||
Point3D p_min ;
|
||||
if (! areas_file.read_point3d(p_min))
|
||||
throw malformed_input_data("Cannot read area min coordinates!") ;
|
||||
|
||||
Point3D p_max ;
|
||||
if (! areas_file.read_point3d(p_max))
|
||||
throw malformed_input_data("Cannot read area max coordinates!") ;
|
||||
|
||||
Area *area = new Area(&building, name, p_min, p_max) ;
|
||||
building.add_area(area) ;
|
||||
|
@ -55,7 +60,10 @@ void TopologyReaderCSV::process_area_line()
|
|||
|
||||
void TopologyReaderCSV::process_waypoint_line()
|
||||
{
|
||||
Point3D coordinates(read_point(waypoints_file)) ;
|
||||
Point3D coordinates ;
|
||||
if (! waypoints_file.read_point3d(coordinates))
|
||||
throw malformed_input_data("Cannot read waypoint coordinates!") ;
|
||||
|
||||
Waypoint &waypoint_ref =
|
||||
const_cast<Waypoint&>(Stock::find_create_waypoint(coordinates)) ;
|
||||
|
||||
|
@ -65,7 +73,7 @@ void TopologyReaderCSV::process_waypoint_line()
|
|||
buildings.push_back(const_cast<Building*>(
|
||||
&Stock::find_create_building(building_name))) ;
|
||||
if (buildings.empty())
|
||||
throw malformed_topology("Cannot read building name!") ;
|
||||
throw malformed_input_data("Cannot read building name!") ;
|
||||
|
||||
for (vector<Building*>::iterator i = buildings.begin() ;
|
||||
i != buildings.end() ; ++i)
|
||||
|
@ -74,14 +82,3 @@ void TopologyReaderCSV::process_waypoint_line()
|
|||
(*i)->add_waypoint(&waypoint_ref) ;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Point3D TopologyReaderCSV::read_point(CSVFileReader &file)
|
||||
{
|
||||
float coord[3] ;
|
||||
for (unsigned int i = 0 ; i < 3 ; ++i)
|
||||
if (! file.read_field(coord[i]))
|
||||
throw malformed_topology("Cannot read a whole Point3D!") ;
|
||||
|
||||
return Point3D(coord) ;
|
||||
}
|
||||
|
|
|
@ -25,7 +25,6 @@ protected:
|
|||
void read_topology(void) ;
|
||||
void process_area_line(void) ;
|
||||
void process_waypoint_line(void) ;
|
||||
Point3D read_point(CSVFileReader &file) ;
|
||||
|
||||
public:
|
||||
TopologyReaderCSV(const std::string &areas_file_name,
|
||||
|
|
Loading…
Reference in New Issue