OpenWalnut  1.5.0dev
WMDataCreatorVector.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 
29 #include "WDataCreatorRandom.h"
30 #include "WDataCreatorSingleDirection.h"
31 #include "WDataCreatorSphere.h"
32 #include "WMDataCreatorVector.h"
33 #include "WMDataCreatorVector.xpm"
34 #include "core/common/WAssert.h"
35 #include "core/common/WProgress.h"
36 #include "core/common/WStrategyHelper.h"
37 #include "core/dataHandler/WDataSetVector.h"
38 #include "core/dataHandler/WGridRegular3D.h"
39 #include "core/kernel/WKernel.h"
40 
42  WModule(),
43  m_strategy( "Dataset Creators", "Select one of the dataset creators and configure it to your needs.", NULL,
44  "Creator", "A list of all known creators." )
45 {
46  // add some strategies here
49 
50  // NOTE: the sphere strategy does not support vectors -> if you want them, create a scalar sphere and derive from it.
51 }
52 
54 {
55  // cleanup
57 }
58 
59 std::shared_ptr< WModule > WMDataCreatorVector::factory() const
60 {
61  return std::shared_ptr< WModule >( new WMDataCreatorVector() );
62 }
63 
64 const char** WMDataCreatorVector::getXPMIcon() const
65 {
66  return WMDataCreatorVector_xpm;
67 }
68 
69 const std::string WMDataCreatorVector::getName() const
70 {
71  return "Data Creator Vector";
72 }
73 
74 const std::string WMDataCreatorVector::getDescription() const
75 {
76  return "Allows the user to create vector data sets on a regular grid by providing a bunch of data creation schemes.";
77 }
78 
80 {
81  // initialize connectors
82  m_output = WModuleOutputData< WDataSetVector >::createAndAdd( shared_from_this(), "out", "The data that has been created" );
83 
84  // call WModule's initialization
86 }
87 
89 {
90  m_propCondition = std::shared_ptr< WCondition >( new WCondition() );
91 
92  // how much voxels?
93  m_nbVoxelsX = m_properties->addProperty( "Voxels X", "The number of voxels in X direction.", 128, m_propCondition );
94  m_nbVoxelsX->setMin( 2 );
95  m_nbVoxelsX->setMax( 4096 );
96  m_nbVoxelsY = m_properties->addProperty( "Voxels Y", "The number of voxels in Y direction.", 128, m_propCondition );
97  m_nbVoxelsY->setMin( 2 );
98  m_nbVoxelsY->setMax( 4096 );
99  m_nbVoxelsZ = m_properties->addProperty( "Voxels Z", "The number of voxels in Z direction.", 128, m_propCondition );
100  m_nbVoxelsZ->setMin( 2 );
101  m_nbVoxelsZ->setMax( 4096 );
102 
103  // grid transform information
104  m_origin = m_properties->addProperty( "Origin", "Coordinate of the origin (voxel 0,0,0).", WPosition( 0.0, 0.0, 0.0 ), m_propCondition );
105  m_size = m_properties->addProperty( "Size", "The size of the dataset along the X,Y, and Z axis in the OpenWalnut coordinate system.",
106  WPosition( 128.0, 128.0, 128.0 ), m_propCondition );
107 
108  // now, setup the strategy helper.
109  m_properties->addProperty( m_strategy.getProperties() );
110 
111  // call WModule's initialization
113 }
114 
116 {
117  // let the main loop awake if the data changes or the properties changed.
118  m_moduleState.setResetable( true, true );
120  // we need to wake up if some strategy prop changed
121  m_moduleState.add( m_strategy.getProperties()->getUpdateCondition() );
122 
123  // signal ready state
124  ready();
125 
126  // loop until the module container requests the module to quit
127  while( !m_shutdownFlag() )
128  {
129  debugLog() << "Creating dataset";
130 
131  // create a new WGridRegular3D
132  WGridTransformOrtho transform( m_size->get( true ).x() / static_cast< double >( m_nbVoxelsX->get( true ) - 1 ), // NOLINT
133  // - its not std::transform
134  m_size->get( true ).y() / static_cast< double >( m_nbVoxelsY->get( true ) - 1 ),
135  m_size->get( true ).z() / static_cast< double >( m_nbVoxelsZ->get( true ) - 1 )
136  );
137  // apply transform to new origin
138  transform.translate( m_origin->get( true ) );
139  WGridRegular3D::SPtr grid( new WGridRegular3D( m_nbVoxelsX->get(), m_nbVoxelsY->get(), m_nbVoxelsZ->get(), transform ) );
140 
141  WProgress::SPtr progress( new WProgress( "Creating Dataset", grid->size() ) );
142  m_progress->addSubProgress( progress );
143 
144  // get the current strategy
145  WValueSetBase::SPtr valueSet = m_strategy()->operator()( progress, grid, 1, 3 );
146 
147  debugLog() << "Created dataset with " << grid->size() << " voxels.";
148 
149  // build dataset
150  WDataSetVector::SPtr ds( new WDataSetVector( valueSet, grid ) );
151 
152  // done. Notify user.
153  progress->finish();
154  m_progress->removeSubProgress( progress );
155 
156  // done. update output
157  m_output->updateData( ds );
158 
159  // Now, the moduleState variable comes into play. The module can wait for the condition, which gets fired whenever the input receives data
160  // or an property changes. The main loop now waits until something happens.
161  debugLog() << "Waiting ...";
163  }
164 }
165 
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 random values inside a given grid.
Sets all voxels to the same vector.
This data set type contains vectors as values.
std::shared_ptr< WDataSetVector > 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.
WMDataCreatorVector()
Standard constructor.
std::shared_ptr< WCondition > m_propCondition
A condition used to notify about changes in several properties.
~WMDataCreatorVector()
Destructor.
virtual void connectors()
Initialize the connectors this module is using.
WPropInt m_nbVoxelsX
number of voxels in x direction
virtual void properties()
Initialize the properties for this module.
WPropPosition m_size
where to put the origin
WPropInt m_nbVoxelsY
number of voxels in y direction
virtual const std::string getDescription() const
Gives back a description of this module.
std::shared_ptr< WModuleOutputData< WDataSetVector > > m_output
The only output of this module.
virtual const char ** getXPMIcon() const
Get the icon for this module in XPM format.
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 std::string getName() const
Gives back the name of this module.
WStrategyHelper< WObjectNDIP< WDataSetSingleCreatorInterface > > m_strategy
the strategy currently active.
virtual void moduleMain()
Entry point after loading the module.
WPropInt m_nbVoxelsZ
number of voxels in z direction
WPropPosition m_origin
where to put the origin
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