RcsPySim
A robot control and simulation library
BoxSpace.h
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 #ifndef _BOXSPACE_H_
32 #define _BOXSPACE_H_
33 
34 #include <Rcs_MatNd.h>
35 
36 #include <string>
37 #include <vector>
38 
39 namespace Rcs
40 {
41 
42 /**
43  * A box in R^n.
44  *
45  * Each coordinate is bounded by a minimum and maximum value.
46  *
47  * This class doesn't define any advanced operations, it solely exists to clean up the C++ interface of the implementation.
48  */
49 class BoxSpace
50 {
51 public:
52  /**
53  * Create a box space from min/max value matrices. The shape of the matrices must match.
54  *
55  * The BoxSpace object will assume ownership of the MatNds.
56  *
57  * @param min minimum values for each element.
58  * @param max maximum values for each element.
59  * @param names variable names for each element.
60  */
61  BoxSpace(MatNd* min, MatNd* max, std::vector<std::string> names = {});
62 
63  /**
64  * Create a box space with the same min/max value for all dimensions.
65  *
66  * @param min minimum value for any element
67  * @param max maximum value for any element
68  * @param m number of rows in space
69  * @param n number of columns in space
70  * @param names variable names for each element.
71  */
72  BoxSpace(double min, double max, unsigned int m, unsigned int n = 1, std::vector<std::string> names = {});
73 
74  // copyable
75  BoxSpace(const BoxSpace&);
76 
77  BoxSpace& operator=(const BoxSpace&);
78 
79  // moveable
80  BoxSpace(BoxSpace&&) noexcept;
81 
82  BoxSpace& operator=(BoxSpace&&) noexcept;
83 
84  // cleans up owned MatNds
85  ~BoxSpace();
86 
87 
88  /**
89  * Check if the given MatNd has the right shape for this space.
90  *
91  * The optional parameter can take an error message to report back.
92  *
93  * @param[in] values value matrix to check
94  * @param[out] msg string variable to write a message to if the values do not fit.
95  *
96  * @return true if the values fit
97  */
98  bool checkDimension(const MatNd* values, std::string* msg = NULL) const;
99 
100  /**
101  * Check if the given MatNd has the right shape and it's values are valid.
102  *
103  * The optional parameter can take an error message to report back.
104  *
105  * @param[in] values value matrix to check
106  * @param[out] msg string variable to write a message to if the values do not fit.
107  *
108  * @return true if the values fit
109  */
110  bool contains(const MatNd* values, std::string* msg = NULL) const;
111 
112 
113  /**
114  * Create a matrix of the right shape to fit into the space.
115  * @return a MatNd of the right shape
116  */
117  MatNd* createValueMatrix() const;
118 
119  /**
120  * Fill the given matrix out with random values fitting into the space bounds.
121  *
122  * @param[out] out MatNd to fill. The shape must match.
123  */
124  void sample(MatNd* out) const;
125 
126  /**
127  * Lower bounds for each variable.
128  */
129  const MatNd* getMin() const
130  {
131  return min;
132  }
133 
134  /**
135  * Upper bounds for each variable.
136  */
137  const MatNd* getMax() const
138  {
139  return max;
140  }
141 
142  /**
143  * Names for each variable.
144  *
145  * These are intended for use in python, i.e., for pandas dataframe column names.
146  */
147  const std::vector<std::string>& getNames() const
148  {
149  return names;
150  }
151 
152 private:
153  // space bounds
154  MatNd* min;
155  MatNd* max;
156 
157  // names for every space entry
158  // flattened, row-major
159  std::vector<std::string> names;
160 };
161 
162 } /* namespace Rcs */
163 
164 #endif /* _BOXSPACE_H_ */
MatNd * max
Definition: BoxSpace.h:155
MatNd * createValueMatrix() const
Definition: BoxSpace.cpp:197
void sample(MatNd *out) const
Definition: BoxSpace.cpp:202
const MatNd * getMin() const
Definition: BoxSpace.h:129
std::vector< std::string > names
Definition: BoxSpace.h:159
const MatNd * getMax() const
Definition: BoxSpace.h:137
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
const std::vector< std::string > & getNames() const
Definition: BoxSpace.h:147