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/base/PlannerDataStorage.h"
38 #include <boost/archive/archive_exception.hpp>
39 
40 static const std::uint_fast32_t OMPL_PLANNER_DATA_ARCHIVE_MARKER = 0x5044414D; // this spells PDAM
41 
43 
45 
46 void ompl::base::PlannerDataStorage::store(const PlannerData &pd, const char *filename)
47 {
48  std::ofstream out(filename, std::ios::binary);
49  store(pd, out);
50  out.close();
51 }
52 
53 void ompl::base::PlannerDataStorage::store(const PlannerData &pd, std::ostream &out)
54 {
56  if (!out.good())
57  {
58  OMPL_ERROR("Failed to store PlannerData: output stream is invalid");
59  return;
60  }
61  if (!si)
62  {
63  OMPL_ERROR("Failed to store PlannerData: SpaceInformation is invalid");
64  return;
65  }
66  try
67  {
68  boost::archive::binary_oarchive oa(out);
69 
70  // Writing the header
71  Header h;
72  h.marker = OMPL_PLANNER_DATA_ARCHIVE_MARKER;
73  h.vertex_count = pd.numVertices();
74  h.edge_count = pd.numEdges();
75  si->getStateSpace()->computeSignature(h.signature);
76  oa << h;
77 
78  storeVertices(pd, oa);
79  storeEdges(pd, oa);
80  }
81  catch (boost::archive::archive_exception &ae)
82  {
83  OMPL_ERROR("Failed to store PlannerData: %s", ae.what());
84  }
85 }
86 
87 void ompl::base::PlannerDataStorage::load(const char *filename, PlannerData &pd)
88 {
89  std::ifstream in(filename, std::ios::binary);
90  load(in, pd);
91  in.close();
92 }
93 
95 {
96  pd.clear();
97 
99  if (!in.good())
100  {
101  OMPL_ERROR("Failed to load PlannerData: input stream is invalid");
102  return;
103  }
104  if (!si)
105  {
106  OMPL_ERROR("Failed to load PlannerData: SpaceInformation is invalid");
107  return;
108  }
109  // Loading the planner data:
110  try
111  {
112  boost::archive::binary_iarchive ia(in);
113 
114  // Read the header
115  Header h;
116  ia >> h;
117 
118  // Checking the archive marker
119  if (h.marker != OMPL_PLANNER_DATA_ARCHIVE_MARKER)
120  {
121  OMPL_ERROR("Failed to load PlannerData: PlannerData archive marker not found");
122  return;
123  }
124 
125  // Verify that the state space is the same
126  std::vector<int> sig;
127  si->getStateSpace()->computeSignature(sig);
128  if (h.signature != sig)
129  {
130  OMPL_ERROR("Failed to load PlannerData: StateSpace signature mismatch");
131  return;
132  }
133 
134  // File seems ok... loading vertices and edges
135  loadVertices(pd, h.vertex_count, ia);
136  loadEdges(pd, h.edge_count, ia);
137  }
138  catch (boost::archive::archive_exception &ae)
139  {
140  OMPL_ERROR("Failed to load PlannerData: %s", ae.what());
141  }
142 }
A shared pointer wrapper for ompl::base::SpaceInformation.
const SpaceInformationPtr & getSpaceInformation() const
Return the instance of SpaceInformation used in this PlannerData.
virtual ~PlannerDataStorage()
Destructor.
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:238
Information stored at the beginning of the PlannerData archive.
virtual void store(const PlannerData &pd, const char *filename)
Store (serialize) the PlannerData structure to the given filename.
unsigned int numVertices() const
Retrieve the number of vertices in this structure.
PlannerDataStorage()
Default constructor.
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 ...
virtual void clear()
Clears the entire data structure.
Definition: PlannerData.cpp:74
#define OMPL_ERROR(fmt,...)
Log a formatted error string.
Definition: Console.h:64