StateStorage.h
1 /*********************************************************************
2 * Software License Agreement (BSD License)
3 *
4 * Copyright (c) 2012, Willow Garage
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 *
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
17 * * Neither the name of the Willow Garage nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 * POSSIBILITY OF SUCH DAMAGE.
33 *********************************************************************/
34 
35 /* Author: Ioan Sucan */
36 
37 #ifndef OMPL_BASE_STATE_STORAGE_
38 #define OMPL_BASE_STATE_STORAGE_
39 
40 #include "ompl/base/StateSpace.h"
41 #include <boost/archive/binary_oarchive.hpp>
42 #include <boost/archive/binary_iarchive.hpp>
43 #include <boost/serialization/vector.hpp>
44 #include <boost/function.hpp>
45 #include <iostream>
46 
47 namespace ompl
48 {
49  namespace base
50  {
51 
53 
54  OMPL_CLASS_FORWARD(StateStorage);
56 
62  {
63  public:
64 
66  StateStorage(const StateSpacePtr &space);
67  virtual ~StateStorage();
68 
71  {
72  return space_;
73  }
74 
76  void load(const char *filename);
77 
79  virtual void load(std::istream &in);
80 
82  void store(const char *filename);
83 
85  virtual void store(std::ostream &out);
86 
89  virtual void addState(const State *state);
90 
92  virtual void generateSamples(unsigned int count);
93 
95  virtual void clear();
96 
98  std::size_t size() const
99  {
100  return states_.size();
101  }
102 
104  const std::vector<const State*>& getStates() const
105  {
106  return states_;
107  }
108 
110  State* getState(unsigned int index)
111  {
112  assert(states_.size() > index);
113  return const_cast<State*>(states_[index]);
114  }
115 
117  const State* getState(unsigned int index) const
118  {
119  assert(states_.size() > index);
120  return states_[index];
121  }
122 
124  bool hasMetadata() const
125  {
126  return hasMetadata_;
127  }
128 
131  void sort(const boost::function<bool(const State*, const State*)> &op);
132 
136 
140 
144 
147  virtual StateSamplerAllocator getStateSamplerAllocatorRange(std::size_t from, std::size_t to) const;
148 
150  virtual void print(std::ostream &out = std::cout) const;
151 
152  protected:
153 
155  struct Header
156  {
158  boost::uint32_t marker;
159 
161  std::size_t state_count;
162 
164  std::vector<int> signature;
165 
167  template<typename Archive>
168  void serialize(Archive & ar, const unsigned int /*version*/)
169  {
170  ar & marker;
171  ar & state_count;
172  ar & signature;
173  }
174  };
175 
177  virtual void loadStates(const Header &h, boost::archive::binary_iarchive &ia);
178 
183  virtual void loadMetadata(const Header &h, boost::archive::binary_iarchive &ia);
184 
186  virtual void storeStates(const Header &h, boost::archive::binary_oarchive &oa);
187 
192  virtual void storeMetadata(const Header &h, boost::archive::binary_oarchive &oa);
193 
195  void freeMemory();
196 
199 
201  std::vector<const State*> states_;
202 
205  };
206 
209  template<typename M>
211  {
212  public:
213 
215  typedef M MetadataType;
216 
219  {
220  hasMetadata_ = true;
221  }
222 
226  virtual void addState(const State *state)
227  {
228  addState(state, M());
229  }
230 
233  virtual void addState(const State *state, const M& metadata)
234  {
235  StateStorage::addState(state);
236  metadata_.push_back(metadata);
237  }
238 
239  virtual void clear()
240  {
242  metadata_.clear();
243  }
244 
246  const M& getMetadata(unsigned int index) const
247  {
248  assert(metadata_.size() > index);
249  return metadata_[index];
250  }
251 
253  M& getMetadata(unsigned int index)
254  {
255  assert(metadata_.size() > index);
256  return metadata_[index];
257  }
258 
259  protected:
260 
261  virtual void loadMetadata(const Header& /*h*/, boost::archive::binary_iarchive &ia)
262  {
263  ia >> metadata_;
264  }
265 
266  virtual void storeMetadata(const Header& /*h*/, boost::archive::binary_oarchive &oa)
267  {
268  oa << metadata_;
269  }
270 
272  std::vector<M> metadata_;
273 
274  };
275 
278  typedef boost::shared_ptr<GraphStateStorage> GraphStateStoragePtr;
279  }
280 }
281 #endif
boost::function< StateSamplerPtr(const StateSpace *)> StateSamplerAllocator
Definition of a function that can allocate a state sampler.
Definition: StateSampler.h:184
void freeMemory()
Free the memory allocated for states.
Manage loading and storing for a set of states of a specified state space.
Definition: StateStorage.h:61
virtual void generateSamples(unsigned int count)
Generate count states uniformly at random and store them in this structure.
StateSamplerAllocator getStateSamplerAllocatorRangeAfter(std::size_t after) const
Get a sampler allocator to a sampler that can be specified for a StateSpace, such that all sampled st...
void store(const char *filename)
Save a set of states to a file.
const State * getState(unsigned int index) const
Get a particular state.
Definition: StateStorage.h:117
std::vector< const State * > states_
The list of maintained states.
Definition: StateStorage.h:201
A boost shared pointer wrapper for ompl::base::StateSpace.
StateStorage(const StateSpacePtr &space)
The state space to store states for is specified as argument.
bool hasMetadata() const
Return a flag that indicates whether there is metadata associated to the states in this storage...
Definition: StateStorage.h:124
virtual void loadStates(const Header &h, boost::archive::binary_iarchive &ia)
Load the states from a binary archive ia, given the loaded header is h.
StateSpacePtr space_
State space that corresponds to maintained states.
Definition: StateStorage.h:198
std::size_t state_count
Number of states stored in the archive.
Definition: StateStorage.h:161
M MetadataType
the datatype of the metadata
Definition: StateStorage.h:215
StateSamplerAllocator getStateSamplerAllocatorRangeUntil(std::size_t until) const
Get a sampler allocator to a sampler that can be specified for a StateSpace, such that all sampled st...
virtual void addState(const State *state, const M &metadata)
Add a state to the set of states maintained by this storage structure. The state is copied to interna...
Definition: StateStorage.h:233
StateSamplerAllocator getStateSamplerAllocator() const
Get a sampler allocator to a sampler that can be specified for a StateSpace, such that all sampled st...
virtual void addState(const State *state)
Add a state to the set of states maintained by this storage structure. The state is copied to interna...
Definition: StateStorage.h:226
M & getMetadata(unsigned int index)
Get write access to the metadata of a state at a particular index.
Definition: StateStorage.h:253
const std::vector< const State * > & getStates() const
Get the stored states.
Definition: StateStorage.h:104
Main namespace. Contains everything in this library.
Definition: Cost.h:42
virtual void loadMetadata(const Header &h, boost::archive::binary_iarchive &ia)
Load the state metadata from a binary archive ia, given the loaded header is h. No metadata is actual...
virtual void loadMetadata(const Header &, boost::archive::binary_iarchive &ia)
Load the state metadata from a binary archive ia, given the loaded header is h. No metadata is actual...
Definition: StateStorage.h:261
virtual void storeMetadata(const Header &, boost::archive::binary_oarchive &oa)
Save the state metadata to a binary archive oa, given the stored header is h. No metadata is actually...
Definition: StateStorage.h:266
StateStorageWithMetadata< std::vector< std::size_t > > GraphStateStorage
Storage of states where the metadata is a vector of indices. This is is typically used to store a gra...
Definition: StateStorage.h:277
virtual void clear()
Clear the stored states. This frees all the memory.
const M & getMetadata(unsigned int index) const
Get const access to the metadata of a state at a particular index.
Definition: StateStorage.h:246
const StateSpacePtr & getStateSpace() const
Get the state space this class maintains states for.
Definition: StateStorage.h:70
std::vector< M > metadata_
The metadata for each state.
Definition: StateStorage.h:272
virtual void addState(const State *state)
Add a state to the set of states maintained by this storage structure. The state is copied to interna...
Definition of an abstract state.
Definition: State.h:50
std::vector< int > signature
Signature of state space that allocated the saved states (see ompl::base::StateSpace::computeSignatur...
Definition: StateStorage.h:164
State storage that allows storing state metadata as well.
Definition: StateStorage.h:210
Information stored at the beginning of the archive.
Definition: StateStorage.h:155
virtual void storeStates(const Header &h, boost::archive::binary_oarchive &oa)
Store the states to a binary archive oa, given the stored header is h.
void load(const char *filename)
Load a set of states from a specified file.
boost::uint32_t marker
OMPL specific marker (fixed value)
Definition: StateStorage.h:158
bool hasMetadata_
Flag indicating whether there is metadata associated to the states in this storage.
Definition: StateStorage.h:204
virtual void storeMetadata(const Header &h, boost::archive::binary_oarchive &oa)
Save the state metadata to a binary archive oa, given the stored header is h. No metadata is actually...
void sort(const boost::function< bool(const State *, const State *)> &op)
Sort the states according to the less-equal operator op. Metadata is NOT sorted; if metadata was adde...
virtual void clear()
Clear the stored states. This frees all the memory.
Definition: StateStorage.h:239
State * getState(unsigned int index)
Get a particular state for non-const access.
Definition: StateStorage.h:110
void serialize(Archive &ar, const unsigned int)
boost::serialization routine
Definition: StateStorage.h:168
StateStorageWithMetadata(const StateSpacePtr &space)
The state space to store states for is specified as argument.
Definition: StateStorage.h:218
virtual StateSamplerAllocator getStateSamplerAllocatorRange(std::size_t from, std::size_t to) const
Get a sampler allocator to a sampler that can be specified for a StateSpace, such that all sampled st...
virtual void print(std::ostream &out=std::cout) const
Output the set of states to a specified stream, in a human readable fashion.
std::size_t size() const
Return the number of stored states.
Definition: StateStorage.h:98