-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4732 from syershov/pytraffic
Python bindings for traffic
- Loading branch information
Showing
9 changed files
with
533 additions
and
59 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
#pragma once | ||
|
||
#include "std/utility.hpp" | ||
|
||
#include <boost/python.hpp> | ||
#include <boost/python/suite/indexing/vector_indexing_suite.hpp> | ||
|
||
namespace | ||
{ | ||
using namespace boost::python; | ||
|
||
// Converts a std::pair instance to a Python tuple. | ||
template <typename T1, typename T2> | ||
struct pair_to_tuple | ||
{ | ||
static PyObject * convert(pair<T1, T2> const & p) | ||
{ | ||
return incref(make_tuple(p.first, p.second).ptr()); | ||
} | ||
|
||
static PyTypeObject const * get_pytype() { return &PyTuple_Type; } | ||
}; | ||
|
||
template <typename T1, typename T2> | ||
struct pair_to_python_converter | ||
{ | ||
pair_to_python_converter() { to_python_converter<pair<T1, T2>, pair_to_tuple<T1, T2>, true>(); } | ||
}; | ||
} // namespace |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
#pragma once | ||
|
||
#include "std/vector.hpp" | ||
|
||
#include <boost/python.hpp> | ||
#include <boost/python/suite/indexing/map_indexing_suite.hpp> | ||
|
||
namespace | ||
{ | ||
using namespace boost::python; | ||
|
||
// Converts a vector<uint8_t> to/from Python str. | ||
struct vector_uint8t_to_str | ||
{ | ||
static PyObject * convert(vector<uint8_t> const & v) | ||
{ | ||
str s(reinterpret_cast<char const *>(v.data()), v.size()); | ||
return incref(s.ptr()); | ||
} | ||
}; | ||
|
||
struct vector_uint8t_from_python_str | ||
{ | ||
vector_uint8t_from_python_str() | ||
{ | ||
converter::registry::push_back(&convertible, &construct, type_id<vector<uint8_t>>()); | ||
} | ||
|
||
static void * convertible(PyObject * obj_ptr) | ||
{ | ||
if (!PyString_Check(obj_ptr)) | ||
return nullptr; | ||
return obj_ptr; | ||
} | ||
|
||
static void construct(PyObject * obj_ptr, converter::rvalue_from_python_stage1_data * data) | ||
{ | ||
const char * value = PyString_AsString(obj_ptr); | ||
if (value == nullptr) | ||
throw_error_already_set(); | ||
void * storage = | ||
((converter::rvalue_from_python_storage<vector<uint8_t>> *)data)->storage.bytes; | ||
new (storage) vector<uint8_t>(value, value + PyString_Size(obj_ptr)); | ||
data->convertible = storage; | ||
} | ||
}; | ||
} // namespace |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
project(pytraffic) | ||
|
||
check_pybindings() | ||
|
||
set( | ||
SRC | ||
bindings.cpp | ||
) | ||
|
||
# Suppress boost-python warnings | ||
add_compile_options( | ||
"-Wno-unused-local-typedef" | ||
) | ||
|
||
set(Boost_USE_MULTITHREADED ON) | ||
|
||
# For macOS we can use static linking, on Linux we can't. | ||
if (PLATFORM_MAC) | ||
set(Boost_USE_STATIC_LIBS ON) | ||
set(Boost_USE_STATIC_RUNTIME ON) | ||
endif() | ||
|
||
find_package(PythonLibs 2.7 REQUIRED) | ||
find_package(Boost 1.54 REQUIRED COMPONENTS python) | ||
include_directories(${PYTHON_INCLUDE_DIRS}) | ||
|
||
add_library(${PROJECT_NAME} MODULE ${SRC}) | ||
|
||
if (PLATFORM_MAC) | ||
omim_link_libraries( | ||
${PROJECT_NAME} | ||
${Qt5Widgets_LIBRARIES} | ||
"-framework Cocoa" | ||
"-framework IOKit" | ||
"-framework QuartzCore" | ||
"-framework SystemConfiguration" | ||
) | ||
endif() | ||
|
||
if (PLATFORM_WIN OR PLATFORM_LINUX) | ||
omim_link_libraries( | ||
${PROJECT_NAME} | ||
${Qt5Widgets_LIBRARIES} | ||
) | ||
endif() | ||
|
||
omim_link_libraries(${PROJECT_NAME} ${PYTHON_LIBRARIES} ${Boost_LIBRARIES} traffic platform geometry base) | ||
set_target_properties(${PROJECT_NAME} PROPERTIES PREFIX "") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
#include "traffic/traffic_info.hpp" | ||
#include "traffic/speed_groups.hpp" | ||
|
||
#include "pyhelpers/vector_uint8.hpp" | ||
|
||
#include <boost/python.hpp> | ||
#include <boost/python/suite/indexing/map_indexing_suite.hpp> | ||
|
||
namespace | ||
{ | ||
using namespace boost::python; | ||
|
||
vector<uint8_t> Serialize(traffic::TrafficInfo::Coloring const & coloring) | ||
{ | ||
vector<uint8_t> data; | ||
traffic::TrafficInfo::SerializeTrafficData(coloring, data); | ||
return data; | ||
} | ||
|
||
traffic::TrafficInfo::Coloring Deserialize(vector<uint8_t> const & data) | ||
{ | ||
traffic::TrafficInfo::Coloring coloring; | ||
traffic::TrafficInfo::DeserializeTrafficData(data, coloring); | ||
return coloring; | ||
} | ||
|
||
string RoadSegmentIdRepr(traffic::TrafficInfo::RoadSegmentId const & v) | ||
{ | ||
stringstream ss; | ||
ss << "RoadSegmentId(" << v.m_fid << ", " << v.m_idx << ", " << int(v.m_dir) << ")"; | ||
return ss.str(); | ||
} | ||
} // namespace | ||
|
||
BOOST_PYTHON_MODULE(pytraffic) | ||
{ | ||
using namespace boost::python; | ||
|
||
// Register the to-python converters. | ||
to_python_converter<vector<uint8_t>, vector_uint8t_to_str>(); | ||
vector_uint8t_from_python_str(); | ||
|
||
class_<traffic::TrafficInfo::RoadSegmentId>("RoadSegmentId", init<uint32_t, uint16_t, uint8_t>()) | ||
.def("__repr__", &RoadSegmentIdRepr) | ||
; | ||
|
||
enum_<traffic::SpeedGroup>("SpeedGroup") | ||
.value("G0", traffic::SpeedGroup::G0) | ||
.value("G1", traffic::SpeedGroup::G1) | ||
.value("G2", traffic::SpeedGroup::G2) | ||
.value("G3", traffic::SpeedGroup::G3) | ||
.value("G4", traffic::SpeedGroup::G4) | ||
.value("G5", traffic::SpeedGroup::G5) | ||
.value("TempBlock", traffic::SpeedGroup::TempBlock) | ||
.value("Unknown", traffic::SpeedGroup::Unknown) | ||
; | ||
|
||
class_<traffic::TrafficInfo::Coloring>("Coloring") | ||
.def(map_indexing_suite<traffic::TrafficInfo::Coloring>()) | ||
; | ||
|
||
def("dumps", Serialize); | ||
def("loads", Deserialize); | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.