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