All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends
src/ompl/base/src/PlannerDataStorage.cpp
00001 /*********************************************************************
00002 * Software License Agreement (BSD License)
00003 *
00004 *  Copyright (c) 2012, Rice University
00005 *  All rights reserved.
00006 *
00007 *  Redistribution and use in source and binary forms, with or without
00008 *  modification, are permitted provided that the following conditions
00009 *  are met:
00010 *
00011 *   * Redistributions of source code must retain the above copyright
00012 *     notice, this list of conditions and the following disclaimer.
00013 *   * Redistributions in binary form must reproduce the above
00014 *     copyright notice, this list of conditions and the following
00015 *     disclaimer in the documentation and/or other materials provided
00016 *     with the distribution.
00017 *   * Neither the name of the Rice University nor the names of its
00018 *     contributors may be used to endorse or promote products derived
00019 *     from this software without specific prior written permission.
00020 *
00021 *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00022 *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00023 *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00024 *  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00025 *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00026 *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00027 *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00028 *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00029 *  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00030 *  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00031 *  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00032 *  POSSIBILITY OF SUCH DAMAGE.
00033 *********************************************************************/
00034 
00035 /* Author: Ryan Luna */
00036 
00037 // PlannerDataStorage requires Boost version >= 1.44
00038 #include <boost/version.hpp>
00039 #if BOOST_VERSION < 104400
00040 #warning Boost version >= 1.44 is required for PlannerDataStorage classes
00041 #else
00042 
00043 #include "ompl/base/PlannerDataStorage.h"
00044 #include <boost/archive/archive_exception.hpp>
00045 
00046 static const boost::uint32_t OMPL_PLANNER_DATA_ARCHIVE_MARKER = 0x5044414D; // this spells PDAM
00047 
00048 ompl::base::PlannerDataStorage::PlannerDataStorage(void)
00049 {
00050 }
00051 
00052 ompl::base::PlannerDataStorage::~PlannerDataStorage(void)
00053 {
00054 }
00055 
00056 void ompl::base::PlannerDataStorage::store(const PlannerData& pd, const char *filename)
00057 {
00058     std::ofstream out(filename, std::ios::binary);
00059     store(pd, out);
00060     out.close();
00061 }
00062 
00063 void ompl::base::PlannerDataStorage::store(const PlannerData& pd, std::ostream &out)
00064 {
00065     const SpaceInformationPtr &si = pd.getSpaceInformation();
00066     if (!out.good())
00067     {
00068         logError("Failed to store PlannerData: output stream is invalid");
00069         return;
00070     }
00071     if (!si)
00072     {
00073         logError("Failed to store PlannerData: SpaceInformation is invalid");
00074         return;
00075     }
00076     try
00077     {
00078         boost::archive::binary_oarchive oa(out);
00079 
00080         // Writing the header
00081         Header h;
00082         h.marker = OMPL_PLANNER_DATA_ARCHIVE_MARKER;
00083         h.vertex_count = pd.numVertices();
00084         h.edge_count = pd.numEdges();
00085         si->getStateSpace()->computeSignature(h.signature);
00086         oa << h;
00087 
00088         storeVertices(pd, oa);
00089         storeEdges(pd, oa);
00090     }
00091     catch (boost::archive::archive_exception &ae)
00092     {
00093         logError("Failed to store PlannerData: %s", ae.what());
00094     }
00095 }
00096 
00097 void ompl::base::PlannerDataStorage::load(const char *filename, PlannerData& pd)
00098 {
00099     std::ifstream in(filename, std::ios::binary);
00100     load(in, pd);
00101     in.close();
00102 }
00103 
00104 void ompl::base::PlannerDataStorage::load(std::istream &in, PlannerData& pd)
00105 {
00106     pd.clear();
00107 
00108     const SpaceInformationPtr &si = pd.getSpaceInformation();
00109     if (!in.good())
00110     {
00111         logError("Failed to load PlannerData: input stream is invalid");
00112         return;
00113     }
00114     if (!si)
00115     {
00116         logError("Failed to load PlannerData: SpaceInformation is invalid");
00117         return;
00118     }
00119     // Loading the planner data:
00120     try
00121     {
00122         boost::archive::binary_iarchive ia(in);
00123 
00124         // Read the header
00125         Header h;
00126         ia >> h;
00127 
00128         // Checking the archive marker
00129         if (h.marker != OMPL_PLANNER_DATA_ARCHIVE_MARKER)
00130         {
00131             logError("Failed to load PlannerData: PlannerData archive marker not found");
00132             return;
00133         }
00134 
00135         // Verify that the state space is the same
00136         std::vector<int> sig;
00137         si->getStateSpace()->computeSignature(sig);
00138         if (h.signature != sig)
00139         {
00140             logError("Failed to load PlannerData: StateSpace signature mismatch");
00141             return;
00142         }
00143 
00144         // File seems ok... loading vertices and edges
00145         loadVertices(pd, h.vertex_count, ia);
00146         loadEdges(pd, h.edge_count, ia);
00147     }
00148     catch (boost::archive::archive_exception &ae)
00149     {
00150         logError("Failed to load PlannerData: %s", ae.what());
00151     }
00152 }
00153 
00154 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines