ControlSpace.h
1 /*********************************************************************
2 * Software License Agreement (BSD License)
3 *
4 * Copyright (c) 2010, Rice University
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 Rice University 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_CONTROL_CONTROL_SPACE_
38 #define OMPL_CONTROL_CONTROL_SPACE_
39 
40 #include "ompl/base/StateSpace.h"
41 #include "ompl/control/Control.h"
42 #include "ompl/control/ControlSampler.h"
43 #include "ompl/control/ControlSpaceTypes.h"
44 #include "ompl/util/Console.h"
45 #include "ompl/util/ClassForward.h"
46 #include <boost/concept_check.hpp>
47 #include <boost/noncopyable.hpp>
48 #include <iostream>
49 #include <vector>
50 
51 namespace ompl
52 {
53 
54  namespace control
55  {
56 
58 
59  OMPL_CLASS_FORWARD(ControlSpace);
61 
66  class ControlSpace : private boost::noncopyable
67  {
68  public:
69 
71  ControlSpace(const base::StateSpacePtr &stateSpace);
72 
73  virtual ~ControlSpace();
74 
76  template<class T>
77  T* as()
78  {
80  BOOST_CONCEPT_ASSERT((boost::Convertible<T*, ControlSpace*>));
81 
82  return static_cast<T*>(this);
83  }
84 
86  template<class T>
87  const T* as() const
88  {
90  BOOST_CONCEPT_ASSERT((boost::Convertible<T*, ControlSpace*>));
91 
92  return static_cast<const T*>(this);
93  }
94 
96  const std::string& getName() const;
97 
99  void setName(const std::string &name);
100 
104  int getType() const
105  {
106  return type_;
107  }
108 
111  {
112  return stateSpace_;
113  }
114 
116  virtual unsigned int getDimension() const = 0;
117 
119  virtual Control* allocControl() const = 0;
120 
122  virtual void freeControl(Control *control) const = 0;
123 
125  virtual void copyControl(Control *destination, const Control *source) const = 0;
126 
128  virtual bool equalControls(const Control *control1, const Control *control2) const = 0;
129 
131  virtual void nullControl(Control *control) const = 0;
132 
134  virtual ControlSamplerPtr allocDefaultControlSampler() const = 0;
135 
139  virtual ControlSamplerPtr allocControlSampler() const;
140 
143 
146 
151  virtual double* getValueAddressAtIndex(Control *control, const unsigned int index) const;
152 
154  virtual void printControl(const Control *control, std::ostream &out) const;
155 
157  virtual void printSettings(std::ostream &out) const;
158 
160  virtual void setup();
161 
163  virtual unsigned int getSerializationLength() const;
164 
166  virtual void serialize(void *serialization, const Control *ctrl) const;
167 
169  virtual void deserialize(Control *ctrl, const void *serialization) const;
170 
173  void computeSignature(std::vector<int> &signature) const;
174 
176  virtual bool isCompound() const;
177 
178  protected:
179 
181  int type_;
182 
185 
188 
189  private:
190 
192  std::string name_;
193  };
194 
197  {
198  public:
199 
202 
204  CompoundControlSpace(const base::StateSpacePtr &stateSpace) : ControlSpace(stateSpace), componentCount_(0), locked_(false)
205  {
206  }
207 
208  virtual ~CompoundControlSpace()
209  {
210  }
211 
213  template<class T>
214  T* as(const unsigned int index) const
215  {
217  BOOST_CONCEPT_ASSERT((boost::Convertible<T*, ControlSpace*>));
218 
219  return static_cast<T*>(getSubspace(index).get());
220  }
221 
223  virtual void addSubspace(const ControlSpacePtr &component);
224 
226  unsigned int getSubspaceCount() const;
227 
229  const ControlSpacePtr& getSubspace(const unsigned int index) const;
230 
232  const ControlSpacePtr& getSubspace(const std::string &name) const;
233 
234  virtual unsigned int getDimension() const;
235 
236  virtual Control* allocControl() const;
237 
238  virtual void freeControl(Control *control) const;
239 
240  virtual void copyControl(Control *destination, const Control *source) const;
241 
242  virtual bool equalControls(const Control *control1, const Control *control2) const;
243 
244  virtual void nullControl(Control *control) const;
245 
247 
248  virtual double* getValueAddressAtIndex(Control *control, const unsigned int index) const;
249 
250  virtual void printControl(const Control *control, std::ostream &out = std::cout) const;
251 
252  virtual void printSettings(std::ostream &out) const;
253 
254  virtual void setup();
255 
257  virtual unsigned int getSerializationLength() const;
258 
260  virtual void serialize(void *serialization, const Control *ctrl) const;
261 
263  virtual void deserialize(Control *ctrl, const void *serialization) const;
264 
265  virtual bool isCompound() const;
266 
272  void lock();
273 
274  protected:
275 
277  std::vector<ControlSpacePtr> components_;
278 
280  unsigned int componentCount_;
281 
283  bool locked_;
284  };
285  }
286 }
287 
288 #endif
virtual Control * allocControl() const =0
Allocate memory for a control.
virtual void serialize(void *serialization, const Control *ctrl) const
Serializes the given control into the serialization buffer.
const base::StateSpacePtr & getStateSpace() const
Return the state space this control space depends on.
Definition: ControlSpace.h:110
base::StateSpacePtr stateSpace_
The state space controls can be applied to.
Definition: ControlSpace.h:184
const std::string & getName() const
Get the name of the control space.
bool locked_
Flag indicating whether adding further components is allowed or not.
Definition: ControlSpace.h:283
Definition of an abstract control.
Definition: Control.h:48
A boost shared pointer wrapper for ompl::base::StateSpace.
A boost shared pointer wrapper for ompl::control::ControlSampler.
virtual void deserialize(Control *ctrl, const void *serialization) const
Deserializes a control from the serialization buffer.
CompoundControl ControlType
Define the type of control allocated by this control space.
Definition: ControlSpace.h:201
void setName(const std::string &name)
Set the name of the control space.
void clearControlSamplerAllocator()
Clear the control sampler allocator (reset to default)
virtual ControlSamplerPtr allocDefaultControlSampler() const =0
Allocate the default control sampler.
CompoundControlSpace(const base::StateSpacePtr &stateSpace)
Constructor. The corresponding state space needs to be specified.
Definition: ControlSpace.h:204
virtual void copyControl(Control *destination, const Control *source) const =0
Copy a control to another.
T * as(const unsigned int index) const
Cast a component of this instance to a desired type.
Definition: ControlSpace.h:214
virtual void printSettings(std::ostream &out) const
Print the settings for this control space to a stream.
virtual double * getValueAddressAtIndex(Control *control, const unsigned int index) const
Many controls contain a number of double values. This function provides a means to get the memory add...
std::vector< ControlSpacePtr > components_
The component control spaces that make up the compound control space.
Definition: ControlSpace.h:277
A boost shared pointer wrapper for ompl::control::ControlSpace.
virtual void freeControl(Control *control) const =0
Free the memory of a control.
T * as()
Cast this instance to a desired type.
Definition: ControlSpace.h:77
Main namespace. Contains everything in this library.
Definition: Cost.h:42
int getType() const
Get the type of the control space. The type can be used to verify whether two space instances are of ...
Definition: ControlSpace.h:104
virtual void setup()
Perform final setup steps. This function is automatically called by the SpaceInformation.
Definition of a compound control.
Definition: Control.h:93
virtual bool equalControls(const Control *control1, const Control *control2) const =0
Check if two controls are the same.
int type_
A type assigned for this control space.
Definition: ControlSpace.h:181
virtual void nullControl(Control *control) const =0
Make the control have no effect if it were to be applied to a state for any amount of time...
ControlSpace(const base::StateSpacePtr &stateSpace)
Construct a control space, given the state space.
A control space representing the space of applicable controls.
Definition: ControlSpace.h:66
virtual unsigned int getSerializationLength() const
Returns the serialization size for a single control in this space.
const T * as() const
Cast this instance to a desired type.
Definition: ControlSpace.h:87
unsigned int componentCount_
The number of contained components.
Definition: ControlSpace.h:280
virtual void printControl(const Control *control, std::ostream &out) const
Print a control to a stream.
void computeSignature(std::vector< int > &signature) const
Compute an array of ints that uniquely identifies the structure of the control space. The first element of the signature is the number of integers that follow.
void setControlSamplerAllocator(const ControlSamplerAllocator &csa)
Set the sampler allocator to use.
ControlSamplerAllocator csa_
An optional control sampler allocator.
Definition: ControlSpace.h:187
A control space to allow the composition of control spaces.
Definition: ControlSpace.h:196
virtual bool isCompound() const
Check if the control space is compound.
virtual ControlSamplerPtr allocControlSampler() const
Allocate an instance of the control sampler for this space. This sampler will be allocated with the s...
virtual unsigned int getDimension() const =0
Get the dimension of this control space.
boost::function< ControlSamplerPtr(const ControlSpace *)> ControlSamplerAllocator
Definition of a function that can allocate a control sampler.