OpenWalnut  1.5.0dev
WTuringPatternCreator.h
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 #ifndef WTURINGPATTERNCREATOR_H
26 #define WTURINGPATTERNCREATOR_H
27 
28 #include <memory>
29 #include <vector>
30 
31 #include <boost/thread.hpp>
32 #include <core/common/WConditionSet.h>
33 #include <core/common/WCounter.h>
34 #include <core/common/WProgress.h>
35 #include <core/common/WThreadedRunner.h>
36 #include <core/graphicsEngine/WGETexture.h>
37 
38 /**
39  * Creates a pattern of evenly spaced dots via simulating a chemical reaction.
40  *
41  * The pattern is created on a regular 3D grid.
42  */
44 {
45 public:
46  /**
47  * Constructor.
48  *
49  * \param progress The progress indicator to use.
50  * \param numThreads The number of threads to use.
51  */
52  WTuringPatternCreator( std::shared_ptr< WProgress > const progress, std::size_t numThreads = boost::thread::hardware_concurrency() );
53 
54  /**
55  * Destructor.
56  */
58 
59  /**
60  * Creates the 3D pattern and writes it into a vector of floats. This vector can simply be added to a valueset.
61  *
62  * \param sizeX The size of the grid in x-direction.
63  * \param sizeY The size of the grid in y-direction.
64  * \param sizeZ The size of the grid in z-direction.
65  *
66  * \return The vector containing the pattern data.
67  */
68  std::shared_ptr< std::vector< float > > create( std::size_t sizeX, std::size_t sizeY, std::size_t sizeZ );
69 
70  /**
71  * Sets the spotsize parameter.
72  *
73  * \param size A value between 0 and 1.
74  */
75  void setSpotSize( float size );
76 
77  /**
78  * Sets the spot irregularity parameter.
79  *
80  * \param irr A value between 0 and 1.
81  */
82  void setSpotIrregularity( float irr );
83 
84  /**
85  * Sets the number of iterations for the chemical simulation that creates the pattern.
86  *
87  * \param iter The number of iterations.
88  */
89  void setNumIterations( std::size_t iter );
90 
91 private:
92  /**
93  * A thread calculating the reaction for a certain part of the domain.
94  *
95  * The thread is constructed once. Using the conditions provided to the constructor,
96  * we can synchronize the pattern threads and the main thread.
97  */
99  {
100  public:
101  /**
102  * Constructor.
103  *
104  * \param id The id of the thread. Must be smaller than max.
105  * \param max The number of threads.
106  * \param mainThreadContinueCondition The condition we use to tell the main thread we are done for the current iteration.
107  * \param waitForMainThreadCondition The condition used to wait for the main thread to prepare the next iteration.
108  * \param counter A counter used by all threads to signal how many have finished the current iteration.
109  */
110  PatternThread( std::size_t id, std::size_t max, std::shared_ptr< WCondition > const mainThreadContinueCondition,
111  std::shared_ptr< WCondition > const waitForMainThreadCondition, WCounter* const counter );
112 
113  /**
114  * Destructor.
115  */
116  ~PatternThread();
117 
118  /**
119  * Set the domain size.
120  *
121  * \param sizeX The size of the 3D regular domain in x-direction.
122  * \param sizeY The size of the 3D regular domain in y-direction.
123  * \param sizeZ The size of the 3D regular domain in z-direction.
124  */
125  void setDomainSize( std::size_t sizeX, std::size_t sizeY, std::size_t sizeZ );
126 
127  /**
128  * Set the spot factor.
129  *
130  * \param spotFactor The spot factor.
131  */
132  void setSpotFactor( float spotFactor );
133 
134  /**
135  * Set the diffusion constants.
136  *
137  * \param d1 First diffusion constant.
138  * \param d2 Second diffusion constant.
139  */
140  void setDiffusionConstants( float d1, float d2 );
141 
142  /**
143  * Set the pointers to the buffer all threads share.
144  *
145  * \param concentration1 The buffer for the concentration of the first substance.
146  * \param concentration2 The buffer for the concentration of the second substance.
147  * \param noise A constant buffer containing a random value per voxel.
148  * \param delta1 A buffer that will store the change in concentration for the first substance.
149  * \param delta2 A buffer that will store the change in concentration for the second substance.
150  */
151  void setBufferPointers( std::vector< float > const* concentration1, std::vector< float > const* concentration2,
152  std::vector< float > const* noise, std::vector< float >* delta1, std::vector< float >* delta2 );
153 
154  /**
155  * The entry point for the thread.
156  */
157  virtual void threadMain();
158 
159  /**
160  * Tells the thread to finish.
161  *
162  * Note that the waiting condition also needs to be notified for the thread to actually finish.
163  */
164  void finish();
165 
166  private:
167  //! The number of this thread.
168  std::size_t m_id;
169 
170  //! The maximum number of threads.
171  std::size_t m_maxThreads;
172 
173  //! The size of the domain in x-direction.
174  std::size_t m_sizeX;
175 
176  //! The size of the domain in y-direction.
177  std::size_t m_sizeY;
178 
179  //! The size of the domain in z-direction.
180  std::size_t m_sizeZ;
181 
182  //! The spot factor.
184 
185  //! The first diffusion constant.
187 
188  //! The second diffusion constant.
190 
191  //! The buffer for the concentration of the first substance.
192  std::vector< float > const* m_concentration1;
193 
194  //! The buffer for the concentration of the second substance.
195  std::vector< float > const* m_concentration2;
196 
197  //! A constant buffer containing a random value per voxel.
198  std::vector< float > const* m_noise;
199 
200  //! A buffer that will store the change in concentration for the first substance.
201  std::vector< float >* m_delta1;
202 
203  //! A buffer that will store the change in concentration for the second substance.
204  std::vector< float >* m_delta2;
205 
206  //! The condition used to tell the main thread we are done for this iteration.
207  std::shared_ptr< WCondition > m_mainThreadContinueCondition;
208 
209  //! The condition used by the main thread to notify we are ready to start the next iteration.
210  std::shared_ptr< WCondition > m_waitForMainThreadCondition;
211 
212  //! Counts the number of threads done with the current iteration.
214 
215  //! Whether we are to stop the thread.
216  bool m_stop;
217  };
218 
219  //! The number of threads to use.
220  std::size_t m_numThreads;
221 
222  //! The number of iterations for the simulation.
224 
225  //! The spot irregularity parameter.
227 
228  //! The spot size parameter.
229  float m_spotSize;
230 
231  //! The progress to increment.
232  std::shared_ptr< WProgress > m_progress;
233 };
234 
235 #endif // WTURINGPATTERNCREATOR_H
This is a simple but thread-safe counter.
Definition: WCounter.h:37
Base class for all classes needing to be executed in a separate thread.
A thread calculating the reaction for a certain part of the domain.
std::vector< float > * m_delta1
A buffer that will store the change in concentration for the first substance.
void setDomainSize(std::size_t sizeX, std::size_t sizeY, std::size_t sizeZ)
Set the domain size.
std::size_t m_id
The number of this thread.
std::vector< float > * m_delta2
A buffer that will store the change in concentration for the second substance.
std::vector< float > const * m_concentration1
The buffer for the concentration of the first substance.
std::shared_ptr< WCondition > m_waitForMainThreadCondition
The condition used by the main thread to notify we are ready to start the next iteration.
void setDiffusionConstants(float d1, float d2)
Set the diffusion constants.
std::size_t m_sizeY
The size of the domain in y-direction.
PatternThread(std::size_t id, std::size_t max, std::shared_ptr< WCondition > const mainThreadContinueCondition, std::shared_ptr< WCondition > const waitForMainThreadCondition, WCounter *const counter)
Constructor.
void setBufferPointers(std::vector< float > const *concentration1, std::vector< float > const *concentration2, std::vector< float > const *noise, std::vector< float > *delta1, std::vector< float > *delta2)
Set the pointers to the buffer all threads share.
void setSpotFactor(float spotFactor)
Set the spot factor.
void finish()
Tells the thread to finish.
WCounter *const m_counter
Counts the number of threads done with the current iteration.
std::size_t m_sizeZ
The size of the domain in z-direction.
std::size_t m_maxThreads
The maximum number of threads.
virtual void threadMain()
The entry point for the thread.
std::vector< float > const * m_noise
A constant buffer containing a random value per voxel.
std::shared_ptr< WCondition > m_mainThreadContinueCondition
The condition used to tell the main thread we are done for this iteration.
float m_diffusionConstant1
The first diffusion constant.
bool m_stop
Whether we are to stop the thread.
float m_diffusionConstant2
The second diffusion constant.
std::vector< float > const * m_concentration2
The buffer for the concentration of the second substance.
std::size_t m_sizeX
The size of the domain in x-direction.
Creates a pattern of evenly spaced dots via simulating a chemical reaction.
std::shared_ptr< std::vector< float > > create(std::size_t sizeX, std::size_t sizeY, std::size_t sizeZ)
Creates the 3D pattern and writes it into a vector of floats.
void setSpotIrregularity(float irr)
Sets the spot irregularity parameter.
float m_spotSize
The spot size parameter.
void setSpotSize(float size)
Sets the spotsize parameter.
float m_spotIrregularity
The spot irregularity parameter.
void setNumIterations(std::size_t iter)
Sets the number of iterations for the chemical simulation that creates the pattern.
WTuringPatternCreator(std::shared_ptr< WProgress > const progress, std::size_t numThreads=boost::thread::hardware_concurrency())
Constructor.
float m_numIterations
The number of iterations for the simulation.
std::size_t m_numThreads
The number of threads to use.
std::shared_ptr< WProgress > m_progress
The progress to increment.