toolbox_quaternion.cpp
Go to the documentation of this file.
1 #include "toolbox_quaternion.h"
2 #include "ui_quaternion_to_rpy.h"
3 
4 #include <QDialogButtonBox>
5 #include <QEvent>
6 #include <QMimeData>
7 #include <QDragEnterEvent>
8 #include <array>
9 #include <math.h>
10 #include "quaternion_to_rpy.h"
11 
13 {
14  _widget = new QWidget(nullptr);
15  ui = new Ui::quaternion_to_RPY;
16 
17  ui->setupUi(_widget);
18 
19  ui->lineEditX->installEventFilter(this);
20  ui->lineEditY->installEventFilter(this);
21  ui->lineEditZ->installEventFilter(this);
22  ui->lineEditW->installEventFilter(this);
23 
24  connect(ui->buttonBox, &QDialogButtonBox::rejected, this, &ToolboxQuaternion::onClosed);
25 
26  connect(ui->checkBoxUnwrap, &QCheckBox::toggled, this,
28 
29  connect(ui->radioButtonDegrees, &QRadioButton::toggled, this,
31 
32  connect(ui->pushButtonSave, &QPushButton::clicked, this,
34 }
35 
37 {
38 }
39 
41  PJ::TransformsMap& transform_map)
42 {
43  _plot_data = &src_data;
44  _transforms = &transform_map;
45 
46  _plot_widget = new PJ::PlotWidgetBase(ui->frame);
47 
48  auto preview_layout = new QHBoxLayout(ui->framePlotPreview);
49  preview_layout->setMargin(6);
50  preview_layout->addWidget(_plot_widget);
51 }
52 
53 std::pair<QWidget*, PJ::ToolboxPlugin::WidgetType>
55 {
57 }
58 
60 {
61  return true;
62 }
63 
64 bool ToolboxQuaternion::eventFilter(QObject* obj, QEvent* ev)
65 {
66  if (ev->type() == QEvent::DragEnter)
67  {
68  auto event = static_cast<QDragEnterEvent*>(ev);
69  const QMimeData* mimeData = event->mimeData();
70  QStringList mimeFormats = mimeData->formats();
71 
72  for (const QString& format : mimeFormats)
73  {
74  QByteArray encoded = mimeData->data(format);
75  QDataStream stream(&encoded, QIODevice::ReadOnly);
76 
77  if (format != "curveslist/add_curve")
78  {
79  return false;
80  }
81 
82  QStringList curves;
83  while (!stream.atEnd())
84  {
85  QString curve_name;
86  stream >> curve_name;
87  if (!curve_name.isEmpty())
88  {
89  curves.push_back(curve_name);
90  }
91  }
92  if (curves.size() != 1)
93  {
94  return false;
95  }
96 
97  _dragging_curve = curves.front();
98 
99  if (obj == ui->lineEditX || obj == ui->lineEditY || obj == ui->lineEditZ ||
100  obj == ui->lineEditW)
101  {
102  event->acceptProposedAction();
103  return true;
104  }
105  }
106  }
107  else if (ev->type() == QEvent::Drop)
108  {
109  auto lineEdit = qobject_cast<QLineEdit*>(obj);
110 
111  if (!lineEdit)
112  {
113  return false;
114  }
115  lineEdit->setText(_dragging_curve);
116 
117  if ((obj == ui->lineEditX && _dragging_curve.endsWith("x")) ||
118  (obj == ui->lineEditY && _dragging_curve.endsWith("y")) ||
119  (obj == ui->lineEditZ && _dragging_curve.endsWith("z")) ||
120  (obj == ui->lineEditW && _dragging_curve.endsWith("w")))
121  {
122  autoFill(_dragging_curve.left(_dragging_curve.size() - 1));
123  }
124  }
125 
126  return false;
127 }
128 
129 void ToolboxQuaternion::autoFill(QString prefix)
130 {
131  QStringList suffix = { "x", "y", "z", "w" };
132  std::array<QLineEdit*, 4> lineEdits = { ui->lineEditX, ui->lineEditY, ui->lineEditZ,
133  ui->lineEditW };
134  QStringList names;
135  for (int i = 0; i < 4; i++)
136  {
137  QString name = prefix + suffix[i];
138  auto it = _plot_data->numeric.find(name.toStdString());
139  if (it != _plot_data->numeric.end())
140  {
141  names.push_back(name);
142  }
143  }
144 
145  if (names.size() == 4)
146  {
147  for (int i = 0; i < 4; i++)
148  {
149  lineEdits[i]->setText(names[i]);
150  }
151  ui->lineEditOut->setText(prefix);
152  ui->pushButtonSave->setEnabled(true);
153 
155  }
156 }
157 
159 {
160  using namespace PJ;
161 
162  bool wrap = ui->checkBoxUnwrap->isChecked();
163  double unit_scale = ui->radioButtonDegrees->isChecked() ? (180.0 / M_PI) : 1.0;
164  auto transform = std::make_shared<QuaternionToRollPitchYaw>();
165 
166  std::vector<const PlotData*> src_data;
167  {
168  for (QLineEdit* line : { ui->lineEditX, ui->lineEditY, ui->lineEditZ, ui->lineEditW })
169  {
170  auto it = _plot_data->numeric.find(line->text().toStdString());
171  if (it == _plot_data->numeric.end())
172  {
173  return false;
174  }
175  src_data.push_back(&it->second);
176  }
177  }
178 
179  std::string prefix = ui->lineEditOut->text().toStdString();
180 
181  // remove previous cruves bvefore creating new one
183 
184  _preview_data_roll.reset(new PlotData(prefix + "roll", {}));
185  _preview_data_pitch.reset(new PlotData(prefix + "pitch", {}));
186  _preview_data_yaw.reset(new PlotData(prefix + "yaw", {}));
187 
188  std::vector<PlotData*> dst_vector = { _preview_data_roll.get(),
189  _preview_data_pitch.get(),
190  _preview_data_yaw.get() };
191  if (type == SAVE)
192  {
193  dst_vector[0] = &_plot_data->getOrCreateNumeric(prefix + "roll", {});
194  dst_vector[1] = &_plot_data->getOrCreateNumeric(prefix + "pitch", {});
195  dst_vector[2] = &_plot_data->getOrCreateNumeric(prefix + "yaw", {});
196  }
197 
198  transform->setData(_plot_data, src_data, dst_vector);
199  transform->setWarp(wrap);
200  transform->setScale(unit_scale);
201 
202  transform->calculate();
203 
204  if (type == PREVIEW)
205  {
207  for (auto dst_data : dst_vector)
208  {
209  _plot_widget->addCurve(dst_data->plotName(), *dst_data);
210  }
212  }
213 
214  if (type == SAVE)
215  {
216  _transforms->insert({ prefix + "RPY", transform });
217 
218  emit plotCreated(prefix + "roll");
219  emit plotCreated(prefix + "pitch");
220  emit plotCreated(prefix + "yaw");
221  }
222  return true;
223 }
224 
226 {
227  generateRPY(SAVE);
228 
229  ui->lineEditX->setText({});
230  ui->lineEditY->setText({});
231  ui->lineEditZ->setText({});
232  ui->lineEditW->setText({});
233  ui->lineEditOut->setText({});
235 
236  emit this->closed();
237 }
238 
240 {
241  if (ui->lineEditX->text().isEmpty() || ui->lineEditY->text().isEmpty() ||
242  ui->lineEditZ->text().isEmpty() || ui->lineEditW->text().isEmpty() ||
243  ui->lineEditOut->text().isEmpty())
244  {
245  ui->pushButtonSave->setEnabled(false);
246  return;
247  }
248  bool valid = generateRPY(PREVIEW);
249  ui->pushButtonSave->setEnabled(valid);
250 }
251 
253 {
254  emit this->closed();
255 }
ToolboxQuaternion::_preview_data_roll
std::unique_ptr< PlotData > _preview_data_roll
Definition: toolbox_quaternion.h:62
ToolboxQuaternion::_plot_data
PJ::PlotDataMapRef * _plot_data
Definition: toolbox_quaternion.h:58
ToolboxQuaternion::_dragging_curve
QString _dragging_curve
Definition: toolbox_quaternion.h:52
ToolboxQuaternion::onShowWidget
bool onShowWidget() override
Definition: toolbox_quaternion.cpp:59
backward::ColorMode::type
type
Definition: backward.hpp:3600
quaternion_to_rpy.h
ToolboxQuaternion::onParametersChanged
void onParametersChanged()
Definition: toolbox_quaternion.cpp:239
PJ::ToolboxPlugin::plotCreated
void plotCreated(std::string plot_name, bool is_custom=true)
PJ::PlotWidgetBase::removeAllCurves
virtual void removeAllCurves()
Definition: plotwidget_base.cpp:789
PJ::PlotWidgetBase::resetZoom
virtual void resetZoom()
Definition: plotwidget_base.cpp:175
PJ::TransformsMap
std::unordered_map< std::string, std::shared_ptr< TransformFunction > > TransformsMap
Definition: transform_function.h:85
PJ::ToolboxPlugin::FIXED
@ FIXED
Definition: toolbox_base.h:34
ToolboxQuaternion::SAVE
@ SAVE
Definition: toolbox_quaternion.h:69
M_PI
#define M_PI
Definition: qwt_math.h:56
PJ::PlotDataMapRef::numeric
TimeseriesMap numeric
Numerical timeseries.
Definition: plotdata.h:39
ToolboxQuaternion::init
void init(PJ::PlotDataMapRef &src_data, PJ::TransformsMap &transform_map) override
Definition: toolbox_quaternion.cpp:40
PJ::PlotWidgetBase::addCurve
virtual CurveInfo * addCurve(const std::string &name, PlotDataXY &src_data, QColor color=Qt::transparent)
Definition: plotwidget_base.cpp:391
ToolboxQuaternion::ToolboxQuaternion
ToolboxQuaternion()
Definition: toolbox_quaternion.cpp:12
ToolboxQuaternion::_transforms
PJ::TransformsMap * _transforms
Definition: toolbox_quaternion.h:60
PJ::PlotData
TimeseriesBase< double > PlotData
Definition: plotdata.h:18
ToolboxQuaternion::_preview_data_yaw
std::unique_ptr< PlotData > _preview_data_yaw
Definition: toolbox_quaternion.h:64
ToolboxQuaternion::eventFilter
bool eventFilter(QObject *obj, QEvent *event) override
Definition: toolbox_quaternion.cpp:64
toolbox_quaternion.h
ToolboxQuaternion::onClosed
void onClosed()
Definition: toolbox_quaternion.cpp:252
PJ::PlotDataMapRef::getOrCreateNumeric
PlotData & getOrCreateNumeric(const std::string &name, PlotGroup::Ptr group={})
Definition: plotdata.cpp:75
format
auto format(const text_style &ts, const S &format_str, const Args &... args) -> std::basic_string< Char >
Definition: color.h:543
ToolboxQuaternion::_preview_data_pitch
std::unique_ptr< PlotData > _preview_data_pitch
Definition: toolbox_quaternion.h:63
PJ
Definition: dataloader_base.h:16
ToolboxQuaternion::name
const char * name() const override
Name of the plugin type, NOT the particular instance.
Definition: toolbox_quaternion.h:25
ToolboxQuaternion::_widget
QWidget * _widget
Definition: toolbox_quaternion.h:47
ToolboxQuaternion::PREVIEW
@ PREVIEW
Definition: toolbox_quaternion.h:68
PJ::PlotDataMapRef
Definition: plotdata.h:34
ToolboxQuaternion::autoFill
void autoFill(QString prefix)
Definition: toolbox_quaternion.cpp:129
ToolboxQuaternion::~ToolboxQuaternion
~ToolboxQuaternion() override
Definition: toolbox_quaternion.cpp:36
ToolboxQuaternion::_plot_widget
PJ::PlotWidgetBase * _plot_widget
Definition: toolbox_quaternion.h:56
ToolboxQuaternion::ui
Ui::quaternion_to_RPY * ui
Definition: toolbox_quaternion.h:48
ToolboxQuaternion::on_pushButtonSave_clicked
void on_pushButtonSave_clicked()
Definition: toolbox_quaternion.cpp:225
ToolboxQuaternion::GenerateType
GenerateType
Definition: toolbox_quaternion.h:66
PJ::ToolboxPlugin::closed
void closed()
ToolboxQuaternion::providedWidget
std::pair< QWidget *, WidgetType > providedWidget() const override
Definition: toolbox_quaternion.cpp:54
ToolboxQuaternion::generateRPY
bool generateRPY(GenerateType type)
Definition: toolbox_quaternion.cpp:158
PJ::PlotWidgetBase
Definition: plotwidget_base.h:25


plotjuggler
Author(s): Davide Faconti
autogenerated on Mon Nov 11 2024 03:23:47