Loading...
Searching...
No Matches
DubinsMotionValidator.h
1/*********************************************************************
2 * Software License Agreement (BSD License)
3 *
4 * Copyright (c) 2025, Autonomous Systems Laboratory, ETH Zurich
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 ETH Zurich 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: Jaeyoung Lim */
36
37#ifndef OMPL_BASE_SPACES_DUBINS_MOTION_VALIDATOR_
38#define OMPL_BASE_SPACES_DUBINS_MOTION_VALIDATOR_
39
40#include "ompl/base/SpaceInformation.h"
41#include <queue>
42
43namespace ompl
44{
45 namespace base
46 {
53 template <class Dubins2DStateSpace>
54 class DubinsMotionValidator : public MotionValidator
55 {
56 public:
57 DubinsMotionValidator(SpaceInformation *si) : MotionValidator(si)
58 {
59 defaultSettings();
60 }
61 DubinsMotionValidator(const SpaceInformationPtr &si) : MotionValidator(si)
62 {
63 defaultSettings();
64 }
65 ~DubinsMotionValidator() override = default;
66 bool checkMotion(const State *s1, const State *s2) const override
67 {
68 /* assume motion starts in a valid configuration so s1 is valid */
69 if (!si_->isValid(s2))
70 return false;
71 auto path = stateSpace_->getPath(s1, s2);
72 // if (!path)
73 // return false;
74
75 bool result = true, firstTime = true;
76 int nd = stateSpace_->validSegmentCount(s1, s2);
77
78 /* initialize the queue of test positions */
79 std::queue<std::pair<int, int>> pos;
80 if (nd >= 2)
81 {
82 pos.emplace(1, nd - 1);
83
84 /* temporary storage for the checked state */
85 State *test = si_->allocState();
86
87 /* repeatedly subdivide the path segment in the middle (and check the middle) */
88 while (!pos.empty())
89 {
90 std::pair<int, int> x = pos.front();
91
92 int mid = (x.first + x.second) / 2;
93 stateSpace_->interpolate(s1, s2, (double)mid / (double)nd, firstTime, path, test);
94
95 if (!si_->isValid(test))
96 {
97 result = false;
98 break;
99 }
100
101 pos.pop();
102
103 if (x.first < mid)
104 pos.emplace(x.first, mid - 1);
105 if (x.second > mid)
106 pos.emplace(mid + 1, x.second);
107 }
108
109 si_->freeState(test);
110 }
111
112 if (result)
113 valid_++;
114 else
115 invalid_++;
116
117 return result;
118 }
119
120 bool checkMotion(const State *s1, const State *s2, std::pair<State *, double> &lastValid) const override
121 {
122 /* assume motion starts in a valid configuration so s1 is valid */
123
124 auto path = stateSpace_->getPath(s1, s2);
125 // if (!path)
126 // return false;
127
128 bool result = true, firstTime = 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, firstTime, 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, firstTime, 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, firstTime, 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 Dubins2DStateSpace *stateSpace_;
170 void defaultSettings()
171 {
172 stateSpace_ = dynamic_cast<Dubins2DStateSpace *>(si_->getStateSpace().get());
173 if (stateSpace_ == nullptr)
174 throw Exception("No state space for motion validator");
175 }
176 };
177 } // namespace base
178} // namespace ompl
179
180#endif
The exception type for ompl.
Definition Exception.h:47
bool checkMotion(const State *s1, const State *s2, std::pair< State *, double > &lastValid) const override
Check if the path between two states is valid. Also compute the last state that was valid and the tim...
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.
MotionValidator(SpaceInformation *si)
Constructor.
SpaceInformation * si_
The instance of space information this state validity checker operates on.
unsigned int valid_
Number of valid segments.
unsigned int invalid_
Number of invalid segments.
A shared pointer wrapper for ompl::base::SpaceInformation.
The base class for space information. This contains all the information about the space planning is d...
const StateSpacePtr & getStateSpace() const
Return the instance of the used state space.
Definition of an abstract state.
Definition State.h:50
This namespace contains sampling based planning routines shared by both planning under geometric cons...
Main namespace. Contains everything in this library.