PropositionalTriangularDecomposition.cpp
1 /*********************************************************************
2 * Software License Agreement (BSD License)
3 *
4 * Copyright (c) 2013, 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: Matt Maly */
36 
37 #include "ompl/extensions/triangle/PropositionalTriangularDecomposition.h"
38 #include "ompl/extensions/triangle/TriangularDecomposition.h"
39 #include "ompl/control/planners/ltl/PropositionalDecomposition.h"
40 #include "ompl/control/planners/ltl/World.h"
41 #include "ompl/util/RandomNumbers.h"
42 #include "ompl/base/State.h"
43 #include "ompl/base/StateSampler.h"
44 #include "ompl/base/spaces/RealVectorBounds.h"
45 #include <ostream>
46 #include <set>
47 #include <vector>
48 
49 namespace ob = ompl::base;
50 namespace oc = ompl::control;
51 
52 namespace
53 {
54  /* PropositionalTriangularDecomposition creates a WrapperDecomposition under-the-hood
55  (which represents a triangulation over the given bounds that uses
56  PropositionalTriangularDecomposition's user-defined project and sample methods)
57  and hands it upward to the PropositionalDecomposition superclass constructor. */
58  class WrapperDecomposition : public oc::TriangularDecomposition
59  {
60  public:
61  typedef TriangularDecomposition::Polygon Polygon;
62  typedef TriangularDecomposition::Vertex Vertex;
63  WrapperDecomposition(const oc::Decomposition* decomp,
64  const ob::RealVectorBounds& bounds,
65  const std::vector<Polygon>& holes,
66  const std::vector<Polygon>& props);
67  virtual ~WrapperDecomposition(void) {}
68  virtual void project(const ob::State* s, std::vector<double>& coord) const;
69  virtual void sampleFromRegion(int rid, ompl::RNG& rng, std::vector<double>& coord) const;
70  virtual void sampleFullState(const ob::StateSamplerPtr& sampler,
71  const std::vector<double>& coord, ob::State* s) const;
72  protected:
73  const oc::Decomposition* decomp_;
74  };
75 }
76 
78  const ob::RealVectorBounds& bounds, const std::vector<Polygon>& holes, const std::vector<Polygon>& props)
79  : PropositionalDecomposition(DecompositionPtr(new WrapperDecomposition(this, bounds, holes, props))),
80  triDecomp_(static_cast<TriangularDecomposition*>(decomp_.get()))
81 {
82 }
83 
85 {
86  return triDecomp_->getNumRegionsOfInterest();
87 }
88 
90 {
91  int numProps = getNumProps();
92  World world(numProps);
93  for (int p = 0; p < numProps; ++p)
94  world[p] = false;
95  if (triID == -1) return world;
96  int prop = triDecomp_->getRegionOfInterestAt(triID);
97  if (prop >= 0) world[prop] = true;
98  return world;
99 }
100 
101 void oc::PropositionalTriangularDecomposition::setup(void)
102 {
103  triDecomp_->setup();
104 }
105 
106 void oc::PropositionalTriangularDecomposition::addHole(const Polygon& hole)
107 {
108  triDecomp_->addHole(hole);
109 }
110 
111 void oc::PropositionalTriangularDecomposition::addProposition(const Polygon& prop)
112 {
113  triDecomp_->addRegionOfInterest(prop);
114 }
115 
116 const std::vector<oc::PropositionalTriangularDecomposition::Polygon>&
117  oc::PropositionalTriangularDecomposition::getHoles(void) const
118 {
119  return triDecomp_->getHoles();
120 }
121 
122 const std::vector<oc::PropositionalTriangularDecomposition::Polygon>&
123  oc::PropositionalTriangularDecomposition::getPropositions(void) const
124 {
125  return triDecomp_->getAreasOfInterest();
126 }
127 
128 void oc::PropositionalTriangularDecomposition::print(std::ostream& out) const
129 {
130  triDecomp_->print(out);
131 }
132 
133 namespace
134 {
135  WrapperDecomposition::WrapperDecomposition(const oc::Decomposition* decomp,
136  const ob::RealVectorBounds& bounds,
137  const std::vector<Polygon>& holes,
138  const std::vector<Polygon>& props)
139  : oc::TriangularDecomposition(bounds, holes, props),
140  decomp_(decomp)
141  {
142  }
143 
144  void WrapperDecomposition::project(const ob::State* s, std::vector<double>& coord) const
145  {
146  decomp_->project(s, coord);
147  }
148 
149  void WrapperDecomposition::sampleFromRegion(int rid,
150  ompl::RNG& rng,
151  std::vector<double>& coord) const
152  {
153  decomp_->sampleFromRegion(rid, rng, coord);
154  }
155 
156  void WrapperDecomposition::sampleFullState(const ob::StateSamplerPtr& sampler,
157  const std::vector<double>& coord,
158  ob::State* s) const
159  {
160  decomp_->sampleFullState(sampler, coord, s);
161  }
162 }
A TriangularDecomposition is a triangulation that ignores obstacles.
A class to represent an assignment of boolean values to propositions. A World can be partially restri...
Definition: World.h:51
virtual World worldAtRegion(int triID)
Returns the World corresponding to a given region.
This namespace contains sampling based planning routines used by planning under differential constrai...
Definition: Control.h:44
A boost shared pointer wrapper for ompl::base::StateSampler.
A Decomposition is a partition of a bounded Euclidean space into a fixed number of regions which are ...
Definition: Decomposition.h:62
virtual int getNumProps(void) const
Returns the number of propositions in this propositional decomposition.
virtual void sampleFromRegion(int triID, RNG &rng, std::vector< double > &coord) const
Samples a projected coordinate from a given region.
A propositional decomposition wraps a given Decomposition with a region-to-proposition assignment ope...
virtual void sampleFullState(const base::StateSamplerPtr &sampler, const std::vector< double > &coord, base::State *s) const =0
Samples a State using a projected coordinate and a StateSampler.
Random number generation. An instance of this class cannot be used by multiple threads at once (membe...
Definition: RandomNumbers.h:54
Definition of an abstract state.
Definition: State.h:50
This namespace contains sampling based planning routines shared by both planning under geometric cons...
Definition: Cost.h:44
virtual void project(const base::State *s, std::vector< double > &coord) const =0
Project a given State to a set of coordinates in R^k, where k is the dimension of this Decomposition...
The lower and upper bounds for an Rn space.
PropositionalTriangularDecomposition(const base::RealVectorBounds &bounds, const std::vector< Polygon > &holes=std::vector< Polygon >(), const std::vector< Polygon > &props=std::vector< Polygon >())
Creates a PropositionalTriangularDecomposition over the given bounds, which must be 2-dimensional...
A boost shared pointer wrapper for ompl::control::Decomposition.