ccSetSFAsVec3Dlg.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. //##########################################################################
  2. //# #
  3. //# CLOUDCOMPARE #
  4. //# #
  5. //# This program is free software; you can redistribute it and/or modify #
  6. //# it under the terms of the GNU General Public License as published by #
  7. //# the Free Software Foundation; version 2 or later of the License. #
  8. //# #
  9. //# This program is distributed in the hope that it will be useful, #
  10. //# but WITHOUT ANY WARRANTY; without even the implied warranty of #
  11. //# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
  12. //# GNU General Public License for more details. #
  13. //# #
  14. //# COPYRIGHT: CloudCompare project #
  15. //# #
  16. //##########################################################################
  17. #include "ccSetSFAsVec3Dlg.h"
  18. // qCC_db
  19. #include <ccPointCloud.h>
  20. ccSetSFsAsVec3Dialog::ccSetSFsAsVec3Dialog( const ccPointCloud* cloud,
  21. const QString& xLabel,
  22. const QString& yLabel,
  23. const QString& zLabel,
  24. bool allowUnchanged,
  25. QWidget* parent/*=nullptr*/)
  26. : QDialog(parent, Qt::Tool)
  27. , Ui::SetSFsAsVec3Dialog()
  28. , m_constFields(0)
  29. {
  30. setupUi(this);
  31. QStringList fields{ "0", "1" };
  32. // default fields ('0 0 1' by default)
  33. int xIndex = SF_INDEX_ZERO;
  34. int yIndex = SF_INDEX_ZERO;
  35. int zIndex = SF_INDEX_ONE;
  36. if (allowUnchanged)
  37. {
  38. fields << tr("Unchanged");
  39. xIndex = SF_INDEX_UNCHANGED;
  40. yIndex = SF_INDEX_UNCHANGED;
  41. zIndex = SF_INDEX_UNCHANGED;
  42. }
  43. m_constFields = fields.size();
  44. if (cloud && cloud->hasScalarFields())
  45. {
  46. QString upperXLabel = xLabel.toUpper();
  47. QString upperYLabel = yLabel.toUpper();
  48. QString upperZLabel = zLabel.toUpper();
  49. for (unsigned i = 0; i < cloud->getNumberOfScalarFields(); ++i)
  50. {
  51. CCCoreLib::ScalarField* sf = cloud->getScalarField(i);
  52. if (sf)
  53. {
  54. QString sfName = QString::fromStdString(sf->getName());
  55. fields << sfName;
  56. sfName = sfName.toUpper();
  57. if (xIndex < m_constFields && sfName.contains(upperXLabel))
  58. {
  59. xIndex = static_cast<int>(i);
  60. }
  61. if (yIndex < m_constFields && sfName.contains(upperYLabel))
  62. {
  63. yIndex = static_cast<int>(i);
  64. }
  65. if (zIndex < m_constFields && sfName.contains(upperZLabel))
  66. {
  67. zIndex = static_cast<int>(i);
  68. }
  69. }
  70. }
  71. }
  72. else
  73. {
  74. ccLog::Warning("Cloud has no scalar field");
  75. }
  76. xComboBox->addItems(fields);
  77. yComboBox->addItems(fields);
  78. zComboBox->addItems(fields);
  79. setSFIndexes(xIndex, yIndex, zIndex);
  80. }
  81. int ccSetSFsAsVec3Dialog::toComboBoxIndex(int index) const
  82. {
  83. if (index >= 0)
  84. {
  85. // valid SF index
  86. index += m_constFields;
  87. if (index >= xComboBox->count())
  88. {
  89. // we probably have less SFs as before
  90. return 0;
  91. }
  92. else
  93. {
  94. return index;
  95. }
  96. }
  97. else if (index == ccSetSFsAsVec3Dialog::SF_INDEX_NO)
  98. {
  99. return -1;
  100. }
  101. else if (index == ccSetSFsAsVec3Dialog::SF_INDEX_ZERO)
  102. {
  103. return 0;
  104. }
  105. else if (index == ccSetSFsAsVec3Dialog::SF_INDEX_ONE)
  106. {
  107. return 1;
  108. }
  109. else if (index == ccSetSFsAsVec3Dialog::SF_INDEX_UNCHANGED)
  110. {
  111. if (m_constFields == 3)
  112. {
  113. return 2;
  114. }
  115. else
  116. {
  117. // we don't have a 'Unchanged' field anymore, let's switch to Zero by default
  118. return 0;
  119. }
  120. }
  121. else
  122. {
  123. ccLog::Warning("[ccSetSFsAsVec3Dialog] Invalid SF index " + QString::number(index));
  124. return -1;
  125. }
  126. }
  127. int ccSetSFsAsVec3Dialog::fromComboBoxIndex(int index) const
  128. {
  129. if (index == 0)
  130. {
  131. return ccSetSFsAsVec3Dialog::SF_INDEX_ZERO;
  132. }
  133. else if (index == 1)
  134. {
  135. return ccSetSFsAsVec3Dialog::SF_INDEX_ONE;
  136. }
  137. else if (index == 2 && m_constFields == 3)
  138. {
  139. return ccSetSFsAsVec3Dialog::SF_INDEX_UNCHANGED;
  140. }
  141. else if (index >= m_constFields)
  142. {
  143. // valid SF index
  144. return index - m_constFields;
  145. }
  146. else
  147. {
  148. return -1;
  149. }
  150. }
  151. void ccSetSFsAsVec3Dialog::setSFIndexes(int sf1Index, int sf2Index, int sf3Index)
  152. {
  153. xComboBox->setCurrentIndex(toComboBoxIndex(sf1Index));
  154. yComboBox->setCurrentIndex(toComboBoxIndex(sf2Index));
  155. zComboBox->setCurrentIndex(toComboBoxIndex(sf3Index));
  156. }
  157. void ccSetSFsAsVec3Dialog::getSFIndexes(int& sf1Index, int& sf2Index, int& sf3Index) const
  158. {
  159. sf1Index = fromComboBoxIndex(xComboBox->currentIndex());
  160. sf2Index = fromComboBoxIndex(yComboBox->currentIndex());
  161. sf3Index = fromComboBoxIndex(zComboBox->currentIndex());
  162. }