Loading...
Searching...
No Matches
ConnectionStrategy.h
1/*********************************************************************
2 * Software License Agreement (BSD License)
3 *
4 * Copyright (c) 2011, Rice University
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 *
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
17 * * Neither the name of the Rice University nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 * POSSIBILITY OF SUCH DAMAGE.
33 *********************************************************************/
34
35/* Author: James D. Marble */
36
37#ifndef OMPL_GEOMETRIC_PLANNERS_PRM_CONNECTION_STRATEGY_
38#define OMPL_GEOMETRIC_PLANNERS_PRM_CONNECTION_STRATEGY_
39
40#include "ompl/datastructures/NearestNeighbors.h"
41#include <functional>
42#include <memory>
43#include <boost/math/constants/constants.hpp>
44#include <algorithm>
45#include <utility>
46#include <vector>
47
48namespace ompl
49{
50 namespace geometric
51 {
55 template <class Milestone>
57 {
58 public:
61 KStrategy(const unsigned int k, std::shared_ptr<NearestNeighbors<Milestone>> nn) : k_(k), nn_(std::move(nn))
62 {
63 neighbors_.reserve(k_);
64 }
65
66 virtual ~KStrategy() = default;
67
69 void setNearestNeighbors(const std::shared_ptr<NearestNeighbors<Milestone>> &nn)
70 {
71 nn_ = nn;
72 }
73
77 const std::vector<Milestone> &operator()(const Milestone &m)
78 {
79 nn_->nearestK(m, k_, neighbors_);
80 return neighbors_;
81 }
82
83 unsigned int getNumNeighbors() const
84 {
85 return k_;
86 }
87
88 protected:
90 unsigned int k_;
91
93 std::shared_ptr<NearestNeighbors<Milestone>> nn_;
94
96 std::vector<Milestone> neighbors_;
97 };
98
124 template <class Milestone>
125 class KStarStrategy : public KStrategy<Milestone>
126 {
127 public:
128 using NumNeighborsFn = std::function<unsigned int()>;
138 KStarStrategy(const NumNeighborsFn &n, const std::shared_ptr<NearestNeighbors<Milestone>> &nn,
139 const unsigned int d = 1)
140 : KStrategy<Milestone>(n(), nn)
141 , n_(n)
142 , kPRMConstant_(boost::math::constants::e<double>() + (boost::math::constants::e<double>() / (double)d))
143 {
144 }
145
146 const std::vector<Milestone> &operator()(const Milestone &m)
147 {
148 KStrategy<Milestone>::k_ = static_cast<unsigned int>(ceil(kPRMConstant_ * log((double)n_())));
149 return static_cast<KStrategy<Milestone> &>(*this)(m);
150 }
151
152 protected:
154 const NumNeighborsFn n_;
155 const double kPRMConstant_;
156 };
157
161 template <class Milestone>
162 class KBoundedStrategy : public KStrategy<Milestone>
163 {
164 public:
172 KBoundedStrategy(const unsigned int k, const double bound,
173 const std::shared_ptr<NearestNeighbors<Milestone>> &nn)
174 : KStrategy<Milestone>(k, nn), bound_(bound)
175 {
176 }
177
178 const std::vector<Milestone> &operator()(const Milestone &m)
179 {
182 if (result.empty())
183 return result;
184 const auto &dist = KStrategy<Milestone>::nn_->getDistanceFunction();
185 if (!KStrategy<Milestone>::nn_->reportsSortedResults())
186 std::sort(result.begin(), result.end(), dist);
187 auto newCount = result.size();
188 while (newCount > 0 && dist(result[newCount - 1], m) > bound_)
189 --newCount;
190 result.resize(newCount);
191 return result;
192 }
193
194 protected:
196 const double bound_;
197 };
198 } // namespace geometric
199} // namespace ompl
200
201#endif
Abstract representation of a container that can perform nearest neighbors queries.
const double bound_
The maximum distance at which nearby milestones are reported.
KBoundedStrategy(const unsigned int k, const double bound, const std::shared_ptr< NearestNeighbors< Milestone > > &nn)
Constructor.
KStarStrategy(const NumNeighborsFn &n, const std::shared_ptr< NearestNeighbors< Milestone > > &nn, const unsigned int d=1)
Constructor.
const NumNeighborsFn n_
Function returning the number of milestones added to the roadmap so far.
void setNearestNeighbors(const std::shared_ptr< NearestNeighbors< Milestone > > &nn)
Set the nearest neighbors datastructure to use.
std::shared_ptr< NearestNeighbors< Milestone > > nn_
Nearest neighbors data structure.
const std::vector< Milestone > & operator()(const Milestone &m)
Given a milestone m, find the number of nearest neighbors connection attempts that should be made fro...
unsigned int k_
Maximum number of nearest neighbors to attempt to connect new milestones to.
KStrategy(const unsigned int k, std::shared_ptr< NearestNeighbors< Milestone > > nn)
Constructor takes the maximum number of nearest neighbors to return (k) and the nearest neighbors dat...
std::vector< Milestone > neighbors_
Scratch space for storing k-nearest neighbors.
This namespace contains code that is specific to planning under geometric constraints.
Main namespace. Contains everything in this library.
STL namespace.