PlannerData.cpp
1 /*********************************************************************
2 * Software License Agreement (BSD License)
3 *
4 * Copyright (c) 2012, 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: Ryan Luna */
36 
37 #include "ompl/control/PlannerData.h"
38 
40  : base::PlannerData(std::static_pointer_cast<base::SpaceInformation>(siC)), siC_(siC)
41 {
42 }
43 
45 {
46  freeMemory();
47 }
48 
50 {
52 }
53 
55 {
56  unsigned int index = vertexIndex(st);
57  if (index == INVALID_INDEX)
58  return false;
59 
60  std::map<unsigned int, const base::PlannerDataEdge *> edgeMap;
61  getEdges(index, edgeMap);
62 
63  for (auto &edgemapit : edgeMap)
64  {
65  // Before deleting the edge, free the control associated with it, if it was decoupled
66  Control *ctrl =
67  const_cast<Control *>(static_cast<const PlannerDataEdgeControl *>(edgemapit.second)->getControl());
68  auto it = decoupledControls_.find(ctrl);
69  if (it != decoupledControls_.end())
70  {
71  siC_->freeControl(*it);
72  decoupledControls_.erase(it);
73  }
74  }
75 
77 }
78 
79 bool ompl::control::PlannerData::removeEdge(unsigned int v1, unsigned int v2)
80 {
82 }
83 
86 {
87  unsigned int index1, index2;
88  index1 = vertexIndex(v1);
89  index2 = vertexIndex(v2);
90 
91  if (index1 == INVALID_INDEX || index2 == INVALID_INDEX)
92  return false;
93 
94  // Before deleting the edge, free the control associated with it, if it was decoupled
95  PlannerDataEdgeControl &edge = static_cast<PlannerDataEdgeControl &>(getEdge(index1, index2));
96  Control *ctrl = const_cast<Control *>(edge.getControl());
97  auto it = decoupledControls_.find(ctrl);
98  if (it != decoupledControls_.end())
99  {
100  siC_->freeControl(*it);
101  decoupledControls_.erase(it);
102  }
103 
104  return ompl::base::PlannerData::removeEdge(index1, index2);
105 }
106 
108 {
110 
111  freeMemory();
112  decoupledControls_.clear();
113 }
114 
116 {
118 
119  for (unsigned int i = 0; i < numVertices(); ++i)
120  {
121  for (unsigned int j = 0; j < numVertices(); ++j)
122  {
123  if (edgeExists(i, j))
124  {
125  PlannerDataEdgeControl &edge = static_cast<PlannerDataEdgeControl &>(getEdge(i, j));
126  // If this edge's control is not in the decoupled list, clone it and add it
127  Control *ctrl = const_cast<Control *>(edge.getControl());
128  if (decoupledControls_.find(ctrl) == decoupledControls_.end())
129  {
130  Control *clone = siC_->cloneControl(ctrl);
131  decoupledControls_.insert(clone);
132  // Replacing the shallow control pointer with our shiny new clone
133  edge.c_ = clone;
134  }
135  }
136  }
137  }
138 }
139 
141 {
142  return siC_;
143 }
144 
146 {
147  return true;
148 }
149 
150 void ompl::control::PlannerData::freeMemory()
151 {
152  for (auto decoupledControl : decoupledControls_)
153  siC_->freeControl(decoupledControl);
154 }
void decoupleFromPlanner() override
Creates a deep copy of the states contained in the vertices of this PlannerData structure so that whe...
virtual void decoupleFromPlanner()
Creates a deep copy of the states contained in the vertices of this PlannerData structure so that whe...
Definition: PlannerData.cpp:80
Definition of an abstract control.
Definition: Control.h:47
unsigned int vertexIndex(const PlannerDataVertex &v) const
Return the index for the vertex associated with the given data. INVALID_INDEX is returned if this ver...
STL namespace.
unsigned int numVertices() const
Retrieve the number of vertices in this structure.
Representation of an edge in PlannerData for planning with controls. This structure encodes a specifi...
Definition: PlannerData.h:60
PlannerData(const SpaceInformationPtr &siC)
Constructor. Accepts a SpaceInformationPtr for the space planned in.
Definition: PlannerData.cpp:39
bool hasControls() const override
Returns true if this PlannerData instance has controls associated with it.
const PlannerDataEdge & getEdge(unsigned int v1, unsigned int v2) const
Retrieve a reference to the edge object connecting vertices with indexes v1 and v2. If this edge does not exist, NO_EDGE is returned.
Base class for a vertex in the PlannerData structure. All derived classes must implement the clone an...
Definition: PlannerData.h:58
SpaceInformationPtr siC_
The instance of control::SpaceInformation associated with this data.
Definition: PlannerData.h:165
bool removeVertex(const base::PlannerDataVertex &st) override
Removes the vertex associated with the given data. If the vertex does not exist, false is returned...
Definition: PlannerData.cpp:54
virtual bool removeVertex(const PlannerDataVertex &st)
Removes the vertex associated with the given data. If the vertex does not exist, false is returned...
const SpaceInformationPtr & getSpaceInformation() const
Return the instance of SpaceInformation used in this PlannerData.
std::set< Control * > decoupledControls_
A list of controls that are allocated during the decoupleFromPlanner method. These controls are freed...
Definition: PlannerData.h:168
~PlannerData() override
Destructor.
Definition: PlannerData.cpp:44
virtual void clear()
Clears the entire data structure.
Definition: PlannerData.cpp:74
A shared pointer wrapper for ompl::control::SpaceInformation.
const Control * getControl() const
Return the control associated with this edge.
Definition: PlannerData.h:81
static const unsigned int INVALID_INDEX
Representation of an invalid vertex index.
Definition: PlannerData.h:184
bool edgeExists(unsigned int v1, unsigned int v2) const
Check whether an edge between vertex index v1 and index v2 exists.
unsigned int getEdges(unsigned int v, std::vector< unsigned int > &edgeList) const
Returns a list of the vertex indexes directly connected to vertex with index v (outgoing edges)...
void clear() override
Clears the entire data structure.
Space information containing necessary information for planning with controls. setup() needs to be ca...
virtual bool removeEdge(unsigned int v1, unsigned int v2)
Removes the edge between vertex indexes v1 and v2. Success is returned.
bool removeEdge(unsigned int v1, unsigned int v2) override
Removes the edge between vertex indexes v1 and v2. Success is returned.
Definition: PlannerData.cpp:79