ompl/control/ControlSpace.h
00001 /*********************************************************************
00002 * Software License Agreement (BSD License)
00003 *
00004 *  Copyright (c) 2010, Rice University
00005 *  All rights reserved.
00006 *
00007 *  Redistribution and use in source and binary forms, with or without
00008 *  modification, are permitted provided that the following conditions
00009 *  are met:
00010 *
00011 *   * Redistributions of source code must retain the above copyright
00012 *     notice, this list of conditions and the following disclaimer.
00013 *   * Redistributions in binary form must reproduce the above
00014 *     copyright notice, this list of conditions and the following
00015 *     disclaimer in the documentation and/or other materials provided
00016 *     with the distribution.
00017 *   * Neither the name of the Rice University nor the names of its
00018 *     contributors may be used to endorse or promote products derived
00019 *     from this software without specific prior written permission.
00020 *
00021 *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00022 *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00023 *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00024 *  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00025 *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00026 *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00027 *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00028 *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00029 *  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00030 *  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00031 *  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00032 *  POSSIBILITY OF SUCH DAMAGE.
00033 *********************************************************************/
00034 
00035 /* Author: Ioan Sucan */
00036 
00037 #ifndef OMPL_CONTROL_CONTROL_SPACE_
00038 #define OMPL_CONTROL_CONTROL_SPACE_
00039 
00040 #include "ompl/base/StateSpace.h"
00041 #include "ompl/control/Control.h"
00042 #include "ompl/control/ControlSampler.h"
00043 #include "ompl/control/ControlSpaceTypes.h"
00044 #include "ompl/util/Console.h"
00045 #include "ompl/util/ClassForward.h"
00046 #include <boost/concept_check.hpp>
00047 #include <boost/noncopyable.hpp>
00048 #include <iostream>
00049 #include <vector>
00050 
00051 namespace ompl
00052 {
00053 
00054     namespace control
00055     {
00056 
00058 
00059         OMPL_CLASS_FORWARD(ControlSpace);
00061 
00066         class ControlSpace : private boost::noncopyable
00067         {
00068         public:
00069 
00071             ControlSpace(const base::StateSpacePtr &stateSpace);
00072 
00073             virtual ~ControlSpace();
00074 
00076             template<class T>
00077             T* as()
00078             {
00080                 BOOST_CONCEPT_ASSERT((boost::Convertible<T*, ControlSpace*>));
00081 
00082                 return static_cast<T*>(this);
00083             }
00084 
00086             template<class T>
00087             const T* as() const
00088             {
00090                 BOOST_CONCEPT_ASSERT((boost::Convertible<T*, ControlSpace*>));
00091 
00092                 return static_cast<const T*>(this);
00093             }
00094 
00096             const std::string& getName() const;
00097 
00099             void setName(const std::string &name);
00100 
00104             int getType() const
00105             {
00106                 return type_;
00107             }
00108 
00110             const base::StateSpacePtr& getStateSpace() const
00111             {
00112                 return stateSpace_;
00113             }
00114 
00116             virtual unsigned int getDimension() const = 0;
00117 
00119             virtual Control* allocControl() const = 0;
00120 
00122             virtual void freeControl(Control *control) const = 0;
00123 
00125             virtual void copyControl(Control *destination, const Control *source) const = 0;
00126 
00128             virtual bool equalControls(const Control *control1, const Control *control2) const = 0;
00129 
00131             virtual void nullControl(Control *control) const = 0;
00132 
00134             virtual ControlSamplerPtr allocDefaultControlSampler() const = 0;
00135 
00139             virtual ControlSamplerPtr allocControlSampler() const;
00140 
00142             void setControlSamplerAllocator(const ControlSamplerAllocator &csa);
00143 
00145             void clearControlSamplerAllocator();
00146 
00151             virtual double* getValueAddressAtIndex(Control *control, const unsigned int index) const;
00152 
00154             virtual void printControl(const Control *control, std::ostream &out) const;
00155 
00157             virtual void printSettings(std::ostream &out) const;
00158 
00160             virtual void setup();
00161 
00163             virtual unsigned int getSerializationLength() const;
00164 
00166             virtual void serialize(void *serialization, const Control *ctrl) const;
00167 
00169             virtual void deserialize(Control *ctrl, const void *serialization) const;
00170 
00173             void computeSignature(std::vector<int> &signature) const;
00174 
00176             virtual bool isCompound() const;
00177 
00178         protected:
00179 
00181             int                     type_;
00182 
00184             base::StateSpacePtr     stateSpace_;
00185 
00187             ControlSamplerAllocator csa_;
00188 
00189         private:
00190 
00192             std::string             name_;
00193         };
00194 
00196         class CompoundControlSpace : public ControlSpace
00197         {
00198         public:
00199 
00201             typedef CompoundControl ControlType;
00202 
00204             CompoundControlSpace(const base::StateSpacePtr &stateSpace) : ControlSpace(stateSpace), componentCount_(0), locked_(false)
00205             {
00206             }
00207 
00208             virtual ~CompoundControlSpace()
00209             {
00210             }
00211 
00213             template<class T>
00214             T* as(const unsigned int index) const
00215             {
00217                 BOOST_CONCEPT_ASSERT((boost::Convertible<T*, ControlSpace*>));
00218 
00219                 return static_cast<T*>(getSubspace(index).get());
00220             }
00221 
00223             virtual void addSubspace(const ControlSpacePtr &component);
00224 
00226             unsigned int getSubspaceCount() const;
00227 
00229             const ControlSpacePtr& getSubspace(const unsigned int index) const;
00230 
00232             const ControlSpacePtr& getSubspace(const std::string &name) const;
00233 
00234             virtual unsigned int getDimension() const;
00235 
00236             virtual Control* allocControl() const;
00237 
00238             virtual void freeControl(Control *control) const;
00239 
00240             virtual void copyControl(Control *destination, const Control *source) const;
00241 
00242             virtual bool equalControls(const Control *control1, const Control *control2) const;
00243 
00244             virtual void nullControl(Control *control) const;
00245 
00246             virtual ControlSamplerPtr allocDefaultControlSampler() const;
00247 
00248             virtual double* getValueAddressAtIndex(Control *control, const unsigned int index) const;
00249 
00250             virtual void printControl(const Control *control, std::ostream &out = std::cout) const;
00251 
00252             virtual void printSettings(std::ostream &out) const;
00253 
00254             virtual void setup();
00255 
00257             virtual unsigned int getSerializationLength() const;
00258 
00260             virtual void serialize(void *serialization, const Control *ctrl) const;
00261 
00263             virtual void deserialize(Control *ctrl, const void *serialization) const;
00264 
00265             virtual bool isCompound() const;
00266 
00272             void lock();
00273 
00274         protected:
00275 
00277             std::vector<ControlSpacePtr>    components_;
00278 
00280             unsigned int                    componentCount_;
00281 
00283             bool                            locked_;
00284         };
00285     }
00286 }
00287 
00288 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines