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 <iostream>
48 #include <vector>
49 
50 namespace ompl
51 {
52 
53  namespace control
54  {
55 
57 
58  OMPL_CLASS_FORWARD(ControlSpace);
60 
66  {
67  public:
68  // non-copyable
69  ControlSpace(const ControlSpace&) = delete;
70  ControlSpace& operator=(const ControlSpace&) = delete;
71 
73  ControlSpace(const base::StateSpacePtr &stateSpace);
74 
75  virtual ~ControlSpace();
76 
78  template<class T>
79  T* as()
80  {
82  BOOST_CONCEPT_ASSERT((boost::Convertible<T*, ControlSpace*>));
83 
84  return static_cast<T*>(this);
85  }
86 
88  template<class T>
89  const T* as() const
90  {
92  BOOST_CONCEPT_ASSERT((boost::Convertible<T*, ControlSpace*>));
93 
94  return static_cast<const T*>(this);
95  }
96 
98  const std::string& getName() const;
99 
101  void setName(const std::string &name);
102 
106  int getType() const
107  {
108  return type_;
109  }
110 
113  {
114  return stateSpace_;
115  }
116 
118  virtual unsigned int getDimension() const = 0;
119 
121  virtual Control* allocControl() const = 0;
122 
124  virtual void freeControl(Control *control) const = 0;
125 
127  virtual void copyControl(Control *destination, const Control *source) const = 0;
128 
130  virtual bool equalControls(const Control *control1, const Control *control2) const = 0;
131 
133  virtual void nullControl(Control *control) const = 0;
134 
136  virtual ControlSamplerPtr allocDefaultControlSampler() const = 0;
137 
141  virtual ControlSamplerPtr allocControlSampler() const;
142 
145 
148 
153  virtual double* getValueAddressAtIndex(Control *control, const unsigned int index) const;
154 
156  virtual void printControl(const Control *control, std::ostream &out) const;
157 
159  virtual void printSettings(std::ostream &out) const;
160 
162  virtual void setup();
163 
165  virtual unsigned int getSerializationLength() const;
166 
168  virtual void serialize(void *serialization, const Control *ctrl) const;
169 
171  virtual void deserialize(Control *ctrl, const void *serialization) const;
172 
175  void computeSignature(std::vector<int> &signature) const;
176 
178  virtual bool isCompound() const;
179 
180  protected:
181 
183  int type_;
184 
187 
190 
191  private:
192 
194  std::string name_;
195  };
196 
199  {
200  public:
201 
204 
206  CompoundControlSpace(const base::StateSpacePtr &stateSpace) : ControlSpace(stateSpace), componentCount_(0), locked_(false)
207  {
208  }
209 
210  virtual ~CompoundControlSpace()
211  {
212  }
213 
215  template<class T>
216  T* as(const unsigned int index) const
217  {
219  BOOST_CONCEPT_ASSERT((boost::Convertible<T*, ControlSpace*>));
220 
221  return static_cast<T*>(getSubspace(index).get());
222  }
223 
225  virtual void addSubspace(const ControlSpacePtr &component);
226 
228  unsigned int getSubspaceCount() const;
229 
231  const ControlSpacePtr& getSubspace(const unsigned int index) const;
232 
234  const ControlSpacePtr& getSubspace(const std::string &name) const;
235 
236  virtual unsigned int getDimension() const;
237 
238  virtual Control* allocControl() const;
239 
240  virtual void freeControl(Control *control) const;
241 
242  virtual void copyControl(Control *destination, const Control *source) const;
243 
244  virtual bool equalControls(const Control *control1, const Control *control2) const;
245 
246  virtual void nullControl(Control *control) const;
247 
249 
250  virtual double* getValueAddressAtIndex(Control *control, const unsigned int index) const;
251 
252  virtual void printControl(const Control *control, std::ostream &out = std::cout) const;
253 
254  virtual void printSettings(std::ostream &out) const;
255 
256  virtual void setup();
257 
259  virtual unsigned int getSerializationLength() const;
260 
262  virtual void serialize(void *serialization, const Control *ctrl) const;
263 
265  virtual void deserialize(Control *ctrl, const void *serialization) const;
266 
267  virtual bool isCompound() const;
268 
274  void lock();
275 
276  protected:
277 
279  std::vector<ControlSpacePtr> components_;
280 
282  unsigned int componentCount_;
283 
285  bool locked_;
286  };
287  }
288 }
289 
290 #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:112
base::StateSpacePtr stateSpace_
The state space controls can be applied to.
Definition: ControlSpace.h:186
const std::string & getName() const
Get the name of the control space.
virtual void freeControl(Control *control) const =0
Free the memory of a control.
bool locked_
Flag indicating whether adding further components is allowed or not.
Definition: ControlSpace.h:285
virtual ControlSamplerPtr allocDefaultControlSampler() const =0
Allocate the default control sampler.
Definition of an abstract control.
Definition: Control.h:48
A shared pointer wrapper for ompl::base::StateSpace.
A shared pointer wrapper for ompl::control::ControlSampler.
virtual void deserialize(Control *ctrl, const void *serialization) const
Deserializes a control from the serialization buffer.
virtual void copyControl(Control *destination, const Control *source) const =0
Copy a control to another.
CompoundControl ControlType
Define the type of control allocated by this control space.
Definition: ControlSpace.h:203
void setName(const std::string &name)
Set the name of the control space.
std::function< ControlSamplerPtr(const ControlSpace *)> ControlSamplerAllocator
Definition of a function that can allocate a control sampler.
void clearControlSamplerAllocator()
Clear the control sampler allocator (reset to default)
CompoundControlSpace(const base::StateSpacePtr &stateSpace)
Constructor. The corresponding state space needs to be specified.
Definition: ControlSpace.h:206
T * as(const unsigned int index) const
Cast a component of this instance to a desired type.
Definition: ControlSpace.h:216
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:279
A shared pointer wrapper for ompl::control::ControlSpace.
T * as()
Cast this instance to a desired type.
Definition: ControlSpace.h:79
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:106
virtual void setup()
Perform final setup steps. This function is automatically called by the SpaceInformation.
Definition of a compound control.
Definition: Control.h:93
int type_
A type assigned for this control space.
Definition: ControlSpace.h:183
virtual unsigned int getDimension() const =0
Get the dimension of this control space.
A control space representing the space of applicable controls.
Definition: ControlSpace.h:65
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:89
unsigned int componentCount_
The number of contained components.
Definition: ControlSpace.h:282
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...
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:189
A control space to allow the composition of control spaces.
Definition: ControlSpace.h:198
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 bool equalControls(const Control *control1, const Control *control2) const =0
Check if two controls are the same.