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