All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends
src/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         ClassForward(StateStorage);
00056 
00058         class StateStorage
00059         {
00060         public:
00061 
00063             StateStorage(const StateSpacePtr &space);
00064             virtual ~StateStorage(void);
00065 
00067             const StateSpacePtr& getStateSpace(void) const
00068             {
00069                 return space_;
00070             }
00071 
00073             void load(const char *filename);
00074 
00076             virtual void load(std::istream &in);
00077 
00079             void store(const char *filename);
00080 
00082             virtual void store(std::ostream &out);
00083 
00086             virtual void addState(const State *state);
00087 
00089             virtual void generateSamples(unsigned int count);
00090 
00092             virtual void clear(void);
00093 
00095             std::size_t size(void) const
00096             {
00097                 return states_.size();
00098             }
00099 
00101             const std::vector<const State*>& getStates(void) const
00102             {
00103                 return states_;
00104             }
00105 
00107             State* getState(unsigned int index)
00108             {
00109                 assert(states_.size() > index);
00110                 return const_cast<State*>(states_[index]);
00111             }
00112 
00114             const State* getState(unsigned int index) const
00115             {
00116                 assert(states_.size() > index);
00117                 return states_[index];
00118             }
00119 
00122             void sort(const boost::function<bool(const State*, const State*)> &op);
00123 
00126             StateSamplerAllocator getStateSamplerAllocator(void) const;
00127 
00130             StateSamplerAllocator getStateSamplerAllocatorRangeUntil(std::size_t until) const;
00131 
00134             StateSamplerAllocator getStateSamplerAllocatorRangeAfter(std::size_t after) const;
00135 
00138             virtual StateSamplerAllocator getStateSamplerAllocatorRange(std::size_t from, std::size_t to) const;
00139 
00141             virtual void print(std::ostream &out = std::cout) const;
00142 
00143         protected:
00144 
00146             struct Header
00147             {
00149                 boost::uint32_t  marker;
00150 
00152                 std::size_t      state_count;
00153 
00155                 std::vector<int> signature;
00156 
00158                 template<typename Archive>
00159                 void serialize(Archive & ar, const unsigned int version)
00160                 {
00161                     ar & marker;
00162                     ar & state_count;
00163                     ar & signature;
00164                 }
00165             };
00166 
00168             virtual void loadStates(const Header &h, boost::archive::binary_iarchive &ia);
00169 
00174             virtual void loadMetadata(const Header &h, boost::archive::binary_iarchive &ia);
00175 
00177             virtual void storeStates(const Header &h, boost::archive::binary_oarchive &oa);
00178 
00183             virtual void storeMetadata(const Header &h, boost::archive::binary_oarchive &oa);
00184 
00186             void freeMemory(void);
00187 
00189             StateSpacePtr             space_;
00190 
00192             std::vector<const State*> states_;
00193         };
00194 
00197         template<typename M>
00198         class StateStorageWithMetadata : public StateStorage
00199         {
00200         public:
00201 
00203             StateStorageWithMetadata(const StateSpacePtr &space) : StateStorage(space)
00204             {
00205             }
00206 
00210             virtual void addState(const State *state)
00211             {
00212                 addState(state, M());
00213             }
00214 
00217             virtual void addState(const State *state, const M& metadata)
00218             {
00219                 StateStorage::addState(state);
00220                 metadata_.push_back(metadata);
00221             }
00222 
00223             virtual void clear(void)
00224             {
00225                 StateStorage::clear();
00226                 metadata_.clear();
00227             }
00228 
00230             const M& getMetadata(unsigned int index) const
00231             {
00232                 assert(metadata_.size() > index);
00233                 return metadata_[index];
00234             }
00235 
00237             M& getMetadata(unsigned int index)
00238             {
00239                 assert(metadata_.size() > index);
00240                 return metadata_[index];
00241             }
00242 
00243         protected:
00244 
00245             virtual void loadMetadata(const Header &h, boost::archive::binary_iarchive &ia)
00246             {
00247                 ia >> metadata_;
00248             }
00249 
00250             virtual void storeMetadata(const Header &h, boost::archive::binary_oarchive &oa)
00251             {
00252                 oa << metadata_;
00253             }
00254 
00256             std::vector<M> metadata_;
00257 
00258         };
00259 
00260     }
00261 }
00262 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines