Grid distance API changes

This commit is contained in:
James P. Howard, II
2022-09-07 21:46:41 -04:00
parent 552d8fc725
commit 2aeef72f19
5 changed files with 35 additions and 17 deletions

View File

@@ -131,7 +131,7 @@ namespace kami {
*
* @returns the distance as a `double`
*/
virtual std::optional<double> distance(std::shared_ptr<Coord> &p, GridDistanceType distance_type) const = 0;
virtual double distance(std::shared_ptr<Coord> &p) const = 0;
};

View File

@@ -80,12 +80,10 @@ namespace kami {
* and toroidal wrapping of the underlying `Grid1D`.
*
* @param p the point to measure the distance to
* @param distance_type specify the distance type
*
* @returns the distance as a `double`
*/
std::optional<double>
distance(std::shared_ptr<Coord> &p, [[maybe_unused]] GridDistanceType distance_type) const override;
double distance(std::shared_ptr<Coord> &p) const override;
/**
* @brief Test if two coordinates are equal
@@ -118,7 +116,7 @@ namespace kami {
* @details If any component of the resulting value is not a whole number, it is
* truncated following the same rules as `int`.
*/
inline friend GridCoord1D operator*(const GridCoord1D &lhs, const double rhs);
inline friend GridCoord1D operator*(const GridCoord1D &lhs, double rhs);
/**
* @brief Multiply a coordinate by a scalar
@@ -126,7 +124,7 @@ namespace kami {
* @details If any component of the resulting value is not a whole number, it is
* truncated following the same rules as `int`.
*/
inline friend GridCoord1D operator*(const double lhs, const GridCoord1D &rhs);
inline friend GridCoord1D operator*(double lhs, const GridCoord1D &rhs);
private:
int _x_coord;

View File

@@ -72,6 +72,23 @@ namespace kami {
*/
[[nodiscard]] std::string to_string() const override;
/**
* @brief Find the distance between two points
*
* @details Find the distance between two points using the
* specified metric.
*
* However, the coordinate class is not aware of the
* properties of the `Grid2D` it is operating on. Accordingly,
* if the direct path is measured, without accounting for
* and toroidal wrapping of the underlying `Grid2D`.
*
* @param p the point to measure the distance to
*
* @returns the distance as a `double`
*/
double distance(std::shared_ptr<Coord> &p) const override;
/**
* @brief Find the distance between two points
*
@@ -89,7 +106,8 @@ namespace kami {
*
* @returns the distance as a `double`
*/
std::optional<double> distance(std::shared_ptr<Coord> &p, GridDistanceType distance_type) const override;
double
distance(std::shared_ptr<GridCoord2D> &p, GridDistanceType distance_type = GridDistanceType::Euclidean) const;
/**
* @brief Test if two coordinates are equal
@@ -243,14 +261,14 @@ namespace kami {
*/
[[nodiscard]] bool is_location_valid(const GridCoord2D& coord) const;
/**
virtual /**
* @brief Get the location of the specified agent.
*
* @param[in] agent_id the `AgentID` of the agent in question.
*
* @return the location of the specified `Agent`
*/
[[nodiscard]] std::optional<GridCoord2D> get_location_by_agent(const AgentID &agent_id) const;
std::optional<GridCoord2D> get_location_by_agent(const AgentID &agent_id) const;
/**
* @brief Get the contents of the specified location.
@@ -279,7 +297,7 @@ namespace kami {
*/
[[nodiscard]] bool get_wrap_y() const;
/**
virtual /**
* @brief Return the neighborhood of the specified Agent
*
* @param[in] agent_id the `AgentID` of the agent in question.
@@ -292,7 +310,7 @@ namespace kami {
*
* @see `NeighborhoodType`
*/
[[nodiscard]] std::optional<std::shared_ptr<std::unordered_set<GridCoord2D>>>
std::optional<std::shared_ptr<std::unordered_set<GridCoord2D>>>
get_neighborhood(AgentID agent_id, bool include_center, GridNeighborhoodType neighborhood_type) const;
/**

View File

@@ -47,10 +47,7 @@ namespace kami {
return std::string("(" + std::to_string(_x_coord) + ")");
}
std::optional<double>
GridCoord1D::distance(std::shared_ptr<Coord> &p, [[maybe_unused]] GridDistanceType distance_type) const {
// We are going to ignore distance type since they all
// have the same result in one dimension.
double GridCoord1D::distance(std::shared_ptr<Coord> &p) const {
auto p1d = std::static_pointer_cast<GridCoord1D>(p);
return static_cast<double>(abs(_x_coord - p1d->_x_coord));
}

View File

@@ -48,7 +48,12 @@ namespace kami {
return std::string("(" + std::to_string(_x_coord) + ", " + std::to_string(_y_coord) + ")");
}
std::optional<double> GridCoord2D::distance(std::shared_ptr<Coord> &p, GridDistanceType distance_type) const {
double GridCoord2D::distance(std::shared_ptr<Coord> &p) const {
auto p2d = std::static_pointer_cast<GridCoord2D>(p);
return distance(p2d);
}
double GridCoord2D::distance(std::shared_ptr<GridCoord2D> &p, GridDistanceType distance_type) const {
auto p2d = std::static_pointer_cast<GridCoord2D>(p);
switch (distance_type) {
@@ -57,9 +62,9 @@ namespace kami {
case GridDistanceType::Manhattan:
return distance_manhattan(p2d);
case GridDistanceType::Euclidean:
default:
return distance_euclidean(p2d);
}
return std::nullopt;
}
bool operator==(const GridCoord2D &lhs, const GridCoord2D &rhs) {