Skip to content

Commit

Permalink
fourth test using 3d segments, see also issue Project-OSRM#271
Browse files Browse the repository at this point in the history
  • Loading branch information
Jens Thiele committed Jan 21, 2013
1 parent 9da4e18 commit fbeb705
Show file tree
Hide file tree
Showing 12 changed files with 352 additions and 31 deletions.
92 changes: 68 additions & 24 deletions Extractor/ExtractionContainers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,18 @@

#include "ExtractionContainers.h"

void ExtractionContainers::PrepareData(const std::string & outputFileName, const std::string restrictionsFileName, const unsigned amountOfRAM) {
// from: https://raw.github.com/DennisOSRM/Project-OSRM/639d325b4b79e55f6f03f8582137cb2d39c9fd30/Util/Lua.h
// todo: remove again and include Util/Lua.h after rebase
static
bool lua_function_exists(lua_State* lua_state, const char* name)
{
using namespace luabind;
object g = globals(lua_state);
object func = g[name];
return func && type(func) == LUA_TFUNCTION;
}

void ExtractionContainers::PrepareData(const std::string & outputFileName, const std::string restrictionsFileName, const unsigned amountOfRAM, lua_State *myLuaState) {
try {
unsigned usedNodeCounter = 0;
unsigned usedEdgeCounter = 0;
Expand Down Expand Up @@ -205,6 +216,7 @@ void ExtractionContainers::PrepareData(const std::string & outputFileName, const
nodesIT = allNodes.begin();
edgeIT = allEdges.begin();

bool have_segment_function = lua_function_exists(myLuaState, "segment_function");
while(edgeIT != allEdges.end() && nodesIT != allNodes.end()) {
if(edgeIT->target < nodesIT->id){
++edgeIT;
Expand All @@ -219,45 +231,77 @@ void ExtractionContainers::PrepareData(const std::string & outputFileName, const
edgeIT->targetCoord.lat = nodesIT->lat;
edgeIT->targetCoord.lon = nodesIT->lon;

double distance = ApproximateDistance(edgeIT->startCoord.lat, edgeIT->startCoord.lon, nodesIT->lat, nodesIT->lon);
assert(edgeIT->speed != -1);
double weight = ( distance * 10. ) / (edgeIT->speed / 3.6);
int intWeight = std::max(1, (int)std::floor((edgeIT->isDurationSet ? edgeIT->speed : weight)+.5) );

double weight[2];
double distance=0;
if (have_segment_function) {
try {
// note: didn't find a way to get multiple return values with luabind
// => using table
luabind::object r = luabind::call_function<luabind::object>
(myLuaState,
"segment_function",
edgeIT->startCoord.lat, edgeIT->startCoord.lon,
edgeIT->targetCoord.lat, edgeIT->targetCoord.lon,
edgeIT->speed,
edgeIT->maxspeed);
weight[0]=luabind::object_cast<double>(r[1]);
weight[1]=luabind::object_cast<double>(r[2]);
distance=luabind::object_cast<double>(r[3]);
// INFO("weight[0]=" << weight[0] << " weight[1]=" << weight[1] << " distance=" << distance);
} catch (const luabind::error &er) {
lua_State* Ler=er.state();
std::cerr << "-- " << lua_tostring(Ler, -1) << std::endl;
lua_pop(Ler, 1); // remove error message
ERR(er.what());
} catch (const std::exception &er) {
ERR(er.what());
} catch (...) {
ERR("Unknown error!");
}
} else {
weight[0] = ( distance * 10. ) / (edgeIT->speed / 3.6);
weight[1] = weight[0];
}

int intDist = std::max(1, (int)distance);
short zero = 0;
short one = 1;

fout.write((char*)&edgeIT->start, sizeof(unsigned));
fout.write((char*)&edgeIT->target, sizeof(unsigned));
fout.write((char*)&intDist, sizeof(int));
// note: maybe 2 half-edges!
bool oneway = (weight[0]!=weight[1])
|| (edgeIT->direction == _Way::oneway)
|| (edgeIT->direction == _Way::opposite);
switch(edgeIT->direction) {
case _Way::notSure:
fout.write((char*)&zero, sizeof(short));
break;
case _Way::oneway:
fout.write((char*)&one, sizeof(short));
break;
case _Way::bidirectional:
fout.write((char*)&zero, sizeof(short));

break;
case _Way::opposite:
fout.write((char*)&one, sizeof(short));
break;
default:
std::cerr << "[error] edge with no direction: " << edgeIT->direction << std::endl;
assert(false);
break;
}
fout.write((char*)&intWeight, sizeof(int));
assert(edgeIT->type >= 0);
fout.write((char*)&edgeIT->type, sizeof(short));
fout.write((char*)&edgeIT->nameID, sizeof(unsigned));
fout.write((char*)&edgeIT->isRoundabout, sizeof(bool));
fout.write((char*)&edgeIT->ignoreInGrid, sizeof(bool));
fout.write((char*)&edgeIT->isAccessRestricted, sizeof(bool));
for (int i=0; i<((weight[0]==weight[1])||(edgeIT->direction == _Way::oneway)||(edgeIT->direction == _Way::opposite) ? 1 : 2); ++i) {
fout.write((char*)((i==0) ? &edgeIT->start : &edgeIT->target), sizeof(unsigned));
fout.write((char*)((i==0) ? &edgeIT->target : &edgeIT->start), sizeof(unsigned));
fout.write((char*)&intDist, sizeof(int));
if (oneway)
fout.write((char*)&one, sizeof(short));
else
fout.write((char*)&zero, sizeof(short));
int intWeight = std::max(1, (int)std::floor((edgeIT->isDurationSet ? edgeIT->speed : weight[i])+.5) );
fout.write((char*)&intWeight, sizeof(int));
assert(edgeIT->type >= 0);
fout.write((char*)&edgeIT->type, sizeof(short));
fout.write((char*)&edgeIT->nameID, sizeof(unsigned));
fout.write((char*)&edgeIT->isRoundabout, sizeof(bool));
fout.write((char*)&edgeIT->ignoreInGrid, sizeof(bool));
fout.write((char*)&edgeIT->isAccessRestricted, sizeof(bool));
++usedEdgeCounter;
}
}
++usedEdgeCounter;
++edgeIT;
}
}
Expand Down
9 changes: 8 additions & 1 deletion Extractor/ExtractionContainers.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,13 @@
#include "ExtractorStructs.h"
#include "../DataStructures/TimingUtil.h"

extern "C" {
#include <lua.h>
#include <lauxlib.h>
#include <lualib.h>
}
#include <luabind/luabind.hpp>

class ExtractionContainers {
public:
typedef stxxl::vector<NodeID> STXXLNodeIDVector;
Expand Down Expand Up @@ -55,7 +62,7 @@ class ExtractionContainers {
wayStartEndVector.clear();
}

void PrepareData( const std::string & outputFileName, const std::string restrictionsFileName, const unsigned amountOfRAM);
void PrepareData( const std::string & outputFileName, const std::string restrictionsFileName, const unsigned amountOfRAM, lua_State *myLuaState);

STXXLNodeIDVector usedNodeIDs;
STXXLNodeVector allNodes;
Expand Down
2 changes: 1 addition & 1 deletion Extractor/ExtractorCallbacks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ bool ExtractorCallbacks::wayFunction(_Way &w) {
}

for(std::vector< NodeID >::size_type n = 0; n < w.path.size()-1; ++n) {
externalMemory->allEdges.push_back(_Edge(w.path[n], w.path[n+1], w.type, w.direction, w.speed, w.nameID, w.roundabout, w.ignoreInGrid, w.isDurationSet, w.isAccessRestricted));
externalMemory->allEdges.push_back(_Edge(w.path[n], w.path[n+1], w.type, w.direction, w.speed, w.maxspeed, w.nameID, w.roundabout, w.ignoreInGrid, w.isDurationSet, w.isAccessRestricted));
externalMemory->usedNodeIDs.push_back(w.path[n]);
}
externalMemory->usedNodeIDs.push_back(w.path.back());
Expand Down
11 changes: 7 additions & 4 deletions Extractor/ExtractorStructs.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ struct _Way {
keyVals.EraseAll();
direction = _Way::notSure;
speed = -1;
maxspeed = -1;
type = -1;
access = true;
roundabout = false;
Expand All @@ -67,6 +68,7 @@ struct _Way {
unsigned nameID;
std::string name;
double speed;
double maxspeed;
short type;
bool access;
bool roundabout;
Expand All @@ -86,17 +88,18 @@ struct _Relation {
};

struct _Edge {
_Edge() : start(0), target(0), type(0), direction(0), speed(0), nameID(0), isRoundabout(false), ignoreInGrid(false), isDurationSet(false), isAccessRestricted(false) {};
_Edge(NodeID s, NodeID t) : start(s), target(t), type(0), direction(0), speed(0), nameID(0), isRoundabout(false), ignoreInGrid(false), isDurationSet(false), isAccessRestricted(false) { }
_Edge(NodeID s, NodeID t, short tp, short d, double sp): start(s), target(t), type(tp), direction(d), speed(sp), nameID(0), isRoundabout(false), ignoreInGrid(false), isDurationSet(false), isAccessRestricted(false) { }
_Edge(NodeID s, NodeID t, short tp, short d, double sp, unsigned nid, bool isra, bool iing, bool ids, bool iar): start(s), target(t), type(tp), direction(d), speed(sp), nameID(nid), isRoundabout(isra), ignoreInGrid(iing), isDurationSet(ids), isAccessRestricted(iar) {
_Edge() : start(0), target(0), type(0), direction(0), speed(0), maxspeed(0), nameID(0), isRoundabout(false), ignoreInGrid(false), isDurationSet(false), isAccessRestricted(false) {};
_Edge(NodeID s, NodeID t) : start(s), target(t), type(0), direction(0), speed(0), maxspeed(0), nameID(0), isRoundabout(false), ignoreInGrid(false), isDurationSet(false), isAccessRestricted(false) { }
_Edge(NodeID s, NodeID t, short tp, short d, double sp): start(s), target(t), type(tp), direction(d), speed(sp), maxspeed(0), nameID(0), isRoundabout(false), ignoreInGrid(false), isDurationSet(false), isAccessRestricted(false) { }
_Edge(NodeID s, NodeID t, short tp, short d, double sp, double maxsp, unsigned nid, bool isra, bool iing, bool ids, bool iar): start(s), target(t), type(tp), direction(d), speed(sp), maxspeed(maxsp), nameID(nid), isRoundabout(isra), ignoreInGrid(iing), isDurationSet(ids), isAccessRestricted(iar) {
assert(0 <= type);
}
NodeID start;
NodeID target;
short type;
short direction;
double speed;
double maxspeed;
unsigned nameID;
bool isRoundabout;
bool ignoreInGrid;
Expand Down
1 change: 1 addition & 0 deletions Extractor/ScriptingEnvironment.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ ScriptingEnvironment::ScriptingEnvironment(const char * fileName) {
.def(luabind::constructor<>())
.def_readwrite("name", &_Way::name)
.def_readwrite("speed", &_Way::speed)
.def_readwrite("maxspeed", &_Way::maxspeed)
.def_readwrite("type", &_Way::type)
.def_readwrite("access", &_Way::access)
.def_readwrite("roundabout", &_Way::roundabout)
Expand Down
5 changes: 5 additions & 0 deletions createHierarchy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,11 @@ int main (int argc, char *argv[]) {

// Create a new lua state
lua_State *myLuaState = luaL_newstate();
#if LUA_VERSION_NUM >= 501
luaL_openlibs(myLuaState);
#else
lua_baselibopen(myLuaState);
#endif

// Connect LuaBind to this lua state
luabind::open(myLuaState);
Expand Down
3 changes: 2 additions & 1 deletion extractor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,8 @@ int main (int argc, char *argv[]) {
parser->Parse();
INFO("parsing finished after " << get_timestamp() - time << " seconds");

externalMemory.PrepareData(outputFileName, restrictionsFileName, amountOfRAM);
// todo
externalMemory.PrepareData(outputFileName, restrictionsFileName, amountOfRAM, scriptingEnvironment.luaStateVector[0]);

stringMap.clear();
delete parser;
Expand Down
3 changes: 3 additions & 0 deletions profiles/bicycle.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require("lib/access")
require("lib/segment3d")

-- Begin of globals
barrier_whitelist = { [""] = true, ["cycle_barrier"] = true, ["bollard"] = true, ["entrance"] = true, ["cattle_grid"] = true, ["border_control"] = true, ["toll_booth"] = true, ["sally_port"] = true, ["gate"] = true}
Expand Down Expand Up @@ -162,6 +163,7 @@ function way_function (way, numberOfNodesInWay)
way.direction = Way.bidirectional
way.ignore_in_grid = true
if durationIsValid(duration) then
-- todo: shouldn't the weight at least be distributed according to segment lenghts?
way.speed = math.max( parseDuration(duration) / math.max(1, numberOfNodesInWay-1) )
way.is_duration_set = true
else
Expand Down Expand Up @@ -203,6 +205,7 @@ function way_function (way, numberOfNodesInWay)
-- maxspeed
if take_minimum_of_speeds then
if maxspeed and maxspeed>0 then
way.maxspeed = maxspeed
way.speed = math.min(way.speed, maxspeed)
end
end
Expand Down
6 changes: 6 additions & 0 deletions profiles/foot.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

-- Begin of globals

-- modules must be in lua path
require("lib/segment3d")

bollards_whitelist = { [""] = true, ["cattle_grid"] = true, ["border_control"] = true, ["toll_booth"] = true, ["sally_port"] = true, ["gate"] = true}
access_tag_whitelist = { ["yes"] = true, ["foot"] = true, ["permissive"] = true, ["designated"] = true }
access_tag_blacklist = { ["no"] = true, ["private"] = true, ["agricultural"] = true, ["forestery"] = true }
Expand Down Expand Up @@ -166,6 +169,9 @@ function way_function (way, numberOfNodesInWay)
way.speed = math.min(speed_profile["default"], maxspeed)
end

-- needed in the segment callback
way.maxspeed = maxspeed

-- Set access restriction flag if access is allowed under certain restrictions only
if access ~= "" and access_tag_restricted[access] then
way.is_access_restricted = true
Expand Down
46 changes: 46 additions & 0 deletions profiles/lib/elpro-http.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
require("curl")
require("json")

function get_upsample_pl4d(host, port)
local clib = curl.easy_init()
local url = 'http://'..host..':'..port..'/cgi-bin/elpro.fcgi?'

-- todo
local function reverse(a)
local r={}
local n=table.maxn(a)
for i=1,n do
r[n-i+1]=a[i]
end
return r
end

-- todo
local function map(func, array)
local new_array = {}
for i,v in ipairs(array) do
new_array[i] = func(v)
end
return new_array
end

local function params(pl,dist)
return 'path='..table.concat(map(function(x) return table.concat(reverse(x),",") end, pl),"|")..'&upsample='..dist..'&format=sjs'
end

-- input: 2d polyline
-- output: upsampled 4d polyline with elevation and wgs84-distance from startpoint as 3rd and 4th dimension added
return function(pl,dist)
local header = {}
local body = {}
-- todo: maybe use http post
-- print(url..params(pl,dist))
clib:setopt(curl.OPT_URL,url..params(pl,dist))
clib:setopt(curl.OPT_HEADERFUNCTION,function(s,len) table.insert(header,s) return len,nil end)
clib:setopt(curl.OPT_WRITEFUNCTION,function(s,len) table.insert(body,s) return len,nil end)
clib:perform()
local r=json.decode(table.concat(body))
assert(r and (r['status']=='OK')) -- nil is false in lua
return r['results']
end
end
Loading

0 comments on commit fbeb705

Please sign in to comment.