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