RcsPySim
A robot control and simulation library
BoxSpace.cpp
Go to the documentation of this file.
1 /*******************************************************************************
2  Copyright (c) 2020, Fabio Muratore, Honda Research Institute Europe GmbH, and
3  Technical University of Darmstadt.
4  All rights reserved.
5 
6  Redistribution and use in source and binary forms, with or without
7  modification, are permitted provided that the following conditions are met:
8  1. Redistributions of source code must retain the above copyright
9  notice, this list of conditions and the following disclaimer.
10  2. Redistributions in binary form must reproduce the above copyright
11  notice, this list of conditions and the following disclaimer in the
12  documentation and/or other materials provided with the distribution.
13  3. Neither the name of Fabio Muratore, Honda Research Institute Europe GmbH,
14  or Technical University of Darmstadt, nor the names of its contributors may
15  be used to endorse or promote products derived from this software without
16  specific prior written permission.
17 
18  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
19  ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21  DISCLAIMED. IN NO EVENT SHALL FABIO MURATORE, HONDA RESEARCH INSTITUTE EUROPE GMBH,
22  OR TECHNICAL UNIVERSITY OF DARMSTADT BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
25  OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
26  IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28  POSSIBILITY OF SUCH DAMAGE.
29 *******************************************************************************/
30 
31 #include "BoxSpace.h"
32 
33 #include <Rcs_macros.h>
34 #include <Rcs_basicMath.h>
35 
36 #include <sstream>
37 
38 namespace Rcs
39 {
40 
41 static void initDefaultNames(std::vector<std::string>& out, unsigned int dim)
42 {
43  // generate numbered values
44  const char* format = "value %02d";
45  const size_t BUFFER_LEN = 10;
46  char buffer[BUFFER_LEN];
47 
48  for (unsigned int i = 0; i < dim; ++i) {
49  snprintf(buffer, BUFFER_LEN, format, i);
50  out.emplace_back(buffer);
51  }
52 }
53 
54 BoxSpace::BoxSpace(MatNd* min, MatNd* max, std::vector<std::string> names) :
55  min(min), max(max), names(names)
56 {
57  RCHECK(min);
58  RCHECK(max);
59  // validate that the dimensions match
60  RCHECK_MSG(min->m == max->m && min->n == max->n,
61  "Min (%d, %d) and max (%d, %d) dimensions must match", min->m,
62  max->m, min->n, max->n);
63 
64  // init names with default if any
65  if (names.empty()) {
66  initDefaultNames(names, min->m*min->n);
67  }
68  else {
69  RCHECK_MSG(min->m*min->n == names.size(), "Name variables must match element count");
70  }
71 }
72 
73 BoxSpace::BoxSpace(double min, double max, unsigned int m, unsigned int n, std::vector<std::string> names) : names(
74  names)
75 {
76  // create matrices according to size
77  this->min = MatNd_create(m, n);
78  this->max = MatNd_create(m, n);
79 
80  // fill them with the passed values
81  MatNd_setElementsTo(this->min, min);
82  MatNd_setElementsTo(this->max, max);
83 
84  // init names with default if any
85  if (names.empty()) {
86  initDefaultNames(names, m*n);
87  }
88  else {
89  RCHECK_MSG(m*n == names.size(), "Name variables must match element count");
90  }
91 }
92 
94 {
95  // copy new
96  min = MatNd_clone(other.min);
97  max = MatNd_clone(other.max);
98 }
99 
101 {
102  // destroy old
103  MatNd_destroy(min);
104  MatNd_destroy(max);
105 
106  // copy new
107  min = MatNd_clone(other.min);
108  max = MatNd_clone(other.max);
109 
110  return *this;
111 }
112 
113 BoxSpace::BoxSpace(BoxSpace&& other) noexcept
114 {
115  // steal values
116  min = other.min;
117  max = other.max;
118 
119  // and set them to NULL over there. The destructor can deal with it.
120  other.min = NULL;
121  other.max = NULL;
122 }
123 
125 {
126  // destroy old
127  MatNd_destroy(min);
128  MatNd_destroy(max);
129 
130  // steal values
131  min = other.min;
132  max = other.max;
133 
134  // and set them to NULL over there. The destructor can deal with it.
135  other.min = NULL;
136  other.max = NULL;
137 
138  return *this;
139 }
140 
142 {
143  MatNd_destroy(min);
144  MatNd_destroy(max);
145 }
146 
147 bool BoxSpace::checkDimension(const MatNd* values, std::string* msg) const
148 {
149  if (values->m != min->m || values->n != min->n) {
150  // They don't match
151  if (msg) {
152  std::ostringstream os;
153  os << "mismatching dimensions: expected (" << min->m << ", "
154  << min->n << ") but got (" << values->m << ", " << values->n
155  << ")";
156  *msg = os.str();
157  }
158  return false;
159  }
160  return true;
161 }
162 
163 bool BoxSpace::contains(const MatNd* values, std::string* msg) const
164 {
165  bool valid = true;
166 
167  // Check if the dimensions match
168  if (!checkDimension(values, msg)) {
169  valid = false;
170  }
171 
172  // Check individual value bounds
173  for (unsigned int i = 0; i < values->m*values->n; ++i) {
174  bool less = values->ele[i] < min->ele[i];
175  bool more = values->ele[i] > max->ele[i];
176  if (less || more) {
177  // They don't match
178  if (msg) {
179  std::ostringstream os;
180  os << "value out of bounds: val(" << (i/values->n) << ", "
181  << (i%values->n) << ") = " << values->ele[i];
182  if (less) {
183  os << " < " << min->ele[i];
184  }
185  else {
186  os << " > " << max->ele[i];
187  }
188  *msg = os.str();
189  }
190  valid = false;
191  }
192  }
193 
194  return valid;
195 }
196 
198 {
199  return MatNd_create(min->m, min->n);
200 }
201 
202 void BoxSpace::sample(MatNd* out) const
203 {
204  RCHECK(out->m == min->m && out->n == min->n);
205  // iterate elements
206  for (unsigned int i = 0; i < out->m*out->n; ++i) {
207  out->ele[i] = Math_getRandomNumber(min->ele[i], max->ele[i]);
208  }
209 }
210 
211 } /* namespace Rcs */
MatNd * max
Definition: BoxSpace.h:155
MatNd * createValueMatrix() const
Definition: BoxSpace.cpp:197
void sample(MatNd *out) const
Definition: BoxSpace.cpp:202
std::vector< std::string > names
Definition: BoxSpace.h:159
static void initDefaultNames(std::vector< std::string > &out, unsigned int dim)
Definition: BoxSpace.cpp:41
BoxSpace(MatNd *min, MatNd *max, std::vector< std::string > names={})
Definition: BoxSpace.cpp:54
BoxSpace & operator=(const BoxSpace &)
Definition: BoxSpace.cpp:100
bool contains(const MatNd *values, std::string *msg=NULL) const
Definition: BoxSpace.cpp:163
MatNd * min
Definition: BoxSpace.h:154
bool checkDimension(const MatNd *values, std::string *msg=NULL) const
Definition: BoxSpace.cpp:147