本文整理汇总了C++中PathGeometric::getStates方法的典型用法代码示例。如果您正苦于以下问题:C++ PathGeometric::getStates方法的具体用法?C++ PathGeometric::getStates怎么用?C++ PathGeometric::getStates使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类PathGeometric
的用法示例。
在下文中一共展示了PathGeometric::getStates方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1:
bool ompl::geometric::PathSimplifier::collapseCloseVertices(PathGeometric &path, unsigned int maxSteps, unsigned int maxEmptySteps)
{
if (path.getStateCount() < 3)
return false;
if (maxSteps == 0)
maxSteps = path.getStateCount();
if (maxEmptySteps == 0)
maxEmptySteps = path.getStateCount();
const base::SpaceInformationPtr &si = path.getSpaceInformation();
std::vector<base::State*> &states = path.getStates();
// compute pair-wise distances in path (construct only half the matrix)
std::map<std::pair<const base::State*, const base::State*>, double> distances;
for (unsigned int i = 0 ; i < states.size() ; ++i)
for (unsigned int j = i + 2 ; j < states.size() ; ++j)
distances[std::make_pair(states[i], states[j])] = si->distance(states[i], states[j]);
bool result = false;
unsigned int nochange = 0;
for (unsigned int s = 0 ; s < maxSteps && nochange < maxEmptySteps ; ++s, ++nochange)
{
// find closest pair of points
double minDist = std::numeric_limits<double>::infinity();
int p1 = -1;
int p2 = -1;
for (unsigned int i = 0 ; i < states.size() ; ++i)
for (unsigned int j = i + 2 ; j < states.size() ; ++j)
{
double d = distances[std::make_pair(states[i], states[j])];
if (d < minDist)
{
minDist = d;
p1 = i;
p2 = j;
}
}
if (p1 >= 0 && p2 >= 0)
{
if (si->checkMotion(states[p1], states[p2]))
{
if (freeStates_)
for (int i = p1 + 1 ; i < p2 ; ++i)
si->freeState(states[i]);
states.erase(states.begin() + p1 + 1, states.begin() + p2);
result = true;
nochange = 0;
}
else
distances[std::make_pair(states[p1], states[p2])] = std::numeric_limits<double>::infinity();
}
else
break;
}
return result;
}
示例2: while
/* Based on COMP450 2010 project of Yun Yu and Linda Hill (Rice University) */
void ompl::geometric::PathSimplifier::smoothBSpline(PathGeometric &path, unsigned int maxSteps, double minChange)
{
if (path.getStateCount() < 3)
return;
const base::SpaceInformationPtr &si = path.getSpaceInformation();
std::vector<base::State*> &states = path.getStates();
base::State *temp1 = si->allocState();
base::State *temp2 = si->allocState();
for (unsigned int s = 0 ; s < maxSteps ; ++s)
{
path.subdivide();
unsigned int i = 2, u = 0, n1 = states.size() - 1;
while (i < n1)
{
if (si->isValid(states[i - 1]))
{
si->getStateSpace()->interpolate(states[i - 1], states[i], 0.5, temp1);
si->getStateSpace()->interpolate(states[i], states[i + 1], 0.5, temp2);
si->getStateSpace()->interpolate(temp1, temp2, 0.5, temp1);
if (si->checkMotion(states[i - 1], temp1) && si->checkMotion(temp1, states[i + 1]))
{
if (si->distance(states[i], temp1) > minChange)
{
si->copyState(states[i], temp1);
++u;
}
}
}
i += 2;
}
if (u == 0)
break;
}
si->freeState(temp1);
si->freeState(temp2);
}
示例3: checkValidity
//.........这里部分代码省略.........
{
const base::State *st = dGoal_.getMotionCount() == 0 ? pis_.nextGoal(ptc) : pis_.nextGoal();
if (st)
{
Motion* motion = new Motion(si_);
si_->copyState(motion->state, st);
motion->root = motion->state;
motion->valid = true;
projectionEvaluator_->computeCoordinates(motion->state, xcoord);
dGoal_.addMotion(motion, xcoord);
}
if (dGoal_.getMotionCount() == 0)
{
OMPL_ERROR("%s: Unable to sample any valid states for goal tree", getName().c_str());
break;
}
}
Discretization<Motion>::Cell *ecell = NULL;
Motion *existing = NULL;
disc.selectMotion(existing, ecell);
assert(existing);
sampler_->sampleUniformNear(xstate, existing->state, maxDistance_);
/* create a motion */
Motion* motion = new Motion(si_);
si_->copyState(motion->state, xstate);
motion->parent = existing;
motion->root = existing->root;
existing->children.push_back(motion);
projectionEvaluator_->computeCoordinates(motion->state, xcoord);
disc.addMotion(motion, xcoord);
/* attempt to connect trees */
Discretization<Motion>::Cell *ocell = otherDisc.getGrid().getCell(xcoord);
if (ocell && !ocell->data->motions.empty())
{
Motion* connectOther = ocell->data->motions[rng_.uniformInt(0, ocell->data->motions.size() - 1)];
if (goal->isStartGoalPairValid(startTree ? connectOther->root : motion->root, startTree ? motion->root : connectOther->root))
{
Motion* connect = new Motion(si_);
si_->copyState(connect->state, connectOther->state);
connect->parent = motion;
connect->root = motion->root;
motion->children.push_back(connect);
projectionEvaluator_->computeCoordinates(connect->state, xcoord);
disc.addMotion(connect, xcoord);
if (isPathValid(disc, connect, xstate) && isPathValid(otherDisc, connectOther, xstate))
{
if (startTree)
connectionPoint_ = std::make_pair(connectOther->state, motion->state);
else
connectionPoint_ = std::make_pair(motion->state, connectOther->state);
/* extract the motions and put them in solution vector */
std::vector<Motion*> mpath1;
while (motion != NULL)
{
mpath1.push_back(motion);
motion = motion->parent;
}
std::vector<Motion*> mpath2;
while (connectOther != NULL)
{
mpath2.push_back(connectOther);
connectOther = connectOther->parent;
}
if (startTree)
mpath1.swap(mpath2);
PathGeometric *path = new PathGeometric(si_);
path->getStates().reserve(mpath1.size() + mpath2.size());
for (int i = mpath1.size() - 1 ; i >= 0 ; --i)
path->append(mpath1[i]->state);
for (unsigned int i = 0 ; i < mpath2.size() ; ++i)
path->append(mpath2[i]->state);
pdef_->addSolutionPath(base::PathPtr(path), false, 0.0);
solved = true;
break;
}
}
}
}
si_->freeState(xstate);
OMPL_INFORM("%s: Created %u (%u start + %u goal) states in %u cells (%u start (%u on boundary) + %u goal (%u on boundary))",
getName().c_str(),
dStart_.getMotionCount() + dGoal_.getMotionCount(), dStart_.getMotionCount(), dGoal_.getMotionCount(),
dStart_.getCellCount() + dGoal_.getCellCount(), dStart_.getCellCount(), dStart_.getGrid().countExternal(),
dGoal_.getCellCount(), dGoal_.getGrid().countExternal());
return solved ? base::PlannerStatus::EXACT_SOLUTION : base::PlannerStatus::TIMEOUT;
}
示例4: checkValidity
//.........这里部分代码省略.........
// if we have not sampled too many goals already
if (dGoal_.getMotionCount() == 0 || pis_.getSampledGoalsCount() < dGoal_.getMotionCount() / 2)
{
const base::State *st = dGoal_.getMotionCount() == 0 ? pis_.nextGoal(ptc) : pis_.nextGoal();
if (st)
{
Motion* motion = new Motion(si_);
si_->copyState(motion->state, st);
motion->root = motion->state;
projectionEvaluator_->computeCoordinates(motion->state, xcoord);
dGoal_.addMotion(motion, xcoord);
}
if (dGoal_.getMotionCount() == 0)
{
msg_.error("Unable to sample any valid states for goal tree");
break;
}
}
Discretization<Motion>::Cell *ecell = NULL;
Motion *existing = NULL;
disc.selectMotion(existing, ecell);
assert(existing);
if (sampler_->sampleNear(xstate, existing->state, maxDistance_))
{
std::pair<base::State*, double> fail(xstate, 0.0);
bool keep = si_->checkMotion(existing->state, xstate, fail);
if (!keep && fail.second > minValidPathFraction_)
keep = true;
if (keep)
{
/* create a motion */
Motion *motion = new Motion(si_);
si_->copyState(motion->state, xstate);
motion->root = existing->root;
motion->parent = existing;
projectionEvaluator_->computeCoordinates(motion->state, xcoord);
disc.addMotion(motion, xcoord);
Discretization<Motion>::Cell* cellC = otherDisc.getGrid().getCell(xcoord);
if (cellC && !cellC->data->motions.empty())
{
Motion* connectOther = cellC->data->motions[rng_.uniformInt(0, cellC->data->motions.size() - 1)];
if (goal->isStartGoalPairValid(startTree ? connectOther->root : motion->root, startTree ? motion->root : connectOther->root) &&
si_->checkMotion(motion->state, connectOther->state))
{
/* extract the motions and put them in solution vector */
std::vector<Motion*> mpath1;
while (motion != NULL)
{
mpath1.push_back(motion);
motion = motion->parent;
}
std::vector<Motion*> mpath2;
while (connectOther != NULL)
{
mpath2.push_back(connectOther);
connectOther = connectOther->parent;
}
if (startTree)
mpath1.swap(mpath2);
PathGeometric *path = new PathGeometric(si_);
path->getStates().reserve(mpath1.size() + mpath2.size());
for (int i = mpath1.size() - 1 ; i >= 0 ; --i)
path->append(mpath1[i]->state);
for (unsigned int i = 0 ; i < mpath2.size() ; ++i)
path->append(mpath2[i]->state);
goal->addSolutionPath(base::PathPtr(path), false, 0.0);
solved = true;
break;
}
}
}
else
ecell->data->score *= failedExpansionScoreFactor_;
}
else
ecell->data->score *= failedExpansionScoreFactor_;
disc.updateCell(ecell);
}
si_->freeState(xstate);
msg_.inform("Created %u (%u start + %u goal) states in %u cells (%u start (%u on boundary) + %u goal (%u on boundary))",
dStart_.getMotionCount() + dGoal_.getMotionCount(), dStart_.getMotionCount(), dGoal_.getMotionCount(),
dStart_.getCellCount() + dGoal_.getCellCount(), dStart_.getCellCount(), dStart_.getGrid().countExternal(),
dGoal_.getCellCount(), dGoal_.getGrid().countExternal());
return solved;
}
示例5: solve
//.........这里部分代码省略.........
// for (int i = 0; i < n_motions.size(); i++)
// if (!n_motions[i]->global_valid_)
// {
// addNew = false;
// break;
// }
if (addNew)
{
new_motion->global_valid_ = false;
tGoal_->add(new_motion);
}
}
}
/// If succeeded both ways, the a solution is found
if (succ_s && succ_g)
{
connectionPoint_ = std::make_pair(new_s->state, new_g->state);
Motion *solution = new_s;
std::vector<Motion*> mpath1;
while (solution != NULL)
{
if (solution->internal_path != nullptr)
{
for (int i = solution->internal_path->rows() - 1; i > 0; i--)
{
Motion *local_motion = new Motion(si_);
Eigen::VectorXd tmp = solution->internal_path->row(i);
memcpy(
local_motion->state->as<
ompl::base::RealVectorStateSpace::StateType>()->values,
tmp.data(),
sizeof(double) * (int) si_->getStateDimension());
mpath1.push_back(local_motion);
}
if (solution->inter_state != NULL)
{
Motion *local_motion = new Motion(si_);
si_->copyState(local_motion->state, solution->inter_state);
mpath1.push_back(local_motion);
}
}
else
mpath1.push_back(solution);
solution = solution->parent;
}
solution = new_g;
std::vector<Motion*> mpath2;
while (solution != NULL)
{
if (solution->internal_path != nullptr)
{
for (int i = solution->internal_path->rows() - 1; i > 0; i--)
{
Motion *local_motion = new Motion(si_);
Eigen::VectorXd tmp = solution->internal_path->row(i);
memcpy(
local_motion->state->as<
ompl::base::RealVectorStateSpace::StateType>()->values,
tmp.data(),
sizeof(double) * (int) si_->getStateDimension());
mpath2.push_back(local_motion);
}
if (solution->inter_state != NULL)
{
Motion *local_motion = new Motion(si_);
si_->copyState(local_motion->state, solution->inter_state);
mpath2.push_back(local_motion);
}
}
else
mpath2.push_back(solution);
solution = solution->parent;
}
PathGeometric *path = new PathGeometric(si_);
path->getStates().reserve(mpath1.size() + mpath2.size());
for (int i = mpath1.size() - 1; i >= 0; --i)
path->append(mpath1[i]->state);
for (unsigned int i = 0; i < mpath2.size(); ++i)
path->append(mpath2[i]->state);
pdef_->addSolutionPath(base::PathPtr(path), false, 0.0, getName());
solved = true;
break;
}
}
si_->freeState(rmotion->state);
delete rmotion;
OMPL_INFORM("%s: Created %u states (%u start + %u goal)",
getName().c_str(), tStart_->size() + tGoal_->size(), tStart_->size(),
tGoal_->size());
return
solved ?
base::PlannerStatus::EXACT_SOLUTION : base::PlannerStatus::TIMEOUT;
}
示例6: checkValidity
//.........这里部分代码省略.........
{
TreeData &tree = startTree ? tStart_ : tGoal_;
tgi.start = startTree;
startTree = !startTree;
TreeData &otherTree = startTree ? tStart_ : tGoal_;
if (tGoal_->size() == 0 || pis_.getSampledGoalsCount() < tGoal_->size() / 2)
{
const base::State *st = tGoal_->size() == 0 ? pis_.nextGoal(ptc) : pis_.nextGoal();
if (st)
{
Motion* motion = new Motion(si_);
si_->copyState(motion->state, st);
motion->root = motion->state;
tGoal_->add(motion);
}
if (tGoal_->size() == 0)
{
logError("Unable to sample any valid states for goal tree");
break;
}
}
/* sample random state */
sampler_->sampleUniform(rstate);
GrowState gs = growTree(tree, tgi, rmotion);
if (gs != TRAPPED)
{
/* remember which motion was just added */
Motion *addedMotion = tgi.xmotion;
/* attempt to connect trees */
/* if reached, it means we used rstate directly, no need top copy again */
if (gs != REACHED)
si_->copyState(rstate, tgi.xstate);
GrowState gsc = ADVANCED;
tgi.start = startTree;
while (gsc == ADVANCED)
gsc = growTree(otherTree, tgi, rmotion);
Motion *startMotion = startTree ? tgi.xmotion : addedMotion;
Motion *goalMotion = startTree ? addedMotion : tgi.xmotion;
/* if we connected the trees in a valid way (start and goal pair is valid)*/
if (gsc == REACHED && goal->isStartGoalPairValid(startMotion->root, goalMotion->root))
{
// it must be the case that either the start tree or the goal tree has made some progress
// so one of the parents is not NULL. We go one step 'back' to avoid having a duplicate state
// on the solution path
if (startMotion->parent)
startMotion = startMotion->parent;
else
goalMotion = goalMotion->parent;
connectionPoint_ = std::make_pair<base::State*, base::State*>(startMotion->state, goalMotion->state);
/* construct the solution path */
Motion *solution = startMotion;
std::vector<Motion*> mpath1;
while (solution != NULL)
{
mpath1.push_back(solution);
solution = solution->parent;
}
solution = goalMotion;
std::vector<Motion*> mpath2;
while (solution != NULL)
{
mpath2.push_back(solution);
solution = solution->parent;
}
PathGeometric *path = new PathGeometric(si_);
path->getStates().reserve(mpath1.size() + mpath2.size());
for (int i = mpath1.size() - 1 ; i >= 0 ; --i)
path->append(mpath1[i]->state);
for (unsigned int i = 0 ; i < mpath2.size() ; ++i)
path->append(mpath2[i]->state);
pdef_->addSolutionPath(base::PathPtr(path), false, 0.0);
solved = true;
break;
}
}
}
si_->freeState(tgi.xstate);
si_->freeState(rstate);
delete rmotion;
logInform("Created %u states (%u start + %u goal)", tStart_->size() + tGoal_->size(), tStart_->size(), tGoal_->size());
return solved ? base::PlannerStatus::EXACT_SOLUTION : base::PlannerStatus::TIMEOUT;
}
示例7: checkValidity
//.........这里部分代码省略.........
if (tStart_->size() == 0)
{
OMPL_ERROR("%s: Start tree has no valid states!", getName().c_str());
return base::PlannerStatus::INVALID_START;
}
// Do the same for the goal tree, if it is empty, but only once
if (tGoal_->size() == 0)
{
const base::State *state = pis_.nextGoal(ptc);
if (state)
{
Motion* motion = addMotion(state, tGoal_);
motion->root = motion->state; // this state is the root of a tree
}
}
if (tGoal_->size() == 0)
{
OMPL_ERROR("%s: Goal tree has no valid states!", getName().c_str());
return base::PlannerStatus::INVALID_GOAL;
}
OMPL_INFORM("%s: Planning started with %d states already in datastructure", getName().c_str(), (int)(tStart_->size() + tGoal_->size()));
base::StateSamplerPtr sampler = si_->allocStateSampler();
Motion *rmotion = new Motion(si_);
base::State *rstate = rmotion->state;
Motion *xmotion = new Motion(si_);
base::State *xstate = xmotion->state;
TreeData tree = tStart_;
TreeData otherTree = tGoal_;
bool solved = false;
// Planning loop
while (ptc == false)
{
// Check if there are more goal states
if (pis_.getSampledGoalsCount() < tGoal_->size() / 2)
{
if (const base::State *state = pis_.nextGoal())
{
Motion* motion = addMotion(state, tGoal_);
motion->root = motion->state; // this state is the root of a tree
}
}
// Sample a state uniformly at random
sampler->sampleUniform(rstate);
Motion* result; // the motion that gets added in extendTree
if (extendTree(rmotion, tree, result) != FAILED) // we added something new to the tree
{
// Try to connect the other tree to the node we just added
if (connectTrees(result, otherTree, xmotion))
{
// The trees have been connected. Construct the solution path
Motion *solution = connectionPoint_.first;
std::vector<Motion*> mpath1;
while (solution != NULL)
{
mpath1.push_back(solution);
solution = solution->parent;
}
solution = connectionPoint_.second;
std::vector<Motion*> mpath2;
while (solution != NULL)
{
mpath2.push_back(solution);
solution = solution->parent;
}
PathGeometric *path = new PathGeometric(si_);
path->getStates().reserve(mpath1.size() + mpath2.size());
for (int i = mpath1.size() - 1 ; i >= 0 ; --i)
path->append(mpath1[i]->state);
for (unsigned int i = 0 ; i < mpath2.size() ; ++i)
path->append(mpath2[i]->state);
pdef_->addSolutionPath(base::PathPtr(path), false, 0.0, getName());
solved = true;
break;
}
}
std::swap(tree, otherTree);
}
si_->freeState(rstate);
si_->freeState(xstate);
delete rmotion;
delete xmotion;
OMPL_INFORM("%s: Created %u states (%u start + %u goal)", getName().c_str(), tStart_->size() + tGoal_->size(), tStart_->size(), tGoal_->size());
return solved ? base::PlannerStatus::EXACT_SOLUTION : base::PlannerStatus::TIMEOUT;
}
示例8: dists
bool ompl::geometric::PathSimplifier::findBetterGoal(PathGeometric &path, const base::PlannerTerminationCondition &ptc,
unsigned int samplingAttempts, double rangeRatio, double snapToVertex)
{
if (path.getStateCount() < 2)
return false;
if (!gsr_)
{
OMPL_WARN("%s: No goal sampleable object to sample a better goal from.", "PathSimplifier::findBetterGoal");
return false;
}
unsigned int maxGoals = std::min((unsigned)10, gsr_->maxSampleCount()); // the number of goals we will sample
unsigned int failedTries = 0;
bool betterGoal = false;
const base::StateSpacePtr& ss = si_->getStateSpace();
std::vector<base::State*> &states = path.getStates();
// dists[i] contains the cumulative length of the path up to and including state i
std::vector<double> dists(states.size(), 0.0);
for (unsigned int i = 1 ; i < dists.size() ; ++i)
dists[i] = dists[i-1] + si_->distance(states[i-1], states[i]);
// Sampled states closer than 'threshold' distance to any existing state in the path
// are snapped to the close state
double threshold = dists.back() * snapToVertex;
// The range (distance) of a single connection that will be attempted
double rd = rangeRatio * dists.back();
base::State* temp = si_->allocState();
base::State* tempGoal = si_->allocState();
while(!ptc && failedTries++ < maxGoals && !betterGoal)
{
gsr_->sampleGoal(tempGoal);
// Goal state is not compatible with the start state
if (!gsr_->isStartGoalPairValid(path.getState(0), tempGoal))
continue;
unsigned int numSamples = 0;
while (!ptc && numSamples++ < samplingAttempts && !betterGoal)
{
// sample a state within rangeRatio
double t = rng_.uniformReal(std::max(dists.back() - rd, 0.0), dists.back()); // Sample a random point within rd of the end of the path
std::vector<double>::iterator end = std::lower_bound(dists.begin(), dists.end(), t);
std::vector<double>::iterator start = end;
while(start != dists.begin() && *start >= t)
start -= 1;
unsigned int startIndex = start - dists.begin();
unsigned int endIndex = end - dists.begin();
// Snap the random point to the nearest vertex, if within the threshold
if (t - (*start) < threshold) // snap to the starting waypoint
endIndex = startIndex;
if ((*end) - t < threshold) // snap to the ending waypoint
startIndex = endIndex;
// Compute the state value and the accumulated cost up to that state
double costToCome = dists[startIndex];
base::State* state;
if (startIndex == endIndex)
{
state = states[startIndex];
}
else
{
double tSeg = (t - (*start)) / (*end - *start);
ss->interpolate(states[startIndex], states[endIndex], tSeg, temp);
state = temp;
costToCome += si_->distance(states[startIndex], state);
}
double costToGo = si_->distance(state, tempGoal);
double candidateCost = costToCome + costToGo;
// Make sure we improve before attempting validation
if (dists.back() - candidateCost > std::numeric_limits<float>::epsilon() && si_->checkMotion(state, tempGoal))
{
// insert the new states
if (startIndex == endIndex)
{
// new intermediate state
si_->copyState(states[startIndex], state);
// new goal state
si_->copyState(states[startIndex+1], tempGoal);
if (freeStates_)
{
for(size_t i = startIndex + 2; i < states.size(); ++i)
si_->freeState(states[i]);
}
states.erase(states.begin() + startIndex + 2, states.end());
}
else
{
//.........这里部分代码省略.........
示例9: newStates
bool ompl::geometric::PathSimplifier::reduceVertices(PathGeometric &path, unsigned int maxSteps, unsigned int maxEmptySteps, double rangeRatio)
{
if (path.getStateCount() < 3)
return false;
if (maxSteps == 0)
maxSteps = path.getStateCount();
if (maxEmptySteps == 0)
maxEmptySteps = path.getStateCount();
bool result = false;
unsigned int nochange = 0;
const base::SpaceInformationPtr &si = path.getSpaceInformation();
std::vector<base::State*> &states = path.getStates();
if (si->checkMotion(states.front(), states.back()))
{
if (freeStates_)
for (std::size_t i = 2 ; i < states.size() ; ++i)
si->freeState(states[i-1]);
std::vector<base::State*> newStates(2);
newStates[0] = states.front();
newStates[1] = states.back();
states.swap(newStates);
result = true;
}
else
for (unsigned int i = 0 ; i < maxSteps && nochange < maxEmptySteps ; ++i, ++nochange)
{
int count = states.size();
int maxN = count - 1;
int range = 1 + (int)(floor(0.5 + (double)count * rangeRatio));
int p1 = rng_.uniformInt(0, maxN);
int p2 = rng_.uniformInt(std::max(p1 - range, 0), std::min(maxN, p1 + range));
if (abs(p1 - p2) < 2)
{
if (p1 < maxN - 1)
p2 = p1 + 2;
else
if (p1 > 1)
p2 = p1 - 2;
else
continue;
}
if (p1 > p2)
std::swap(p1, p2);
if (si->checkMotion(states[p1], states[p2]))
{
if (freeStates_)
for (int j = p1 + 1 ; j < p2 ; ++j)
si->freeState(states[j]);
states.erase(states.begin() + p1 + 1, states.begin() + p2);
nochange = 0;
result = true;
}
}
return result;
}