LVRCorrespondanceDialog.cpp
Go to the documentation of this file.
1 
35 #include "LVRPointCloudItem.hpp"
36 #include "LVRPickItem.hpp"
37 #include "LVRItemTypes.hpp"
38 
39 #include <QMessageBox>
40 #include <QFont>
41 #include <QFileDialog>
42 
43 #include <fstream>
44 using std::ifstream;
45 using std::ofstream;
46 
47 namespace lvr2
48 {
49 
51  m_treeWidget(treeWidget)
52 {
53  m_dialog = new QDialog(treeWidget);
54  m_ui = new Ui_CorrespondenceDialog;
55  m_ui->setupUi(m_dialog);
56 
57 
58  m_dataSelectionColor = QColor(0, 0, 255, 0); // Blue
59  m_modelSelectionColor = QColor(255, 255, 0, 0); // Yellow
60  m_defaultColor = QColor(255, 255, 255, 0);
61 
63 
64  m_ui->comboBoxModel->setAutoFillBackground( true );
65  m_ui->comboBoxModel->setStyleSheet("QComboBox { background-color: blue; } QComboBox QAbstractItemView {border: 2px solid darkgray; selection-background-color: lightgray;}");
66 
67 
68 
69  m_ui->comboBoxData->setAutoFillBackground( true );
70  m_ui->comboBoxData->setStyleSheet("QComboBox { background-color: yellow; }");
71 
72  QObject::connect(m_ui->comboBoxModel, SIGNAL(activated(int)), this, SLOT(updateModelSelection(int)));
73  QObject::connect(m_ui->comboBoxData, SIGNAL(activated(int)), this, SLOT(updateDataSelection(int)));
74  QObject::connect(m_ui->buttonNew, SIGNAL(pressed()), this, SLOT(insertNewItem()));
75  QObject::connect(m_ui->buttonDelete, SIGNAL(pressed()), this, SLOT(deleteItem()));
76  QObject::connect(m_ui->buttonLoad, SIGNAL(pressed()), this, SLOT(loadCorrespondences()));
77  QObject::connect(m_ui->buttonSave, SIGNAL(pressed()), this, SLOT(saveCorrespondences()));
78  QObject::connect(m_ui->buttonSwap, SIGNAL(pressed()), this, SLOT(swapItemPositions()));
79  QObject::connect(m_ui->buttonDelete, SIGNAL(pressed()), this, SLOT(deleteItem()));
80  QObject::connect(m_ui->treeWidget, SIGNAL(currentItemChanged (QTreeWidgetItem*, QTreeWidgetItem*)), this, SLOT(treeItemSelected(QTreeWidgetItem*, QTreeWidgetItem*)));
81 }
82 
84 {
85  // For some reason we have to hack here. Code below
86  // is not working properly
87  while(m_ui->treeWidget->topLevelItemCount ())
88  {
89  m_ui->treeWidget->selectAll();
90  deleteItem();
91  }
92 
93  /* NOT WORKING! Don't know why though...
94  QTreeWidgetItemIterator it( m_ui->treeWidget );
95  while(*it)
96  {
97  QTreeWidgetItem* item = *it;
98  if(item->type() == LVRPickItemType)
99  {
100  int index = m_ui->treeWidget->indexOfTopLevelItem(item);
101  LVRPickItem* i = static_cast<LVRPickItem*>(m_ui->treeWidget->takeTopLevelItem(index));
102  if(i->getArrow())
103  {
104  Q_EMIT(removeArrow(i->getArrow()));
105  }
106  //if(i) delete i;
107  }
108  ++it;
109  }
110  */
111 
112  Q_EMIT(disableCorrespondenceSearch());
113 }
114 
116 {
117  LVRPickItem* item = new LVRPickItem( m_ui->treeWidget);
118  QList<QTreeWidgetItem*> items = m_ui->treeWidget->selectedItems();
119 
120  // De-select all previously selected items
121  QList<QTreeWidgetItem*>::iterator it;
122  for(it = items.begin(); it != items.end(); it++)
123  {
124  m_ui->treeWidget->setItemSelected(*it,false);
125  }
126 
127  m_ui->treeWidget->addTopLevelItem(item);
128  m_ui->treeWidget->setItemSelected(item, true);
129  m_ui->treeWidget->setCurrentItem(item);
130 
131  Q_EMIT(enableCorrespondenceSearch());
132 }
133 
135 {
136  QList<QTreeWidgetItem*> items = m_ui->treeWidget->selectedItems();
137  if(items.size())
138  {
139  QTreeWidgetItem* it = items.first();
140  if(it->type() == LVRPickItemType)
141  {
142  int index = m_ui->treeWidget->indexOfTopLevelItem(it);
143  LVRPickItem* i = static_cast<LVRPickItem*>(m_ui->treeWidget->takeTopLevelItem(index));
144  if(i->getArrow())
145  {
146  Q_EMIT(removeArrow(i->getArrow()));
147  }
148  //if(i) delete i;
149  }
150  }
151 }
152 
154 {
155  // Clear contends
156  m_ui->comboBoxModel->clear();
157  m_ui->comboBoxData->clear();
158 
159  int index = 0;
160 
161  // Iterator over all items
162  QTreeWidgetItemIterator it(m_treeWidget);
163  while (*it)
164  {
165  if ( (*it)->type() == LVRPointCloudItemType)
166  {
167  QString text = (*it)->parent()->text(0);
168  m_ui->comboBoxData->addItem(text);
169  m_ui->comboBoxModel->addItem(text);
170 
171  if (index == 0)
172  {
173  m_ui->comboBoxModel->setCurrentText(text);
174  }
175  else if (index == 1)
176  {
177  m_ui->comboBoxData->setCurrentText(text);
178  }
179  index++;
180  }
181  ++it;
182  }
183  Q_EMIT(render());
184 }
185 
187 {
188  QString str = m_ui->comboBoxModel->currentText();
189  m_modelSelection = str;
190  QTreeWidgetItemIterator it(m_treeWidget);
191  while (*it)
192  {
193  if ( (*it)->type() == LVRPointCloudItemType )
194  {
195  LVRPointCloudItem* item = static_cast<LVRPointCloudItem*>(*it);
196  if(item->parent()->text(0) == str)
197  {
199  }
200  /* else if(item->parent()->text(0) != m_dataSelection)
201  {
202  item->setSelectionColor(m_defaultColor);
203  }*/
204  }
205  ++it;
206  }
207  Q_EMIT(render());
208 }
209 
211 {
212  QString str = m_ui->comboBoxData->currentText();
213  m_dataSelection = str;
214  QTreeWidgetItemIterator it(m_treeWidget);
215  while (*it)
216  {
217  if ( (*it)->type() == LVRPointCloudItemType )
218  {
219  LVRPointCloudItem* item = static_cast<LVRPointCloudItem*>(*it);
220  if(item->parent()->text(0) == str)
221  {
223  }
224  /* else if(item->parent()->text(0) != m_modelSelection)
225  {
226  item->setSelectionColor(m_defaultColor);
227  }*/
228  }
229  ++it;
230  }
231  Q_EMIT(render());
232 }
233 
235 {
236  return m_ui->checkBoxICP->isChecked();
237 }
238 
240 {
241  return m_ui->spinBoxEpsilon->value();
242 }
243 
245 {
246  return m_ui->spinBoxDistance->value();
247 }
248 
250 {
251  return m_ui->spinBoxIterations->value();
252 }
253 
255 {
256  delete m_ui;
257  delete m_dialog;
258  // TODO Auto-generated destructor stub
259 }
260 
262 {
263  QList<QTreeWidgetItem*> items = m_ui->treeWidget->selectedItems();
264  if(items.size())
265  {
266  QTreeWidgetItem* it = items.first();
267  if(it->type() == LVRPickItemType)
268  {
269  LVRPickItem* item = static_cast<LVRPickItem*>(it);
270 
271  // Handle arrow displayment: Check if an arrow exists,
272  // remove it from renderer and create a new one (and pray
273  // that there are no concurrency problems...
274  LVRVtkArrow* arrow = item->getArrow();
275  if(arrow)
276  {
277  Q_EMIT(removeArrow(arrow));
278  }
279  item->setStart(pos);
280  arrow = item->getArrow();
281  if(arrow)
282  {
283  Q_EMIT(addArrow(arrow));
284  }
285 
286 
287  }
288  }
289  else
290  {
291  /*QMessageBox msgBox;
292  msgBox.setText("No item to edit selected.");
293  msgBox.setInformativeText("Please select one or add a new item to the list. Press 'q' to quit pick mode.");
294  msgBox.setStandardButtons(QMessageBox::Ok);
295  msgBox.exec();*/
296  }
297 }
298 
300 {
301  QList<QTreeWidgetItem*> items = m_ui->treeWidget->selectedItems();
302  if(items.size())
303  {
304  QTreeWidgetItem* it = items.first();
305  if(it->type() == LVRPickItemType)
306  {
307  LVRPickItem* item = static_cast<LVRPickItem*>(it);
308 
309  // Handle arrow displayment: Check if an arrow existed,
310  // remove it from renderer and create a new one (and pray
311  // that there are no concurrency problems...
312  LVRVtkArrow* arrow = item->getArrow();
313  if(arrow)
314  {
315  Q_EMIT(removeArrow(arrow));
316  }
317  item->setEnd(pos);
318  arrow = item->getArrow();
319  if(arrow)
320  {
321  Q_EMIT(addArrow(arrow));
322  }
323  }
324  }
325  else
326  {
327  /*
328  QMessageBox msgBox;
329  msgBox.setText("No item to edit selected.");
330  msgBox.setInformativeText("Please select one or add a new item to the list. Press 'q' to quit pick mode.");
331  msgBox.setStandardButtons(QMessageBox::Ok);
332  msgBox.exec();*/
333  }
334 }
335 
337 {
338  QList<QTreeWidgetItem*> items = m_ui->treeWidget->selectedItems();
339  if(items.size())
340  {
341  QTreeWidgetItem* it = items.first();
342  if(it->type() == LVRPickItemType)
343  {
344  LVRPickItem* item = static_cast<LVRPickItem*>(it);
345  double* end = item->getEnd();
346  double* start = item->getStart();
347  item->setStart(end);
348  item->setEnd(start);
349  }
350  }
351 }
352 
354 {
355  QString fileName = QFileDialog::getSaveFileName(m_treeWidget,
356  tr("Save Correspondences"), "./", tr("Correspondence Files (*.cor)"));
357 
358  if(fileName != "")
359  {
360  ofstream outfile(fileName.toStdString().c_str());
361  QTreeWidgetItemIterator it(m_ui->treeWidget);
362  while(*it)
363  {
364  if( (*it)->type() == LVRPickItemType)
365  {
366  LVRPickItem* item = static_cast<LVRPickItem*>(*it);
367  double* start = item->getStart();
368  double* end = item->getEnd();
369  outfile << start[0] << " " << start[1] << " " << start[2] << " ";
370  outfile << end[0] << " " << end[1] << " " << end[2] << endl;
371  cout << start << " " << end << endl;
372  }
373  ++it;
374  }
375  outfile.close();
376  }
377 }
378 
380 {
381  QString fileName = QFileDialog::getOpenFileName(m_treeWidget,
382  tr("Load Correspondences"), "./", tr("Correspondence Files (*.cor)"));
383 
384  if(fileName != "")
385  {
386  ifstream infile(fileName.toStdString().c_str());
387  while(infile.good())
388  {
389  double* start = new double[3];
390  double* end = new double[3];
391  infile >> start[0] >> start[1] >> start[2];
392  infile >> end[0] >> end[1] >> end[2];
393  // Check if we reached Eof with last read
394  if(infile.good())
395  {
396  LVRPickItem* item = new LVRPickItem( m_ui->treeWidget);
397  item->setStart(start);
398  item->setEnd(end);
399 
400  LVRVtkArrow* arrow = item->getArrow();
401  if(arrow)
402  {
403  Q_EMIT(addArrow(arrow));
404  }
405 
406  m_ui->treeWidget->addTopLevelItem(item);
407  m_ui->treeWidget->setItemSelected(item, false);
408  m_ui->treeWidget->setCurrentItem(item);
409  }
410  }
411  // Disable search to prevent change of last loaded correspondence
412  // when the user clicks into the window
413  Q_EMIT(disableCorrespondenceSearch());
414  }
415 }
416 
417 void LVRCorrespondanceDialog::treeItemSelected(QTreeWidgetItem* current, QTreeWidgetItem* prev)
418 {
419  // Enable picking
420  Q_EMIT(enableCorrespondenceSearch());
421 
422  // Set color of current item to red
423  if(current)
424  {
425  if(current->type() == LVRPickItemType)
426  {
427  LVRPickItem* item = static_cast<LVRPickItem*>(current);
428  if(item->getArrow())
429  {
430  item->getArrow()->setTmpColor(1.0, 0.2, 0.2);
431  }
432  }
433  }
434 
435  // Reset previous item to default color
436  if(prev)
437  {
438  if(prev->type() == LVRPickItemType)
439  {
440  LVRPickItem* item = static_cast<LVRPickItem*>(prev);
441  if(item->getArrow())
442  {
443  item->getArrow()->restoreColor();
444  }
445  }
446  }
447  Q_EMIT(render());
448 }
449 
450 boost::optional<Transformf> LVRCorrespondanceDialog::getTransformation()
451 {
452  std::vector<std::pair<Eigen::Vector3f, Eigen::Vector3f>> pairs;
453  Eigen::Vector3f centroid_m = Eigen::Vector3f::Zero();
454  Eigen::Vector3f centroid_d = Eigen::Vector3f::Zero();
455 
456  QTreeWidgetItemIterator it(m_ui->treeWidget);
457  while (*it)
458  {
459  if( (*it)->type() == LVRPickItemType)
460  {
461  LVRPickItem* item = static_cast<LVRPickItem*>(*it);
462  if(item->getStart() && item->getEnd())
463  {
464  double* s = item->getStart();
465  double* e = item->getEnd();
466 
467  Vector3f start(s[0], s[1], s[2]);
468  Vector3f end(e[0], e[1], e[2]);
469 
470  centroid_m += start.cast<float>();
471  centroid_d += end.cast<float>();
472 
473  pairs.push_back(make_pair(start, end));
474  }
475  }
476  ++it;
477 
478  }
479 
480  if(pairs.size() > 3)
481  {
482  centroid_m /= pairs.size();
483  centroid_d /= pairs.size();
484 
485  Transformf matrix;
487  align.alignPoints(pairs, centroid_m, centroid_d, matrix);
488 
489  return boost::make_optional(matrix);
490  }
491  else
492  {
493  cout << "Need at least 4 corresponding points" << endl;
494  return boost::none;
495  }
496 }
497 
499 {
500  return m_ui->comboBoxModel->currentText();
501 }
502 
504 {
505  return m_ui->comboBoxData->currentText();
506 }
507 
508 } /* namespace lvr2 */
509 
510 
lvr2::LVRCorrespondanceDialog::deleteItem
void deleteItem()
Definition: LVRCorrespondanceDialog.cpp:134
lvr2::LVRVtkArrow::setTmpColor
void setTmpColor(double r, double g, double b)
Definition: LVRVtkArrow.cpp:145
lvr2::LVRPickItem
Definition: LVRPickItem.hpp:45
lvr2::LVRCorrespondanceDialog::m_dataSelection
QString m_dataSelection
Definition: LVRCorrespondanceDialog.hpp:99
lvr2::EigenSVDPointAlign::alignPoints
T alignPoints(SLAMScanPtr scan, Point3 **neighbors, const Vec3 &centroid_m, const Vec3 &centroid_d, Mat4 &align) const
Calculates the estimated Transformation to match a Data Pointcloud to a Model Pointcloud.
lvr2::LVRCorrespondanceDialog::swapItemPositions
void swapItemPositions()
Definition: LVRCorrespondanceDialog.cpp:336
lvr2::LVRCorrespondanceDialog::secondPointPicked
void secondPointPicked(double *)
Definition: LVRCorrespondanceDialog.cpp:299
lvr2::LVRCorrespondanceDialog::saveCorrespondences
void saveCorrespondences()
Definition: LVRCorrespondanceDialog.cpp:353
lvr2::LVRCorrespondanceDialog::m_treeWidget
QTreeWidget * m_treeWidget
Definition: LVRCorrespondanceDialog.hpp:95
LVRCorrespondanceDialog.hpp
lvr2::LVRPickItem::getEnd
double * getEnd()
Definition: LVRPickItem.cpp:83
LVRItemTypes.hpp
lvr2::LVRCorrespondanceDialog::m_defaultColor
QColor m_defaultColor
Definition: LVRCorrespondanceDialog.hpp:98
lvr2::LVRCorrespondanceDialog::getMaxIterations
int getMaxIterations()
Definition: LVRCorrespondanceDialog.cpp:249
lvr2::LVRCorrespondanceDialog::doICP
bool doICP()
Definition: LVRCorrespondanceDialog.cpp:234
lvr2::LVRVtkArrow
A wrapper class to generate arrow actors for vtk based on VTK's oriented arrow example.
Definition: LVRVtkArrow.hpp:49
lvr2::LVRCorrespondanceDialog::LVRCorrespondanceDialog
LVRCorrespondanceDialog(QTreeWidget *parent)
Definition: LVRCorrespondanceDialog.cpp:50
lvr2::LVRCorrespondanceDialog::getEpsilon
double getEpsilon()
Definition: LVRCorrespondanceDialog.cpp:239
lvr2::LVRPointCloudItem
Definition: LVRPointCloudItem.hpp:45
lvr2::LVRCorrespondanceDialog::disableCorrespondenceSearch
void disableCorrespondenceSearch()
lvr2::LVRCorrespondanceDialog::getDataName
QString getDataName()
Definition: LVRCorrespondanceDialog.cpp:503
lvr2::LVRPointCloudItem::setSelectionColor
void setSelectionColor(QColor &c)
Definition: LVRPointCloudItem.cpp:142
lvr2::LVRCorrespondanceDialog::~LVRCorrespondanceDialog
virtual ~LVRCorrespondanceDialog()
Definition: LVRCorrespondanceDialog.cpp:254
lvr2::LVRCorrespondanceDialog::render
void render()
lvr2::LVRPickItem::getArrow
LVRVtkArrow * getArrow()
Definition: LVRPickItem.cpp:108
lvr2::LVRCorrespondanceDialog::clearAllItems
void clearAllItems()
Definition: LVRCorrespondanceDialog.cpp:83
lvr2::LVRCorrespondanceDialog::treeItemSelected
void treeItemSelected(QTreeWidgetItem *, QTreeWidgetItem *)
Definition: LVRCorrespondanceDialog.cpp:417
lvr2::LVRPickItem::setStart
void setStart(double *start)
Definition: LVRPickItem.cpp:57
lvr2::LVRCorrespondanceDialog::getModelName
QString getModelName()
Definition: LVRCorrespondanceDialog.cpp:498
lvr2::LVRCorrespondanceDialog::enableCorrespondenceSearch
void enableCorrespondenceSearch()
lvr2::LVRCorrespondanceDialog::removeArrow
void removeArrow(LVRVtkArrow *)
lvr2::LVRPickItem::setEnd
void setEnd(double *end)
Definition: LVRPickItem.cpp:88
lvr2::LVRPickItem::getStart
double * getStart()
Definition: LVRPickItem.cpp:78
lvr2::Vector3f
Eigen::Vector3f Vector3f
Eigen 3D vector, single precision.
Definition: MatrixTypes.hpp:118
lvr2::LVRCorrespondanceDialog::updateModelSelection
void updateModelSelection(int)
Definition: LVRCorrespondanceDialog.cpp:186
lvr2::LVRCorrespondanceDialog::getTransformation
boost::optional< Transformf > getTransformation()
Definition: LVRCorrespondanceDialog.cpp:450
lvr2::LVRCorrespondanceDialog::m_modelSelection
QString m_modelSelection
Definition: LVRCorrespondanceDialog.hpp:100
lvr2::LVRPointCloudItemType
@ LVRPointCloudItemType
Definition: LVRItemTypes.hpp:41
lvr2::LVRCorrespondanceDialog::m_ui
Ui_CorrespondenceDialog * m_ui
Definition: LVRCorrespondanceDialog.hpp:91
lvr2::LVRCorrespondanceDialog::addArrow
void addArrow(LVRVtkArrow *)
lvr2::LVRPickItemType
@ LVRPickItemType
Definition: LVRItemTypes.hpp:44
lvr2::LVRCorrespondanceDialog::m_dialog
QDialog * m_dialog
Definition: LVRCorrespondanceDialog.hpp:92
lvr2::LVRCorrespondanceDialog::insertNewItem
void insertNewItem()
Definition: LVRCorrespondanceDialog.cpp:115
lvr2::LVRCorrespondanceDialog::m_dataSelectionColor
QColor m_dataSelectionColor
Definition: LVRCorrespondanceDialog.hpp:96
lvr2::Transformf
Transform< float > Transformf
4x4 single precision transformation matrix
Definition: MatrixTypes.hpp:68
lvr2
Definition: BaseBufferManipulators.hpp:39
lvr2::LVRCorrespondanceDialog::fillComboBoxes
void fillComboBoxes()
Definition: LVRCorrespondanceDialog.cpp:153
lvr2::LVRCorrespondanceDialog::firstPointPicked
void firstPointPicked(double *)
Definition: LVRCorrespondanceDialog.cpp:261
lvr2::LVRCorrespondanceDialog::m_modelSelectionColor
QColor m_modelSelectionColor
Definition: LVRCorrespondanceDialog.hpp:97
LVRPointCloudItem.hpp
lvr2::LVRVtkArrow::restoreColor
void restoreColor()
Definition: LVRVtkArrow.cpp:140
lvr2::EigenSVDPointAlign
Definition: EigenSVDPointAlign.hpp:46
kfusion::device::tr
__kf_device__ Vec3f tr(const float4 &v)
Definition: device.hpp:79
lvr2::LVRCorrespondanceDialog::updateDataSelection
void updateDataSelection(int)
Definition: LVRCorrespondanceDialog.cpp:210
lvr2::LVRCorrespondanceDialog::loadCorrespondences
void loadCorrespondences()
Definition: LVRCorrespondanceDialog.cpp:379
LVRPickItem.hpp
lvr2::LVRCorrespondanceDialog::getMaxDistance
double getMaxDistance()
Definition: LVRCorrespondanceDialog.cpp:244


lvr2
Author(s): Thomas Wiemann , Sebastian Pütz , Alexander Mock , Lars Kiesow , Lukas Kalbertodt , Tristan Igelbrink , Johan M. von Behren , Dominik Feldschnieders , Alexander Löhr
autogenerated on Wed Mar 2 2022 00:37:24