Skip to content

Commit

Permalink
Split python bindings in multiple files
Browse files Browse the repository at this point in the history
  • Loading branch information
Ozaq committed Dec 15, 2023
1 parent 77382fa commit bf78517
Show file tree
Hide file tree
Showing 22 changed files with 1,311 additions and 1,072 deletions.
1 change: 1 addition & 0 deletions libcommon/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
add_library(common INTERFACE
src/Unreachable.hpp
src/Visitor.hpp
)

target_include_directories(common INTERFACE
Expand Down
3 changes: 3 additions & 0 deletions libjupedsim/src/Conversion.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

#include "Conversion.hpp"

namespace jupedsim::detail
{
Point intoPoint(JPS_Point p)
{
return {p.x, p.y};
Expand All @@ -22,3 +24,4 @@ std::tuple<double, double> intoTuple(JPS_Point p)
{
return std::make_tuple(p.x, p.y);
}
} // namespace jupedsim::detail
3 changes: 3 additions & 0 deletions libjupedsim/src/Conversion.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,13 @@
#include "jupedsim/jupedsim.h"
#include <tuple>

namespace jupedsim::detail
{
Point intoPoint(JPS_Point p);

JPS_Point intoJPS_Point(Point p);

JPS_Point intoJPS_Point(std::tuple<double, double> p);

std::tuple<double, double> intoTuple(JPS_Point p);
} // namespace jupedsim::detail
4 changes: 4 additions & 0 deletions libjupedsim/src/jupedsim.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@
#include <tuple>
#include <vector>

using jupedsim::detail::intoJPS_Point;
using jupedsim::detail::intoPoint;
using jupedsim::detail::intoTuple;

////////////////////////////////////////////////////////////////////////////////////////////////////
/// BuildInfo
////////////////////////////////////////////////////////////////////////////////////////////////////
Expand Down
20 changes: 18 additions & 2 deletions python_bindings_jupedsim/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,21 @@
add_library(py_jupedsim MODULE
bindings_jupedsim.cpp
build_info.cpp
conversion.cpp
conversion.hpp
generalized_centrifugal_force_model.cpp
collision_free_speed_model.cpp
logging.cpp
logging.hpp
trace.cpp
wrapper.hpp
geometry.cpp
routing.cpp
simulation.cpp
agent.cpp
stage.cpp
journey.cpp
transition.cpp
)

target_link_libraries(py_jupedsim
Expand All @@ -19,8 +35,8 @@ if(NOT MSVC AND NOT ${CMAKE_BUILD_TYPE} MATCHES Debug|RelWithDebInfo)
pybind11_strip(py_jupedsim)
endif()

set_target_properties(py_jupedsim
PROPERTIES
set_target_properties(py_jupedsim
PROPERTIES
CXX_VISIBILITY_PRESET "hidden"
CUDA_VISIBILITY_PRESET "hidden"
)
Expand Down
82 changes: 82 additions & 0 deletions python_bindings_jupedsim/agent.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
// Copyright © 2012-2023 Forschungszentrum Jülich GmbH
// SPDX-License-Identifier: LGPL-3.0-or-later
#include "conversion.hpp"
#include "wrapper.hpp"

#include <Unreachable.hpp>
#include <jupedsim/jupedsim.h>

#include <pybind11/pybind11.h>
#include <pybind11/stl.h>

namespace py = pybind11;

void init_agent(py::module_& m)
{
py::class_<JPS_AgentIterator_Wrapper>(m, "CollisionFreeSpeedModelAgentIterator")
.def(
"__iter__",
[](JPS_AgentIterator_Wrapper& w) -> JPS_AgentIterator_Wrapper& { return w; })
.def("__next__", [](JPS_AgentIterator_Wrapper& w) {
const auto result = JPS_AgentIterator_Next(w.handle);
if(result) {
return std::make_unique<JPS_Agent_Wrapper>(result);
}
throw py::stop_iteration{};
});
py::class_<JPS_AgentIdIterator_Wrapper>(m, "AgentIdIterator")
.def(
"__iter__",
[](JPS_AgentIdIterator_Wrapper& w) -> JPS_AgentIdIterator_Wrapper& { return w; })
.def("__next__", [](JPS_AgentIdIterator_Wrapper& w) {
const auto id = JPS_AgentIdIterator_Next(w.handle);
if(id != 0) {
return id;
}
throw py::stop_iteration{};
});
py::class_<JPS_Agent_Wrapper>(m, "Agent")
.def_property_readonly(
"id", [](const JPS_Agent_Wrapper& w) { return JPS_Agent_GetId(w.handle); })
.def_property_readonly(
"journey_id",
[](const JPS_Agent_Wrapper& w) { return JPS_Agent_GetJourneyId(w.handle); })
.def_property_readonly(
"stage_id", [](const JPS_Agent_Wrapper& w) { return JPS_Agent_GetStageId(w.handle); })
.def_property_readonly(
"position",
[](const JPS_Agent_Wrapper& w) { return intoTuple(JPS_Agent_GetPosition(w.handle)); })
.def_property_readonly(
"orientation",
[](const JPS_Agent_Wrapper& w) {
return intoTuple(JPS_Agent_GetOrientation(w.handle));
})
.def_property(
"target",
[](const JPS_Agent_Wrapper& w) { return intoTuple(JPS_Agent_GetTarget(w.handle)); },
[](JPS_Agent_Wrapper& w, std::tuple<double, double> target) {
JPS_ErrorMessage errorMsg{};
auto success = JPS_Agent_SetTarget(w.handle, intoJPS_Point(target), &errorMsg);
if(!success) {
auto msg = std::string(JPS_ErrorMessage_GetMessage(errorMsg));
JPS_ErrorMessage_Free(errorMsg);
throw std::runtime_error{msg};
}
})
.def_property_readonly(
"model",
[](const JPS_Agent_Wrapper& w)
-> std::variant<
std::unique_ptr<JPS_GeneralizedCentrifugalForceModelState_Wrapper>,
std::unique_ptr<JPS_CollisionFreeSpeedModelState_Wrapper>> {
switch(JPS_Agent_GetModelType(w.handle)) {
case JPS_GeneralizedCentrifugalForceModel:
return std::make_unique<JPS_GeneralizedCentrifugalForceModelState_Wrapper>(
JPS_Agent_GetGeneralizedCentrifugalForceModelState(w.handle, nullptr));
case JPS_CollisionFreeSpeedModel:
return std::make_unique<JPS_CollisionFreeSpeedModelState_Wrapper>(
JPS_Agent_GetCollisionFreeSpeedModelState(w.handle, nullptr));
}
UNREACHABLE();
});
}
Loading

0 comments on commit bf78517

Please sign in to comment.