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 <QDoubleSpinBox>
29
30#include <Eigen/Dense>
31
32#include <VirtualRobot/Nodes/RobotNode.h>
33#include <VirtualRobot/RobotNodeSet.h>
34
37
38namespace armarx
39{
40 template <class Qwid = QDoubleSpinBox, std::size_t Size = 0>
42 {
43 public:
44 template <class Scalar, int Rows, int Cols>
45 void
47 {
49 if constexpr (Size)
50 {
51 ARMARX_CHECK_EQUAL(Size, _widgets.size());
52 }
53 if constexpr (Rows == -1 && Cols == -1)
54 {
55 m.resize(_widgets.size(), 1);
56 }
57 else if constexpr (Rows == 1 && Cols == -1)
58 {
59 m.resize(_widgets.size());
60 }
61 else if constexpr (Rows == -1 && Cols == 1)
62 {
63 m.resize(_widgets.size());
64 }
65 else if constexpr (Cols == -1)
66 {
67 m.resize(Rows, _widgets.size() / Rows);
68 }
69 else if constexpr (Rows == -1)
70 {
71 m.resize(_widgets.size() / Cols, Cols);
72 }
73
75 ARMARX_CHECK_EQUAL(static_cast<std::size_t>(m.size()), _widgets.size());
76 for (std::size_t i = 0; i < _widgets.size(); ++i)
77 {
78 m.data()[i] = _widgets.at(i)->value();
79 }
80 }
81
82 template <class Scalar>
83 void
84 get(std::vector<Scalar>& m) const
85 {
87 if constexpr (Size)
88 {
89 ARMARX_CHECK_EQUAL(Size, _widgets.size());
90 }
91 m.resize(_widgets.size());
92 ARMARX_CHECK_EQUAL(m.size(), _widgets.size());
93 for (std::size_t i = 0; i < _widgets.size(); ++i)
94 {
95 m.at(i) = _widgets.at(i)->value();
96 }
97 }
98
99 template <class T>
100 T
101 get() const
102 {
104 if constexpr (Size)
105 {
106 ARMARX_CHECK_EQUAL(Size, _widgets.size());
107 }
108 T m;
109 get(m);
110 return m;
111 }
112
113 template <class Scalar, int Rows, int Cols>
114 void
116 {
118 if constexpr (Size)
119 {
120 ARMARX_CHECK_EQUAL(Size, _widgets.size());
121 }
122 ARMARX_CHECK_EQUAL(m.size(), _widgets.size());
123 for (int i = 0; i < _widgets.size(); ++i)
124 {
125 _widgets.at(i)->setValue(m.data()[i]);
126 }
127 }
128
129 void
130 set(const VirtualRobot::RobotNodeSetPtr& set)
131 {
134 ARMARX_CHECK_EQUAL(set->getSize(), _widgets.size());
135 for (std::size_t i = 0; i < _widgets.size(); ++i)
136 {
137 ARMARX_CHECK_NOT_NULL(set->getNode(i));
138 _widgets.at(i)->setValue(set->getNode(i)->getJointValue());
139 }
140 }
141
142 void
143 set(double d)
144 {
145 for (auto w : _widgets)
146 {
147 w->setValue(d);
148 }
149 }
150
151 public:
152 void
153 addWidget(Qwid* s)
154 {
157 if constexpr (Size)
158 {
159 ARMARX_CHECK_LESS(_sz, Size);
160 _widgets.at(_sz) = s;
161 }
162 else
163 {
164 _widgets.emplace_back(s);
165 }
166 ++_sz;
167 }
168
169 void
171 {
173 _sz = 0;
174 if constexpr (!Size)
175 {
176 _widgets.clear();
177 }
178 }
179
180 void
181 visitWidgets(const auto& f)
182 {
183 for (auto w : _widgets)
184 {
185 f(w);
186 }
187 }
188
189 void
191 {
192 for (auto w : _widgets)
193 {
194 w->setMinimum(min);
195 }
196 }
197
198 void
200 {
201 for (auto w : _widgets)
202 {
203 w->setMaximum(max);
204 }
205 }
206
207 void
208 setMinMax(auto min, auto max)
209 {
210 setMin(min);
211 setMax(max);
212 }
213
214 auto&
216 {
217 return _widgets;
218 }
219
220 private:
221 unsigned _sz = 0;
222 std::conditional_t<Size, std::array<Qwid*, Size>, std::vector<Qwid*>> _widgets;
223 };
224} // namespace armarx
void get(std::vector< Scalar > &m) const
void visitWidgets(const auto &f)
void setMinMax(auto min, auto max)
void get(Eigen::Matrix< Scalar, Rows, Cols > &m) const
void set(const VirtualRobot::RobotNodeSetPtr &set)
void set(const Eigen::Matrix< Scalar, Rows, Cols > &m)
#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...
#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...
#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...
This file offers overloads of toIce() and fromIce() functions for STL container types.
std::vector< T > max(const std::vector< T > &v1, const std::vector< T > &v2)
std::vector< T > min(const std::vector< T > &v1, const std::vector< T > &v2)
#define ARMARX_TRACE
Definition trace.h:77