Program Listing for File StateStorage.h

Return to documentation for file (src/ompl/base/StateStorage.h)

/*********************************************************************
* Software License Agreement (BSD License)
*
*  Copyright (c) 2012, Willow Garage
*  All rights reserved.
*
*  Redistribution and use in source and binary forms, with or without
*  modification, are permitted provided that the following conditions
*  are met:
*
*   * Redistributions of source code must retain the above copyright
*     notice, this list of conditions and the following disclaimer.
*   * Redistributions in binary form must reproduce the above
*     copyright notice, this list of conditions and the following
*     disclaimer in the documentation and/or other materials provided
*     with the distribution.
*   * Neither the name of the Willow Garage nor the names of its
*     contributors may be used to endorse or promote products derived
*     from this software without specific prior written permission.
*
*  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
*  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
*  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
*  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
*  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
*  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
*  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
*  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
*  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
*  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
*  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
*  POSSIBILITY OF SUCH DAMAGE.
*********************************************************************/

/* Author: Ioan Sucan */

#ifndef OMPL_BASE_STATE_STORAGE_
#define OMPL_BASE_STATE_STORAGE_

#include "ompl/base/StateSpace.h"
#include <boost/archive/binary_oarchive.hpp>
#include <boost/archive/binary_iarchive.hpp>
#include <boost/serialization/vector.hpp>
#include <functional>
#include <iostream>

namespace ompl
{
    namespace base
    {

        OMPL_CLASS_FORWARD(StateStorage);

        class StateStorage
        {
        public:
            StateStorage(StateSpacePtr space);
            virtual ~StateStorage();

            const StateSpacePtr &getStateSpace() const
            {
                return space_;
            }

            void load(const char *filename);

            virtual void load(std::istream &in);

            void store(const char *filename);

            virtual void store(std::ostream &out);

            virtual void addState(const State *state);

            virtual void generateSamples(unsigned int count);

            virtual void clear();

            std::size_t size() const
            {
                return states_.size();
            }

            const std::vector<const State *> &getStates() const
            {
                return states_;
            }

            State *getState(unsigned int index)
            {
                assert(states_.size() > index);
                return const_cast<State *>(states_[index]);
            }

            const State *getState(unsigned int index) const
            {
                assert(states_.size() > index);
                return states_[index];
            }

            bool hasMetadata() const
            {
                return hasMetadata_;
            }

            void sort(const std::function<bool(const State *, const State *)> &op);

            StateSamplerAllocator getStateSamplerAllocator() const;

            StateSamplerAllocator getStateSamplerAllocatorRangeUntil(std::size_t until) const;

            StateSamplerAllocator getStateSamplerAllocatorRangeAfter(std::size_t after) const;

            virtual StateSamplerAllocator getStateSamplerAllocatorRange(std::size_t from, std::size_t to) const;

            virtual void print(std::ostream &out = std::cout) const;

        protected:
            struct Header
            {
                std::uint_fast32_t marker;

                std::size_t state_count;

                std::vector<int> signature;

                template <typename Archive>
                void serialize(Archive &ar, const unsigned int /*version*/)
                {
                    ar &marker;
                    ar &state_count;
                    ar &signature;
                }
            };

            virtual void loadStates(const Header &h, boost::archive::binary_iarchive &ia);

            virtual void loadMetadata(const Header &h, boost::archive::binary_iarchive &ia);

            virtual void storeStates(const Header &h, boost::archive::binary_oarchive &oa);

            virtual void storeMetadata(const Header &h, boost::archive::binary_oarchive &oa);

            void freeMemory();

            StateSpacePtr space_;

            std::vector<const State *> states_;

            bool hasMetadata_;
        };

        template <typename M>
        class StateStorageWithMetadata : public StateStorage
        {
        public:
            using MetadataType = M;

            StateStorageWithMetadata(const StateSpacePtr &space) : StateStorage(space)
            {
                hasMetadata_ = true;
            }

            void addState(const State *state) override
            {
                addState(state, M());
            }

            virtual void addState(const State *state, const M &metadata)
            {
                StateStorage::addState(state);
                metadata_.push_back(metadata);
            }

            void clear() override
            {
                StateStorage::clear();
                metadata_.clear();
            }

            const M &getMetadata(unsigned int index) const
            {
                assert(metadata_.size() > index);
                return metadata_[index];
            }

            M &getMetadata(unsigned int index)
            {
                assert(metadata_.size() > index);
                return metadata_[index];
            }

        protected:
            void loadMetadata(const Header & /*h*/, boost::archive::binary_iarchive &ia) override
            {
                // clear default metadata that was added by StateStorage::loadStates()
                metadata_.clear();
                ia >> metadata_;
            }

            void storeMetadata(const Header & /*h*/, boost::archive::binary_oarchive &oa) override
            {
                oa << metadata_;
            }

            std::vector<M> metadata_;
        };

        using GraphStateStorage = StateStorageWithMetadata<std::vector<std::size_t>>;
        using GraphStateStoragePtr = std::shared_ptr<GraphStateStorage>;
    }
}
#endif