RealVectorControlSpace.cpp
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 #include "ompl/control/spaces/RealVectorControlSpace.h"
38 #include "ompl/util/Exception.h"
39 #include <cstring>
40 #include <limits>
41 
43 {
44  const unsigned int dim = space_->getDimension();
45  const base::RealVectorBounds &bounds = static_cast<const RealVectorControlSpace*>(space_)->getBounds();
46 
48  for (unsigned int i = 0 ; i < dim ; ++i)
49  rcontrol->values[i] = rng_.uniformReal(bounds.low[i], bounds.high[i]);
50 }
51 
53 {
55  bounds_.check();
56 }
57 
59 {
60  bounds.check();
61  if (bounds.low.size() != dimension_)
62  throw Exception("Bounds do not match dimension of control space: expected dimension " +
63  std::to_string(dimension_) + " but got dimension " +
64  std::to_string(bounds.low.size()));
65  bounds_ = bounds;
66 }
67 
69 {
70  return dimension_;
71 }
72 
74 {
75  memcpy(static_cast<ControlType*>(destination)->values,
76  static_cast<const ControlType*>(source)->values, controlBytes_);
77 }
78 
79 bool ompl::control::RealVectorControlSpace::equalControls(const Control *control1, const Control *control2) const
80 {
81  const double *s1 = static_cast<const ControlType*>(control1)->values;
82  const double *s2 = static_cast<const ControlType*>(control2)->values;
83  for (unsigned int i = 0 ; i < dimension_ ; ++i)
84  {
85  double diff = (*s1++) - (*s2++);
86  if (fabs(diff) > std::numeric_limits<double>::epsilon() * 2.0)
87  return false;
88  }
89  return true;
90 }
91 
93 {
95 }
96 
98 {
99  ControlType *rcontrol = new ControlType();
100  rcontrol->values = new double[dimension_];
101  return rcontrol;
102 }
103 
105 {
106  ControlType *rcontrol = static_cast<ControlType*>(control);
107  delete[] rcontrol->values;
108  delete rcontrol;
109 }
110 
112 {
113  ControlType *rcontrol = static_cast<ControlType*>(control);
114  for (unsigned int i = 0 ; i < dimension_ ; ++i)
115  {
116  if (bounds_.low[i] <= 0.0 && bounds_.high[i] >= 0.0)
117  rcontrol->values[i] = 0.0;
118  else
119  rcontrol->values[i] = bounds_.low[i];
120  }
121 }
122 
123 double* ompl::control::RealVectorControlSpace::getValueAddressAtIndex(Control *control, const unsigned int index) const
124 {
125  return index < dimension_ ? static_cast<ControlType*>(control)->values + index : nullptr;
126 }
127 
128 void ompl::control::RealVectorControlSpace::printControl(const Control *control, std::ostream &out) const
129 {
130  out << "RealVectorControl [";
131  if (control)
132  {
133  const ControlType *rcontrol = static_cast<const ControlType*>(control);
134  for (unsigned int i = 0 ; i < dimension_ ; ++i)
135  {
136  out << rcontrol->values[i];
137  if (i + 1 < dimension_)
138  out << ' ';
139  }
140  }
141  else
142  out << "nullptr";
143  out << ']' << std::endl;
144 }
145 
147 {
148  out << "Real vector control space '" << getName() << "' with bounds: " << std::endl;
149  out << " - min: ";
150  for (unsigned int i = 0 ; i < dimension_ ; ++i)
151  out << bounds_.low[i] << " ";
152  out << std::endl;
153  out << " - max: ";
154  for (unsigned int i = 0 ; i < dimension_ ; ++i)
155  out << bounds_.high[i] << " ";
156  out << std::endl;
157 }
158 
160 {
161  return controlBytes_;
162 }
163 
164 void ompl::control::RealVectorControlSpace::serialize(void *serialization, const Control *ctrl) const
165 {
166  memcpy(serialization, ctrl->as<ControlType>()->values, controlBytes_);
167 }
168 
169 void ompl::control::RealVectorControlSpace::deserialize(Control *ctrl, const void *serialization) const
170 {
171  memcpy(ctrl->as<ControlType>()->values, serialization, controlBytes_);
172 }
std::vector< double > low
Lower bound.
const T * as() const
Cast this instance to a desired type.
Definition: Control.h:72
Definition of an abstract control.
Definition: Control.h:48
A shared pointer wrapper for ompl::control::ControlSampler.
const ControlSpace * space_
The control space this sampler operates on.
virtual unsigned int getSerializationLength() const
Returns the serialization size for a single control in this space.
virtual void serialize(void *serialization, const Control *ctrl) const
Serializes the given control into the serialization buffer.
virtual void printControl(const Control *control, std::ostream &out) const
Print a control to a stream.
virtual unsigned int getDimension() const
Get the dimension of this control space.
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...
virtual void nullControl(Control *control) const
Make the control have no effect if it were to be applied to a state for any amount of time...
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
Copy a control to another.
virtual void setup()
Perform final setup steps. This function is automatically called by the SpaceInformation.
A control space representing Rn.
virtual ControlSamplerPtr allocDefaultControlSampler() const
Allocate the default control sampler.
virtual Control * allocControl() const
Allocate memory for a control.
std::vector< double > high
Upper bound.
virtual unsigned int getDimension() const =0
Get the dimension of this control space.
double uniformReal(double lower_bound, double upper_bound)
Generate a random real within given bounds: [lower_bound, upper_bound)
Definition: RandomNumbers.h:75
virtual void freeControl(Control *control) const
Free the memory of a control.
virtual void printSettings(std::ostream &out) const
Print the settings for this control space to a stream.
void check() const
Check if the bounds are valid (same length for low and high, high[i] > low[i]). Throw an exception if...
Uniform sampler for the Rn state space.
The exception type for ompl.
Definition: Exception.h:47
The lower and upper bounds for an Rn space.
virtual void sample(Control *control)
Sample a control. All other control sampling functions default to this one, unless a user-specified i...
RNG rng_
Instance of random number generator.
virtual bool equalControls(const Control *control1, const Control *control2) const
Check if two controls are the same.
void setBounds(const base::RealVectorBounds &bounds)
Set the bounds (min max values for each dimension) for the control.
virtual void setup()
Perform final setup steps. This function is automatically called by the SpaceInformation.
double * values
An array of length n, representing the value of the control.