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()
42 {
43  stateSpace_ = si_->getStateSpace().get();
44  if (stateSpace_ == nullptr)
45  throw Exception("No state space for motion validator");
46 }
47 
49  std::pair<State *, double> &lastValid) const
50 {
51  /* assume motion starts in a valid configuration so s1 is valid */
52 
53  bool result = true;
54  int nd = stateSpace_->validSegmentCount(s1, s2);
55 
56  if (nd > 1)
57  {
58  /* temporary storage for the checked state */
59  State *test = si_->allocState();
60 
61  for (int j = 1; j < nd; ++j)
62  {
63  stateSpace_->interpolate(s1, s2, (double)j / (double)nd, test);
64  if (!si_->isValid(test))
65  {
66  lastValid.second = (double)(j - 1) / (double)nd;
67  if (lastValid.first != nullptr)
68  stateSpace_->interpolate(s1, s2, lastValid.second, lastValid.first);
69  result = false;
70  break;
71  }
72  }
73  si_->freeState(test);
74  }
75 
76  if (result)
77  if (!si_->isValid(s2))
78  {
79  lastValid.second = (double)(nd - 1) / (double)nd;
80  if (lastValid.first != nullptr)
81  stateSpace_->interpolate(s1, s2, lastValid.second, lastValid.first);
82  result = false;
83  }
84 
85  if (result)
86  valid_++;
87  else
88  invalid_++;
89 
90  return result;
91 }
92 
94 {
95  /* assume motion starts in a valid configuration so s1 is valid */
96  if (!si_->isValid(s2))
97  {
98  invalid_++;
99  return false;
100  }
101 
102  bool result = true;
103  int nd = stateSpace_->validSegmentCount(s1, s2);
104 
105  /* initialize the queue of test positions */
106  std::queue<std::pair<int, int>> pos;
107  if (nd >= 2)
108  {
109  pos.push(std::make_pair(1, nd - 1));
110 
111  /* temporary storage for the checked state */
112  State *test = si_->allocState();
113 
114  /* repeatedly subdivide the path segment in the middle (and check the middle) */
115  while (!pos.empty())
116  {
117  std::pair<int, int> x = pos.front();
118 
119  int mid = (x.first + x.second) / 2;
120  stateSpace_->interpolate(s1, s2, (double)mid / (double)nd, test);
121 
122  if (!si_->isValid(test))
123  {
124  result = false;
125  break;
126  }
127 
128  pos.pop();
129 
130  if (x.first < mid)
131  pos.push(std::make_pair(x.first, mid - 1));
132  if (x.second > mid)
133  pos.push(std::make_pair(mid + 1, x.second));
134  }
135 
136  si_->freeState(test);
137  }
138 
139  if (result)
140  valid_++;
141  else
142  invalid_++;
143 
144  return result;
145 }
unsigned int invalid_
Number of invalid segments.
const StateSpacePtr & getStateSpace() const
Return the instance of the used state space.
void freeState(State *state) const
Free the memory of a state.
bool isValid(const State *state) const
Check if a given state is valid or not.
State * allocState() const
Allocate memory for a state.
SpaceInformation * si_
The instance of space information this state validity checker operates on.
unsigned int valid_
Number of valid segments.
virtual void interpolate(const State *from, const State *to, double t, State *state) const =0
Computes the state that lies at time t in [0, 1] on the segment that connects from state to to state...
Definition of an abstract state.
Definition: State.h:49
bool checkMotion(const State *s1, const State *s2) const override
Check if the path between two states (from s1 to s2) is valid. This function assumes s1 is valid...
virtual unsigned int validSegmentCount(const State *state1, const State *state2) const
Count how many segments of the "longest valid length" fit on the motion from state1 to state2...