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 the 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 
00039 namespace
00040 {
00041 ompl::base::SpaceInformationPtr getGoalsSI(const std::vector<ompl::base::GoalPtr> &goals)
00042 {
00043   if (goals.empty())
00044     return ompl::base::SpaceInformationPtr();
00045   for (std::size_t i = 0 ; i < goals.size() ; ++i)
00046     if (!goals[i]->hasType(ompl::base::GOAL_SAMPLEABLE_REGION))
00047       throw ompl::Exception("Multiplexed goals must be instances of GoalSampleableRegion");
00048   for (std::size_t i = 1 ; i < goals.size() ; ++i)
00049     if (goals[i]->getSpaceInformation() != goals[0]->getSpaceInformation())
00050       throw ompl::Exception("The instance of SpaceInformation must be the same among the goals to be considered");
00051   return goals[0]->getSpaceInformation();
00052 }
00053 }
00054 
00055 ompl_interface::GoalSampleableRegionMux::GoalSampleableRegionMux(const std::vector<ompl::base::GoalPtr> &goals) :
00056   ompl::base::GoalSampleableRegion(getGoalsSI(goals)), goals_(goals), gindex_(0)
00057 {
00058 }
00059 
00060 void ompl_interface::GoalSampleableRegionMux::sampleGoal(ompl::base::State *st) const
00061 {
00062   for (std::size_t i = 0 ; i < goals_.size() ; ++i)
00063   {
00064     if (goals_[gindex_]->as<ompl::base::GoalSampleableRegion>()->maxSampleCount() > 0)
00065     {
00066       goals_[gindex_]->as<ompl::base::GoalSampleableRegion>()->sampleGoal(st);
00067       return;
00068     }
00069     gindex_ = (gindex_ + 1) % goals_.size();
00070   }
00071   throw ompl::Exception("There are no states to sample");
00072 }
00073 
00074 unsigned int ompl_interface::GoalSampleableRegionMux::maxSampleCount() const
00075 {
00076   unsigned int sc = 0;
00077   for (std::size_t i = 0 ; i < goals_.size() ; ++i)
00078     sc += goals_[i]->as<GoalSampleableRegion>()->maxSampleCount();
00079   return sc;
00080 }
00081 
00082 bool ompl_interface::GoalSampleableRegionMux::canSample() const
00083 {
00084   for (std::size_t i = 0 ; i < goals_.size() ; ++i)
00085     if (goals_[i]->as<ompl::base::GoalSampleableRegion>()->canSample())
00086       return true;
00087   return false;
00088 }
00089 
00090 bool ompl_interface::GoalSampleableRegionMux::isSatisfied(const ompl::base::State *st, double *distance) const
00091 {
00092   for (std::size_t i = 0 ; i < goals_.size() ; ++i)
00093     if (goals_[i]->isSatisfied(st, distance))
00094       return true;
00095   return false;
00096 }
00097 
00098 double ompl_interface::GoalSampleableRegionMux::distanceGoal(const ompl::base::State *st) const
00099 {
00100   double min_d = std::numeric_limits<double>::infinity();
00101   for (std::size_t i = 0 ; i < goals_.size() ; ++i)
00102   {
00103     double d = goals_[i]->as<ompl::base::GoalRegion>()->distanceGoal(st);
00104     if (d < min_d)
00105       min_d = d;
00106   }
00107   return min_d;
00108 }
00109 
00110 void ompl_interface::GoalSampleableRegionMux::print(std::ostream &out) const
00111 {
00112   out << "MultiGoal [" << std::endl;
00113   for (std::size_t i = 0 ; i < goals_.size() ; ++i)
00114     goals_[i]->print(out);
00115   out << "]" << std::endl;
00116 }


ompl
Author(s): Ioan Sucan
autogenerated on Mon Oct 6 2014 11:12:03