OpenWalnut  1.5.0dev
WMDataCreatorFibers.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 "WDataCreatorFiberParallel.h"
32 #include "WDataCreatorFiberRandom.h"
33 #include "WDataCreatorFiberSpiral.h"
34 #include "WDataCreatorFiberStar.h"
35 #include "WDataCreatorTorus.h"
36 #include "WMDataCreatorFibers.h"
37 #include "WMDataCreatorFibers.xpm"
38 #include "core/common/WAssert.h"
39 #include "core/common/WProgress.h"
40 #include "core/common/WStrategyHelper.h"
41 #include "core/dataHandler/WDataSetFibers.h"
42 #include "core/kernel/WKernel.h"
43 
45  WModule(),
46  m_strategy( "Dataset Creators", "Select one of the dataset creators and configure it to your needs.", NULL,
47  "Creator", "A list of all known creators." )
48 {
49  // add some strategies here
55 }
56 
58 {
59  // cleanup
61 }
62 
63 std::shared_ptr< WModule > WMDataCreatorFibers::factory() const
64 {
65  return std::shared_ptr< WModule >( new WMDataCreatorFibers() );
66 }
67 
68 const char** WMDataCreatorFibers::getXPMIcon() const
69 {
70  return datacreator_xpm;
71 }
72 
73 const std::string WMDataCreatorFibers::getName() const
74 {
75  return "Data Creator Fibers";
76 }
77 
78 const std::string WMDataCreatorFibers::getDescription() const
79 {
80  return "Allows the user to create fiber data sets providing a bunch of data creation schemes.";
81 }
82 
84 {
85  // initialize connectors
86  m_output = WModuleOutputData< WDataSetFibers >::createAndAdd( shared_from_this(), "out", "The data that has been created" );
87 
88  // call WModule's initialization
90 }
91 
93 {
94  m_propCondition = std::shared_ptr< WCondition >( new WCondition() );
95 
96  // grid transform information
97  m_origin = m_properties->addProperty( "Origin", "Coordinate of the origin (voxel 0,0,0).", WPosition( 0.0, 0.0, 0.0 ), m_propCondition );
98  m_size = m_properties->addProperty( "Size", "The size of the dataset along the X,Y, and Z axis in the OpenWalnut coordinate system.",
99  WPosition( 128.0, 128.0, 128.0 ), m_propCondition );
100 
101  // the seed
102  m_seed = m_properties->addProperty( "Seed", "The seed for the random numbers to create.", 0, m_propCondition );
103 
104  // how much fibs and verts?
105  m_numFibers = m_properties->addProperty( "Num Fibers", "The number of fibers to create.", 500, m_propCondition );
106  m_numFibers->setMin( 1 );
107  m_numFibers->setMax( 10000 );
108  m_numVertsPerFiber = m_properties->addProperty( "Num Vertices", "Vertices per fiber.", 1000, m_propCondition );
109  m_numVertsPerFiber->setMin( 1 );
110  m_numVertsPerFiber->setMax( 10000 );
111 
112  m_timeDependent = m_properties->addProperty( "Time dependent (experimental)",
113  "Vary data over time. This feature is <b>experimental</b>.",
114  false,
115  m_propCondition );
116 
117  m_fibColor = m_properties->addProperty( "Color", "Color for the fibers.", defaultColor::WHITE, m_propCondition );
118 
119  // now, setup the strategy helper.
120  m_properties->addProperty( m_strategy.getProperties() );
121 
122  // call WModule's initialization
124 }
125 
127 {
128  // let the main loop awake if the data changes or the properties changed.
129  m_moduleState.setResetable( true, true );
131  // we need to wake up if some strategy prop changed
132  m_moduleState.add( m_strategy.getProperties()->getUpdateCondition() );
133 
134  // signal ready state
135  ready();
136 
137  // loop until the module container requests the module to quit
138  while( !m_shutdownFlag() )
139  {
140  debugLog() << "Creating dataset";
141 
142  WProgress::SPtr progress( new WProgress( "Creating Dataset", m_numFibers->get() ) );
143  m_progress->addSubProgress( progress );
144 
145  // build structures for keeping the data
146  size_t numFibers = m_numFibers->get();
147  size_t numVertsPerFiber = m_numVertsPerFiber->get();
148  size_t numVerts = numVertsPerFiber * numFibers;
149 
150  WDataSetFibers::VertexArray vertices = WDataSetFibers::VertexArray( new WDataSetFibers::VertexArray::element_type() );
151  WDataSetFibers::IndexArray fibIdx = WDataSetFibers::IndexArray( new WDataSetFibers::IndexArray::element_type() );
152  WDataSetFibers::LengthArray lengths = WDataSetFibers::LengthArray( new WDataSetFibers::LengthArray::element_type() );
153  WDataSetFibers::IndexArray fibIdxVertexMap = WDataSetFibers::IndexArray( new WDataSetFibers::IndexArray::element_type() );
154  WDataSetFibers::ColorArray colors = WDataSetFibers::ColorArray( new WDataSetFibers::ColorArray::element_type() );
155  vertices->reserve( numVerts * 3 );
156  fibIdx->reserve( numFibers );
157  lengths->reserve( numFibers );
158  fibIdxVertexMap->reserve( numVerts );
159  colors->reserve( numVerts * 3 );
160 
161  // get the current strategy
162  m_strategy()->operator()( m_seed->get(),
163  progress,
164  m_fibColor->get(),
165  numFibers,
166  numVertsPerFiber,
167  m_origin->get(),
168  m_size->get(),
169  vertices, fibIdx, lengths, fibIdxVertexMap, colors );
170 
171  // build the dataset
172  WDataSetFibers::SPtr ds = WDataSetFibers::SPtr( new WDataSetFibers( vertices, fibIdx, lengths, fibIdxVertexMap ) );
173  ds->addColorScheme( colors, "Fiber Creator", "The color from Fiber Creator." );
174 
175  // done. Notify user.
176  debugLog() << "Created dataset with " << m_numFibers->get() << " fibers.";
177  progress->finish();
178  m_progress->removeSubProgress( progress );
179 
180  // done. update output
181  m_output->updateData( ds );
182 
183  if( m_timeDependent->get() )
184  {
185  m_size->set( WVector3d( m_size->get()[0], m_size->get()[1], static_cast<int>( m_size->get()[2] + 10 ) % 1000 ) );
186  std::this_thread::sleep_for( std::chrono::milliseconds( 100 ) );
187  }
188 
189  // Now, the moduleState variable comes into play. The module can wait for the condition, which gets fired whenever the input receives data
190  // or an property changes. The main loop now waits until something happens.
191  debugLog() << "Waiting ...";
193  }
194 }
195 
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
std::shared_ptr< WDataCreatorFiberParallel > SPtr
Abbreviate shared_ptr.
std::shared_ptr< WDataCreatorFiberRandom > SPtr
Abbreviate shared_ptr.
Create a fiber spiral.
std::shared_ptr< WDataCreatorFiberSpiral > SPtr
Abbreviate shared_ptr.
Create a fiber star.
std::shared_ptr< WDataCreatorFiberStar > SPtr
Abbreviate shared_ptr.
Create a torus.
std::shared_ptr< WDataCreatorTorus< T > > SPtr
Abbreviate shared_ptr.
Represents a simple set of WFibers.
std::shared_ptr< std::vector< size_t > > IndexArray
Index list indexing fibers in VertexArray in terms of vertex numbers.
std::shared_ptr< std::vector< float > > ColorArray
Colors for each vertex in VertexArray.
std::shared_ptr< std::vector< size_t > > LengthArray
Lengths of fibers in terms of vertices.
std::shared_ptr< WDataSetFibers > SPtr
Pointer to dataset.
std::shared_ptr< std::vector< float > > VertexArray
List of vertex coordinates in term of components of vertices.
WPropPosition m_size
Size of the fiber bounding box.
virtual void connectors()
Initialize the connectors this module is using.
WPropPosition m_origin
Origin of the bounding box.
virtual const std::string getName() const
Gives back the name of this module.
WPropInt m_numVertsPerFiber
Number of vertices per fiber.
~WMDataCreatorFibers()
Destructor.
virtual const std::string getDescription() const
Gives back a description of this module.
WPropBool m_timeDependent
Use time dependent variation of data (experimental).
WMDataCreatorFibers()
Standard constructor.
std::shared_ptr< WModuleOutputData< WDataSetFibers > > m_output
The only output of this module.
WPropInt m_numFibers
Number of fibers.
WStrategyHelper< WObjectNDIP< WDataSetFibersCreatorInterface > > m_strategy
the strategy currently active.
WPropColor m_fibColor
Fiber color.
std::shared_ptr< WCondition > m_propCondition
A condition used to notify about changes in several properties.
virtual const char ** getXPMIcon() const
Get the icon for this module in XPM format.
virtual void properties()
Initialize the properties for this module.
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...
WPropInt m_seed
The seed for the random.
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
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.