All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator
PlannerTerminationCondition.cpp
00001 /*********************************************************************
00002 * Software License Agreement (BSD License)
00003 *
00004 *  Copyright (c) 2011, 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/PlannerTerminationCondition.h"
00038 #include "ompl/util/Time.h"
00039 #include <boost/bind.hpp>
00040 #include <boost/lambda/bind.hpp>
00041 #include <utility>
00042 
00043 void ompl::base::PlannerTerminationCondition::terminate(void) const
00044 {
00045     terminate_ = true;
00046 }
00047 
00048 bool ompl::base::PlannerTerminationCondition::eval(void) const
00049 {
00050     return fn_();
00051 }
00052 
00053 ompl::base::PlannerNonTerminatingCondition::PlannerNonTerminatingCondition(void) : PlannerTerminationCondition(boost::lambda::constant(false))
00054 {
00055 }
00056 
00057 ompl::base::PlannerAlwaysTerminatingCondition::PlannerAlwaysTerminatingCondition(void) : PlannerTerminationCondition(boost::lambda::constant(true))
00058 {
00059 }
00060 
00062 namespace ompl
00063 {
00064     namespace base
00065     {
00066         static bool plannerOrTerminationCondition(const PlannerTerminationCondition &c1, const PlannerTerminationCondition &c2)
00067         {
00068             return c1() || c2();
00069         }
00070 
00071         static bool plannerAndTerminationCondition(const PlannerTerminationCondition &c1, const PlannerTerminationCondition &c2)
00072         {
00073             return c1() && c2();
00074         }
00075 
00076         // return true if a certain point in time has passed
00077         static bool timePassed(const time::point &endTime)
00078         {
00079             return time::now() > endTime;
00080         }
00081     }
00082 }
00084 
00085 ompl::base::PlannerOrTerminationCondition::PlannerOrTerminationCondition(const PlannerTerminationCondition &c1, const PlannerTerminationCondition &c2) :
00086     PlannerTerminationCondition(boost::bind(&plannerOrTerminationCondition, c1, c2))
00087 {
00088 }
00089 
00090 ompl::base::PlannerAndTerminationCondition::PlannerAndTerminationCondition(const PlannerTerminationCondition &c1, const PlannerTerminationCondition &c2) :
00091     PlannerTerminationCondition(boost::bind(&plannerAndTerminationCondition, c1, c2))
00092 {
00093 }
00094 
00095 bool ompl::base::PlannerThreadedTerminationCondition::eval(void) const
00096 {
00097     return evalValue_;
00098 }
00099 
00100 void ompl::base::PlannerThreadedTerminationCondition::startEvalThread(void)
00101 {
00102     if (!thread_)
00103         thread_ = new boost::thread(boost::bind(&PlannerThreadedTerminationCondition::periodicEval, this));
00104 }
00105 
00106 void ompl::base::PlannerThreadedTerminationCondition::stopEvalThread(void)
00107 {
00108     if (thread_)
00109     {
00110         thread_->interrupt();
00111         thread_->join();
00112         delete thread_;
00113         thread_ = NULL;
00114     }
00115 }
00116 
00117 bool ompl::base::PlannerThreadedTerminationCondition::computeEval(void)
00118 {
00119     return fn_();
00120 }
00121 
00122 ompl::base::PlannerThreadedTerminationCondition::PlannerThreadedTerminationCondition(const PlannerTerminationConditionFn &fn, double period) :
00123     PlannerTerminationCondition(fn), thread_(NULL), evalValue_(terminate_), period_(period)
00124 {
00125     startEvalThread();
00126 }
00127 
00128 ompl::base::PlannerThreadedTerminationCondition::~PlannerThreadedTerminationCondition(void)
00129 {
00130     terminate_ = true;
00131     stopEvalThread();
00132 }
00133 
00134 void ompl::base::PlannerThreadedTerminationCondition::periodicEval(void)
00135 {
00136     time::duration s = time::seconds(period_);
00137     do
00138     {
00139         evalValue_ = computeEval();
00140         if ((*this)())
00141             break;
00142         boost::this_thread::sleep(s);
00143     } while (!(*this)());
00144 }
00145 
00146 ompl::base::PlannerTerminationCondition ompl::base::timedPlannerTerminationCondition(double duration)
00147 {
00148     return PlannerTerminationCondition(boost::bind(&timePassed, time::now() + time::seconds(duration)));
00149 }
00150 
00151 ompl::base::PlannerThreadedTerminationCondition ompl::base::timedPlannerTerminationCondition(double duration, double interval)
00152 {
00153     if (interval > duration)
00154         interval = duration;
00155     return PlannerThreadedTerminationCondition(boost::bind(&timePassed, time::now() + time::seconds(duration)), interval);
00156 }
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends