Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bounding box in level storage #89

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions cpp/libcore/source/geometry/level_storage.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#include "level_storage.hpp"

#include <algorithm>

namespace jps
{
auto LevelStorage::addLineSegment(LineSegment const & p_segment) -> void
Expand All @@ -17,4 +19,40 @@ auto LevelStorage::addAgent(Coordinate const & p_coordinate) -> void
m_agents.emplace_back(Agent{p_coordinate});
}

auto LevelStorage::computeBoundingBox() -> BoundingBox
{
// vector to store all coordinates
std::vector<Coordinate> all_coordinates;

// extract all coordinates from different components of level storage
// extract from line segments
for(const auto & line_segment : m_line_segments) {
all_coordinates.push_back(line_segment.getStart());
all_coordinates.push_back(line_segment.getEnd());
}

// extract from special areas
for(const auto & special_area : m_special_areas) {
auto polygon_coordinates = special_area.getArea().getPolygon();
all_coordinates.insert(
all_coordinates.end(), polygon_coordinates.begin(), polygon_coordinates.end());
}
// extract from agents
for(const auto & agent : m_agents) {
all_coordinates.push_back(agent.pos);
}

// get max/min x- and y-components
auto [min_x, max_x] = std::minmax_element(
all_coordinates.begin(),
all_coordinates.end(),
[](auto p_coord_first, auto p_coord_second) { return p_coord_first.x < p_coord_second.x; });
auto [min_y, max_y] = std::minmax_element(
all_coordinates.begin(),
all_coordinates.end(),
[](auto p_coord_first, auto p_coord_second) { return p_coord_first.y < p_coord_second.y; });

return BoundingBox(Coordinate(min_x->x, min_y->y), Coordinate(max_x->x, max_y->y));
}

} // namespace jps
14 changes: 14 additions & 0 deletions cpp/libcore/source/geometry/level_storage.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,16 @@

namespace jps
{
/// Struct to hold the information of lower left and upper right corner which define the bounding
/// box.
struct BoundingBox {
Coordinate lower_left; // NOLINTLINE(cppcoreguidelines-non-private-member-variables-in-classes)
Coordinate upper_right; // NOLINTLINE(cppcoreguidelines-non-private-member-variables-in-classes)

BoundingBox(Coordinate p_lower_left, Coordinate p_upper_right) :
lower_left(p_lower_left), upper_right(p_upper_right){};
};

class LevelStorage
{
std::vector<LineSegment> m_line_segments;
Expand All @@ -22,5 +32,9 @@ class LevelStorage
auto getAgents() -> std::vector<Agent> & { return m_agents; };
auto getAgents() const -> std::vector<Agent> const & { return m_agents; };
auto addAgent(Coordinate const & p_coordinate) -> void;

/// Compute a bounding box based on the current entities in LevelStorage
JetteSchumann marked this conversation as resolved.
Show resolved Hide resolved
/// @return bounding box with coordinates for lower left and upper right corned
auto computeBoundingBox() -> BoundingBox;
};
} // namespace jps