OpenWalnut  1.5.0dev
WMCalculateHistogram.cpp
1 //---------------------------------------------------------------------------
2 //
3 // Project: OpenWalnut ( http://www.openwalnut.org )
4 //
5 // Copyright 2014 OpenWalnut Community, Nemtics, BSV-Leipzig
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 #include <memory>
26 #include <string>
27 
28 #include "../../core/kernel/WKernel.h"
29 #include "WMCalculateHistogram.h"
30 
31 // This line is needed by the module loader to actually find your module. Do not remove. Do NOT add a ";" here.
32 W_LOADABLE_MODULE( WMCalculateHistogram )
33 
35  WModule()
36 {
37 }
38 
40 {
41 }
42 
43 std::shared_ptr< WModule > WMCalculateHistogram::factory() const
44 {
45  return std::shared_ptr< WModule >( new WMCalculateHistogram() );
46 }
47 
49 {
50  return NULL;
51 }
52 const std::string WMCalculateHistogram::getName() const
53 {
54  // Specify your module name here. This name must be UNIQUE!
55  return "CalculateHistogram";
56 }
57 
58 const std::string WMCalculateHistogram::getDescription() const
59 {
60  return "Calculates the histogram of the values found in the input dataset.";
61 }
62 
64 {
65  m_dataInput = std::shared_ptr< WModuleInputData< WDataSetSingle > >( new WModuleInputData< WDataSetSingle >(
66  shared_from_this(), "Input data", "A dataset to calculate the histogram from." ) );
68 
69  m_histogramOutput = std::shared_ptr< WModuleOutputData< WDataSetHistogram1D > >( new WModuleOutputData< WDataSetHistogram1D >(
70  shared_from_this(), "Histogram", "The calculated histogram." ) );
72 
74 }
75 
77 {
78  m_propCondition = std::shared_ptr< WCondition >( new WCondition );
79 
80  m_histogramBins = m_properties->addProperty( "Histogram bins", "The number of bins in the calculated histogram.", 100, m_propCondition );
81  m_histogramBins->setMin( 1 );
82  m_histogramBins->setMax( 1000 );
83 
85 }
86 
88 {
89 }
90 
92 {
93  m_moduleState.setResetable( true, true );
95  m_moduleState.add( m_dataInput->getDataChangedCondition() );
96 
97  ready();
98 
99  while( !m_shutdownFlag() )
100  {
101  debugLog() << "Waiting ...";
102 
104 
105  if( m_shutdownFlag() )
106  {
107  break;
108  }
109 
110  if( m_dataInput->getData() && m_data != m_dataInput->getData() )
111  {
112  m_data = m_dataInput->getData();
113 
114  updateOutput();
115  }
116 
117  if( m_data && m_histogramBins->changed() )
118  {
119  updateOutput();
120  }
121  }
122 }
123 
125 {
126  std::shared_ptr< WHistogramBasic > histo( new WHistogramBasic( m_data->getValueSet()->getMinimumValue(),
127  m_data->getValueSet()->getMaximumValue(),
128  m_histogramBins->get( true ) ) );
129  for( std::size_t j = 0; j < m_data->getValueSet()->size(); ++j )
130  {
131  histo->insert( m_data->getValueSet()->getScalarDouble( j ) );
132  }
133 
134  m_histogramOutput->updateData( std::shared_ptr< WDataSetHistogram1D >( new WDataSetHistogram1D( histo ) ) );
135 }
136 
virtual void wait() const
Wait for the condition.
void setResetable(bool resetable=true, bool autoReset=true)
Sets the resetable flag.
virtual void add(std::shared_ptr< WCondition > condition)
Adds another condition to the set of conditions to wait for.
Class to encapsulate boost::condition_variable_any.
Definition: WCondition.h:42
This data set type contains a 1D histogram.
Container which associate values with (uniform width) bins (aka intervals or buckets).
Calculates a histogram from WDataSetSingle.
virtual void connectors()
Initialize the connectors this module is using.
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...
std::shared_ptr< WCondition > m_propCondition
A property condition used to notify of changes in the properties.
virtual void moduleMain()
Entry point after loading the module.
virtual ~WMCalculateHistogram()
Destructor.
void updateOutput()
Calculate the histogram and update the output.
WPropInt m_histogramBins
A property for the number of bins in the histogram.
std::shared_ptr< WModuleInputData< WDataSetSingle > > m_dataInput
The input connector for the data.
virtual const char ** getXPMIcon() const
Get the icon for this module in XPM format.
std::shared_ptr< WModuleOutputData< WDataSetHistogram1D > > m_histogramOutput
The output for the histogram.
WMCalculateHistogram()
Constructor.
std::shared_ptr< WDataSetSingle > m_data
The current input data.
virtual const std::string getDescription() const
Gives back a description of this module.
virtual const std::string getName() const
Gives back the name of this module.
virtual void properties()
Initialize the properties for this module.
virtual void requirements()
Initialize requirements for this module.
Class offering an instantiate-able data connection between modules.
Class representing a single module of OpenWalnut.
Definition: WModule.h:72
virtual void properties()
Initialize properties in this function.
Definition: WModule.cpp:212
wlog::WStreamedLogger debugLog() const
Logger instance for comfortable debug logging.
Definition: WModule.cpp:575
void addConnector(std::shared_ptr< WModuleInputConnector > con)
Adds the specified connector to the list of inputs.
Definition: WModule.cpp:108
std::shared_ptr< WProperties > m_properties
The property object for the module.
Definition: WModule.h:640
void ready()
Call this whenever your module is ready and can react on property changes.
Definition: WModule.cpp:505
WConditionSet m_moduleState
The internal state of the module.
Definition: WModule.h:703
virtual void connectors()
Initialize connectors in this function.
Definition: WModule.cpp:208
WBoolFlag m_shutdownFlag
Condition getting fired whenever the thread should quit.