OpenWalnut  1.5.0dev
WMDataCreatorPoints.cpp
1 //---------------------------------------------------------------------------
2 //
3 // Project: OpenWalnut ( http://www.openwalnut.org )
4 //
5 // Copyright 2022 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 "WDataCreatorTorus.h"
32 #include "WMDataCreatorPoints.h"
33 #include "WDataCreatorPointsRandom.h"
34 #include "WMDataCreatorPoints.xpm"
35 #include "core/common/WAssert.h"
36 #include "core/common/WProgress.h"
37 #include "core/common/WStrategyHelper.h"
38 #include "core/dataHandler/WDataSetFibers.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
48  // m_strategy.addStrategy( WDataCreatorFiberRandom::SPtr( new WDataCreatorFiberRandom() ) );
49  // m_strategy.addStrategy( WDataCreatorFiberSpiral::SPtr( new WDataCreatorFiberSpiral() ) );
50  // m_strategy.addStrategy( WDataCreatorFiberStar::SPtr( new WDataCreatorFiberStar() ) );
52 }
53 
55 {
56  // cleanup
58 }
59 
60 std::shared_ptr< WModule > WMDataCreatorPoints::factory() const
61 {
62  return std::shared_ptr< WModule >( new WMDataCreatorPoints() );
63 }
64 
65 const char** WMDataCreatorPoints::getXPMIcon() const
66 {
67  return datacreatorpoints_xpm;
68 }
69 
70 const std::string WMDataCreatorPoints::getName() const
71 {
72  return "Data Creator Points";
73 }
74 
75 const std::string WMDataCreatorPoints::getDescription() const
76 {
77  return "Allows the user to create point data sets providing a bunch of data creation schemes.";
78 }
79 
81 {
82  // initialize connectors
83  m_output = WModuleOutputData< WDataSetPoints >::createAndAdd( shared_from_this(), "out", "The data that has been created" );
84 
85  // call WModule's initialization
87 }
88 
90 {
91  m_propCondition = std::shared_ptr< WCondition >( new WCondition() );
92 
93  // grid transform information
94  m_origin = m_properties->addProperty( "Origin", "Coordinate of the origin (voxel 0,0,0).", WPosition( 0.0, 0.0, 0.0 ), m_propCondition );
95  m_size = m_properties->addProperty( "Size", "The size of the dataset along the X,Y, and Z axis in the OpenWalnut coordinate system.",
96  WPosition( 128.0, 128.0, 128.0 ), m_propCondition );
97 
98  m_seed = m_properties->addProperty( "Seed", "The seed for the random numbers to create.", 0, m_propCondition );
99 
100  m_numPoints = m_properties->addProperty( "Num Points", "The number of points to create.", 500, m_propCondition );
101  m_numPoints->setMin( 1 );
102  m_numPoints->setMax( 1000000 );
103 
104  m_timeDependent = m_properties->addProperty( "Time dependent (experimental)",
105  "Vary data over time. This feature is <b>experimental</b>.",
106  false,
107  m_propCondition );
108 
109  m_pointColor = m_properties->addProperty( "Color", "Color for the points.", defaultColor::WHITE, m_propCondition );
110 
111  // now, setup the strategy helper.
112  m_properties->addProperty( m_strategy.getProperties() );
113 
114  // call WModule's initialization
116 }
117 
119 {
120  // let the main loop awake if the data changes or the properties changed.
121  m_moduleState.setResetable( true, true );
123  // we need to wake up if some strategy prop changed
124  m_moduleState.add( m_strategy.getProperties()->getUpdateCondition() );
125 
126  // signal ready state
127  ready();
128 
129  // loop until the module container requests the module to quit
130  while( !m_shutdownFlag() )
131  {
132  debugLog() << "Creating dataset";
133 
134  WProgress::SPtr progress( new WProgress( "Creating Dataset", m_numPoints->get() ) );
135  m_progress->addSubProgress( progress );
136 
137  // build structures for keeping the data
138  size_t numPoints = m_numPoints->get();
139 
140  WDataSetPoints::VertexArray vertices = WDataSetPoints::VertexArray( new WDataSetPoints::VertexArray::element_type() );
141  WDataSetPoints::ColorArray colors = WDataSetPoints::ColorArray( new WDataSetPoints::ColorArray::element_type() );
142  vertices->reserve( numPoints * 3 );
143  colors->reserve( numPoints * 3 );
144 
145  // get the current strategy
146  m_strategy()->operator()( m_seed->get(),
147  progress,
148  m_pointColor->get(),
149  numPoints,
150  m_origin->get(),
151  m_size->get(),
152  vertices, colors );
153 
154  // build the dataset
155  WDataSetPoints::SPtr ds = WDataSetPoints::SPtr( new WDataSetPoints( vertices, colors ) );
156 
157  // done. Notify user.
158  debugLog() << "Created dataset with " << m_numPoints->get() << " points.";
159  progress->finish();
160  m_progress->removeSubProgress( progress );
161 
162  // done. update output
163  m_output->updateData( ds );
164 
165  // Now, the moduleState variable comes into play. The module can wait for the condition, which gets fired whenever the input receives data
166  // or an property changes. The main loop now waits until something happens.
167  debugLog() << "Waiting ...";
168 
169  if( m_timeDependent->get() )
170  {
171  m_size->set( WVector3d( m_size->get()[0], m_size->get()[1], static_cast<int>( m_size->get()[2] + 10 ) % 1000 ) );
172  std::this_thread::sleep_for( std::chrono::milliseconds( 100 ) );
173  }
174 
176  }
177 }
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< WDataCreatorPointsRandom > SPtr
Abbreviate shared_ptr.
Create a torus.
std::shared_ptr< WDataCreatorTorus< T > > SPtr
Abbreviate shared_ptr.
Dataset to store a bunch of points without order or topology.
std::shared_ptr< WDataSetPoints > SPtr
Pointer to dataset.
std::shared_ptr< std::vector< float > > ColorArray
Colors for each vertex in VertexArray.
std::shared_ptr< std::vector< float > > VertexArray
List of vertex coordinates in term of components of vertices.
WPropPosition m_size
Size of the point bounding box.
WStrategyHelper< WObjectNDIP< WDataSetPointsCreatorInterface > > m_strategy
the strategy currently active.
virtual const char ** getXPMIcon() const
Get the icon for this module in XPM format.
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...
WPropBool m_timeDependent
Use time dependent variation of data (experimental).
virtual const std::string getDescription() const
Gives back a description of this module.
WMDataCreatorPoints()
Standard constructor.
WPropInt m_numPoints
Number of points.
std::shared_ptr< WCondition > m_propCondition
A condition used to notify about changes in several properties.
virtual void properties()
Initialize the properties for this module.
WPropPosition m_origin
Origin of the bounding box.
virtual void moduleMain()
Entry point after loading the module.
std::shared_ptr< WModuleOutputData< WDataSetPoints > > m_output
The only output of this module.
virtual const std::string getName() const
Gives back the name of this module.
WPropColor m_pointColor
Point color.
WPropInt m_seed
The seed for the random.
~WMDataCreatorPoints()
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
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.