GoalStates.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/goals/GoalStates.h"
38 #include "ompl/base/SpaceInformation.h"
39 #include "ompl/util/Exception.h"
40 #include <limits>
41 
42 ompl::base::GoalStates::~GoalStates()
43 {
44  freeMemory();
45 }
46 
48 {
49  freeMemory();
50  states_.clear();
51 }
52 
53 void ompl::base::GoalStates::freeMemory()
54 {
55  for (auto &state : states_)
56  si_->freeState(state);
57 }
58 
60 {
61  double dist = std::numeric_limits<double>::infinity();
62  for (auto state : states_)
63  {
64  double d = si_->distance(st, state);
65  if (d < dist)
66  dist = d;
67  }
68  return dist;
69 }
70 
71 void ompl::base::GoalStates::print(std::ostream &out) const
72 {
73  out << states_.size() << " goal states, threshold = " << threshold_ << ", memory address = " << this << std::endl;
74  for (auto state : states_)
75  {
76  si_->printState(state, out);
77  out << std::endl;
78  }
79 }
80 
82 {
83  if (states_.empty())
84  throw Exception("There are no goals to sample");
85 
86  // Roll over the samplePosition_ if it points past the number of states.
87  samplePosition_ = samplePosition_ % states_.size();
88  // Get the next state.
89  si_->copyState(st, states_[samplePosition_]);
90  // Increment the counter. Do NOT roll over incase a new state is added before sampleGoal is called again.
91  samplePosition_++;
92 }
93 
95 {
96  return states_.size();
97 }
98 
100 {
101  states_.push_back(si_->cloneState(st));
102 }
103 
105 {
106  addState(st.get());
107 }
108 
109 const ompl::base::State *ompl::base::GoalStates::getState(unsigned int index) const
110 {
111  if (index >= states_.size())
112  throw Exception("Index " + std::to_string(index) + " out of range. Only " + std::to_string(states_.size()) +
113  " states are available");
114  return states_[index];
115 }
116 
118 {
119  return states_.size();
120 }
121 
123 {
124  return !states_.empty();
125 }
Definition of an abstract state.
Definition: State.h:113
virtual void clear()
Clear all goal states.
Definition: GoalStates.cpp:47
void print(std::ostream &out=std::cout) const override
Print information about the goal data structure to a stream.
Definition: GoalStates.cpp:71
void sampleGoal(State *st) const override
Sample a state in the goal region.
Definition: GoalStates.cpp:81
StateType * get()
Returns a pointer to the contained state.
Definition: ScopedState.h:489
double distanceGoal(const State *st) const override
Compute the distance to the goal (heuristic). This function is the one used in computing the distance...
Definition: GoalStates.cpp:59
virtual const State * getState(unsigned int index) const
Return a pointer to the indexth state in the state list.
Definition: GoalStates.cpp:109
virtual void addState(const State *st)
Add a goal state.
Definition: GoalStates.cpp:99
unsigned int maxSampleCount() const override
Return the maximum number of samples that can be asked for before repeating.
Definition: GoalStates.cpp:94
virtual std::size_t getStateCount() const
Return the number of valid goal states.
Definition: GoalStates.cpp:117
virtual bool hasStates() const
Check if there are any states in this goal region.
Definition: GoalStates.cpp:122
Definition of a scoped state.
Definition: ScopedState.h:120
The exception type for ompl.
Definition: Exception.h:78