diff --git a/src/impl/vamp/planning/cost.hh b/src/impl/vamp/planning/cost.hh new file mode 100644 index 00000000..fb548609 --- /dev/null +++ b/src/impl/vamp/planning/cost.hh @@ -0,0 +1,41 @@ +#pragma once + +#include +#include + +namespace vamp::planning +{ + VAMP_DEFINE_HAS_METHOD(cost) + + template + [[nodiscard]] inline auto + cost(const typename Robot::Configuration &a, const typename Robot::Configuration &b) noexcept -> float + { + if constexpr (has_cost_v) + { + return Robot::cost(a, b); + } + else + { + return robots::distance(a, b); + } + } + + template + [[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) + { + return cost(prev, mid_new) + cost(mid_new, next) <= + cost(prev, mid_old) + cost(mid_old, next); + } + else + { + return true; + } + } +} // namespace vamp::planning diff --git a/src/impl/vamp/planning/phs.hh b/src/impl/vamp/planning/phs.hh index 48e4f2b1..e6665627 100644 --- a/src/impl/vamp/planning/phs.hh +++ b/src/impl/vamp/planning/phs.hh @@ -10,6 +10,7 @@ #include #include #include +#include #include namespace vamp::planning @@ -162,20 +163,31 @@ namespace vamp::planning inline auto next() noexcept -> FloatVector override { - auto x = phs.transform(uniform_in_ball()); + if constexpr (robots::is_euclidean_v) + { + 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 { auto U1 = rng->next(); - Robot::descale_configuration(U1); + if constexpr (robots::is_euclidean_v) + { + Robot::descale_configuration(U1); + } return (U1 * (1 - U1).rcp()).log() * std::sqrt(vamp::utils::constants::pi / 8.F); } diff --git a/src/impl/vamp/planning/plan.hh b/src/impl/vamp/planning/plan.hh index 7b9ac60f..07e8a7e7 100644 --- a/src/impl/vamp/planning/plan.hh +++ b/src/impl/vamp/planning/plan.hh @@ -1,155 +1,161 @@ #pragma once #include +#include #include +#include #include #include namespace vamp::planning { - template - struct Path : public std::vector> + namespace path_helpers { - [[nodiscard]] inline auto cost() const noexcept -> float + template + [[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(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(wps.front(), wps.back()); } - return std::numeric_limits::infinity(); } - inline auto subdivide() noexcept + template + inline auto subdivide_robot(Container &wps) noexcept { - Path 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 ¤t = 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(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 + 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 new_path; - new_path.reserve(n); - - std::vector segment_lengths(n_p - 1); + std::vector 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(wps[i], wps[i + 1]); + remaining_length += seg_lengths[i]; } - if (remaining_length < std::numeric_limits::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(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::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(k) / ns) * v); + next.emplace_back( + robots::interpolate(a, b, static_cast(k) / static_cast(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 + 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(path_cost * static_cast(resolution)); - - Path 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 ¤t = 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(a, b); const auto segment_states = static_cast(segment_cost * static_cast(resolution)); - - new_path.emplace_back(current); - + next.emplace_back(a); if (segment_cost < 1.F / static_cast(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(i) / static_cast(segment_states))); + next.emplace_back( + robots::interpolate( + a, b, static_cast(k) / static_cast(segment_states))); } } + next.emplace_back(wps.back()); + wps = std::move(next); + } + } // namespace path_helpers + + template + struct Path : public std::vector> + { + [[nodiscard]] inline auto cost() const noexcept -> float + { + return path_helpers::cost_robot(*this); + } + + inline auto subdivide() noexcept + { + path_helpers::subdivide_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(*this, n); + } + + inline auto interpolate_to_resolution(std::size_t resolution) noexcept + { + path_helpers::interpolate_to_resolution_robot(*this, resolution); } template diff --git a/src/impl/vamp/planning/validate.hh b/src/impl/vamp/planning/validate.hh index 0f011d6c..2fd9a200 100644 --- a/src/impl/vamp/planning/validate.hh +++ b/src/impl/vamp/planning/validate.hh @@ -3,6 +3,7 @@ #include #include +#include #include #include @@ -20,6 +21,17 @@ namespace vamp::planning inline static constexpr auto percents = generate_percents(std::make_index_sequence()); }; + // Collision-check a block, dispatching to the attachment-aware kernel when the + // environment carries attachments. True when every lane is collision-free. + template + inline auto fkcc_block( + const collision::Environment> &environment, + const typename Robot::template ConfigurationBlock &block) noexcept -> bool + { + return (environment.attachments) ? Robot::template fkcc_attach(environment, block) : + Robot::template fkcc(environment, block); + } + template inline constexpr auto validate_vector( const typename Robot::Configuration &start, @@ -40,8 +52,7 @@ namespace vamp::planning const std::size_t n = std::max(std::ceil(distance / static_cast(rake) * resolution), 1.F); - bool valid = (environment.attachments) ? Robot::template fkcc_attach(environment, block) : - Robot::template fkcc(environment, block); + bool valid = fkcc_block(environment, block); if (not valid or n == 1) { return valid; @@ -55,8 +66,7 @@ namespace vamp::planning block[j] = block[j] - backstep.broadcast(j); } - bool valid = (environment.attachments) ? Robot::template fkcc_attach(environment, block) : - Robot::template fkcc(environment, block); + bool valid = fkcc_block(environment, block); if (not valid) { return false; @@ -72,7 +82,43 @@ namespace vamp::planning const typename Robot::Configuration &goal, const collision::Environment> &environment) -> bool { - auto vector = goal - start; - return validate_vector(start, vector, vector.l2_norm(), environment); + if constexpr (robots::is_euclidean_v) + { + auto vector = goal - start; + return validate_vector(start, vector, vector.l2_norm(), environment); + } + else + { + // Joint-aware motion validation: interpolate via Robot::interpolate_block. + // Total sub-states needed: ceil(distance * resolution); processed in batches of `rake`. + const float distance = Robot::distance(start, goal); + const std::size_t n = std::max(std::ceil(distance / static_cast(rake) * resolution), 1.F); + const auto percents = FloatVector(Percents::percents); + const auto t_step = FloatVector::fill(1.F / static_cast(rake * n)); + + typename Robot::template ConfigurationBlock block; + auto t_block = percents; + Robot::template interpolate_block(start, goal, t_block, block); + + bool valid = fkcc_block(environment, block); + if (not valid or n == 1) + { + return valid; + } + + for (auto i = 1U; i < n; ++i) + { + t_block = t_block - t_step; + Robot::template interpolate_block(start, goal, t_block, block); + + valid = fkcc_block(environment, block); + if (not valid) + { + return false; + } + } + + return true; + } } } // namespace vamp::planning diff --git a/src/impl/vamp/random/halton.hh b/src/impl/vamp/random/halton.hh index cb6a53af..e4e4bfa3 100644 --- a/src/impl/vamp/random/halton.hh +++ b/src/impl/vamp/random/halton.hh @@ -2,6 +2,7 @@ #include #include +#include namespace vamp::rng { @@ -12,30 +13,22 @@ namespace vamp::rng static constexpr const std::size_t max_iterations = 1000000U; using Configuration = typename Robot::Configuration; - - static constexpr const std::array primes{ - 3.F, - 5.F, - 7.F, - 11.F, - 13.F, - 17.F, - 19.F, - 23.F, - 29.F, - 31.F, - 37.F, - 41.F, - 43.F, - 47.F, - 53.F, - 59.F}; - - explicit Halton(Configuration b_in) noexcept : b_init(b_in), b(b_in) + using Sample = FloatVector>; + + // First 40 primes (excluding 2 — Halton avoids it because the base-2 + // sequence has poor stratification at coarse subdivisions). Robots with a + // floating base can have sample dimensions well above 16; extend if you need + // more than 40. + static constexpr const std::array primes{ + 3.F, 5.F, 7.F, 11.F, 13.F, 17.F, 19.F, 23.F, 29.F, 31.F, 37.F, 41.F, 43.F, 47.F, + 53.F, 59.F, 61.F, 67.F, 71.F, 73.F, 79.F, 83.F, 89.F, 97.F, 101.F, 103.F, 107.F, 109.F, + 113.F, 127.F, 131.F, 137.F, 139.F, 149.F, 151.F, 157.F, 163.F, 167.F, 173.F, 179.F}; + + explicit Halton(Sample b_in) noexcept : b_init(b_in), b(b_in) { } - Halton(std::initializer_list v) noexcept : Halton(Configuration::pack_and_pad(v)) + Halton(std::initializer_list v) noexcept : Halton(Sample::pack_and_pad(v)) { } @@ -45,33 +38,33 @@ namespace vamp::rng virtual ~Halton() = default; - inline constexpr auto bases() noexcept -> Configuration + inline constexpr auto bases() noexcept -> Sample { - alignas(FloatVectorAlignment) std::array a; - std::copy_n(primes.cbegin(), Robot::dimension, a.begin()); - return Configuration(a); + alignas(FloatVectorAlignment) std::array> a; + std::copy_n(primes.cbegin(), robots::sample_dimension_v, a.begin()); + return Sample(a); } auto rotate_bases() noexcept { - alignas(FloatVectorAlignment) std::array a; + alignas(FloatVectorAlignment) std::array a; b.to_array(a.data()); - std::rotate(a.begin(), a.begin() + 1, a.begin() + Robot::dimension); - b = Configuration(a.data()); + std::rotate(a.begin(), a.begin() + 1, a.begin() + robots::sample_dimension_v); + b = Sample(a.data()); } - const Configuration b_init; - Configuration b; - Configuration n = Configuration::fill(0); - Configuration d = Configuration::fill(1); + const Sample b_init; + Sample b; + Sample n = Sample::fill(0); + Sample d = Sample::fill(1); std::size_t iterations = 0; inline void reset() noexcept override final { iterations = 0; b = b_init; - n = Configuration::fill(0); - d = Configuration::fill(1); + n = Sample::fill(0); + d = Sample::fill(1); } inline auto next() noexcept -> Configuration override final @@ -79,8 +72,8 @@ namespace vamp::rng iterations++; if (iterations > max_iterations) { - n = Configuration::fill(0); - d = Configuration::fill(1); + n = Sample::fill(0); + d = Sample::fill(1); iterations = 0; rotate_bases(); } @@ -102,11 +95,9 @@ namespace vamp::rng x_le_y = x_le_y & (xf <= y); } - n = (((b + 1.F) * y).floor() - xf).blend(Configuration::fill(1), x_eq_1); + n = (((b + 1.F) * y).floor() - xf).blend(Sample::fill(1), x_eq_1); - auto result = (n / d).trim(); - Robot::scale_configuration(result); - return result; + return robots::sample((n / d).trim()); } }; } // namespace vamp::rng diff --git a/src/impl/vamp/random/xorshift.hh b/src/impl/vamp/random/xorshift.hh index cd307a52..b309bf00 100644 --- a/src/impl/vamp/random/xorshift.hh +++ b/src/impl/vamp/random/xorshift.hh @@ -8,6 +8,7 @@ extern "C" #include #include #include +#include namespace vamp::rng { @@ -26,7 +27,7 @@ namespace vamp::rng uint64_t key2_init; avx_xorshift128plus_key_t key{}; - using IntVector = Vector, 1, Robot::dimension>; + using IntVector = Vector, 1, robots::sample_dimension_v>; IntVector buffer; inline void reset() noexcept override final @@ -41,9 +42,8 @@ namespace vamp::rng buffer.data[i] = avx_xorshift128plus(&key); } - auto result = FloatVector::map_to_range(buffer, 0.F, 1.F); - Robot::scale_configuration(result); - return result; + return robots::sample( + FloatVector>::map_to_range(buffer, 0.F, 1.F)); } }; } // namespace vamp::rng diff --git a/src/impl/vamp/robots/traits.hh b/src/impl/vamp/robots/traits.hh new file mode 100644 index 00000000..4c5a95ec --- /dev/null +++ b/src/impl/vamp/robots/traits.hh @@ -0,0 +1,142 @@ +#pragma once + +#include +#include +#include + +#include + +// Optional robot hooks for planning in non-Euclidean configuration spaces (e.g. floating bases with +// quaternion dimensions). A robot may define: +// +// static constexpr bool euclidean; // joint space is a flat L2 space +// static constexpr std::size_t sample_dimension; // dimension of the sampler's unit cube +// static auto sample(FloatVector) -> Configuration; // unit cube -> configuration +// static auto distance(const Configuration &, const Configuration &) -> float; +// static auto interpolate(const Configuration &, const Configuration &, float t) -> Configuration; +// +// Robots that define none of them are Euclidean, sample the configuration cube directly, and use +// Configuration::distance and Configuration::interpolate. The helpers below resolve each hook with +// that fallback, so planning code can be written once for both kinds of robot. +namespace vamp::robots +{ + namespace detail + { + template + struct euclidean : std::true_type + { + }; + + template + struct euclidean> + : std::bool_constant + { + }; + + template + struct sample_dimension : std::integral_constant + { + }; + + template + struct sample_dimension> + : std::integral_constant + { + }; + + template + struct has_sample : std::false_type + { + }; + + template + struct has_sample< + Robot, + std::void_t::value> &>()))>> : std::true_type + { + }; + + template + struct has_distance : std::false_type + { + }; + + template + struct has_distance< + Robot, + std::void_t(), + std::declval()))>> : std::true_type + { + }; + + template + struct has_interpolate : std::false_type + { + }; + + template + struct has_interpolate< + Robot, + std::void_t(), + std::declval(), + 0.F))>> : std::true_type + { + }; + } // namespace detail + + template + inline constexpr bool is_euclidean_v = detail::euclidean::value; + + template + inline constexpr std::size_t sample_dimension_v = detail::sample_dimension::value; + + // Map a point of the sampler's unit cube to a configuration. + template + [[nodiscard]] inline auto sample(const FloatVector> &unit) noexcept -> + typename Robot::Configuration + { + if constexpr (detail::has_sample::value) + { + return Robot::sample(unit); + } + else + { + auto configuration = unit; + Robot::scale_configuration(configuration); + return configuration; + } + } + + template + [[nodiscard]] inline auto + distance(const typename Robot::Configuration &a, const typename Robot::Configuration &b) noexcept -> float + { + if constexpr (detail::has_distance::value) + { + return Robot::distance(a, b); + } + else + { + return a.distance(b); + } + } + + template + [[nodiscard]] inline auto interpolate( + const typename Robot::Configuration &a, + const typename Robot::Configuration &b, + float t) noexcept -> typename Robot::Configuration + { + if constexpr (detail::has_interpolate::value) + { + return Robot::interpolate(a, b, t); + } + else + { + return a.interpolate(b, t); + } + } +} // namespace vamp::robots diff --git a/src/impl/vamp/utils.hh b/src/impl/vamp/utils.hh index 9c4e84a9..4c80ac90 100644 --- a/src/impl/vamp/utils.hh +++ b/src/impl/vamp/utils.hh @@ -5,6 +5,20 @@ #include #include +#define VAMP_DEFINE_HAS_METHOD(method_name) \ + template \ + struct has_##method_name : std::false_type \ + { \ + }; \ + \ + template \ + struct has_##method_name> : std::true_type \ + { \ + }; \ + \ + template \ + constexpr bool has_##method_name##_v = has_##method_name::value; + namespace vamp::utils { inline constexpr auto round_size(std::size_t size, std::size_t block) noexcept -> std::size_t