OpenWalnut  1.5.0dev
WRasterAlgorithm.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 WRASTERALGORITHM_H
26 #define WRASTERALGORITHM_H
27 
28 #include <memory>
29 #include <shared_mutex>
30 #include <vector>
31 
32 #include <boost/thread.hpp>
33 
34 #include "WRasterParameterization.h"
35 #include "core/common/math/WLine.h"
36 #include "core/common/math/linearAlgebra/WVectorFixed.h"
37 #include "core/dataHandler/WDataSetScalar.h"
38 #include "core/dataHandler/WGridRegular3D.h"
39 
40 /**
41  * Base class for all rasterization algorithms. The interface will be as
42  * follows: You need a WGridRegular3D grid and some geometry. The grid
43  * specifies the volume where you raster the geometry. For other geometries
44  * than lines please overload the raster() member function.
45  */
47 {
48 public:
49  /**
50  * Creates new raster algorithm within the given grid. The grid may later
51  * also be used to generate a real DataSet, \see generateDataSet() for
52  * further instructions.
53  *
54  * \param grid The grid specifying the voxels.
55  */
56  explicit WRasterAlgorithm( std::shared_ptr< WGridRegular3D > grid );
57 
58  /**
59  * Dispose a this raster algorithm.
60  */
61  virtual ~WRasterAlgorithm();
62 
63  /**
64  * Rasterize the given line into the grid of dataset.
65  * The value of the voxel which will be hit changes its value.
66  *
67  * \param line Polyline which is about to be rastered.
68  */
69  virtual void raster( const WLine& line ) = 0;
70 
71  /**
72  * Computes a dataset out of our voxel values and the previously given
73  * grid. Note this may take some time.
74  *
75  * \return Dataset where all voxels which are hit by the rastered lines are
76  * non zero.
77  */
78  std::shared_ptr< WDataSetScalar > generateDataSet() const;
79 
80  /**
81  * This method allows the user of the raster algorithm to add arbitrary parameterizations. Each parameterization creates a new volume dataset
82  * parameterizing the voxelized line somehow.
83  *
84  * \param algorithm the algorithm
85  */
86  void addParameterizationAlgorithm( std::shared_ptr< WRasterParameterization > algorithm );
87 
88  /**
89  * Called whenever all lines have been rasterized.
90  */
91  virtual void finished();
92 
93 protected:
94  /**
95  * All the parameterization algorithms to apply while rasterizing a line.
96  */
97  std::vector< std::shared_ptr< WRasterParameterization > > m_parameterizations;
98 
99  /**
100  * The mutex used to lock access to m_parameterizations.
101  */
102  std::shared_mutex m_parameterizationsLock;
103 
104  /**
105  * The grid is used for the following purposes:
106  * - First we need it when creating the final dataset
107  * - Second we need it to determine how many voxels are there at
108  * construction time
109  * - Third we need it when computing the value number out of a position
110  */
111  std::shared_ptr< WGridRegular3D > m_grid;
112 
113  /**
114  * Stores the value of each voxel. If and only if a voxel is _not_ hit by
115  * a line segment its value is 0.0.
116  */
117  std::vector< double > m_values;
118 
119  /**
120  * This method allows all registered parameterization algorithms to update. This basically simply calls all parameterizeVoxel methods in
121  * m_parameterizations vector.
122  *
123  * \param voxel the voxel to parameterize
124  * \param voxelIdx the voxel index in the common grid calculated using "voxel" (this is for convenience)
125  * \param axis Along which axis the traversal takes place. Since when walking in e.g. X-direction there are not supporting voxels in the
126  * \param value the new voxel value
127  * \param start Start point of the line segment (used to computed the distance)
128  * \param end End point of the line segment (used to computed the distance)
129  */
130  virtual void parameterizeVoxel( const WVector3i& voxel, size_t voxelIdx, const int axis, const double value,
131  const WPosition& start,
132  const WPosition& end );
133 
134  /**
135  * Distribute a new line getting rasterized to all parameterize algorithms.
136  *
137  * \param line the new line.
138  */
139  virtual void newLine( const WLine& line );
140 
141  /**
142  * Distribute a new segment of a line to all parameterization algorithms.
143  * Gets called for each new line segment getting rasterized, as one segment can have multiple voxels.
144  *
145  * \param start start point of the new line segment
146  * \param end end point of the new line segment
147  */
148  virtual void newSegment( const WPosition& start, const WPosition& end );
149 
150 private:
151 };
152 #endif // WRASTERALGORITHM_H
A line is an ordered sequence of WPositions.
Definition: WLine.h:42
A fixed size matrix class.
Definition: WMatrixFixed.h:150
This only is a 3d double vector.
Base class for all rasterization algorithms.
std::shared_ptr< WDataSetScalar > generateDataSet() const
Computes a dataset out of our voxel values and the previously given grid.
void addParameterizationAlgorithm(std::shared_ptr< WRasterParameterization > algorithm)
This method allows the user of the raster algorithm to add arbitrary parameterizations.
std::shared_ptr< WGridRegular3D > m_grid
The grid is used for the following purposes:
std::vector< std::shared_ptr< WRasterParameterization > > m_parameterizations
All the parameterization algorithms to apply while rasterizing a line.
virtual void finished()
Called whenever all lines have been rasterized.
virtual void parameterizeVoxel(const WVector3i &voxel, size_t voxelIdx, const int axis, const double value, const WPosition &start, const WPosition &end)
This method allows all registered parameterization algorithms to update.
WRasterAlgorithm(std::shared_ptr< WGridRegular3D > grid)
Creates new raster algorithm within the given grid.
std::shared_mutex m_parameterizationsLock
The mutex used to lock access to m_parameterizations.
std::vector< double > m_values
Stores the value of each voxel.
virtual void newLine(const WLine &line)
Distribute a new line getting rasterized to all parameterize algorithms.
virtual void newSegment(const WPosition &start, const WPosition &end)
Distribute a new segment of a line to all parameterization algorithms.
virtual ~WRasterAlgorithm()
Dispose a this raster algorithm.
virtual void raster(const WLine &line)=0
Rasterize the given line into the grid of dataset.