36#include "ompl/geometric/planners/lazyinformedtrees/blitstar/Vertex.h"
43#include "ompl/base/goals/GoalState.h"
44#include "ompl/base/goals/GoalStates.h"
46using namespace std::string_literals;
56 std::size_t generateId()
58 static std::atomic<std::size_t>
id{0u};
63 Vertex::Vertex(
const ompl::base::SpaceInformationPtr &spaceInformation,
64 const ompl::base::ProblemDefinitionPtr &problemDefinition,
const std::size_t &batchId)
65 : spaceInformation_(spaceInformation)
66 , problemDefinition_(problemDefinition)
67 , objective_(problemDefinition->getOptimizationObjective())
72 , state_(spaceInformation->allocState())
73 , costToComeFromStart_(objective_->infiniteCost())
74 , costToComeFromGoal_(objective_->infiniteCost())
75 , edgeCostFromForwardParent_(objective_->infiniteCost())
76 , expandedCostToComeFromGoal_(objective_->infiniteCost())
77 , costToGoToGoal_(objective_->infiniteCost())
78 , costToGoToStart_(objective_->infiniteCost())
79 , vertexId_(generateId())
84 Vertex::Vertex(
const std::shared_ptr<Vertex> &other)
85 : spaceInformation_(other->spaceInformation_)
86 , problemDefinition_(other->problemDefinition_)
87 , objective_(other->objective_)
88 , forwardChildren_(other->forwardChildren_)
89 , reverseChildren_(other->reverseChildren_)
90 , forwardParent_(other->forwardParent_)
91 , reverseParent_(other->reverseParent_)
92 , state_(spaceInformation_->allocState())
93 , costToComeFromStart_(other->costToComeFromStart_)
94 , costToComeFromGoal_(other->costToComeFromGoal_)
95 , edgeCostFromForwardParent_(other->edgeCostFromForwardParent_)
96 , expandedCostToComeFromGoal_(other->expandedCostToComeFromGoal_)
97 , costToGoToGoal_(other->costToGoToGoal_)
98 , costToGoToStart_(other->costToGoToStart_)
99 , vertexId_(other->vertexId_)
100 , batchId_(other->batchId_)
102 spaceInformation_->copyState(state_, other->getState());
108 spaceInformation_->freeState(state_);
111 std::size_t Vertex::getId()
const
133 return costToComeFromStart_;
136 bool Vertex::isGoal()
141 bool Vertex::isStart()
145 void Vertex::setGoalVertex()
149 void Vertex::setStartVertex()
154 void Vertex::setForwardId(
const std::size_t counter)
156 ForwardVersion_ = counter;
158 std::size_t Vertex::getForwardId()
160 return ForwardVersion_;
162 void Vertex::resetForwardId()
164 ForwardVersion_ = 0u;
167 void Vertex::setReverseId(
const std::size_t counter)
169 ReverseVersion_ = counter;
171 std::size_t Vertex::getReverseId()
173 return ReverseVersion_;
175 void Vertex::resetReverseId()
177 ReverseVersion_ = 0u;
182 return costToComeFromGoal_;
192 return edgeCostFromForwardParent_;
197 return edgeCostFromValidForwardParent_;
202 return edgeCostFromValidReverseParent_;
207 return edgeCostFromBackwardParent_;
210 bool Vertex::hasForwardParent()
const
212 return static_cast<bool>(forwardParent_.lock());
215 bool Vertex::hasForwardEdgeParent()
const
217 return static_cast<bool>(forwardEdgeParent_.lock());
220 std::shared_ptr<Vertex> Vertex::getForwardParent()
const
222 return forwardParent_.lock();
225 std::shared_ptr<Vertex> Vertex::getForwardEdgeParent()
const
227 return forwardEdgeParent_.lock();
230 bool Vertex::hasReverseParent()
const
232 return static_cast<bool>(reverseParent_.lock());
235 bool Vertex::hasReverseEdgeParent()
const
237 return static_cast<bool>(reverseEdgeParent_.lock());
240 std::shared_ptr<Vertex> Vertex::getReverseParent()
const
242 return reverseParent_.lock();
245 std::shared_ptr<Vertex> Vertex::getReverseEdgeParent()
const
247 return reverseEdgeParent_.lock();
252 edgeCostFromForwardParent_ = cost;
257 costToComeFromStart_ = cost;
260 void Vertex::resetMeet()
264 void Vertex::setMeet()
268 bool Vertex::meetVertex()
275 costToComeFromGoal_ = cost;
278 void Vertex::resetCostToComeFromGoal()
280 costToComeFromGoal_ = objective_->infiniteCost();
283 void Vertex::resetCostToComeFromStart()
285 costToComeFromStart_ = objective_->infiniteCost();
288 void Vertex::setForwardInvalid()
290 forwardInvalidChildState_ =
true;
293 bool Vertex::forwardInvalid()
295 return forwardInvalidChildState_;
298 void Vertex::resetForwardInvalid()
300 forwardInvalidChildState_ =
false;
303 void Vertex::setReverseInvalid()
305 reverseInvalidChildState_ =
true;
308 bool Vertex::reverseInvalid()
310 return reverseInvalidChildState_;
313 void Vertex::resetReverseInvalid()
315 reverseInvalidChildState_ =
false;
320 costToGoToGoal_ = cost;
325 costToGoToStart_ = cost;
328 void Vertex::setNearObstacle()
330 nearObstacle_ =
true;
333 bool Vertex::nearObstacle()
335 return nearObstacle_;
338 void Vertex::resetNearObstacle()
340 nearObstacle_ =
false;
342 bool Vertex::isForwardExpanded()
344 return IsForwardExpanded_;
347 void Vertex::setForwardExpanded()
349 IsForwardExpanded_ =
true;
352 void Vertex::resetForwardExpanded()
354 IsForwardExpanded_ =
false;
357 bool Vertex::isReverseExpanded()
359 return IsReverseExpanded_;
362 void Vertex::setReverseExpanded()
364 IsReverseExpanded_ =
true;
367 void Vertex::resetReverseExpanded()
369 IsReverseExpanded_ =
false;
374 lowerCostBoundToGoToStart_ = ToStart;
379 lowerCostBoundToGoToGoal_ = ToGoal;
384 return lowerCostBoundToGoToStart_;
389 return lowerCostBoundToGoToGoal_;
392 void Vertex::setForwardValidParent(
const std::shared_ptr<Vertex> &vertex,
const ompl::base::Cost &edgeCost)
395 edgeCostFromValidForwardParent_ = edgeCost;
397 forwardEdgeParent_ = std::weak_ptr<Vertex>(vertex);
400 void Vertex::setReverseValidParent(
const std::shared_ptr<Vertex> &vertex,
const ompl::base::Cost &edgeCost)
403 edgeCostFromValidReverseParent_ = edgeCost;
405 reverseEdgeParent_ = std::weak_ptr<Vertex>(vertex);
408 void Vertex::resetForwardParent()
410 forwardParent_.reset();
413 void Vertex::resetForwardEdgeParent()
415 forwardEdgeParent_.reset();
418 void Vertex::setForwardVertexParent(
const std::shared_ptr<Vertex> &vertex,
const ompl::base::Cost &edgeCost)
421 if (
static_cast<bool>(forwardParent_.lock()))
425 forwardParent_.lock()->removeFromForwardChildren(vertexId_);
428 edgeCostFromForwardParent_ = edgeCost;
429 forwardParent_ = std::weak_ptr<Vertex>(vertex);
432 void Vertex::setReverseVertexParent(
const std::shared_ptr<Vertex> &vertex,
const ompl::base::Cost &edgeCost)
435 if (
static_cast<bool>(reverseParent_.lock()))
439 reverseParent_.lock()->removeFromReverseChildren(vertexId_);
442 edgeCostFromBackwardParent_ = edgeCost;
443 reverseParent_ = std::weak_ptr<Vertex>(vertex);
446 void Vertex::resetReverseParent()
448 reverseParent_.reset();
451 void Vertex::resetReverseEdgeParent()
453 reverseEdgeParent_.reset();
456 void Vertex::addToForwardChildren(
const std::shared_ptr<Vertex> &vertex)
458 forwardChildren_.emplace_back(vertex);
461 void Vertex::removeFromForwardChildren(std::size_t vertexId)
464 auto it = std::find_if(forwardChildren_.begin(), forwardChildren_.end(),
465 [vertexId](
const std::weak_ptr<Vertex> &child)
466 { return vertexId == child.lock()->getId(); });
469 if (it == forwardChildren_.end())
471 auto msg =
"Asked to remove vertex from forward children that is currently not a child."s;
476 std::iter_swap(it, forwardChildren_.rbegin());
477 forwardChildren_.pop_back();
480 void Vertex::addToReverseChildren(
const std::shared_ptr<Vertex> &vertex)
482 reverseChildren_.push_back(vertex);
485 void Vertex::removeFromReverseChildren(std::size_t vertexId)
488 auto it = std::find_if(reverseChildren_.begin(), reverseChildren_.end(),
489 [vertexId](
const std::weak_ptr<Vertex> &child)
490 { return vertexId == child.lock()->getId(); });
493 if (it == reverseChildren_.end())
495 auto msg =
"Asked to remove vertex from reverse children that is currently not a child."s;
500 std::iter_swap(it, reverseChildren_.rbegin());
501 reverseChildren_.pop_back();
504 void Vertex::whitelistAsChild(
const std::shared_ptr<Vertex> &vertex)
const
506 whitelistedChildren_.emplace_back(vertex);
509 bool Vertex::isWhitelistedAsChild(
const std::shared_ptr<Vertex> &vertex)
const
514 auto it = whitelistedChildren_.begin();
515 while (it != whitelistedChildren_.end())
518 if (
const auto child = it->lock())
521 if (child->getId() == vertex->getId())
529 it = whitelistedChildren_.erase(it);
535 void Vertex::setIncomingCollisionCheckResolution(
const std::size_t vertexId, std::size_t numChecks)
const
537 incomingCollisionCheckResolution_[vertexId] = numChecks;
540 std::size_t Vertex::getIncomingCollisionCheckResolution(
const std::size_t vertexId)
const
542 if (incomingCollisionCheckResolution_.find(vertexId) == incomingCollisionCheckResolution_.end())
548 return incomingCollisionCheckResolution_[vertexId];
552 void Vertex::blacklistAsChild(
const std::shared_ptr<Vertex> &vertex)
const
554 blacklistedChildren_.emplace_back(vertex);
557 bool Vertex::isBlacklistedAsChild(
const std::shared_ptr<Vertex> &vertex)
const
559 auto it = blacklistedChildren_.begin();
560 while (it != blacklistedChildren_.end())
563 if (
const auto child = it->lock())
566 if (child->getId() == vertex->getId())
574 it = blacklistedChildren_.erase(it);
580 bool Vertex::hasCachedNeighbors()
const
582 return neighborBatchId_ == batchId_;
585 void Vertex::cacheNeighbors(
const std::vector<std::shared_ptr<Vertex>> &neighbors)
const
588 neighbors_.insert(neighbors_.begin(), neighbors.begin(), neighbors.end());
589 neighborBatchId_ = batchId_;
592 const std::vector<std::shared_ptr<Vertex>> Vertex::getNeighbors()
const
594 if (neighborBatchId_ != batchId_)
596 throw ompl::Exception(
"Requested neighbors from vertex of outdated approximation.");
599 std::vector<std::shared_ptr<Vertex>> neighbors;
600 for (
const auto &neighbor : neighbors_)
602 assert(neighbor.lock());
603 neighbors.emplace_back(neighbor.lock());
609 std::vector<std::shared_ptr<Vertex>> Vertex::getForwardChildren()
const
611 std::vector<std::shared_ptr<Vertex>> children;
612 for (
const auto &child : forwardChildren_)
614 assert(!child.expired());
615 children.emplace_back(child.lock());
620 std::vector<std::shared_ptr<Vertex>> Vertex::getReverseChildren()
const
622 std::vector<std::shared_ptr<Vertex>> children;
623 children.reserve(reverseChildren_.size());
624 for (
const auto &child : reverseChildren_)
626 assert(!child.expired());
627 children.emplace_back(child.lock());
632 void Vertex::setReverseVertexQueuePointer(
634 std::pair<std::array<ompl::base::Cost, 2u>, std::shared_ptr<Vertex>>,
635 std::function<
bool(
const std::pair<std::array<ompl::base::Cost, 2u>, std::shared_ptr<Vertex>> &,
636 const std::pair<std::array<ompl::base::Cost, 2u>, std::shared_ptr<Vertex>> &)>>::
639 reverseVertexQueuePointerId_ = batchId_;
640 reverseVertexQueuePointer_ = pointer;
643 void Vertex::setForwardVertexQueuePointer(
645 std::pair<std::array<ompl::base::Cost, 2u>, std::shared_ptr<Vertex>>,
646 std::function<
bool(
const std::pair<std::array<ompl::base::Cost, 2u>, std::shared_ptr<Vertex>> &,
647 const std::pair<std::array<ompl::base::Cost, 2u>, std::shared_ptr<Vertex>> &)>>::
650 forwardVertexQueuePointerId_ = batchId_;
651 forwardVertexQueuePointer_ = pointer;
655 std::pair<std::array<ompl::base::Cost, 2u>, std::shared_ptr<Vertex>>,
656 std::function<bool(
const std::pair<std::array<ompl::base::Cost, 2u>, std::shared_ptr<Vertex>> &,
657 const std::pair<std::array<ompl::base::Cost, 2u>, std::shared_ptr<Vertex>> &)>>::
659 Vertex::getForwardVertexQueuePointer()
const
661 if (batchId_ != forwardVertexQueuePointerId_)
663 forwardVertexQueuePointer_ =
nullptr;
665 return forwardVertexQueuePointer_;
669 std::pair<std::array<ompl::base::Cost, 2u>, std::shared_ptr<Vertex>>,
670 std::function<bool(
const std::pair<std::array<ompl::base::Cost, 2u>, std::shared_ptr<Vertex>> &,
671 const std::pair<std::array<ompl::base::Cost, 2u>, std::shared_ptr<Vertex>> &)>>::
673 Vertex::getReverseVertexQueuePointer()
const
675 if (batchId_ != reverseVertexQueuePointerId_)
677 reverseVertexQueuePointer_ =
nullptr;
679 return reverseVertexQueuePointer_;
682 void Vertex::resetForwardVertexQueuePointer()
684 forwardVertexQueuePointer_ =
nullptr;
687 void Vertex::resetReverseVertexQueuePointer()
689 reverseVertexQueuePointer_ =
nullptr;
This class provides an implementation of an updatable min-heap. Using it is a bit cumbersome,...
The exception type for ompl.
Definition of a cost value. Can represent the cost of a motion or the cost of a state.
Definition of a scoped state.
Definition of an abstract state.
ompl::base::Cost getCostToComeFromGoal() const
Returns the cost to come to this vertex from the goal.
This namespace contains code that is specific to planning under geometric constraints.
Message namespace. This contains classes needed to output error messages (or logging) from within the...
Main namespace. Contains everything in this library.