OpenWalnut  1.5.0dev
WThreadedTrackingFunction.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 WTHREADEDTRACKINGFUNCTION_H
26 #define WTHREADEDTRACKINGFUNCTION_H
27 
28 #include <memory>
29 #include <stdint.h>
30 #include <utility>
31 #include <vector>
32 
33 #include <boost/array.hpp>
34 
35 #include "../common/WSharedObject.h"
36 #include "../common/WThreadedJobs.h"
37 #include "../common/math/linearAlgebra/WVectorFixed.h"
38 #include "WDataSetSingle.h"
39 
40 class WThreadedTrackingFunctionTest; //! forward declaration
41 
42 
43 /**
44  * For tracking related functionality.
45  */
46 namespace wtracking // note that this is not final
47 {
48  // an epsilon value for various floating point comparisons
49 #if INTPTR_MAX == INT32_MAX
50  #define TRACKING_EPS 0.00001
51 #else
52  #define TRACKING_EPS 0.0000001
53 #endif
54 
55  /**
56  * \class WTrackingUtility
57  *
58  * A class that provides untility functions and typedefs for tracking algorithms.
59  */
61  {
62  public:
63  //! define a job type for tracking algorithms
64  typedef std::pair< WVector3d, WVector3d > JobType;
65 
66  //! the dataset type
68 
69  //! a pointer to a dataset
70  typedef std::shared_ptr< DataSetType const > DataSetPtr;
71 
72  //! a function that calculates a direction to continue tracking
73  typedef boost::function< WVector3d ( DataSetPtr, JobType const& ) > DirFunc;
74 
75  //! a pointer to a regular 3d grid
76  // other grid types are not supported at the moment
77  typedef std::shared_ptr< WGridRegular3D > Grid3DPtr;
78 
79  /**
80  * A function that follows a direction until leaving the current voxel.
81  *
82  * \param dataset A pointer to the input dataset.
83  * \param job A pair of vectors, the position and the direction of the last integration.
84  * \param dirFunc A function that computes the next direction.
85  *
86  * \return true, iff the calculated point is a valid position inside the grid
87  */
88  static bool followToNextVoxel( DataSetPtr dataset, JobType& job, DirFunc const& dirFunc );
89 
90  // one could add a runge-kutta-integrator too
91 
92  /**
93  * Check if a point is on the boundary of the given grid, where boundary
94  * means a distance less then TRACKING_EPS from any plane between
95  * voxels. This does not check if the position is actually inside the grid.
96  *
97  * \param grid The grid.
98  * \param pos The position to test.
99  *
100  * \return true, iff the position is on any voxel boundary
101  */
102  static bool onBoundary( Grid3DPtr grid, WVector3d const& pos );
103 
104  /**
105  * Calculate the distance from a given position to the nearest voxel boundary
106  * on the ray from the position along the given direction.
107  *
108  * \param grid The grid.
109  * \param pos The starting position of the ray.
110  * \param dir The normalized direction of the ray.
111  *
112  * \return The distance to the next voxel boundary.
113  *
114  * \note pos + getDistanceToBoundary( grid, pos, dir ) * dir will be a position on a voxel boundary
115  */
116  static double getDistanceToBoundary( Grid3DPtr grid, WVector3d const& pos, WVector3d const& dir );
117  };
118 
119  //////////////////////////////////////////////////////////////////////////////////////////
120 
121  /**
122  * \class WThreadedTrackingFunction
123  *
124  * Implements a generalized multithreaded tracking algorithm. A function that calculates the direction
125  * and a function that calculates a new position have to be provided.
126  *
127  * Output values can be retrieved via two visitor functions that get called per fiber tracked and
128  * per point calculated respectively.
129  *
130  * There are a certain number n of seeds per direction, this meens n*n*n seeds per voxel. For every
131  * seed, m fibers get integrated. These two parameters are the seedPositions and seedsPerVoxel parameters
132  * of the constructor, respectively.
133  *
134  * A 'cubic' region of the grid can be chosen for seeding. The v0 and v1 parameters of the constructor
135  * are the starting/target voxel coords. Example:
136  *
137  * v0: 1, 1, 1
138  * v1: 4, 5, 3
139  *
140  * In this case, only voxels between coords 1 to 3 in the x-direction, the voxels 1 to 4 in y- and the voxels 1 to 2
141  * in z-direction are used for seeding.
142  *
143  * Note that voxels at the first (0) and last (grid->getNbCoords*()) position in any direction are
144  * invalid seeding voxels as they are partially outside of the grid.
145  */
146  class WThreadedTrackingFunction : public WThreadedJobs< WTrackingUtility::DataSetType, WTrackingUtility::JobType >
147  {
148  //! make the test a friend
149  friend class ::WThreadedTrackingFunctionTest;
150 
151  //! the job type
153 
154  //! the dataset type
156 
157  //! a pointer to a dataset
159 
160  //! the grid type
162 
163  //! a pointer to the grid
164  typedef std::shared_ptr< GridType > GridPtr;
165 
166  //! the direction calculation function
168 
169  //! the path integration function
170  typedef boost::function< bool ( DataSetPtr, JobType&, DirFunc const& ) > NextPositionFunc;
171 
172  //! a visitor function for fibers
173  typedef boost::function< void ( std::vector< WVector3d > const& ) > FiberVisitorFunc;
174 
175  //! a visitor function type for points
176  typedef boost::function< void ( WVector3d const& ) > PointVisitorFunc;
177 
178  //! the base class, a threaded job function
180 
181  //! this type
183 
184  public:
185  /**
186  * Constructor.
187  *
188  * \param dataset A pointer to a dataset.
189  * \param dirFunc A direction calculation function.
190  * \param nextFunc A position integration function.
191  * \param fiberVst A visitor for fibers.
192  * \param pointVst A visitor for points.
193  * \param seedPositions The number of seed positions in every direction per voxel.
194  * \param seedsPerPos The number of fibers startet from every seed position.
195  * \param v0 A vector of starting voxel indices for every direction.
196  * \param v1 A vector of target voxel indices for every direction.
197  */
198  WThreadedTrackingFunction( DataSetPtr dataset, DirFunc dirFunc, NextPositionFunc nextFunc,
199  FiberVisitorFunc fiberVst, PointVisitorFunc pointVst,
200  std::size_t seedPositions = 1, std::size_t seedsPerPos = 1,
201  std::vector< int > v0 = std::vector< int >(),
202  std::vector< int > v1 = std::vector< int >() );
203 
204  /**
205  * Destructor.
206  */
207  virtual ~WThreadedTrackingFunction();
208 
209  /**
210  * The job generator.
211  *
212  * \param job The next job (output).
213  *
214  * \return false, iff there are no more jobs.
215  */
216  virtual bool getJob( JobType& job ); // NOLINT
217 
218  /**
219  * The calculation per job.
220  *
221  * \param input The input dataset.
222  * \param job The job.
223  */
224  virtual void compute( DataSetPtr input, JobType const& job );
225 
226  private:
227  /**
228  * \class IndexType
229  *
230  * An index for seed positions.
231  */
232  class IndexType
233  {
234  friend class ::WThreadedTrackingFunctionTest; //!< Access for test class.
235  public:
236  /**
237  * Construct an invalid index.
238  */
239  IndexType();
240 
241  /**
242  * Construct an index.
243  *
244  * \param grid The grid.
245  * \param v0 A vector of starting voxel indices for every direction.
246  * \param v1 A vector of target voxel indices for every direction.
247  * \param seedPositions The number of seed positions in every direction per voxel.
248  * \param seedsPerPosition The number of fibers startet from every seed position.
249  */
250  IndexType( GridPtr grid, std::vector< int > const& v0,
251  std::vector< int > const& v1, std::size_t seedPositions,
252  std::size_t seedsPerPosition );
253 
254  /**
255  * Increase the index by one, effectively generating the next seed position.
256  *
257  * \return *this
258  */
260 
261  /**
262  * Check if there aren't any more seed positions.
263  *
264  * \return true, iff there aren't any more seed positions.
265  */
266  bool done();
267 
268  /**
269  * Create a job from this index.
270  *
271  * \return The job that is the current position.
272  */
273  JobType job();
274 
275  private:
276  //! a pointer to the grid
278 
279  //! true, iff there are no more seeds
280  bool m_done;
281 
282  //! the position in the seed space
283  boost::array< std::size_t, 4 > m_pos;
284 
285  //! the minimum position in the seed space
286  boost::array< std::size_t, 4 > m_min;
287 
288  //! the maximum position in the seed space
289  boost::array< std::size_t, 4 > m_max;
290 
291  //! the relative (to the size of a voxel) distance between seeds
292  double m_offset;
293  };
294 
295  //! a pointer to the grid
297 
298  //! a function that returns the next direction
300 
301  //! a function that calculates the next position
303 
304  //! the fiber visitor
306 
307  //! the point visitor
309 
310  //! the maximum number of points per forward/backward integration of a fiber
311  std::size_t m_maxPoints;
312 
313  //! the current index/seed position
315  };
316 
317 } /* namespace wtracking */
318 
319 #endif // WTHREADEDTRACKINGFUNCTION_H
A data set consisting of a set of values based on a grid.
A grid that has parallelepiped cells which all have the same proportion.
Wrapper around an object/type for thread safe sharing of objects among multiple threads.
Definition: WSharedObject.h:45
A threaded functor base class for producer-consumer-style multithreaded computation.
Definition: WThreadedJobs.h:51
Test the WThreadedTrackingFunction class.
boost::array< std::size_t, 4 > m_max
the maximum position in the seed space
boost::array< std::size_t, 4 > m_min
the minimum position in the seed space
bool done()
Check if there aren't any more seed positions.
double m_offset
the relative (to the size of a voxel) distance between seeds
IndexType & operator++()
Increase the index by one, effectively generating the next seed position.
boost::array< std::size_t, 4 > m_pos
the position in the seed space
Implements a generalized multithreaded tracking algorithm.
WTrackingUtility::JobType JobType
the job type
WTrackingUtility::DataSetPtr DataSetPtr
a pointer to a dataset
std::shared_ptr< GridType > GridPtr
a pointer to the grid
FiberVisitorFunc m_fiberVisitor
the fiber visitor
std::size_t m_maxPoints
the maximum number of points per forward/backward integration of a fiber
WTrackingUtility::DataSetType DataSetType
the dataset type
boost::function< void(std::vector< WVector3d > const &) > FiberVisitorFunc
a visitor function for fibers
WThreadedTrackingFunction(DataSetPtr dataset, DirFunc dirFunc, NextPositionFunc nextFunc, FiberVisitorFunc fiberVst, PointVisitorFunc pointVst, std::size_t seedPositions=1, std::size_t seedsPerPos=1, std::vector< int > v0=std::vector< int >(), std::vector< int > v1=std::vector< int >())
Constructor.
WThreadedTrackingFunction This
this type
WTrackingUtility::DirFunc DirFunc
the direction calculation function
boost::function< bool(DataSetPtr, JobType &, DirFunc const &) > NextPositionFunc
the path integration function
NextPositionFunc m_nextPosFunc
a function that calculates the next position
boost::function< void(WVector3d const &) > PointVisitorFunc
a visitor function type for points
WSharedObject< IndexType > m_currentIndex
the current index/seed position
virtual void compute(DataSetPtr input, JobType const &job)
The calculation per job.
DirFunc m_directionFunc
a function that returns the next direction
virtual bool getJob(JobType &job)
The job generator.
WThreadedJobs< DataSetType, JobType > Base
the base class, a threaded job function
PointVisitorFunc m_pointVisitor
the point visitor
A class that provides untility functions and typedefs for tracking algorithms.
static bool onBoundary(Grid3DPtr grid, WVector3d const &pos)
Check if a point is on the boundary of the given grid, where boundary means a distance less then TRAC...
std::pair< WVector3d, WVector3d > JobType
define a job type for tracking algorithms
static bool followToNextVoxel(DataSetPtr dataset, JobType &job, DirFunc const &dirFunc)
A function that follows a direction until leaving the current voxel.
WDataSetSingle DataSetType
the dataset type
std::shared_ptr< WGridRegular3D > Grid3DPtr
a pointer to a regular 3d grid
std::shared_ptr< DataSetType const > DataSetPtr
a pointer to a dataset
static double getDistanceToBoundary(Grid3DPtr grid, WVector3d const &pos, WVector3d const &dir)
Calculate the distance from a given position to the nearest voxel boundary on the ray from the positi...
boost::function< WVector3d(DataSetPtr, JobType const &) > DirFunc
a function that calculates a direction to continue tracking
forward declaration