diff --git a/include/kami/grid.h b/include/kami/grid.h index e820a06..aacc964 100644 --- a/include/kami/grid.h +++ b/include/kami/grid.h @@ -131,7 +131,7 @@ namespace kami { * * @returns the distance as a `double` */ - virtual std::optional distance(std::shared_ptr &p, GridDistanceType distance_type) const = 0; + virtual double distance(std::shared_ptr &p) const = 0; }; diff --git a/include/kami/grid1d.h b/include/kami/grid1d.h index b7755a9..b205704 100644 --- a/include/kami/grid1d.h +++ b/include/kami/grid1d.h @@ -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 - distance(std::shared_ptr &p, [[maybe_unused]] GridDistanceType distance_type) const override; + double distance(std::shared_ptr &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; diff --git a/include/kami/grid2d.h b/include/kami/grid2d.h index 6414db5..cc0981f 100644 --- a/include/kami/grid2d.h +++ b/include/kami/grid2d.h @@ -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 &p) const override; + /** * @brief Find the distance between two points * @@ -89,7 +106,8 @@ namespace kami { * * @returns the distance as a `double` */ - std::optional distance(std::shared_ptr &p, GridDistanceType distance_type) const override; + double + distance(std::shared_ptr &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 get_location_by_agent(const AgentID &agent_id) const; + std::optional 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::optional>> get_neighborhood(AgentID agent_id, bool include_center, GridNeighborhoodType neighborhood_type) const; /** diff --git a/src/libkami/grid1d.cc b/src/libkami/grid1d.cc index 84ebedd..d885aaf 100644 --- a/src/libkami/grid1d.cc +++ b/src/libkami/grid1d.cc @@ -47,10 +47,7 @@ namespace kami { return std::string("(" + std::to_string(_x_coord) + ")"); } - std::optional - GridCoord1D::distance(std::shared_ptr &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 &p) const { auto p1d = std::static_pointer_cast(p); return static_cast(abs(_x_coord - p1d->_x_coord)); } diff --git a/src/libkami/grid2d.cc b/src/libkami/grid2d.cc index a764a31..c15e68b 100644 --- a/src/libkami/grid2d.cc +++ b/src/libkami/grid2d.cc @@ -48,7 +48,12 @@ namespace kami { return std::string("(" + std::to_string(_x_coord) + ", " + std::to_string(_y_coord) + ")"); } - std::optional GridCoord2D::distance(std::shared_ptr &p, GridDistanceType distance_type) const { + double GridCoord2D::distance(std::shared_ptr &p) const { + auto p2d = std::static_pointer_cast(p); + return distance(p2d); + } + + double GridCoord2D::distance(std::shared_ptr &p, GridDistanceType distance_type) const { auto p2d = std::static_pointer_cast(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) {