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 
47  auto *rcontrol = static_cast<RealVectorControlSpace::ControlType *>(control);
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 " + std::to_string(bounds.low.size()));
64  bounds_ = bounds;
65 }
66 
68 {
69  return dimension_;
70 }
71 
73 {
74  memcpy(static_cast<ControlType *>(destination)->values, static_cast<const ControlType *>(source)->values,
75  controlBytes_);
76 }
77 
78 bool ompl::control::RealVectorControlSpace::equalControls(const Control *control1, const Control *control2) const
79 {
80  const double *s1 = static_cast<const ControlType *>(control1)->values;
81  const double *s2 = static_cast<const ControlType *>(control2)->values;
82  for (unsigned int i = 0; i < dimension_; ++i)
83  {
84  double diff = (*s1++) - (*s2++);
85  if (fabs(diff) > std::numeric_limits<double>::epsilon() * 2.0)
86  return false;
87  }
88  return true;
89 }
90 
92 {
93  return std::make_shared<RealVectorControlUniformSampler>(this);
94 }
95 
97 {
98  auto *rcontrol = new ControlType();
99  rcontrol->values = new double[dimension_];
100  return rcontrol;
101 }
102 
104 {
105  auto *rcontrol = static_cast<ControlType *>(control);
106  delete[] rcontrol->values;
107  delete rcontrol;
108 }
109 
111 {
112  auto *rcontrol = static_cast<ControlType *>(control);
113  for (unsigned int i = 0; i < dimension_; ++i)
114  {
115  if (bounds_.low[i] <= 0.0 && bounds_.high[i] >= 0.0)
116  rcontrol->values[i] = 0.0;
117  else
118  rcontrol->values[i] = bounds_.low[i];
119  }
120 }
121 
122 double *ompl::control::RealVectorControlSpace::getValueAddressAtIndex(Control *control, const unsigned int index) const
123 {
124  return index < dimension_ ? static_cast<ControlType *>(control)->values + index : nullptr;
125 }
126 
127 void ompl::control::RealVectorControlSpace::printControl(const Control *control, std::ostream &out) const
128 {
129  out << "RealVectorControl [";
130  if (control != nullptr)
131  {
132  const auto *rcontrol = static_cast<const ControlType *>(control);
133  for (unsigned int i = 0; i < dimension_; ++i)
134  {
135  out << rcontrol->values[i];
136  if (i + 1 < dimension_)
137  out << ' ';
138  }
139  }
140  else
141  out << "nullptr";
142  out << ']' << std::endl;
143 }
144 
146 {
147  out << "Real vector control space '" << getName() << "' with bounds: " << std::endl;
148  out << " - min: ";
149  for (unsigned int i = 0; i < dimension_; ++i)
150  out << bounds_.low[i] << " ";
151  out << std::endl;
152  out << " - max: ";
153  for (unsigned int i = 0; i < dimension_; ++i)
154  out << bounds_.high[i] << " ";
155  out << std::endl;
156 }
157 
159 {
160  return controlBytes_;
161 }
162 
163 void ompl::control::RealVectorControlSpace::serialize(void *serialization, const Control *ctrl) const
164 {
165  memcpy(serialization, ctrl->as<ControlType>()->values, controlBytes_);
166 }
167 
168 void ompl::control::RealVectorControlSpace::deserialize(Control *ctrl, const void *serialization) const
169 {
170  memcpy(ctrl->as<ControlType>()->values, serialization, controlBytes_);
171 }
unsigned int getSerializationLength() const override
Returns the serialization size for a single control in this space.
ControlSamplerPtr allocDefaultControlSampler() const override
Allocate the default control sampler.
void copyControl(Control *destination, const Control *source) const override
Copy a control to another.
RNG rng_
Instance of random number generator.
Control * allocControl() const override
Allocate memory for a control.
void setBounds(const base::RealVectorBounds &bounds)
Set the bounds (min max values for each dimension) for the control.
bool equalControls(const Control *control1, const Control *control2) const override
Check if two controls are the same.
Definition of an abstract control.
Definition: Control.h:111
double * getValueAddressAtIndex(Control *control, unsigned int index) const override
Many controls contain a number of double values. This function provides a means to get the memory add...
virtual unsigned int getDimension() const =0
Get the dimension of this control space.
virtual void setup()
Perform final setup steps. This function is automatically called by the SpaceInformation.
void freeControl(Control *control) const override
Free the memory of a control.
A shared pointer wrapper for ompl::control::ControlSampler.
void printSettings(std::ostream &out) const override
Print the settings for this control space to a stream.
void serialize(void *serialization, const Control *ctrl) const override
Serializes the given control into the serialization buffer.
double uniformReal(double lower_bound, double upper_bound)
Generate a random real within given bounds: [lower_bound, upper_bound)
void deserialize(Control *ctrl, const void *serialization) const override
Deserializes a control from the serialization buffer.
A control space representing Rn.
void nullControl(Control *control) const override
Make the control have no effect if it were to be applied to a state for any amount of time.
void sample(Control *control) override
Sample a control. All other control sampling functions default to this one, unless a user-specified i...
const T * as() const
Cast this instance to a desired type.
Definition: Control.h:160
unsigned int getDimension() const override
Get the dimension of this control space.
void setup() override
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.
The exception type for ompl.
Definition: Exception.h:78
void printControl(const Control *control, std::ostream &out) const override
Print a control to a stream.
const ControlSpace * space_
The control space this sampler operates on.
The lower and upper bounds for an Rn space.