Loading...
Searching...
No Matches
PlannerDataStorage.h
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#ifndef OMPL_BASE_PLANNER_DATA_STORAGE_
38#define OMPL_BASE_PLANNER_DATA_STORAGE_
39
40#include "ompl/base/PlannerData.h"
41#include "ompl/util/Console.h"
42#include <boost/archive/binary_oarchive.hpp>
43#include <boost/archive/binary_iarchive.hpp>
44#include <boost/serialization/vector.hpp>
45#include <boost/serialization/utility.hpp>
46#include <fstream>
47
48namespace ompl
49{
50 namespace base
51 {
80 {
81 public:
86
89 virtual bool store(const PlannerData &pd, const char *filename);
90
93 virtual bool store(const PlannerData &pd, std::ostream &out);
94
99 virtual bool load(const char *filename, PlannerData &pd);
100
105 virtual bool load(std::istream &in, PlannerData &pd);
106
107 protected:
109 struct Header
110 {
112 std::uint_fast32_t marker;
113
115 std::size_t vertex_count;
116
118 std::size_t edge_count;
119
122 std::vector<int> signature;
123
125 template <typename Archive>
126 void serialize(Archive &ar, const unsigned int /*version*/)
127 {
128 ar & marker;
129 ar & vertex_count;
130 ar & edge_count;
131 ar & signature;
132 }
133 };
134
137 {
138 enum VertexType
139 {
140 STANDARD = 0,
141 START,
142 GOAL
143 };
144
145 template <typename Archive>
146 void serialize(Archive &ar, const unsigned int /*version*/)
147 {
148 ar & v_;
149 ar & state_;
150 ar & type_;
151 }
152
153 const PlannerDataVertex *v_;
154 std::vector<unsigned char> state_;
155 VertexType type_;
156 };
157
160 {
161 template <typename Archive>
162 void serialize(Archive &ar, const unsigned int /*version*/)
163 {
164 ar & e_;
165 ar & endpoints_;
166 ar & weight_;
167 }
168
169 const PlannerDataEdge *e_;
170 std::pair<unsigned int, unsigned int> endpoints_;
171 double weight_;
172 };
173
175 virtual void loadVertices(PlannerData &pd, unsigned int numVertices, boost::archive::binary_iarchive &ia)
176 {
177 const StateSpacePtr &space = pd.getSpaceInformation()->getStateSpace();
178 std::vector<State *> states;
179 for (unsigned int i = 0; i < numVertices; ++i)
180 {
181 PlannerDataVertexData vertexData;
182 ia >> vertexData;
183
184 // Deserializing all data in the vertex (except the state)
185 const PlannerDataVertex *v = vertexData.v_;
186
187 // Allocating a new state and deserializing it from the buffer
188 State *state = space->allocState();
189 states.push_back(state);
190 space->deserialize(state, &vertexData.state_[0]);
191 const_cast<PlannerDataVertex *>(v)->state_ = state;
192
193 // Record the type of the vertex (i.e. start vertex).
194 if (vertexData.type_ == PlannerDataVertexData::START)
195 pd.addStartVertex(*v);
196 else if (vertexData.type_ == PlannerDataVertexData::GOAL)
197 pd.addGoalVertex(*v);
198 else
199 pd.addVertex(*v);
200
201 // We deserialized the vertex object pointer, and we own it.
202 // Since addEdge copies the object, it is safe to free here.
203 delete vertexData.v_;
204 }
205
206 // These vertices are using state pointers allocated here.
207 // To avoid a memory leak, we decouple planner data from the
208 // 'planner', which will clone all states and properly free the
209 // memory when PlannerData goes out of scope. Then it is safe
210 // to free all memory allocated here.
212
213 for (auto &state : states)
214 space->freeState(state);
215 }
216
218 virtual void storeVertices(const PlannerData &pd, boost::archive::binary_oarchive &oa)
219 {
220 const StateSpacePtr &space = pd.getSpaceInformation()->getStateSpace();
221 std::vector<unsigned char> state(space->getSerializationLength());
222 for (unsigned int i = 0; i < pd.numVertices(); ++i)
223 {
224 PlannerDataVertexData vertexData;
225
226 // Serializing all data in the vertex (except the state)
227 const PlannerDataVertex &v = pd.getVertex(i);
228 vertexData.v_ = &v;
229
230 // Record the type of the vertex (i.e. start vertex).
231 if (pd.isStartVertex(i))
232 vertexData.type_ = PlannerDataVertexData::START;
233 else if (pd.isGoalVertex(i))
234 vertexData.type_ = PlannerDataVertexData::GOAL;
235 else
236 vertexData.type_ = PlannerDataVertexData::STANDARD;
237
238 // Serializing the state contained in this vertex
239 space->serialize(&state[0], v.getState());
240 vertexData.state_ = state;
241
242 oa << vertexData;
243 }
244 }
245
247 virtual void loadEdges(PlannerData &pd, unsigned int numEdges, boost::archive::binary_iarchive &ia)
248 {
249 for (unsigned int i = 0; i < numEdges; ++i)
250 {
251 PlannerDataEdgeData edgeData;
252 ia >> edgeData;
253 pd.addEdge(edgeData.endpoints_.first, edgeData.endpoints_.second, *edgeData.e_,
254 Cost(edgeData.weight_));
255
256 // We deserialized the edge object pointer, and we own it.
257 // Since addEdge copies the object, it is safe to free here.
258 delete edgeData.e_;
259 }
260 }
261
263 virtual void storeEdges(const PlannerData &pd, boost::archive::binary_oarchive &oa)
264 {
265 std::vector<unsigned int> edgeList;
266 for (unsigned int fromVertex = 0; fromVertex < pd.numVertices(); ++fromVertex)
267 {
268 edgeList.clear();
269 pd.getEdges(fromVertex, edgeList); // returns the id of each edge
270
271 // Process edges
272 for (unsigned int toVertex : edgeList)
273 {
274 // Get cost
275 Cost weight;
276 if (!pd.getEdgeWeight(fromVertex, toVertex, &weight))
277 OMPL_ERROR("Unable to get edge weight");
278
279 // Convert to new structure
280 PlannerDataEdgeData edgeData;
281 edgeData.e_ = &pd.getEdge(fromVertex, toVertex);
282 edgeData.endpoints_.first = fromVertex;
283 edgeData.endpoints_.second = toVertex;
284 edgeData.weight_ = weight.value();
285 oa << edgeData;
286
287 } // for each edge
288 } // for each vertex
289 }
290 };
291 } // namespace base
292} // namespace ompl
293
294#endif
Definition of a cost value. Can represent the cost of a motion or the cost of a state.
Definition Cost.h:48
double value() const
The value of the cost.
Definition Cost.h:56
Base class for a PlannerData edge.
PlannerDataStorage()
Default constructor.
virtual bool load(const char *filename, PlannerData &pd)
Load the PlannerData structure from the given stream. The StateSpace that was used to store the data ...
virtual void storeEdges(const PlannerData &pd, boost::archive::binary_oarchive &oa)
Serialize and store all edges in pd to the binary archive.
virtual void loadVertices(PlannerData &pd, unsigned int numVertices, boost::archive::binary_iarchive &ia)
Read numVertices from the binary input ia and store them as PlannerData.
virtual bool store(const PlannerData &pd, const char *filename)
Store (serialize) the PlannerData structure to the given filename.
virtual void storeVertices(const PlannerData &pd, boost::archive::binary_oarchive &oa)
Serialize and store all vertices in pd to the binary archive.
virtual ~PlannerDataStorage()
Destructor.
virtual void loadEdges(PlannerData &pd, unsigned int numEdges, boost::archive::binary_iarchive &ia)
Read numEdges from the binary input ia and store them as PlannerData.
Base class for a vertex in the PlannerData structure. All derived classes must implement the clone an...
Definition PlannerData.h:59
virtual const State * getState() const
Retrieve the state associated with this vertex.
Definition PlannerData.h:80
Object containing planner generated vertex and edge data. It is assumed that all vertices are unique,...
bool getEdgeWeight(unsigned int v1, unsigned int v2, Cost *weight) const
Returns the weight of the edge between the given vertex indices. If there exists an edge between v1 a...
bool isGoalVertex(unsigned int index) const
Returns true if the given vertex index is marked as a goal vertex.
const SpaceInformationPtr & getSpaceInformation() const
Return the instance of SpaceInformation used in this PlannerData.
unsigned int addStartVertex(const PlannerDataVertex &v)
Adds the given vertex to the graph data, and marks it as a start vertex. The vertex index is returned...
unsigned int addGoalVertex(const PlannerDataVertex &v)
Adds the given vertex to the graph data, and marks it as a start vertex. The vertex index is returned...
unsigned int numVertices() const
Retrieve the number of vertices in this structure.
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)....
const PlannerDataEdge & getEdge(unsigned int v1, unsigned int v2) const
Retrieve a reference to the edge object connecting vertices with indexes v1 and v2....
const PlannerDataVertex & getVertex(unsigned int index) const
Retrieve a reference to the vertex object with the given index. If this vertex does not exist,...
virtual bool addEdge(unsigned int v1, unsigned int v2, const PlannerDataEdge &edge=PlannerDataEdge(), Cost weight=Cost(1.0))
Adds a directed edge between the given vertex indexes. An optional edge structure and weight can be s...
unsigned int addVertex(const PlannerDataVertex &st)
Adds the given vertex to the graph data. The vertex index is returned. Duplicates are not added....
bool isStartVertex(unsigned int index) const
Returns true if the given vertex index is marked as a start vertex.
virtual void decoupleFromPlanner()
Creates a deep copy of the states contained in the vertices of this PlannerData structure so that whe...
A shared pointer wrapper for ompl::base::StateSpace.
Definition of an abstract state.
Definition State.h:50
#define OMPL_ERROR(fmt,...)
Log a formatted error string.
Definition Console.h:64
This namespace contains sampling based planning routines shared by both planning under geometric cons...
Main namespace. Contains everything in this library.
Information stored at the beginning of the PlannerData archive.
std::uint_fast32_t marker
OMPL PlannerData specific marker (fixed value).
std::size_t edge_count
Number of edges stored in the archive.
std::vector< int > signature
Signature of state space that allocated the saved states in the vertices (see ompl::base::StateSpace:...
std::size_t vertex_count
Number of vertices stored in the archive.
void serialize(Archive &ar, const unsigned int)
boost::serialization routine
The object containing all edge data that will be stored.
The object containing all vertex data that will be stored.