OpenWalnut  1.5.0dev
WTuringTextureCreator.h
1 //---------------------------------------------------------------------------
2 //
3 // Project: OpenWalnut ( http://www.openwalnut.org )
4 //
5 // Copyright 2009 OpenWalnut Community, BSV-Leipzig and CNCF-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 WTURINGTEXTURECREATOR_H
26 #define WTURINGTEXTURECREATOR_H
27 
28 #include <memory>
29 #include <vector>
30 
31 #include <boost/thread.hpp>
32 #include <core/common/WThreadedRunner.h>
33 #include <core/graphicsEngine/WGETexture.h>
34 
35 /**
36  * Class to create a Turing noise texture using multiple threads if needed.
37  */
39 {
40 public:
41  /**
42  * Constructor. Initializes threading but does not yet start texture creation.
43  *
44  * \param numThreads the number of threads to use. By default, this is the number of threads natively supported by
45  * the CPU.
46  */
47  WTuringTextureCreator( std::size_t numThreads = boost::thread::hardware_concurrency() );
48 
49  /**
50  * Destructor.
51  */
53 
54  /**
55  * Create the Turing noise texture of arbitrary size. This function is synchronous and returns a valid texture.
56  *
57  * \param sizeX the size along the x axis.
58  * \param sizeY the size along the y axis.
59  * \param sizeZ the size along the z axis.
60  *
61  * \return the texture.
62  */
63  osg::ref_ptr< WGETexture3D > create( std::size_t sizeX, std::size_t sizeY, std::size_t sizeZ );
64 
65  /**
66  * Define the size of the spots inside the texture.
67  *
68  * \param size the size in [0,1], 1 meaning larger spots.
69  */
70  void setSpotSize( float size );
71 
72  /**
73  * The irregularity of the spots.
74  *
75  * \param irr the irregularity in [0,1]; 0 producing a very regular grid of spots, whereas 1 causes the spots to be
76  * rather irregular.
77  */
78  void setSpotIrregularity( float irr );
79 
80  /**
81  * The number of iterations to use for calculating the texture.
82  *
83  * \param iter the number of iterations. 100 is recommended, lower values create a noisy texture, higher values
84  * cause the spots to be rather smooth.
85  */
86  void setNumIterations( std::size_t iter );
87 
88 private:
89  /**
90  * The thread calculating the Turing concentration diffusion in a given range.
91  */
93  {
94  public:
95  /**
96  * Create a calculation thread. This does not start calculation. It uses both parameters to define the memory
97  * range to work in.
98  *
99  * \param id the id of this thread. Must be in [0, max).
100  * \param max the maximum number of threads.
101  */
102  TextureThread( std::size_t id, std::size_t max );
103 
104  /**
105  * Destructor.
106  */
107  ~TextureThread();
108 
109  /**
110  * Target texture size.
111  *
112  * \param sizeX size in X
113  * \param sizeY size in Y
114  * \param sizeZ size in Z
115  */
116  void setTextureSize( std::size_t sizeX, std::size_t sizeY, std::size_t sizeZ );
117 
118  /**
119  * The factor influencing size and shape of a spot.
120  *
121  * \param spotFactor the factor. Refer to the Turing Reaction Diffusion algorithm. It mainly relates to the spot
122  * size.
123  */
124  void setSpotFactor( float spotFactor );
125 
126  /**
127  * Diffusion constants according to Turing Reaction Diffusion algorithm.
128  *
129  * \param d1 Diffusion constant of fluid 1
130  * \param d2 Diffusion constant of fluid 2
131  */
132  void setDiffusionConstants( float d1, float d2 );
133 
134  /**
135  * Define the memory to work in.
136  *
137  * \param concentration1 the shared memory for concentration values of fluid 1
138  * \param concentration2 the shared memory for concentration values of fluid 2
139  * \param noise the input noise.
140  * \param delta1 the shared memory representing the concentration change of fluid 1
141  * \param delta2 the shared memory representing the concentration change of fluid 2
142  */
143  void setBufferPointers( std::vector< float > const* concentration1, std::vector< float > const* concentration2,
144  std::vector< float > const* noise, std::vector< float >* delta1, std::vector< float >* delta2 );
145 
146  /**
147  * The actual thread function running the algorithm.
148  */
149  virtual void threadMain();
150 
151  private:
152  /**
153  * ID of the thread.
154  */
155  std::size_t m_id;
156 
157  /**
158  * Number of all spawned threads.
159  */
160  std::size_t m_maxThreads;
161 
162  /**
163  * Size of the resulting texture.
164  */
165  std::size_t m_sizeX;
166 
167  /**
168  * Size of the resulting texture.
169  */
170  std::size_t m_sizeY;
171 
172  /**
173  * Size of the resulting texture.
174  */
175  std::size_t m_sizeZ;
176 
177  /**
178  * The factor defining the spot size and shape.
179  */
181 
182  /**
183  * The amount of diffusion of fluid 1.
184  */
186 
187  /**
188  * The amount of diffusion of fluid 2.
189  */
191 
192  //! the shared memory for concentration values of fluid 1
193  std::vector< float > const* m_concentration1;
194 
195  //! the shared memory for concentration values of fluid 2
196  std::vector< float > const* m_concentration2;
197 
198  //! the input noise.
199  std::vector< float > const* m_noise;
200 
201  //! the shared memory representing the concentration change of fluid 1
202  std::vector< float >* m_delta1;
203 
204  //! the shared memory representing the concentration change of fluid 2
205  std::vector< float >* m_delta2;
206  };
207 
208  /**
209  * Number of iterations. 100 by default.
210  */
212 
213  /**
214  * Spot irregularity. 0.1 by default.
215  */
217 
218  /**
219  * Spot size. 0.1 by default.
220  */
221  float m_spotSize;
222 
223  /**
224  * The thread pool.
225  */
226  std::vector< std::shared_ptr< TextureThread > > m_threads;
227 };
228 
229 #endif // WTURINGTEXTURECREATOR_H
Base class for all classes needing to be executed in a separate thread.
The thread calculating the Turing concentration diffusion in a given range.
float m_diffusionConstant2
The amount of diffusion of fluid 2.
TextureThread(std::size_t id, std::size_t max)
Create a calculation thread.
std::vector< float > const * m_concentration1
the shared memory for concentration values of fluid 1
void setDiffusionConstants(float d1, float d2)
Diffusion constants according to Turing Reaction Diffusion algorithm.
float m_diffusionConstant1
The amount of diffusion of fluid 1.
std::size_t m_sizeY
Size of the resulting texture.
std::vector< float > const * m_concentration2
the shared memory for concentration values of fluid 2
std::vector< float > * m_delta2
the shared memory representing the concentration change of fluid 2
float m_spotFactor
The factor defining the spot size and shape.
virtual void threadMain()
The actual thread function running the algorithm.
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)
Define the memory to work in.
void setTextureSize(std::size_t sizeX, std::size_t sizeY, std::size_t sizeZ)
Target texture size.
std::size_t m_sizeZ
Size of the resulting texture.
std::vector< float > * m_delta1
the shared memory representing the concentration change of fluid 1
void setSpotFactor(float spotFactor)
The factor influencing size and shape of a spot.
std::size_t m_sizeX
Size of the resulting texture.
std::vector< float > const * m_noise
the input noise.
std::size_t m_maxThreads
Number of all spawned threads.
Class to create a Turing noise texture using multiple threads if needed.
WTuringTextureCreator(std::size_t numThreads=boost::thread::hardware_concurrency())
Constructor.
void setSpotIrregularity(float irr)
The irregularity of the spots.
void setNumIterations(std::size_t iter)
The number of iterations to use for calculating the texture.
std::vector< std::shared_ptr< TextureThread > > m_threads
The thread pool.
float m_numIterations
Number of iterations.
void setSpotSize(float size)
Define the size of the spots inside the texture.
float m_spotIrregularity
Spot irregularity.
osg::ref_ptr< WGETexture3D > create(std::size_t sizeX, std::size_t sizeY, std::size_t sizeZ)
Create the Turing noise texture of arbitrary size.