Dubins3DMotionValidator.h
1 /*********************************************************************
2  * Software License Agreement (BSD License)
3  *
4  * Copyright (c) 2024, Metron, Inc.
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 Metron, Inc. 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: Mark Moll */
36 
37 #ifndef OMPL_BASE_SPACES_DUBINS3D_MOTION_VALIDATOR_
38 #define OMPL_BASE_SPACES_DUBINS3D_MOTION_VALIDATOR_
39 
40 #include "ompl/base/SpaceInformation.h"
41 #include <queue>
42 
43 namespace ompl::base
44 {
45 
55  template <class Dubins3DStateSpace>
56  class Dubins3DMotionValidator : public MotionValidator
57  {
58  public:
59  Dubins3DMotionValidator(SpaceInformation *si) : MotionValidator(si)
60  {
61  defaultSettings();
62  }
63  Dubins3DMotionValidator(const SpaceInformationPtr &si) : MotionValidator(si)
64  {
65  defaultSettings();
66  }
67  ~Dubins3DMotionValidator() override = default;
68  bool checkMotion(const State *s1, const State *s2) const override
69  {
70  /* assume motion starts in a valid configuration so s1 is valid */
71  if (!si_->isValid(s2))
72  return false;
73  auto path = stateSpace_->getPath(s1, s2);
74  if (!path)
75  return false;
76 
77  bool result = true;
78  int nd = stateSpace_->validSegmentCount(s1, s2);
79 
80  /* initialize the queue of test positions */
81  std::queue<std::pair<int, int>> pos;
82  if (nd >= 2)
83  {
84  pos.emplace(1, nd - 1);
85 
86  /* temporary storage for the checked state */
87  State *test = si_->allocState();
88 
89  /* repeatedly subdivide the path segment in the middle (and check the middle) */
90  while (!pos.empty())
91  {
92  std::pair<int, int> x = pos.front();
93 
94  int mid = (x.first + x.second) / 2;
95  stateSpace_->interpolate(s1, s2, (double)mid / (double)nd, *path, test);
96 
97  if (!si_->isValid(test))
98  {
99  result = false;
100  break;
101  }
102 
103  pos.pop();
104 
105  if (x.first < mid)
106  pos.emplace(x.first, mid - 1);
107  if (x.second > mid)
108  pos.emplace(mid + 1, x.second);
109  }
110 
111  si_->freeState(test);
112  }
113 
114  if (result)
115  valid_++;
116  else
117  invalid_++;
118 
119  return result;
120  }
121  bool checkMotion(const State *s1, const State *s2, std::pair<State *, double> &lastValid) const override
122  {
123  auto path = stateSpace_->getPath(s1, s2);
124  if (!path)
125  return false;
126 
127  /* assume motion starts in a valid configuration so s1 is valid */
128  bool result = true;
129  int nd = stateSpace_->validSegmentCount(s1, s2);
130 
131  if (nd > 1)
132  {
133  /* temporary storage for the checked state */
134  State *test = si_->allocState();
135 
136  for (int j = 1; j < nd; ++j)
137  {
138  stateSpace_->interpolate(s1, s2, (double)j / (double)nd, *path, test);
139  if (!si_->isValid(test))
140  {
141  lastValid.second = (double)(j - 1) / (double)nd;
142  if (lastValid.first != nullptr)
143  stateSpace_->interpolate(s1, s2, lastValid.second, *path, lastValid.first);
144  result = false;
145  break;
146  }
147  }
148  si_->freeState(test);
149  }
150 
151  if (result)
152  if (!si_->isValid(s2))
153  {
154  lastValid.second = (double)(nd - 1) / (double)nd;
155  if (lastValid.first != nullptr)
156  stateSpace_->interpolate(s1, s2, lastValid.second, *path, lastValid.first);
157  result = false;
158  }
159 
160  if (result)
161  valid_++;
162  else
163  invalid_++;
164 
165  return result;
166  }
167 
168  private:
169  Dubins3DStateSpace *stateSpace_;
170  void defaultSettings()
171  {
172  stateSpace_ = dynamic_cast<Dubins3DStateSpace *>(si_->getStateSpace().get());
173  if (stateSpace_ == nullptr)
174  throw Exception("No state space for motion validator");
175  }
176  };
177 } // namespace ompl::base
178 
179 #endif
A shared pointer wrapper for ompl::base::SpaceInformation.
Definition of an abstract state.
Definition: State.h:113
This namespace contains sampling based planning routines shared by both planning under geometric cons...
void freeState(State *state) const
Free the memory of a state.
MotionValidator(SpaceInformation *si)
Constructor.
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.
State * allocState() const
Allocate memory for a state.
unsigned int invalid_
Number of invalid segments.
SpaceInformation * si_
The instance of space information this state validity checker operates on.
const StateSpacePtr & getStateSpace() const
Return the instance of the used state space.
unsigned int valid_
Number of valid segments.
bool isValid(const State *state) const
Check if a given state is valid or not.