All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator
GoalLazySamples.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/GoalLazySamples.h"
00038 #include "ompl/base/ScopedState.h"
00039 #include "ompl/util/Time.h"
00040 
00041 ompl::base::GoalLazySamples::GoalLazySamples(const SpaceInformationPtr &si, const GoalSamplingFn &samplerFunc, bool autoStart, double minDist) :
00042     GoalStates(si), samplerFunc_(samplerFunc), terminateSamplingThread_(false), samplingThread_(NULL), lastStateAdded_(false), minDist_(minDist)
00043 {
00044     if (autoStart)
00045         startSampling();
00046 }
00047 
00048 ompl::base::GoalLazySamples::~GoalLazySamples(void)
00049 {
00050     stopSampling();
00051 }
00052 
00053 void ompl::base::GoalLazySamples::startSampling(void)
00054 {
00055     if (samplingThread_ == NULL)
00056     {
00057         terminateSamplingThread_ = false;
00058         samplingThread_ = new boost::thread(&GoalLazySamples::goalSamplingThread, this);
00059     }
00060 }
00061 
00062 void ompl::base::GoalLazySamples::stopSampling(void)
00063 {
00064     if (isSampling())
00065     {
00066         terminateSamplingThread_ = true;
00067         samplingThread_->join();
00068         delete samplingThread_;
00069         samplingThread_ = NULL;
00070     }
00071 }
00072 
00073 void ompl::base::GoalLazySamples::goalSamplingThread(void)
00074 {
00075     // wait for everything to be set up before performing computation
00076     while (!terminateSamplingThread_ && !si_->isSetup())
00077         boost::this_thread::sleep(time::seconds(0.01));
00078 
00079     if (!terminateSamplingThread_ && samplerFunc_)
00080     {
00081         ScopedState<> s(si_);
00082         while (!terminateSamplingThread_ && samplerFunc_(this, s.get()))
00083             addStateIfDifferent(s.get(), minDist_);
00084     }
00085     terminateSamplingThread_ = true;
00086 }
00087 
00088 bool ompl::base::GoalLazySamples::isSampling(void) const
00089 {
00090     return terminateSamplingThread_ == false && samplingThread_ != NULL;
00091 }
00092 
00093 bool ompl::base::GoalLazySamples::canSample(void) const
00094 {
00095     return maxSampleCount() > 0 || (terminateSamplingThread_ == false && samplingThread_ != NULL);
00096 }
00097 
00098 void ompl::base::GoalLazySamples::clear(void)
00099 {
00100     boost::mutex::scoped_lock slock(lock_);
00101     GoalStates::clear();
00102 }
00103 
00104 double ompl::base::GoalLazySamples::distanceGoal(const State *st) const
00105 {
00106     boost::mutex::scoped_lock slock(lock_);
00107     return GoalStates::distanceGoal(st);
00108 }
00109 
00110 void ompl::base::GoalLazySamples::sampleGoal(base::State *st) const
00111 {
00112     boost::mutex::scoped_lock slock(lock_);
00113     GoalStates::sampleGoal(st);
00114 }
00115 
00116 void ompl::base::GoalLazySamples::addState(const State* st)
00117 {
00118     boost::mutex::scoped_lock slock(lock_);
00119     GoalStates::addState(st);
00120 }
00121 
00122 bool ompl::base::GoalLazySamples::addStateIfDifferent(const State* st, double minDistance)
00123 {
00124     boost::mutex::scoped_lock slock(lock_);
00125     if (GoalStates::distanceGoal(st) > minDistance)
00126     {
00127         GoalStates::addState(st);
00128         lastStateAdded_ = true;
00129     }
00130     else
00131         lastStateAdded_ = false;
00132     return lastStateAdded_;
00133 }
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator