From 94a31fe54591a20b50e449c8adb7eac644874644 Mon Sep 17 00:00:00 2001 From: Ozaq Date: Thu, 7 Dec 2023 15:59:01 +0100 Subject: [PATCH] Split jupedsim.h/cpp --- libjupedsim/CMakeLists.txt | 33 +- libjupedsim/include/jupedsim/agent.h | 137 ++ libjupedsim/include/jupedsim/build_info.h | 49 + .../jupedsim/collision_free_speed_model.h | 170 ++ libjupedsim/include/jupedsim/error.h | 36 + libjupedsim/include/jupedsim/export.h | 13 + .../generalized_centrifugal_force_model.h | 285 +++ libjupedsim/include/jupedsim/geometry.h | 129 ++ libjupedsim/include/jupedsim/journey.h | 53 + libjupedsim/include/jupedsim/jupedsim.h | 1396 +------------- libjupedsim/include/jupedsim/logging.h | 53 + .../include/jupedsim/operational_model.h | 24 + libjupedsim/include/jupedsim/routing.h | 66 + libjupedsim/include/jupedsim/simulation.h | 349 ++++ libjupedsim/include/jupedsim/stage.h | 77 + libjupedsim/include/jupedsim/transition.h | 60 + libjupedsim/include/jupedsim/types.h | 91 + libjupedsim/src/AgentIterator.hpp | 10 + libjupedsim/src/JourneyDescription.hpp | 8 + libjupedsim/src/agent.cpp | 171 ++ libjupedsim/src/build_info.cpp | 19 + .../src/collision_free_speed_model.cpp | 107 ++ libjupedsim/src/error.cpp | 19 + .../generalized_centrifugal_force_model.cpp | 224 +++ libjupedsim/src/geometry.cpp | 146 ++ libjupedsim/src/journey.cpp | 136 ++ libjupedsim/src/jupedsim.cpp | 1621 ----------------- libjupedsim/src/logging.cpp | 48 + libjupedsim/src/operational_model.cpp | 13 + libjupedsim/src/routing.cpp | 106 ++ libjupedsim/src/simulation.cpp | 558 ++++++ libjupedsim/src/stage.cpp | 156 ++ 32 files changed, 3358 insertions(+), 3005 deletions(-) create mode 100644 libjupedsim/include/jupedsim/agent.h create mode 100644 libjupedsim/include/jupedsim/build_info.h create mode 100644 libjupedsim/include/jupedsim/collision_free_speed_model.h create mode 100644 libjupedsim/include/jupedsim/error.h create mode 100644 libjupedsim/include/jupedsim/export.h create mode 100644 libjupedsim/include/jupedsim/generalized_centrifugal_force_model.h create mode 100644 libjupedsim/include/jupedsim/geometry.h create mode 100644 libjupedsim/include/jupedsim/journey.h create mode 100644 libjupedsim/include/jupedsim/logging.h create mode 100644 libjupedsim/include/jupedsim/operational_model.h create mode 100644 libjupedsim/include/jupedsim/routing.h create mode 100644 libjupedsim/include/jupedsim/simulation.h create mode 100644 libjupedsim/include/jupedsim/stage.h create mode 100644 libjupedsim/include/jupedsim/transition.h create mode 100644 libjupedsim/include/jupedsim/types.h create mode 100644 libjupedsim/src/JourneyDescription.hpp create mode 100644 libjupedsim/src/agent.cpp create mode 100644 libjupedsim/src/build_info.cpp create mode 100644 libjupedsim/src/collision_free_speed_model.cpp create mode 100644 libjupedsim/src/error.cpp create mode 100644 libjupedsim/src/generalized_centrifugal_force_model.cpp create mode 100644 libjupedsim/src/geometry.cpp create mode 100644 libjupedsim/src/journey.cpp delete mode 100644 libjupedsim/src/jupedsim.cpp create mode 100644 libjupedsim/src/logging.cpp create mode 100644 libjupedsim/src/operational_model.cpp create mode 100644 libjupedsim/src/routing.cpp create mode 100644 libjupedsim/src/simulation.cpp create mode 100644 libjupedsim/src/stage.cpp diff --git a/libjupedsim/CMakeLists.txt b/libjupedsim/CMakeLists.txt index 4ef72f9be9..953c12bde9 100644 --- a/libjupedsim/CMakeLists.txt +++ b/libjupedsim/CMakeLists.txt @@ -7,7 +7,18 @@ add_library(jupedsim_obj OBJECT src/Conversion.cpp src/Conversion.hpp src/ErrorMessage.hpp - src/jupedsim.cpp + src/agent.cpp + src/build_info.cpp + src/collision_free_speed_model.cpp + src/error.cpp + src/generalized_centrifugal_force_model.cpp + src/geometry.cpp + src/journey.cpp + src/logging.cpp + src/operational_model.cpp + src/simulation.cpp + src/stage.cpp + src/routing.cpp ) target_compile_options(jupedsim_obj PRIVATE @@ -92,10 +103,26 @@ install(TARGETS jupedsim INCLUDES DESTINATION include ) +set(header_dest include/jupedsim) install( FILES - include/jupedsim/jupedsim.h - DESTINATION include/jupedsim + ${header_dest}/agent.h + ${header_dest}/error.h + ${header_dest}/geometry.h + ${header_dest}/logging.h + ${header_dest}/simulation.h + ${header_dest}/types.h + ${header_dest}/build_info.h + ${header_dest}/export.h + ${header_dest}/journey.h + ${header_dest}/operational_model.h + ${header_dest}/stage.h + ${header_dest}/collision_free_speed_model.h + ${header_dest}/generalized_centrifugal_force_model.h + ${header_dest}/jupedsim.h + ${header_dest}/routing.h + ${header_dest}/transition.h + DESTINATION ${header_dest} ) install(EXPORT jupedsimTargets diff --git a/libjupedsim/include/jupedsim/agent.h b/libjupedsim/include/jupedsim/agent.h new file mode 100644 index 0000000000..f7243c0fc2 --- /dev/null +++ b/libjupedsim/include/jupedsim/agent.h @@ -0,0 +1,137 @@ +/* Copyright © 2012-2023 Forschungszentrum Jülich GmbH */ +/* SPDX-License-Identifier: LGPL-3.0-or-later */ +#pragma once + +#include "collision_free_speed_model.h" +#include "error.h" +#include "export.h" +#include "generalized_centrifugal_force_model.h" +#include "types.h" + +#include /*NOLINT(modernize-deprecated-headers)*/ + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * Opaque type of an agent + */ +typedef struct JPS_Agent_t* JPS_Agent; + +/** + * Access the agents id. + * @param handle of the agent to access. + * @return the id + */ +JUPEDSIM_API JPS_AgentId JPS_Agent_GetId(JPS_Agent handle); + +/** + * Access the agents journey id. + * @param handle of the agent to access. + * @return the id of this agents journey + */ +JUPEDSIM_API JPS_JourneyId JPS_Agent_GetJourneyId(JPS_Agent handle); + +/** + * Access the agents currently targeted stage id. + * @param handle of the agent to access. + * @return the id of the stage currently targeted + */ +JUPEDSIM_API JPS_StageId JPS_Agent_GetStageId(JPS_Agent handle); + +/** + * Access the agents position. + * @param handle of the agent to access. + * @return position + */ +JUPEDSIM_API JPS_Point JPS_Agent_GetPosition(JPS_Agent handle); + +/** + * Access the agents current target. + * @param handle of the agent to access. + * @return target of the agent + */ +JUPEDSIM_API JPS_Point JPS_Agent_GetTarget(JPS_Agent handle); + +JUPEDSIM_API bool +JPS_Agent_SetTarget(JPS_Agent handle, JPS_Point target, JPS_ErrorMessage* errorMessage); + +/** + * Access the agents orientation. + * The orientation is unit length. + * @param handle of the agent to access. + * @return the orientation + */ +JUPEDSIM_API JPS_Point JPS_Agent_GetOrientation(JPS_Agent handle); + +/** + * Access the agets model type information. + * @param handle of the agent to access. + * @return type of model in use + */ +JUPEDSIM_API JPS_ModelType JPS_Agent_GetModelType(JPS_Agent handle); + +/** + * Access Generalized Centrifugal Force model state. + * Precondition: Agent needs to use Generalized Centrifugal Force model + * @param handle of the agent to access. + * @param[out] errorMessage if not NULL: will be set to a JPS_ErrorMessage in case of an error. + * @return state or NULL on error + */ +JUPEDSIM_API JPS_GeneralizedCentrifugalForceModelState +JPS_Agent_GetGeneralizedCentrifugalForceModelState( + JPS_Agent handle, + JPS_ErrorMessage* errorMessage); + +/** + * Access Collision Free Speed model state. + * Precondition: Agent needs to use Collision Free Speed model + * @param handle of the agent to access. + * @param[out] errorMessage if not NULL: will be set to a JPS_ErrorMessage in case of an error. + * @return state or NULL on error + */ +JUPEDSIM_API JPS_CollisionFreeSpeedModelState +JPS_Agent_GetCollisionFreeSpeedModelState(JPS_Agent handle, JPS_ErrorMessage* errorMessage); + +/** + * Opaque type of an iterator over agents + */ +typedef struct JPS_AgentIterator_t* JPS_AgentIterator; + +/** + * Access the next element in the iterator. + * Calling JPS_AgentIterator_Next repeatedly on a finished iterator is save. + * @param handle of the iterator to advance and access + * @return an agent or NULL if the iterator is at the end. The pointer returned does not need to be + * freed and is invalidated on the next call to this function! + */ +JUPEDSIM_API JPS_Agent JPS_AgentIterator_Next(JPS_AgentIterator handle); + +/** + * Free the iterator. + * @param handle to the JPS_AgentIterator to free. + */ +JUPEDSIM_API void JPS_AgentIterator_Free(JPS_AgentIterator handle); + +/** + * Opaque type of an iterator over agent ids + */ +typedef struct JPS_AgentIdIterator_t* JPS_AgentIdIterator; + +/** + * Access the next element in the iterator. + * Calling JPS_AgentIterator_Next repeatedly on a finished iterator is save. + * @param handle of the iterator to advance and access + * @return an agentId, Zero in case the iterator has reachedits end. + */ +JUPEDSIM_API JPS_AgentId JPS_AgentIdIterator_Next(JPS_AgentIdIterator handle); + +/** + * Free the iterator. + * @param handle to the JPS_AgentIterator to free. + */ +JUPEDSIM_API void JPS_AgentIdIterator_Free(JPS_AgentIdIterator handle); +#ifdef __cplusplus +} +#endif diff --git a/libjupedsim/include/jupedsim/build_info.h b/libjupedsim/include/jupedsim/build_info.h new file mode 100644 index 0000000000..e44df46655 --- /dev/null +++ b/libjupedsim/include/jupedsim/build_info.h @@ -0,0 +1,49 @@ +/* Copyright © 2012-2023 Forschungszentrum Jülich GmbH */ +/* SPDX-License-Identifier: LGPL-3.0-or-later */ +#pragma once + +#include "export.h" + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * Contains build information about this library + */ +typedef struct JPS_BuildInfo { + /** + * Shortened commit id from which this version was build + */ + const char* git_commit_hash; + /** + * Date the last commit was made + */ + const char* git_commit_date; + /** + * Branch name from which this libabry was build + */ + const char* git_branch; + /** + * Compiler identifier used to build this library + */ + const char* compiler; + /** + * Compiler version used to build this library + */ + const char* compiler_version; + /** + * Version of this library + */ + const char* library_version; +} JPS_BuildInfo; + +/** + * Access meta information about the library. + * @return build information about this library + */ +JUPEDSIM_API JPS_BuildInfo JPS_GetBuildInfo(); + +#ifdef __cplusplus +} +#endif diff --git a/libjupedsim/include/jupedsim/collision_free_speed_model.h b/libjupedsim/include/jupedsim/collision_free_speed_model.h new file mode 100644 index 0000000000..5c1e7da287 --- /dev/null +++ b/libjupedsim/include/jupedsim/collision_free_speed_model.h @@ -0,0 +1,170 @@ +/* Copyright © 2012-2023 Forschungszentrum Jülich GmbH */ +/* SPDX-License-Identifier: LGPL-3.0-or-later */ +#pragma once + +#include "error.h" +#include "export.h" +#include "operational_model.h" +#include "types.h" + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * Opaque type for a Collision Free Speed Model Builder + */ +typedef struct JPS_CollisionFreeSpeedModelBuilder_t* JPS_CollisionFreeSpeedModelBuilder; + +/** + * Creates a Collision Free Speed Model builder. + * @param strength_neighbor_repulsion describes the strength with which neighbors repulse each + * other. + * @param range_neighbor_repulsion describes the range at hich neighbors repulse each other. + * @param strength_geometry_repulsion describes the strength with which neighbors are repules by + * geometry. + * @param range_geometry_repulsion describes the range at hich neighbors are repulsed by geometry. + * @return the builder + */ +JUPEDSIM_API JPS_CollisionFreeSpeedModelBuilder JPS_CollisionFreeSpeedModelBuilder_Create( + double strengthNeighborRepulsion, + double rangeNeighborRepulsion, + double strengthGeometryRepulsion, + double rangeGeometryRepulsion); + +/** + * Creates a JPS_OperationalModel of type Collision Free Speed Model from the + * JPS_GeneralizedCentrifugalForceModelBuilder. + * @param handle the builder to operate on + * @param[out] errorMessage if not NULL: will be set to a JPS_ErrorMessage in case of an error + * @return a JPS_GeneralizedCentrifugalForceModel or NULL if an error occured. + */ +JUPEDSIM_API JPS_OperationalModel JPS_CollisionFreeSpeedModelBuilder_Build( + JPS_CollisionFreeSpeedModelBuilder handle, + JPS_ErrorMessage* errorMessage); + +/** + * Frees a JPS_CollisionFreeSpeedModelBuilder + * @param handle to the JPS_CollisionFreeSpeedModelBuilder to free. + */ +JUPEDSIM_API void +JPS_CollisionFreeSpeedModelBuilder_Free(JPS_CollisionFreeSpeedModelBuilder handle); + +/** + * Opaque type of Collision Free Speed model state + */ +typedef struct JPS_CollisionFreeSpeedModelState_t* JPS_CollisionFreeSpeedModelState; + +/** + * Read e0 of this agent. + * @param handle of the Agent to access. + * @return e0 of this agent + */ +JUPEDSIM_API JPS_Point +JPS_CollisionFreeSpeedModelState_GetE0(JPS_CollisionFreeSpeedModelState handle); + +/** + * Write e0 of this agent. + * @param handle of the Agent to access. + * @param e0 of this agent. + */ +JUPEDSIM_API void +JPS_CollisionFreeSpeedModelState_SetE0(JPS_CollisionFreeSpeedModelState handle, JPS_Point e0); + +/** + * Read time gap of this agent. + * @param handle of the Agent to access. + * @return time gap of this agent + */ +JUPEDSIM_API double +JPS_CollisionFreeSpeedModelState_GetTimeGap(JPS_CollisionFreeSpeedModelState handle); + +/** + * Write time gap of this agent. + * @param handle of the Agent to access. + * @param time_gap of this agent. + */ +JUPEDSIM_API void JPS_CollisionFreeSpeedModelState_SetTimeGap( + JPS_CollisionFreeSpeedModelState handle, + double time_gap); + +/** + * Read tau of this agent. + * @param handle of the Agent to access. + * @return tau of this agent + */ +JUPEDSIM_API double +JPS_CollisionFreeSpeedModelState_GetTau(JPS_CollisionFreeSpeedModelState handle); + +/** + * Write tau of this agent. + * @param handle of the Agent to access. + * @param tau of this agent. + */ +JUPEDSIM_API void +JPS_CollisionFreeSpeedModelState_SetTau(JPS_CollisionFreeSpeedModelState handle, double tau); + +/** + * Read v0 of this agent. + * @param handle of the Agent to access. + * @return v0 of this agent + */ +JUPEDSIM_API double JPS_CollisionFreeSpeedModelState_GetV0(JPS_CollisionFreeSpeedModelState handle); + +/** + * Write v0 of this agent. + * @param handle of the Agent to access. + * @param v0 of this agent. + */ +JUPEDSIM_API void +JPS_CollisionFreeSpeedModelState_SetV0(JPS_CollisionFreeSpeedModelState handle, double v0); + +/** + * Read radius of this agent. + * @param handle of the Agent to access. + * @return radius of this agent + */ +JUPEDSIM_API double +JPS_CollisionFreeSpeedModelState_GetRadius(JPS_CollisionFreeSpeedModelState handle); + +/** + * Write radius of this agent in meters. + * @param handle of the Agent to access. + * @param radius (m) of this agent. + */ +JUPEDSIM_API void +JPS_CollisionFreeSpeedModelState_SetRadius(JPS_CollisionFreeSpeedModelState handle, double radius); +/** + * Describes parameters of an Agent in GeneralizedCentrifugalForceModel + */ +typedef struct JPS_CollisionFreeSpeedModelAgentParameters { + /** + * Position of the agent. + * The position needs to inside the accessible area. + */ + JPS_Point position{0, 0}; + /** + * Defines the journey this agent will take use + */ + JPS_JourneyId journeyId = 0; + /** + * Defines the current stage of its journey + */ + JPS_StageId stageId = 0; + /** + * @param time_gap of the agents using this profile (T in the OV-function) + */ + double time_gap = 1.; + /** + *@param v0 of the agents using this profile(desired speed) double radius; + */ + double v0 = 1.2; + /** + *@param radius of the agent in 'meters' + */ + double radius = 0.2; +} JPS_CollisionFreeSpeedModelAgentParameters; + +#ifdef __cplusplus +} +#endif diff --git a/libjupedsim/include/jupedsim/error.h b/libjupedsim/include/jupedsim/error.h new file mode 100644 index 0000000000..86840eb712 --- /dev/null +++ b/libjupedsim/include/jupedsim/error.h @@ -0,0 +1,36 @@ +/* Copyright © 2012-2023 Forschungszentrum Jülich GmbH */ +/* SPDX-License-Identifier: LGPL-3.0-or-later */ +#pragma once + +#include "export.h" + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * Opaque type for error messages. + * JPS_ErrorMessage will be returned via out parameters from functions that can fail. + * JPS_ErrorMessage have to be deallocated with JPS_ErrorMessage_Free. + */ +typedef struct JPS_ErrorMessage_t* JPS_ErrorMessage; + +/** + * Access c-string representation of the error message. + * Lifetime: the returned c-string will be valid until JPS_ErrorMessage_Free has been called on + * this message. + * @param handle to the JPS_ErrorMessage to inspect. + * @return Error message as null terminated c string. This pointer is valid until + * JPS_ErrorMessage_Free is called on this handle. + */ +JUPEDSIM_API const char* JPS_ErrorMessage_GetMessage(JPS_ErrorMessage handle); + +/** + * Frees a JPS_ErrorMessage + * @param handle to the JPS_ErrorMessage to free. + */ +JUPEDSIM_API void JPS_ErrorMessage_Free(JPS_ErrorMessage handle); + +#ifdef __cplusplus +} +#endif diff --git a/libjupedsim/include/jupedsim/export.h b/libjupedsim/include/jupedsim/export.h new file mode 100644 index 0000000000..a3987845d3 --- /dev/null +++ b/libjupedsim/include/jupedsim/export.h @@ -0,0 +1,13 @@ +/* Copyright © 2012-2023 Forschungszentrum Jülich GmbH */ +/* SPDX-License-Identifier: LGPL-3.0-or-later */ +#pragma once + +#ifdef _WIN32 +#ifdef JUPEDSIM_API_EXPORTS +#define JUPEDSIM_API __declspec(dllexport) +#else +#define JUPEDSIM_API __declspec(dllimport) +#endif +#else +#define JUPEDSIM_API +#endif diff --git a/libjupedsim/include/jupedsim/generalized_centrifugal_force_model.h b/libjupedsim/include/jupedsim/generalized_centrifugal_force_model.h new file mode 100644 index 0000000000..8a97dd066c --- /dev/null +++ b/libjupedsim/include/jupedsim/generalized_centrifugal_force_model.h @@ -0,0 +1,285 @@ +/* Copyright © 2012-2023 Forschungszentrum Jülich GmbH */ +/* SPDX-License-Identifier: LGPL-3.0-or-later */ +#pragma once + +#include "error.h" +#include "export.h" +#include "operational_model.h" +#include "types.h" + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * Opaque type for a GeneralizedCentrifugalForceModel Model Builder + */ +typedef struct JPS_GeneralizedCentrifugalForceModelBuilder_t* + JPS_GeneralizedCentrifugalForceModelBuilder; + +/** + * Creates a GeneralizedCentrifugalForceModel model builder. + * @param strengthNeighborRepulsion Strength of the repulsion from neighbors + * @param strengthGeometryRepulsion Strength of the repulsion from geometry boundaries + * @param maxNeighborInteractionDistance cut-off-radius for ped-ped repulsion (r_c in FIG. 7) + * @param maxGeometryInteractionDistance cut-off-radius for ped-wall repulsion (r_c in FIG. 7) + * @param maxNeighborInterpolationDistance distance of interpolation of repulsive force for + * ped-ped interaction (r_eps in FIG. 7) + * @param maxGeometryInterpolationDistance distance of interpolation of repulsive force for + * ped-wall interaction (r_eps in FIG. 7) + * @param maxNeighborRepulsionForce maximum of the repulsion force for ped-ped interaction by + * contact of ellipses (f_m in FIG. 7) + * @param maxGeometryRepulsionForce maximum of the repulsion force for ped-wall interaction by + * contact of ellipses (f_m in FIG. 7) + * @return the GeneralizedCentrifugalForceModel model builder + */ +JUPEDSIM_API JPS_GeneralizedCentrifugalForceModelBuilder +JPS_GeneralizedCentrifugalForceModelBuilder_Create( + double strengthNeighborRepulsion, + double strengthGeometryRepulsion, + double maxNeighborInteractionDistance, + double maxGeometryInteractionDistance, + double maxNeighborInterpolationDistance, + double maxGeometryInterpolationDistance, + double maxNeighborRepulsionForce, + double maxGeometryRepulsionForce); + +/** + * Creates a JPS_OperationalModel of type GeneralizedCentrifugalForceModel Model from the + * JPS_GeneralizedCentrifugalForceModelBuilder. + * @param handle the builder to operate on + * @param[out] errorMessage if not NULL: will be set to a JPS_ErrorMessage in case of an error + * @return a JPS_GeneralizedCentrifugalForceModel or NULL if an error occured. + */ +JUPEDSIM_API JPS_OperationalModel JPS_GeneralizedCentrifugalForceModelBuilder_Build( + JPS_GeneralizedCentrifugalForceModelBuilder handle, + JPS_ErrorMessage* errorMessage); + +/** + * Frees a JPS_GeneralizedCentrifugalForceModelBuilder + * @param handle to the JPS_GeneralizedCentrifugalForceModelBuilder to free. + */ +JUPEDSIM_API void JPS_GeneralizedCentrifugalForceModelBuilder_Free( + JPS_GeneralizedCentrifugalForceModelBuilder handle); + +/** + * Opaque type of Generalized Centrifugal Force model state + */ +typedef struct JPS_GeneralizedCentrifugalForceModelState_t* + JPS_GeneralizedCentrifugalForceModelState; + +/** + * Read speed of this agent. + * @param handle of the Agent to access. + * @return speed in m/s + */ +JUPEDSIM_API double JPS_GeneralizedCentrifugalForceModelState_GetSpeed( + JPS_GeneralizedCentrifugalForceModelState handle); + +/** + * Write speed of this agent. + * @param handle of the Agent to access. + * @param speed in m/s + */ +JUPEDSIM_API void JPS_GeneralizedCentrifugalForceModelState_SetSpeed( + JPS_GeneralizedCentrifugalForceModelState handle, + double speed); + +/** + * Read desired orientation of this agent. + * @param handle of the Agent to access. + * @return 2D orientation vector + */ +JUPEDSIM_API JPS_Point +JPS_GeneralizedCentrifugalForceModelState_GetE0(JPS_GeneralizedCentrifugalForceModelState handle); + +/** + * Write desired direction of this agent. + * @param handle of the Agent to access. + * @param e0 desired orientation of this agent. + */ +JUPEDSIM_API void JPS_GeneralizedCentrifugalForceModelState_SetE0( + JPS_GeneralizedCentrifugalForceModelState handle, + JPS_Point e0); + +/** + * Read mass in Kg of this agent. + * @param handle of the Agent to access. + * @return mass (Kg) of this agent + */ +JUPEDSIM_API double +JPS_GeneralizedCentrifugalForceModelState_GetMass(JPS_GeneralizedCentrifugalForceModelState handle); + +/** + * Write mass in Kg of this agent. + * @param handle of the Agent to access. + * @param mass (Kg) of this agent. + */ +JUPEDSIM_API void JPS_GeneralizedCentrifugalForceModelState_SetMass( + JPS_GeneralizedCentrifugalForceModelState handle, + double mass); + +/** + * Read tau of this agent. + * @param handle of the Agent to access. + * @return tau of this agent + */ +JUPEDSIM_API double +JPS_GeneralizedCentrifugalForceModelState_GetTau(JPS_GeneralizedCentrifugalForceModelState handle); + +/** + * Write tau of this agent. + * @param handle of the Agent to access. + * @param tau of this agent. + */ +JUPEDSIM_API void JPS_GeneralizedCentrifugalForceModelState_SetTau( + JPS_GeneralizedCentrifugalForceModelState handle, + double tau); + +/** + * Read v0 of this agent. + * @param handle of the Agent to access. + * @return v0 of this agent + */ +JUPEDSIM_API double +JPS_GeneralizedCentrifugalForceModelState_GetV0(JPS_GeneralizedCentrifugalForceModelState handle); + +/** + * Write v0 of this agent. + * @param handle of the Agent to access. + * @param v0 of this agent. + */ +JUPEDSIM_API void JPS_GeneralizedCentrifugalForceModelState_SetV0( + JPS_GeneralizedCentrifugalForceModelState handle, + double v0); + +/** + * Read a_v of this agent. + * @param handle of the Agent to access. + * @return AV of this agent + */ +JUPEDSIM_API double +JPS_GeneralizedCentrifugalForceModelState_GetAV(JPS_GeneralizedCentrifugalForceModelState handle); + +/** + * Write a_v of this agent. + * @param handle of the Agent to access. + * @param a_v of this agent. + */ +JUPEDSIM_API void JPS_GeneralizedCentrifugalForceModelState_SetAV( + JPS_GeneralizedCentrifugalForceModelState handle, + double a_v); + +/** + * Read a_min of this agent. + * @param handle of the Agent to access. + * @return a_min of this agent + */ +JUPEDSIM_API double +JPS_GeneralizedCentrifugalForceModelState_GetAMin(JPS_GeneralizedCentrifugalForceModelState handle); + +/** + * Write a_min of this agent. + * @param handle of the Agent to access. + * @param a_min of this agent. + */ +JUPEDSIM_API void JPS_GeneralizedCentrifugalForceModelState_SetAMin( + JPS_GeneralizedCentrifugalForceModelState handle, + double a_min); + +/** + * Read b_min of this agent. + * @param handle of the Agent to access. + * @return b_min of this agent + */ +JUPEDSIM_API double +JPS_GeneralizedCentrifugalForceModelState_GetBMin(JPS_GeneralizedCentrifugalForceModelState handle); + +/** + * Write b_min of this agent. + * @param handle of the Agent to access. + * @param b_min of this agent. + */ +JUPEDSIM_API void JPS_GeneralizedCentrifugalForceModelState_SetBMin( + JPS_GeneralizedCentrifugalForceModelState handle, + double b_min); + +/** + * Read b_max of this agent. + * @param handle of the Agent to access. + * @return b_max of this agent + */ +JUPEDSIM_API double +JPS_GeneralizedCentrifugalForceModelState_GetBMax(JPS_GeneralizedCentrifugalForceModelState handle); + +/** + * Write b_max of this agent. + * @param handle of the Agent to access. + * @param a_min of this agent. + */ +JUPEDSIM_API void JPS_GeneralizedCentrifugalForceModelState_SetBMax( + JPS_GeneralizedCentrifugalForceModelState handle, + double b_max); + +/** + * Describes parameters of an Agent in GeneralizedCentrifugalForceModel + */ +typedef struct JPS_GeneralizedCentrifugalForceModelAgentParameters { + /** + * Initial speed of the Agent + */ + double speed = 0.0; + /** + * Desired orientation. + */ + JPS_Point e0{0, 0}; + /** + * Position of the agent. + * The position needs to inside the accessible area. + */ + JPS_Point position{0, 0}; + /* + * Orientation vector of the agent. + * The orientation vector will internally be normalized. + */ + JPS_Point orientation{0, 0}; + /** + * Defines the journey this agent will take use + */ + JPS_JourneyId journeyId = 0; + /** + * Defines the current stage of its journey + */ + JPS_StageId stageId = 0; + /** + * Mass of the agent + */ + double mass = 1; + /** + * Tau of the agent + */ + double tau = 0.5; + /** + * V0 of the agent + */ + double v0 = 1.2; + /** + * a_v sagital axis stretch factor + */ + double a_v = 1.; + /** + * b_min minimum length of transversal axis in 'meters' + */ + double a_min = 0.2; + /** + * a_min minimum length of sagital axis in 'meters' + */ + double b_min = 0.2; + /** + * b_max maximum length of transversal axis in 'meters' + */ + double b_max = 0.4; +} JPS_GeneralizedCentrifugalForceModelAgentParameters; +#ifdef __cplusplus +} +#endif diff --git a/libjupedsim/include/jupedsim/geometry.h b/libjupedsim/include/jupedsim/geometry.h new file mode 100644 index 0000000000..a6732acded --- /dev/null +++ b/libjupedsim/include/jupedsim/geometry.h @@ -0,0 +1,129 @@ +/* Copyright © 2012-2023 Forschungszentrum Jülich GmbH */ +/* SPDX-License-Identifier: LGPL-3.0-or-later */ +#pragma once + +#include "error.h" +#include "export.h" +#include "operational_model.h" +#include "types.h" + +#include /*NOLINT(modernize-deprecated-headers)*/ + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * Opaque type that represents the geometry the simulation acts on. + * This type is created from JPS_GeometryBuilder. + */ +typedef struct JPS_Geometry_t const* JPS_Geometry; + +/** + * Returns the number of points that the outer boundary of the geometry consists of. + * @param handle to the JPS_Geometry to operate on + * @return Number of points in the outer boundary + */ +JUPEDSIM_API size_t JPS_Geometry_GetBoundarySize(JPS_Geometry handle); + +/** + * Returns the points the outer boundary consists of. + * @param handle to the JPS_Geometry to operate on + * @return the outer boundary. Do not free the returned pointer, it is still owned by JPS_Geometry. + */ +JUPEDSIM_API const JPS_Point* JPS_Geometry_GetBoundaryData(JPS_Geometry handle); + +/** + * Returns the number of holes in the geometry. + * @param handle to the JPS_Geometry to operate on + * @return Number of holes + */ +JUPEDSIM_API size_t JPS_Geometry_GetHoleCount(JPS_Geometry handle); + +/** + * Returns the number of points that hole consists of. + * @param handle to the JPS_Geometry to operate on + * @param index of the hole to access + * @param[out] errorMessage if not NULL: will be set to a JPS_ErrorMessage in case of an error + * @return Number of points in hole boundary or 0 on error. + */ +JUPEDSIM_API size_t +JPS_Geometry_GetHoleSize(JPS_Geometry handle, size_t hole_index, JPS_ErrorMessage* errorMessage); + +/** + * Returns the points the hole boundary consists of. + * @param handle to the JPS_Geometry to operate on + * @param index of the hole to access + * @param[out] errorMessage if not NULL: will be set to a JPS_ErrorMessage in case of an error + * @return points of the hole boundary or NULL on error. + */ +JUPEDSIM_API const JPS_Point* +JPS_Geometry_GetHoleData(JPS_Geometry handle, size_t hole_index, JPS_ErrorMessage* errorMessage); + +/** + * Frees a JPS_Geometry + * @param handle to the JPS_Geometry to free. + */ +JUPEDSIM_API void JPS_Geometry_Free(JPS_Geometry geometry); + +/** + * Opaque type that is used to build geometry for the simulation. + */ +typedef struct JPS_GeometryBuilder_t* JPS_GeometryBuilder; + +/** + * Creates a new GeometryBuilder + * @return the new builder. + */ +JUPEDSIM_API JPS_GeometryBuilder JPS_GeometryBuilder_Create(); + +/** + * Adds an accessible area to the geometry. + * The polygon described by points and pointCount must be a simple polygon. The polygon my be + * specified in clockwise or counter-clockwise order. + * Multiple accessible areas may overlap, the final accessible area is created by forming the union + * over all accessible areas. + * The Union over all accessible areas minus exclusions must form one polygon. + * @param polygon describing the accessible area. + * @param pointCount number of points the polygon consists of. + */ +JUPEDSIM_API void JPS_GeometryBuilder_AddAccessibleArea( + JPS_GeometryBuilder handle, + const JPS_Point* polygon, + size_t lenPolygon); + +/** + * Adds an accessible area to the geometry. + * The polygon described by points and pointCount must be a simple polygon. The polygon my be + * specified in clockwise or counter-clockwise order. + * Multiple exclusion areas may overlap, the final exclusion area is created by forming the union + * over all exclusion areas. + * Exclusion areas are subtracted from accessible areas. + * The Union over all accessible areas minus exclusions must form a single polygon. + * @param polygon describing the accessible area. + * @param pointCount number of points the polygon consists of. + */ +JUPEDSIM_API void JPS_GeometryBuilder_ExcludeFromAccessibleArea( + JPS_GeometryBuilder handle, + const JPS_Point* polygon, + size_t lenPolygon); + +/** + * Creates a JPS_Geometry from a JPS_GeometryBuilder. After this call the builder still has to be + * freed with JPS_GeometryBuilder_Free. + * @param handle to operate on. + * @param[out] errorMessage if not NULL: will be set to a JPS_ErrorMessage in case of an error. + * @return a JPS_Geometry handle on success or NULL on any error. + */ +JUPEDSIM_API JPS_Geometry +JPS_GeometryBuilder_Build(JPS_GeometryBuilder handle, JPS_ErrorMessage* errorMessage); + +/** + * Frees a JPS_GeometryBuilder. + * @param handle to the JPS_GeometryBuilder to free. + */ +JUPEDSIM_API void JPS_GeometryBuilder_Free(JPS_GeometryBuilder handle); + +#ifdef __cplusplus +} +#endif diff --git a/libjupedsim/include/jupedsim/journey.h b/libjupedsim/include/jupedsim/journey.h new file mode 100644 index 0000000000..feaba2a007 --- /dev/null +++ b/libjupedsim/include/jupedsim/journey.h @@ -0,0 +1,53 @@ +/* Copyright © 2012-2023 Forschungszentrum Jülich GmbH */ +/* SPDX-License-Identifier: LGPL-3.0-or-later */ +#pragma once + +#include "error.h" +#include "export.h" +#include "transition.h" +#include "types.h" + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * Opaque type that describes a journey + */ +typedef struct JPS_Journey_t* JPS_JourneyDescription; + +/** + * Creates an empty journey. + */ +JUPEDSIM_API JPS_JourneyDescription JPS_JourneyDescription_Create(); + +/** + * Extends the journey with the stage specified by its ID. + * @param handle of the JourneyDescription to extend. + * @param id of the stage to extend the journey with. + */ +JUPEDSIM_API void JPS_JourneyDescription_AddStage(JPS_JourneyDescription handle, JPS_StageId id); + +/** + * Specifies the transition to the next stage, once this stage is completed. + * @param handle of the JourneyDescription to modify. + * @param id of the stage to set the transition for. + * @param transition transition to the next stage. + * @param[out] errorMessage if not NULL: will be set to a JPS_ErrorMessage in case of an error. + * @return true if the transition for the stage could be added otherwise false + */ +JUPEDSIM_API bool JPS_JourneyDescription_SetTransitionForStage( + JPS_JourneyDescription handle, + JPS_StageId id, + JPS_Transition transition, + JPS_ErrorMessage* errorMessage); + +/** + * Frees a JPS_Journey. + * @param handle to the JPS_Journey to free. + */ +JUPEDSIM_API void JPS_JourneyDescription_Free(JPS_JourneyDescription handle); + +#ifdef __cplusplus +} +#endif diff --git a/libjupedsim/include/jupedsim/jupedsim.h b/libjupedsim/include/jupedsim/jupedsim.h index 66a7f18c75..e9567aa6b5 100644 --- a/libjupedsim/include/jupedsim/jupedsim.h +++ b/libjupedsim/include/jupedsim/jupedsim.h @@ -2,1384 +2,18 @@ /* SPDX-License-Identifier: LGPL-3.0-or-later */ #pragma once -#include -#ifdef _WIN32 -#ifdef JUPEDSIM_API_EXPORTS -#define JUPEDSIM_API __declspec(dllexport) -#else -#define JUPEDSIM_API __declspec(dllimport) -#endif -#else -#define JUPEDSIM_API -#endif - -#include /*NOLINT(modernize-deprecated-headers)*/ -#include /*NOLINT(modernize-deprecated-headers)*/ -#include /*NOLINT(modernize-deprecated-headers)*/ - -#ifdef __cplusplus -extern "C" { -#endif - -/** - * Contains build information about this library - */ -typedef struct JPS_BuildInfo { - /** - * Shortened commit id from which this version was build - */ - const char* git_commit_hash; - /** - * Date the last commit was made - */ - const char* git_commit_date; - /** - * Branch name from which this libabry was build - */ - const char* git_branch; - /** - * Compiler identifier used to build this library - */ - const char* compiler; - /** - * Compiler version used to build this library - */ - const char* compiler_version; - /** - * Version of this library - */ - const char* library_version; -} JPS_BuildInfo; - -/** - * Access meta information about the library. - * @return build information about this library - */ -JUPEDSIM_API JPS_BuildInfo JPS_GetBuildInfo(); - -/** - * Contains basic performance trace information - */ -typedef struct JPS_Trace { - /** - * Duration of the iterate call in micorseconds - */ - uint64_t iteration_duration; - /** - * Duration to compute updates of the operational decision level in micorseconds. - * This is fully contained in iterate. - */ - uint64_t operational_level_duration; -} JPS_Trace; - -/** - * A 2D coordinate. Units are 'meters' - */ -typedef struct JPS_Point { - /** - * x component in 'meters' - */ - double x; - /** - * y component in 'meters' - */ - double y; -} JPS_Point; - -/** - * Describes a waypoint. - */ -typedef struct JPS_Waypoint { - /** - * Position of the waypoint - */ - JPS_Point position; - /** - * Distance in 'meters' at which this waypoint is considered to be reached. - */ - double distance; -} JPS_Waypoint; - -/** - * Describes the pedestrian model used in the simulation. - */ -typedef enum JPS_ModelType { - JPS_GeneralizedCentrifugalForceModel, - JPS_CollisionFreeSpeedModel -} JPS_ModelType; - -/** - * Id of a journey. - * Zero represents an invalid id. - */ -typedef uint64_t JPS_JourneyId; - -/** - * Id of a stage. - * Zero represents an invalid id. - */ -typedef uint64_t JPS_StageId; - -/** - * Index of a stage within a journey. - * Note that stage ids are only unique within the journey they refer to. - */ -typedef size_t JPS_StageIndex; - -/** - * Id of an agent. - * Zero represents an invalid id. - */ -typedef uint64_t JPS_AgentId; - -/** - * Callback type for logging - */ -typedef void (*JPS_LoggingCallBack)(const char*, void*); - -/** - * Register callback to receive debug level log messages. - * To unregsiter a callback supply a NULL pointer as callback. - * The optional userdata parameter is stored (non-owning) and always passed to the callback. - * @param callback to call with debug message - * @param userdata optional pointer to state needed by the callback - */ -JUPEDSIM_API void JPS_Logging_SetDebugCallback(JPS_LoggingCallBack callback, void* userdata); - -/** - * Register callback to receive info level log messages. - * To unregsiter a callback supply a NULL pointer. - * The optional userdata parameter is stored (non-owning) and always passed to the callback. - * @param callback to call with debug message - * @param userdata optional pointer to state needed by the callback - */ -JUPEDSIM_API void JPS_Logging_SetInfoCallback(JPS_LoggingCallBack callback, void* userdata); - -/** - * Register callback to receive warning level log messages. - * To unregsiter a callback supply a NULL pointer. - * The optional userdata parameter is stored (non-owning) and always passed to the callback. - * @param callback to call with debug message - * @param userdata optional pointer to state needed by the callback - */ -JUPEDSIM_API void JPS_Logging_SetWarningCallback(JPS_LoggingCallBack callback, void* userdata); - -/** - * Register callback to receive error level log messages. - * To unregsiter a callback supply a NULL pointer. - * The optional userdata parameter is stored (non-owning) and always passed to the callback. - * @param callback to call with debug message - * @param userdata optional pointer to state needed by the callback - */ -JUPEDSIM_API void JPS_Logging_SetErrorCallback(JPS_LoggingCallBack callback, void* userdata); - -/** - * Opaque type for error messages. - * JPS_ErrorMessage will be returned via out parameters from functions that can fail. - * JPS_ErrorMessage have to be deallocated with JPS_ErrorMessage_Free. - */ -typedef struct JPS_ErrorMessage_t* JPS_ErrorMessage; - -/** - * Access c-string representation of the error message. - * Lifetime: the returned c-string will be valid until JPS_ErrorMessage_Free has been called on - * this message. - * @param handle to the JPS_ErrorMessage to inspect. - * @return Error message as null terminated c string. This pointer is valid until - * JPS_ErrorMessage_Free is called on this handle. - */ -JUPEDSIM_API const char* JPS_ErrorMessage_GetMessage(JPS_ErrorMessage handle); - -/** - * Frees a JPS_ErrorMessage - * @param handle to the JPS_ErrorMessage to free. - */ -JUPEDSIM_API void JPS_ErrorMessage_Free(JPS_ErrorMessage handle); - -/** - * Opaque type for operational models describing how agents interact in the simulation. - */ -typedef struct JPS_OperationalModel_t* JPS_OperationalModel; - -/** - * Frees a JPS_OperationalModel - * @param handle to the JPS_OperationalModel to free. - */ -JUPEDSIM_API void JPS_OperationalModel_Free(JPS_OperationalModel handle); - -/** - * Opaque type for a GeneralizedCentrifugalForceModel Model Builder - */ -typedef struct JPS_GeneralizedCentrifugalForceModelBuilder_t* - JPS_GeneralizedCentrifugalForceModelBuilder; - -/** - * Creates a GeneralizedCentrifugalForceModel model builder. - * @param strengthNeighborRepulsion Strength of the repulsion from neighbors - * @param strengthGeometryRepulsion Strength of the repulsion from geometry boundaries - * @param maxNeighborInteractionDistance cut-off-radius for ped-ped repulsion (r_c in FIG. 7) - * @param maxGeometryInteractionDistance cut-off-radius for ped-wall repulsion (r_c in FIG. 7) - * @param maxNeighborInterpolationDistance distance of interpolation of repulsive force for - * ped-ped interaction (r_eps in FIG. 7) - * @param maxGeometryInterpolationDistance distance of interpolation of repulsive force for - * ped-wall interaction (r_eps in FIG. 7) - * @param maxNeighborRepulsionForce maximum of the repulsion force for ped-ped interaction by - * contact of ellipses (f_m in FIG. 7) - * @param maxGeometryRepulsionForce maximum of the repulsion force for ped-wall interaction by - * contact of ellipses (f_m in FIG. 7) - * @return the GeneralizedCentrifugalForceModel model builder - */ -JUPEDSIM_API JPS_GeneralizedCentrifugalForceModelBuilder -JPS_GeneralizedCentrifugalForceModelBuilder_Create( - double strengthNeighborRepulsion, - double strengthGeometryRepulsion, - double maxNeighborInteractionDistance, - double maxGeometryInteractionDistance, - double maxNeighborInterpolationDistance, - double maxGeometryInterpolationDistance, - double maxNeighborRepulsionForce, - double maxGeometryRepulsionForce); - -/** - * Creates a JPS_OperationalModel of type GeneralizedCentrifugalForceModel Model from the - * JPS_GeneralizedCentrifugalForceModelBuilder. - * @param handle the builder to operate on - * @param[out] errorMessage if not NULL: will be set to a JPS_ErrorMessage in case of an error - * @return a JPS_GeneralizedCentrifugalForceModel or NULL if an error occured. - */ -JUPEDSIM_API JPS_OperationalModel JPS_GeneralizedCentrifugalForceModelBuilder_Build( - JPS_GeneralizedCentrifugalForceModelBuilder handle, - JPS_ErrorMessage* errorMessage); - -/** - * Frees a JPS_GeneralizedCentrifugalForceModelBuilder - * @param handle to the JPS_GeneralizedCentrifugalForceModelBuilder to free. - */ -JUPEDSIM_API void JPS_GeneralizedCentrifugalForceModelBuilder_Free( - JPS_GeneralizedCentrifugalForceModelBuilder handle); - -/** - * Opaque type for a Collision Free Speed Model Builder - */ -typedef struct JPS_CollisionFreeSpeedModelBuilder_t* JPS_CollisionFreeSpeedModelBuilder; - -/** - * Creates a Collision Free Speed Model builder. - * @param strength_neighbor_repulsion describes the strength with which neighbors repulse each - * other. - * @param range_neighbor_repulsion describes the range at hich neighbors repulse each other. - * @param strength_geometry_repulsion describes the strength with which neighbors are repules by - * geometry. - * @param range_geometry_repulsion describes the range at hich neighbors are repulsed by geometry. - * @return the builder - */ -JUPEDSIM_API JPS_CollisionFreeSpeedModelBuilder JPS_CollisionFreeSpeedModelBuilder_Create( - double strengthNeighborRepulsion, - double rangeNeighborRepulsion, - double strengthGeometryRepulsion, - double rangeGeometryRepulsion); - -/** - * Creates a JPS_OperationalModel of type Collision Free Speed Model from the - * JPS_GeneralizedCentrifugalForceModelBuilder. - * @param handle the builder to operate on - * @param[out] errorMessage if not NULL: will be set to a JPS_ErrorMessage in case of an error - * @return a JPS_GeneralizedCentrifugalForceModel or NULL if an error occured. - */ -JUPEDSIM_API JPS_OperationalModel JPS_CollisionFreeSpeedModelBuilder_Build( - JPS_CollisionFreeSpeedModelBuilder handle, - JPS_ErrorMessage* errorMessage); - -/** - * Frees a JPS_CollisionFreeSpeedModelBuilder - * @param handle to the JPS_CollisionFreeSpeedModelBuilder to free. - */ -JUPEDSIM_API void -JPS_CollisionFreeSpeedModelBuilder_Free(JPS_CollisionFreeSpeedModelBuilder handle); - -/** - * Opaque type that represents the geometry the simulation acts on. - * This type is created from JPS_GeometryBuilder. - */ -typedef struct JPS_Geometry_t const* JPS_Geometry; - -/** - * Returns the number of points that the outer boundary of the geometry consists of. - * @param handle to the JPS_Geometry to operate on - * @return Number of points in the outer boundary - */ -JUPEDSIM_API size_t JPS_Geometry_GetBoundarySize(JPS_Geometry handle); - -/** - * Returns the points the outer boundary consists of. - * @param handle to the JPS_Geometry to operate on - * @return the outer boundary. Do not free the returned pointer, it is still owned by JPS_Geometry. - */ -JUPEDSIM_API const JPS_Point* JPS_Geometry_GetBoundaryData(JPS_Geometry handle); - -/** - * Returns the number of holes in the geometry. - * @param handle to the JPS_Geometry to operate on - * @return Number of holes - */ -JUPEDSIM_API size_t JPS_Geometry_GetHoleCount(JPS_Geometry handle); - -/** - * Returns the number of points that hole consists of. - * @param handle to the JPS_Geometry to operate on - * @param index of the hole to access - * @param[out] errorMessage if not NULL: will be set to a JPS_ErrorMessage in case of an error - * @return Number of points in hole boundary or 0 on error. - */ -JUPEDSIM_API size_t -JPS_Geometry_GetHoleSize(JPS_Geometry handle, size_t hole_index, JPS_ErrorMessage* errorMessage); - -/** - * Returns the points the hole boundary consists of. - * @param handle to the JPS_Geometry to operate on - * @param index of the hole to access - * @param[out] errorMessage if not NULL: will be set to a JPS_ErrorMessage in case of an error - * @return points of the hole boundary or NULL on error. - */ -JUPEDSIM_API const JPS_Point* -JPS_Geometry_GetHoleData(JPS_Geometry handle, size_t hole_index, JPS_ErrorMessage* errorMessage); - -/** - * Frees a JPS_Geometry - * @param handle to the JPS_Geometry to free. - */ -JUPEDSIM_API void JPS_Geometry_Free(JPS_Geometry geometry); - -/** - * Opaque type that is used to build geometry for the simulation. - */ -typedef struct JPS_GeometryBuilder_t* JPS_GeometryBuilder; - -/** - * Creates a new GeometryBuilder - * @return the new builder. - */ -JUPEDSIM_API JPS_GeometryBuilder JPS_GeometryBuilder_Create(); - -/** - * Adds an accessible area to the geometry. - * The polygon described by points and pointCount must be a simple polygon. The polygon my be - * specified in clockwise or counter-clockwise order. - * Multiple accessible areas may overlap, the final accessible area is created by forming the union - * over all accessible areas. - * The Union over all accessible areas minus exclusions must form one polygon. - * @param polygon describing the accessible area. - * @param pointCount number of points the polygon consists of. - */ -JUPEDSIM_API void JPS_GeometryBuilder_AddAccessibleArea( - JPS_GeometryBuilder handle, - const JPS_Point* polygon, - size_t lenPolygon); - -/** - * Adds an accessible area to the geometry. - * The polygon described by points and pointCount must be a simple polygon. The polygon my be - * specified in clockwise or counter-clockwise order. - * Multiple exclusion areas may overlap, the final exclusion area is created by forming the union - * over all exclusion areas. - * Exclusion areas are subtracted from accessible areas. - * The Union over all accessible areas minus exclusions must form a single polygon. - * @param polygon describing the accessible area. - * @param pointCount number of points the polygon consists of. - */ -JUPEDSIM_API void JPS_GeometryBuilder_ExcludeFromAccessibleArea( - JPS_GeometryBuilder handle, - const JPS_Point* polygon, - size_t lenPolygon); - -/** - * Creates a JPS_Geometry from a JPS_GeometryBuilder. After this call the builder still has to be - * freed with JPS_GeometryBuilder_Free. - * @param handle to operate on. - * @param[out] errorMessage if not NULL: will be set to a JPS_ErrorMessage in case of an error. - * @return a JPS_Geometry handle on success or NULL on any error. - */ -JUPEDSIM_API JPS_Geometry -JPS_GeometryBuilder_Build(JPS_GeometryBuilder handle, JPS_ErrorMessage* errorMessage); - -/** - * Frees a JPS_GeometryBuilder. - * @param handle to the JPS_GeometryBuilder to free. - */ -JUPEDSIM_API void JPS_GeometryBuilder_Free(JPS_GeometryBuilder handle); - -/** - * Opaque type for transition, describing route based decisions. - */ -typedef struct JPS_Transition_t* JPS_Transition; - -/** - * Create a fixed transition to stage - * @param[out] errorMessage if not NULL: will be set to a JPS_ErrorMessage in case of an error. - * @return Fixed transition to stage or NULL on any error. - */ -JUPEDSIM_API JPS_Transition -JPS_Transition_CreateFixedTransition(JPS_StageId stageId, JPS_ErrorMessage* errorMessage); - -/** - * Create a round robin transition to stages - * @param stages target stages - * @param weights weights of target stage - * @param len length of stages and weights - * @param[out] errorMessage if not NULL: will be set to a JPS_ErrorMessage in case of an error. - * @return Round robin transition to stages or NULL on any error. - */ -JUPEDSIM_API JPS_Transition JPS_Transition_CreateRoundRobinTransition( - JPS_StageId* stages, - uint64_t* weights, - size_t len_stages, - JPS_ErrorMessage* errorMessage); - -/** - * Create a least targeted transition to stages - * @param stages target stages - * @param len length of stages - * @param[out] errorMessage if not NULL: will be set to a JPS_ErrorMessage in case of an error. - * @return least targeted transition to stages or NULL on any error. - */ -JUPEDSIM_API JPS_Transition JPS_Transition_CreateLeastTargetedTransition( - JPS_StageId* stages, - size_t len_stages, - JPS_ErrorMessage* errorMessage); - -/** - * Frees a JPS_Transition - * @param handle to the JPS_Transition to free. - */ -JUPEDSIM_API void JPS_Transition_Free(JPS_Transition handle); - -/** - * Opaque type that describes a journey - */ -typedef struct JPS_Journey_t* JPS_JourneyDescription; - -/** - * Creates an empty journey. - */ -JUPEDSIM_API JPS_JourneyDescription JPS_JourneyDescription_Create(); - -/** - * Extends the journey with the stage specified by its ID. - * @param handle of the JourneyDescription to extend. - * @param id of the stage to extend the journey with. - */ -JUPEDSIM_API void JPS_JourneyDescription_AddStage(JPS_JourneyDescription handle, JPS_StageId id); - -/** - * Specifies the transition to the next stage, once this stage is completed. - * @param handle of the JourneyDescription to modify. - * @param id of the stage to set the transition for. - * @param transition transition to the next stage. - * @param[out] errorMessage if not NULL: will be set to a JPS_ErrorMessage in case of an error. - * @return true if the transition for the stage could be added otherwise false - */ -JUPEDSIM_API bool JPS_JourneyDescription_SetTransitionForStage( - JPS_JourneyDescription handle, - JPS_StageId id, - JPS_Transition transition, - JPS_ErrorMessage* errorMessage); - -/** - * Frees a JPS_Journey. - * @param handle to the JPS_Journey to free. - */ -JUPEDSIM_API void JPS_JourneyDescription_Free(JPS_JourneyDescription handle); - -/** - * Opaque type of Generalized Centrifugal Force model state - */ -typedef struct JPS_GeneralizedCentrifugalForceModelState_t* - JPS_GeneralizedCentrifugalForceModelState; - -/** - * Read speed of this agent. - * @param handle of the Agent to access. - * @return speed in m/s - */ -JUPEDSIM_API double JPS_GeneralizedCentrifugalForceModelState_GetSpeed( - JPS_GeneralizedCentrifugalForceModelState handle); - -/** - * Write speed of this agent. - * @param handle of the Agent to access. - * @param speed in m/s - */ -JUPEDSIM_API void JPS_GeneralizedCentrifugalForceModelState_SetSpeed( - JPS_GeneralizedCentrifugalForceModelState handle, - double speed); - -/** - * Read desired orientation of this agent. - * @param handle of the Agent to access. - * @return 2D orientation vector - */ -JUPEDSIM_API JPS_Point -JPS_GeneralizedCentrifugalForceModelState_GetE0(JPS_GeneralizedCentrifugalForceModelState handle); - -/** - * Write desired direction of this agent. - * @param handle of the Agent to access. - * @param e0 desired orientation of this agent. - */ -JUPEDSIM_API void JPS_GeneralizedCentrifugalForceModelState_SetE0( - JPS_GeneralizedCentrifugalForceModelState handle, - JPS_Point e0); - -/** - * Read mass in Kg of this agent. - * @param handle of the Agent to access. - * @return mass (Kg) of this agent - */ -JUPEDSIM_API double -JPS_GeneralizedCentrifugalForceModelState_GetMass(JPS_GeneralizedCentrifugalForceModelState handle); - -/** - * Write mass in Kg of this agent. - * @param handle of the Agent to access. - * @param mass (Kg) of this agent. - */ -JUPEDSIM_API void JPS_GeneralizedCentrifugalForceModelState_SetMass( - JPS_GeneralizedCentrifugalForceModelState handle, - double mass); - -/** - * Read tau of this agent. - * @param handle of the Agent to access. - * @return tau of this agent - */ -JUPEDSIM_API double -JPS_GeneralizedCentrifugalForceModelState_GetTau(JPS_GeneralizedCentrifugalForceModelState handle); - -/** - * Write tau of this agent. - * @param handle of the Agent to access. - * @param tau of this agent. - */ -JUPEDSIM_API void JPS_GeneralizedCentrifugalForceModelState_SetTau( - JPS_GeneralizedCentrifugalForceModelState handle, - double tau); - -/** - * Read v0 of this agent. - * @param handle of the Agent to access. - * @return v0 of this agent - */ -JUPEDSIM_API double -JPS_GeneralizedCentrifugalForceModelState_GetV0(JPS_GeneralizedCentrifugalForceModelState handle); - -/** - * Write v0 of this agent. - * @param handle of the Agent to access. - * @param v0 of this agent. - */ -JUPEDSIM_API void JPS_GeneralizedCentrifugalForceModelState_SetV0( - JPS_GeneralizedCentrifugalForceModelState handle, - double v0); - -/** - * Read a_v of this agent. - * @param handle of the Agent to access. - * @return AV of this agent - */ -JUPEDSIM_API double -JPS_GeneralizedCentrifugalForceModelState_GetAV(JPS_GeneralizedCentrifugalForceModelState handle); - -/** - * Write a_v of this agent. - * @param handle of the Agent to access. - * @param a_v of this agent. - */ -JUPEDSIM_API void JPS_GeneralizedCentrifugalForceModelState_SetAV( - JPS_GeneralizedCentrifugalForceModelState handle, - double a_v); - -/** - * Read a_min of this agent. - * @param handle of the Agent to access. - * @return a_min of this agent - */ -JUPEDSIM_API double -JPS_GeneralizedCentrifugalForceModelState_GetAMin(JPS_GeneralizedCentrifugalForceModelState handle); - -/** - * Write a_min of this agent. - * @param handle of the Agent to access. - * @param a_min of this agent. - */ -JUPEDSIM_API void JPS_GeneralizedCentrifugalForceModelState_SetAMin( - JPS_GeneralizedCentrifugalForceModelState handle, - double a_min); - -/** - * Read b_min of this agent. - * @param handle of the Agent to access. - * @return b_min of this agent - */ -JUPEDSIM_API double -JPS_GeneralizedCentrifugalForceModelState_GetBMin(JPS_GeneralizedCentrifugalForceModelState handle); - -/** - * Write b_min of this agent. - * @param handle of the Agent to access. - * @param b_min of this agent. - */ -JUPEDSIM_API void JPS_GeneralizedCentrifugalForceModelState_SetBMin( - JPS_GeneralizedCentrifugalForceModelState handle, - double b_min); - -/** - * Read b_max of this agent. - * @param handle of the Agent to access. - * @return b_max of this agent - */ -JUPEDSIM_API double -JPS_GeneralizedCentrifugalForceModelState_GetBMax(JPS_GeneralizedCentrifugalForceModelState handle); - -/** - * Write b_max of this agent. - * @param handle of the Agent to access. - * @param a_min of this agent. - */ -JUPEDSIM_API void JPS_GeneralizedCentrifugalForceModelState_SetBMax( - JPS_GeneralizedCentrifugalForceModelState handle, - double b_max); - -/** - * Opaque type of Collision Free Speed model state - */ -typedef struct JPS_CollisionFreeSpeedModelState_t* JPS_CollisionFreeSpeedModelState; - -/** - * Read e0 of this agent. - * @param handle of the Agent to access. - * @return e0 of this agent - */ -JUPEDSIM_API JPS_Point -JPS_CollisionFreeSpeedModelState_GetE0(JPS_CollisionFreeSpeedModelState handle); - -/** - * Write e0 of this agent. - * @param handle of the Agent to access. - * @param e0 of this agent. - */ -JUPEDSIM_API void -JPS_CollisionFreeSpeedModelState_SetE0(JPS_CollisionFreeSpeedModelState handle, JPS_Point e0); - -/** - * Read time gap of this agent. - * @param handle of the Agent to access. - * @return time gap of this agent - */ -JUPEDSIM_API double -JPS_CollisionFreeSpeedModelState_GetTimeGap(JPS_CollisionFreeSpeedModelState handle); - -/** - * Write time gap of this agent. - * @param handle of the Agent to access. - * @param time_gap of this agent. - */ -JUPEDSIM_API void JPS_CollisionFreeSpeedModelState_SetTimeGap( - JPS_CollisionFreeSpeedModelState handle, - double time_gap); - -/** - * Read tau of this agent. - * @param handle of the Agent to access. - * @return tau of this agent - */ -JUPEDSIM_API double -JPS_CollisionFreeSpeedModelState_GetTau(JPS_CollisionFreeSpeedModelState handle); - -/** - * Write tau of this agent. - * @param handle of the Agent to access. - * @param tau of this agent. - */ -JUPEDSIM_API void -JPS_CollisionFreeSpeedModelState_SetTau(JPS_CollisionFreeSpeedModelState handle, double tau); - -/** - * Read v0 of this agent. - * @param handle of the Agent to access. - * @return v0 of this agent - */ -JUPEDSIM_API double JPS_CollisionFreeSpeedModelState_GetV0(JPS_CollisionFreeSpeedModelState handle); - -/** - * Write v0 of this agent. - * @param handle of the Agent to access. - * @param v0 of this agent. - */ -JUPEDSIM_API void -JPS_CollisionFreeSpeedModelState_SetV0(JPS_CollisionFreeSpeedModelState handle, double v0); - -/** - * Read radius of this agent. - * @param handle of the Agent to access. - * @return radius of this agent - */ -JUPEDSIM_API double -JPS_CollisionFreeSpeedModelState_GetRadius(JPS_CollisionFreeSpeedModelState handle); - -/** - * Write radius of this agent in meters. - * @param handle of the Agent to access. - * @param radius (m) of this agent. - */ -JUPEDSIM_API void -JPS_CollisionFreeSpeedModelState_SetRadius(JPS_CollisionFreeSpeedModelState handle, double radius); - -/** - * Identifies the type of stage - */ -enum JPS_StageType { - JPS_NotifiableQueueType, - JPS_WaitingSetType, - JPS_WaypointType, - JPS_ExitType, - JPS_DirectSteeringType -}; - -/** - * Opaque type of an NotifiableQueueProxy - */ -typedef struct JPS_NotifiableQueueProxy_t* JPS_NotifiableQueueProxy; - -JUPEDSIM_API size_t JPS_NotifiableQueueProxy_GetCountTargeting(JPS_NotifiableQueueProxy handle); - -JUPEDSIM_API size_t JPS_NotifiableQueueProxy_GetCountEnqueued(JPS_NotifiableQueueProxy handle); - -JUPEDSIM_API void JPS_NotifiableQueueProxy_Pop(JPS_NotifiableQueueProxy handle, size_t count); - -JUPEDSIM_API size_t -JPS_NotifiableQueueProxy_GetEnqueued(JPS_NotifiableQueueProxy handle, const JPS_AgentId** data); - -JUPEDSIM_API void JPS_NotifiableQueueProxy_Free(JPS_NotifiableQueueProxy handle); - -enum JPS_WaitingSetState { JPS_WaitingSet_Active, JPS_WaitingSet_Inactive }; -typedef struct JPS_WaitingSetProxy_t* JPS_WaitingSetProxy; - -JUPEDSIM_API size_t JPS_WaitingSetProxy_GetCountTargeting(JPS_WaitingSetProxy handle); - -JUPEDSIM_API size_t JPS_WaitingSetProxy_GetCountWaiting(JPS_WaitingSetProxy handle); - -JUPEDSIM_API size_t -JPS_WaitingSetProxy_GetWaiting(JPS_WaitingSetProxy handle, const JPS_AgentId** data); - -JUPEDSIM_API void -JPS_WaitingSetProxy_SetWaitingSetState(JPS_WaitingSetProxy handle, JPS_WaitingSetState newState); - -JUPEDSIM_API JPS_WaitingSetState JPS_WaitingSetProxy_GetWaitingSetState(JPS_WaitingSetProxy handle); - -JUPEDSIM_API void JPS_WaitingSetProxy_Free(JPS_WaitingSetProxy handle); - -typedef struct JPS_WaypointProxy_t* JPS_WaypointProxy; - -JUPEDSIM_API size_t JPS_WaypointProxy_GetCountTargeting(JPS_WaypointProxy handle); - -JUPEDSIM_API void JPS_WaypointProxy_Free(JPS_WaypointProxy handle); - -typedef struct JPS_ExitProxy_t* JPS_ExitProxy; - -JUPEDSIM_API size_t JPS_ExitProxy_GetCountTargeting(JPS_ExitProxy handle); - -JUPEDSIM_API void JPS_ExitProxy_Free(JPS_ExitProxy handle); - -typedef struct JPS_DirectSteeringProxy_t* JPS_DirectSteeringProxy; - -JUPEDSIM_API void JPS_DirectSteeringProxy_Free(JPS_DirectSteeringProxy handle); - -/** - * Opaque type of an agent - */ -typedef struct JPS_Agent_t* JPS_Agent; - -/** - * Access the agents id. - * @param handle of the agent to access. - * @return the id - */ -JUPEDSIM_API JPS_AgentId JPS_Agent_GetId(JPS_Agent handle); - -/** - * Access the agents journey id. - * @param handle of the agent to access. - * @return the id of this agents journey - */ -JUPEDSIM_API JPS_JourneyId JPS_Agent_GetJourneyId(JPS_Agent handle); - -/** - * Access the agents currently targeted stage id. - * @param handle of the agent to access. - * @return the id of the stage currently targeted - */ -JUPEDSIM_API JPS_StageId JPS_Agent_GetStageId(JPS_Agent handle); - -/** - * Access the agents position. - * @param handle of the agent to access. - * @return position - */ -JUPEDSIM_API JPS_Point JPS_Agent_GetPosition(JPS_Agent handle); - -/** - * Access the agents current target. - * @param handle of the agent to access. - * @return target of the agent - */ -JUPEDSIM_API JPS_Point JPS_Agent_GetTarget(JPS_Agent handle); - -JUPEDSIM_API bool -JPS_Agent_SetTarget(JPS_Agent handle, JPS_Point target, JPS_ErrorMessage* errorMessage); - -/** - * Access the agents orientation. - * The orientation is unit length. - * @param handle of the agent to access. - * @return the orientation - */ -JUPEDSIM_API JPS_Point JPS_Agent_GetOrientation(JPS_Agent handle); - -/** - * Access the agets model type information. - * @param handle of the agent to access. - * @return type of model in use - */ -JUPEDSIM_API JPS_ModelType JPS_Agent_GetModelType(JPS_Agent handle); - -/** - * Access Generalized Centrifugal Force model state. - * Precondition: Agent needs to use Generalized Centrifugal Force model - * @param handle of the agent to access. - * @param[out] errorMessage if not NULL: will be set to a JPS_ErrorMessage in case of an error. - * @return state or NULL on error - */ -JUPEDSIM_API JPS_GeneralizedCentrifugalForceModelState -JPS_Agent_GetGeneralizedCentrifugalForceModelState( - JPS_Agent handle, - JPS_ErrorMessage* errorMessage); - -/** - * Access Collision Free Speed model state. - * Precondition: Agent needs to use Collision Free Speed model - * @param handle of the agent to access. - * @param[out] errorMessage if not NULL: will be set to a JPS_ErrorMessage in case of an error. - * @return state or NULL on error - */ -JUPEDSIM_API JPS_CollisionFreeSpeedModelState -JPS_Agent_GetCollisionFreeSpeedModelState(JPS_Agent handle, JPS_ErrorMessage* errorMessage); - -/** - * Opaque type of an iterator over agents - */ -typedef struct JPS_AgentIterator_t* JPS_AgentIterator; - -/** - * Access the next element in the iterator. - * Calling JPS_AgentIterator_Next repeatedly on a finished iterator is save. - * @param handle of the iterator to advance and access - * @return an agent or NULL if the iterator is at the end. The pointer returned does not need to be - * freed and is invalidated on the next call to this function! - */ -JUPEDSIM_API JPS_Agent JPS_AgentIterator_Next(JPS_AgentIterator handle); - -/** - * Free the iterator. - * @param handle to the JPS_AgentIterator to free. - */ -JUPEDSIM_API void JPS_AgentIterator_Free(JPS_AgentIterator handle); - -/** - * Describes parameters of an Agent in GeneralizedCentrifugalForceModel - */ -typedef struct JPS_GeneralizedCentrifugalForceModelAgentParameters { - /** - * Initial speed of the Agent - */ - double speed = 0.0; - /** - * Desired orientation. - */ - JPS_Point e0{0, 0}; - /** - * Position of the agent. - * The position needs to inside the accessible area. - */ - JPS_Point position{0, 0}; - /* - * Orientation vector of the agent. - * The orientation vector will internally be normalized. - */ - JPS_Point orientation{0, 0}; - /** - * Defines the journey this agent will take use - */ - JPS_JourneyId journeyId = 0; - /** - * Defines the current stage of its journey - */ - JPS_StageId stageId = 0; - /** - * Mass of the agent - */ - double mass = 1; - /** - * Tau of the agent - */ - double tau = 0.5; - /** - * V0 of the agent - */ - double v0 = 1.2; - /** - * a_v sagital axis stretch factor - */ - double a_v = 1.; - /** - * b_min minimum length of transversal axis in 'meters' - */ - double a_min = 0.2; - /** - * a_min minimum length of sagital axis in 'meters' - */ - double b_min = 0.2; - /** - * b_max maximum length of transversal axis in 'meters' - */ - double b_max = 0.4; -} JPS_GeneralizedCentrifugalForceModelAgentParameters; - -/** - * Describes parameters of an Agent in GeneralizedCentrifugalForceModel - */ -typedef struct JPS_CollisionFreeSpeedModelAgentParameters { - /** - * Position of the agent. - * The position needs to inside the accessible area. - */ - JPS_Point position{0, 0}; - /** - * Defines the journey this agent will take use - */ - JPS_JourneyId journeyId = 0; - /** - * Defines the current stage of its journey - */ - JPS_StageId stageId = 0; - /** - * @param time_gap of the agents using this profile (T in the OV-function) - */ - double time_gap = 1.; - /** - *@param v0 of the agents using this profile(desired speed) double radius; - */ - double v0 = 1.2; - /** - *@param radius of the agent in 'meters' - */ - double radius = 0.2; -} JPS_CollisionFreeSpeedModelAgentParameters; - -/** - * Opaque type of an iterator over agent ids - */ -typedef struct JPS_AgentIdIterator_t* JPS_AgentIdIterator; - -/** - * Access the next element in the iterator. - * Calling JPS_AgentIterator_Next repeatedly on a finished iterator is save. - * @param handle of the iterator to advance and access - * @return an agentId, Zero in case the iterator has reachedits end. - */ -JUPEDSIM_API JPS_AgentId JPS_AgentIdIterator_Next(JPS_AgentIdIterator handle); - -/** - * Free the iterator. - * @param handle to the JPS_AgentIterator to free. - */ -JUPEDSIM_API void JPS_AgentIdIterator_Free(JPS_AgentIdIterator handle); - -/* - * Opaque type to a Simulator object. - */ -typedef struct JPS_Simulation_t* JPS_Simulation; - -/* - * Creates a new JPS_Simulation object. - * NOTE: JPS_Simulation_Create will take ownership of all indicated parameters even in case an error - * occured. - * @param model to use. Will copy 'model', 'model' can be freed after this call or reused for - * another simulation. - * @param geometry to use. Will copy 'geometry', 'geometry' can be freed after this call or reused - * for another simulation. - * @param dT simulation timestep in seconds - * @param[out] errorMessage if not NULL: will be set to a JPS_ErrorMessage in case of an error. - * @return the Simulation - */ -JUPEDSIM_API JPS_Simulation JPS_Simulation_Create( - JPS_OperationalModel model, - JPS_Geometry geometry, - double dT, - JPS_ErrorMessage* errorMessage); - -/** - * Populates the Simulation with a Journey for agents to use. - * @param handle to the simulation to act on - * @param journey to add. Will copy 'journey', 'journey' needs to be freed after this call. - * @param[out] errorMessage if not NULL: will be set to a JPS_ErrorMessage in case of an error. - * @return Id of the journey - */ -JUPEDSIM_API JPS_JourneyId JPS_Simulation_AddJourney( - JPS_Simulation handle, - JPS_JourneyDescription journey, - JPS_ErrorMessage* errorMessage); - -/** - * Adds a new waypoint stage to the simulation. - * @param handle to the simulation to act on - * @param position of the waypoiny - * @parma required distance to check if the waypoint was reached - * @param[out] errorMessage if not NULL: will be set to a JPS_ErrorMessage in case of an error. - * @return Id of the stage - */ -JUPEDSIM_API JPS_StageId JPS_Simulation_AddStageWaypoint( - JPS_Simulation handle, - JPS_Point position, - double distance, - JPS_ErrorMessage* errorMessage); - -/** - * Adds a new exit stage to the simulation. - * @param handle to the simulation to act on - * @param polygon describing the exit - * @param number of points in the polygon - * @param[out] errorMessage if not NULL: will be set to a JPS_ErrorMessage in case of an error. - * @return Id of the stage - */ -JUPEDSIM_API JPS_StageId JPS_Simulation_AddStageExit( - JPS_Simulation handle, - const JPS_Point* polygon, - size_t len_polygon, - JPS_ErrorMessage* errorMessage); - -/** - * Adds a new notifiable queue stage to the simulation. - * The waiting positions supplied are the queue positions. The first point - * defines the head of the queue and the last point defines the tail of the - * queue. When there are more agents waiting in the queue than there are waiting - * slots the overflowing agents will all try to wait at the tail end of the - * queue. - * - * Agents can be popped from the queue by calling - * 'JPS_Simulation_PopAgentsFromQueue' - * - * @param handle to the simulation to act on - * @param waiting_positions of this queue - * @param number of waiting positions - * @param[out] errorMessage if not NULL: will be set to a JPS_ErrorMessage in case of an error. - * @return Id of the stage - */ -JUPEDSIM_API JPS_StageId JPS_Simulation_AddStageNotifiableQueue( - JPS_Simulation handle, - const JPS_Point* waiting_positions, - size_t len_waiting_positions, - JPS_ErrorMessage* errorMessage); - -/** - * Adds a new notifiable waiting set to the simulation. - * The waiting positions supplied are filled in order. - * - * A waiting set can be set active or inactive with - * `JPS_Simulation_ChangeWaitingSetState` When active agents will wait in the - * waiting set until the stage is set inactive. When inactive the stage - * functions as a waypoint at the first position supplied. - * - * @param handle to the simulation to act on - * @param waiting_positions of this waiting set - * @param number of waiting positions - * @param[out] errorMessage if not NULL: will be set to a JPS_ErrorMessage in case of an error. - * @return Id of the stage - */ -JUPEDSIM_API JPS_StageId JPS_Simulation_AddStageWaitingSet( - JPS_Simulation handle, - const JPS_Point* waiting_positions, - size_t len_waiting_positions, - JPS_ErrorMessage* errorMessage); - -/** - * Adds a new direct steering stage to the simulation. - * - * This allows a direct control of the target of an agent. - * - * Important: A direct steering stage can only be used if it is the only stage in a Journey. - * - * @param handle to the simulation to act on - * @param[out] errorMessage if not NULL: will be set to a JPS_ErrorMessage in case of an error. - * @return Id of the stage - */ -JUPEDSIM_API JPS_StageId -JPS_Simulation_AddStageDirectSteering(JPS_Simulation handle, JPS_ErrorMessage* errorMessage); - -/** - * Adds a new agent to the simulation. - * This can be called at any time, i.e. agents can be added at any iteration. - * NOTE: Currently there is no checking done to ensure the agent can be placed at the desired - * location. - * @param handle to the simulation to act on - * @param parameters describing the new agent. - * @param[out] errorMessage if not NULL. Will contain address of JPS_ErrorMessage in case of an - * error. - * @return id of the new agent or 0 if the agent could not be added due to an error. - */ -JUPEDSIM_API JPS_AgentId JPS_Simulation_AddGeneralizedCentrifugalForceModelAgent( - JPS_Simulation handle, - JPS_GeneralizedCentrifugalForceModelAgentParameters parameters, - JPS_ErrorMessage* errorMessage); - -/** - * Adds a new agent to the simulation. - * This can be called at any time, i.e. agents can be added at any iteration. - * NOTE: Currently there is no checking done to ensure the agent can be placed at the desired - * location. - * @param handle to the simulation to act on - * @param parameters describing the new agent. - * @param[out] errorMessage if not NULL. Will contain address of JPS_ErrorMessage in case of an - * error. - * @return id of the new agent or 0 if the agent could not be added due to an error. - */ -JUPEDSIM_API JPS_AgentId JPS_Simulation_AddCollisionFreeSpeedModelAgent( - JPS_Simulation handle, - JPS_CollisionFreeSpeedModelAgentParameters parameters, - JPS_ErrorMessage* errorMessage); - -/** - * Marks an agent from the simuation for removal. - * The agent will be removed at the start of the next simulation iteration, before the interaction - * are computed. - * @param handle to the simulation to act on - * @param agentID to remove - * @param[out] errorMessage if not NULL: will be set to a JPS_ErrorMessage in case of an error. - * @return bool true if the agent existed and was marked for removal otherwise false - */ -JUPEDSIM_API bool JPS_Simulation_MarkAgentForRemoval( - JPS_Simulation handle, - JPS_AgentId agentId, - JPS_ErrorMessage* errorMessage); - -/* - * Returns the ids of all agents that exited the simulation in the last iteration. - * @param handle of the Simulation - * @param[out] data pointer to the ids - * @return numer of agents that have been removed in the last iteration - */ -JUPEDSIM_API size_t JPS_Simulation_RemovedAgents(JPS_Simulation handle, const JPS_AgentId** data); - -/* - * Advances the simulation by one step. - * @param handle of the Simulation - * @param[out] errorMessage if not NULL: will be set to a JPS_ErrorMessage in case of an error. - * @return true if no errors occured - */ -JUPEDSIM_API bool JPS_Simulation_Iterate(JPS_Simulation handle, JPS_ErrorMessage* errorMessage); - -/** - * How many agents are in the simulation. - * @param handle of the simulation - * @return count agents in the simulation - */ -JUPEDSIM_API size_t JPS_Simulation_AgentCount(JPS_Simulation handle); - -/** - * Returns time progressed in the simulation. - * @param handle of the simulation - * @return seconds elapsed - */ -JUPEDSIM_API double JPS_Simulation_ElapsedTime(JPS_Simulation handle); - -/** - * Returns time simulad per iteration call. - * @param handle of the simulation - * @return time simulated per iteration call in seconds - */ -JUPEDSIM_API double JPS_Simulation_DeltaTime(JPS_Simulation handle); - -/** - * Returns how many iterations have been simulated. - * @param handle of the simulation - * @return count of elapsed iterations - */ -JUPEDSIM_API uint64_t JPS_Simulation_IterationCount(JPS_Simulation handle); - -/** - * Returns an iterator over all agents in the simulation. - * Notes: - * The iterator will be invalidated once JPS_Simulation_Iterate is called. - * The iterator needs to be freed after use. - * @param handle of the simulation - * @return iterator over agents in the simulation - */ -JUPEDSIM_API JPS_AgentIterator JPS_Simulation_AgentIterator(JPS_Simulation handle); - -/** - * Returns a specific agent of the simulation. - * @param handle of the simulation - * @param agentId Id of the agent to get - * @return Agent with given Id - */ -JUPEDSIM_API JPS_Agent -JPS_Simulation_GetAgent(JPS_Simulation handle, JPS_AgentId agentId, JPS_ErrorMessage* errorMessage); - -/** - * Switches the journey and currently selected stage of this agent - * @param handle of the Simulation to operate on - * @param agentId id of the agent to modify - * @param journeyId of the journey to select - * @param stageId of the stage to select - * @param[out] errorMessage if not NULL: will be set to a JPS_ErrorMessage in case of an error. - * @return true on success, false on any error, e.g. unknown agent id or profile id - */ -JUPEDSIM_API bool JPS_Simulation_SwitchAgentJourney( - JPS_Simulation handle, - JPS_AgentId agentId, - JPS_JourneyId journeyId, - JPS_StageId stageId, - JPS_ErrorMessage* errorMessage); - -/** - * Query the pedestrian model used by this simulation. - * @return the type of pedestrian model used in this simulation instance. - */ -JUPEDSIM_API JPS_ModelType JPS_Simulation_ModelType(JPS_Simulation handle); - -/** - * Query the simulation for all agent ids in distance to this point; - * @param handle of the Simulation to operate on - * @param position to query - * @param distance around position - * @return iterator containing all agent ids in range - */ -JUPEDSIM_API JPS_AgentIdIterator -JPS_Simulation_AgentsInRange(JPS_Simulation handle, JPS_Point position, double distance); - -/** - * Query the simulation for all agent ids in the supplied polygon. - * @param handle of the Simulation to operate on - * @param polygon array of JPS_Points (CCW ordered convex polygon) - * @param len_polygon number of points in the polygon - * @return iterator over the Ids inside the polygon - */ -JUPEDSIM_API JPS_AgentIdIterator -JPS_Simulation_AgentsInPolygon(JPS_Simulation handle, const JPS_Point* polygon, size_t len_polygon); - -JUPEDSIM_API JPS_StageType JPS_Simulation_GetStageType(JPS_Simulation handle, JPS_StageId id); - -JUPEDSIM_API JPS_NotifiableQueueProxy JPS_Simulation_GetNotifiableQueueProxy( - JPS_Simulation handle, - JPS_StageId stageId, - JPS_ErrorMessage* errorMessage); - -JUPEDSIM_API JPS_WaitingSetProxy JPS_Simulation_GetWaitingSetProxy( - JPS_Simulation handle, - JPS_StageId stageId, - JPS_ErrorMessage* errorMessage); - -JUPEDSIM_API JPS_WaypointProxy JPS_Simulation_GetWaypointProxy( - JPS_Simulation handle, - JPS_StageId stageId, - JPS_ErrorMessage* errorMessage); - -JUPEDSIM_API JPS_ExitProxy JPS_Simulation_GetExitProxy( - JPS_Simulation handle, - JPS_StageId stageId, - JPS_ErrorMessage* errorMessage); - -JUPEDSIM_API JPS_DirectSteeringProxy JPS_Simulation_GetDirectSteeringProxy( - JPS_Simulation handle, - JPS_StageId stageId, - JPS_ErrorMessage* errorMessage); - -/** - * Enable / disable collection of performance data. - * @param handle of the Simulation to operate on - * @param status new status to set - */ -JUPEDSIM_API void JPS_Simulation_SetTracing(JPS_Simulation handle, bool status); - -/** - * Read trace data from alst iteration. If tracing is disable all timings will be zero. - * @param handle of the Simulation to operate on - * @return trace data - */ -JUPEDSIM_API JPS_Trace JPS_Simulation_GetTrace(JPS_Simulation handle); - -/** - * Gain read access to the geometry used by this simulation. - * @param handle of the Simulation to operate on - * @return the geometry. Do not call JPS_Geometry_Free on this handle, - * the geometry is still owned by the simulation! - */ -JUPEDSIM_API JPS_Geometry JPS_Simulation_GetGeometry(JPS_Simulation handle); - -/** - * Frees a JPS_Simulation. - * @param handle to the JPS_Simulation to free. - */ -JUPEDSIM_API void JPS_Simulation_Free(JPS_Simulation handle); - -struct JPS_Path { - size_t len; - const JPS_Point* points; -}; - -JUPEDSIM_API void JPS_Path_Free(JPS_Path* path); - -struct JPS_Triangle { - JPS_Point points[3]; -}; - -struct JPS_TriangleMesh { - size_t len; - JPS_Triangle* triangles; -}; - -JUPEDSIM_API void JPS_TriangleMesh_Free(JPS_TriangleMesh* mesh); - -struct JPS_Line { - JPS_Point points[2]; -}; - -struct JPS_Lines { - size_t len; - JPS_Line* lines; -}; - -JUPEDSIM_API void JPS_Lines_Free(JPS_Lines* lines); - -/** - * WARNING this is currently a NavMeshRoutingEngine! This does not account possible other types of - * routing engines - */ -typedef struct JPS_RoutingEngine_t* JPS_RoutingEngine; - -JUPEDSIM_API JPS_RoutingEngine JPS_RoutingEngine_Create(JPS_Geometry geometry); - -JUPEDSIM_API JPS_Path -JPS_RoutingEngine_ComputeWaypoint(JPS_RoutingEngine handle, JPS_Point from, JPS_Point to); - -JUPEDSIM_API bool JPS_RoutingEngine_IsRoutable(JPS_RoutingEngine handle, JPS_Point p); - -JUPEDSIM_API JPS_TriangleMesh JPS_RoutingEngine_Mesh(JPS_RoutingEngine handle); - -/// Note: Currently the ID is the index of the vertex from 'JPS_RoutingEngine_Mesh' -JUPEDSIM_API JPS_Lines JPS_RoutingEngine_EdgesFor(JPS_RoutingEngine handle, uint32_t id); - -JUPEDSIM_API void JPS_RoutingEngine_Free(JPS_RoutingEngine handle); - -#ifdef __cplusplus -} -#endif +#include "agent.h" +#include "build_info.h" +#include "collision_free_speed_model.h" +#include "error.h" +#include "export.h" +#include "generalized_centrifugal_force_model.h" +#include "geometry.h" +#include "journey.h" +#include "logging.h" +#include "operational_model.h" +#include "routing.h" +#include "simulation.h" +#include "stage.h" +#include "transition.h" +#include "types.h" diff --git a/libjupedsim/include/jupedsim/logging.h b/libjupedsim/include/jupedsim/logging.h new file mode 100644 index 0000000000..16dddc75d4 --- /dev/null +++ b/libjupedsim/include/jupedsim/logging.h @@ -0,0 +1,53 @@ +/* Copyright © 2012-2023 Forschungszentrum Jülich GmbH */ +/* SPDX-License-Identifier: LGPL-3.0-or-later */ +#pragma once + +#include "export.h" + +#ifdef __cplusplus +extern "C" { +#endif +/** + * Callback type for logging + */ +typedef void (*JPS_LoggingCallBack)(const char*, void*); + +/** + * Register callback to receive debug level log messages. + * To unregsiter a callback supply a NULL pointer as callback. + * The optional userdata parameter is stored (non-owning) and always passed to the callback. + * @param callback to call with debug message + * @param userdata optional pointer to state needed by the callback + */ +JUPEDSIM_API void JPS_Logging_SetDebugCallback(JPS_LoggingCallBack callback, void* userdata); + +/** + * Register callback to receive info level log messages. + * To unregsiter a callback supply a NULL pointer. + * The optional userdata parameter is stored (non-owning) and always passed to the callback. + * @param callback to call with debug message + * @param userdata optional pointer to state needed by the callback + */ +JUPEDSIM_API void JPS_Logging_SetInfoCallback(JPS_LoggingCallBack callback, void* userdata); + +/** + * Register callback to receive warning level log messages. + * To unregsiter a callback supply a NULL pointer. + * The optional userdata parameter is stored (non-owning) and always passed to the callback. + * @param callback to call with debug message + * @param userdata optional pointer to state needed by the callback + */ +JUPEDSIM_API void JPS_Logging_SetWarningCallback(JPS_LoggingCallBack callback, void* userdata); + +/** + * Register callback to receive error level log messages. + * To unregsiter a callback supply a NULL pointer. + * The optional userdata parameter is stored (non-owning) and always passed to the callback. + * @param callback to call with debug message + * @param userdata optional pointer to state needed by the callback + */ +JUPEDSIM_API void JPS_Logging_SetErrorCallback(JPS_LoggingCallBack callback, void* userdata); + +#ifdef __cplusplus +} +#endif diff --git a/libjupedsim/include/jupedsim/operational_model.h b/libjupedsim/include/jupedsim/operational_model.h new file mode 100644 index 0000000000..9783934913 --- /dev/null +++ b/libjupedsim/include/jupedsim/operational_model.h @@ -0,0 +1,24 @@ +/* Copyright © 2012-2023 Forschungszentrum Jülich GmbH */ +/* SPDX-License-Identifier: LGPL-3.0-or-later */ +#pragma once + +#include "export.h" + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * Opaque type for operational models describing how agents interact in the simulation. + */ +typedef struct JPS_OperationalModel_t* JPS_OperationalModel; + +/** + * Frees a JPS_OperationalModel + * @param handle to the JPS_OperationalModel to free. + */ +JUPEDSIM_API void JPS_OperationalModel_Free(JPS_OperationalModel handle); + +#ifdef __cplusplus +} +#endif diff --git a/libjupedsim/include/jupedsim/routing.h b/libjupedsim/include/jupedsim/routing.h new file mode 100644 index 0000000000..61d4b8e893 --- /dev/null +++ b/libjupedsim/include/jupedsim/routing.h @@ -0,0 +1,66 @@ +/* Copyright © 2012-2023 Forschungszentrum Jülich GmbH */ +/* SPDX-License-Identifier: LGPL-3.0-or-later */ +#pragma once + +#include "export.h" +#include "geometry.h" +#include "types.h" + +#include /*NOLINT(modernize-deprecated-headers)*/ + +#ifdef __cplusplus +extern "C" { +#endif + +struct JPS_Path { + size_t len; + const JPS_Point* points; +}; + +JUPEDSIM_API void JPS_Path_Free(JPS_Path* path); + +struct JPS_Triangle { + JPS_Point points[3]; +}; + +struct JPS_TriangleMesh { + size_t len; + JPS_Triangle* triangles; +}; + +JUPEDSIM_API void JPS_TriangleMesh_Free(JPS_TriangleMesh* mesh); + +struct JPS_Line { + JPS_Point points[2]; +}; + +struct JPS_Lines { + size_t len; + JPS_Line* lines; +}; + +JUPEDSIM_API void JPS_Lines_Free(JPS_Lines* lines); + +/** + * WARNING this is currently a NavMeshRoutingEngine! This does not account possible other types of + * routing engines + */ +typedef struct JPS_RoutingEngine_t* JPS_RoutingEngine; + +JUPEDSIM_API JPS_RoutingEngine JPS_RoutingEngine_Create(JPS_Geometry geometry); + +JUPEDSIM_API JPS_Path +JPS_RoutingEngine_ComputeWaypoint(JPS_RoutingEngine handle, JPS_Point from, JPS_Point to); + +JUPEDSIM_API bool JPS_RoutingEngine_IsRoutable(JPS_RoutingEngine handle, JPS_Point p); + +JUPEDSIM_API JPS_TriangleMesh JPS_RoutingEngine_Mesh(JPS_RoutingEngine handle); + +/// Note: Currently the ID is the index of the vertex from 'JPS_RoutingEngine_Mesh' +JUPEDSIM_API JPS_Lines JPS_RoutingEngine_EdgesFor(JPS_RoutingEngine handle, uint32_t id); + +JUPEDSIM_API void JPS_RoutingEngine_Free(JPS_RoutingEngine handle); + +#ifdef __cplusplus +} +#endif diff --git a/libjupedsim/include/jupedsim/simulation.h b/libjupedsim/include/jupedsim/simulation.h new file mode 100644 index 0000000000..ce1c1275c6 --- /dev/null +++ b/libjupedsim/include/jupedsim/simulation.h @@ -0,0 +1,349 @@ +/* Copyright © 2012-2023 Forschungszentrum Jülich GmbH */ +/* SPDX-License-Identifier: LGPL-3.0-or-later */ +#pragma once + +#include "agent.h" +#include "collision_free_speed_model.h" +#include "error.h" +#include "export.h" +#include "generalized_centrifugal_force_model.h" +#include "geometry.h" +#include "journey.h" +#include "operational_model.h" +#include "stage.h" +#include "types.h" + +#ifdef __cplusplus +extern "C" { +#endif + +/* + * Opaque type to a Simulator object. + */ +typedef struct JPS_Simulation_t* JPS_Simulation; + +/* + * Creates a new JPS_Simulation object. + * NOTE: JPS_Simulation_Create will take ownership of all indicated parameters even in case an error + * occured. + * @param model to use. Will copy 'model', 'model' can be freed after this call or reused for + * another simulation. + * @param geometry to use. Will copy 'geometry', 'geometry' can be freed after this call or reused + * for another simulation. + * @param dT simulation timestep in seconds + * @param[out] errorMessage if not NULL: will be set to a JPS_ErrorMessage in case of an error. + * @return the Simulation + */ +JUPEDSIM_API JPS_Simulation JPS_Simulation_Create( + JPS_OperationalModel model, + JPS_Geometry geometry, + double dT, + JPS_ErrorMessage* errorMessage); + +/** + * Populates the Simulation with a Journey for agents to use. + * @param handle to the simulation to act on + * @param journey to add. Will copy 'journey', 'journey' needs to be freed after this call. + * @param[out] errorMessage if not NULL: will be set to a JPS_ErrorMessage in case of an error. + * @return Id of the journey + */ +JUPEDSIM_API JPS_JourneyId JPS_Simulation_AddJourney( + JPS_Simulation handle, + JPS_JourneyDescription journey, + JPS_ErrorMessage* errorMessage); + +/** + * Adds a new waypoint stage to the simulation. + * @param handle to the simulation to act on + * @param position of the waypoiny + * @parma required distance to check if the waypoint was reached + * @param[out] errorMessage if not NULL: will be set to a JPS_ErrorMessage in case of an error. + * @return Id of the stage + */ +JUPEDSIM_API JPS_StageId JPS_Simulation_AddStageWaypoint( + JPS_Simulation handle, + JPS_Point position, + double distance, + JPS_ErrorMessage* errorMessage); + +/** + * Adds a new exit stage to the simulation. + * @param handle to the simulation to act on + * @param polygon describing the exit + * @param number of points in the polygon + * @param[out] errorMessage if not NULL: will be set to a JPS_ErrorMessage in case of an error. + * @return Id of the stage + */ +JUPEDSIM_API JPS_StageId JPS_Simulation_AddStageExit( + JPS_Simulation handle, + const JPS_Point* polygon, + size_t len_polygon, + JPS_ErrorMessage* errorMessage); + +/** + * Adds a new notifiable queue stage to the simulation. + * The waiting positions supplied are the queue positions. The first point + * defines the head of the queue and the last point defines the tail of the + * queue. When there are more agents waiting in the queue than there are waiting + * slots the overflowing agents will all try to wait at the tail end of the + * queue. + * + * Agents can be popped from the queue by calling + * 'JPS_Simulation_PopAgentsFromQueue' + * + * @param handle to the simulation to act on + * @param waiting_positions of this queue + * @param number of waiting positions + * @param[out] errorMessage if not NULL: will be set to a JPS_ErrorMessage in case of an error. + * @return Id of the stage + */ +JUPEDSIM_API JPS_StageId JPS_Simulation_AddStageNotifiableQueue( + JPS_Simulation handle, + const JPS_Point* waiting_positions, + size_t len_waiting_positions, + JPS_ErrorMessage* errorMessage); + +/** + * Adds a new notifiable waiting set to the simulation. + * The waiting positions supplied are filled in order. + * + * A waiting set can be set active or inactive with + * `JPS_Simulation_ChangeWaitingSetState` When active agents will wait in the + * waiting set until the stage is set inactive. When inactive the stage + * functions as a waypoint at the first position supplied. + * + * @param handle to the simulation to act on + * @param waiting_positions of this waiting set + * @param number of waiting positions + * @param[out] errorMessage if not NULL: will be set to a JPS_ErrorMessage in case of an error. + * @return Id of the stage + */ +JUPEDSIM_API JPS_StageId JPS_Simulation_AddStageWaitingSet( + JPS_Simulation handle, + const JPS_Point* waiting_positions, + size_t len_waiting_positions, + JPS_ErrorMessage* errorMessage); + +/** + * Adds a new direct steering stage to the simulation. + * + * This allows a direct control of the target of an agent. + * + * Important: A direct steering stage can only be used if it is the only stage in a Journey. + * + * @param handle to the simulation to act on + * @param[out] errorMessage if not NULL: will be set to a JPS_ErrorMessage in case of an error. + * @return Id of the stage + */ +JUPEDSIM_API JPS_StageId +JPS_Simulation_AddStageDirectSteering(JPS_Simulation handle, JPS_ErrorMessage* errorMessage); + +/** + * Adds a new agent to the simulation. + * This can be called at any time, i.e. agents can be added at any iteration. + * NOTE: Currently there is no checking done to ensure the agent can be placed at the desired + * location. + * @param handle to the simulation to act on + * @param parameters describing the new agent. + * @param[out] errorMessage if not NULL. Will contain address of JPS_ErrorMessage in case of an + * error. + * @return id of the new agent or 0 if the agent could not be added due to an error. + */ +JUPEDSIM_API JPS_AgentId JPS_Simulation_AddGeneralizedCentrifugalForceModelAgent( + JPS_Simulation handle, + JPS_GeneralizedCentrifugalForceModelAgentParameters parameters, + JPS_ErrorMessage* errorMessage); + +/** + * Adds a new agent to the simulation. + * This can be called at any time, i.e. agents can be added at any iteration. + * NOTE: Currently there is no checking done to ensure the agent can be placed at the desired + * location. + * @param handle to the simulation to act on + * @param parameters describing the new agent. + * @param[out] errorMessage if not NULL. Will contain address of JPS_ErrorMessage in case of an + * error. + * @return id of the new agent or 0 if the agent could not be added due to an error. + */ +JUPEDSIM_API JPS_AgentId JPS_Simulation_AddCollisionFreeSpeedModelAgent( + JPS_Simulation handle, + JPS_CollisionFreeSpeedModelAgentParameters parameters, + JPS_ErrorMessage* errorMessage); + +/** + * Marks an agent from the simuation for removal. + * The agent will be removed at the start of the next simulation iteration, before the interaction + * are computed. + * @param handle to the simulation to act on + * @param agentID to remove + * @param[out] errorMessage if not NULL: will be set to a JPS_ErrorMessage in case of an error. + * @return bool true if the agent existed and was marked for removal otherwise false + */ +JUPEDSIM_API bool JPS_Simulation_MarkAgentForRemoval( + JPS_Simulation handle, + JPS_AgentId agentId, + JPS_ErrorMessage* errorMessage); + +/* + * Returns the ids of all agents that exited the simulation in the last iteration. + * @param handle of the Simulation + * @param[out] data pointer to the ids + * @return numer of agents that have been removed in the last iteration + */ +JUPEDSIM_API size_t JPS_Simulation_RemovedAgents(JPS_Simulation handle, const JPS_AgentId** data); + +/* + * Advances the simulation by one step. + * @param handle of the Simulation + * @param[out] errorMessage if not NULL: will be set to a JPS_ErrorMessage in case of an error. + * @return true if no errors occured + */ +JUPEDSIM_API bool JPS_Simulation_Iterate(JPS_Simulation handle, JPS_ErrorMessage* errorMessage); + +/** + * How many agents are in the simulation. + * @param handle of the simulation + * @return count agents in the simulation + */ +JUPEDSIM_API size_t JPS_Simulation_AgentCount(JPS_Simulation handle); + +/** + * Returns time progressed in the simulation. + * @param handle of the simulation + * @return seconds elapsed + */ +JUPEDSIM_API double JPS_Simulation_ElapsedTime(JPS_Simulation handle); + +/** + * Returns time simulad per iteration call. + * @param handle of the simulation + * @return time simulated per iteration call in seconds + */ +JUPEDSIM_API double JPS_Simulation_DeltaTime(JPS_Simulation handle); + +/** + * Returns how many iterations have been simulated. + * @param handle of the simulation + * @return count of elapsed iterations + */ +JUPEDSIM_API uint64_t JPS_Simulation_IterationCount(JPS_Simulation handle); + +/** + * Returns an iterator over all agents in the simulation. + * Notes: + * The iterator will be invalidated once JPS_Simulation_Iterate is called. + * The iterator needs to be freed after use. + * @param handle of the simulation + * @return iterator over agents in the simulation + */ +JUPEDSIM_API JPS_AgentIterator JPS_Simulation_AgentIterator(JPS_Simulation handle); + +/** + * Returns a specific agent of the simulation. + * @param handle of the simulation + * @param agentId Id of the agent to get + * @return Agent with given Id + */ +JUPEDSIM_API JPS_Agent +JPS_Simulation_GetAgent(JPS_Simulation handle, JPS_AgentId agentId, JPS_ErrorMessage* errorMessage); + +/** + * Switches the journey and currently selected stage of this agent + * @param handle of the Simulation to operate on + * @param agentId id of the agent to modify + * @param journeyId of the journey to select + * @param stageId of the stage to select + * @param[out] errorMessage if not NULL: will be set to a JPS_ErrorMessage in case of an error. + * @return true on success, false on any error, e.g. unknown agent id or profile id + */ +JUPEDSIM_API bool JPS_Simulation_SwitchAgentJourney( + JPS_Simulation handle, + JPS_AgentId agentId, + JPS_JourneyId journeyId, + JPS_StageId stageId, + JPS_ErrorMessage* errorMessage); + +/** + * Query the pedestrian model used by this simulation. + * @return the type of pedestrian model used in this simulation instance. + */ +JUPEDSIM_API JPS_ModelType JPS_Simulation_ModelType(JPS_Simulation handle); + +/** + * Query the simulation for all agent ids in distance to this point; + * @param handle of the Simulation to operate on + * @param position to query + * @param distance around position + * @return iterator containing all agent ids in range + */ +JUPEDSIM_API JPS_AgentIdIterator +JPS_Simulation_AgentsInRange(JPS_Simulation handle, JPS_Point position, double distance); + +/** + * Query the simulation for all agent ids in the supplied polygon. + * @param handle of the Simulation to operate on + * @param polygon array of JPS_Points (CCW ordered convex polygon) + * @param len_polygon number of points in the polygon + * @return iterator over the Ids inside the polygon + */ +JUPEDSIM_API JPS_AgentIdIterator +JPS_Simulation_AgentsInPolygon(JPS_Simulation handle, const JPS_Point* polygon, size_t len_polygon); + +JUPEDSIM_API JPS_StageType JPS_Simulation_GetStageType(JPS_Simulation handle, JPS_StageId id); + +JUPEDSIM_API JPS_NotifiableQueueProxy JPS_Simulation_GetNotifiableQueueProxy( + JPS_Simulation handle, + JPS_StageId stageId, + JPS_ErrorMessage* errorMessage); + +JUPEDSIM_API JPS_WaitingSetProxy JPS_Simulation_GetWaitingSetProxy( + JPS_Simulation handle, + JPS_StageId stageId, + JPS_ErrorMessage* errorMessage); + +JUPEDSIM_API JPS_WaypointProxy JPS_Simulation_GetWaypointProxy( + JPS_Simulation handle, + JPS_StageId stageId, + JPS_ErrorMessage* errorMessage); + +JUPEDSIM_API JPS_ExitProxy JPS_Simulation_GetExitProxy( + JPS_Simulation handle, + JPS_StageId stageId, + JPS_ErrorMessage* errorMessage); + +JUPEDSIM_API JPS_DirectSteeringProxy JPS_Simulation_GetDirectSteeringProxy( + JPS_Simulation handle, + JPS_StageId stageId, + JPS_ErrorMessage* errorMessage); + +/** + * Enable / disable collection of performance data. + * @param handle of the Simulation to operate on + * @param status new status to set + */ +JUPEDSIM_API void JPS_Simulation_SetTracing(JPS_Simulation handle, bool status); + +/** + * Read trace data from alst iteration. If tracing is disable all timings will be zero. + * @param handle of the Simulation to operate on + * @return trace data + */ +JUPEDSIM_API JPS_Trace JPS_Simulation_GetTrace(JPS_Simulation handle); + +/** + * Gain read access to the geometry used by this simulation. + * @param handle of the Simulation to operate on + * @return the geometry. Do not call JPS_Geometry_Free on this handle, + * the geometry is still owned by the simulation! + */ +JUPEDSIM_API JPS_Geometry JPS_Simulation_GetGeometry(JPS_Simulation handle); + +/** + * Frees a JPS_Simulation. + * @param handle to the JPS_Simulation to free. + */ +JUPEDSIM_API void JPS_Simulation_Free(JPS_Simulation handle); + +#ifdef __cplusplus +} +#endif diff --git a/libjupedsim/include/jupedsim/stage.h b/libjupedsim/include/jupedsim/stage.h new file mode 100644 index 0000000000..ce594469b5 --- /dev/null +++ b/libjupedsim/include/jupedsim/stage.h @@ -0,0 +1,77 @@ +/* Copyright © 2012-2023 Forschungszentrum Jülich GmbH */ +/* SPDX-License-Identifier: LGPL-3.0-or-later */ +#pragma once + +#include "error.h" +#include "export.h" +#include "types.h" + +#include /*NOLINT(modernize-deprecated-headers)*/ + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * Identifies the type of stage + */ +enum JPS_StageType { + JPS_NotifiableQueueType, + JPS_WaitingSetType, + JPS_WaypointType, + JPS_ExitType, + JPS_DirectSteeringType +}; + +/** + * Opaque type of an NotifiableQueueProxy + */ +typedef struct JPS_NotifiableQueueProxy_t* JPS_NotifiableQueueProxy; + +JUPEDSIM_API size_t JPS_NotifiableQueueProxy_GetCountTargeting(JPS_NotifiableQueueProxy handle); + +JUPEDSIM_API size_t JPS_NotifiableQueueProxy_GetCountEnqueued(JPS_NotifiableQueueProxy handle); + +JUPEDSIM_API void JPS_NotifiableQueueProxy_Pop(JPS_NotifiableQueueProxy handle, size_t count); + +JUPEDSIM_API size_t +JPS_NotifiableQueueProxy_GetEnqueued(JPS_NotifiableQueueProxy handle, const JPS_AgentId** data); + +JUPEDSIM_API void JPS_NotifiableQueueProxy_Free(JPS_NotifiableQueueProxy handle); + +enum JPS_WaitingSetState { JPS_WaitingSet_Active, JPS_WaitingSet_Inactive }; +typedef struct JPS_WaitingSetProxy_t* JPS_WaitingSetProxy; + +JUPEDSIM_API size_t JPS_WaitingSetProxy_GetCountTargeting(JPS_WaitingSetProxy handle); + +JUPEDSIM_API size_t JPS_WaitingSetProxy_GetCountWaiting(JPS_WaitingSetProxy handle); + +JUPEDSIM_API size_t +JPS_WaitingSetProxy_GetWaiting(JPS_WaitingSetProxy handle, const JPS_AgentId** data); + +JUPEDSIM_API void +JPS_WaitingSetProxy_SetWaitingSetState(JPS_WaitingSetProxy handle, JPS_WaitingSetState newState); + +JUPEDSIM_API JPS_WaitingSetState JPS_WaitingSetProxy_GetWaitingSetState(JPS_WaitingSetProxy handle); + +JUPEDSIM_API void JPS_WaitingSetProxy_Free(JPS_WaitingSetProxy handle); + +typedef struct JPS_WaypointProxy_t* JPS_WaypointProxy; + +JUPEDSIM_API size_t JPS_WaypointProxy_GetCountTargeting(JPS_WaypointProxy handle); + +JUPEDSIM_API void JPS_WaypointProxy_Free(JPS_WaypointProxy handle); + +typedef struct JPS_ExitProxy_t* JPS_ExitProxy; + +JUPEDSIM_API size_t JPS_ExitProxy_GetCountTargeting(JPS_ExitProxy handle); + +JUPEDSIM_API void JPS_ExitProxy_Free(JPS_ExitProxy handle); + +typedef struct JPS_DirectSteeringProxy_t* JPS_DirectSteeringProxy; + +JUPEDSIM_API void JPS_DirectSteeringProxy_Free(JPS_DirectSteeringProxy handle); + +#ifdef __cplusplus +} +#endif diff --git a/libjupedsim/include/jupedsim/transition.h b/libjupedsim/include/jupedsim/transition.h new file mode 100644 index 0000000000..a5e7c3969e --- /dev/null +++ b/libjupedsim/include/jupedsim/transition.h @@ -0,0 +1,60 @@ +/* Copyright © 2012-2023 Forschungszentrum Jülich GmbH */ +/* SPDX-License-Identifier: LGPL-3.0-or-later */ +#pragma once + +#include "error.h" +#include "export.h" +#include "types.h" + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * Opaque type for transition, describing route based decisions. + */ +typedef struct JPS_Transition_t* JPS_Transition; + +/** + * Create a fixed transition to stage + * @param[out] errorMessage if not NULL: will be set to a JPS_ErrorMessage in case of an error. + * @return Fixed transition to stage or NULL on any error. + */ +JUPEDSIM_API JPS_Transition +JPS_Transition_CreateFixedTransition(JPS_StageId stageId, JPS_ErrorMessage* errorMessage); + +/** + * Create a round robin transition to stages + * @param stages target stages + * @param weights weights of target stage + * @param len length of stages and weights + * @param[out] errorMessage if not NULL: will be set to a JPS_ErrorMessage in case of an error. + * @return Round robin transition to stages or NULL on any error. + */ +JUPEDSIM_API JPS_Transition JPS_Transition_CreateRoundRobinTransition( + JPS_StageId* stages, + uint64_t* weights, + size_t len_stages, + JPS_ErrorMessage* errorMessage); + +/** + * Create a least targeted transition to stages + * @param stages target stages + * @param len length of stages + * @param[out] errorMessage if not NULL: will be set to a JPS_ErrorMessage in case of an error. + * @return least targeted transition to stages or NULL on any error. + */ +JUPEDSIM_API JPS_Transition JPS_Transition_CreateLeastTargetedTransition( + JPS_StageId* stages, + size_t len_stages, + JPS_ErrorMessage* errorMessage); + +/** + * Frees a JPS_Transition + * @param handle to the JPS_Transition to free. + */ +JUPEDSIM_API void JPS_Transition_Free(JPS_Transition handle); + +#ifdef __cplusplus +} +#endif diff --git a/libjupedsim/include/jupedsim/types.h b/libjupedsim/include/jupedsim/types.h new file mode 100644 index 0000000000..447edb11d5 --- /dev/null +++ b/libjupedsim/include/jupedsim/types.h @@ -0,0 +1,91 @@ +/* Copyright © 2012-2023 Forschungszentrum Jülich GmbH */ +/* SPDX-License-Identifier: LGPL-3.0-or-later */ +#pragma once + +#include "export.h" + +#include /*NOLINT(modernize-deprecated-headers)*/ +#include /*NOLINT(modernize-deprecated-headers)*/ + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * Contains basic performance trace information + */ +typedef struct JPS_Trace { + /** + * Duration of the iterate call in micorseconds + */ + uint64_t iteration_duration; + /** + * Duration to compute updates of the operational decision level in micorseconds. + * This is fully contained in iterate. + */ + uint64_t operational_level_duration; +} JPS_Trace; + +/** + * A 2D coordinate. Units are 'meters' + */ +typedef struct JPS_Point { + /** + * x component in 'meters' + */ + double x; + /** + * y component in 'meters' + */ + double y; +} JPS_Point; + +/** + * Describes a waypoint. + */ +typedef struct JPS_Waypoint { + /** + * Position of the waypoint + */ + JPS_Point position; + /** + * Distance in 'meters' at which this waypoint is considered to be reached. + */ + double distance; +} JPS_Waypoint; + +/** + * Describes the pedestrian model used in the simulation. + */ +typedef enum JPS_ModelType { + JPS_GeneralizedCentrifugalForceModel, + JPS_CollisionFreeSpeedModel +} JPS_ModelType; + +/** + * Id of a journey. + * Zero represents an invalid id. + */ +typedef uint64_t JPS_JourneyId; + +/** + * Id of a stage. + * Zero represents an invalid id. + */ +typedef uint64_t JPS_StageId; + +/** + * Index of a stage within a journey. + * Note that stage ids are only unique within the journey they refer to. + */ +typedef size_t JPS_StageIndex; + +/** + * Id of an agent. + * Zero represents an invalid id. + */ +typedef uint64_t JPS_AgentId; + +#ifdef __cplusplus +} +#endif diff --git a/libjupedsim/src/AgentIterator.hpp b/libjupedsim/src/AgentIterator.hpp index a6e4882af8..91c98edb8c 100644 --- a/libjupedsim/src/AgentIterator.hpp +++ b/libjupedsim/src/AgentIterator.hpp @@ -2,6 +2,8 @@ // SPDX-License-Identifier: LGPL-3.0-or-later #pragma once +#include + #include #include @@ -27,3 +29,11 @@ class AgentIterator return result; } }; + +struct AgentIdIterator { + using AgentIds = std::vector; + AgentIds ids; + AgentIds::const_iterator iter; + + AgentIdIterator(AgentIds&& ids_) : ids(std::move(ids_)) { iter = std::begin(ids); } +}; diff --git a/libjupedsim/src/JourneyDescription.hpp b/libjupedsim/src/JourneyDescription.hpp new file mode 100644 index 0000000000..19eea5608d --- /dev/null +++ b/libjupedsim/src/JourneyDescription.hpp @@ -0,0 +1,8 @@ +// Copyright © 2012-2023 Forschungszentrum Jülich GmbH +// SPDX-License-Identifier: LGPL-3.0-or-later +#include +#include + +#include + +using JourneyDesc = std::map; diff --git a/libjupedsim/src/agent.cpp b/libjupedsim/src/agent.cpp new file mode 100644 index 0000000000..7856cc03a3 --- /dev/null +++ b/libjupedsim/src/agent.cpp @@ -0,0 +1,171 @@ +// Copyright © 2012-2023 Forschungszentrum Jülich GmbH +// SPDX-License-Identifier: LGPL-3.0-or-later +#include "jupedsim/agent.h" + +#include "AgentIterator.hpp" +#include "Conversion.hpp" +#include "ErrorMessage.hpp" + +#include +#include + +#include + +using jupedsim::detail::intoJPS_Point; +using jupedsim::detail::intoPoint; + +//////////////////////////////////////////////////////////////////////////////////////////////////// +/// Agent +//////////////////////////////////////////////////////////////////////////////////////////////////// +JPS_AgentId JPS_Agent_GetId(JPS_Agent handle) +{ + assert(handle); + const auto agent = reinterpret_cast(handle); + return agent->id.getID(); +} + +JPS_JourneyId JPS_Agent_GetJourneyId(JPS_Agent handle) +{ + assert(handle); + const auto agent = reinterpret_cast(handle); + return agent->journeyId.getID(); +} + +JPS_StageId JPS_Agent_GetStageId(JPS_Agent handle) +{ + assert(handle); + const auto agent = reinterpret_cast(handle); + return agent->stageId.getID(); +} + +JPS_Point JPS_Agent_GetPosition(JPS_Agent handle) +{ + assert(handle); + const auto agent = reinterpret_cast(handle); + return intoJPS_Point(agent->pos); +} + +JPS_Point JPS_Agent_GetTarget(JPS_Agent handle) +{ + assert(handle); + const auto agent = reinterpret_cast(handle); + return intoJPS_Point(agent->target); +} + +bool JPS_Agent_SetTarget(JPS_Agent handle, JPS_Point waypoint, JPS_ErrorMessage* errorMessage) +{ + assert(handle); + try { + auto agent = reinterpret_cast(handle); + agent->target = intoPoint(waypoint); + return true; + } catch(const std::exception& ex) { + if(errorMessage) { + *errorMessage = reinterpret_cast(new JPS_ErrorMessage_t{ex.what()}); + } + } catch(...) { + if(errorMessage) { + *errorMessage = reinterpret_cast( + new JPS_ErrorMessage_t{"Unknown internal error."}); + } + } + return false; +} + +JPS_Point JPS_Agent_GetOrientation(JPS_Agent handle) +{ + assert(handle); + const auto agent = reinterpret_cast(handle); + return intoJPS_Point(agent->orientation); +} + +JPS_ModelType JPS_Agent_GetModelType(JPS_Agent handle) +{ + assert(handle); + const auto agent = reinterpret_cast(handle); + switch(agent->model.index()) { + case 0: + return JPS_GeneralizedCentrifugalForceModel; + case 1: + return JPS_CollisionFreeSpeedModel; + } + UNREACHABLE(); +} + +JPS_GeneralizedCentrifugalForceModelState +JPS_Agent_GetGeneralizedCentrifugalForceModelState(JPS_Agent handle, JPS_ErrorMessage* errorMessage) +{ + assert(handle); + const auto agent = reinterpret_cast(handle); + try { + auto& model = std::get(agent->model); + return reinterpret_cast(&model); + } catch(const std::exception& ex) { + if(errorMessage) { + *errorMessage = reinterpret_cast(new JPS_ErrorMessage_t{ex.what()}); + } + } catch(...) { + if(errorMessage) { + *errorMessage = reinterpret_cast( + new JPS_ErrorMessage_t{"Unknown internal error."}); + } + } + return nullptr; +} + +JPS_CollisionFreeSpeedModelState +JPS_Agent_GetCollisionFreeSpeedModelState(JPS_Agent handle, JPS_ErrorMessage* errorMessage) +{ + assert(handle); + const auto agent = reinterpret_cast(handle); + try { + auto& model = std::get(agent->model); + return reinterpret_cast(&model); + } catch(const std::exception& ex) { + if(errorMessage) { + *errorMessage = reinterpret_cast(new JPS_ErrorMessage_t{ex.what()}); + } + } catch(...) { + if(errorMessage) { + *errorMessage = reinterpret_cast( + new JPS_ErrorMessage_t{"Unknown internal error."}); + } + } + return nullptr; +} + +//////////////////////////////////////////////////////////////////////////////////////////////////// +/// AgentIterator +//////////////////////////////////////////////////////////////////////////////////////////////////// +JPS_Agent JPS_AgentIterator_Next(JPS_AgentIterator handle) +{ + assert(handle); + auto iterator = reinterpret_cast*>(handle); + return reinterpret_cast(iterator->Next()); +} + +void JPS_AgentIterator_Free(JPS_AgentIterator handle) +{ + delete reinterpret_cast*>(handle); +} + +//////////////////////////////////////////////////////////////////////////////////////////////////// +/// AgentIdIterator +//////////////////////////////////////////////////////////////////////////////////////////////////// +JPS_AgentId JPS_AgentIdIterator_Next(JPS_AgentIdIterator handle) +{ + assert(handle); + auto iterator = reinterpret_cast(handle); + auto& [vec, iter] = *iterator; + if(iter == std::end(vec)) { + return GenericAgent::ID::Invalid.getID(); + } + const auto id = *iter; + ++iter; + return id.getID(); +} + +void JPS_AgentIdIterator_Free(JPS_AgentIdIterator handle) +{ + delete reinterpret_cast(handle); +} diff --git a/libjupedsim/src/build_info.cpp b/libjupedsim/src/build_info.cpp new file mode 100644 index 0000000000..cec37068fe --- /dev/null +++ b/libjupedsim/src/build_info.cpp @@ -0,0 +1,19 @@ +// Copyright © 2012-2023 Forschungszentrum Jülich GmbH +// SPDX-License-Identifier: LGPL-3.0-or-later +#include "jupedsim/build_info.h" + +#include + +//////////////////////////////////////////////////////////////////////////////////////////////////// +/// BuildInfo +//////////////////////////////////////////////////////////////////////////////////////////////////// +JPS_BuildInfo JPS_GetBuildInfo() +{ + return JPS_BuildInfo{ + GIT_COMMIT_HASH.c_str(), + GIT_COMMIT_DATE.c_str(), + GIT_BRANCH.c_str(), + COMPILER.c_str(), + COMPILER_VERSION.c_str(), + LIBRARY_VERSION.c_str()}; +} diff --git a/libjupedsim/src/collision_free_speed_model.cpp b/libjupedsim/src/collision_free_speed_model.cpp new file mode 100644 index 0000000000..7dafa90c72 --- /dev/null +++ b/libjupedsim/src/collision_free_speed_model.cpp @@ -0,0 +1,107 @@ +// Copyright © 2012-2023 Forschungszentrum Jülich GmbH +// SPDX-License-Identifier: LGPL-3.0-or-later +#include "jupedsim/collision_free_speed_model.h" +#include "jupedsim/error.h" + +#include "Conversion.hpp" +#include "ErrorMessage.hpp" + +#include +#include +#include + +using jupedsim::detail::intoJPS_Point; +using jupedsim::detail::intoPoint; +using jupedsim::detail::intoTuple; + +//////////////////////////////////////////////////////////////////////////////////////////////////// +/// Collision Free Speed Model Builder +//////////////////////////////////////////////////////////////////////////////////////////////////// +JUPEDSIM_API JPS_CollisionFreeSpeedModelBuilder JPS_CollisionFreeSpeedModelBuilder_Create( + double strengthNeighborRepulsion, + double rangeNeighborRepulsion, + double strengthGeometryRepulsion, + double rangeGeometryRepulsion) +{ + return reinterpret_cast(new CollisionFreeSpeedModelBuilder( + strengthNeighborRepulsion, + rangeNeighborRepulsion, + strengthGeometryRepulsion, + rangeGeometryRepulsion)); +} + +JUPEDSIM_API JPS_OperationalModel JPS_CollisionFreeSpeedModelBuilder_Build( + JPS_CollisionFreeSpeedModelBuilder handle, + JPS_ErrorMessage* errorMessage) +{ + assert(handle != nullptr); + auto builder = reinterpret_cast(handle); + JPS_OperationalModel result{}; + try { + result = + reinterpret_cast(new CollisionFreeSpeedModel(builder->Build())); + } catch(const std::exception& ex) { + if(errorMessage) { + *errorMessage = reinterpret_cast(new JPS_ErrorMessage_t{ex.what()}); + } + } catch(...) { + if(errorMessage) { + *errorMessage = reinterpret_cast( + new JPS_ErrorMessage_t{"Unknown internal error."}); + } + } + return result; +} + +JUPEDSIM_API void JPS_CollisionFreeSpeedModelBuilder_Free(JPS_CollisionFreeSpeedModelBuilder handle) +{ + delete reinterpret_cast(handle); +} + +//////////////////////////////////////////////////////////////////////////////////////////////////// +/// CollisionFreeSpeedModelState +//////////////////////////////////////////////////////////////////////////////////////////////////// +double JPS_CollisionFreeSpeedModelState_GetTimeGap(JPS_CollisionFreeSpeedModelState handle) +{ + assert(handle); + const auto state = reinterpret_cast(handle); + return state->timeGap; +} + +void JPS_CollisionFreeSpeedModelState_SetTimeGap( + JPS_CollisionFreeSpeedModelState handle, + double time_gap) +{ + assert(handle); + auto state = reinterpret_cast(handle); + state->timeGap = time_gap; +} + +double JPS_CollisionFreeSpeedModelState_GetV0(JPS_CollisionFreeSpeedModelState handle) +{ + assert(handle); + const auto state = reinterpret_cast(handle); + return state->v0; +} + +void JPS_CollisionFreeSpeedModelState_SetV0(JPS_CollisionFreeSpeedModelState handle, double v0) +{ + assert(handle); + auto state = reinterpret_cast(handle); + state->v0 = v0; +} +double JPS_CollisionFreeSpeedModelState_GetRadius(JPS_CollisionFreeSpeedModelState handle) +{ + assert(handle); + const auto state = reinterpret_cast(handle); + return state->radius; +} + +void JPS_CollisionFreeSpeedModelState_SetRadius( + JPS_CollisionFreeSpeedModelState handle, + double radius) +{ + assert(handle); + auto state = reinterpret_cast(handle); + state->radius = radius; +} diff --git a/libjupedsim/src/error.cpp b/libjupedsim/src/error.cpp new file mode 100644 index 0000000000..f9f60cfbfc --- /dev/null +++ b/libjupedsim/src/error.cpp @@ -0,0 +1,19 @@ +// Copyright © 2012-2023 Forschungszentrum Jülich GmbH +// SPDX-License-Identifier: LGPL-3.0-or-later +#include "jupedsim/error.h" + +#include "ErrorMessage.hpp" + +//////////////////////////////////////////////////////////////////////////////////////////////////// +/// ErrorMessage +//////////////////////////////////////////////////////////////////////////////////////////////////// +const char* JPS_ErrorMessage_GetMessage(JPS_ErrorMessage handle) +{ + JPS_ErrorMessage_t* msg = reinterpret_cast(handle); + return msg->message.c_str(); +} + +void JPS_ErrorMessage_Free(JPS_ErrorMessage handle) +{ + delete reinterpret_cast(handle); +} diff --git a/libjupedsim/src/generalized_centrifugal_force_model.cpp b/libjupedsim/src/generalized_centrifugal_force_model.cpp new file mode 100644 index 0000000000..1bdacce6e1 --- /dev/null +++ b/libjupedsim/src/generalized_centrifugal_force_model.cpp @@ -0,0 +1,224 @@ +// Copyright © 2012-2023 Forschungszentrum Jülich GmbH +// SPDX-License-Identifier: LGPL-3.0-or-later +#include "jupedsim/generalized_centrifugal_force_model.h" +#include "jupedsim/error.h" + +#include "Conversion.hpp" +#include "ErrorMessage.hpp" + +#include +#include +#include + +using jupedsim::detail::intoJPS_Point; +using jupedsim::detail::intoPoint; +using jupedsim::detail::intoTuple; + +//////////////////////////////////////////////////////////////////////////////////////////////////// +/// GeneralizedCentrifugalForceModel Model Builder +//////////////////////////////////////////////////////////////////////////////////////////////////// +JPS_GeneralizedCentrifugalForceModelBuilder JPS_GeneralizedCentrifugalForceModelBuilder_Create( + double strengthNeighborRepulsion, + double strengthGeometryRepulsion, + double maxNeighborInteractionDistance, + double maxGeometryInteractionDistance, + double maxNeighborInterpolationDistance, + double maxGeometryInterpolationDistance, + double maxNeighborRepulsionForce, + double maxGeometryRepulsionForce) +{ + return reinterpret_cast( + new GeneralizedCentrifugalForceModelBuilder( + strengthNeighborRepulsion, + strengthGeometryRepulsion, + maxNeighborInteractionDistance, + maxGeometryInteractionDistance, + maxNeighborInterpolationDistance, + maxGeometryInterpolationDistance, + maxNeighborRepulsionForce, + maxGeometryRepulsionForce)); +} + +JPS_OperationalModel JPS_GeneralizedCentrifugalForceModelBuilder_Build( + JPS_GeneralizedCentrifugalForceModelBuilder handle, + JPS_ErrorMessage* errorMessage) +{ + assert(handle != nullptr); + auto builder = reinterpret_cast(handle); + JPS_OperationalModel result{}; + try { + result = reinterpret_cast( + new GeneralizedCentrifugalForceModel(builder->Build())); + } catch(const std::exception& ex) { + if(errorMessage) { + *errorMessage = reinterpret_cast(new JPS_ErrorMessage_t{ex.what()}); + } + } catch(...) { + if(errorMessage) { + *errorMessage = reinterpret_cast( + new JPS_ErrorMessage_t{"Unknown internal error."}); + } + } + return result; +} + +void JPS_GeneralizedCentrifugalForceModelBuilder_Free( + JPS_GeneralizedCentrifugalForceModelBuilder handle) +{ + delete reinterpret_cast(handle); +} + +//////////////////////////////////////////////////////////////////////////////////////////////////// +/// GeneralizedCentrifugalForceModelState +//////////////////////////////////////////////////////////////////////////////////////////////////// +double +JPS_GeneralizedCentrifugalForceModelState_GetSpeed(JPS_GeneralizedCentrifugalForceModelState handle) +{ + assert(handle); + const auto state = reinterpret_cast(handle); + return state->speed; +} +void JPS_GeneralizedCentrifugalForceModelState_SetSpeed( + JPS_GeneralizedCentrifugalForceModelState handle, + double speed) +{ + assert(handle); + const auto state = reinterpret_cast(handle); + state->speed = speed; +} + +JPS_Point +JPS_GeneralizedCentrifugalForceModelState_GetE0(JPS_GeneralizedCentrifugalForceModelState handle) +{ + assert(handle); + const auto state = reinterpret_cast(handle); + return intoJPS_Point(state->e0); +} + +void JPS_GeneralizedCentrifugalForceModelState_SetE0( + JPS_GeneralizedCentrifugalForceModelState handle, + JPS_Point e0) +{ + assert(handle); + const auto state = reinterpret_cast(handle); + state->e0 = intoPoint(e0); +} + +double +JPS_GeneralizedCentrifugalForceModelState_GetMass(JPS_GeneralizedCentrifugalForceModelState handle) +{ + assert(handle); + const auto state = reinterpret_cast(handle); + return state->mass; +} + +void JPS_GeneralizedCentrifugalForceModelState_SetMass( + JPS_GeneralizedCentrifugalForceModelState handle, + double mass) +{ + assert(handle); + const auto state = reinterpret_cast(handle); + state->mass = mass; +} + +double +JPS_GeneralizedCentrifugalForceModelState_GetTau(JPS_GeneralizedCentrifugalForceModelState handle) +{ + assert(handle); + const auto state = reinterpret_cast(handle); + return state->tau; +} + +void JPS_GeneralizedCentrifugalForceModelState_SetTau( + JPS_GeneralizedCentrifugalForceModelState handle, + double tau) +{ + assert(handle); + const auto state = reinterpret_cast(handle); + state->tau = tau; +} + +double +JPS_GeneralizedCentrifugalForceModelState_GetV0(JPS_GeneralizedCentrifugalForceModelState handle) +{ + assert(handle); + const auto state = reinterpret_cast(handle); + return state->v0; +} + +void JPS_GeneralizedCentrifugalForceModelState_SetV0( + JPS_GeneralizedCentrifugalForceModelState handle, + double v0) +{ + assert(handle); + const auto state = reinterpret_cast(handle); + state->v0 = v0; +} + +double +JPS_GeneralizedCentrifugalForceModelState_GetAV(JPS_GeneralizedCentrifugalForceModelState handle) +{ + assert(handle); + const auto state = reinterpret_cast(handle); + return state->Av; +} + +void JPS_GeneralizedCentrifugalForceModelState_SetAV( + JPS_GeneralizedCentrifugalForceModelState handle, + double a_v) +{ + assert(handle); + const auto state = reinterpret_cast(handle); + state->Av = a_v; +} + +double +JPS_GeneralizedCentrifugalForceModelState_GetAMin(JPS_GeneralizedCentrifugalForceModelState handle) +{ + assert(handle); + const auto state = reinterpret_cast(handle); + return state->AMin; +} + +void JPS_GeneralizedCentrifugalForceModelState_SetAMin( + JPS_GeneralizedCentrifugalForceModelState handle, + double a_min) +{ + assert(handle); + const auto state = reinterpret_cast(handle); + state->AMin = a_min; +} + +double +JPS_GeneralizedCentrifugalForceModelState_GetBMin(JPS_GeneralizedCentrifugalForceModelState handle) +{ + assert(handle); + const auto state = reinterpret_cast(handle); + return state->BMin; +} + +void JPS_GeneralizedCentrifugalForceModelState_SetBMin( + JPS_GeneralizedCentrifugalForceModelState handle, + double b_min) +{ + assert(handle); + const auto state = reinterpret_cast(handle); + state->BMin = b_min; +} + +double +JPS_GeneralizedCentrifugalForceModelState_GetBMax(JPS_GeneralizedCentrifugalForceModelState handle) +{ + assert(handle); + const auto state = reinterpret_cast(handle); + return state->BMax; +} + +void JPS_GeneralizedCentrifugalForceModelState_SetBMax( + JPS_GeneralizedCentrifugalForceModelState handle, + double b_max) +{ + assert(handle); + const auto state = reinterpret_cast(handle); + state->BMax = b_max; +} diff --git a/libjupedsim/src/geometry.cpp b/libjupedsim/src/geometry.cpp new file mode 100644 index 0000000000..09d88dfd34 --- /dev/null +++ b/libjupedsim/src/geometry.cpp @@ -0,0 +1,146 @@ +// Copyright © 2012-2023 Forschungszentrum Jülich GmbH +// SPDX-License-Identifier: LGPL-3.0-or-later +#include "jupedsim/geometry.h" + +#include "jupedsim/error.h" + +#include "Conversion.hpp" +#include "ErrorMessage.hpp" + +#include +#include + +using jupedsim::detail::intoJPS_Point; +using jupedsim::detail::intoPoint; +using jupedsim::detail::intoTuple; + +//////////////////////////////////////////////////////////////////////////////////////////////////// +/// GeometryBuilder +//////////////////////////////////////////////////////////////////////////////////////////////////// + +JPS_GeometryBuilder JPS_GeometryBuilder_Create() +{ + return reinterpret_cast(new GeometryBuilder{}); +} + +void JPS_GeometryBuilder_AddAccessibleArea( + JPS_GeometryBuilder handle, + const JPS_Point* polygon, + size_t lenPolygon) +{ + assert(handle != nullptr); + auto builder = reinterpret_cast(handle); + std::vector loop{}; + loop.reserve(lenPolygon); + std::transform(polygon, polygon + lenPolygon, std::back_inserter(loop), intoPoint); + builder->AddAccessibleArea(loop); +} + +void JPS_GeometryBuilder_ExcludeFromAccessibleArea( + JPS_GeometryBuilder handle, + const JPS_Point* polygon, + size_t lenPolygon) +{ + assert(handle != nullptr); + auto builder = reinterpret_cast(handle); + std::vector loop{}; + loop.reserve(lenPolygon); + std::transform(polygon, polygon + lenPolygon, std::back_inserter(loop), intoPoint); + builder->ExcludeFromAccessibleArea(loop); +} + +JPS_Geometry JPS_GeometryBuilder_Build(JPS_GeometryBuilder handle, JPS_ErrorMessage* errorMessage) +{ + assert(handle != nullptr); + auto builder = reinterpret_cast(handle); + JPS_Geometry result{}; + try { + result = reinterpret_cast(new Geometry(builder->Build())); + } catch(const std::exception& ex) { + if(errorMessage) { + *errorMessage = reinterpret_cast(new JPS_ErrorMessage_t{ex.what()}); + } + } catch(...) { + if(errorMessage) { + *errorMessage = reinterpret_cast( + new JPS_ErrorMessage_t{"Unknown internal error."}); + } + } + return result; +} + +void JPS_GeometryBuilder_Free(JPS_GeometryBuilder handle) +{ + delete reinterpret_cast(handle); +} + +//////////////////////////////////////////////////////////////////////////////////////////////////// +/// Geometry +//////////////////////////////////////////////////////////////////////////////////////////////////// +size_t JPS_Geometry_GetBoundarySize(JPS_Geometry handle) +{ + assert(handle); + const auto geo = reinterpret_cast(handle); + return std::get<0>(geo->collisionGeometry->AccessibleArea()).size(); +} + +const JPS_Point* JPS_Geometry_GetBoundaryData(JPS_Geometry handle) +{ + assert(handle); + const auto geo = reinterpret_cast(handle); + return reinterpret_cast( + std::get<0>(geo->collisionGeometry->AccessibleArea()).data()); +} + +size_t JPS_Geometry_GetHoleCount(JPS_Geometry handle) +{ + assert(handle); + const auto geo = reinterpret_cast(handle); + return std::get<1>(geo->collisionGeometry->AccessibleArea()).size(); +} + +size_t +JPS_Geometry_GetHoleSize(JPS_Geometry handle, size_t hole_index, JPS_ErrorMessage* errorMessage) +{ + assert(handle); + const auto geo = reinterpret_cast(handle); + try { + return std::get<1>(geo->collisionGeometry->AccessibleArea()).at(hole_index).size(); + } catch(const std::exception& ex) { + if(errorMessage) { + *errorMessage = reinterpret_cast(new JPS_ErrorMessage_t{ex.what()}); + } + } catch(...) { + if(errorMessage) { + *errorMessage = reinterpret_cast( + new JPS_ErrorMessage_t{"Unknown internal error."}); + } + } + return 0; +} + +const JPS_Point* +JPS_Geometry_GetHoleData(JPS_Geometry handle, size_t hole_index, JPS_ErrorMessage* errorMessage) +{ + assert(handle); + const auto geo = reinterpret_cast(handle); + try { + return reinterpret_cast( + std::get<1>(geo->collisionGeometry->AccessibleArea()).at(hole_index).data()); + } catch(const std::exception& ex) { + if(errorMessage) { + *errorMessage = reinterpret_cast(new JPS_ErrorMessage_t{ex.what()}); + } + } catch(...) { + if(errorMessage) { + *errorMessage = reinterpret_cast( + new JPS_ErrorMessage_t{"Unknown internal error."}); + } + } + return nullptr; +} + +void JPS_Geometry_Free(JPS_Geometry handle) +{ + delete reinterpret_cast(handle); +} diff --git a/libjupedsim/src/journey.cpp b/libjupedsim/src/journey.cpp new file mode 100644 index 0000000000..0e0a683d49 --- /dev/null +++ b/libjupedsim/src/journey.cpp @@ -0,0 +1,136 @@ +// Copyright © 2012-2023 Forschungszentrum Jülich GmbH +// SPDX-License-Identifier: LGPL-3.0-or-later +#include "jupedsim/journey.h" + +#include "jupedsim/error.h" + +#include "ErrorMessage.hpp" +#include "JourneyDescription.hpp" + +#include +#include + +//////////////////////////////////////////////////////////////////////////////////////////////////// +/// JourneyDescription +//////////////////////////////////////////////////////////////////////////////////////////////////// +using JourneyDesc = std::map; + +JPS_JourneyDescription JPS_JourneyDescription_Create() +{ + return reinterpret_cast(new JourneyDesc{}); +} + +void JPS_JourneyDescription_AddStage(JPS_JourneyDescription handle, JPS_StageId id) +{ + assert(handle); + auto journeyDesc = reinterpret_cast(handle); + (*journeyDesc)[BaseStage::ID{id}] = NonTransitionDescription{}; +} + +bool JPS_JourneyDescription_SetTransitionForStage( + JPS_JourneyDescription handle, + JPS_StageId id, + JPS_Transition transition, + JPS_ErrorMessage* errorMessage) +{ + assert(handle); + assert(transition); + auto journeyDesc = reinterpret_cast(handle); + + auto iter = journeyDesc->find(BaseStage::ID{id}); + if(iter != std::end(*journeyDesc)) { + iter->second = *reinterpret_cast(transition); + return true; + } + + if(errorMessage) { + *errorMessage = reinterpret_cast(new JPS_ErrorMessage_t{ + fmt::format("Could not set transition for given stage id {}. Stage not found.", id)}); + } + return false; +} + +void JPS_JourneyDescription_Free(JPS_JourneyDescription handle) +{ + delete reinterpret_cast(handle); +} +//////////////////////////////////////////////////////////////////////////////////////////////////// +/// JourneyTransition +//////////////////////////////////////////////////////////////////////////////////////////////////// +JPS_Transition +JPS_Transition_CreateFixedTransition(JPS_StageId stageId, JPS_ErrorMessage* errorMessage) +{ + JPS_Transition result{}; + try { + result = reinterpret_cast( + new TransitionDescription(FixedTransitionDescription{stageId})); + + } catch(const std::exception& ex) { + if(errorMessage) { + *errorMessage = reinterpret_cast(new JPS_ErrorMessage_t{ex.what()}); + } + } catch(...) { + if(errorMessage) { + *errorMessage = reinterpret_cast( + new JPS_ErrorMessage_t{"Unknown internal error."}); + } + } + return result; +} + +JPS_Transition JPS_Transition_CreateRoundRobinTransition( + JPS_StageId* stages, + uint64_t* weights, + size_t len, + JPS_ErrorMessage* errorMessage) +{ + JPS_Transition result{}; + std::vector> stageWeights; + stageWeights.reserve(len); + for(size_t i = 0; i < len; ++i) { + stageWeights.emplace_back(std::make_tuple(stages[i], weights[i])); + } + + try { + result = reinterpret_cast( + new TransitionDescription(RoundRobinTransitionDescription{stageWeights})); + } catch(const std::exception& ex) { + if(errorMessage) { + *errorMessage = reinterpret_cast(new JPS_ErrorMessage_t{ex.what()}); + } + } catch(...) { + if(errorMessage) { + *errorMessage = reinterpret_cast( + new JPS_ErrorMessage_t{"Unknown internal error."}); + } + } + return result; +} + +JPS_Transition JPS_Transition_CreateLeastTargetedTransition( + JPS_StageId* stages, + size_t len, + JPS_ErrorMessage* errorMessage) +{ + JPS_Transition result{}; + std::vector stageIds(stages, stages + len); + try { + result = reinterpret_cast( + new TransitionDescription(LeastTargetedTransitionDescription(std::move(stageIds)))); + } catch(const std::exception& ex) { + if(errorMessage) { + *errorMessage = reinterpret_cast(new JPS_ErrorMessage_t{ex.what()}); + } + } catch(...) { + if(errorMessage) { + *errorMessage = reinterpret_cast( + new JPS_ErrorMessage_t{"Unknown internal error."}); + } + } + return result; +} + +void JPS_Transition_Free(JPS_Transition handle) +{ + delete reinterpret_cast(handle); +} diff --git a/libjupedsim/src/jupedsim.cpp b/libjupedsim/src/jupedsim.cpp deleted file mode 100644 index d2a0152e15..0000000000 --- a/libjupedsim/src/jupedsim.cpp +++ /dev/null @@ -1,1621 +0,0 @@ -// Copyright © 2012-2023 Forschungszentrum Jülich GmbH -// SPDX-License-Identifier: LGPL-3.0-or-later -#include "jupedsim/jupedsim.h" - -#include "AgentIterator.hpp" -#include "CollisionFreeSpeedModelData.hpp" -#include "ErrorMessage.hpp" -#include "GeneralizedCentrifugalForceModelData.hpp" -#include "Journey.hpp" -#include "Stage.hpp" - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include -#include -#include -#include -#include -#include -#include -#include -#include - -using jupedsim::detail::intoJPS_Point; -using jupedsim::detail::intoPoint; -using jupedsim::detail::intoTuple; - -//////////////////////////////////////////////////////////////////////////////////////////////////// -/// BuildInfo -//////////////////////////////////////////////////////////////////////////////////////////////////// -JPS_BuildInfo JPS_GetBuildInfo() -{ - return JPS_BuildInfo{ - GIT_COMMIT_HASH.c_str(), - GIT_COMMIT_DATE.c_str(), - GIT_BRANCH.c_str(), - COMPILER.c_str(), - COMPILER_VERSION.c_str(), - LIBRARY_VERSION.c_str()}; -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// -/// Logging -//////////////////////////////////////////////////////////////////////////////////////////////////// -JUPEDSIM_API void JPS_Logging_SetDebugCallback(JPS_LoggingCallBack callback, void* userdata) -{ - if(callback) { - Logging::Logger::Instance().SetDebugCallback( - [callback, userdata](const std::string& msg) { callback(msg.c_str(), userdata); }); - } else { - Logging::Logger::Instance().ClearDebugCallback(); - } -} - -JUPEDSIM_API void JPS_Logging_SetInfoCallback(JPS_LoggingCallBack callback, void* userdata) -{ - if(callback) { - Logging::Logger::Instance().SetInfoCallback( - [callback, userdata](const std::string& msg) { callback(msg.c_str(), userdata); }); - } else { - Logging::Logger::Instance().ClearInfoCallback(); - } -} - -JUPEDSIM_API void JPS_Logging_SetWarningCallback(JPS_LoggingCallBack callback, void* userdata) -{ - if(callback) { - Logging::Logger::Instance().SetWarningCallback( - [callback, userdata](const std::string& msg) { callback(msg.c_str(), userdata); }); - } else { - Logging::Logger::Instance().ClearWarningCallback(); - } -} - -JUPEDSIM_API void JPS_Logging_SetErrorCallback(JPS_LoggingCallBack callback, void* userdata) -{ - if(callback) { - Logging::Logger::Instance().SetErrorCallback( - [callback, userdata](const std::string& msg) { callback(msg.c_str(), userdata); }); - } else { - Logging::Logger::Instance().ClearErrorCallback(); - } -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// -/// ErrorMessage -//////////////////////////////////////////////////////////////////////////////////////////////////// -const char* JPS_ErrorMessage_GetMessage(JPS_ErrorMessage handle) -{ - JPS_ErrorMessage_t* msg = reinterpret_cast(handle); - return msg->message.c_str(); -} - -void JPS_ErrorMessage_Free(JPS_ErrorMessage handle) -{ - delete reinterpret_cast(handle); -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// -/// Operational Model -//////////////////////////////////////////////////////////////////////////////////////////////////// -void JPS_OperationalModel_Free(JPS_OperationalModel handle) -{ - delete reinterpret_cast(handle); -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// -/// GeneralizedCentrifugalForceModel Model Builder -//////////////////////////////////////////////////////////////////////////////////////////////////// -JPS_GeneralizedCentrifugalForceModelBuilder JPS_GeneralizedCentrifugalForceModelBuilder_Create( - double strengthNeighborRepulsion, - double strengthGeometryRepulsion, - double maxNeighborInteractionDistance, - double maxGeometryInteractionDistance, - double maxNeighborInterpolationDistance, - double maxGeometryInterpolationDistance, - double maxNeighborRepulsionForce, - double maxGeometryRepulsionForce) -{ - return reinterpret_cast( - new GeneralizedCentrifugalForceModelBuilder( - strengthNeighborRepulsion, - strengthGeometryRepulsion, - maxNeighborInteractionDistance, - maxGeometryInteractionDistance, - maxNeighborInterpolationDistance, - maxGeometryInterpolationDistance, - maxNeighborRepulsionForce, - maxGeometryRepulsionForce)); -} - -JPS_OperationalModel JPS_GeneralizedCentrifugalForceModelBuilder_Build( - JPS_GeneralizedCentrifugalForceModelBuilder handle, - JPS_ErrorMessage* errorMessage) -{ - assert(handle != nullptr); - auto builder = reinterpret_cast(handle); - JPS_OperationalModel result{}; - try { - result = reinterpret_cast( - new GeneralizedCentrifugalForceModel(builder->Build())); - } catch(const std::exception& ex) { - if(errorMessage) { - *errorMessage = reinterpret_cast(new JPS_ErrorMessage_t{ex.what()}); - } - } catch(...) { - if(errorMessage) { - *errorMessage = reinterpret_cast( - new JPS_ErrorMessage_t{"Unknown internal error."}); - } - } - return result; -} - -void JPS_GeneralizedCentrifugalForceModelBuilder_Free( - JPS_GeneralizedCentrifugalForceModelBuilder handle) -{ - delete reinterpret_cast(handle); -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// -/// Collision Free Speed Model Builder -//////////////////////////////////////////////////////////////////////////////////////////////////// -JUPEDSIM_API JPS_CollisionFreeSpeedModelBuilder JPS_CollisionFreeSpeedModelBuilder_Create( - double strengthNeighborRepulsion, - double rangeNeighborRepulsion, - double strengthGeometryRepulsion, - double rangeGeometryRepulsion) -{ - return reinterpret_cast(new CollisionFreeSpeedModelBuilder( - strengthNeighborRepulsion, - rangeNeighborRepulsion, - strengthGeometryRepulsion, - rangeGeometryRepulsion)); -} - -JUPEDSIM_API JPS_OperationalModel JPS_CollisionFreeSpeedModelBuilder_Build( - JPS_CollisionFreeSpeedModelBuilder handle, - JPS_ErrorMessage* errorMessage) -{ - assert(handle != nullptr); - auto builder = reinterpret_cast(handle); - JPS_OperationalModel result{}; - try { - result = - reinterpret_cast(new CollisionFreeSpeedModel(builder->Build())); - } catch(const std::exception& ex) { - if(errorMessage) { - *errorMessage = reinterpret_cast(new JPS_ErrorMessage_t{ex.what()}); - } - } catch(...) { - if(errorMessage) { - *errorMessage = reinterpret_cast( - new JPS_ErrorMessage_t{"Unknown internal error."}); - } - } - return result; -} - -JUPEDSIM_API void JPS_CollisionFreeSpeedModelBuilder_Free(JPS_CollisionFreeSpeedModelBuilder handle) -{ - delete reinterpret_cast(handle); -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// -/// GeometryBuilder -//////////////////////////////////////////////////////////////////////////////////////////////////// - -JPS_GeometryBuilder JPS_GeometryBuilder_Create() -{ - return reinterpret_cast(new GeometryBuilder{}); -} - -void JPS_GeometryBuilder_AddAccessibleArea( - JPS_GeometryBuilder handle, - const JPS_Point* polygon, - size_t lenPolygon) -{ - assert(handle != nullptr); - auto builder = reinterpret_cast(handle); - std::vector loop{}; - loop.reserve(lenPolygon); - std::transform(polygon, polygon + lenPolygon, std::back_inserter(loop), intoPoint); - builder->AddAccessibleArea(loop); -} - -void JPS_GeometryBuilder_ExcludeFromAccessibleArea( - JPS_GeometryBuilder handle, - const JPS_Point* polygon, - size_t lenPolygon) -{ - assert(handle != nullptr); - auto builder = reinterpret_cast(handle); - std::vector loop{}; - loop.reserve(lenPolygon); - std::transform(polygon, polygon + lenPolygon, std::back_inserter(loop), intoPoint); - builder->ExcludeFromAccessibleArea(loop); -} - -JPS_Geometry JPS_GeometryBuilder_Build(JPS_GeometryBuilder handle, JPS_ErrorMessage* errorMessage) -{ - assert(handle != nullptr); - auto builder = reinterpret_cast(handle); - JPS_Geometry result{}; - try { - result = reinterpret_cast(new Geometry(builder->Build())); - } catch(const std::exception& ex) { - if(errorMessage) { - *errorMessage = reinterpret_cast(new JPS_ErrorMessage_t{ex.what()}); - } - } catch(...) { - if(errorMessage) { - *errorMessage = reinterpret_cast( - new JPS_ErrorMessage_t{"Unknown internal error."}); - } - } - return result; -} - -void JPS_GeometryBuilder_Free(JPS_GeometryBuilder handle) -{ - delete reinterpret_cast(handle); -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// -/// Geometry -//////////////////////////////////////////////////////////////////////////////////////////////////// -size_t JPS_Geometry_GetBoundarySize(JPS_Geometry handle) -{ - assert(handle); - const auto geo = reinterpret_cast(handle); - return std::get<0>(geo->collisionGeometry->AccessibleArea()).size(); -} - -const JPS_Point* JPS_Geometry_GetBoundaryData(JPS_Geometry handle) -{ - assert(handle); - const auto geo = reinterpret_cast(handle); - return reinterpret_cast( - std::get<0>(geo->collisionGeometry->AccessibleArea()).data()); -} - -size_t JPS_Geometry_GetHoleCount(JPS_Geometry handle) -{ - assert(handle); - const auto geo = reinterpret_cast(handle); - return std::get<1>(geo->collisionGeometry->AccessibleArea()).size(); -} - -size_t -JPS_Geometry_GetHoleSize(JPS_Geometry handle, size_t hole_index, JPS_ErrorMessage* errorMessage) -{ - assert(handle); - const auto geo = reinterpret_cast(handle); - try { - return std::get<1>(geo->collisionGeometry->AccessibleArea()).at(hole_index).size(); - } catch(const std::exception& ex) { - if(errorMessage) { - *errorMessage = reinterpret_cast(new JPS_ErrorMessage_t{ex.what()}); - } - } catch(...) { - if(errorMessage) { - *errorMessage = reinterpret_cast( - new JPS_ErrorMessage_t{"Unknown internal error."}); - } - } - return 0; -} - -const JPS_Point* -JPS_Geometry_GetHoleData(JPS_Geometry handle, size_t hole_index, JPS_ErrorMessage* errorMessage) -{ - assert(handle); - const auto geo = reinterpret_cast(handle); - try { - return reinterpret_cast( - std::get<1>(geo->collisionGeometry->AccessibleArea()).at(hole_index).data()); - } catch(const std::exception& ex) { - if(errorMessage) { - *errorMessage = reinterpret_cast(new JPS_ErrorMessage_t{ex.what()}); - } - } catch(...) { - if(errorMessage) { - *errorMessage = reinterpret_cast( - new JPS_ErrorMessage_t{"Unknown internal error."}); - } - } - return nullptr; -} - -void JPS_Geometry_Free(JPS_Geometry handle) -{ - delete reinterpret_cast(handle); -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// -/// GeneralizedCentrifugalForceModelState -//////////////////////////////////////////////////////////////////////////////////////////////////// -double -JPS_GeneralizedCentrifugalForceModelState_GetSpeed(JPS_GeneralizedCentrifugalForceModelState handle) -{ - assert(handle); - const auto state = reinterpret_cast(handle); - return state->speed; -} -void JPS_GeneralizedCentrifugalForceModelState_SetSpeed( - JPS_GeneralizedCentrifugalForceModelState handle, - double speed) -{ - assert(handle); - const auto state = reinterpret_cast(handle); - state->speed = speed; -} - -JPS_Point -JPS_GeneralizedCentrifugalForceModelState_GetE0(JPS_GeneralizedCentrifugalForceModelState handle) -{ - assert(handle); - const auto state = reinterpret_cast(handle); - return intoJPS_Point(state->e0); -} - -void JPS_GeneralizedCentrifugalForceModelState_SetE0( - JPS_GeneralizedCentrifugalForceModelState handle, - JPS_Point e0) -{ - assert(handle); - const auto state = reinterpret_cast(handle); - state->e0 = intoPoint(e0); -} - -double -JPS_GeneralizedCentrifugalForceModelState_GetMass(JPS_GeneralizedCentrifugalForceModelState handle) -{ - assert(handle); - const auto state = reinterpret_cast(handle); - return state->mass; -} - -void JPS_GeneralizedCentrifugalForceModelState_SetMass( - JPS_GeneralizedCentrifugalForceModelState handle, - double mass) -{ - assert(handle); - const auto state = reinterpret_cast(handle); - state->mass = mass; -} - -double -JPS_GeneralizedCentrifugalForceModelState_GetTau(JPS_GeneralizedCentrifugalForceModelState handle) -{ - assert(handle); - const auto state = reinterpret_cast(handle); - return state->tau; -} - -void JPS_GeneralizedCentrifugalForceModelState_SetTau( - JPS_GeneralizedCentrifugalForceModelState handle, - double tau) -{ - assert(handle); - const auto state = reinterpret_cast(handle); - state->tau = tau; -} - -double -JPS_GeneralizedCentrifugalForceModelState_GetV0(JPS_GeneralizedCentrifugalForceModelState handle) -{ - assert(handle); - const auto state = reinterpret_cast(handle); - return state->v0; -} - -void JPS_GeneralizedCentrifugalForceModelState_SetV0( - JPS_GeneralizedCentrifugalForceModelState handle, - double v0) -{ - assert(handle); - const auto state = reinterpret_cast(handle); - state->v0 = v0; -} - -double -JPS_GeneralizedCentrifugalForceModelState_GetAV(JPS_GeneralizedCentrifugalForceModelState handle) -{ - assert(handle); - const auto state = reinterpret_cast(handle); - return state->Av; -} - -void JPS_GeneralizedCentrifugalForceModelState_SetAV( - JPS_GeneralizedCentrifugalForceModelState handle, - double a_v) -{ - assert(handle); - const auto state = reinterpret_cast(handle); - state->Av = a_v; -} - -double -JPS_GeneralizedCentrifugalForceModelState_GetAMin(JPS_GeneralizedCentrifugalForceModelState handle) -{ - assert(handle); - const auto state = reinterpret_cast(handle); - return state->AMin; -} - -void JPS_GeneralizedCentrifugalForceModelState_SetAMin( - JPS_GeneralizedCentrifugalForceModelState handle, - double a_min) -{ - assert(handle); - const auto state = reinterpret_cast(handle); - state->AMin = a_min; -} - -double -JPS_GeneralizedCentrifugalForceModelState_GetBMin(JPS_GeneralizedCentrifugalForceModelState handle) -{ - assert(handle); - const auto state = reinterpret_cast(handle); - return state->BMin; -} - -void JPS_GeneralizedCentrifugalForceModelState_SetBMin( - JPS_GeneralizedCentrifugalForceModelState handle, - double b_min) -{ - assert(handle); - const auto state = reinterpret_cast(handle); - state->BMin = b_min; -} - -double -JPS_GeneralizedCentrifugalForceModelState_GetBMax(JPS_GeneralizedCentrifugalForceModelState handle) -{ - assert(handle); - const auto state = reinterpret_cast(handle); - return state->BMax; -} - -void JPS_GeneralizedCentrifugalForceModelState_SetBMax( - JPS_GeneralizedCentrifugalForceModelState handle, - double b_max) -{ - assert(handle); - const auto state = reinterpret_cast(handle); - state->BMax = b_max; -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// -/// CollisionFreeSpeedModelState -//////////////////////////////////////////////////////////////////////////////////////////////////// -double JPS_CollisionFreeSpeedModelState_GetTimeGap(JPS_CollisionFreeSpeedModelState handle) -{ - assert(handle); - const auto state = reinterpret_cast(handle); - return state->timeGap; -} - -void JPS_CollisionFreeSpeedModelState_SetTimeGap( - JPS_CollisionFreeSpeedModelState handle, - double time_gap) -{ - assert(handle); - auto state = reinterpret_cast(handle); - state->timeGap = time_gap; -} - -double JPS_CollisionFreeSpeedModelState_GetV0(JPS_CollisionFreeSpeedModelState handle) -{ - assert(handle); - const auto state = reinterpret_cast(handle); - return state->v0; -} - -void JPS_CollisionFreeSpeedModelState_SetV0(JPS_CollisionFreeSpeedModelState handle, double v0) -{ - assert(handle); - auto state = reinterpret_cast(handle); - state->v0 = v0; -} -double JPS_CollisionFreeSpeedModelState_GetRadius(JPS_CollisionFreeSpeedModelState handle) -{ - assert(handle); - const auto state = reinterpret_cast(handle); - return state->radius; -} - -void JPS_CollisionFreeSpeedModelState_SetRadius( - JPS_CollisionFreeSpeedModelState handle, - double radius) -{ - assert(handle); - auto state = reinterpret_cast(handle); - state->radius = radius; -} -//////////////////////////////////////////////////////////////////////////////////////////////////// -/// NotifiableQueueProxy -//////////////////////////////////////////////////////////////////////////////////////////////////// -size_t JPS_NotifiableQueueProxy_GetCountTargeting(JPS_NotifiableQueueProxy handle) -{ - assert(handle); - auto proxy = reinterpret_cast(handle); - return proxy->CountTargeting(); -} - -size_t JPS_NotifiableQueueProxy_GetCountEnqueued(JPS_NotifiableQueueProxy handle) -{ - assert(handle); - auto proxy = reinterpret_cast(handle); - return proxy->CountEnqueued(); -} - -void JPS_NotifiableQueueProxy_Pop(JPS_NotifiableQueueProxy handle, size_t count) -{ - assert(handle); - auto proxy = reinterpret_cast(handle); - proxy->Pop(count); -} - -size_t -JPS_NotifiableQueueProxy_GetEnqueued(JPS_NotifiableQueueProxy handle, const JPS_AgentId** data) -{ - assert(handle); - auto proxy = reinterpret_cast(handle); - const auto& agents = proxy->Enqueued(); - static_assert( - std::is_same::value, - "GenericAgentIDs cannot be casted in JPS_AgentId"); - *data = reinterpret_cast(agents.data()); - return agents.size(); -} - -void JPS_NotifiableQueueProxy_Free(JPS_NotifiableQueueProxy handle) -{ - delete reinterpret_cast(handle); -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// -/// WaitingSetProxy -//////////////////////////////////////////////////////////////////////////////////////////////////// -size_t JPS_WaitingSetProxy_GetCountTargeting(JPS_WaitingSetProxy handle) -{ - assert(handle); - auto proxy = reinterpret_cast(handle); - return proxy->CountTargeting(); -} - -size_t JPS_WaitingSetProxy_GetCountWaiting(JPS_WaitingSetProxy handle) -{ - assert(handle); - auto proxy = reinterpret_cast(handle); - return proxy->CountWaiting(); -} - -size_t JPS_WaitingSetProxy_GetWaiting(JPS_WaitingSetProxy handle, const JPS_AgentId** data) -{ - assert(handle); - auto proxy = reinterpret_cast(handle); - const auto& agents = proxy->Waiting(); - static_assert( - std::is_same::value, - "GenericAgentIDs cannot be casted in JPS_AgentId"); - *data = reinterpret_cast(agents.data()); - return agents.size(); -} - -void JPS_WaitingSetProxy_SetWaitingSetState( - JPS_WaitingSetProxy handle, - JPS_WaitingSetState newState) -{ - const auto convert = [](const auto s) { - switch(s) { - case JPS_WaitingSet_Active: - return WaitingSetState::Active; - case JPS_WaitingSet_Inactive: - return WaitingSetState::Inactive; - } - UNREACHABLE(); - }; - assert(handle); - auto proxy = reinterpret_cast(handle); - proxy->State(convert(newState)); -} - -JPS_WaitingSetState JPS_WaitingSetProxy_GetWaitingSetState(JPS_WaitingSetProxy handle) -{ - const auto convert = [](const auto s) { - switch(s) { - case WaitingSetState::Active: - return JPS_WaitingSet_Active; - case WaitingSetState::Inactive: - return JPS_WaitingSet_Inactive; - } - UNREACHABLE(); - }; - assert(handle); - auto proxy = reinterpret_cast(handle); - return convert(proxy->State()); -} - -void JPS_WaitingSetProxy_Free(JPS_WaitingSetProxy handle) -{ - delete reinterpret_cast(handle); -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// -/// DirectSteeringProxy -//////////////////////////////////////////////////////////////////////////////////////////////////// -void JPS_DirectSteeringProxy_Free(JPS_DirectSteeringProxy handle) -{ - delete reinterpret_cast(handle); -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// -/// WaitPointProxy -//////////////////////////////////////////////////////////////////////////////////////////////////// -size_t JPS_WaypointProxy_GetCountTargeting(JPS_WaypointProxy handle) -{ - assert(handle); - auto proxy = reinterpret_cast(handle); - return proxy->CountTargeting(); -} - -void JPS_WaypointProxy_Free(JPS_WaypointProxy handle) -{ - delete reinterpret_cast(handle); -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// -/// ExitProxy -//////////////////////////////////////////////////////////////////////////////////////////////////// -size_t JPS_ExitProxy_GetCountTargeting(JPS_ExitProxy handle) -{ - assert(handle); - auto proxy = reinterpret_cast(handle); - return proxy->CountTargeting(); -} - -void JPS_ExitProxy_Free(JPS_ExitProxy handle) -{ - delete reinterpret_cast(handle); -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// -/// Agent -//////////////////////////////////////////////////////////////////////////////////////////////////// -JPS_AgentId JPS_Agent_GetId(JPS_Agent handle) -{ - assert(handle); - const auto agent = reinterpret_cast(handle); - return agent->id.getID(); -} - -JPS_JourneyId JPS_Agent_GetJourneyId(JPS_Agent handle) -{ - assert(handle); - const auto agent = reinterpret_cast(handle); - return agent->journeyId.getID(); -} - -JPS_StageId JPS_Agent_GetStageId(JPS_Agent handle) -{ - assert(handle); - const auto agent = reinterpret_cast(handle); - return agent->stageId.getID(); -} - -JPS_Point JPS_Agent_GetPosition(JPS_Agent handle) -{ - assert(handle); - const auto agent = reinterpret_cast(handle); - return intoJPS_Point(agent->pos); -} - -JPS_Point JPS_Agent_GetTarget(JPS_Agent handle) -{ - assert(handle); - const auto agent = reinterpret_cast(handle); - return intoJPS_Point(agent->target); -} - -bool JPS_Agent_SetTarget(JPS_Agent handle, JPS_Point waypoint, JPS_ErrorMessage* errorMessage) -{ - assert(handle); - try { - auto agent = reinterpret_cast(handle); - agent->target = intoPoint(waypoint); - return true; - } catch(const std::exception& ex) { - if(errorMessage) { - *errorMessage = reinterpret_cast(new JPS_ErrorMessage_t{ex.what()}); - } - } catch(...) { - if(errorMessage) { - *errorMessage = reinterpret_cast( - new JPS_ErrorMessage_t{"Unknown internal error."}); - } - } - return false; -} - -JPS_Point JPS_Agent_GetOrientation(JPS_Agent handle) -{ - assert(handle); - const auto agent = reinterpret_cast(handle); - return intoJPS_Point(agent->orientation); -} - -JPS_ModelType JPS_Agent_GetModelType(JPS_Agent handle) -{ - assert(handle); - const auto agent = reinterpret_cast(handle); - switch(agent->model.index()) { - case 0: - return JPS_GeneralizedCentrifugalForceModel; - case 1: - return JPS_CollisionFreeSpeedModel; - } - UNREACHABLE(); -} - -JPS_GeneralizedCentrifugalForceModelState -JPS_Agent_GetGeneralizedCentrifugalForceModelState(JPS_Agent handle, JPS_ErrorMessage* errorMessage) -{ - assert(handle); - const auto agent = reinterpret_cast(handle); - try { - auto& model = std::get(agent->model); - return reinterpret_cast(&model); - } catch(const std::exception& ex) { - if(errorMessage) { - *errorMessage = reinterpret_cast(new JPS_ErrorMessage_t{ex.what()}); - } - } catch(...) { - if(errorMessage) { - *errorMessage = reinterpret_cast( - new JPS_ErrorMessage_t{"Unknown internal error."}); - } - } - return nullptr; -} - -JPS_CollisionFreeSpeedModelState -JPS_Agent_GetCollisionFreeSpeedModelState(JPS_Agent handle, JPS_ErrorMessage* errorMessage) -{ - assert(handle); - const auto agent = reinterpret_cast(handle); - try { - auto& model = std::get(agent->model); - return reinterpret_cast(&model); - } catch(const std::exception& ex) { - if(errorMessage) { - *errorMessage = reinterpret_cast(new JPS_ErrorMessage_t{ex.what()}); - } - } catch(...) { - if(errorMessage) { - *errorMessage = reinterpret_cast( - new JPS_ErrorMessage_t{"Unknown internal error."}); - } - } - return nullptr; -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// -/// AgentIterator -//////////////////////////////////////////////////////////////////////////////////////////////////// -JPS_Agent JPS_AgentIterator_Next(JPS_AgentIterator handle) -{ - assert(handle); - auto iterator = reinterpret_cast*>(handle); - return reinterpret_cast(iterator->Next()); -} - -void JPS_AgentIterator_Free(JPS_AgentIterator handle) -{ - delete reinterpret_cast*>(handle); -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// -/// AgentIdIterator -//////////////////////////////////////////////////////////////////////////////////////////////////// -struct AgentIdIterator { - using AgentIds = std::vector; - AgentIds ids; - AgentIds::const_iterator iter; - - AgentIdIterator(AgentIds&& ids_) : ids(std::move(ids_)) { iter = std::begin(ids); } -}; - -JPS_AgentId JPS_AgentIdIterator_Next(JPS_AgentIdIterator handle) -{ - assert(handle); - auto iterator = reinterpret_cast(handle); - auto& [vec, iter] = *iterator; - if(iter == std::end(vec)) { - return GenericAgent::ID::Invalid.getID(); - } - const auto id = *iter; - ++iter; - return id.getID(); -} - -void JPS_AgentIdIterator_Free(JPS_AgentIdIterator handle) -{ - delete reinterpret_cast(handle); -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// -/// JourneyDescription -//////////////////////////////////////////////////////////////////////////////////////////////////// -using JourneyDesc = std::map; - -JPS_JourneyDescription JPS_JourneyDescription_Create() -{ - return reinterpret_cast(new JourneyDesc{}); -} - -void JPS_JourneyDescription_AddStage(JPS_JourneyDescription handle, JPS_StageId id) -{ - assert(handle); - auto journeyDesc = reinterpret_cast(handle); - (*journeyDesc)[BaseStage::ID{id}] = NonTransitionDescription{}; -} - -bool JPS_JourneyDescription_SetTransitionForStage( - JPS_JourneyDescription handle, - JPS_StageId id, - JPS_Transition transition, - JPS_ErrorMessage* errorMessage) -{ - assert(handle); - assert(transition); - auto journeyDesc = reinterpret_cast(handle); - - auto iter = journeyDesc->find(BaseStage::ID{id}); - if(iter != std::end(*journeyDesc)) { - iter->second = *reinterpret_cast(transition); - return true; - } - - if(errorMessage) { - *errorMessage = reinterpret_cast(new JPS_ErrorMessage_t{ - fmt::format("Could not set transition for given stage id {}. Stage not found.", id)}); - } - return false; -} - -void JPS_JourneyDescription_Free(JPS_JourneyDescription handle) -{ - delete reinterpret_cast(handle); -} -//////////////////////////////////////////////////////////////////////////////////////////////////// -/// JourneyTransition -//////////////////////////////////////////////////////////////////////////////////////////////////// -JPS_Transition -JPS_Transition_CreateFixedTransition(JPS_StageId stageId, JPS_ErrorMessage* errorMessage) -{ - JPS_Transition result{}; - try { - result = reinterpret_cast( - new TransitionDescription(FixedTransitionDescription{stageId})); - - } catch(const std::exception& ex) { - if(errorMessage) { - *errorMessage = reinterpret_cast(new JPS_ErrorMessage_t{ex.what()}); - } - } catch(...) { - if(errorMessage) { - *errorMessage = reinterpret_cast( - new JPS_ErrorMessage_t{"Unknown internal error."}); - } - } - return result; -} - -JPS_Transition JPS_Transition_CreateRoundRobinTransition( - JPS_StageId* stages, - uint64_t* weights, - size_t len, - JPS_ErrorMessage* errorMessage) -{ - JPS_Transition result{}; - std::vector> stageWeights; - stageWeights.reserve(len); - for(size_t i = 0; i < len; ++i) { - stageWeights.emplace_back(std::make_tuple(stages[i], weights[i])); - } - - try { - result = reinterpret_cast( - new TransitionDescription(RoundRobinTransitionDescription{stageWeights})); - } catch(const std::exception& ex) { - if(errorMessage) { - *errorMessage = reinterpret_cast(new JPS_ErrorMessage_t{ex.what()}); - } - } catch(...) { - if(errorMessage) { - *errorMessage = reinterpret_cast( - new JPS_ErrorMessage_t{"Unknown internal error."}); - } - } - return result; -} - -JPS_Transition JPS_Transition_CreateLeastTargetedTransition( - JPS_StageId* stages, - size_t len, - JPS_ErrorMessage* errorMessage) -{ - JPS_Transition result{}; - std::vector stageIds(stages, stages + len); - try { - result = reinterpret_cast( - new TransitionDescription(LeastTargetedTransitionDescription(std::move(stageIds)))); - } catch(const std::exception& ex) { - if(errorMessage) { - *errorMessage = reinterpret_cast(new JPS_ErrorMessage_t{ex.what()}); - } - } catch(...) { - if(errorMessage) { - *errorMessage = reinterpret_cast( - new JPS_ErrorMessage_t{"Unknown internal error."}); - } - } - return result; -} - -void JPS_Transition_Free(JPS_Transition handle) -{ - delete reinterpret_cast(handle); -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// -/// Simulation -//////////////////////////////////////////////////////////////////////////////////////////////////// -JPS_Simulation JPS_Simulation_Create( - JPS_OperationalModel model, - JPS_Geometry geometry, - double dT, - JPS_ErrorMessage* errorMessage) -{ - assert(model); - assert(geometry); - JPS_Simulation result{}; - try { - auto geometryInternal = reinterpret_cast(geometry); - auto collisionGeometry = - std::make_unique(*geometryInternal->collisionGeometry); - auto routingEngine = geometryInternal->routingEngine->Clone(); - - auto modelInternal = reinterpret_cast(model); - auto model = modelInternal->Clone(); - result = reinterpret_cast(new Simulation( - std::move(model), std::move(collisionGeometry), std::move(routingEngine), dT)); - } catch(const std::exception& ex) { - if(errorMessage) { - *errorMessage = reinterpret_cast(new JPS_ErrorMessage_t{ex.what()}); - } - } catch(...) { - if(errorMessage) { - *errorMessage = reinterpret_cast( - new JPS_ErrorMessage_t{"Unknown internal error."}); - } - } - return result; -} - -JPS_JourneyId JPS_Simulation_AddJourney( - JPS_Simulation handle, - JPS_JourneyDescription journey, - JPS_ErrorMessage* errorMessage) -{ - assert(handle); - assert(journey); - - auto simulation = reinterpret_cast(handle); - auto journeyInternal = reinterpret_cast(journey); - auto result = Journey::ID::Invalid.getID(); - try { - result = simulation->AddJourney(*journeyInternal).getID(); - } catch(const std::exception& ex) { - if(errorMessage) { - *errorMessage = reinterpret_cast(new JPS_ErrorMessage_t{ex.what()}); - } - } catch(...) { - if(errorMessage) { - *errorMessage = reinterpret_cast( - new JPS_ErrorMessage_t{"Unknown internal error."}); - } - } - return result; -} - -static JPS_StageId add_stage( - JPS_Simulation handle, - const StageDescription& stageDescription, - JPS_ErrorMessage* errorMessage) -{ - assert(handle); - - auto simulation = reinterpret_cast(handle); - auto result = Journey::ID::Invalid.getID(); - try { - - result = simulation->AddStage(stageDescription).getID(); - } catch(const std::exception& ex) { - if(errorMessage) { - *errorMessage = reinterpret_cast(new JPS_ErrorMessage_t{ex.what()}); - } - } catch(...) { - if(errorMessage) { - *errorMessage = reinterpret_cast( - new JPS_ErrorMessage_t{"Unknown internal error."}); - } - } - return result; -} - -JPS_StageId JPS_Simulation_AddStageWaypoint( - JPS_Simulation handle, - JPS_Point position, - double distance, - JPS_ErrorMessage* errorMessage) -{ - return add_stage(handle, WaypointDescription{intoPoint(position), distance}, errorMessage); -} - -JPS_StageId JPS_Simulation_AddStageExit( - JPS_Simulation handle, - const JPS_Point* polygon, - size_t len_polygon, - JPS_ErrorMessage* errorMessage) -{ - std::vector loop{}; - loop.reserve(len_polygon); - std::transform(polygon, polygon + len_polygon, std::back_inserter(loop), intoPoint); - return add_stage(handle, ExitDescription{Polygon(loop)}, errorMessage); -} - -JPS_StageId JPS_Simulation_AddStageNotifiableQueue( - JPS_Simulation handle, - const JPS_Point* waiting_positions, - size_t len_waiting_positions, - JPS_ErrorMessage* errorMessage) -{ - std::vector positions{}; - positions.reserve(len_waiting_positions); - std::transform( - waiting_positions, - waiting_positions + len_waiting_positions, - std::back_inserter(positions), - intoPoint); - return add_stage(handle, NotifiableQueueDescription{positions}, errorMessage); -} - -JPS_StageId JPS_Simulation_AddStageWaitingSet( - JPS_Simulation handle, - const JPS_Point* waiting_positions, - size_t len_waiting_positions, - JPS_ErrorMessage* errorMessage) -{ - std::vector positions{}; - positions.reserve(len_waiting_positions); - std::transform( - waiting_positions, - waiting_positions + len_waiting_positions, - std::back_inserter(positions), - intoPoint); - return add_stage(handle, NotifiableWaitingSetDescription{positions}, errorMessage); -} - -JPS_StageId -JPS_Simulation_AddStageDirectSteering(JPS_Simulation handle, JPS_ErrorMessage* errorMessage) -{ - return add_stage(handle, DirectSteeringDescription{}, errorMessage); -} - -JPS_AgentId JPS_Simulation_AddGeneralizedCentrifugalForceModelAgent( - JPS_Simulation handle, - JPS_GeneralizedCentrifugalForceModelAgentParameters parameters, - JPS_ErrorMessage* errorMessage) -{ - assert(handle); - auto result = GenericAgent::ID::Invalid; - auto simulation = reinterpret_cast(handle); - try { - if(simulation->ModelType() != OperationalModelType::GENERALIZED_CENTRIFUGAL_FORCE) { - throw std::runtime_error( - "Simulation is not configured to use Generalized Centrifugal Force Model"); - } - GenericAgent agent{ - GenericAgent::ID::Invalid, - Journey::ID(parameters.journeyId), - BaseStage::ID(parameters.stageId), - intoPoint(parameters.position), - intoPoint(parameters.orientation), - GeneralizedCentrifugalForceModelData{ - parameters.speed, - intoPoint(parameters.e0), - 0, - parameters.mass, - parameters.tau, - parameters.v0, - parameters.a_v, - parameters.a_min, - parameters.b_min, - parameters.b_max}}; - result = simulation->AddAgent(std::move(agent)); - } catch(const std::exception& ex) { - if(errorMessage) { - *errorMessage = reinterpret_cast(new JPS_ErrorMessage_t{ex.what()}); - } - } catch(...) { - if(errorMessage) { - *errorMessage = reinterpret_cast( - new JPS_ErrorMessage_t{"Unknown internal error."}); - } - } - return result.getID(); -} - -JPS_AgentId JPS_Simulation_AddCollisionFreeSpeedModelAgent( - JPS_Simulation handle, - JPS_CollisionFreeSpeedModelAgentParameters parameters, - JPS_ErrorMessage* errorMessage) -{ - assert(handle); - auto result = GenericAgent::ID::Invalid; - auto simulation = reinterpret_cast(handle); - try { - if(simulation->ModelType() != OperationalModelType::COLLISION_FREE_SPEED) { - throw std::runtime_error( - "Simulation is not configured to use Collision Free Speed Model"); - } - GenericAgent agent( - GenericAgent::ID::Invalid, - Journey::ID(parameters.journeyId), - BaseStage::ID(parameters.stageId), - intoPoint(parameters.position), - {}, - CollisionFreeSpeedModelData{parameters.time_gap, parameters.v0, parameters.radius}); - result = simulation->AddAgent(std::move(agent)); - } catch(const std::exception& ex) { - if(errorMessage) { - *errorMessage = reinterpret_cast(new JPS_ErrorMessage_t{ex.what()}); - } - } catch(...) { - if(errorMessage) { - *errorMessage = reinterpret_cast( - new JPS_ErrorMessage_t{"Unknown internal error."}); - } - } - return result.getID(); -} - -bool JPS_Simulation_MarkAgentForRemoval( - JPS_Simulation handle, - JPS_AgentId agentId, - JPS_ErrorMessage* errorMessage) -{ - assert(handle); - auto simulation = reinterpret_cast(handle); - bool result{false}; - try { - simulation->MarkAgentForRemoval(agentId); - result = true; - } catch(const std::exception& ex) { - if(errorMessage) { - *errorMessage = reinterpret_cast(new JPS_ErrorMessage_t{ex.what()}); - } - } catch(...) { - if(errorMessage) { - *errorMessage = reinterpret_cast( - new JPS_ErrorMessage_t{"Unknown internal error."}); - } - } - return result; -} - -size_t JPS_Simulation_RemovedAgents(JPS_Simulation handle, const JPS_AgentId** data) -{ - assert(handle); - auto simulation = reinterpret_cast(handle); - const auto& agents = simulation->RemovedAgents(); - static_assert( - std::is_same::value, - "GenericAgentIDs cannot be casted in JPS_AgentId"); - *data = reinterpret_cast(agents.data()); - return agents.size(); -} - -bool JPS_Simulation_Iterate(JPS_Simulation handle, JPS_ErrorMessage* errorMessage) -{ - assert(handle); - auto simulation = reinterpret_cast(handle); - bool result = false; - try { - simulation->Iterate(); - result = true; - } catch(const std::exception& ex) { - if(errorMessage) { - *errorMessage = reinterpret_cast(new JPS_ErrorMessage_t{ex.what()}); - } - } catch(...) { - if(errorMessage) { - *errorMessage = reinterpret_cast( - new JPS_ErrorMessage_t{"Unknown internal error."}); - } - } - return result; -} - -size_t JPS_Simulation_AgentCount(JPS_Simulation handle) -{ - assert(handle); - auto simulation = reinterpret_cast(handle); - return simulation->AgentCount(); -} - -double JPS_Simulation_ElapsedTime(JPS_Simulation handle) -{ - assert(handle); - auto simulation = reinterpret_cast(handle); - return simulation->ElapsedTime(); -} - -double JPS_Simulation_DeltaTime(JPS_Simulation handle) -{ - assert(handle); - auto simulation = reinterpret_cast(handle); - return simulation->DT(); -} - -uint64_t JPS_Simulation_IterationCount(JPS_Simulation handle) -{ - assert(handle); - auto simulation = reinterpret_cast(handle); - return simulation->Iteration(); -} - -JPS_AgentIterator JPS_Simulation_AgentIterator(JPS_Simulation handle) -{ - assert(handle); - auto simulation = reinterpret_cast(handle); - return reinterpret_cast( - new AgentIterator(simulation->Agents())); -} - -JPS_Agent -JPS_Simulation_GetAgent(JPS_Simulation handle, JPS_AgentId agentId, JPS_ErrorMessage* errorMessage) -{ - assert(handle); - const auto simulation = reinterpret_cast(handle); - - try { - const auto agent = &simulation->Agent(agentId); - return reinterpret_cast(agent); - } catch(const std::exception& ex) { - if(errorMessage) { - *errorMessage = reinterpret_cast(new JPS_ErrorMessage_t{ex.what()}); - } - } catch(...) { - if(errorMessage) { - *errorMessage = reinterpret_cast( - new JPS_ErrorMessage_t{"Unknown internal error."}); - } - } - return nullptr; -} - -bool JPS_Simulation_SwitchAgentJourney( - JPS_Simulation handle, - JPS_AgentId agentId, - JPS_JourneyId journeyId, - JPS_StageId stageId, - JPS_ErrorMessage* errorMessage) -{ - assert(handle); - const auto simulation = reinterpret_cast(handle); - bool result = false; - try { - simulation->SwitchAgentJourney(agentId, journeyId, stageId); - result = true; - } catch(const std::exception& ex) { - if(errorMessage) { - *errorMessage = reinterpret_cast(new JPS_ErrorMessage_t{ex.what()}); - } - } catch(...) { - if(errorMessage) { - *errorMessage = reinterpret_cast( - new JPS_ErrorMessage_t{"Unknown internal error."}); - } - } - return result; -} - -JPS_ModelType JPS_Simulation_ModelType(JPS_Simulation handle) -{ - assert(handle); - const auto simulation = reinterpret_cast(handle); - const auto type = simulation->ModelType(); - switch(type) { - case OperationalModelType::COLLISION_FREE_SPEED: - return JPS_CollisionFreeSpeedModel; - case OperationalModelType::GENERALIZED_CENTRIFUGAL_FORCE: - return JPS_GeneralizedCentrifugalForceModel; - } - UNREACHABLE(); -} - -JPS_AgentIdIterator -JPS_Simulation_AgentsInRange(JPS_Simulation handle, JPS_Point position, double distance) -{ - assert(handle); - auto simulation = reinterpret_cast(handle); - return reinterpret_cast( - new AgentIdIterator(simulation->AgentsInRange(intoPoint(position), distance))); -} - -JPS_AgentIdIterator -JPS_Simulation_AgentsInPolygon(JPS_Simulation handle, const JPS_Point* polygon, size_t len_polygon) -{ - assert(handle); - auto simulation = reinterpret_cast(handle); - std::vector poly{}; - poly.reserve(len_polygon); - std::transform(polygon, polygon + len_polygon, std::back_inserter(poly), intoPoint); - return reinterpret_cast( - new AgentIdIterator(simulation->AgentsInPolygon(poly))); -} - -JPS_StageType JPS_Simulation_GetStageType(JPS_Simulation handle, JPS_StageId id) -{ - assert(handle); - auto simulation = reinterpret_cast(handle); - const auto convert = [](const auto t) { - switch(t) { - case 0: - return JPS_WaypointType; - case 1: - return JPS_WaitingSetType; - case 2: - return JPS_NotifiableQueueType; - case 3: - return JPS_ExitType; - case 4: - return JPS_DirectSteeringType; - } - UNREACHABLE(); - }; - return convert(simulation->Stage(id).index()); -} - -JPS_NotifiableQueueProxy JPS_Simulation_GetNotifiableQueueProxy( - JPS_Simulation handle, - JPS_StageId stageId, - JPS_ErrorMessage* errorMessage) -{ - assert(handle); - auto simulation = reinterpret_cast(handle); - try { - return reinterpret_cast( - new NotifiableQueueProxy(std::get(simulation->Stage(stageId)))); - } catch(const std::exception& ex) { - if(errorMessage) { - *errorMessage = reinterpret_cast(new JPS_ErrorMessage_t{ex.what()}); - } - return nullptr; - } -} -JUPEDSIM_API JPS_WaitingSetProxy JPS_Simulation_GetWaitingSetProxy( - JPS_Simulation handle, - JPS_StageId stageId, - JPS_ErrorMessage* errorMessage) -{ - assert(handle); - auto simulation = reinterpret_cast(handle); - try { - return reinterpret_cast(new NotifiableWaitingSetProxy( - std::get(simulation->Stage(stageId)))); - } catch(const std::exception& ex) { - if(errorMessage) { - *errorMessage = reinterpret_cast(new JPS_ErrorMessage_t{ex.what()}); - } - return nullptr; - } -} - -JUPEDSIM_API JPS_WaypointProxy JPS_Simulation_GetWaypointProxy( - JPS_Simulation handle, - JPS_StageId stageId, - JPS_ErrorMessage* errorMessage) -{ - assert(handle); - auto simulation = reinterpret_cast(handle); - try { - return reinterpret_cast( - new WaypointProxy(std::get(simulation->Stage(stageId)))); - } catch(const std::exception& ex) { - if(errorMessage) { - *errorMessage = reinterpret_cast(new JPS_ErrorMessage_t{ex.what()}); - } - return nullptr; - } -} - -JUPEDSIM_API JPS_ExitProxy JPS_Simulation_GetExitProxy( - JPS_Simulation handle, - JPS_StageId stageId, - JPS_ErrorMessage* errorMessage) -{ - assert(handle); - auto simulation = reinterpret_cast(handle); - try { - return reinterpret_cast( - new ExitProxy(std::get(simulation->Stage(stageId)))); - } catch(const std::exception& ex) { - if(errorMessage) { - *errorMessage = reinterpret_cast(new JPS_ErrorMessage_t{ex.what()}); - } - return nullptr; - } -} - -JPS_DirectSteeringProxy JPS_Simulation_GetDirectSteeringProxy( - JPS_Simulation handle, - JPS_StageId stageId, - JPS_ErrorMessage* errorMessage) -{ - assert(handle); - auto simulation = reinterpret_cast(handle); - try { - return reinterpret_cast( - new DirectSteeringProxy(std::get(simulation->Stage(stageId)))); - } catch(const std::exception& ex) { - if(errorMessage) { - *errorMessage = reinterpret_cast(new JPS_ErrorMessage_t{ex.what()}); - } - return nullptr; - } -} - -void JPS_Simulation_SetTracing(JPS_Simulation handle, bool status) -{ - assert(handle); - auto simuation = reinterpret_cast(handle); - simuation->SetTracing(status); -} - -JPS_Trace JPS_Simulation_GetTrace(JPS_Simulation handle) -{ - assert(handle); - auto simuation = reinterpret_cast(handle); - const auto stats = simuation->GetLastStats(); - return JPS_Trace{stats.IterationDuration(), stats.OpDecSystemRunDuration()}; -} - -JPS_Geometry JPS_Simulation_GetGeometry(JPS_Simulation handle) -{ - assert(handle); - const auto simulation = reinterpret_cast(handle); - return reinterpret_cast(new Geometry(simulation->Geo())); -} - -void JPS_Simulation_Free(JPS_Simulation handle) -{ - delete reinterpret_cast(handle); -} - -//////////////////////////////////////////////////////////////////////////////// -/// JPS_Path -//////////////////////////////////////////////////////////////////////////////// -JUPEDSIM_API void JPS_Path_Free(JPS_Path* path) -{ - delete[] path->points; - path->points = nullptr; - path->len = 0; -} - -//////////////////////////////////////////////////////////////////////////////// -/// JPS_TriangleMesh -//////////////////////////////////////////////////////////////////////////////// -JUPEDSIM_API void JPS_TriangleMesh_Free(JPS_TriangleMesh* mesh) -{ - delete[] mesh->triangles; - mesh->triangles = nullptr; - mesh->len = 0; -} - -//////////////////////////////////////////////////////////////////////////////// -/// JPS_Lines -//////////////////////////////////////////////////////////////////////////////// -JUPEDSIM_API void JPS_Lines_Free(JPS_Lines* lines) -{ - delete[] lines->lines; - lines->lines = nullptr; - lines->len = 0; -} - -//////////////////////////////////////////////////////////////////////////////// -/// JPS_RoutingEngine -//////////////////////////////////////////////////////////////////////////////// -JUPEDSIM_API JPS_RoutingEngine JPS_RoutingEngine_Create(JPS_Geometry geometry) -{ - auto* geo = reinterpret_cast(geometry); - return reinterpret_cast(geo->routingEngine->Clone().release()); -} - -JUPEDSIM_API JPS_Path -JPS_RoutingEngine_ComputeWaypoint(JPS_RoutingEngine handle, JPS_Point from, JPS_Point to) -{ - auto* engine = reinterpret_cast(handle); - const auto path = engine->ComputeAllWaypoints(intoPoint(from), intoPoint(to)); - auto points = new JPS_Point[path.size()]; - JPS_Path p{path.size(), points}; - std::transform( - std::begin(path), std::end(path), points, [](const auto& p) { return intoJPS_Point(p); }); - return p; -} - -JUPEDSIM_API bool JPS_RoutingEngine_IsRoutable(JPS_RoutingEngine handle, JPS_Point p) -{ - const auto* engine = reinterpret_cast(handle); - return engine->IsRoutable(intoPoint(p)); -} - -JUPEDSIM_API JPS_TriangleMesh JPS_RoutingEngine_Mesh(JPS_RoutingEngine handle) -{ - const auto* engine = reinterpret_cast(handle); - const auto res = engine->Mesh(); - JPS_TriangleMesh mesh{res.size(), new JPS_Triangle[res.size()]}; - std::transform(std::begin(res), std::end(res), mesh.triangles, [](const auto& t) { - auto tri = JPS_Triangle{}; - tri.points[0] = intoJPS_Point(t.points[0]); - tri.points[1] = intoJPS_Point(t.points[1]); - tri.points[2] = intoJPS_Point(t.points[2]); - return tri; - }); - return mesh; -} - -JUPEDSIM_API JPS_Lines JPS_RoutingEngine_EdgesFor(JPS_RoutingEngine handle, uint32_t id) -{ - const auto* engine = reinterpret_cast(handle); - const auto res = engine->EdgesFor(id); - - JPS_Lines lines{}; - lines.lines = new JPS_Line[res.size()]; - lines.len = res.size(); - std::transform(std::begin(res), std::end(res), lines.lines, [](const auto& edge) { - JPS_Line line{}; - line.points[0] = intoJPS_Point(edge.edge.p1); - line.points[1] = intoJPS_Point(edge.edge.p2); - return line; - }); - return lines; -} - -JUPEDSIM_API void JPS_RoutingEngine_Free(JPS_RoutingEngine handle) -{ - delete reinterpret_cast(handle); -} diff --git a/libjupedsim/src/logging.cpp b/libjupedsim/src/logging.cpp new file mode 100644 index 0000000000..d2eae66631 --- /dev/null +++ b/libjupedsim/src/logging.cpp @@ -0,0 +1,48 @@ +// Copyright © 2012-2023 Forschungszentrum Jülich GmbH +// SPDX-License-Identifier: LGPL-3.0-or-later +#include "jupedsim/logging.h" + +#include + +//////////////////////////////////////////////////////////////////////////////////////////////////// +/// Logging +//////////////////////////////////////////////////////////////////////////////////////////////////// +JUPEDSIM_API void JPS_Logging_SetDebugCallback(JPS_LoggingCallBack callback, void* userdata) +{ + if(callback) { + Logging::Logger::Instance().SetDebugCallback( + [callback, userdata](const std::string& msg) { callback(msg.c_str(), userdata); }); + } else { + Logging::Logger::Instance().ClearDebugCallback(); + } +} + +JUPEDSIM_API void JPS_Logging_SetInfoCallback(JPS_LoggingCallBack callback, void* userdata) +{ + if(callback) { + Logging::Logger::Instance().SetInfoCallback( + [callback, userdata](const std::string& msg) { callback(msg.c_str(), userdata); }); + } else { + Logging::Logger::Instance().ClearInfoCallback(); + } +} + +JUPEDSIM_API void JPS_Logging_SetWarningCallback(JPS_LoggingCallBack callback, void* userdata) +{ + if(callback) { + Logging::Logger::Instance().SetWarningCallback( + [callback, userdata](const std::string& msg) { callback(msg.c_str(), userdata); }); + } else { + Logging::Logger::Instance().ClearWarningCallback(); + } +} + +JUPEDSIM_API void JPS_Logging_SetErrorCallback(JPS_LoggingCallBack callback, void* userdata) +{ + if(callback) { + Logging::Logger::Instance().SetErrorCallback( + [callback, userdata](const std::string& msg) { callback(msg.c_str(), userdata); }); + } else { + Logging::Logger::Instance().ClearErrorCallback(); + } +} diff --git a/libjupedsim/src/operational_model.cpp b/libjupedsim/src/operational_model.cpp new file mode 100644 index 0000000000..0305edf89c --- /dev/null +++ b/libjupedsim/src/operational_model.cpp @@ -0,0 +1,13 @@ +// Copyright © 2012-2023 Forschungszentrum Jülich GmbH +// SPDX-License-Identifier: LGPL-3.0-or-later +#include "jupedsim/operational_model.h" + +#include + +//////////////////////////////////////////////////////////////////////////////////////////////////// +/// Operational Model +//////////////////////////////////////////////////////////////////////////////////////////////////// +void JPS_OperationalModel_Free(JPS_OperationalModel handle) +{ + delete reinterpret_cast(handle); +} diff --git a/libjupedsim/src/routing.cpp b/libjupedsim/src/routing.cpp new file mode 100644 index 0000000000..3458c58329 --- /dev/null +++ b/libjupedsim/src/routing.cpp @@ -0,0 +1,106 @@ +// Copyright © 2012-2023 Forschungszentrum Jülich GmbH +// SPDX-License-Identifier: LGPL-3.0-or-later +#include "jupedsim/routing.h" + +#include "Conversion.hpp" + +#include +#include + +using jupedsim::detail::intoJPS_Point; +using jupedsim::detail::intoPoint; +using jupedsim::detail::intoTuple; + +//////////////////////////////////////////////////////////////////////////////// +/// JPS_Path +//////////////////////////////////////////////////////////////////////////////// +JUPEDSIM_API void JPS_Path_Free(JPS_Path* path) +{ + delete[] path->points; + path->points = nullptr; + path->len = 0; +} + +//////////////////////////////////////////////////////////////////////////////// +/// JPS_TriangleMesh +//////////////////////////////////////////////////////////////////////////////// +JUPEDSIM_API void JPS_TriangleMesh_Free(JPS_TriangleMesh* mesh) +{ + delete[] mesh->triangles; + mesh->triangles = nullptr; + mesh->len = 0; +} + +//////////////////////////////////////////////////////////////////////////////// +/// JPS_Lines +//////////////////////////////////////////////////////////////////////////////// +JUPEDSIM_API void JPS_Lines_Free(JPS_Lines* lines) +{ + delete[] lines->lines; + lines->lines = nullptr; + lines->len = 0; +} + +//////////////////////////////////////////////////////////////////////////////// +/// JPS_RoutingEngine +//////////////////////////////////////////////////////////////////////////////// +JUPEDSIM_API JPS_RoutingEngine JPS_RoutingEngine_Create(JPS_Geometry geometry) +{ + auto* geo = reinterpret_cast(geometry); + return reinterpret_cast(geo->routingEngine->Clone().release()); +} + +JUPEDSIM_API JPS_Path +JPS_RoutingEngine_ComputeWaypoint(JPS_RoutingEngine handle, JPS_Point from, JPS_Point to) +{ + auto* engine = reinterpret_cast(handle); + const auto path = engine->ComputeAllWaypoints(intoPoint(from), intoPoint(to)); + auto points = new JPS_Point[path.size()]; + JPS_Path p{path.size(), points}; + std::transform( + std::begin(path), std::end(path), points, [](const auto& p) { return intoJPS_Point(p); }); + return p; +} + +JUPEDSIM_API bool JPS_RoutingEngine_IsRoutable(JPS_RoutingEngine handle, JPS_Point p) +{ + const auto* engine = reinterpret_cast(handle); + return engine->IsRoutable(intoPoint(p)); +} + +JUPEDSIM_API JPS_TriangleMesh JPS_RoutingEngine_Mesh(JPS_RoutingEngine handle) +{ + const auto* engine = reinterpret_cast(handle); + const auto res = engine->Mesh(); + JPS_TriangleMesh mesh{res.size(), new JPS_Triangle[res.size()]}; + std::transform(std::begin(res), std::end(res), mesh.triangles, [](const auto& t) { + auto tri = JPS_Triangle{}; + tri.points[0] = intoJPS_Point(t.points[0]); + tri.points[1] = intoJPS_Point(t.points[1]); + tri.points[2] = intoJPS_Point(t.points[2]); + return tri; + }); + return mesh; +} + +JUPEDSIM_API JPS_Lines JPS_RoutingEngine_EdgesFor(JPS_RoutingEngine handle, uint32_t id) +{ + const auto* engine = reinterpret_cast(handle); + const auto res = engine->EdgesFor(id); + + JPS_Lines lines{}; + lines.lines = new JPS_Line[res.size()]; + lines.len = res.size(); + std::transform(std::begin(res), std::end(res), lines.lines, [](const auto& edge) { + JPS_Line line{}; + line.points[0] = intoJPS_Point(edge.edge.p1); + line.points[1] = intoJPS_Point(edge.edge.p2); + return line; + }); + return lines; +} + +JUPEDSIM_API void JPS_RoutingEngine_Free(JPS_RoutingEngine handle) +{ + delete reinterpret_cast(handle); +} diff --git a/libjupedsim/src/simulation.cpp b/libjupedsim/src/simulation.cpp new file mode 100644 index 0000000000..59a092a619 --- /dev/null +++ b/libjupedsim/src/simulation.cpp @@ -0,0 +1,558 @@ +// Copyright © 2012-2023 Forschungszentrum Jülich GmbH +// SPDX-License-Identifier: LGPL-3.0-or-later +#include "jupedsim/simulation.h" + +#include "jupedsim/agent.h" +#include "jupedsim/error.h" + +#include "AgentIterator.hpp" +#include "Conversion.hpp" +#include "ErrorMessage.hpp" +#include "JourneyDescription.hpp" + +#include +#include +#include + +#include + +using jupedsim::detail::intoJPS_Point; +using jupedsim::detail::intoPoint; +using jupedsim::detail::intoTuple; + +//////////////////////////////////////////////////////////////////////////////////////////////////// +/// Simulation +//////////////////////////////////////////////////////////////////////////////////////////////////// +JPS_Simulation JPS_Simulation_Create( + JPS_OperationalModel model, + JPS_Geometry geometry, + double dT, + JPS_ErrorMessage* errorMessage) +{ + assert(model); + assert(geometry); + JPS_Simulation result{}; + try { + auto geometryInternal = reinterpret_cast(geometry); + auto collisionGeometry = + std::make_unique(*geometryInternal->collisionGeometry); + auto routingEngine = geometryInternal->routingEngine->Clone(); + + auto modelInternal = reinterpret_cast(model); + auto model = modelInternal->Clone(); + result = reinterpret_cast(new Simulation( + std::move(model), std::move(collisionGeometry), std::move(routingEngine), dT)); + } catch(const std::exception& ex) { + if(errorMessage) { + *errorMessage = reinterpret_cast(new JPS_ErrorMessage_t{ex.what()}); + } + } catch(...) { + if(errorMessage) { + *errorMessage = reinterpret_cast( + new JPS_ErrorMessage_t{"Unknown internal error."}); + } + } + return result; +} + +JPS_JourneyId JPS_Simulation_AddJourney( + JPS_Simulation handle, + JPS_JourneyDescription journey, + JPS_ErrorMessage* errorMessage) +{ + assert(handle); + assert(journey); + + auto simulation = reinterpret_cast(handle); + auto journeyInternal = reinterpret_cast(journey); + auto result = Journey::ID::Invalid.getID(); + try { + result = simulation->AddJourney(*journeyInternal).getID(); + } catch(const std::exception& ex) { + if(errorMessage) { + *errorMessage = reinterpret_cast(new JPS_ErrorMessage_t{ex.what()}); + } + } catch(...) { + if(errorMessage) { + *errorMessage = reinterpret_cast( + new JPS_ErrorMessage_t{"Unknown internal error."}); + } + } + return result; +} + +static JPS_StageId add_stage( + JPS_Simulation handle, + const StageDescription& stageDescription, + JPS_ErrorMessage* errorMessage) +{ + assert(handle); + + auto simulation = reinterpret_cast(handle); + auto result = Journey::ID::Invalid.getID(); + try { + + result = simulation->AddStage(stageDescription).getID(); + } catch(const std::exception& ex) { + if(errorMessage) { + *errorMessage = reinterpret_cast(new JPS_ErrorMessage_t{ex.what()}); + } + } catch(...) { + if(errorMessage) { + *errorMessage = reinterpret_cast( + new JPS_ErrorMessage_t{"Unknown internal error."}); + } + } + return result; +} + +JPS_StageId JPS_Simulation_AddStageWaypoint( + JPS_Simulation handle, + JPS_Point position, + double distance, + JPS_ErrorMessage* errorMessage) +{ + return add_stage(handle, WaypointDescription{intoPoint(position), distance}, errorMessage); +} + +JPS_StageId JPS_Simulation_AddStageExit( + JPS_Simulation handle, + const JPS_Point* polygon, + size_t len_polygon, + JPS_ErrorMessage* errorMessage) +{ + std::vector loop{}; + loop.reserve(len_polygon); + std::transform(polygon, polygon + len_polygon, std::back_inserter(loop), intoPoint); + return add_stage(handle, ExitDescription{Polygon(loop)}, errorMessage); +} + +JPS_StageId JPS_Simulation_AddStageNotifiableQueue( + JPS_Simulation handle, + const JPS_Point* waiting_positions, + size_t len_waiting_positions, + JPS_ErrorMessage* errorMessage) +{ + std::vector positions{}; + positions.reserve(len_waiting_positions); + std::transform( + waiting_positions, + waiting_positions + len_waiting_positions, + std::back_inserter(positions), + intoPoint); + return add_stage(handle, NotifiableQueueDescription{positions}, errorMessage); +} + +JPS_StageId JPS_Simulation_AddStageWaitingSet( + JPS_Simulation handle, + const JPS_Point* waiting_positions, + size_t len_waiting_positions, + JPS_ErrorMessage* errorMessage) +{ + std::vector positions{}; + positions.reserve(len_waiting_positions); + std::transform( + waiting_positions, + waiting_positions + len_waiting_positions, + std::back_inserter(positions), + intoPoint); + return add_stage(handle, NotifiableWaitingSetDescription{positions}, errorMessage); +} + +JPS_StageId +JPS_Simulation_AddStageDirectSteering(JPS_Simulation handle, JPS_ErrorMessage* errorMessage) +{ + return add_stage(handle, DirectSteeringDescription{}, errorMessage); +} + +JPS_AgentId JPS_Simulation_AddGeneralizedCentrifugalForceModelAgent( + JPS_Simulation handle, + JPS_GeneralizedCentrifugalForceModelAgentParameters parameters, + JPS_ErrorMessage* errorMessage) +{ + assert(handle); + auto result = GenericAgent::ID::Invalid; + auto simulation = reinterpret_cast(handle); + try { + if(simulation->ModelType() != OperationalModelType::GENERALIZED_CENTRIFUGAL_FORCE) { + throw std::runtime_error( + "Simulation is not configured to use Generalized Centrifugal Force Model"); + } + GenericAgent agent{ + GenericAgent::ID::Invalid, + Journey::ID(parameters.journeyId), + BaseStage::ID(parameters.stageId), + intoPoint(parameters.position), + intoPoint(parameters.orientation), + GeneralizedCentrifugalForceModelData{ + parameters.speed, + intoPoint(parameters.e0), + 0, + parameters.mass, + parameters.tau, + parameters.v0, + parameters.a_v, + parameters.a_min, + parameters.b_min, + parameters.b_max}}; + result = simulation->AddAgent(std::move(agent)); + } catch(const std::exception& ex) { + if(errorMessage) { + *errorMessage = reinterpret_cast(new JPS_ErrorMessage_t{ex.what()}); + } + } catch(...) { + if(errorMessage) { + *errorMessage = reinterpret_cast( + new JPS_ErrorMessage_t{"Unknown internal error."}); + } + } + return result.getID(); +} + +JPS_AgentId JPS_Simulation_AddCollisionFreeSpeedModelAgent( + JPS_Simulation handle, + JPS_CollisionFreeSpeedModelAgentParameters parameters, + JPS_ErrorMessage* errorMessage) +{ + assert(handle); + auto result = GenericAgent::ID::Invalid; + auto simulation = reinterpret_cast(handle); + try { + if(simulation->ModelType() != OperationalModelType::COLLISION_FREE_SPEED) { + throw std::runtime_error( + "Simulation is not configured to use Collision Free Speed Model"); + } + GenericAgent agent( + GenericAgent::ID::Invalid, + Journey::ID(parameters.journeyId), + BaseStage::ID(parameters.stageId), + intoPoint(parameters.position), + {}, + CollisionFreeSpeedModelData{parameters.time_gap, parameters.v0, parameters.radius}); + result = simulation->AddAgent(std::move(agent)); + } catch(const std::exception& ex) { + if(errorMessage) { + *errorMessage = reinterpret_cast(new JPS_ErrorMessage_t{ex.what()}); + } + } catch(...) { + if(errorMessage) { + *errorMessage = reinterpret_cast( + new JPS_ErrorMessage_t{"Unknown internal error."}); + } + } + return result.getID(); +} + +bool JPS_Simulation_MarkAgentForRemoval( + JPS_Simulation handle, + JPS_AgentId agentId, + JPS_ErrorMessage* errorMessage) +{ + assert(handle); + auto simulation = reinterpret_cast(handle); + bool result{false}; + try { + simulation->MarkAgentForRemoval(agentId); + result = true; + } catch(const std::exception& ex) { + if(errorMessage) { + *errorMessage = reinterpret_cast(new JPS_ErrorMessage_t{ex.what()}); + } + } catch(...) { + if(errorMessage) { + *errorMessage = reinterpret_cast( + new JPS_ErrorMessage_t{"Unknown internal error."}); + } + } + return result; +} + +size_t JPS_Simulation_RemovedAgents(JPS_Simulation handle, const JPS_AgentId** data) +{ + assert(handle); + auto simulation = reinterpret_cast(handle); + const auto& agents = simulation->RemovedAgents(); + static_assert( + std::is_same::value, + "GenericAgentIDs cannot be casted in JPS_AgentId"); + *data = reinterpret_cast(agents.data()); + return agents.size(); +} + +bool JPS_Simulation_Iterate(JPS_Simulation handle, JPS_ErrorMessage* errorMessage) +{ + assert(handle); + auto simulation = reinterpret_cast(handle); + bool result = false; + try { + simulation->Iterate(); + result = true; + } catch(const std::exception& ex) { + if(errorMessage) { + *errorMessage = reinterpret_cast(new JPS_ErrorMessage_t{ex.what()}); + } + } catch(...) { + if(errorMessage) { + *errorMessage = reinterpret_cast( + new JPS_ErrorMessage_t{"Unknown internal error."}); + } + } + return result; +} + +size_t JPS_Simulation_AgentCount(JPS_Simulation handle) +{ + assert(handle); + auto simulation = reinterpret_cast(handle); + return simulation->AgentCount(); +} + +double JPS_Simulation_ElapsedTime(JPS_Simulation handle) +{ + assert(handle); + auto simulation = reinterpret_cast(handle); + return simulation->ElapsedTime(); +} + +double JPS_Simulation_DeltaTime(JPS_Simulation handle) +{ + assert(handle); + auto simulation = reinterpret_cast(handle); + return simulation->DT(); +} + +uint64_t JPS_Simulation_IterationCount(JPS_Simulation handle) +{ + assert(handle); + auto simulation = reinterpret_cast(handle); + return simulation->Iteration(); +} + +JPS_AgentIterator JPS_Simulation_AgentIterator(JPS_Simulation handle) +{ + assert(handle); + auto simulation = reinterpret_cast(handle); + return reinterpret_cast( + new AgentIterator(simulation->Agents())); +} + +JPS_Agent +JPS_Simulation_GetAgent(JPS_Simulation handle, JPS_AgentId agentId, JPS_ErrorMessage* errorMessage) +{ + assert(handle); + const auto simulation = reinterpret_cast(handle); + + try { + const auto agent = &simulation->Agent(agentId); + return reinterpret_cast(agent); + } catch(const std::exception& ex) { + if(errorMessage) { + *errorMessage = reinterpret_cast(new JPS_ErrorMessage_t{ex.what()}); + } + } catch(...) { + if(errorMessage) { + *errorMessage = reinterpret_cast( + new JPS_ErrorMessage_t{"Unknown internal error."}); + } + } + return nullptr; +} + +bool JPS_Simulation_SwitchAgentJourney( + JPS_Simulation handle, + JPS_AgentId agentId, + JPS_JourneyId journeyId, + JPS_StageId stageId, + JPS_ErrorMessage* errorMessage) +{ + assert(handle); + const auto simulation = reinterpret_cast(handle); + bool result = false; + try { + simulation->SwitchAgentJourney(agentId, journeyId, stageId); + result = true; + } catch(const std::exception& ex) { + if(errorMessage) { + *errorMessage = reinterpret_cast(new JPS_ErrorMessage_t{ex.what()}); + } + } catch(...) { + if(errorMessage) { + *errorMessage = reinterpret_cast( + new JPS_ErrorMessage_t{"Unknown internal error."}); + } + } + return result; +} + +JPS_ModelType JPS_Simulation_ModelType(JPS_Simulation handle) +{ + assert(handle); + const auto simulation = reinterpret_cast(handle); + const auto type = simulation->ModelType(); + switch(type) { + case OperationalModelType::COLLISION_FREE_SPEED: + return JPS_CollisionFreeSpeedModel; + case OperationalModelType::GENERALIZED_CENTRIFUGAL_FORCE: + return JPS_GeneralizedCentrifugalForceModel; + } + UNREACHABLE(); +} + +JPS_AgentIdIterator +JPS_Simulation_AgentsInRange(JPS_Simulation handle, JPS_Point position, double distance) +{ + assert(handle); + auto simulation = reinterpret_cast(handle); + return reinterpret_cast( + new AgentIdIterator(simulation->AgentsInRange(intoPoint(position), distance))); +} + +JPS_AgentIdIterator +JPS_Simulation_AgentsInPolygon(JPS_Simulation handle, const JPS_Point* polygon, size_t len_polygon) +{ + assert(handle); + auto simulation = reinterpret_cast(handle); + std::vector poly{}; + poly.reserve(len_polygon); + std::transform(polygon, polygon + len_polygon, std::back_inserter(poly), intoPoint); + return reinterpret_cast( + new AgentIdIterator(simulation->AgentsInPolygon(poly))); +} + +JPS_StageType JPS_Simulation_GetStageType(JPS_Simulation handle, JPS_StageId id) +{ + assert(handle); + auto simulation = reinterpret_cast(handle); + const auto convert = [](const auto t) { + switch(t) { + case 0: + return JPS_WaypointType; + case 1: + return JPS_WaitingSetType; + case 2: + return JPS_NotifiableQueueType; + case 3: + return JPS_ExitType; + case 4: + return JPS_DirectSteeringType; + } + UNREACHABLE(); + }; + return convert(simulation->Stage(id).index()); +} + +JPS_NotifiableQueueProxy JPS_Simulation_GetNotifiableQueueProxy( + JPS_Simulation handle, + JPS_StageId stageId, + JPS_ErrorMessage* errorMessage) +{ + assert(handle); + auto simulation = reinterpret_cast(handle); + try { + return reinterpret_cast( + new NotifiableQueueProxy(std::get(simulation->Stage(stageId)))); + } catch(const std::exception& ex) { + if(errorMessage) { + *errorMessage = reinterpret_cast(new JPS_ErrorMessage_t{ex.what()}); + } + return nullptr; + } +} +JUPEDSIM_API JPS_WaitingSetProxy JPS_Simulation_GetWaitingSetProxy( + JPS_Simulation handle, + JPS_StageId stageId, + JPS_ErrorMessage* errorMessage) +{ + assert(handle); + auto simulation = reinterpret_cast(handle); + try { + return reinterpret_cast(new NotifiableWaitingSetProxy( + std::get(simulation->Stage(stageId)))); + } catch(const std::exception& ex) { + if(errorMessage) { + *errorMessage = reinterpret_cast(new JPS_ErrorMessage_t{ex.what()}); + } + return nullptr; + } +} + +JUPEDSIM_API JPS_WaypointProxy JPS_Simulation_GetWaypointProxy( + JPS_Simulation handle, + JPS_StageId stageId, + JPS_ErrorMessage* errorMessage) +{ + assert(handle); + auto simulation = reinterpret_cast(handle); + try { + return reinterpret_cast( + new WaypointProxy(std::get(simulation->Stage(stageId)))); + } catch(const std::exception& ex) { + if(errorMessage) { + *errorMessage = reinterpret_cast(new JPS_ErrorMessage_t{ex.what()}); + } + return nullptr; + } +} + +JUPEDSIM_API JPS_ExitProxy JPS_Simulation_GetExitProxy( + JPS_Simulation handle, + JPS_StageId stageId, + JPS_ErrorMessage* errorMessage) +{ + assert(handle); + auto simulation = reinterpret_cast(handle); + try { + return reinterpret_cast( + new ExitProxy(std::get(simulation->Stage(stageId)))); + } catch(const std::exception& ex) { + if(errorMessage) { + *errorMessage = reinterpret_cast(new JPS_ErrorMessage_t{ex.what()}); + } + return nullptr; + } +} + +JPS_DirectSteeringProxy JPS_Simulation_GetDirectSteeringProxy( + JPS_Simulation handle, + JPS_StageId stageId, + JPS_ErrorMessage* errorMessage) +{ + assert(handle); + auto simulation = reinterpret_cast(handle); + try { + return reinterpret_cast( + new DirectSteeringProxy(std::get(simulation->Stage(stageId)))); + } catch(const std::exception& ex) { + if(errorMessage) { + *errorMessage = reinterpret_cast(new JPS_ErrorMessage_t{ex.what()}); + } + return nullptr; + } +} + +void JPS_Simulation_SetTracing(JPS_Simulation handle, bool status) +{ + assert(handle); + auto simuation = reinterpret_cast(handle); + simuation->SetTracing(status); +} + +JPS_Trace JPS_Simulation_GetTrace(JPS_Simulation handle) +{ + assert(handle); + auto simuation = reinterpret_cast(handle); + const auto stats = simuation->GetLastStats(); + return JPS_Trace{stats.IterationDuration(), stats.OpDecSystemRunDuration()}; +} + +JPS_Geometry JPS_Simulation_GetGeometry(JPS_Simulation handle) +{ + assert(handle); + const auto simulation = reinterpret_cast(handle); + return reinterpret_cast(new Geometry(simulation->Geo())); +} + +void JPS_Simulation_Free(JPS_Simulation handle) +{ + delete reinterpret_cast(handle); +} diff --git a/libjupedsim/src/stage.cpp b/libjupedsim/src/stage.cpp new file mode 100644 index 0000000000..c6014d4bdc --- /dev/null +++ b/libjupedsim/src/stage.cpp @@ -0,0 +1,156 @@ +// Copyright © 2012-2023 Forschungszentrum Jülich GmbH +// SPDX-License-Identifier: LGPL-3.0-or-later +#include "jupedsim/stage.h" + +#include +#include + +#include + +//////////////////////////////////////////////////////////////////////////////////////////////////// +/// NotifiableQueueProxy +//////////////////////////////////////////////////////////////////////////////////////////////////// +size_t JPS_NotifiableQueueProxy_GetCountTargeting(JPS_NotifiableQueueProxy handle) +{ + assert(handle); + auto proxy = reinterpret_cast(handle); + return proxy->CountTargeting(); +} + +size_t JPS_NotifiableQueueProxy_GetCountEnqueued(JPS_NotifiableQueueProxy handle) +{ + assert(handle); + auto proxy = reinterpret_cast(handle); + return proxy->CountEnqueued(); +} + +void JPS_NotifiableQueueProxy_Pop(JPS_NotifiableQueueProxy handle, size_t count) +{ + assert(handle); + auto proxy = reinterpret_cast(handle); + proxy->Pop(count); +} + +size_t +JPS_NotifiableQueueProxy_GetEnqueued(JPS_NotifiableQueueProxy handle, const JPS_AgentId** data) +{ + assert(handle); + auto proxy = reinterpret_cast(handle); + const auto& agents = proxy->Enqueued(); + static_assert( + std::is_same::value, + "GenericAgentIDs cannot be casted in JPS_AgentId"); + *data = reinterpret_cast(agents.data()); + return agents.size(); +} + +void JPS_NotifiableQueueProxy_Free(JPS_NotifiableQueueProxy handle) +{ + delete reinterpret_cast(handle); +} + +//////////////////////////////////////////////////////////////////////////////////////////////////// +/// WaitingSetProxy +//////////////////////////////////////////////////////////////////////////////////////////////////// +size_t JPS_WaitingSetProxy_GetCountTargeting(JPS_WaitingSetProxy handle) +{ + assert(handle); + auto proxy = reinterpret_cast(handle); + return proxy->CountTargeting(); +} + +size_t JPS_WaitingSetProxy_GetCountWaiting(JPS_WaitingSetProxy handle) +{ + assert(handle); + auto proxy = reinterpret_cast(handle); + return proxy->CountWaiting(); +} + +size_t JPS_WaitingSetProxy_GetWaiting(JPS_WaitingSetProxy handle, const JPS_AgentId** data) +{ + assert(handle); + auto proxy = reinterpret_cast(handle); + const auto& agents = proxy->Waiting(); + static_assert( + std::is_same::value, + "GenericAgentIDs cannot be casted in JPS_AgentId"); + *data = reinterpret_cast(agents.data()); + return agents.size(); +} + +void JPS_WaitingSetProxy_SetWaitingSetState( + JPS_WaitingSetProxy handle, + JPS_WaitingSetState newState) +{ + const auto convert = [](const auto s) { + switch(s) { + case JPS_WaitingSet_Active: + return WaitingSetState::Active; + case JPS_WaitingSet_Inactive: + return WaitingSetState::Inactive; + } + UNREACHABLE(); + }; + assert(handle); + auto proxy = reinterpret_cast(handle); + proxy->State(convert(newState)); +} + +JPS_WaitingSetState JPS_WaitingSetProxy_GetWaitingSetState(JPS_WaitingSetProxy handle) +{ + const auto convert = [](const auto s) { + switch(s) { + case WaitingSetState::Active: + return JPS_WaitingSet_Active; + case WaitingSetState::Inactive: + return JPS_WaitingSet_Inactive; + } + UNREACHABLE(); + }; + assert(handle); + auto proxy = reinterpret_cast(handle); + return convert(proxy->State()); +} + +void JPS_WaitingSetProxy_Free(JPS_WaitingSetProxy handle) +{ + delete reinterpret_cast(handle); +} + +//////////////////////////////////////////////////////////////////////////////////////////////////// +/// DirectSteeringProxy +//////////////////////////////////////////////////////////////////////////////////////////////////// +void JPS_DirectSteeringProxy_Free(JPS_DirectSteeringProxy handle) +{ + delete reinterpret_cast(handle); +} + +//////////////////////////////////////////////////////////////////////////////////////////////////// +/// WaitPointProxy +//////////////////////////////////////////////////////////////////////////////////////////////////// +size_t JPS_WaypointProxy_GetCountTargeting(JPS_WaypointProxy handle) +{ + assert(handle); + auto proxy = reinterpret_cast(handle); + return proxy->CountTargeting(); +} + +void JPS_WaypointProxy_Free(JPS_WaypointProxy handle) +{ + delete reinterpret_cast(handle); +} + +//////////////////////////////////////////////////////////////////////////////////////////////////// +/// ExitProxy +//////////////////////////////////////////////////////////////////////////////////////////////////// +size_t JPS_ExitProxy_GetCountTargeting(JPS_ExitProxy handle) +{ + assert(handle); + auto proxy = reinterpret_cast(handle); + return proxy->CountTargeting(); +} + +void JPS_ExitProxy_Free(JPS_ExitProxy handle) +{ + delete reinterpret_cast(handle); +}