SpinBoxToVector.h
Go to the documentation of this file.
1 /*
2  * This file is part of ArmarX.
3  *
4  * ArmarX is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License version 2 as
6  * published by the Free Software Foundation.
7  *
8  * ArmarX is distributed in the hope that it will be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program. If not, see <http://www.gnu.org/licenses/>.
15  *
16  * @package RobotAPI::ArmarXObjects::NJointControllerGuiPluginUtility
17  * @author Raphael Grimm ( raphael dot grimm at kit dot edu )
18  * @date 2020
19  * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
20  * GNU General Public License
21  */
22 
23 #pragma once
24 
25 #include <array>
26 #include <vector>
27 
28 #include <Eigen/Dense>
29 
30 #include <QDoubleSpinBox>
31 
32 #include <VirtualRobot/RobotNodeSet.h>
33 #include <VirtualRobot/Nodes/RobotNode.h>
34 
37 
38 namespace armarx
39 {
40  template<class Qwid = QDoubleSpinBox, std::size_t Size = 0>
42  {
43  public:
44  template<class Scalar, int Rows, int Cols>
46  {
48  if constexpr(Size)
49  {
50  ARMARX_CHECK_EQUAL(Size, _widgets.size());
51  }
52  if constexpr(Rows == -1 && Cols == -1)
53  {
54  m.resize(_widgets.size(), 1);
55  }
56  else if constexpr(Rows == 1 && Cols == -1)
57  {
58  m.resize(_widgets.size());
59  }
60  else if constexpr(Rows == -1 && Cols == 1)
61  {
62  m.resize(_widgets.size());
63  }
64  else if constexpr(Cols == -1)
65  {
66  m.resize(Rows, _widgets.size() / Rows);
67  }
68  else if constexpr(Rows == -1)
69  {
70  m.resize(_widgets.size() / Cols, Cols);
71  }
72 
74  ARMARX_CHECK_EQUAL(static_cast<std::size_t>(m.size()), _widgets.size());
75  for (std::size_t i = 0; i < _widgets.size(); ++i)
76  {
77  m.data()[i] = _widgets.at(i)->value();
78  }
79  }
80  template<class Scalar>
81  void get(std::vector<Scalar>& m) const
82  {
84  if constexpr(Size)
85  {
86  ARMARX_CHECK_EQUAL(Size, _widgets.size());
87  }
88  m.resize(_widgets.size());
89  ARMARX_CHECK_EQUAL(m.size(), _widgets.size());
90  for (std::size_t i = 0; i < _widgets.size(); ++i)
91  {
92  m.at(i) = _widgets.at(i)->value();
93  }
94  }
95  template<class T>
96  T get() const
97  {
99  if constexpr(Size)
100  {
101  ARMARX_CHECK_EQUAL(Size, _widgets.size());
102  }
103  T m;
104  get(m);
105  return m;
106  }
107  template<class Scalar, int Rows, int Cols>
109  {
110  ARMARX_TRACE;
111  if constexpr(Size)
112  {
113  ARMARX_CHECK_EQUAL(Size, _widgets.size());
114  }
115  ARMARX_CHECK_EQUAL(m.size(), _widgets.size());
116  for (int i = 0; i < _widgets.size(); ++i)
117  {
118  _widgets.at(i)->setValue(m.data()[i]);
119  }
120  }
121  void set(const VirtualRobot::RobotNodeSetPtr& set)
122  {
123  ARMARX_TRACE;
125  ARMARX_CHECK_EQUAL(set->getSize(), _widgets.size());
126  for (std::size_t i = 0; i < _widgets.size(); ++i)
127  {
128  ARMARX_CHECK_NOT_NULL(set->getNode(i));
129  _widgets.at(i)->setValue(set->getNode(i)->getJointValue());
130  }
131  }
132  void set(double d)
133  {
134  for (auto w : _widgets)
135  {
136  w->setValue(d);
137  }
138  }
139  public:
140  void addWidget(Qwid* s)
141  {
142  ARMARX_TRACE;
144  if constexpr(Size)
145  {
146  ARMARX_CHECK_LESS(_sz, Size);
147  _widgets.at(_sz) = s;
148  }
149  else
150  {
151  _widgets.emplace_back(s);
152  }
153  ++_sz;
154  }
155  void clear()
156  {
157  ARMARX_TRACE;
158  _sz = 0;
159  if constexpr(!Size)
160  {
161  _widgets.clear();
162  }
163  }
164  void visitWidgets(const auto& f)
165  {
166  for (auto w : _widgets)
167  {
168  f(w);
169  }
170  }
171  void setMin(auto min)
172  {
173  for (auto w : _widgets)
174  {
175  w->setMinimum(min);
176  }
177  }
178  void setMax(auto max)
179  {
180  for (auto w : _widgets)
181  {
182  w->setMaximum(max);
183  }
184  }
185  void setMinMax(auto min, auto max)
186  {
187  setMin(min);
188  setMax(max);
189  }
190  auto& widgets()
191  {
192  return _widgets;
193  }
194  private:
195  unsigned _sz = 0;
196  std::conditional_t <
197  Size,
198  std::array<Qwid*, Size>,
199  std::vector<Qwid*>
200  > _widgets;
201  };
202 }
armarx::SpinBoxToVector::clear
void clear()
Definition: SpinBoxToVector.h:155
ARMARX_CHECK_NOT_NULL
#define ARMARX_CHECK_NOT_NULL(ptr)
This macro evaluates whether ptr is not null and if it turns out to be false it will throw an Express...
Definition: ExpressionException.h:206
trace.h
armarx::max
std::vector< T > max(const std::vector< T > &v1, const std::vector< T > &v2)
Definition: VectorHelpers.h:267
armarx::SpinBoxToVector::widgets
auto & widgets()
Definition: SpinBoxToVector.h:190
armarx::SpinBoxToVector::setMax
void setMax(auto max)
Definition: SpinBoxToVector.h:178
ARMARX_CHECK_LESS
#define ARMARX_CHECK_LESS(lhs, rhs)
This macro evaluates whether lhs is less (<) than rhs and if it turns out to be false it will throw a...
Definition: ExpressionException.h:102
armarx::SpinBoxToVector::visitWidgets
void visitWidgets(const auto &f)
Definition: SpinBoxToVector.h:164
armarx::SpinBoxToVector::get
void get(Eigen::Matrix< Scalar, Rows, Cols > &m) const
Definition: SpinBoxToVector.h:45
armarx::SpinBoxToVector::set
void set(double d)
Definition: SpinBoxToVector.h:132
ARMARX_TRACE
#define ARMARX_TRACE
Definition: trace.h:69
armarx::SpinBoxToVector::addWidget
void addWidget(Qwid *s)
Definition: SpinBoxToVector.h:140
armarx::SpinBoxToVector
Definition: SpinBoxToVector.h:41
armarx::SpinBoxToVector::setMinMax
void setMinMax(auto min, auto max)
Definition: SpinBoxToVector.h:185
ExpressionException.h
armarx::SpinBoxToVector::set
void set(const VirtualRobot::RobotNodeSetPtr &set)
Definition: SpinBoxToVector.h:121
armarx::min
std::vector< T > min(const std::vector< T > &v1, const std::vector< T > &v2)
Definition: VectorHelpers.h:294
armarx::SpinBoxToVector::get
T get() const
Definition: SpinBoxToVector.h:96
armarx::SpinBoxToVector::setMin
void setMin(auto min)
Definition: SpinBoxToVector.h:171
Eigen::Matrix
Definition: EigenForwardDeclarations.h:27
armarx::SpinBoxToVector::set
void set(const Eigen::Matrix< Scalar, Rows, Cols > &m)
Definition: SpinBoxToVector.h:108
T
float T
Definition: UnscentedKalmanFilterTest.cpp:35
ARMARX_CHECK_EQUAL
#define ARMARX_CHECK_EQUAL(lhs, rhs)
This macro evaluates whether lhs is equal (==) rhs and if it turns out to be false it will throw an E...
Definition: ExpressionException.h:130
armarx::SpinBoxToVector::get
void get(std::vector< Scalar > &m) const
Definition: SpinBoxToVector.h:81
armarx::ctrlutil::s
double s(double t, double s0, double v0, double a0, double j)
Definition: CtrlUtil.h:33
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:28