Skip to content

Commit

Permalink
Simulation now keeps track of multiple geometries
Browse files Browse the repository at this point in the history
  • Loading branch information
Ozaq committed Apr 11, 2024
1 parent e8b99fa commit 1850f76
Show file tree
Hide file tree
Showing 20 changed files with 128 additions and 163 deletions.
27 changes: 13 additions & 14 deletions libjupedsim/src/geometry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#include "Conversion.hpp"
#include "ErrorMessage.hpp"

#include <Geometry.hpp>
#include <CollisionGeometry.hpp>
#include <GeometryBuilder.hpp>

using jupedsim::detail::intoJPS_Point;
Expand Down Expand Up @@ -55,7 +55,7 @@ JPS_Geometry JPS_GeometryBuilder_Build(JPS_GeometryBuilder handle, JPS_ErrorMess
auto builder = reinterpret_cast<GeometryBuilder*>(handle);
JPS_Geometry result{};
try {
result = reinterpret_cast<JPS_Geometry>(new Geometry(builder->Build()));
result = reinterpret_cast<JPS_Geometry>(new CollisionGeometry(builder->Build()));
} catch(const std::exception& ex) {
if(errorMessage) {
*errorMessage = reinterpret_cast<JPS_ErrorMessage>(new JPS_ErrorMessage_t{ex.what()});
Expand All @@ -80,32 +80,31 @@ void JPS_GeometryBuilder_Free(JPS_GeometryBuilder handle)
size_t JPS_Geometry_GetBoundarySize(JPS_Geometry handle)
{
assert(handle);
const auto geo = reinterpret_cast<Geometry const*>(handle);
return std::get<0>(geo->collisionGeometry->AccessibleArea()).size();
const auto geo = reinterpret_cast<CollisionGeometry const*>(handle);
return std::get<0>(geo->AccessibleArea()).size();
}

const JPS_Point* JPS_Geometry_GetBoundaryData(JPS_Geometry handle)
{
assert(handle);
const auto geo = reinterpret_cast<Geometry const*>(handle);
return reinterpret_cast<const JPS_Point*>(
std::get<0>(geo->collisionGeometry->AccessibleArea()).data());
const auto geo = reinterpret_cast<CollisionGeometry const*>(handle);
return reinterpret_cast<const JPS_Point*>(std::get<0>(geo->AccessibleArea()).data());
}

size_t JPS_Geometry_GetHoleCount(JPS_Geometry handle)
{
assert(handle);
const auto geo = reinterpret_cast<Geometry const*>(handle);
return std::get<1>(geo->collisionGeometry->AccessibleArea()).size();
const auto geo = reinterpret_cast<CollisionGeometry const*>(handle);
return std::get<1>(geo->AccessibleArea()).size();
}

size_t
JPS_Geometry_GetHoleSize(JPS_Geometry handle, size_t hole_index, JPS_ErrorMessage* errorMessage)
{
assert(handle);
const auto geo = reinterpret_cast<Geometry const*>(handle);
const auto geo = reinterpret_cast<CollisionGeometry const*>(handle);
try {
return std::get<1>(geo->collisionGeometry->AccessibleArea()).at(hole_index).size();
return std::get<1>(geo->AccessibleArea()).at(hole_index).size();
} catch(const std::exception& ex) {
if(errorMessage) {
*errorMessage = reinterpret_cast<JPS_ErrorMessage>(new JPS_ErrorMessage_t{ex.what()});
Expand All @@ -123,10 +122,10 @@ const JPS_Point*
JPS_Geometry_GetHoleData(JPS_Geometry handle, size_t hole_index, JPS_ErrorMessage* errorMessage)
{
assert(handle);
const auto geo = reinterpret_cast<Geometry const*>(handle);
const auto geo = reinterpret_cast<CollisionGeometry const*>(handle);
try {
return reinterpret_cast<const JPS_Point*>(
std::get<1>(geo->collisionGeometry->AccessibleArea()).at(hole_index).data());
std::get<1>(geo->AccessibleArea()).at(hole_index).data());
} catch(const std::exception& ex) {
if(errorMessage) {
*errorMessage = reinterpret_cast<JPS_ErrorMessage>(new JPS_ErrorMessage_t{ex.what()});
Expand All @@ -142,5 +141,5 @@ JPS_Geometry_GetHoleData(JPS_Geometry handle, size_t hole_index, JPS_ErrorMessag

void JPS_Geometry_Free(JPS_Geometry handle)
{
delete reinterpret_cast<Geometry const*>(handle);
delete reinterpret_cast<CollisionGeometry const*>(handle);
}
14 changes: 7 additions & 7 deletions libjupedsim/src/routing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

#include "Conversion.hpp"

#include <Geometry.hpp>
#include <CollisionGeometry.hpp>
#include <RoutingEngine.hpp>

#include <algorithm>
Expand Down Expand Up @@ -38,14 +38,14 @@ JUPEDSIM_API void JPS_Mesh_Free(JPS_Mesh mesh)
////////////////////////////////////////////////////////////////////////////////
JUPEDSIM_API JPS_RoutingEngine JPS_RoutingEngine_Create(JPS_Geometry geometry)
{
auto* geo = reinterpret_cast<const Geometry*>(geometry);
return reinterpret_cast<JPS_RoutingEngine>(geo->routingEngine->Clone().release());
auto* geo = reinterpret_cast<const CollisionGeometry*>(geometry);
return reinterpret_cast<JPS_RoutingEngine>(new RoutingEngine(geo->Polygon()));
}

JUPEDSIM_API JPS_Path
JPS_RoutingEngine_ComputeWaypoint(JPS_RoutingEngine handle, JPS_Point from, JPS_Point to)
{
auto* engine = reinterpret_cast<NavMeshRoutingEngine*>(handle);
auto* engine = reinterpret_cast<RoutingEngine*>(handle);
const auto path = engine->ComputeAllWaypoints(intoPoint(from), intoPoint(to));
auto points = new JPS_Point[path.size()];
JPS_Path p{path.size(), points};
Expand All @@ -56,13 +56,13 @@ JPS_RoutingEngine_ComputeWaypoint(JPS_RoutingEngine handle, JPS_Point from, JPS_

JUPEDSIM_API bool JPS_RoutingEngine_IsRoutable(JPS_RoutingEngine handle, JPS_Point p)
{
const auto* engine = reinterpret_cast<NavMeshRoutingEngine*>(handle);
const auto* engine = reinterpret_cast<RoutingEngine*>(handle);
return engine->IsRoutable(intoPoint(p));
}

JUPEDSIM_API JPS_Mesh JPS_RoutingEngine_Mesh(JPS_RoutingEngine handle)
{
const auto* engine = reinterpret_cast<NavMeshRoutingEngine*>(handle);
const auto* engine = reinterpret_cast<RoutingEngine*>(handle);
const auto mesh = engine->MeshData();

JPS_Mesh result{};
Expand Down Expand Up @@ -96,5 +96,5 @@ JUPEDSIM_API JPS_Mesh JPS_RoutingEngine_Mesh(JPS_RoutingEngine handle)

JUPEDSIM_API void JPS_RoutingEngine_Free(JPS_RoutingEngine handle)
{
delete reinterpret_cast<NavMeshRoutingEngine*>(handle);
delete reinterpret_cast<RoutingEngine*>(handle);
}
19 changes: 6 additions & 13 deletions libjupedsim/src/simulation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#include "ErrorMessage.hpp"
#include "JourneyDescription.hpp"

#include <Geometry.hpp>
#include <CollisionGeometry.hpp>
#include <GeometrySwitchError.hpp>
#include <Simulation.hpp>
#include <Unreachable.hpp>
Expand All @@ -34,15 +34,11 @@ JPS_Simulation JPS_Simulation_Create(
assert(geometry);
JPS_Simulation result{};
try {
auto geometryInternal = reinterpret_cast<const Geometry*>(geometry);
auto collisionGeometry =
std::make_unique<CollisionGeometry>(*geometryInternal->collisionGeometry);
auto routingEngine = geometryInternal->routingEngine->Clone();

auto collisionGeometry = reinterpret_cast<const CollisionGeometry*>(geometry);
auto modelInternal = reinterpret_cast<OperationalModel*>(model);
auto model = modelInternal->Clone();
result = reinterpret_cast<JPS_Simulation>(new Simulation(
std::move(model), std::move(collisionGeometry), std::move(routingEngine), dT));
std::move(model), std::make_unique<CollisionGeometry>(*collisionGeometry), dT));
} catch(const std::exception& ex) {
if(errorMessage) {
*errorMessage = reinterpret_cast<JPS_ErrorMessage>(new JPS_ErrorMessage_t{ex.what()});
Expand Down Expand Up @@ -636,7 +632,7 @@ JPS_Geometry JPS_Simulation_GetGeometry(JPS_Simulation handle)
{
assert(handle);
const auto simulation = reinterpret_cast<const Simulation*>(handle);
return reinterpret_cast<JPS_Geometry>(new Geometry(simulation->Geo()));
return reinterpret_cast<JPS_Geometry>(new CollisionGeometry(simulation->Geo()));
}

bool JPS_Simulation_SwitchGeometry(
Expand All @@ -649,14 +645,11 @@ bool JPS_Simulation_SwitchGeometry(
assert(geometry);

auto simulation = reinterpret_cast<Simulation*>(handle);
auto geometryInternal = reinterpret_cast<const Geometry*>(geometry);
auto collisionGeometry =
std::make_unique<CollisionGeometry>(*geometryInternal->collisionGeometry);
auto routingEngine = geometryInternal->routingEngine->Clone();
auto collisionGeometry = reinterpret_cast<const CollisionGeometry*>(geometry);

bool result = false;
try {
simulation->SwitchGeometry(std::move(collisionGeometry), std::move(routingEngine));
simulation->SwitchGeometry(std::make_unique<CollisionGeometry>(*collisionGeometry));
result = true;
} catch(const GeometrySwitchError& ex) {
if(errorMessage) {
Expand Down
2 changes: 0 additions & 2 deletions libsimulator/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,6 @@ add_library(simulator STATIC
src/GeneralizedCentrifugalForceModelUpdate.hpp
src/GenericAgent.hpp
src/GeometricFunctions.hpp
src/Geometry.cpp
src/Geometry.hpp
src/GeometryBuilder.cpp
src/GeometryBuilder.hpp
src/GeometrySwitchError.hpp
Expand Down
11 changes: 4 additions & 7 deletions libsimulator/benchmark/benchmarkCollisionGeometry.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,16 @@
#include <benchmark/benchmark.h>

#include "CollisionGeometry.hpp"
#include "GeometryBuilder.hpp"
#include "LineSegment.hpp"
#include "buildGeometries.hpp"

template <class... Args>
void bmLineSegmentsInDistanceTo(benchmark::State& state, Args&&... args)
{
auto args_tuple = std::make_tuple(std::move(args)...);
auto geometry = std::move(std::get<Geometry>(args_tuple));
auto geometry = std::move(std::get<CollisionGeometry>(args_tuple));

for(auto _ : state) {
benchmark::DoNotOptimize(geometry.collisionGeometry->LineSegmentsInDistanceTo(5., {0, 0}));
benchmark::DoNotOptimize(geometry.LineSegmentsInDistanceTo(5., {0, 0}));
benchmark::ClobberMemory();
}
}
Expand All @@ -24,11 +22,10 @@ template <class... Args>
void bmLineSegmentsInApproxDistanceTo(benchmark::State& state, Args&&... args)
{
auto args_tuple = std::make_tuple(std::move(args)...);
auto geometry = std::move(std::get<Geometry>(args_tuple));
auto geometry = std::move(std::get<CollisionGeometry>(args_tuple));

for(auto _ : state) {
benchmark::DoNotOptimize(
geometry.collisionGeometry->LineSegmentsInApproxDistanceTo({0, 0}));
benchmark::DoNotOptimize(geometry.LineSegmentsInApproxDistanceTo({0, 0}));
benchmark::ClobberMemory();
}
}
Expand Down
6 changes: 3 additions & 3 deletions libsimulator/benchmark/buildGeometries.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
// SPDX-License-Identifier: LGPL-3.0-or-later
#pragma once

#include "Geometry.hpp"
#include "CollisionGeometry.hpp"
#include "GeometryBuilder.hpp"
#include "Point.hpp"

inline Geometry buildGrosserStern()
inline CollisionGeometry buildGrosserStern()
{
GeometryBuilder builder;
builder.AddAccessibleArea(std::vector<Point>{
Expand Down Expand Up @@ -1377,7 +1377,7 @@ inline Geometry buildGrosserStern()
return builder.Build();
}

inline Geometry buildLargeStreetNetwork()
inline CollisionGeometry buildLargeStreetNetwork()
{

GeometryBuilder builder;
Expand Down
16 changes: 16 additions & 0 deletions libsimulator/src/CfgCgal.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include <CGAL/draw_triangulation_2.h>
#include <CGAL/mark_domain_in_triangulation.h>

#include <functional>
#include <list>

// using K = CGAL::Exact_predicates_exact_constructions_kernel;
Expand Down Expand Up @@ -42,3 +43,18 @@ class MyFace : public Fb
using TDS = CGAL::Triangulation_data_structure_2<Vb, MyFace<K>>;
using Itag = CGAL::Exact_predicates_tag;
using CDT = CGAL::Constrained_Delaunay_triangulation_2<K, TDS, Itag>;

namespace std
{
template <>
class hash<PolyWithHoles>
{
public:
size_t operator()(const PolyWithHoles& p) const
{
std::stringstream ss{};
ss << p;
return std::hash<std::string>()(ss.str());
}
};
} // namespace std
12 changes: 10 additions & 2 deletions libsimulator/src/CollisionGeometry.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@
#include "HashCombine.hpp"
#include "IteratorPair.hpp"
#include "LineSegment.hpp"
#include "UniqueID.hpp"

#include <map>
#include <optional>
#include <set>
#include <unordered_map>
#include <vector>
Expand Down Expand Up @@ -90,6 +89,11 @@ std::set<Cell> cellsFromLineSegment(LineSegment ls);

class CollisionGeometry
{
public:
using ID = jps::UniqueID<CollisionGeometry>;

private:
ID _id{};
PolyWithHoles _accessibleAreaPolygon;
std::vector<LineSegment> _segments;
std::unordered_map<Cell, std::set<LineSegment>> _grid{};
Expand Down Expand Up @@ -129,6 +133,10 @@ class CollisionGeometry

const std::tuple<std::vector<Point>, std::vector<std::vector<Point>>>& AccessibleArea() const;

const PolyWithHoles& Polygon() const { return _accessibleAreaPolygon; }

ID Id() const { return _id; }

private:
void insertIntoApproximateGrid(const LineSegment& ls);
};
3 changes: 0 additions & 3 deletions libsimulator/src/Geometry.cpp

This file was deleted.

12 changes: 0 additions & 12 deletions libsimulator/src/Geometry.hpp

This file was deleted.

12 changes: 2 additions & 10 deletions libsimulator/src/GeometryBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,14 @@

#include "CfgCgal.hpp"
#include "CollisionGeometry.hpp"
#include "Geometry.hpp"
#include "Graph.hpp"
#include "LineSegment.hpp"
#include "Point.hpp"
#include "RoutingEngine.hpp"
#include "SimulationError.hpp"

#include <fmt/format.h>
#include <fmt/ranges.h>

#include <algorithm>
#include <memory>
#include <stdexcept>
#include <vector>

GeometryBuilder& GeometryBuilder::AddAccessibleArea(const std::vector<Point>& lineLoop)
Expand All @@ -31,7 +26,7 @@ GeometryBuilder& GeometryBuilder::ExcludeFromAccessibleArea(const std::vector<Po
return *this;
}

Geometry GeometryBuilder::Build()
CollisionGeometry GeometryBuilder::Build()
{
const std::vector<Poly> accessibleListInput{
std::begin(_accessibleAreas), std::end(_accessibleAreas)};
Expand Down Expand Up @@ -64,8 +59,5 @@ Geometry GeometryBuilder::Build()
accessibleArea = *res.begin();
}

std::unique_ptr<CollisionGeometry> collisionGeometry =
std::make_unique<CollisionGeometry>(accessibleArea);

return {std::move(collisionGeometry), std::make_unique<NavMeshRoutingEngine>(accessibleArea)};
return CollisionGeometry(accessibleArea);
}
4 changes: 2 additions & 2 deletions libsimulator/src/GeometryBuilder.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// SPDX-License-Identifier: LGPL-3.0-or-later
#pragma once

#include "Geometry.hpp"
#include "CollisionGeometry.hpp"
#include "Polygon.hpp"

#include <vector>
Expand All @@ -22,5 +22,5 @@ class GeometryBuilder

GeometryBuilder& AddAccessibleArea(const std::vector<Point>& lineLoop);
GeometryBuilder& ExcludeFromAccessibleArea(const std::vector<Point>& lineLoop);
Geometry Build();
CollisionGeometry Build();
};
1 change: 0 additions & 1 deletion libsimulator/src/OperationalDecisionSystem.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
#pragma once

#include "GenericAgent.hpp"
#include "Geometry.hpp"
#include "IteratorPair.hpp"
#include "NeighborhoodSearch.hpp"
#include "OperationalModel.hpp"
Expand Down
Empty file added libsimulator/src/Routing.cpp
Empty file.
Empty file added libsimulator/src/Routing.hpp
Empty file.
Loading

0 comments on commit 1850f76

Please sign in to comment.