All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator
HCIK.cpp
00001 /*********************************************************************
00002 * Software License Agreement (BSD License)
00003 *
00004 *  Copyright (c) 2008, 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 "ompl/geometric/ik/HCIK.h"
00038 
00039 bool ompl::geometric::HCIK::tryToImprove(const base::GoalRegion &goal, base::State *state, double nearDistance, double *betterGoalDistance) const
00040 {
00041     double tempDistance;
00042     double initialDistance;
00043 
00044     bool wasValid = valid(state);
00045     bool wasValidStart = wasValid;
00046 
00047     bool wasSatisfied = goal.isSatisfied(state, &initialDistance);
00048     bool wasSatisfiedStart = wasSatisfied;
00049 
00050     double bestDist = initialDistance;
00051 
00052     base::StateSamplerPtr ss = si_->allocStateSampler();
00053     base::State *test = si_->allocState();
00054     unsigned int noUpdateSteps = 0;
00055 
00056     for (unsigned int i = 0 ; noUpdateSteps < 10 && i < maxImproveSteps_ ; ++i)
00057     {
00058         bool update = false;
00059         ss->sampleUniformNear(test, state, nearDistance);
00060         bool isValid = valid(test);
00061         bool isSatisfied = goal.isSatisfied(test, &tempDistance);
00062         if (!wasValid && isValid)
00063         {
00064             si_->copyState(state, test);
00065             wasValid = true;
00066             wasSatisfied = isSatisfied;
00067             update = true;
00068         }
00069         else
00070             if (wasValid == isValid)
00071             {
00072                 if (!wasSatisfied && isSatisfied)
00073                 {
00074                     si_->copyState(state, test);
00075                     wasSatisfied = true;
00076                     update = true;
00077                 }
00078                 else
00079                     if (wasSatisfied == isSatisfied)
00080                     {
00081                         if (tempDistance < bestDist)
00082                         {
00083                             si_->copyState(state, test);
00084                             bestDist = tempDistance;
00085                             update = true;
00086                         }
00087                     }
00088             }
00089         if (update)
00090             noUpdateSteps = 0;
00091         else
00092             noUpdateSteps++;
00093     }
00094     si_->freeState(test);
00095 
00096     if (betterGoalDistance)
00097         *betterGoalDistance = bestDist;
00098     return (bestDist < initialDistance) || (!wasSatisfiedStart && wasSatisfied) || (!wasValidStart && wasValid);
00099 }