ompl_exo.cpp
Go to the documentation of this file.
1 //
2 // Copyright (c) 2018, University of Edinburgh
3 // All rights reserved.
4 //
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are met:
7 //
8 // * Redistributions of source code must retain the above copyright notice,
9 // this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above copyright
11 // notice, this list of conditions and the following disclaimer in the
12 // documentation and/or other materials provided with the distribution.
13 // * Neither the name of nor the names of its contributors may be used to
14 // endorse or promote products derived from this software without specific
15 // prior written permission.
16 //
17 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21 // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25 // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 // POSSIBILITY OF SUCH DAMAGE.
28 //
29 
31 
32 namespace exotica
33 {
34 OMPLStateValidityChecker::OMPLStateValidityChecker(const ompl::base::SpaceInformationPtr &si, const SamplingProblemPtr &prob) : ompl::base::StateValidityChecker(si), prob_(prob)
35 {
36 }
37 
38 bool OMPLStateValidityChecker::isValid(const ompl::base::State *state) const
39 {
40  double tmp = 0.0;
41  return isValid(state, tmp);
42 }
43 
44 bool OMPLStateValidityChecker::isValid(const ompl::base::State *state, double &dist) const
45 {
46  Eigen::VectorXd q(prob_->N);
47 
48 #if ROS_VERSION_MINIMUM(1, 12, 0) // if ROS version >= ROS_KINETIC
49  std::static_pointer_cast<OMPLStateSpace>(si_->getStateSpace())->OMPLToExoticaState(state, q);
50 #else
51  boost::static_pointer_cast<OMPLStateSpace>(si_->getStateSpace())->OMPLToExoticaState(state, q);
52 #endif
53 
54  if (!prob_->IsStateValid(q))
55  {
56  dist = -1;
57  return false;
58  }
59  return true;
60 }
61 
62 OMPLRNStateSpace::OMPLRNStateSpace(OMPLSolverInitializer init) : OMPLStateSpace(init)
63 {
64  setName("OMPLRNStateSpace");
65 }
66 
67 ompl::base::StateSamplerPtr OMPLRNStateSpace::allocDefaultStateSampler() const
68 {
69  return CompoundStateSpace::allocDefaultStateSampler();
70 }
71 
73 {
74  unsigned int dim = prob->N;
75  addSubspace(ompl::base::StateSpacePtr(new ompl::base::RealVectorStateSpace(dim)), 1.0);
76  ompl::base::RealVectorBounds ompl_bounds(dim);
77  auto bounds = prob->GetBounds();
78  for (unsigned int i = 0; i < dim; ++i)
79  {
80  ompl_bounds.setHigh(i, bounds[i + dim]);
81  ompl_bounds.setLow(i, bounds[i]);
82  }
83  getSubspace(0)->as<ompl::base::RealVectorStateSpace>()->setBounds(ompl_bounds);
84  setLongestValidSegmentFraction(init_.LongestValidSegmentFraction);
85  lock();
86 }
87 
88 void OMPLRNStateSpace::ExoticaToOMPLState(const Eigen::VectorXd &q, ompl::base::State *state) const
89 {
90  if (!state)
91  {
92  ThrowPretty("Invalid state!");
93  }
94  if (q.rows() != (int)getDimension())
95  {
96  ThrowPretty("State vector (" << q.rows() << ") and internal state (" << (int)getDimension() << ") dimension disagree");
97  }
98  memcpy(state->as<OMPLRNStateSpace::StateType>()->getRNSpace().values, q.data(), sizeof(double) * q.rows());
99 }
100 
101 void OMPLRNStateSpace::OMPLToExoticaState(const ompl::base::State *state, Eigen::VectorXd &q) const
102 {
103  if (!state)
104  {
105  ThrowPretty("Invalid state!");
106  }
107  if (q.rows() != (int)getDimension())
108  q.resize((int)getDimension());
109  memcpy(q.data(), state->as<OMPLRNStateSpace::StateType>()->getRNSpace().values, sizeof(double) * q.rows());
110 }
111 
112 void OMPLRNStateSpace::StateDebug(const Eigen::VectorXd &q) const
113 {
114  // TODO
115 }
116 
117 OMPLSE3RNStateSpace::OMPLSE3RNStateSpace(OMPLSolverInitializer init) : OMPLStateSpace(init)
118 {
119  setName("OMPLSE3RNStateSpace");
120 }
121 
122 ompl::base::StateSamplerPtr OMPLSE3RNStateSpace::allocDefaultStateSampler() const
123 {
124  return CompoundStateSpace::allocDefaultStateSampler();
125 }
126 
128 {
129  dim_ = prob->N;
130  addSubspace(ompl::base::StateSpacePtr(new ompl::base::SE3StateSpace()), 1.0);
131 
132  if (dim_ > 6)
133  {
134  addSubspace(ompl::base::StateSpacePtr(new ompl::base::RealVectorStateSpace(dim_ - 6)), 1.0);
135  }
136 
137  auto bounds = prob->GetBounds();
138  if (bounds.size() == 2 * dim_)
139  {
140  // NB: We can only bound the xyz, rotation is unbounded (!)
141  ompl::base::RealVectorBounds SE3bounds(3);
142  for (int i = 0; i < 3; ++i)
143  {
144  SE3bounds.setHigh(i, bounds[i + dim_]);
145  SE3bounds.setLow(i, bounds[i]);
146  }
147  getSubspace(0)->as<ompl::base::SE3StateSpace>()->setBounds(SE3bounds);
148  WARNING_NAMED("OMPLSE3RNStateSpace::SetBounds", "Orientation bounds on SE(3) component ignored.");
149 
150  if (dim_ > 6)
151  {
152  ompl::base::RealVectorBounds RNbounds(dim_ - 6);
153  for (unsigned int i = 6; i < dim_; ++i)
154  {
155  RNbounds.setHigh(i - 6, bounds[i + dim_]);
156  RNbounds.setLow(i - 6, bounds[i]);
157  }
158  getSubspace(1)->as<ompl::base::RealVectorStateSpace>()->setBounds(RNbounds);
159  }
160  }
161  else
162  {
163  ERROR("State space bounds were not specified!\n"
164  << bounds.size() << " " << dim_);
165  }
166  setLongestValidSegmentFraction(init_.LongestValidSegmentFraction);
167  lock();
168 }
169 
170 void OMPLSE3RNStateSpace::StateDebug(const Eigen::VectorXd &q) const
171 {
172 }
173 
174 void OMPLSE3RNStateSpace::ExoticaToOMPLState(const Eigen::VectorXd &q, ompl::base::State *state) const
175 {
176  OMPLSE3RNStateSpace::StateType *statetype = static_cast<OMPLSE3RNStateSpace::StateType *>(state);
177  statetype->SE3StateSpace().setXYZ(q(0), q(1), q(2));
178  KDL::Rotation tmp = KDL::Rotation::RPY(q(3), q(4), q(5));
179  tmp.GetQuaternion(statetype->SE3StateSpace().rotation().x, statetype->SE3StateSpace().rotation().y, statetype->SE3StateSpace().rotation().z, statetype->SE3StateSpace().rotation().w);
180 
181  if (dim_ > 6)
182  {
183  memcpy(statetype->RealVectorStateSpace().values, q.segment(6, q.rows() - 6).data(), sizeof(double) * (q.rows() - 6));
184  }
185 }
186 
187 void OMPLSE3RNStateSpace::OMPLToExoticaState(const ompl::base::State *state, Eigen::VectorXd &q) const
188 {
189  q.setZero(getDimension());
190  const OMPLSE3RNStateSpace::StateType *statetype = static_cast<const OMPLSE3RNStateSpace::StateType *>(state);
191  q(0) = statetype->SE3StateSpace().getX();
192  q(1) = statetype->SE3StateSpace().getY();
193  q(2) = statetype->SE3StateSpace().getZ();
194 
195  KDL::Rotation tmp = KDL::Rotation::Quaternion(statetype->SE3StateSpace().rotation().x, statetype->SE3StateSpace().rotation().y, statetype->SE3StateSpace().rotation().z, statetype->SE3StateSpace().rotation().w);
196  tmp.GetRPY(q(3), q(4), q(5));
197 
198  if (dim_ > 6)
199  {
200  memcpy(q.segment(6, q.rows() - 6).data(), statetype->RealVectorStateSpace().values, sizeof(double) * (q.rows() - 6));
201  }
202 }
203 
204 OMPLSE2RNStateSpace::OMPLSE2RNStateSpace(OMPLSolverInitializer init) : OMPLStateSpace(init)
205 {
206  setName("OMPLSE2RNStateSpace");
207 }
208 
209 ompl::base::StateSamplerPtr OMPLSE2RNStateSpace::allocDefaultStateSampler() const
210 {
211  return CompoundStateSpace::allocDefaultStateSampler();
212 }
213 
215 {
216  dim_ = prob->N;
217  addSubspace(ompl::base::StateSpacePtr(new ompl::base::SE2StateSpace()), 1.0);
218  if (dim_ > 3)
219  {
220  addSubspace(ompl::base::StateSpacePtr(new ompl::base::RealVectorStateSpace(dim_ - 3)), 1.0);
221  }
222 
223  auto bounds = prob->GetBounds();
224  if (bounds.size() == 2 * dim_)
225  {
226  ompl::base::RealVectorBounds SE2bounds(2);
227  for (int i = 0; i < 3; ++i)
228  {
229  SE2bounds.setHigh(i, bounds[i + dim_]);
230  SE2bounds.setLow(i, bounds[i]);
231  }
232  getSubspace(0)->as<ompl::base::SE2StateSpace>()->setBounds(SE2bounds);
233  WARNING_NAMED("OMPLSE2RNStateSpace::SetBounds", "Yaw bounds on SE(2) component ignored.");
234 
235  if (dim_ > 3)
236  {
237  ompl::base::RealVectorBounds RNbounds(dim_ - 3);
238  for (unsigned int i = 3; i < dim_; ++i)
239  {
240  RNbounds.setHigh(i - 3, prob->GetBounds()[i + dim_]);
241  RNbounds.setLow(i - 3, prob->GetBounds()[i]);
242  }
243  getSubspace(1)->as<ompl::base::RealVectorStateSpace>()->setBounds(RNbounds);
244  }
245  }
246  else
247  {
248  ERROR("State space bounds were not specified!" << std::endl
249  << bounds.size() << " " << dim_);
250  }
251  setLongestValidSegmentFraction(init_.LongestValidSegmentFraction);
252  lock();
253 }
254 
255 void OMPLSE2RNStateSpace::StateDebug(const Eigen::VectorXd &q) const
256 {
257 }
258 
259 void OMPLSE2RNStateSpace::ExoticaToOMPLState(const Eigen::VectorXd &q, ompl::base::State *state) const
260 {
261  OMPLSE2RNStateSpace::StateType *statetype = static_cast<OMPLSE2RNStateSpace::StateType *>(state);
262  statetype->SE2StateSpace().setXY(q(0), q(1));
263  statetype->SE2StateSpace().setYaw(q(2));
264 
265  if (dim_ > 3)
266  {
267  memcpy(statetype->RealVectorStateSpace().values, q.segment(3, q.rows() - 3).data(), sizeof(double) * (q.rows() - 3));
268  }
269 }
270 
271 void OMPLSE2RNStateSpace::OMPLToExoticaState(const ompl::base::State *state, Eigen::VectorXd &q) const
272 {
273  q.setZero(getDimension());
274  const OMPLSE2RNStateSpace::StateType *statetype = static_cast<const OMPLSE2RNStateSpace::StateType *>(state);
275  q(0) = statetype->SE2StateSpace().getX();
276  q(1) = statetype->SE2StateSpace().getY();
277  q(2) = statetype->SE2StateSpace().getYaw();
278 
279  if (dim_ > 3)
280  {
281  memcpy(q.segment(3, q.rows() - 3).data(), statetype->RealVectorStateSpace().values, sizeof(double) * (q.rows() - 3));
282  }
283 }
284 
286 {
287  setName("OMPLDubinsRNStateSpace");
288 }
289 
290 ompl::base::StateSamplerPtr OMPLDubinsRNStateSpace::allocDefaultStateSampler() const
291 {
292  return CompoundStateSpace::allocDefaultStateSampler();
293 }
294 
296 {
297  dim_ = prob->N;
298  addSubspace(ompl::base::StateSpacePtr(new ompl::base::DubinsStateSpace(init_.DubinsStateSpaceTurningRadius, init_.DubinsStateSpaceIsSymmetric)), 1.0);
299  if (dim_ > 3)
300  {
301  addSubspace(ompl::base::StateSpacePtr(new ompl::base::RealVectorStateSpace(dim_ - 3)), 1.0);
302  }
303 
304  auto bounds = prob->GetBounds();
305  if (bounds.size() == 2 * dim_)
306  {
307  ompl::base::RealVectorBounds Dubinsbounds(2);
308  for (int i = 0; i < 3; ++i)
309  {
310  Dubinsbounds.setHigh(i, bounds[i + dim_]);
311  Dubinsbounds.setLow(i, bounds[i]);
312  }
313  getSubspace(0)->as<ompl::base::DubinsStateSpace>()->setBounds(Dubinsbounds);
314  WARNING_NAMED("OMPLDubinsRNStateSpace::SetBounds", "Yaw bounds on SE(2) component ignored.");
315 
316  if (dim_ > 3)
317  {
318  ompl::base::RealVectorBounds RNbounds(dim_ - 3);
319  for (unsigned int i = 3; i < dim_; ++i)
320  {
321  RNbounds.setHigh(i - 3, prob->GetBounds()[i + dim_]);
322  RNbounds.setLow(i - 3, prob->GetBounds()[i]);
323  }
324  getSubspace(1)->as<ompl::base::RealVectorStateSpace>()->setBounds(RNbounds);
325  }
326  }
327  else
328  {
329  ERROR("State space bounds were not specified!" << std::endl
330  << bounds.size() << " " << dim_);
331  }
332  setLongestValidSegmentFraction(init_.LongestValidSegmentFraction);
333  lock();
334 }
335 
336 void OMPLDubinsRNStateSpace::StateDebug(const Eigen::VectorXd &q) const
337 {
338 }
339 
340 void OMPLDubinsRNStateSpace::ExoticaToOMPLState(const Eigen::VectorXd &q, ompl::base::State *state) const
341 {
342  OMPLDubinsRNStateSpace::StateType *statetype = static_cast<OMPLDubinsRNStateSpace::StateType *>(state);
343  statetype->DubinsStateSpace().setXY(q(0), q(1));
344  statetype->DubinsStateSpace().setYaw(q(2));
345 
346  if (dim_ > 3)
347  {
348  memcpy(statetype->RealVectorStateSpace().values, q.segment(3, q.rows() - 3).data(), sizeof(double) * (q.rows() - 3));
349  }
350 }
351 
352 void OMPLDubinsRNStateSpace::OMPLToExoticaState(const ompl::base::State *state, Eigen::VectorXd &q) const
353 {
354  q.setZero(getDimension());
355  const OMPLDubinsRNStateSpace::StateType *statetype = static_cast<const OMPLDubinsRNStateSpace::StateType *>(state);
356  q(0) = statetype->DubinsStateSpace().getX();
357  q(1) = statetype->DubinsStateSpace().getY();
358  q(2) = statetype->DubinsStateSpace().getYaw();
359 
360  if (dim_ > 3)
361  {
362  memcpy(q.segment(3, q.rows() - 3).data(), statetype->RealVectorStateSpace().values, sizeof(double) * (q.rows() - 3));
363  }
364 }
365 } // namespace exotica
void ExoticaToOMPLState(const Eigen::VectorXd &q, ompl::base::State *state) const override
Definition: ompl_exo.cpp:88
void StateDebug(const Eigen::VectorXd &q) const override
Definition: ompl_exo.cpp:170
void ExoticaToOMPLState(const Eigen::VectorXd &q, ompl::base::State *state) const override
Definition: ompl_exo.cpp:174
const ompl::base::SE3StateSpace::StateType & SE3StateSpace() const
Definition: ompl_exo.h:134
void SetBounds(SamplingProblemPtr &prob) override
Definition: ompl_exo.cpp:72
#define WARNING_NAMED(name, x)
void OMPLToExoticaState(const ompl::base::State *state, Eigen::VectorXd &q) const override
Definition: ompl_exo.cpp:101
void ExoticaToOMPLState(const Eigen::VectorXd &q, ompl::base::State *state) const override
Definition: ompl_exo.cpp:340
void OMPLToExoticaState(const ompl::base::State *state, Eigen::VectorXd &q) const override
Definition: ompl_exo.cpp:352
static Rotation Quaternion(double x, double y, double z, double w)
#define ThrowPretty(m)
#define ERROR(x)
const ompl::base::RealVectorStateSpace::StateType & RealVectorStateSpace() const
Definition: ompl_exo.h:124
bool isValid(const ompl::base::State *state) const override
Definition: ompl_exo.cpp:38
const ompl::base::SE2StateSpace::StateType & SE2StateSpace() const
Definition: ompl_exo.h:175
ompl::base::StateSamplerPtr allocDefaultStateSampler() const override
Definition: ompl_exo.cpp:209
void GetQuaternion(double &x, double &y, double &z, double &w) const
OMPLDubinsRNStateSpace(OMPLSolverInitializer init)
Definition: ompl_exo.cpp:285
const ompl::base::DubinsStateSpace::StateType & DubinsStateSpace() const
Definition: ompl_exo.h:216
void OMPLToExoticaState(const ompl::base::State *state, Eigen::VectorXd &q) const override
Definition: ompl_exo.cpp:187
void OMPLToExoticaState(const ompl::base::State *state, Eigen::VectorXd &q) const override
Definition: ompl_exo.cpp:271
void SetBounds(SamplingProblemPtr &prob) override
Definition: ompl_exo.cpp:295
static Rotation RPY(double roll, double pitch, double yaw)
const ompl::base::RealVectorStateSpace::StateType & RealVectorStateSpace() const
Definition: ompl_exo.h:206
void ExoticaToOMPLState(const Eigen::VectorXd &q, ompl::base::State *state) const override
Definition: ompl_exo.cpp:259
void StateDebug(const Eigen::VectorXd &q) const override
Definition: ompl_exo.cpp:336
std::shared_ptr< exotica::SamplingProblem > SamplingProblemPtr
SamplingProblemPtr prob_
Definition: ompl_exo.h:82
void GetRPY(double &roll, double &pitch, double &yaw) const
void StateDebug(const Eigen::VectorXd &q) const override
Definition: ompl_exo.cpp:255
const ompl::base::RealVectorStateSpace::StateType & getRNSpace() const
Definition: ompl_exo.h:95
void SetBounds(SamplingProblemPtr &prob) override
Definition: ompl_exo.cpp:214
OMPLSE3RNStateSpace(OMPLSolverInitializer init)
Definition: ompl_exo.cpp:117
OMPLStateValidityChecker(const ompl::base::SpaceInformationPtr &si, const SamplingProblemPtr &prob)
Definition: ompl_exo.cpp:34
const ompl::base::RealVectorStateSpace::StateType & RealVectorStateSpace() const
Definition: ompl_exo.h:165
OMPLSolverInitializer init_
Definition: ompl_exo.h:69
ompl::base::StateSamplerPtr allocDefaultStateSampler() const override
Definition: ompl_exo.cpp:122
void StateDebug(const Eigen::VectorXd &q) const override
Definition: ompl_exo.cpp:112
void SetBounds(SamplingProblemPtr &prob) override
Definition: ompl_exo.cpp:127
ompl::base::StateSamplerPtr allocDefaultStateSampler() const override
Definition: ompl_exo.cpp:290
OMPLRNStateSpace(OMPLSolverInitializer init)
Definition: ompl_exo.cpp:62
OMPLSE2RNStateSpace(OMPLSolverInitializer init)
Definition: ompl_exo.cpp:204
ompl::base::StateSamplerPtr allocDefaultStateSampler() const override
Definition: ompl_exo.cpp:67


exotica_ompl_solver
Author(s): Yiming Yang
autogenerated on Sat Apr 10 2021 02:36:37