OpenWalnut  1.5.0dev
WMHistogramView.h
1 //---------------------------------------------------------------------------
2 //
3 // Project: OpenWalnut ( http://www.openwalnut.org )
4 //
5 // Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
6 // For more information see http://www.openwalnut.org/copying
7 //
8 // This file is part of OpenWalnut.
9 //
10 // OpenWalnut is free software: you can redistribute it and/or modify
11 // it under the terms of the GNU Lesser General Public License as published by
12 // the Free Software Foundation, either version 3 of the License, or
13 // (at your option) any later version.
14 //
15 // OpenWalnut is distributed in the hope that it will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 // GNU Lesser General Public License for more details.
19 //
20 // You should have received a copy of the GNU Lesser General Public License
21 // along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
22 //
23 //---------------------------------------------------------------------------
24 
25 #ifndef WMHISTOGRAMVIEW_H
26 #define WMHISTOGRAMVIEW_H
27 
28 #include <memory>
29 #include <string>
30 #include <vector>
31 
32 #include <boost/thread.hpp>
33 #include <osg/Geode>
34 
35 #include "core/common/WCounter.h"
36 #include "core/common/WHistogramBasic.h"
37 #include "core/common/math/linearAlgebra/WMatrixFixed.h"
38 #include "core/dataHandler/WDataSetHistogram1D.h"
39 #include "core/graphicsEngine/WGEGroupNode.h"
40 #include "core/kernel/WModule.h"
41 #include "core/kernel/WModuleInputData.h"
42 #include "core/kernel/WModuleOutputData.h"
43 #include "core/ui/WUIViewWidget.h"
44 
45 // TODO(reichenbach): choose color of frame and marker depending on the user's chosen window background color
46 /**
47  * \class WMHistogramView
48  *
49  * A module that draws a histogram.
50  *
51  * \ingroup modules
52  */
53 class WMHistogramView : public WModule
54 {
55 public:
56  /**
57  * Constuctor.
58  */
60 
61  /**
62  * Destructor.
63  */
64  virtual ~WMHistogramView();
65 
66  /**
67  * Gives back the name of this module.
68  * \return the module's name.
69  */
70  virtual const std::string getName() const;
71 
72  /**
73  * Gives back a description of this module.
74  * \return description to module.
75  */
76  virtual const std::string getDescription() const;
77 
78  /**
79  * Due to the prototype design pattern used to build modules, this method returns a new instance of this method. NOTE: it
80  * should never be initialized or modified in some other way. A simple new instance is required.
81  *
82  * \return the prototype used to create every module in OpenWalnut.
83  */
84  virtual std::shared_ptr< WModule > factory() const;
85 
86  /**
87  * Get the icon for this module in XPM format.
88  *
89  * \return The icon.
90  */
91  virtual const char** getXPMIcon() const;
92 
93 protected:
94  /**
95  * Entry point after loading the module. Runs in separate thread.
96  */
97  virtual void moduleMain();
98 
99  /**
100  * Initialize the connectors this module is using.
101  */
102  virtual void connectors();
103 
104  /**
105  * Initialize the properties for this module.
106  */
107  virtual void properties();
108 
109  /**
110  * Initialize requirements for this module.
111  */
112  virtual void requirements();
113 
114 private:
115  /**
116  * Called on every mouse move event from the custom widget.
117  *
118  * \note this runs in OSG thread.
119  * \param pos New mouse position.
120  */
121  void handleMouseMove( WVector2f pos );
122 
123  /**
124  * Called on every resize event from the custom widget.
125  *
126  * \note this runs in OSG thread.
127  * \param x X pos
128  * \param y Y pos
129  * \param width Widht
130  * \param height Height
131  */
132  void handleResize( int x, int y, int width, int height );
133 
134  /**
135  * Whenever a new info node is made this mutex should be used.
136  */
137  boost::mutex m_createInfoMutex;
138 
139  /**
140  * Whenever a redraw is made this mutex should be used.
141  */
142  boost::mutex m_redrawMutex;
143 
144  /**
145  * Redraws the histogram and add it to the main geode.
146  */
147  void redraw();
148 
149  /**
150  * This updates the maximum value of the histograms.
151  */
152  void updateHistogramMax();
153 
154  /**
155  * Finds a good size for the frame, depending on the chosen spacing for axis labels.
156  */
157  void calculateFrameSize();
158 
159  /**
160  * Finds a good position of the frame relative to the lower left corner of the window.
161  * Depends on the current frame size and the size of the text to be written left to/under the
162  * frame.
163  */
164  void calculateFramePosition();
165 
166  /**
167  * This simply calculates a histogram per dataset, where the bin sizes and positions are the same for
168  * each histogram.
169  */
170  void calculateHistograms();
171 
172  /**
173  * Creates the geometry for histogram bars.
174  */
175  void createGeometryBars();
176 
177  /**
178  * Creates the geometry for stairs (i.e. bars that are not filled).
179  */
180  void createGeometryStairs();
181 
182  /**
183  * Creates the geometry for curves.
184  */
185  void createGeometryCurves();
186 
187  /**
188  * Creates the geometry for the frame and the ticks/labels.
189  */
190  void createFrame();
191 
192  /**
193  * Writes the values of the currently selected histogram bin to the top right
194  * corner of the window. The selected bin is the one pointed to by the mouse cursor.
195  *
196  * Also adds a quad denoting the currently selected bin that is drawn behind the geometry.
197  *
198  * \param mousePos Current mouse position
199  */
200  void createInfo( WVector2f mousePos );
201 
202  /**
203  * This finds a suitable spacing of ticks to use for an axis with a certain length and
204  * value interval.
205  *
206  * \param intervalLength The length of the value interval to represent.
207  * \param availableSpace The space available (in pixels on screen) for the interval.
208  * \param textSize The estimated size of the text for every tick.
209  *
210  * \return A good distance between ticks to use for labeling an axis.
211  */
212  double findOptimalSpacing( double intervalLength, double availableSpace, double textSize );
213 
214  /**
215  * This transforms histogram space coordinates to window coordinates. Histogram coordinates
216  * range from the minimum to the maximum values of all histograms in x-direction and from
217  * 0 to the maximum bin value over all histograms (or the maximum bin value where the bins of all
218  * datasets are added; this is for cumulative histogram types).
219  *
220  * \note The frame size and position must be already calculated for this function to give meaningful results.
221  *
222  * \param v The input in histogram coords.
223  * \return The given position in window coords.
224  */
226 
227  /**
228  * This is the inverse of histogramSpaceToWindowSpace.
229  * \see histogramSpaceToWindowSpace
230  *
231  * \note The frame size and position must be already calculated for this function to give meaningful results.
232  *
233  * \param v The input in window coords.
234  * \return The given position in histogram coords.
235  */
237 
238  /**
239  * This simply prints a NYI message to the errorLog.
240  */
241  void createNothing();
242 
243  /**
244  * A vector of histograms, one histogram per input. Histograms may be empty if an input
245  * does not have a valid dataset.
246  */
247  std::shared_ptr< WHistogramBasic const > m_histogram;
248 
249  //! A condition for property updates.
250  std::shared_ptr< WCondition > m_propCondition;
251 
252  //! The width of the window.
254 
255  //! The height of the window.
257 
258  //! The position of the mouse cursor in window coordinates.
260 
261  //! Draws histogram bin info to the top right corner of the window.
262  osg::ref_ptr< osg::Geode > m_infoNode;
263 
264  //! Draws a marker showing the currently selected histogram bin.
265  osg::ref_ptr< osg::Geode > m_markerNode;
266 
267  //! Holds the reference to the custom widget used for displaying the histogram
269 
270  //! Draws the frame and ticks/labels.
271  osg::ref_ptr< osg::Geode > m_frameNode;
272 
273  //! The lower left corner of the histogram in histogram coordinates.
275 
276  //! The upper right corner of the histogram in histogram coordinates.
278 
279  //! The lower left vertex of the frame box in histogram coordinates.
281 
282  //! The upper right vertex of the frame box in histogram coordinates.
284 
285  //! The spacing between labels at the histogram axis in histogram coordinates.
287 
288  //! The space to the left and under the frame in window coordinates.
290 
291  //! The distance between the histogram frame and the top resp. right side of the window in relative window coordinates.
292  double const m_frameSize;
293 
294  //! The number of this WMHistogram instance. Used to generate a unique window title for every instance of this module.
296 
297  //! The instance counter used to get the instance ID.
299 
300  //! The scene node of the custom window. All geometry nodes are added as children of this node.
301  osg::ref_ptr< WGEGroupNode > m_mainNode;
302 
303  //! The input connector.
304  std::shared_ptr< WModuleInputData< WDataSetHistogram1D > > m_input;
305 
306  //! The histogram to show.
307  std::shared_ptr< WDataSetHistogram1D > m_data;
308 
309  //! The color properties for the dataset.
310  WPropColor m_color;
311 
312  // the next two implement a kind of strategy pattern; the index of the selected element
313  // is used as an index to the geometry functions vector
314  //! Allows to select which one of the geometry generation functions should be used.
315  WPropSelection m_styleSelection;
316 
317  //! A vector containing functions to use for histogram geometry generation.
318  std::vector< boost::function< void( void ) > > m_geometryFunctions;
319 };
320 
321 #endif // WMHISTOGRAMVIEW_H
322 
This is a simple but thread-safe counter.
Definition: WCounter.h:37
A module that draws a histogram.
void updateHistogramMax()
This updates the maximum value of the histograms.
osg::ref_ptr< osg::Geode > m_infoNode
Draws histogram bin info to the top right corner of the window.
std::shared_ptr< WHistogramBasic const > m_histogram
A vector of histograms, one histogram per input.
WVector2d histogramSpaceToWindowSpace(WVector2d const &v)
This transforms histogram space coordinates to window coordinates.
boost::mutex m_createInfoMutex
Whenever a new info node is made this mutex should be used.
virtual void connectors()
Initialize the connectors this module is using.
static WCounter m_instanceCounter
The instance counter used to get the instance ID.
WMHistogramView()
Constuctor.
WVector2d m_framePosition
The space to the left and under the frame in window coordinates.
int m_windowWidth
The width of the window.
WVector2d m_frameSpacing
The spacing between labels at the histogram axis in histogram coordinates.
WVector2d m_histogramLowerLeft
The lower left corner of the histogram in histogram coordinates.
osg::ref_ptr< osg::Geode > m_markerNode
Draws a marker showing the currently selected histogram bin.
osg::ref_ptr< WGEGroupNode > m_mainNode
The scene node of the custom window. All geometry nodes are added as children of this node.
void handleResize(int x, int y, int width, int height)
Called on every resize event from the custom widget.
virtual void requirements()
Initialize requirements for this module.
virtual const std::string getName() const
Gives back the name of this module.
WUIViewWidget::SPtr m_widget
Holds the reference to the custom widget used for displaying the histogram.
std::shared_ptr< WCondition > m_propCondition
A condition for property updates.
void createFrame()
Creates the geometry for the frame and the ticks/labels.
double const m_frameSize
The distance between the histogram frame and the top resp. right side of the window in relative windo...
void handleMouseMove(WVector2f pos)
Called on every mouse move event from the custom widget.
void calculateFramePosition()
Finds a good position of the frame relative to the lower left corner of the window.
void calculateHistograms()
This simply calculates a histogram per dataset, where the bin sizes and positions are the same for ea...
virtual void properties()
Initialize the properties for this module.
void calculateFrameSize()
Finds a good size for the frame, depending on the chosen spacing for axis labels.
void createGeometryStairs()
Creates the geometry for stairs (i.e.
osg::ref_ptr< osg::Geode > m_frameNode
Draws the frame and ticks/labels.
WVector2d windowSpaceToHistogramSpace(WVector2d const &v)
This is the inverse of histogramSpaceToWindowSpace.
void createNothing()
This simply prints a NYI message to the errorLog.
WVector2d m_histogramUpperRight
The upper right corner of the histogram in histogram coordinates.
std::vector< boost::function< void(void) > > m_geometryFunctions
A vector containing functions to use for histogram geometry generation.
virtual void moduleMain()
Entry point after loading the module.
virtual std::shared_ptr< WModule > factory() const
Due to the prototype design pattern used to build modules, this method returns a new instance of this...
void createGeometryBars()
Creates the geometry for histogram bars.
boost::mutex m_redrawMutex
Whenever a redraw is made this mutex should be used.
WVector2d m_mousePos
The position of the mouse cursor in window coordinates.
WVector2d m_frameUpperRight
The upper right vertex of the frame box in histogram coordinates.
int m_instanceID
The number of this WMHistogram instance. Used to generate a unique window title for every instance of...
int m_windowHeight
The height of the window.
WPropSelection m_styleSelection
Allows to select which one of the geometry generation functions should be used.
std::shared_ptr< WModuleInputData< WDataSetHistogram1D > > m_input
The input connector.
virtual const char ** getXPMIcon() const
Get the icon for this module in XPM format.
WVector2d m_frameLowerLeft
The lower left vertex of the frame box in histogram coordinates.
void createGeometryCurves()
Creates the geometry for curves.
virtual ~WMHistogramView()
Destructor.
WPropColor m_color
The color properties for the dataset.
virtual const std::string getDescription() const
Gives back a description of this module.
void redraw()
Redraws the histogram and add it to the main geode.
void createInfo(WVector2f mousePos)
Writes the values of the currently selected histogram bin to the top right corner of the window.
double findOptimalSpacing(double intervalLength, double availableSpace, double textSize)
This finds a suitable spacing of ticks to use for an axis with a certain length and value interval.
std::shared_ptr< WDataSetHistogram1D > m_data
The histogram to show.
A fixed size matrix class.
Definition: WMatrixFixed.h:150
Class representing a single module of OpenWalnut.
Definition: WModule.h:72
std::shared_ptr< WUIViewWidget > SPtr
Abbreviation for a shared pointer on a instance of this class.
Definition: WUIViewWidget.h:72