All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator
Goal.cpp
00001 /*********************************************************************
00002 * Software License Agreement (BSD License)
00003 *
00004 *  Copyright (c) 2010, Rice University
00005 *  All rights reserved.
00006 *
00007 *  Redistribution and use in source and binary forms, with or without
00008 *  modification, are permitted provided that the following conditions
00009 *  are met:
00010 *
00011 *   * Redistributions of source code must retain the above copyright
00012 *     notice, this list of conditions and the following disclaimer.
00013 *   * Redistributions in binary form must reproduce the above
00014 *     copyright notice, this list of conditions and the following
00015 *     disclaimer in the documentation and/or other materials provided
00016 *     with the distribution.
00017 *   * Neither the name of the Rice University nor the names of its
00018 *     contributors may be used to endorse or promote products derived
00019 *     from this software without specific prior written permission.
00020 *
00021 *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00022 *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00023 *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00024 *  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00025 *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00026 *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00027 *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00028 *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00029 *  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00030 *  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00031 *  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00032 *  POSSIBILITY OF SUCH DAMAGE.
00033 *********************************************************************/
00034 
00035 /* Author: Ioan Sucan */
00036 
00037 #include "ompl/base/Goal.h"
00038 
00039 #include <algorithm>
00040 #include <limits>
00041 
00042 #include <boost/thread/mutex.hpp>
00043 
00045 namespace ompl
00046 {
00047     namespace base
00048     {
00049 
00050         class Goal::PlannerSolutionSet
00051         {
00052         public:
00053 
00054             PlannerSolutionSet(void)
00055             {
00056             }
00057 
00058             void add(const PlannerSolution &s)
00059             {
00060                 boost::mutex::scoped_lock slock(lock_);
00061                 int index = solutions_.size();
00062                 solutions_.push_back(s);
00063                 solutions_.back().index_ = index;
00064                 std::sort(solutions_.begin(), solutions_.end());
00065             }
00066 
00067             void clear(void)
00068             {
00069                 boost::mutex::scoped_lock slock(lock_);
00070                 solutions_.clear();
00071             }
00072 
00073             std::vector<PlannerSolution> getSolutions(void)
00074             {
00075                 boost::mutex::scoped_lock slock(lock_);
00076                 std::vector<PlannerSolution> copy = solutions_;
00077                 return copy;
00078             }
00079 
00080             bool isApproximate(void)
00081             {
00082                 boost::mutex::scoped_lock slock(lock_);
00083                 bool result = false;
00084                 if (!solutions_.empty())
00085                     result = solutions_[0].approximate_;
00086                 return result;
00087             }
00088 
00089             double getDifference(void)
00090             {
00091                 boost::mutex::scoped_lock slock(lock_);
00092                 double diff = -1.0;
00093                 if (!solutions_.empty())
00094                     diff = solutions_[0].difference_;
00095                 return diff;
00096             }
00097 
00098             PathPtr getTopSolution(void)
00099             {
00100                 boost::mutex::scoped_lock slock(lock_);
00101                 PathPtr copy;
00102                 if (!solutions_.empty())
00103                     copy = solutions_[0].path_;
00104                 return copy;
00105             }
00106 
00107             std::size_t getSolutionCount(void)
00108             {
00109                 boost::mutex::scoped_lock slock(lock_);
00110                 std::size_t result = solutions_.size();
00111                 return result;
00112             }
00113 
00114         private:
00115 
00116             std::vector<PlannerSolution> solutions_;
00117             boost::mutex                 lock_;
00118         };
00119     }
00120 }
00122 
00123 ompl::base::Goal::Goal(const SpaceInformationPtr &si) :
00124     type_(GOAL_ANY), si_(si), maximumPathLength_(std::numeric_limits<double>::infinity()),
00125     msg_("Goal"), solutions_(new PlannerSolutionSet())
00126 {
00127 }
00128 
00129 bool ompl::base::Goal::isAchieved(void) const
00130 {
00131     return solutions_->getSolutionCount() > 0;
00132 }
00133 
00134 std::size_t ompl::base::Goal::getSolutionCount(void) const
00135 {
00136     return solutions_->getSolutionCount();
00137 }
00138 
00139 ompl::base::PathPtr ompl::base::Goal::getSolutionPath(void) const
00140 {
00141     return solutions_->getTopSolution();
00142 }
00143 
00144 void ompl::base::Goal::addSolutionPath(const PathPtr &path, bool approximate, double difference) const
00145 {
00146     if (approximate)
00147         msg_.warn("Adding approximate solution");
00148     solutions_->add(PlannerSolution(path, approximate, difference));
00149 }
00150 
00151 bool ompl::base::Goal::isApproximate(void) const
00152 {
00153     return solutions_->isApproximate();
00154 }
00155 
00156 double ompl::base::Goal::getDifference(void) const
00157 {
00158     return solutions_->getDifference();
00159 }
00160 
00161 std::vector<ompl::base::PlannerSolution> ompl::base::Goal::getSolutions(void) const
00162 {
00163     return solutions_->getSolutions();
00164 }
00165 
00166 void ompl::base::Goal::clearSolutionPaths(void) const
00167 {
00168     solutions_->clear();
00169 }
00170 
00171 bool ompl::base::Goal::isSatisfied(const State *st, double *distance) const
00172 {
00173     if (distance != NULL)
00174         *distance = std::numeric_limits<double>::max();
00175     return isSatisfied(st);
00176 }
00177 
00178 bool ompl::base::Goal::isSatisfied(const State *st, double pathLength, double *distance) const
00179 {
00180     if (pathLength > maximumPathLength_)
00181     {
00182         if (distance != NULL)
00183             isSatisfied(st, distance);
00184         return false;
00185     }
00186     else
00187         return isSatisfied(st, distance);
00188 }
00189 
00190 void ompl::base::Goal::print(std::ostream &out) const
00191 {
00192     out << "Goal memory address " << this << std::endl;
00193     out << "There are " << solutions_->getSolutionCount() << " solutions" << std::endl;
00194 }