ccHistogramWindow.h 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. #pragma once
  2. //##########################################################################
  3. //# #
  4. //# CLOUDCOMPARE #
  5. //# #
  6. //# This program is free software; you can redistribute it and/or modify #
  7. //# it under the terms of the GNU General Public License as published by #
  8. //# the Free Software Foundation; version 2 or later of the License. #
  9. //# #
  10. //# This program is distributed in the hope that it will be useful, #
  11. //# but WITHOUT ANY WARRANTY; without even the implied warranty of #
  12. //# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
  13. //# GNU General Public License for more details. #
  14. //# #
  15. //# COPYRIGHT: EDF R&D / TELECOM ParisTech (ENST-TSI) #
  16. //# #
  17. //##########################################################################
  18. //Always first
  19. #include <ccIncludeGL.h>
  20. //Qt
  21. #include <QDialog>
  22. //qCC_db
  23. #include <ccScalarField.h>
  24. //QCustomPlot
  25. #include "ccQCustomPlot.h"
  26. class QCPArrow;
  27. class QCPBarsWithText;
  28. class QCPColoredBars;
  29. class QCPHiddenArea;
  30. class QCPTextElement;
  31. class Ui_HistogramDialog;
  32. //! Histogram widget
  33. class ccHistogramWindow : public QCustomPlot
  34. {
  35. Q_OBJECT
  36. public:
  37. //! Default constructor
  38. explicit ccHistogramWindow(QWidget* parent = nullptr);
  39. //! Destructor
  40. virtual ~ccHistogramWindow();
  41. //! Sets title
  42. void setTitle(const QString& str);
  43. //! Sets axis labels
  44. void setAxisLabels(const QString& xLabel, const QString& yLabel);
  45. //! Computes histogram from a scalar field
  46. /** Number of classes can be freely modified afterwards (if enabled).
  47. \param sf associated scalar field
  48. \param initialNumberOfClasses initial number of classes
  49. \param numberOfClassesCanBeChanged whether to allow the user to modify the number of classes
  50. \param showNaNValuesInGrey show NaN values (in gray)
  51. **/
  52. void fromSF(ccScalarField* sf,
  53. unsigned initialNumberOfClasses = 0,
  54. bool numberOfClassesCanBeChanged = true,
  55. bool showNaNValuesInGrey = true);
  56. //! Creates histogram from a bin array (each bin = number of elements per class)
  57. /** Number of classes can't be modified.
  58. \param histoValues array of bins
  59. \param minVal minimum value
  60. \param maxVal maximum value
  61. **/
  62. void fromBinArray( const std::vector<unsigned>& histoValues,
  63. double minVal,
  64. double maxVal);
  65. //! Creates histogram from a bin array (each bin = number of elements per class)
  66. /** Number of classes can't be modified.
  67. \param histoValues array of bins
  68. \param sf associated scalar field
  69. **/
  70. void fromBinArray( const std::vector<unsigned>& histoValues,
  71. ccScalarField* sf );
  72. //! Sets overlay curve values
  73. /** The curve will only appear over an histogram
  74. \param curveValues curve points 'Y' coordinates only (regularly sampled between the min and max histogram values)
  75. **/
  76. void setCurveValues(const std::vector<double>& curveValues);
  77. //! Color scheme
  78. enum HISTOGRAM_COLOR_SCHEME
  79. {
  80. USE_SOLID_COLOR,
  81. USE_CUSTOM_COLOR_SCALE,
  82. USE_SF_SCALE
  83. };
  84. //! Sets how the gradient bars should be colored
  85. void setColorScheme(HISTOGRAM_COLOR_SCHEME scheme) { m_colorScheme = scheme; }
  86. //! Sets solid color
  87. /** Only used if color scheme is set to USE_SOLID_COLOR. **/
  88. void setSolidColor(QColor color) { m_solidColor = color; }
  89. //! Sets gradient color scale
  90. /** Only used if color scheme is set to USE_CUSTOM_COLOR_SCALE. **/
  91. void setColorScale(ccColorScale::Shared scale) { m_colorScale = scale; }
  92. //! Clears the display
  93. void clear();
  94. //! Updates the display
  95. void refresh();
  96. //! Updates the histogram bars only
  97. /** Only works if a SF is associated and color scheme is USE_SF_SCALE.
  98. **/
  99. void refreshBars();
  100. //! Returns the current histogram bins
  101. inline const std::vector<unsigned>& histoValues() const { return m_histoValues; }
  102. //! Returns the current histogram min value
  103. inline double minVal() const { return m_minVal; }
  104. //! Returns the current histogram max value
  105. inline double maxVal() const { return m_maxVal; }
  106. public: //Axis label display Options
  107. //! Axis display option
  108. enum class AxisDisplayOption
  109. {
  110. None = 0x0,
  111. XAxis = 0x01,
  112. YAxis = 0x02,
  113. All = XAxis | YAxis
  114. };
  115. Q_DECLARE_FLAGS(AxisDisplayOptions, AxisDisplayOption)
  116. //! SF interactor mode
  117. enum class SFInteractionMode
  118. {
  119. None = 0x0,
  120. DisplayRange = 0x01,
  121. SaturationRange = 0x02,
  122. All = DisplayRange | SaturationRange
  123. };
  124. Q_DECLARE_FLAGS(SFInteractionModes, SFInteractionMode)
  125. //! Enables SF interaction mode
  126. void setSFInteractionMode(SFInteractionModes modes);
  127. void setAxisDisplayOption(AxisDisplayOptions axisOptions);
  128. //! Used to disable automatic refresh after resize event
  129. /** Must refresh manually from client code if this is set to false **/
  130. void setRefreshAfterResize(bool refreshAfterResize);
  131. void setMinDispValue(double);
  132. void setMaxDispValue(double);
  133. void setMinSatValue(double);
  134. void setMaxSatValue(double);
  135. Q_SIGNALS:
  136. void sfMinDispValChanged(double);
  137. void sfMaxDispValChanged(double);
  138. void sfMinSatValChanged(double);
  139. void sfMaxSatValChanged(double);
  140. protected: //methods
  141. //! Changes the current number of classes
  142. /** Warning: n should be a multiple of 4.
  143. **/
  144. void setNumberOfClasses(size_t n);
  145. //mouse events handling
  146. void mousePressEvent(QMouseEvent *event);
  147. void mouseMoveEvent(QMouseEvent *event);
  148. void wheelEvent(QWheelEvent* event);
  149. void resizeEvent(QResizeEvent * event);
  150. //! Returns current maximum bin size
  151. unsigned getMaxHistoVal();
  152. //! Clears internal structures
  153. void clearInternal();
  154. //! Dynamically computes histogram bins from scalar field
  155. bool computeBinArrayFromSF(size_t binCount);
  156. //! Updates overlay curve width depending on the widget display size
  157. void updateOverlayCurveWidth(int w, int h);
  158. protected: //attributes
  159. //Title
  160. QString m_titleStr;
  161. QCPTextElement* m_titlePlot;
  162. //! Color scheme
  163. HISTOGRAM_COLOR_SCHEME m_colorScheme;
  164. //! Solid color
  165. QColor m_solidColor;
  166. //! Gradient color scale
  167. ccColorScale::Shared m_colorScale;
  168. //! Associated scalar field
  169. ccScalarField* m_associatedSF;
  170. //Whether the number of classes can be changed or not
  171. /** Only possible with an associated scalar field.
  172. **/
  173. bool m_numberOfClassesCanBeChanged;
  174. bool m_refreshAfterResize;
  175. //histogram data
  176. QCPColoredBars* m_histogram;
  177. std::vector<unsigned> m_histoValues;
  178. double m_minVal;
  179. double m_maxVal;
  180. unsigned m_maxHistoVal;
  181. //! Overlay curve
  182. QCPGraph* m_overlayCurve;
  183. std::vector<double> m_curveValues;
  184. //vertical indicator
  185. QCPBarsWithText* m_vertBar;
  186. bool m_drawVerticalIndicator;
  187. double m_verticalIndicatorPositionPercent;
  188. //! Rendering font
  189. QFont m_renderingFont;
  190. AxisDisplayOptions m_axisDisplayOptions;
  191. protected: //SF interactor mode
  192. //! Which SF interaction modes are enabled
  193. SFInteractionModes m_sfInteractionModes;
  194. //! Selectable items in "SF interaction" mode
  195. enum SELECTABLE_ITEMS
  196. {
  197. NONE,
  198. LEFT_AREA,
  199. RIGHT_AREA,
  200. BOTH_AREAS,
  201. LEFT_ARROW,
  202. RIGHT_ARROW,
  203. BOTH_ARROWS
  204. };
  205. //! Currently selected item
  206. SELECTABLE_ITEMS m_selectedItem;
  207. //! Left greyed area
  208. QCPHiddenArea* m_areaLeft;
  209. double m_areaLeftlastValue;
  210. //! Right greyed area
  211. QCPHiddenArea* m_areaRight;
  212. double m_areaRightlastValue;
  213. //! Left arrow
  214. QCPArrow* m_arrowLeft;
  215. double m_arrowLeftlastValue;
  216. //! Right arrow
  217. QCPArrow* m_arrowRight;
  218. double m_arrowRightlastValue;
  219. //! Last mouse click
  220. QPoint m_lastMouseClick;
  221. };
  222. //! Encapsulating dialog for ccHistogramWindow
  223. class ccHistogramWindowDlg : public QDialog
  224. {
  225. Q_OBJECT
  226. public:
  227. //! Default constructor
  228. explicit ccHistogramWindowDlg(QWidget* parent = nullptr);
  229. //! Destructor
  230. virtual ~ccHistogramWindowDlg();
  231. //! Returns encapsulated ccHistogramWindow
  232. inline ccHistogramWindow* window() { return m_win; }
  233. //! Exports histogram to a CSV file
  234. bool exportToCSV(QString filename) const;
  235. protected:
  236. //! When the export to CSV file button is pressed
  237. void onExportToCSV();
  238. //! When the export to Image file button is pressed
  239. void onExportToImage();
  240. protected:
  241. //Associated histogram window
  242. ccHistogramWindow* m_win;
  243. //! Associated widgets
  244. Ui_HistogramDialog* m_gui;
  245. };