OpenWalnut  1.5.0dev
WMDataCreatorScalar.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 <algorithm>
26 #include <memory>
27 #include <string>
28 #include <thread>
29 #include <chrono>
30 
31 #include "WDataCreatorBreyzel5.h"
32 #include "WDataCreatorConstant.h"
33 #include "WDataCreatorLinearAscent.h"
34 #include "WDataCreatorRandom.h"
35 #include "WDataCreatorSphere.h"
36 #include "WDataCreatorTangle.h"
37 #include "WDataCreatorTuring.h"
38 #include "WMDataCreator.xpm"
39 #include "WMDataCreatorScalar.h"
40 #include "core/common/WAssert.h"
41 #include "core/common/WProgress.h"
42 #include "core/common/WStrategyHelper.h"
43 #include "core/dataHandler/WDataSetScalar.h"
44 #include "core/dataHandler/WGridRegular3D.h"
45 #include "core/kernel/WKernel.h"
46 
48  WModule(),
49  m_strategy( "Dataset Creators", "Select one of the dataset creators and configure it to your needs.", NULL,
50  "Creator", "A list of all known creators." )
51 {
52  // add some strategies here
60 }
61 
63 {
64  // cleanup
66 }
67 
68 std::shared_ptr< WModule > WMDataCreatorScalar::factory() const
69 {
70  return std::shared_ptr< WModule >( new WMDataCreatorScalar() );
71 }
72 
73 const char** WMDataCreatorScalar::getXPMIcon() const
74 {
75  return datacreator_xpm;
76 }
77 
78 const std::string WMDataCreatorScalar::getName() const
79 {
80  return "Data Creator Scalar";
81 }
82 
83 const std::string WMDataCreatorScalar::getDescription() const
84 {
85  return "Allows the user to create scalar data sets on a regular grid by providing a bunch of data creation schemes.";
86 }
87 
89 {
90  // initialize connectors
91  m_output = WModuleOutputData< WDataSetScalar >::createAndAdd( shared_from_this(), "out", "The data that has been created" );
92 
93  // call WModule's initialization
95 }
96 
98 {
99  m_propCondition = std::shared_ptr< WCondition >( new WCondition() );
100 
101  // how much voxels?
102  m_nbVoxelsX = m_properties->addProperty( "Voxels X", "The number of voxels in X direction.", 128, m_propCondition );
103  m_nbVoxelsX->setMin( 2 );
104  m_nbVoxelsX->setMax( 4096 );
105  m_nbVoxelsY = m_properties->addProperty( "Voxels Y", "The number of voxels in Y direction.", 128, m_propCondition );
106  m_nbVoxelsY->setMin( 2 );
107  m_nbVoxelsY->setMax( 4096 );
108  m_nbVoxelsZ = m_properties->addProperty( "Voxels Z", "The number of voxels in Z direction.", 128, m_propCondition );
109  m_nbVoxelsZ->setMin( 2 );
110  m_nbVoxelsZ->setMax( 4096 );
111 
112  // grid transform information
113  m_origin = m_properties->addProperty( "Origin", "Coordinate of the origin (voxel 0,0,0).", WPosition( 0.0, 0.0, 0.0 ), m_propCondition );
114  m_size = m_properties->addProperty( "Size", "The size of the dataset along the X,Y, and Z axis in the OpenWalnut coordinate system.",
115  WPosition( 128.0, 128.0, 128.0 ), m_propCondition );
116 
117  m_timeDependent = m_properties->addProperty( "Time dependent (experimental)",
118  "Vary data over time. This feature is <b>experimental</b>.",
119  false,
120  m_propCondition );
121 
122  // now, setup the strategy helper.
123  m_properties->addProperty( m_strategy.getProperties() );
124 
125  // call WModule's initialization
127 }
128 
130 {
131  // let the main loop awake if the data changes or the properties changed.
132  m_moduleState.setResetable( true, true );
134  // we need to wake up if some strategy prop changed
135  m_moduleState.add( m_strategy.getProperties()->getUpdateCondition() );
136 
137  // signal ready state
138  ready();
139 
140  // loop until the module container requests the module to quit
141  while( !m_shutdownFlag() )
142  {
143  debugLog() << "Creating dataset";
144 
145  // create a new WGridRegular3D
146  WGridTransformOrtho transform( m_size->get( true ).x() / static_cast< double >( m_nbVoxelsX->get( true ) - 1 ), // NOLINT
147  // - its not std::transform
148  m_size->get( true ).y() / static_cast< double >( m_nbVoxelsY->get( true ) - 1 ),
149  m_size->get( true ).z() / static_cast< double >( m_nbVoxelsZ->get( true ) - 1 )
150  );
151  // apply transform to new origin
152  transform.translate( m_origin->get( true ) );
153  WGridRegular3D::SPtr grid( new WGridRegular3D( m_nbVoxelsX->get(), m_nbVoxelsY->get(), m_nbVoxelsZ->get(), transform ) );
154 
155  WProgress::SPtr progress( new WProgress( "Creating Dataset", grid->size() ) );
156  m_progress->addSubProgress( progress );
157 
158  // get the current strategy
159  WValueSetBase::SPtr valueSet = m_strategy()->operator()( progress, grid, 0, 1 );
160 
161  debugLog() << "Created dataset with " << grid->size() << " voxels.";
162 
163  // build dataset
164  WDataSetScalar::SPtr ds( new WDataSetScalar( valueSet, grid ) );
165 
166  // done. Notify user.
167  progress->finish();
168  m_progress->removeSubProgress( progress );
169 
170  // done. update output
171  m_output->updateData( ds );
172 
173  if( m_timeDependent->get() )
174  {
175  m_size->set( WVector3d( m_size->get()[0], m_size->get()[1], static_cast<int>( m_size->get()[2] + 10 ) % 1000 ) );
176  std::this_thread::sleep_for( std::chrono::milliseconds( 100 ) );
177  }
178 
179  // Now, the moduleState variable comes into play. The module can wait for the condition, which gets fired whenever the input receives data
180  // or an property changes. The main loop now waits until something happens.
181  debugLog() << "Waiting ...";
183  }
184 }
185 
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
Creates a breyzel5 inside a given grid.
Creates constant values inside a given grid.
Creates linear increasing values inside a given grid.
Creates random values inside a given grid.
Creates a sphere inside a given grid.
Creates a tangle inside a given grid.
Creates a 3D turing pattern.
This data set type contains scalars as values.
std::shared_ptr< WDataSetScalar > SPtr
shared_ptr abbreviation
A grid that has parallelepiped cells which all have the same proportion.
std::shared_ptr< WGridRegular3DTemplate > SPtr
Convenience typedef for a std::shared_ptr< WGridRegular3DTemplate >.
Implements an orthogonal grid transformation.
void translate(VecType const &vec)
Translate by a vector.
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 void moduleMain()
Entry point after loading the module.
WMDataCreatorScalar()
Standard constructor.
virtual void properties()
Initialize the properties for this module.
virtual const std::string getDescription() const
Gives back a description of this module.
WPropPosition m_size
where to put the origin
WPropInt m_nbVoxelsY
number of voxels in y direction
WPropBool m_timeDependent
Use time dependent variation of data (experimental).
virtual const char ** getXPMIcon() const
Get the icon for this module in XPM format.
WPropInt m_nbVoxelsZ
number of voxels in z direction
std::shared_ptr< WModuleOutputData< WDataSetScalar > > m_output
The only output of this module.
virtual void connectors()
Initialize the connectors this module is using.
std::shared_ptr< WCondition > m_propCondition
A condition used to notify about changes in several properties.
WStrategyHelper< WObjectNDIP< WDataSetSingleCreatorInterface > > m_strategy
the strategy currently active.
virtual const std::string getName() const
Gives back the name of this module.
WPropPosition m_origin
where to put the origin
WPropInt m_nbVoxelsX
number of voxels in x direction
~WMDataCreatorScalar()
Destructor.
static PtrType createAndAdd(std::shared_ptr< WModule > module, std::string name="", std::string description="")
Convenience method to create a new instance of this out data connector with proper type and add it to...
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 removeConnectors()
Removes all connectors properly.
Definition: WModule.cpp:194
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
std::shared_ptr< WProgressCombiner > m_progress
Progress indicator used as parent for all progress' of this module.
Definition: WModule.h:652
virtual void connectors()
Initialize connectors in this function.
Definition: WModule.cpp:208
std::shared_ptr< WObjectNDIP > SPtr
Shared ptr to an instance.
Definition: WObjectNDIP.h:47
This only is a 3d double vector.
Class managing progress inside of modules.
Definition: WProgress.h:42
std::shared_ptr< WProgress > SPtr
Shared pointer on a WProgress.
Definition: WProgress.h:48
WProperties::SPtr getProperties() const
Get this strategy selectors properties.
void addStrategy(typename StrategyType::SPtr strategy)
Adds the given strategy to the list of all strategies.
WBoolFlag m_shutdownFlag
Condition getting fired whenever the thread should quit.
std::shared_ptr< WValueSetBase > SPtr
Shared pointer to an instance of this class.
Definition: WValueSetBase.h:65