goal_union.cpp
Go to the documentation of this file.
00001 /*********************************************************************
00002  * Software License Agreement (BSD License)
00003  *
00004  *  Copyright (c) 2011, Willow Garage, Inc.
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 Willow Garage 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 <moveit/ompl_interface/detail/goal_union.h>
00038 #include <ompl/base/goals/GoalLazySamples.h>
00039 
00040 namespace
00041 {
00042 ompl::base::SpaceInformationPtr getGoalsSI(const std::vector<ompl::base::GoalPtr>& goals)
00043 {
00044   if (goals.empty())
00045     return ompl::base::SpaceInformationPtr();
00046   for (std::size_t i = 0; i < goals.size(); ++i)
00047     if (!goals[i]->hasType(ompl::base::GOAL_SAMPLEABLE_REGION))
00048       throw ompl::Exception("Multiplexed goals must be instances of GoalSampleableRegion");
00049   for (std::size_t i = 1; i < goals.size(); ++i)
00050     if (goals[i]->getSpaceInformation() != goals[0]->getSpaceInformation())
00051       throw ompl::Exception("The instance of SpaceInformation must be the same among the goals to be considered");
00052   return goals[0]->getSpaceInformation();
00053 }
00054 }
00055 
00056 ompl_interface::GoalSampleableRegionMux::GoalSampleableRegionMux(const std::vector<ompl::base::GoalPtr>& goals)
00057   : ompl::base::GoalSampleableRegion(getGoalsSI(goals)), goals_(goals), gindex_(0)
00058 {
00059 }
00060 
00061 void ompl_interface::GoalSampleableRegionMux::startSampling()
00062 {
00063   for (std::size_t i = 0; i < goals_.size(); ++i)
00064     if (goals_[i]->hasType(ompl::base::GOAL_LAZY_SAMPLES))
00065       static_cast<ompl::base::GoalLazySamples*>(goals_[i].get())->startSampling();
00066 }
00067 
00068 void ompl_interface::GoalSampleableRegionMux::stopSampling()
00069 {
00070   for (std::size_t i = 0; i < goals_.size(); ++i)
00071     if (goals_[i]->hasType(ompl::base::GOAL_LAZY_SAMPLES))
00072       static_cast<ompl::base::GoalLazySamples*>(goals_[i].get())->stopSampling();
00073 }
00074 
00075 void ompl_interface::GoalSampleableRegionMux::sampleGoal(ompl::base::State* st) const
00076 {
00077   for (std::size_t i = 0; i < goals_.size(); ++i)
00078   {
00079     if (goals_[gindex_]->as<ompl::base::GoalSampleableRegion>()->maxSampleCount() > 0)
00080     {
00081       goals_[gindex_]->as<ompl::base::GoalSampleableRegion>()->sampleGoal(st);
00082       return;
00083     }
00084     gindex_ = (gindex_ + 1) % goals_.size();
00085   }
00086   throw ompl::Exception("There are no states to sample");
00087 }
00088 
00089 unsigned int ompl_interface::GoalSampleableRegionMux::maxSampleCount() const
00090 {
00091   unsigned int sc = 0;
00092   for (std::size_t i = 0; i < goals_.size(); ++i)
00093     sc += goals_[i]->as<GoalSampleableRegion>()->maxSampleCount();
00094   return sc;
00095 }
00096 
00097 bool ompl_interface::GoalSampleableRegionMux::canSample() const
00098 {
00099   for (std::size_t i = 0; i < goals_.size(); ++i)
00100     if (goals_[i]->as<ompl::base::GoalSampleableRegion>()->canSample())
00101       return true;
00102   return false;
00103 }
00104 
00105 bool ompl_interface::GoalSampleableRegionMux::couldSample() const
00106 {
00107   for (std::size_t i = 0; i < goals_.size(); ++i)
00108     if (goals_[i]->as<ompl::base::GoalSampleableRegion>()->couldSample())
00109       return true;
00110   return false;
00111 }
00112 
00113 bool ompl_interface::GoalSampleableRegionMux::isSatisfied(const ompl::base::State* st, double* distance) const
00114 {
00115   for (std::size_t i = 0; i < goals_.size(); ++i)
00116     if (goals_[i]->isSatisfied(st, distance))
00117       return true;
00118   return false;
00119 }
00120 
00121 double ompl_interface::GoalSampleableRegionMux::distanceGoal(const ompl::base::State* st) const
00122 {
00123   double min_d = std::numeric_limits<double>::infinity();
00124   for (std::size_t i = 0; i < goals_.size(); ++i)
00125   {
00126     double d = goals_[i]->as<ompl::base::GoalRegion>()->distanceGoal(st);
00127     if (d < min_d)
00128       min_d = d;
00129   }
00130   return min_d;
00131 }
00132 
00133 void ompl_interface::GoalSampleableRegionMux::print(std::ostream& out) const
00134 {
00135   out << "MultiGoal [" << std::endl;
00136   for (std::size_t i = 0; i < goals_.size(); ++i)
00137     goals_[i]->print(out);
00138   out << "]" << std::endl;
00139 }


ompl
Author(s): Ioan Sucan
autogenerated on Wed Jun 19 2019 19:24:27