ompl/base/StateStorage.h
00001 /********************************************************************* 00002 * Software License Agreement (BSD License) 00003 * 00004 * Copyright (c) 2012, Willow Garage 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 Willow Garage 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: Ioan Sucan */ 00036 00037 #ifndef OMPL_BASE_STATE_STORAGE_ 00038 #define OMPL_BASE_STATE_STORAGE_ 00039 00040 #include "ompl/base/StateSpace.h" 00041 #include <boost/archive/binary_oarchive.hpp> 00042 #include <boost/archive/binary_iarchive.hpp> 00043 #include <boost/serialization/vector.hpp> 00044 #include <boost/function.hpp> 00045 #include <iostream> 00046 00047 namespace ompl 00048 { 00049 namespace base 00050 { 00051 00053 00054 OMPL_CLASS_FORWARD(StateStorage); 00056 00061 class StateStorage 00062 { 00063 public: 00064 00066 StateStorage(const StateSpacePtr &space); 00067 virtual ~StateStorage(); 00068 00070 const StateSpacePtr& getStateSpace() const 00071 { 00072 return space_; 00073 } 00074 00076 void load(const char *filename); 00077 00079 virtual void load(std::istream &in); 00080 00082 void store(const char *filename); 00083 00085 virtual void store(std::ostream &out); 00086 00089 virtual void addState(const State *state); 00090 00092 virtual void generateSamples(unsigned int count); 00093 00095 virtual void clear(); 00096 00098 std::size_t size() const 00099 { 00100 return states_.size(); 00101 } 00102 00104 const std::vector<const State*>& getStates() const 00105 { 00106 return states_; 00107 } 00108 00110 State* getState(unsigned int index) 00111 { 00112 assert(states_.size() > index); 00113 return const_cast<State*>(states_[index]); 00114 } 00115 00117 const State* getState(unsigned int index) const 00118 { 00119 assert(states_.size() > index); 00120 return states_[index]; 00121 } 00122 00124 bool hasMetadata() const 00125 { 00126 return hasMetadata_; 00127 } 00128 00131 void sort(const boost::function<bool(const State*, const State*)> &op); 00132 00135 StateSamplerAllocator getStateSamplerAllocator() const; 00136 00139 StateSamplerAllocator getStateSamplerAllocatorRangeUntil(std::size_t until) const; 00140 00143 StateSamplerAllocator getStateSamplerAllocatorRangeAfter(std::size_t after) const; 00144 00147 virtual StateSamplerAllocator getStateSamplerAllocatorRange(std::size_t from, std::size_t to) const; 00148 00150 virtual void print(std::ostream &out = std::cout) const; 00151 00152 protected: 00153 00155 struct Header 00156 { 00158 boost::uint32_t marker; 00159 00161 std::size_t state_count; 00162 00164 std::vector<int> signature; 00165 00167 template<typename Archive> 00168 void serialize(Archive & ar, const unsigned int /*version*/) 00169 { 00170 ar & marker; 00171 ar & state_count; 00172 ar & signature; 00173 } 00174 }; 00175 00177 virtual void loadStates(const Header &h, boost::archive::binary_iarchive &ia); 00178 00183 virtual void loadMetadata(const Header &h, boost::archive::binary_iarchive &ia); 00184 00186 virtual void storeStates(const Header &h, boost::archive::binary_oarchive &oa); 00187 00192 virtual void storeMetadata(const Header &h, boost::archive::binary_oarchive &oa); 00193 00195 void freeMemory(); 00196 00198 StateSpacePtr space_; 00199 00201 std::vector<const State*> states_; 00202 00204 bool hasMetadata_; 00205 }; 00206 00209 template<typename M> 00210 class StateStorageWithMetadata : public StateStorage 00211 { 00212 public: 00213 00215 typedef M MetadataType; 00216 00218 StateStorageWithMetadata(const StateSpacePtr &space) : StateStorage(space) 00219 { 00220 hasMetadata_ = true; 00221 } 00222 00226 virtual void addState(const State *state) 00227 { 00228 addState(state, M()); 00229 } 00230 00233 virtual void addState(const State *state, const M& metadata) 00234 { 00235 StateStorage::addState(state); 00236 metadata_.push_back(metadata); 00237 } 00238 00239 virtual void clear() 00240 { 00241 StateStorage::clear(); 00242 metadata_.clear(); 00243 } 00244 00246 const M& getMetadata(unsigned int index) const 00247 { 00248 assert(metadata_.size() > index); 00249 return metadata_[index]; 00250 } 00251 00253 M& getMetadata(unsigned int index) 00254 { 00255 assert(metadata_.size() > index); 00256 return metadata_[index]; 00257 } 00258 00259 protected: 00260 00261 virtual void loadMetadata(const Header& /*h*/, boost::archive::binary_iarchive &ia) 00262 { 00263 ia >> metadata_; 00264 } 00265 00266 virtual void storeMetadata(const Header& /*h*/, boost::archive::binary_oarchive &oa) 00267 { 00268 oa << metadata_; 00269 } 00270 00272 std::vector<M> metadata_; 00273 00274 }; 00275 00277 typedef StateStorageWithMetadata<std::vector<std::size_t> > GraphStateStorage; 00278 typedef boost::shared_ptr<GraphStateStorage> GraphStateStoragePtr; 00279 } 00280 } 00281 #endif