Matrix4fWidgets.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <QWidget>
4 #include <QDoubleSpinBox>
5 #include <QHBoxLayout>
6 #include <QLabel>
7 
8 #include "Basic.h"
9 
10 namespace armarx::RemoteGui
11 {
12  struct PosRPYSpinBoxesHandler : TypedWidget<PosRPYSpinBoxes, QWidget, VALUE_VARIANT_MATRIX4>
13  {
14  static bool isValid(RemoteWidgetT const& desc, std::ostream& out)
15  {
16 #define cmp_or_log(l,c,r) \
17  (l c r ? true : \
18  ( \
19  out << " FAILED: " #l " " #c " " #r " " \
20  << VAROUT(l) << ", " << VAROUT(r) << '\n',\
21  false \
22  ))
23 
24  return cmp_or_log(desc.minPos.x, <=, desc.maxPos.x)
25  && cmp_or_log(desc.minPos.y, <=, desc.maxPos.y)
26  && cmp_or_log(desc.minPos.z, <=, desc.maxPos.z)
27  && cmp_or_log(desc.stepsPos.x, >, 0)
28  && cmp_or_log(desc.stepsPos.y, >, 0)
29  && cmp_or_log(desc.stepsPos.z, >, 0)
30  && cmp_or_log(desc.decimalsPos.x, >=, 0)
31  && cmp_or_log(desc.decimalsPos.y, >=, 0)
32  && cmp_or_log(desc.decimalsPos.z, >=, 0)
33 
34  && cmp_or_log(desc.minRPY.x, <=, desc.maxRPY.x)
35  && cmp_or_log(desc.minRPY.y, <=, desc.maxRPY.y)
36  && cmp_or_log(desc.minRPY.z, <=, desc.maxRPY.z)
37  && cmp_or_log(desc.stepsRPY.x, >, 0)
38  && cmp_or_log(desc.stepsRPY.y, >, 0)
39  && cmp_or_log(desc.stepsRPY.z, >, 0)
40  && cmp_or_log(desc.decimalsRPY.x, >=, 0)
41  && cmp_or_log(desc.decimalsRPY.y, >=, 0)
42  && cmp_or_log(desc.decimalsRPY.z, >=, 0);
43 #undef cmp_or_log
44  }
45 
46  static QWidgetT* createWidget(RemoteWidgetT const& desc, CreateWidgetCallback const& createChild,
47  const QObject* stateChangeReceiver, const char* stateChangeSlot)
48  {
50  QWidget* widget = new QWidget;
51  QHBoxLayout* l = new QHBoxLayout;
52  l->setContentsMargins(0, 0, 0, 0);
53  widget->setLayout(l);
54  widget->setToolTip(QString::fromStdString(desc.toolTip));
55 
56  Eigen::Vector3f minPos = fromIce(desc.minPos);
57  Eigen::Vector3f maxPos = fromIce(desc.maxPos);
58  Eigen::Vector3i decimalsPos = fromIce(desc.decimalsPos);
59  Eigen::Vector3i stepsPos = fromIce(desc.stepsPos);
60  for (int i = 0; i < 3; ++i)
61  {
62  QDoubleSpinBox* e = new QDoubleSpinBox;
63  l->addWidget(e);
64  e->setMinimum(minPos(i));
65  e->setMaximum(maxPos(i));
66  e->setDecimals(decimalsPos(i));
67  e->setSingleStep((maxPos(i) - minPos(i)) / stepsPos(i));
68  QObject::connect(e, SIGNAL(valueChanged(double)), stateChangeReceiver, stateChangeSlot);
69  }
70  l->addWidget(new QLabel{"/"});
71 
72  Eigen::Vector3f minRPY = fromIce(desc.minRPY);
73  Eigen::Vector3f maxRPY = fromIce(desc.maxRPY);
74  Eigen::Vector3i decimalsRPY = fromIce(desc.decimalsRPY);
75  Eigen::Vector3i stepsRPY = fromIce(desc.stepsRPY);
76  for (int i = 0; i < 3; ++i)
77  {
78  QDoubleSpinBox* e = new QDoubleSpinBox;
79  l->addWidget(e);
80  e->setMinimum(minRPY(i));
81  e->setMaximum(maxRPY(i));
82  e->setDecimals(decimalsRPY(i));
83  e->setSingleStep((maxRPY(i) - minRPY(i)) / stepsRPY(i));
84  QObject::connect(e, SIGNAL(valueChanged(double)), stateChangeReceiver, stateChangeSlot);
85  }
86  return widget;
87  }
88 
89  static void updateGui(RemoteWidgetT const&, QWidgetT* widget, ValueT const& m)
90  {
91  float rx, ry, rz;
92  ry = std::atan2(-m(2, 0), sqrtf(m(0, 0) * m(0, 0) + m(1, 0) * m(1, 0)));
93 
94  if (fabs(ry - static_cast<float>(M_PI) * 0.5f) < 1e-10f)
95  {
96  rx = 0;
97  rz = std::atan2(m(0, 1), m(1, 1));
98  }
99  else if (std::fabs(ry + static_cast<float>(M_PI) * 0.5f) < 1e-10f)
100  {
101  rx = 0;
102  rz = -std::atan2(m(0, 1), m(1, 1));
103  }
104  else
105  {
106  float cb = 1.0f / std::cos(ry);
107  rx = std::atan2(m(2, 1) * cb, m(2, 2) * cb);
108  rz = std::atan2(m(1, 0) * cb, m(0, 0) * cb);
109  }
110 
111  static_cast<QDoubleSpinBox*>(widget->layout()->itemAt(0)->widget())->setValue(m(0, 3));
112  static_cast<QDoubleSpinBox*>(widget->layout()->itemAt(1)->widget())->setValue(m(1, 3));
113  static_cast<QDoubleSpinBox*>(widget->layout()->itemAt(2)->widget())->setValue(m(2, 3));
114 
115  static_cast<QDoubleSpinBox*>(widget->layout()->itemAt(4)->widget())->setValue(rx);
116  static_cast<QDoubleSpinBox*>(widget->layout()->itemAt(5)->widget())->setValue(ry);
117  static_cast<QDoubleSpinBox*>(widget->layout()->itemAt(6)->widget())->setValue(rz);
118  }
119 
121  {
122  QWidgetT* parent = static_cast<QWidget*>(widget->parent());
123 
125 
126  const float tx = static_cast<float>(static_cast<QDoubleSpinBox*>(parent->layout()->itemAt(0)->widget())->value());
127  const float ty = static_cast<float>(static_cast<QDoubleSpinBox*>(parent->layout()->itemAt(1)->widget())->value());
128  const float tz = static_cast<float>(static_cast<QDoubleSpinBox*>(parent->layout()->itemAt(2)->widget())->value());
129  const float rx = static_cast<float>(static_cast<QDoubleSpinBox*>(parent->layout()->itemAt(4)->widget())->value());
130  const float ry = static_cast<float>(static_cast<QDoubleSpinBox*>(parent->layout()->itemAt(5)->widget())->value());
131  const float rz = static_cast<float>(static_cast<QDoubleSpinBox*>(parent->layout()->itemAt(6)->widget())->value());
132 
133  const float sgamma = sinf(rx);
134  const float cgamma = cosf(rx);
135  const float sbeta = sinf(ry);
136  const float cbeta = cosf(ry);
137  const float salpha = sinf(rz);
138  const float calpha = cosf(rz);
139 
140  m(0, 0) = calpha * cbeta;
141  m(0, 1) = calpha * sbeta * sgamma - salpha * cgamma;
142  m(0, 2) = calpha * sbeta * cgamma + salpha * sgamma;
143  m(0, 3) = tx;
144 
145  m(1, 0) = salpha * cbeta;
146  m(1, 1) = salpha * sbeta * sgamma + calpha * cgamma;
147  m(1, 2) = salpha * sbeta * cgamma - calpha * sgamma;
148  m(1, 3) = ty;
149 
150  m(2, 0) = - sbeta;
151  m(2, 1) = cbeta * sgamma;
152  m(2, 2) = cbeta * cgamma;
153  m(2, 3) = tz;
154 
155  return m;
156  }
157  };
158 }
armarx::RemoteGui::PosRPYSpinBoxesHandler::createWidget
static QWidgetT * createWidget(RemoteWidgetT const &desc, CreateWidgetCallback const &createChild, const QObject *stateChangeReceiver, const char *stateChangeSlot)
Definition: Matrix4fWidgets.h:46
armarx::RemoteGui::PosRPYSpinBoxesHandler
Definition: Matrix4fWidgets.h:12
armarx::RemoteGui
Definition: LightweightRemoteGuiComponentPlugin.h:30
armarx::RemoteGui::TypedWidget
Definition: TypedWidget.h:115
armarx::RemoteGui::TypedWidget< PosRPYSpinBoxes, QWidget, VALUE_VARIANT_MATRIX4 >::ValueT
typename Storage< ValueType >::Type ValueT
Definition: TypedWidget.h:120
armarx::RemoteGui::PosRPYSpinBoxesHandler::updateGui
static void updateGui(RemoteWidgetT const &, QWidgetT *widget, ValueT const &m)
Definition: Matrix4fWidgets.h:89
ARMARX_TRACE
#define ARMARX_TRACE
Definition: trace.h:69
GfxTL::Identity
void Identity(MatrixXX< N, N, T > *a)
Definition: MatrixXX.h:523
cmp_or_log
#define cmp_or_log(l, c, r)
cxxopts::value
std::shared_ptr< Value > value()
Definition: cxxopts.hpp:926
M_PI
#define M_PI
Definition: MathTools.h:17
Basic.h
armarx::RemoteGui::TypedWidget< PosRPYSpinBoxes, QWidget, VALUE_VARIANT_MATRIX4 >::QWidgetT
QWidget QWidgetT
Definition: TypedWidget.h:118
armarx::RemoteGui::fromIce
Eigen::Vector3f fromIce(Vector3f v)
Definition: Storage.cpp:91
armarx::RemoteGui::PosRPYSpinBoxesHandler::isValid
static bool isValid(RemoteWidgetT const &desc, std::ostream &out)
Definition: Matrix4fWidgets.h:14
GfxTL::Matrix4f
MatrixXX< 4, 4, float > Matrix4f
Definition: MatrixXX.h:601
armarx::RemoteGui::PosRPYSpinBoxesHandler::handleGuiChange
static ValueT handleGuiChange(RemoteWidgetT const &, QWidgetT *widget)
Definition: Matrix4fWidgets.h:120
armarx::RemoteGui::CreateWidgetCallback
std::function< QWidgetPtr(WidgetPtr const &)> CreateWidgetCallback
Definition: WidgetHandler.h:20
armarx::RemoteGui::TypedWidget< PosRPYSpinBoxes, QWidget, VALUE_VARIANT_MATRIX4 >::RemoteWidgetT
PosRPYSpinBoxes RemoteWidgetT
Definition: TypedWidget.h:117