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
~GoalStates()42 ompl::base::GoalStates::~GoalStates()
43 {
44 freeMemory();
45 }
46
clear()47 void ompl::base::GoalStates::clear()
48 {
49 freeMemory();
50 states_.clear();
51 }
52
freeMemory()53 void ompl::base::GoalStates::freeMemory()
54 {
55 for (auto &state : states_)
56 si_->freeState(state);
57 }
58
distanceGoal(const State * st) const59 double ompl::base::GoalStates::distanceGoal(const State *st) const
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
print(std::ostream & out) const71 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
sampleGoal(base::State * st) const81 void ompl::base::GoalStates::sampleGoal(base::State *st) const
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
maxSampleCount() const94 unsigned int ompl::base::GoalStates::maxSampleCount() const
95 {
96 return states_.size();
97 }
98
addState(const State * st)99 void ompl::base::GoalStates::addState(const State *st)
100 {
101 states_.push_back(si_->cloneState(st));
102 }
103
addState(const ScopedState<> & st)104 void ompl::base::GoalStates::addState(const ScopedState<> &st)
105 {
106 addState(st.get());
107 }
108
getState(unsigned int index) const109 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
getStateCount() const117 std::size_t ompl::base::GoalStates::getStateCount() const
118 {
119 return states_.size();
120 }
121
hasStates() const122 bool ompl::base::GoalStates::hasStates() const
123 {
124 return !states_.empty();
125 }
126