OpenWalnut  1.5.0dev
WMTransferFunction1D.cpp
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 #include <memory>
26 #include <string>
27 #include <vector>
28 
29 #include "WMTransferFunction1D.h"
30 #include "WMTransferFunction1D.xpm"
31 #include "core/common/WTransferFunction.h"
32 #include "core/dataHandler/WDataSetScalar.h"
33 #include "core/dataHandler/WGrid.h"
34 #include "core/dataHandler/WGridRegular3D.h"
35 #include "core/dataHandler/WValueSet.h"
36 #include "core/dataHandler/WValueSetBase.h"
37 #include "core/dataHandler/datastructures/WValueSetHistogram.h"
38 #include "core/kernel/WKernel.h"
39 
40 // This line is needed by the module loader to actually find your module. Do not remove. Do NOT add a ";" here.
41 W_LOADABLE_MODULE( WMTransferFunction1D )
42 
43 WMTransferFunction1D::WMTransferFunction1D():
44  WModule()
45 {
46 }
47 
48 WMTransferFunction1D::~WMTransferFunction1D()
49 {
50  // Cleanup!
51 }
52 
53 std::shared_ptr< WModule > WMTransferFunction1D::factory() const
54 {
55  return std::shared_ptr< WModule >( new WMTransferFunction1D() );
56 }
57 
59 {
60  return WMTransferFunction1D_xpm;
61 }
62 
63 const std::string WMTransferFunction1D::getName() const
64 {
65  return "Transfer Function 1D";
66 }
67 
68 const std::string WMTransferFunction1D::getDescription() const
69 {
70  return "A module to modify a transfer function.";
71 }
72 
74 {
75  // the data set we use for displaying the histogram
77  "histogram input", "The data set used to display a histogram." );
78 
79  // an output connector for the transfer function created
81  "transferFunction1D", "The selected transfer function" );
82 
84 }
85 
87 {
88  m_propCondition = std::shared_ptr< WCondition >( new WCondition() );
90  tf.addAlpha( 0.0, 0.0 );
91  tf.addColor( 0.0, WColor( 0.0, 0.0, 1.0, 1.0 ) );
92  tf.addColor( 0.5, WColor( 0.0, 1.0, 0.0, 1.0 ) );
93  tf.addAlpha( 0.75, 0.25 );
94  tf.addAlpha( 1.0, 0.5 );
95  tf.addColor( 1.0, WColor( 1.0, 0.0, 0.0, 1.0 ) );
96  m_transferFunction = m_properties->addProperty( "Transfer Function", "The transfer function editor.", tf, m_propCondition, false );
97 
98  m_opacityScale = m_properties->addProperty( "Opacity Scaling",
99  "Factor used to scale opacity for easier interaction",
100  1.0,
101  m_propCondition );
102  m_opacityScale->setMin( 0 );
103  m_opacityScale->setMax( 1 );
104 
105  m_binSize = m_properties->addProperty( "Histogram Resolution", "Number of bins in histogram.", 64, m_propCondition );
106  m_binSize->setMin( 2 );
107  m_binSize->setMax( 512 );
108 
109  m_resolution = m_properties->addProperty( "Number of Samples",
110  "Number of samples in the transfer function. "
111  "Some modules connected to the output may have additional restrictions. Volume rendering, e.g., requires a power of two "
112  "samples at the moment."
113  , 128, m_propCondition );
114  m_resolution->setMin( 4 );
115  m_resolution->setMax( 1024 );
117 }
118 
120 {
121  //m_requirements.push_back( new WGERequirement() );
122 }
123 
125 {
126  m_moduleState.setResetable( true, true );
129 
130  ready();
131 
132  while( !m_shutdownFlag() )
133  {
135 
136  if( m_shutdownFlag() )
137  break;
138 
139  bool tfChanged = m_transferFunction->changed();
140  WTransferFunction tf = m_transferFunction->get( true );
141  debugLog() << "Current transfer function " << tf.numAlphas() << " alphas.";
142  if( m_input->updated() || m_binSize->changed() )
143  {
144  std::shared_ptr< WDataSetSingle > dataSet= m_input->getData();
145  bool dataValid = ( dataSet != NULL );
146  if( !dataValid )
147  {
148  // FIXME: invalidate histogram in GUI
149  tf.removeHistogram();
150  }
151  else
152  {
153  int binsize = m_binSize->get( true );
154 
155  std::shared_ptr< WValueSetBase > values = dataSet->getValueSet();
156  WValueSetHistogram histogram( values, 0, 255, binsize );
157 
158  // because of the current semantics of WTransferFunction,
159  // we have to create a copy of the data here.
160  std::vector<double> vhistogram( histogram.size() );
161  for( int i = 0; i < binsize; ++i )
162  {
163  vhistogram[ i ] = histogram[ i ];
164  }
165 
166  tf.setHistogram( vhistogram );
167  }
168 
169  // either way, we changed the data and want to update the TF
170  m_transferFunction->set( tf );
171  }
172 
173  if( m_resolution->changed() || tfChanged || m_opacityScale->changed() )
174  {
175  // debugLog() << "resampling transfer function";
176  unsigned int resolution = m_resolution->get( true );
177  // debugLog() << "new resolution: " << resolution;
178  std::shared_ptr< std::vector<unsigned char> > data( new std::vector<unsigned char>( resolution * 4 ) );
179 
180  // FIXME: get transfer function and publish the function
181  tf.setOpacityScale( m_opacityScale->get( true ) );
182  tf.sample1DTransferFunction( &( *data )[ 0 ], resolution, 0.0, 1.0 );
183 
184  std::shared_ptr< WValueSetBase > newValueSet( new WValueSet<unsigned char>( 1, 4, data, W_DT_UNSIGNED_CHAR ) );
185 
186  WGridTransformOrtho transform;
187  std::shared_ptr< WGridRegular3D > newGrid( new WGridRegular3D( resolution, 1, 1, transform ) );
188  std::shared_ptr< WDataSetSingle > newData( new WDataSetSingle( newValueSet, newGrid ) );
189  m_output->updateData( newData );
190  }
191  }
192 }
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
A data set consisting of a set of values based on a grid.
A grid that has parallelepiped cells which all have the same proportion.
Implements an orthogonal grid transformation.
A module to provide 1D transfer functions, e.g., for volume rendering.
virtual void connectors()
Initialize the connectors this module is using.
virtual void requirements()
Initialize requirements for this module.
WPropTransferFunction m_transferFunction
The transfer function property.
virtual const std::string getDescription() const
Gives back a description of this module.
virtual void properties()
Initialize the properties for this module.
WPropInt m_binSize
Histogram bin size.
WPropDouble m_opacityScale
Histogram bin size.
WModuleOutputData< WDataSetSingle >::SPtr m_output
The output connector used to provide the calculated data to other modules.
virtual void moduleMain()
Entry point after loading the module.
WPropInt m_resolution
Resolution of the transfer function.
WModuleInputData< WDataSetSingle >::SPtr m_input
virtual const std::string getName() const
Gives back the name of this 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...
virtual const char ** getXPMIcon() const
Get the icon for this module in XPM format.
std::shared_ptr< WCondition > m_propCondition
A condition used to notify about changes in several properties.
std::shared_ptr< WCondition > getDataChangedCondition()
Gets the condition variable that gets fired whenever new data has been sent.
virtual bool updated()
Denotes whether the connected output was updated.
Class offering an instantiate-able data connection between modules.
const std::shared_ptr< T > getData(bool reset=true)
Gives the currently set data and resets the update flag.
Class offering an instantiate-able data connection between modules.
virtual void updateData(std::shared_ptr< T > data)
Update the data associated.
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
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.
A class that stores a 1D transfer function which consists of a linear interpolation of alpha and colo...
void setHistogram(std::vector< double > &data)
Set the histogram going along with the transfer function.
void setOpacityScale(double factor)
Set the scaling factor for the opacity.
void removeHistogram()
Clears the histogram data so the gui won't show any.
size_t numAlphas() const
Get the number of alphas.
void sample1DTransferFunction(unsigned char *array, int width, double min, double max) const
sample/render the transfer function linearly between min and max in an RGBA texture.
void addColor(double iso, const WColor &color)
Insert a new color point.
void addAlpha(double iso, double alpha)
Insert a new alpha point.
Used to find the occurrence frequencies of values in a value set.
virtual size_t size() const
Returns the number of buckets in the histogram with the actual mapping.
Base Class for all value set types.
Definition: WValueSet.h:47