Commit 4ac77d23 authored by LOPEZ GANDIA Axel's avatar LOPEZ GANDIA Axel
Browse files

Merge branch '27-tinyxml-replace-boost' into 'master'

Resolve "TinyXml should replace boost parser"

Closes #27

See merge request OCSR/OCSR!48
parents edf67d9e 503a8dc7
This diff is collapsed.
This diff is collapsed.
......@@ -31,22 +31,24 @@ set (CMAKE_CXX_STANDARD 11)
set(Boost_USE_STATIC_LIBS ON)
find_package(Boost COMPONENTS system filesystem regex REQUIRED)
include_directories( ./include )
include_directories(./3rd-party/tinyxml/)
link_directories( ./lib/${CMAKE_BUILD_TYPE} )
link_directories(${Boost_LIBRARY_DIRS})
include_directories( ${Boost_INCLUDE_DIRS} )
#link_directories(${Boost_LIBRARY_DIRS})
#include_directories( ${Boost_INCLUDE_DIRS} )
file( GLOB_RECURSE source_files src/* include/*)
file( GLOB_RECURSE tinyxml_src 3rd-party/tinyxml/*)
add_executable(my_app main.cpp ${source_files} )
add_executable(test_xmlparser ./test/test_xmlparser.cpp ${source_files})
add_executable(test_socialforces ./test/SocialForcesExample.cpp ${source_files})
add_executable(test_ttcaDca ./test/DcaTtcaExample.cpp ${source_files})
add_executable(my_app main.cpp ${source_files} ${tinyxml_src})
add_executable(test_xmlparser ./test/test_xmlparser.cpp ${source_files} ${tinyxml_src})
add_executable(test_socialforces ./test/SocialForcesExample.cpp ${source_files} ${tinyxml_src})
add_executable(test_ttcaDca ./test/DcaTtcaExample.cpp ${source_files} ${tinyxml_src})
target_link_libraries(my_app ${Boost_LIBRARIES})
target_link_libraries(test_xmlparser ${Boost_LIBRARIES})
target_link_libraries(test_socialforces ${Boost_LIBRARIES})
target_link_libraries(test_ttcaDca ${Boost_LIBRARIES})
target_link_libraries(my_app)
target_link_libraries(test_xmlparser)
target_link_libraries(test_socialforces)
target_link_libraries(test_ttcaDca)
#message(${Boost_LIBRARIES})
#message(${Boost_LIBRARY_DIRS})
......@@ -70,8 +70,8 @@ class Vector2D {
float dot(Vector2D& v) { return this->x_ * v.x_ + this->y_ * v.y_; }
void set(float x, float y) {
this->x_ = x_;
this->y_ = y_;
this->x_ = x;
this->y_ = y;
}
void setX(float x) {
......
......@@ -36,11 +36,10 @@
#ifndef _XML_PARSER_H
#define _XML_PARSER_H
#include <boost/property_tree/xml_parser.hpp>
#include <boost/property_tree/ptree.hpp>
#include <boost/foreach.hpp>
#include <string>
#include <iostream>
#include <vector>
class Agent;
class Obstacle;
......
......@@ -150,7 +150,10 @@ void World::updateAllAgents(double dt)
void World::setAgents(const std::vector<Agent *> &agents)
{
agents_ = agents;
//agents_ = agents;
for (Agent * agent : agents) {
AddAgent(agent);
}
}
void World::setObstacles(const std::vector<Obstacle *> &obstacles)
......
......@@ -34,15 +34,15 @@
//========================================================================
#include "../include/tools/csvwriter.h"
#include <boost/filesystem.hpp>
//#include <boost/filesystem.hpp>
#include <fstream>
void CSVWriter::setDirectory(const std::string &dirname)
{
this->dirname_ = dirname;
/*
if (!boost::filesystem::exists(dirname))
boost::filesystem::create_directory(dirname);
boost::filesystem::create_directory(dirname);*/
}
void CSVWriter::flush()
......
......@@ -33,102 +33,193 @@
@author Javad Amirian, (C) 2018
*/
//========================================================================
#include "tinyxml2.h"
#include "tools/xmlparser.h"
#include "core/agent.h"
#include "core/obstacle.h"
#include "CostFunctions/goalReaching.h"
#include "CostFunctions/socialforces.h"
namespace pt = boost::property_tree;
#include <fstream>
XMLParser::XMLParser()
{
}
void XMLParser::load(const std::string &filename)
{
// Create empty property tree object
pt::ptree tree;
agents.clear();
obstacles.clear();
agents.clear();
obstacles.clear();
// Parse the XML into the property tree.
// If the path cannot be resolved, an exception is thrown.
try {
pt::read_xml(filename, tree);
}
catch(...) {
std::cerr << "Could not parse XML file!" << std::endl;
return;
// Parse the XML into the property tree.
// If the path cannot be resolved, an exception is thrown.
tinyxml2::XMLDocument doc;
doc.LoadFile(filename.data());
if (doc.ErrorID()!= 0)
{
std::cerr << "Could not parse XML file!" << std::endl;
return;
}
tinyxml2::XMLHandle docHandle(&doc);
tinyxml2::XMLHandle agentHandle = docHandle.FirstChildElement("World").FirstChildElement("Agents").FirstChildElement("Agent");
if (!agentHandle.ToElement()) {
std::cerr << "Could not find any Agent in the XML file!" << std::endl;
return;
}
int agentIndex = 0;
tinyxml2::XMLElement* agentElement = agentHandle.ToElement();
do
{
Agent *agent_data = new Agent();
//id and radius
int id;
float radius;
agentElement->QueryIntAttribute("id", &id);
agentElement->QueryFloatAttribute("rad", &radius);
agent_data->setID(id);
agent_data->setRadius(radius);
//position
tinyxml2::XMLElement* positionElement = agentElement->FirstChildElement("pos");
if (!positionElement) {
std::cerr << "Agent " << agentIndex << " needs a position element"<< std::endl;
return;
}
try {
// Use get_child to find the node containing the modules, and iterate over
// its children. If the path cannot be resolved, get_child throws.
// A C++11 for-range loop would also work.
const pt::ptree & agent_nodes = tree.get_child("World.Agents");
BOOST_FOREACH(const pt::ptree::value_type & agent_node, agent_nodes)
{
Agent *agent_data = new Agent();
agent_data->setID(agent_node.second.get_child("<xmlattr>").get<int>("id"));
agent_data->setRadius(agent_node.second.get_child("<xmlattr>").get<float>("rad"));
float x, y;
positionElement->QueryFloatAttribute("x", &x);
positionElement->QueryFloatAttribute("y", &y);
agent_data->setPosition(x, y);
const pt::ptree & pos_node = agent_node.second.get_child("pos");
agent_data->setPosition(pos_node.get<float>("<xmlattr>.x"), pos_node.get<float>("<xmlattr>.y"));
const pt::ptree & goal_node = agent_node.second.get_child("goal");
agent_data->setGoal(goal_node.get<float>("<xmlattr>.x"), goal_node.get<float>("<xmlattr>.y"));
/// Read policies
const pt::ptree & cost_nodes = agent_node.second.get_child("policy.functions");
Policy* pl = new Policy;
//if(policy_node.get)
BOOST_FOREACH(const pt::ptree::value_type & cost_node, cost_nodes)
{
std::string cost_func_name = cost_node.second.get_child("<xmlattr>").get<std::string>("name");
float coeff = cost_node.second.get_child("<xmlattr>").get<float>("coeff");
if(cost_func_name == SocialForces().getName())
pl->addCostFunction(new SocialForces, coeff);
else if(cost_func_name == GoalReaching().getName())
pl->addCostFunction(new GoalReaching, coeff);
else { }
}
agent_data->setPolicy(pl);
// std::string goal_type_str = goal_node.get<std::string>("<xmlattr>.type");
// if(goal_type_str.compare("dir") == 0)
// agent_data->setGoalType(Agent::eGoalDir);
// else //if(goal_type_str.compare("loc") == 0)
// agent_data->setGoalType(Agent::eGoalLoc);
agents.push_back(agent_data);
}
} catch(...) {
std::cerr << "Could not find any Agent in the XML file!" << std::endl;
return;
//goal
tinyxml2::XMLElement* goalElement = agentElement->FirstChildElement("goal");
if (!goalElement) {
std::cerr << "Agent " << agentIndex << " needs a goal element" << std::endl;
return;
}
goalElement->QueryFloatAttribute("x", &x);
goalElement->QueryFloatAttribute("y", &y);
agent_data->setGoal(x, y);
try {
const pt::ptree & obstacle_nodes = tree.get_child("World.Obstacles");
BOOST_FOREACH(const pt::ptree::value_type & obstacle_node, obstacle_nodes)
{
Obstacle* obstacle_data = new Obstacle;
BOOST_FOREACH(const pt::ptree::value_type & vertex_node, obstacle_node.second)
{
Vector2D vertex;
vertex.setX(vertex_node.second.get<float>("<xmlattr>.x"));
vertex.setY(vertex_node.second.get<float>("<xmlattr>.y"));
obstacle_data->appendVertex(vertex);
}
obstacles.push_back(obstacle_data);
}
//policy
tinyxml2::XMLElement* policyElement = agentElement->FirstChildElement("policy");
if (!policyElement) {
std::cerr << "Agent " << agentIndex << " needs a policy element" << std::endl;
return;
}
tinyxml2::XMLElement* funcElement = policyElement->FirstChildElement("costfunction");
if (!funcElement) {
std::cerr << "Agent " << agentIndex;
std::cerr << " needs at least one cost function element" << std::endl;
return;
}
catch(...) {
std::cout << "Could not find any Obstacle from XML file!" << std::endl;
Policy* pl = new Policy;
do
{
std::string costFunctionName;
costFunctionName = funcElement->Attribute("name");
float coeff;
funcElement->QueryFloatAttribute("coeff", &coeff);
if (costFunctionName == SocialForces().getName())
{
pl->addCostFunction(new SocialForces, coeff);
}
else if (costFunctionName == GoalReaching().getName())
{
pl->addCostFunction(new GoalReaching, coeff);
}
else {
}
} while ((funcElement = funcElement->NextSiblingElement()) != NULL);
agent_data->setPolicy(pl);
agents.push_back(agent_data);
++agentIndex;
} while ((agentElement = agentElement->NextSiblingElement())!=NULL);
}
void XMLParser::save(const std::string &filename)
{
FILE * xmlFile;
xmlFile = fopen(filename.data(), "w");
if (xmlFile == NULL)
{
std::cerr << "Can't open file!" << std::endl;
return;
}
tinyxml2::XMLPrinter printer(xmlFile);
printer.OpenElement("World");
printer.OpenElement("Agents");
for (Agent* agent : agents)
{
//id and radius
printer.OpenElement("Agent");
printer.PushAttribute("id", agent->getID());
printer.PushAttribute("rad", agent->getRadius());
//position
printer.OpenElement("pos");
printer.PushAttribute("x", agent->getPosition().x());
printer.PushAttribute("y", agent->getPosition().y());
printer.CloseElement();
//goal
printer.OpenElement("goal");
printer.PushAttribute("x", agent->getGoal().x());
printer.PushAttribute("y", agent->getGoal().y());
printer.CloseElement();
//policy
printer.OpenElement("policy");
for (int i = 0; i < agent->getPolicy()->getCostFunctionCount(); ++i)
{
printer.OpenElement("costfunction");
printer.PushAttribute("name", agent->getPolicy()->getCostFunctions()[i]->getName().data());
printer.PushAttribute("coeff", agent->getPolicy()->getParameters()[i]);
printer.CloseElement();
}
printer.CloseElement();
printer.CloseElement();
}
printer.CloseElement();
printer.CloseElement();
fclose(xmlFile);
}
/*
#include "tools/xmlparser.h"
#include "core/agent.h"
#include "core/obstacle.h"
#include "CostFunctions/goalReaching.h"
#include "CostFunctions/socialforces.h"
namespace pt = boost::property_tree;
XMLParser::XMLParser()
{
// Use the default-value version of get to find the debug level.
}
void XMLParser::save(const std::string &filename)
......@@ -173,3 +264,4 @@ void XMLParser::save(const std::string &filename)
std::cerr << "Some problem occured during writing XML file!" << std::endl;
}
}
*/
\ No newline at end of file
......@@ -38,7 +38,6 @@
#include <math.h>
#include <assert.h>
#include <boost/timer.hpp>
#include "../include/tools/xmlparser.h"
#include "../include/core/agent.h"
......@@ -53,7 +52,7 @@ int main(int argc, char * argv[])
XMLParser xml_parser_read;
float eps = 10E-8;
size_t Nped = 1;
size_t Nped = 3;
for(size_t i=0; i<Nped; i++)
{
Agent *agent_i = new Agent;
......@@ -69,7 +68,7 @@ int main(int argc, char * argv[])
xml_parser_write.agents.push_back(agent_i);
}
size_t Nobs = 1;
size_t Nobs = 0;
for(size_t i=0; i<Nobs; i++)
{
Obstacle *obs_i = new Obstacle;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment