All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends
DiscreteMotionValidator.cpp
1 /*********************************************************************
2 * Software License Agreement (BSD License)
3 *
4 * Copyright (c) 2010, Rice University
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 *
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
17 * * Neither the name of the Rice University nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 * POSSIBILITY OF SUCH DAMAGE.
33 *********************************************************************/
34 
35 /* Author: Ioan Sucan */
36 
37 #include "ompl/base/DiscreteMotionValidator.h"
38 #include "ompl/util/Exception.h"
39 #include <queue>
40 
41 void ompl::base::DiscreteMotionValidator::defaultSettings(void)
42 {
43  stateSpace_ = si_->getStateSpace().get();
44  if (!stateSpace_)
45  throw Exception("No state space for motion validator");
46 }
47 
48 bool ompl::base::DiscreteMotionValidator::checkMotion(const State *s1, const State *s2, std::pair<State*, double> &lastValid) const
49 {
50  /* assume motion starts in a valid configuration so s1 is valid */
51 
52  bool result = true;
53  int nd = stateSpace_->validSegmentCount(s1, s2);
54 
55  if (nd > 1)
56  {
57  /* temporary storage for the checked state */
58  State *test = si_->allocState();
59 
60  for (int j = 1 ; j < nd ; ++j)
61  {
62  stateSpace_->interpolate(s1, s2, (double)j / (double)nd, test);
63  if (!si_->isValid(test))
64  {
65  lastValid.second = (double)(j - 1) / (double)nd;
66  if (lastValid.first)
67  stateSpace_->interpolate(s1, s2, lastValid.second, lastValid.first);
68  result = false;
69  break;
70  }
71  }
72  si_->freeState(test);
73  }
74 
75  if (result)
76  if (!si_->isValid(s2))
77  {
78  lastValid.second = (double)(nd - 1) / (double)nd;
79  if (lastValid.first)
80  stateSpace_->interpolate(s1, s2, lastValid.second, lastValid.first);
81  result = false;
82  }
83 
84  if (result)
85  valid_++;
86  else
87  invalid_++;
88 
89  return result;
90 }
91 
93 {
94  /* assume motion starts in a valid configuration so s1 is valid */
95  if (!si_->isValid(s2))
96  {
97  invalid_++;
98  return false;
99  }
100 
101  bool result = true;
102  int nd = stateSpace_->validSegmentCount(s1, s2);
103 
104  /* initialize the queue of test positions */
105  std::queue< std::pair<int, int> > pos;
106  if (nd >= 2)
107  {
108  pos.push(std::make_pair(1, nd - 1));
109 
110  /* temporary storage for the checked state */
111  State *test = si_->allocState();
112 
113  /* repeatedly subdivide the path segment in the middle (and check the middle) */
114  while (!pos.empty())
115  {
116  std::pair<int, int> x = pos.front();
117 
118  int mid = (x.first + x.second) / 2;
119  stateSpace_->interpolate(s1, s2, (double)mid / (double)nd, test);
120 
121  if (!si_->isValid(test))
122  {
123  result = false;
124  break;
125  }
126 
127  pos.pop();
128 
129  if (x.first < mid)
130  pos.push(std::make_pair(x.first, mid - 1));
131  if (x.second > mid)
132  pos.push(std::make_pair(mid + 1, x.second));
133  }
134 
135  si_->freeState(test);
136  }
137 
138  if (result)
139  valid_++;
140  else
141  invalid_++;
142 
143  return result;
144 }