Skip to content
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
41 changes: 41 additions & 0 deletions src/impl/vamp/planning/cost.hh
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#pragma once

#include <vamp/robots/traits.hh>
#include <vamp/utils.hh>

namespace vamp::planning
{
VAMP_DEFINE_HAS_METHOD(cost)

template <typename Robot>
[[nodiscard]] inline auto
cost(const typename Robot::Configuration &a, const typename Robot::Configuration &b) noexcept -> float
{
if constexpr (has_cost_v<Robot>)
{
return Robot::cost(a, b);
}
else
{
return robots::distance<Robot>(a, b);
}
}

template <typename Robot>
[[nodiscard]] inline auto cost_nonincreasing(
const typename Robot::Configuration &prev,
const typename Robot::Configuration &mid_old,
const typename Robot::Configuration &mid_new,
const typename Robot::Configuration &next) noexcept -> bool
{
if constexpr (has_cost_v<Robot>)
{
return cost<Robot>(prev, mid_new) + cost<Robot>(mid_new, next) <=
cost<Robot>(prev, mid_old) + cost<Robot>(mid_old, next);
}
else
{
return true;
}
}
} // namespace vamp::planning
26 changes: 19 additions & 7 deletions src/impl/vamp/planning/phs.hh
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include <vamp/random/rng.hh>
#include <vamp/planning/roadmap.hh>
#include <vamp/vector/eigen.hh>
#include <vamp/robots/traits.hh>
#include <vamp/vector.hh>

namespace vamp::planning
Expand Down Expand Up @@ -162,20 +163,31 @@ namespace vamp::planning

inline auto next() noexcept -> FloatVector<Robot::dimension> override
{
auto x = phs.transform(uniform_in_ball());
if constexpr (robots::is_euclidean_v<Robot>)
{
auto x = phs.transform(uniform_in_ball());

// Clamp values
Robot::descale_configuration(x);
x = x.clamp(0.F, 1.F);
Robot::scale_configuration(x);
// Clamp to joint bounds via descale -> [0,1] -> scale
Robot::descale_configuration(x);
x = x.clamp(0.F, 1.F);
Robot::scale_configuration(x);

return x;
return x;
}
else
{
// Informed (PHS) sampling assumes a Euclidean L2 metric.
return rng->next();
}
}

inline auto logit() noexcept -> vamp::FloatVector<Robot::dimension>
{
auto U1 = rng->next();
Robot::descale_configuration(U1);
if constexpr (robots::is_euclidean_v<Robot>)
{
Robot::descale_configuration(U1);
}
return (U1 * (1 - U1).rcp()).log() * std::sqrt(vamp::utils::constants::pi / 8.F);
}

Expand Down
170 changes: 88 additions & 82 deletions src/impl/vamp/planning/plan.hh
Original file line number Diff line number Diff line change
@@ -1,155 +1,161 @@
#pragma once

#include <limits>
#include <vamp/planning/cost.hh>
#include <vamp/planning/validate.hh>
#include <vamp/robots/traits.hh>
#include <vamp/planning/nn.hh>
#include <vamp/vector.hh>

namespace vamp::planning
{
template <typename Robot>
struct Path : public std::vector<FloatVector<Robot::dimension>>
namespace path_helpers
{
[[nodiscard]] inline auto cost() const noexcept -> float
template <typename Robot, typename Container>
[[nodiscard]] inline auto cost_robot(const Container &wps) noexcept -> float
{
if (this->size() > 2)
const auto n = wps.size();
if (n > 2)
{
float distance = 0;
for (auto i = 0U; i < this->size() - 1; ++i)
float total = 0;
for (auto i = 0U; i + 1 < n; ++i)
{
distance += this->operator[](i).distance(this->operator[](i + 1));
total += planning::cost<Robot>(wps[i], wps[i + 1]);
}

return distance;
return total;
}

if (this->size() == 2)
if (n == 2)
{
return this->front().distance(this->back());
return planning::cost<Robot>(wps.front(), wps.back());
}

return std::numeric_limits<float>::infinity();
}

inline auto subdivide() noexcept
template <typename Robot, typename Container>
inline auto subdivide_robot(Container &wps) noexcept
{
Path<Robot> new_path;
new_path.reserve(this->size() * 2);

for (auto i = 0U; i < this->size() - 1; ++i)
const auto n = wps.size();
if (n < 2)
{
const auto &current = this->operator[](i);
const auto &next = this->operator[](i + 1);
new_path.emplace_back(current);
new_path.emplace_back(current.interpolate(next, 0.5));
return;
}

new_path.emplace_back(this->back());
this->swap(new_path);
Container next;
next.reserve(n * 2);
for (auto i = 0U; i + 1 < n; ++i)
{
next.emplace_back(wps[i]);
next.emplace_back(robots::interpolate<Robot>(wps[i], wps[i + 1], 0.5F));
}
next.emplace_back(wps.back());
wps = std::move(next);
}

inline auto interpolate_to_n_states(std::size_t n) noexcept
template <typename Robot, typename Container>
inline auto interpolate_to_n_states_robot(Container &wps, std::size_t n) noexcept
{
const std::size_t n_p = this->size();
if (this->size() < 2 or n < n_p)
const auto n_p = wps.size();
if (n_p < 2 or n < n_p)
{
return;
}

Path<Robot> new_path;
new_path.reserve(n);

std::vector<float> segment_lengths(n_p - 1);
std::vector<float> seg_lengths(n_p - 1);
float remaining_length = 0.;

for (auto i = 0U; i < n_p - 1; ++i)
for (auto i = 0U; i + 1 < n_p; ++i)
{
remaining_length += segment_lengths[i] =
this->operator[](i).distance(this->operator[](i + 1));
seg_lengths[i] = robots::distance<Robot>(wps[i], wps[i + 1]);
remaining_length += seg_lengths[i];
}

if (remaining_length < std::numeric_limits<float>::epsilon())
{
return;
}

Container next;
next.reserve(n);
const auto n1 = n_p - 1;
for (auto i = 0U; i < n1; ++i)
{
const auto &a = this->operator[](i);
const auto &b = this->operator[](i + 1);

new_path.emplace_back(a);
const auto &a = wps[i];
const auto &b = wps[i + 1];
next.emplace_back(a);
const auto max_n_states = n + i - n_p;

if (max_n_states > 0)
{
auto ns =
(i + 1 == n1) ?
(max_n_states + 2) :
(static_cast<int>(std::floor(0.5 + n * segment_lengths[i] / remaining_length)) +
1);

// more than endpoints needed
auto ns = (i + 1 == n1) ? (max_n_states + 2) :
(static_cast<std::size_t>(
std::floor(0.5 + n * seg_lengths[i] / remaining_length)) +
1);
ns = (ns > 2) ? std::min(ns - 2, max_n_states) : 0;

const auto &v = b - a;
for (auto k = 1U; k <= ns; ++k)
{
new_path.emplace_back(a + (static_cast<float>(k) / ns) * v);
next.emplace_back(
robots::interpolate<Robot>(a, b, static_cast<float>(k) / static_cast<float>(ns)));
}

n -= ns + 1;
remaining_length -= segment_lengths[i];
remaining_length -= seg_lengths[i];
}
else
{
n -= 1;
}
}

new_path.emplace_back(this->back());
this->swap(new_path);
next.emplace_back(wps.back());
wps = std::move(next);
}

inline auto interpolate_to_resolution(std::size_t resolution) noexcept
template <typename Robot, typename Container>
inline auto interpolate_to_resolution_robot(Container &wps, std::size_t resolution) noexcept
{
if (this->size() < 2)
const auto n_p = wps.size();
if (n_p < 2)
{
return;
}

const float path_cost = cost();
const auto n_states = static_cast<std::size_t>(path_cost * static_cast<float>(resolution));

Path<Robot> new_path;
new_path.reserve(n_states);

for (auto i = 0U; i < this->size() - 1; ++i)
Container next;
for (auto i = 0U; i + 1 < n_p; ++i)
{
const auto &current = this->operator[](i);
const auto &next = this->operator[](i + 1);

const float segment_cost = current.distance(next);
const auto &a = wps[i];
const auto &b = wps[i + 1];
const float segment_cost = robots::distance<Robot>(a, b);
const auto segment_states =
static_cast<std::size_t>(segment_cost * static_cast<float>(resolution));

new_path.emplace_back(current);

next.emplace_back(a);
if (segment_cost < 1.F / static_cast<float>(resolution))
{
continue;
}

for (auto i = 1U; i < segment_states; ++i)
for (auto k = 1U; k < segment_states; ++k)
{
new_path.emplace_back(current.interpolate(
next, static_cast<float>(i) / static_cast<float>(segment_states)));
next.emplace_back(
robots::interpolate<Robot>(
a, b, static_cast<float>(k) / static_cast<float>(segment_states)));
}
}
next.emplace_back(wps.back());
wps = std::move(next);
}
} // namespace path_helpers

template <typename Robot>
struct Path : public std::vector<FloatVector<Robot::dimension>>
{
[[nodiscard]] inline auto cost() const noexcept -> float
{
return path_helpers::cost_robot<Robot>(*this);
}

inline auto subdivide() noexcept
{
path_helpers::subdivide_robot<Robot>(*this);
}

new_path.emplace_back(this->back());
this->swap(new_path);
inline auto interpolate_to_n_states(std::size_t n) noexcept
{
path_helpers::interpolate_to_n_states_robot<Robot>(*this, n);
}

inline auto interpolate_to_resolution(std::size_t resolution) noexcept
{
path_helpers::interpolate_to_resolution_robot<Robot>(*this, resolution);
}

template <std::size_t rake>
Expand Down
Loading
Loading