ompl/control/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 #include "ompl/control/PlannerDataStorage.h"
00038 #include <boost/archive/archive_exception.hpp>
00039 
00041 static const boost::uint32_t OMPL_PLANNER_DATA_CONTROL_ARCHIVE_MARKER = 0x5044434D; // this spells PDCM
00043 
00044 ompl::control::PlannerDataStorage::PlannerDataStorage() : base::PlannerDataStorage()
00045 {
00046 }
00047 
00048 ompl::control::PlannerDataStorage::~PlannerDataStorage()
00049 {
00050 }
00051 
00052 void ompl::control::PlannerDataStorage::load(const char *filename, base::PlannerData &pd)
00053 {
00054     base::PlannerDataStorage::load(filename, pd);
00055 }
00056 
00057 void ompl::control::PlannerDataStorage::load(std::istream &in, base::PlannerData &pd)
00058 {
00059     if (!pd.hasControls())
00060     {
00061         OMPL_WARN("PlannerData does not have controls.  Invoking base::PlannerDataStorage::load");
00062         base::PlannerDataStorage::load(in, pd);
00063         return;
00064     }
00065 
00066     control::PlannerData *pdc = static_cast<control::PlannerData*>(&pd);
00067     pdc->clear();
00068 
00069     const SpaceInformationPtr &si = pdc->getSpaceInformation();
00070     if (!in.good())
00071     {
00072         OMPL_ERROR("Failed to load PlannerData: input stream is invalid");
00073         return;
00074     }
00075     if (!si)
00076     {
00077         OMPL_ERROR("Failed to load PlannerData: SpaceInformation is invalid");
00078         return;
00079     }
00080     // Loading the planner data:
00081     try
00082     {
00083         boost::archive::binary_iarchive ia(in);
00084 
00085         // Read the header
00086         Header h;
00087         ia >> h;
00088 
00089         // Checking the archive marker
00090         if (h.marker != OMPL_PLANNER_DATA_CONTROL_ARCHIVE_MARKER)
00091         {
00092             OMPL_ERROR("Failed to load PlannerData: PlannerData control archive marker not found");
00093             return;
00094         }
00095 
00096         // Verify that the state space is the same
00097         std::vector<int> sig;
00098         si->getStateSpace()->computeSignature(sig);
00099         if (h.signature != sig)
00100         {
00101             OMPL_ERROR("Failed to load PlannerData: StateSpace signature mismatch");
00102             return;
00103         }
00104 
00105         // Verify that the control space is the same
00106         sig.clear();
00107         si->getControlSpace()->computeSignature(sig);
00108         if (h.control_signature != sig)
00109         {
00110             OMPL_ERROR("Failed to load PlannerData: ControlSpace signature mismatch");
00111             return;
00112         }
00113 
00114         // File seems ok... loading vertices and edges
00115         loadVertices(pd, h.vertex_count, ia);
00116         loadEdges(pd, h.edge_count, ia);
00117     }
00118     catch (boost::archive::archive_exception &ae)
00119     {
00120         OMPL_ERROR("Failed to load PlannerData: %s", ae.what());
00121     }
00122 }
00123 
00124 void ompl::control::PlannerDataStorage::store(const base::PlannerData &pd, const char *filename)
00125 {
00126     base::PlannerDataStorage::store(pd, filename);
00127 }
00128 
00129 void ompl::control::PlannerDataStorage::store(const base::PlannerData &pd, std::ostream &out)
00130 {
00131     const control::PlannerData *pdc = static_cast<const control::PlannerData*>(&pd);
00132     if (!pdc)
00133     {
00134         OMPL_WARN("Failed to cast PlannerData to control::PlannerData.  Invoking base::PlannerDataStorage::store");
00135         base::PlannerDataStorage::store(pd, out);
00136         return;
00137     }
00138 
00139     const SpaceInformationPtr &si = pdc->getSpaceInformation();
00140     if (!out.good())
00141     {
00142         OMPL_ERROR("Failed to store PlannerData: output stream is invalid");
00143         return;
00144     }
00145     if (!si)
00146     {
00147         OMPL_ERROR("Failed to store PlannerData: SpaceInformation is invalid");
00148         return;
00149     }
00150     try
00151     {
00152         boost::archive::binary_oarchive oa(out);
00153 
00154         // Writing the header
00155         Header h;
00156         h.marker = OMPL_PLANNER_DATA_CONTROL_ARCHIVE_MARKER;
00157         h.vertex_count = pdc->numVertices();
00158         h.edge_count = pdc->numEdges();
00159         si->getStateSpace()->computeSignature(h.signature);
00160         si->getControlSpace()->computeSignature(h.control_signature);
00161         oa << h;
00162 
00163         storeVertices(pd, oa);
00164         storeEdges(pd, oa);
00165     }
00166     catch (boost::archive::archive_exception &ae)
00167     {
00168         OMPL_ERROR("Failed to store PlannerData: %s", ae.what());
00169     }
00170 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines