Skip to content

Commit

Permalink
Made the costmap a unique pointer and fixed the costmap class naming
Browse files Browse the repository at this point in the history
  • Loading branch information
mplemay committed Oct 19, 2020
1 parent bb55f49 commit 34ff819
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 18 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ meson compile
## Usage
```
cd <project_root_dir>/build*
chmod +x example
./example
Enter the size of the boarder square: 100
Enter the step size (i.e. double): 5.0
Expand Down
4 changes: 2 additions & 2 deletions include/CostMap2D.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ typedef std::tuple<int64_t, int64_t> Point2D;
* A class which represents a 2D cost map of booleans which represent whether or
* a coordinate is occupied.
*/
class Costmap2D {
class CostMap2D {
public:
Costmap2D() noexcept = default;
CostMap2D() noexcept = default;
std::optional<bool> update(const Point2D &point, const bool &value);
std::optional<bool> get(const Point2D &point) const;
[[nodiscard]] bool contains(const Point2D &point) const;
Expand Down
5 changes: 3 additions & 2 deletions include/RTTStar.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include <chrono> // NOLINT [build/c++11]
#include <map>
#include <memory>
#include <random>
#include <tuple>
#include <vector>
Expand All @@ -13,7 +14,7 @@
*/
class RTTStar {
public:
explicit RTTStar(const Costmap2D &cost_map, double_t epsilon,
explicit RTTStar(const std::unique_ptr<CostMap2D> cost_map, double_t epsilon,
double_t radius) noexcept;

std::vector<Point2D> initial_path(const Point2D &start, const Point2D &goal);
Expand All @@ -29,7 +30,7 @@ class RTTStar {
Point2D steer(const Point2D &x_nearest, const Point2D &x);
bool obstacle_free(const Point2D &x_nearest, const Point2D &x_new);

const Costmap2D &cost_map;
const std::unique_ptr<CostMap2D> cost_map;
std::optional<Point2D> start_point, end_point;
std::map<Point2D, double_t> costs;
std::map<Point2D, Point2D> relations;
Expand Down
12 changes: 7 additions & 5 deletions main.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
#include <algorithm>
#include <iostream>
#include <memory>
#include <numeric>
#include <tuple>
#include <utility>
#include <vector>

#include "CostMap2D.hpp"
Expand Down Expand Up @@ -42,8 +44,8 @@ Point2D prompt_point() {
* @param border_size The size of the borders.
* @return The populated cost map.
*/
Costmap2D make_cost_map(const size_t &border_size) {
Costmap2D cost_map;
std::unique_ptr<CostMap2D> make_cost_map(const size_t &border_size) {
auto cost_map = std::make_unique<CostMap2D>();

std::vector<size_t> indices(border_size);
std::iota(indices.begin(), indices.end(), 0);
Expand All @@ -58,7 +60,7 @@ Costmap2D make_cost_map(const size_t &border_size) {
std::for_each(indices.begin(), indices.end(),
[&cost_map, &distribution, &generator](const auto &i) {
if (distribution(generator)) {
cost_map.update(std::make_tuple(i, 0), true);
cost_map->update(std::make_tuple(i, 0), true);
}
});
});
Expand All @@ -69,7 +71,7 @@ Costmap2D make_cost_map(const size_t &border_size) {
int main() {
const auto border_size =
prompt<size_t>("Enter the size of the boarder square: ");
const auto cost_map = make_cost_map(border_size);
auto cost_map = make_cost_map(border_size);
const auto step_size =
prompt<double_t>("Enter the step size (i.e. double): ");
const auto radius = prompt<double_t>(
Expand All @@ -78,7 +80,7 @@ int main() {
"Enter the amount of time the search should continue for after getting "
"an initial path (in milliseconds): ");

RTTStar rtt_star(cost_map, step_size, radius);
RTTStar rtt_star(std::move(cost_map), step_size, radius);

std::cout << "Where should the algorithm search start point be?" << std::endl;
const auto start_point = prompt_point();
Expand Down
8 changes: 4 additions & 4 deletions src/CostMap2D.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
* @param value The value to associate with a point.
* @return Returns the old value if there is one.
*/
std::optional<bool> Costmap2D::update(const Point2D &point, const bool &value) {
std::optional<bool> CostMap2D::update(const Point2D &point, const bool &value) {
// If the minimum values was previously set
if (this->max_y.has_value() && this->max_x.has_value() &&
this->min_y.has_value() && this->min_x.has_value()) {
Expand Down Expand Up @@ -43,7 +43,7 @@ std::optional<bool> Costmap2D::update(const Point2D &point, const bool &value) {
* @param point The point to get the value of.
* @return The value associated with a point.
*/
std::optional<bool> Costmap2D::get(const Point2D &point) const {
std::optional<bool> CostMap2D::get(const Point2D &point) const {
const auto loc = this->sparse_costmap.find(point);
return loc != this->sparse_costmap.end() ? std::optional<bool>{loc->second}
: std::nullopt;
Expand All @@ -56,7 +56,7 @@ std::optional<bool> Costmap2D::get(const Point2D &point) const {
* @return The random normal distribution of x && y values.
*/
std::tuple<std::uniform_int_distribution<>, std::uniform_int_distribution<>>
Costmap2D::sample_space(const std::vector<Point2D> &waypoints) const {
CostMap2D::sample_space(const std::vector<Point2D> &waypoints) const {
int64_t s_min_x, s_min_y, s_max_x, s_max_y;

// If there are no values, the sample space should not be infinite
Expand Down Expand Up @@ -97,6 +97,6 @@ Costmap2D::sample_space(const std::vector<Point2D> &waypoints) const {
* @param point The point to check for.
* @return true if the value is in the cost map; false otherwise.
*/
bool Costmap2D::contains(const Point2D &point) const {
bool CostMap2D::contains(const Point2D &point) const {
return this->sparse_costmap.contains(point);
}
12 changes: 7 additions & 5 deletions src/RTTStar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

#include <algorithm>
#include <map>
#include <memory>
#include <numeric>
#include <utility>

/**
* Constructs the RRT Star class.
Expand All @@ -12,9 +14,9 @@
* nearest point.
* @param radius The radius to select nearby points from.
*/
RTTStar::RTTStar(const Costmap2D &cost_map, double_t epsilon,
RTTStar::RTTStar(std::unique_ptr<CostMap2D> cost_map, double_t epsilon,
double_t radius) noexcept
: cost_map(cost_map), eps(epsilon), radius(radius) {
: cost_map(std::move(cost_map)), eps(epsilon), radius(radius) {
this->gen = std::mt19937(std::random_device()());
}

Expand All @@ -35,7 +37,7 @@ std::vector<Point2D> RTTStar::initial_path(const Point2D &start,
this->costs.insert(std::make_pair(start, 0.0));

std::tie(this->x_sample_space, this->y_sample_space) =
this->cost_map.sample_space(std::vector{start, goal});
this->cost_map->sample_space(std::vector{start, goal});

while (!this->costs.contains(goal) && !this->relations.contains(goal)) {
this->iterate();
Expand Down Expand Up @@ -76,7 +78,7 @@ void RTTStar::iterate() {
std::make_tuple(x_sample_space(this->gen), y_sample_space(this->gen));

// Check if the random point is valid || has already been selected
if (this->cost_map.get(x) == std::nullopt && !costs.contains(x)) {
if (this->cost_map->get(x) == std::nullopt && !costs.contains(x)) {
// Get the point nearest to the new point
const Point2D x_nearest = this->nearest(x);
// Adjust the distance of the new point
Expand Down Expand Up @@ -201,7 +203,7 @@ bool RTTStar::obstacle_free(const Point2D &x_nearest, const Point2D &x_new) {
// Check that none of the points are in the cost map
return std::all_of(
traversed.begin(), traversed.end(),
[this](const auto &pt) { return !this->cost_map.contains(pt); });
[this](const auto &pt) { return !this->cost_map->contains(pt); });
}

/**
Expand Down

0 comments on commit 34ff819

Please sign in to comment.