Rewrite lua bindings using sol interface

This commit is contained in:
Daniel J. Hofmann 2016-10-19 20:02:41 -07:00 committed by Patrick Niklaus
parent 6e7fe5feb0
commit 0f59b78c02
6 changed files with 2253 additions and 1496 deletions

View File

@ -31,7 +31,6 @@ if(ENABLE_MASON)
set(MASON_STXXL_VERSION "1.4.1")
set(MASON_EXPAT_VERSION "2.2.0")
set(MASON_LUA_VERSION "5.2.4")
set(MASON_LUABIND_VERSION "e414c57bcb687bb3091b7c55bbff6947f052e46b")
set(MASON_BZIP2_VERSION "1.0.6")
set(MASON_TBB_VERSION "43_20150316")
@ -94,6 +93,7 @@ endif()
include_directories(BEFORE ${CMAKE_CURRENT_BINARY_DIR}/include/)
include_directories(BEFORE ${CMAKE_CURRENT_SOURCE_DIR}/include/)
include_directories(SYSTEM ${CMAKE_CURRENT_SOURCE_DIR}/third_party/sol2/)
include_directories(SYSTEM ${CMAKE_CURRENT_SOURCE_DIR}/third_party/variant/include)
add_custom_target(FingerPrintConfigure ALL ${CMAKE_COMMAND}
@ -403,10 +403,6 @@ if(ENABLE_MASON)
add_dependency_includes(${MASON_PACKAGE_lua_INCLUDE_DIRS})
set(USED_LUA_LIBRARIES ${MASON_PACKAGE_lua_STATIC_LIBS})
mason_use(luabind_lua524 VERSION ${MASON_LUABIND_VERSION})
add_dependency_includes(${MASON_PACKAGE_luabind_lua524_INCLUDE_DIRS})
set(LUABIND_LIBRARY ${MASON_PACKAGE_luabind_lua524_STATIC_LIBS})
mason_use(bzip2 VERSION ${MASON_BZIP2_VERSION})
add_dependency_includes(${MASON_PACKAGE_bzip2_INCLUDE_DIRS})
set(BZIP2_LIBRARIES ${MASON_PACKAGE_bzip2_STATIC_LIBS})
@ -455,10 +451,6 @@ else()
if(WIN32 AND CMAKE_BUILD_TYPE MATCHES Debug)
set(TBB_LIBRARIES ${TBB_DEBUG_LIBRARIES})
endif()
find_package(Luabind REQUIRED)
add_dependency_includes(${LUABIND_INCLUDE_DIR})
set(USED_LUA_LIBRARIES ${LUA_LIBRARY})
add_dependency_includes(${LUA_INCLUDE_DIR})
find_package(EXPAT REQUIRED)
add_dependency_includes(${EXPAT_INCLUDE_DIRS})
@ -537,6 +529,7 @@ ELSE()
ENDIF()
set(USED_LUA_LIBRARIES ${LUA_LIBRARY})
add_dependency_includes(${LUA_INCLUDE_DIR})
if(NOT WIN32 AND NOT Boost_USE_STATIC_LIBS)
add_dependency_defines(-DBOOST_TEST_DYN_LINK)

View File

@ -96,6 +96,7 @@ struct ExtractionWay
bool roundabout;
bool circular;
bool is_startpoint;
bool ignore_in_grid;
TravelMode forward_travel_mode : 4;
TravelMode backward_travel_mode : 4;
guidance::RoadClassification road_classification;

View File

@ -239,7 +239,6 @@ function way_function (way, result)
-- ferries (doesn't cover routes tagged using relations)
result.forward_mode = mode.ferry
result.backward_mode = mode.ferry
result.ignore_in_grid = true
if duration and durationIsValid(duration) then
result.duration = math.max( 1, parseDuration(duration) )
else

View File

@ -7,7 +7,6 @@ barrier_whitelist = { [""] = true, ["cycle_barrier"] = true, ["bollard"] = true,
access_tag_whitelist = { ["yes"] = true, ["foot"] = true, ["permissive"] = true, ["designated"] = true }
access_tag_blacklist = { ["no"] = true, ["private"] = true, ["agricultural"] = true, ["forestry"] = true, ["delivery"] = true }
access_tags_hierarchy = { "foot", "access" }
ignore_in_grid = { ["ferry"] = true }
restrictions = { "foot" }
walking_speed = 5

View File

@ -8,6 +8,7 @@
#include "extractor/profile_properties.hpp"
#include "extractor/raster_source.hpp"
#include "extractor/restriction_parser.hpp"
#include "util/coordinate.hpp"
#include "util/exception.hpp"
#include "util/lua_util.hpp"
#include "util/simple_logger.hpp"
@ -20,11 +21,61 @@
#include <memory>
#include <sstream>
namespace sol
{
template <> struct is_container<osmium::Node> : std::false_type
{
};
template <> struct is_container<osmium::Way> : std::false_type
{
};
}
namespace osrm
{
namespace extractor
{
template <class T>
auto get_value_by_key(T const &object, const char *key) -> decltype(object.get_value_by_key(key))
{
auto v = object.get_value_by_key(key);
if (v && *v)
{ // non-empty string?
return v;
}
else
{
return nullptr;
}
}
template <class T, class D>
const char* get_value_by_key(T const &object, const char *key, D const default_value)
{
auto v = get_value_by_key(object, key);
if (v && *v)
{
return v;
}
else
{
return default_value;
}
}
template <class T> double latToDouble(T const &object)
{
return static_cast<double>(util::toFloating(object.lat));
}
template <class T> double lonToDouble(T const &object)
{
return static_cast<double>(util::toFloating(object.lon));
}
auto get_nodes_for_way(const osmium::Way &way) -> decltype(way.nodes()) { return way.nodes(); }
Sol2ScriptingEnvironment::Sol2ScriptingEnvironment(const std::string &file_name)
: file_name(file_name)
{
@ -35,14 +86,222 @@ void Sol2ScriptingEnvironment::InitContext(Sol2ScriptingContext &context)
{
context.state.open_libraries();
// TODO: register things here
context.state["durationIsValid"] = durationIsValid;
context.state["parseDuration"] = parseDuration;
context.state["trimLaneString"] = trimLaneString;
context.state["applyAccessTokens"] = applyAccessTokens;
context.state["canonicalizeStringList"] = canonicalizeStringList;
context.state.new_enum("mode",
"inaccessible",
TRAVEL_MODE_INACCESSIBLE,
"driving",
TRAVEL_MODE_DRIVING,
"cycling",
TRAVEL_MODE_CYCLING,
"walking",
TRAVEL_MODE_WALKING,
"ferry",
TRAVEL_MODE_FERRY,
"train",
TRAVEL_MODE_TRAIN,
"pushing_bike",
TRAVEL_MODE_PUSHING_BIKE,
"steps_up",
TRAVEL_MODE_STEPS_UP,
"steps_down",
TRAVEL_MODE_STEPS_DOWN,
"river_up",
TRAVEL_MODE_RIVER_UP,
"river_down",
TRAVEL_MODE_RIVER_DOWN,
"route",
TRAVEL_MODE_ROUTE);
context.state.new_enum("road_priority_class",
"motorway",
extractor::guidance::RoadPriorityClass::MOTORWAY,
"trunk",
extractor::guidance::RoadPriorityClass::TRUNK,
"primary",
extractor::guidance::RoadPriorityClass::PRIMARY,
"secondary",
extractor::guidance::RoadPriorityClass::SECONDARY,
"tertiary",
extractor::guidance::RoadPriorityClass::TERTIARY,
"main_residential",
extractor::guidance::RoadPriorityClass::MAIN_RESIDENTIAL,
"side_residential",
extractor::guidance::RoadPriorityClass::SIDE_RESIDENTIAL,
"link_road",
extractor::guidance::RoadPriorityClass::LINK_ROAD,
"bike_path",
extractor::guidance::RoadPriorityClass::BIKE_PATH,
"foot_path",
extractor::guidance::RoadPriorityClass::FOOT_PATH,
"connectivity",
extractor::guidance::RoadPriorityClass::CONNECTIVITY);
context.state.new_usertype<SourceContainer>("sources",
"load",
&SourceContainer::LoadRasterSource,
"query",
&SourceContainer::GetRasterDataFromSource,
"interpolate",
&SourceContainer::GetRasterInterpolateFromSource);
context.state.new_enum("constants", "precision", COORDINATE_PRECISION);
context.state.new_usertype<ProfileProperties>(
"ProfileProperties",
"traffic_signal_penalty",
sol::property(&ProfileProperties::GetTrafficSignalPenalty,
&ProfileProperties::SetTrafficSignalPenalty),
"u_turn_penalty",
sol::property(&ProfileProperties::GetUturnPenalty, //
&ProfileProperties::SetUturnPenalty),
"max_speed_for_map_matching",
sol::property(&ProfileProperties::GetMaxSpeedForMapMatching,
&ProfileProperties::SetMaxSpeedForMapMatching),
"continue_straight_at_waypoint",
&ProfileProperties::continue_straight_at_waypoint,
"use_turn_restrictions",
&ProfileProperties::use_turn_restrictions,
"left_hand_driving",
&ProfileProperties::left_hand_driving);
context.state.new_usertype<std::vector<std::string>>(
"vector",
"Add",
static_cast<void (std::vector<std::string>::*)(const std::string &)>(
&std::vector<std::string>::push_back));
context.state.new_usertype<osmium::Location>(
"Location", "lat", &osmium::Location::lat, "lon", &osmium::Location::lon);
context.state.new_usertype<osmium::Way>("Way",
"get_value_by_key",
&get_value_by_key<osmium::Way>,
"id",
&osmium::Way::id,
"get_nodes",
&get_nodes_for_way);
context.state.new_usertype<osmium::Node>("Node",
"location",
&osmium::Node::location,
"get_value_by_key",
&get_value_by_key<osmium::Node>,
"id",
&osmium::Node::id);
context.state.new_usertype<ExtractionNode>("ResultNode",
"traffic_lights",
&ExtractionNode::traffic_lights,
"barrier",
&ExtractionNode::barrier);
context.state.new_usertype<guidance::RoadClassification>(
"RoadClassification",
"motorway_class",
sol::property(&guidance::RoadClassification::IsMotorwayClass,
&guidance::RoadClassification::SetMotorwayFlag),
"link_class",
sol::property(&guidance::RoadClassification::IsLinkClass,
&guidance::RoadClassification::SetLinkClass),
"may_be_ignored",
sol::property(&guidance::RoadClassification::IsLowPriorityRoadClass,
&guidance::RoadClassification::SetLowPriorityFlag),
"road_priority_class",
sol::property(&guidance::RoadClassification::GetClass,
&guidance::RoadClassification::SetClass),
"num_lanes",
sol::property(&guidance::RoadClassification::GetNumberOfLanes,
&guidance::RoadClassification::SetNumberOfLanes));
context.state.new_usertype<ExtractionWay>(
"ResultWay",
"forward_speed",
&ExtractionWay::forward_speed,
"backward_speed",
&ExtractionWay::backward_speed,
"name",
sol::property(&ExtractionWay::GetName, &ExtractionWay::SetName),
"ref",
sol::property(&ExtractionWay::GetRef, &ExtractionWay::SetRef),
"pronunciation",
sol::property(&ExtractionWay::GetPronunciation, &ExtractionWay::SetPronunciation),
"destinations",
sol::property(&ExtractionWay::GetDestinations, &ExtractionWay::SetDestinations),
"turn_lanes_forward",
sol::property(&ExtractionWay::GetTurnLanesForward, &ExtractionWay::SetTurnLanesForward),
"turn_lanes_backward",
sol::property(&ExtractionWay::GetTurnLanesBackward, &ExtractionWay::SetTurnLanesBackward),
"roundabout",
&ExtractionWay::roundabout,
"circular",
&ExtractionWay::circular,
"is_startpoint",
&ExtractionWay::is_startpoint,
"duration",
&ExtractionWay::duration,
"road_classification",
&ExtractionWay::road_classification,
"forward_mode",
sol::property(&ExtractionWay::get_forward_mode, &ExtractionWay::set_forward_mode),
"backward_mode",
sol::property(&ExtractionWay::get_backward_mode, &ExtractionWay::set_backward_mode));
context.state.new_usertype<osmium::WayNodeList>("WayNodeList");
// Keep in mind .location is undefined since we're not using libosmium's location cache
context.state.new_usertype<osmium::NodeRef>("NodeRef", "id", &osmium::NodeRef::ref);
context.state.new_usertype<InternalExtractorEdge>("EdgeSource",
"source_coordinate",
&InternalExtractorEdge::source_coordinate,
"weight_data",
&InternalExtractorEdge::weight_data);
context.state.new_usertype<InternalExtractorEdge::WeightData>(
"WeightData", "speed", &InternalExtractorEdge::WeightData::speed);
context.state.new_usertype<ExternalMemoryNode>("EdgeTarget",
"lon",
&lonToDouble<ExternalMemoryNode>,
"lat",
&latToDouble<ExternalMemoryNode>);
context.state.new_usertype<util::Coordinate>(
"Coordinate",
"lon",
sol::property(&lonToDouble<util::Coordinate>),
"lat",
sol::property(&latToDouble<util::Coordinate>));
context.state.new_usertype<RasterDatum>(
"RasterDatum", "datum", &RasterDatum::datum, "invalid_data", &RasterDatum::get_invalid);
context.state["properties"] = &context.properties;
context.state["sources"] = &context.sources;
//
// end of register block
//
util::luaAddScriptFolderToLoadPath(context.state.lua_state(), file_name.c_str());
context.has_turn_penalty_function = true;
context.has_node_function = true;
context.has_way_function = true;
context.has_segment_function = true;
context.state.script_file(file_name);
sol::function turn_function = context.state["turn_function"];
sol::function node_function = context.state["node_function"];
sol::function way_function = context.state["way_function"];
sol::function segment_function = context.state["segment_function"];
context.has_turn_penalty_function = turn_function.valid();
context.has_node_function = node_function.valid();
context.has_way_function = way_function.valid();
context.has_segment_function = segment_function.valid();
}
const ProfileProperties &Sol2ScriptingEnvironment::GetProfileProperties()
@ -117,33 +376,64 @@ void Sol2ScriptingEnvironment::ProcessElements(
std::vector<std::string> Sol2ScriptingEnvironment::GetNameSuffixList()
{
auto &context = GetSol2Context();
BOOST_ASSERT(context.state != nullptr);
BOOST_ASSERT(context.state.lua_state() != nullptr);
std::vector<std::string> suffixes_vector;
// TODO: fill if get_suff. function exists
sol::function get_name_suffix_list = context.state["get_name_suffix_list"];
if (get_name_suffix_list.valid())
{
get_name_suffix_list(suffixes_vector);
}
return suffixes_vector;
}
std::vector<std::string> Sol2ScriptingEnvironment::GetRestrictions()
{
auto &context = GetSol2Context();
BOOST_ASSERT(context.state != nullptr);
BOOST_ASSERT(context.state.lua_state() != nullptr);
std::vector<std::string> restrictions;
// TODO: fill if get_restrictoins is available
sol::function get_restrictions = context.state["get_restrictions"];
if (get_restrictions.valid())
{
get_restrictions(restrictions);
}
return restrictions;
}
void Sol2ScriptingEnvironment::SetupSources()
{
auto &context = GetSol2Context();
BOOST_ASSERT(context.state != nullptr);
BOOST_ASSERT(context.state.lua_state() != nullptr);
// TODO: call source_function if exists
sol::function source_function = context.state["source_function"];
if (source_function.valid())
{
source_function();
}
}
int32_t Sol2ScriptingEnvironment::GetTurnPenalty(const double angle)
{
auto &context = GetSol2Context();
// turn_function(angle) if function exists
sol::function turn_function = context.state["turn_function"];
if (turn_function.valid())
{
const double penalty = turn_function(angle);
BOOST_ASSERT(penalty < std::numeric_limits<int32_t>::max());
BOOST_ASSERT(penalty > std::numeric_limits<int32_t>::min());
return penalty;
}
return 0;
}
@ -153,19 +443,31 @@ void Sol2ScriptingEnvironment::ProcessSegment(const osrm::util::Coordinate &sour
InternalExtractorEdge::WeightData &weight)
{
auto &context = GetSol2Context();
// TODO: call segment_function if exists
sol::function segment_function = context.state["segment_function"];
if (segment_function.valid())
{
segment_function(source, target, distance, weight);
}
}
void Sol2ScriptingContext::processNode(const osmium::Node &node, ExtractionNode &result)
{
BOOST_ASSERT(state != nullptr);
// TODO: node_function
BOOST_ASSERT(state.lua_state() != nullptr);
sol::function node_function = state["node_function"];
node_function(node, result);
}
void Sol2ScriptingContext::processWay(const osmium::Way &way, ExtractionWay &result)
{
BOOST_ASSERT(state != nullptr);
// TODO: way_function
BOOST_ASSERT(state.lua_state() != nullptr);
sol::function way_function = state["way_function"];
way_function(way, result);
}
}
}

File diff suppressed because it is too large Load Diff