MaximizeClearanceValidStateSampler.cpp
1 /*********************************************************************
2 * Software License Agreement (BSD License)
3 *
4 * Copyright (c) 2010, 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: Ioan Sucan */
36 
37 #include "ompl/base/samplers/MaximizeClearanceValidStateSampler.h"
38 #include "ompl/base/SpaceInformation.h"
39 
41  ValidStateSampler(si), sampler_(si->allocStateSampler()), improveAttempts_(3), work_(si->allocState())
42 {
43  name_ = "max_clearance";
44  params_.declareParam<unsigned int>("nr_improve_attempts",
47 }
48 
49 ompl::base::MaximizeClearanceValidStateSampler::~MaximizeClearanceValidStateSampler()
50 {
51  si_->freeState(work_);
52 }
53 
55 {
56  unsigned int attempts = 0;
57  bool valid = false;
58  double dist = 0.0;
59  do
60  {
61  sampler_->sampleUniform(state);
62  valid = si_->getStateValidityChecker()->isValid(state, dist);
63  ++attempts;
64  } while (!valid && attempts < attempts_);
65 
66  if (valid)
67  {
68  bool validW = false;
69  double distW = 0.0;
70  attempts = 0;
71  while (attempts < improveAttempts_)
72  {
73  sampler_->sampleUniform(work_);
74  validW = si_->getStateValidityChecker()->isValid(work_, distW);
75  ++attempts;
76  if (validW && distW > dist)
77  {
78  dist = distW;
79  si_->copyState(state, work_);
80  }
81  }
82  return true;
83  }
84  else
85  return false;
86 }
87 
88 bool ompl::base::MaximizeClearanceValidStateSampler::sampleNear(State *state, const State *near, const double distance)
89 {
90  unsigned int attempts = 0;
91  bool valid = false;
92  double dist = 0.0;
93  do
94  {
95  sampler_->sampleUniformNear(state, near, distance);
96  valid = si_->getStateValidityChecker()->isValid(state, dist);
97  ++attempts;
98  } while (!valid && attempts < attempts_);
99 
100  if (valid)
101  {
102  bool validW = false;
103  double distW = 0.0;
104  attempts = 0;
105  while (attempts < improveAttempts_)
106  {
107  sampler_->sampleUniformNear(work_, near, distance);
108  validW = si_->getStateValidityChecker()->isValid(work_, distW);
109  ++attempts;
110  if (validW && distW > dist)
111  {
112  dist = distW;
113  si_->copyState(state, work_);
114  }
115  }
116  return true;
117  }
118  else
119  return false;
120 }
virtual bool sampleNear(State *state, const State *near, const double distance)
Sample a state near another, within specified distance. Return false, in case of failure.
unsigned int getNrImproveAttempts() const
Get the number of attempts to improve a sampled state.
virtual bool sample(State *state)
Sample a state. Return false in case of failure.
Abstract definition of a state sampler.
The base class for space information. This contains all the information about the space planning is d...
Definition of an abstract state.
Definition: State.h:50
MaximizeClearanceValidStateSampler(const SpaceInformation *si)
Constructor.
void declareParam(const std::string &name, const typename SpecificParam< T >::SetterFn &setter, const typename SpecificParam< T >::GetterFn &getter=typename SpecificParam< T >::GetterFn())
This function declares a parameter name, and specifies the setter and getter functions.
Definition: GenericParam.h:240
ParamSet params_
The parameters for this instance of the valid state sampler.
void setNrImproveAttempts(unsigned int attempts)
The number of attempts at improving the clearance of the sampled state.
std::string name_
The name of the sampler.