All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends
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 
59  {
60  public:
61 
63  StateStorage(const StateSpacePtr &space);
64  virtual ~StateStorage(void);
65 
67  const StateSpacePtr& getStateSpace(void) const
68  {
69  return space_;
70  }
71 
73  void load(const char *filename);
74 
76  virtual void load(std::istream &in);
77 
79  void store(const char *filename);
80 
82  virtual void store(std::ostream &out);
83 
86  virtual void addState(const State *state);
87 
89  virtual void generateSamples(unsigned int count);
90 
92  virtual void clear(void);
93 
95  std::size_t size(void) const
96  {
97  return states_.size();
98  }
99 
101  const std::vector<const State*>& getStates(void) const
102  {
103  return states_;
104  }
105 
107  State* getState(unsigned int index)
108  {
109  assert(states_.size() > index);
110  return const_cast<State*>(states_[index]);
111  }
112 
114  const State* getState(unsigned int index) const
115  {
116  assert(states_.size() > index);
117  return states_[index];
118  }
119 
122  void sort(const boost::function<bool(const State*, const State*)> &op);
123 
127 
131 
135 
138  virtual StateSamplerAllocator getStateSamplerAllocatorRange(std::size_t from, std::size_t to) const;
139 
141  virtual void print(std::ostream &out = std::cout) const;
142 
143  protected:
144 
146  struct Header
147  {
149  boost::uint32_t marker;
150 
152  std::size_t state_count;
153 
155  std::vector<int> signature;
156 
158  template<typename Archive>
159  void serialize(Archive & ar, const unsigned int version)
160  {
161  ar & marker;
162  ar & state_count;
163  ar & signature;
164  }
165  };
166 
168  virtual void loadStates(const Header &h, boost::archive::binary_iarchive &ia);
169 
174  virtual void loadMetadata(const Header &h, boost::archive::binary_iarchive &ia);
175 
177  virtual void storeStates(const Header &h, boost::archive::binary_oarchive &oa);
178 
183  virtual void storeMetadata(const Header &h, boost::archive::binary_oarchive &oa);
184 
186  void freeMemory(void);
187 
190 
192  std::vector<const State*> states_;
193  };
194 
197  template<typename M>
199  {
200  public:
201 
204  {
205  }
206 
210  virtual void addState(const State *state)
211  {
212  addState(state, M());
213  }
214 
217  virtual void addState(const State *state, const M& metadata)
218  {
219  StateStorage::addState(state);
220  metadata_.push_back(metadata);
221  }
222 
223  virtual void clear(void)
224  {
226  metadata_.clear();
227  }
228 
230  const M& getMetadata(unsigned int index) const
231  {
232  assert(metadata_.size() > index);
233  return metadata_[index];
234  }
235 
237  M& getMetadata(unsigned int index)
238  {
239  assert(metadata_.size() > index);
240  return metadata_[index];
241  }
242 
243  protected:
244 
245  virtual void loadMetadata(const Header &h, boost::archive::binary_iarchive &ia)
246  {
247  ia >> metadata_;
248  }
249 
250  virtual void storeMetadata(const Header &h, boost::archive::binary_oarchive &oa)
251  {
252  oa << metadata_;
253  }
254 
256  std::vector<M> metadata_;
257 
258  };
259 
260  }
261 }
262 #endif