PlannerDataStorage.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/PlannerDataStorage.h"
38 #include <boost/archive/archive_exception.hpp>
39 
41 static const boost::uint32_t OMPL_PLANNER_DATA_CONTROL_ARCHIVE_MARKER = 0x5044434D; // this spells PDCM
43 
45 {
46 }
47 
49 
51 {
52  base::PlannerDataStorage::load(filename, pd);
53 }
54 
56 {
57  if (!pd.hasControls())
58  {
59  OMPL_WARN("PlannerData does not have controls. Invoking base::PlannerDataStorage::load");
61  return;
62  }
63 
64  control::PlannerData *pdc = static_cast<control::PlannerData *>(&pd);
65  pdc->clear();
66 
67  const SpaceInformationPtr &si = pdc->getSpaceInformation();
68  if (!in.good())
69  {
70  OMPL_ERROR("Failed to load PlannerData: input stream is invalid");
71  return;
72  }
73  if (!si)
74  {
75  OMPL_ERROR("Failed to load PlannerData: SpaceInformation is invalid");
76  return;
77  }
78  // Loading the planner data:
79  try
80  {
81  boost::archive::binary_iarchive ia(in);
82 
83  // Read the header
84  Header h;
85  ia >> h;
86 
87  // Checking the archive marker
88  if (h.marker != OMPL_PLANNER_DATA_CONTROL_ARCHIVE_MARKER)
89  {
90  OMPL_ERROR("Failed to load PlannerData: PlannerData control archive marker not found");
91  return;
92  }
93 
94  // Verify that the state space is the same
95  std::vector<int> sig;
96  si->getStateSpace()->computeSignature(sig);
97  if (h.signature != sig)
98  {
99  OMPL_ERROR("Failed to load PlannerData: StateSpace signature mismatch");
100  return;
101  }
102 
103  // Verify that the control space is the same
104  sig.clear();
105  si->getControlSpace()->computeSignature(sig);
106  if (h.control_signature != sig)
107  {
108  OMPL_ERROR("Failed to load PlannerData: ControlSpace signature mismatch");
109  return;
110  }
111 
112  // File seems ok... loading vertices and edges
113  loadVertices(pd, h.vertex_count, ia);
114  loadEdges(pd, h.edge_count, ia);
115  }
116  catch (boost::archive::archive_exception &ae)
117  {
118  OMPL_ERROR("Failed to load PlannerData: %s", ae.what());
119  }
120 }
121 
122 void ompl::control::PlannerDataStorage::store(const base::PlannerData &pd, const char *filename)
123 {
124  base::PlannerDataStorage::store(pd, filename);
125 }
126 
128 {
129  const control::PlannerData *pdc = static_cast<const control::PlannerData *>(&pd);
130  if (!pdc)
131  {
132  OMPL_WARN("Failed to cast PlannerData to control::PlannerData. Invoking base::PlannerDataStorage::store");
134  return;
135  }
136 
137  const SpaceInformationPtr &si = pdc->getSpaceInformation();
138  if (!out.good())
139  {
140  OMPL_ERROR("Failed to store PlannerData: output stream is invalid");
141  return;
142  }
143  if (!si)
144  {
145  OMPL_ERROR("Failed to store PlannerData: SpaceInformation is invalid");
146  return;
147  }
148  try
149  {
150  boost::archive::binary_oarchive oa(out);
151 
152  // Writing the header
153  Header h;
154  h.marker = OMPL_PLANNER_DATA_CONTROL_ARCHIVE_MARKER;
155  h.vertex_count = pdc->numVertices();
156  h.edge_count = pdc->numEdges();
157  si->getStateSpace()->computeSignature(h.signature);
158  si->getControlSpace()->computeSignature(h.control_signature);
159  oa << h;
160 
161  storeVertices(pd, oa);
162  storeEdges(pd, oa);
163  }
164  catch (boost::archive::archive_exception &ae)
165  {
166  OMPL_ERROR("Failed to store PlannerData: %s", ae.what());
167  }
168 }
Object containing planner generated vertex and edge data. It is assumed that all vertices are unique...
Definition: PlannerData.h:174
~PlannerDataStorage() override
Destructor.
Object that handles loading/storing a PlannerData object to/from a binary stream. Serialization of ve...
unsigned int numEdges() const
Retrieve the number of edges in this structure.
Object containing planner generated vertex and edge data. It is assumed that all vertices are unique...
Definition: PlannerData.h:122
unsigned int numVertices() const
Retrieve the number of vertices in this structure.
virtual void load(const char *filename, PlannerData &pd)
Load the PlannerData structure from the given stream. The StateSpace that was used to store the data ...
void loadEdges(base::PlannerData &pd, unsigned int numEdges, boost::archive::binary_iarchive &ia) override
Read numEdges from the binary input ia and store them as PlannerData. It is assumed that the edges ca...
#define OMPL_ERROR(fmt,...)
Log a formatted error string.
Definition: Console.h:64
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.
void load(const char *filename, base::PlannerData &pd) override
Load the PlannerData structure from the given filename.
const SpaceInformationPtr & getSpaceInformation() const
Return the instance of SpaceInformation used in this PlannerData.
#define OMPL_WARN(fmt,...)
Log a formatted warning string.
Definition: Console.h:66
A shared pointer wrapper for ompl::control::SpaceInformation.
Information stored at the beginning of the PlannerData archive.
virtual bool hasControls() const
Indicate whether any information about controls (ompl::control::Control) is stored in this instance...
virtual void storeVertices(const PlannerData &pd, boost::archive::binary_oarchive &oa)
Serialize and store all vertices in pd to the binary archive.
std::size_t edge_count
Number of edges stored in the archive.
std::size_t vertex_count
Number of vertices stored in the archive.
void clear() override
Clears the entire data structure.
boost::uint32_t marker
OMPL PlannerData specific marker (fixed value)
*std::vector< int > signature
Signature of state space that allocated the saved states in the vertices (see ompl::base::StateSpace:...
void store(const base::PlannerData &pd, const char *filename) override
Store (serialize) the structure to the given filename. The StateSpace and ControlSpace that was used ...
PlannerDataStorage()
Default constructor.
virtual void store(const PlannerData &pd, const char *filename)
Store (serialize) the PlannerData structure to the given filename.
void storeEdges(const base::PlannerData &pd, boost::archive::binary_oarchive &oa) override
Serialize and store all edges in pd to the binary archive. It is assumed that the edges can be cast t...