All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends
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 <boost/lexical_cast.hpp>
41 #include <limits>
42 
43 ompl::base::GoalStates::~GoalStates(void)
44 {
45  freeMemory();
46 }
47 
49 {
50  freeMemory();
51  states_.clear();
52 }
53 
54 void ompl::base::GoalStates::freeMemory(void)
55 {
56  for (unsigned int i = 0 ; i < states_.size() ; ++i)
57  si_->freeState(states_[i]);
58 }
59 
61 {
62  double dist = std::numeric_limits<double>::infinity();
63  for (unsigned int i = 0 ; i < states_.size() ; ++i)
64  {
65  double d = si_->distance(st, states_[i]);
66  if (d < dist)
67  dist = d;
68  }
69  return dist;
70 }
71 
72 void ompl::base::GoalStates::print(std::ostream &out) const
73 {
74  out << states_.size() << " goal states, threshold = " << threshold_ << ", memory address = " << this << std::endl;
75  for (unsigned int i = 0 ; i < states_.size() ; ++i)
76  {
77  si_->printState(states_[i], out);
78  out << std::endl;
79  }
80 }
81 
83 {
84  if (states_.empty())
85  throw Exception("There are no goals to sample");
86  si_->copyState(st, states_[samplePosition_]);
87  samplePosition_ = (samplePosition_ + 1) % states_.size();
88 }
89 
91 {
92  return states_.size();
93 }
94 
96 {
97  states_.push_back(si_->cloneState(st));
98 }
99 
101 {
102  addState(st.get());
103 }
104 
105 const ompl::base::State* ompl::base::GoalStates::getState(unsigned int index) const
106 {
107  if (index >= states_.size())
108  throw Exception("Index " + boost::lexical_cast<std::string>(index) + " out of range. Only " +
109  boost::lexical_cast<std::string>(states_.size()) + " states are available");
110  return states_[index];
111 }
112 
114 {
115  return states_.size();
116 }
117 
119 {
120  return !states_.empty();
121 }