OpenWalnut  1.5.0dev
WBresenham.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 WBRESENHAM_H
26 #define WBRESENHAM_H
27 
28 #include <cmath>
29 #include <memory>
30 #include <vector>
31 
32 
33 #include "WRasterAlgorithm.h"
34 #include "core/common/math/WLine.h"
35 #include "core/common/math/linearAlgebra/WPosition.h"
36 #include "core/common/math/linearAlgebra/WVectorFixed.h"
37 #include "core/dataHandler/WGridRegular3D.h"
38 
39 /**
40  * Implements basic Bresenham algorithm for rasterization.
41  */
43 {
44 /**
45 * Only UnitTests may be friends.
46 */
47 friend class WBresenhamTest;
48 public:
49  /**
50  * Initializes new raster algo.
51  *
52  * \param grid The grid which defines the voxels which should be marked.
53  * \param antialiased If true then all voxels of a line are supported with
54  * anti-aliasing voxels around
55  */
56  WBresenham( std::shared_ptr< WGridRegular3D > grid, bool antialiased = true );
57 
58  /**
59  * Finishes this raster algo.
60  */
61  virtual ~WBresenham();
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 );
70 
71 protected:
72  /**
73  * Scans a line segment for voxels which are hit.
74  *
75  * \warning Not every voxel which is hit by the segment will be marked
76  * but at least so many voxels so that the segment is represented by those
77  * voxels.
78  * \warning Every line starting in voxel A and ending in voxel B is
79  * rastered the same way as the line from the middlepoint of A to the
80  * middlepoint of B.
81  *
82  * \note This algorithm is fast since using only integer operations. This
83  * is the real Bresenham
84  *
85  * \param start Start point of the line segment
86  * \param end End point of the line segment
87  */
88  virtual void rasterSegment( const WPosition& start, const WPosition& end );
89 
90  /**
91  * Marks the given voxel as a hit. If anti-aliasing is enabled also some
92  * supporting voxels nearby are marked. The value for marking the voxel
93  * depends on the distance from its center point to the real line.
94  *
95  * \param voxel The voxel to mark
96  * \param axis Along which axis the traversal takes place. Since when
97  * walking in e.g. X-direction there are not supporting voxels in the
98  * same direction.
99  * \param start Start point of the line segment (used to computed the
100  * distance)
101  * \param end End point of the line segment (used to computed the
102  * distance)
103  */
104  virtual void markVoxel( const WVector3i& voxel, const int axis, const WPosition& start, const WPosition& end );
105 
106  /**
107  * Returns the value to mark the hit voxels with, depending on their
108  * distance to the line.
109  *
110  * \param distance Distance of the voxel to the line.
111  *
112  * \return Value which is used for marking a voxel.
113  */
114  virtual double filter( const double distance ) const;
115 
116  /**
117  * Computes the distances for a voxel to the real line segment and also
118  * for its supporting voxels.
119  *
120  * \param voxelNum The voxel number
121  * \param start Start point of the line segment
122  * \param end End point of the line segment
123  *
124  * \return A vector of distances where first distance is the distance of
125  * the voxel itself, then x+1, x-1 supporting voxel, then y+1 and y-1
126  * and at last z+1 and z-1.
127  */
128  std::vector< double > computeDistances( const size_t voxelNum, const WPosition& start, const WPosition& end ) const;
129 
130  /**
131  * Compose the new value for a voxel out of a new computed value and the already existing marking.
132  *
133  * \param newValue Newly computed value
134  * \param existingValue The mark already existing for the voxel (aka previous mark or markSoFar)
135  *
136  * \return The new mark for that voxel
137  */
138  double composeValue( double newValue, double existingValue ) const;
139 
140  bool m_antialiased; //!< If true also some supporting voxels are marked
141 
142 private:
143 };
144 
145 #endif // WBRESENHAM_H
Unit tests the Bresenham algorithm.
Implements basic Bresenham algorithm for rasterization.
Definition: WBresenham.h:43
virtual void raster(const WLine &line)
Rasterize the given line into the grid of dataset.
Definition: WBresenham.cpp:50
WBresenham(std::shared_ptr< WGridRegular3D > grid, bool antialiased=true)
Initializes new raster algo.
Definition: WBresenham.cpp:40
virtual double filter(const double distance) const
Returns the value to mark the hit voxels with, depending on their distance to the line.
Definition: WBresenham.cpp:281
bool m_antialiased
If true also some supporting voxels are marked.
Definition: WBresenham.h:140
virtual void markVoxel(const WVector3i &voxel, const int axis, const WPosition &start, const WPosition &end)
Marks the given voxel as a hit.
Definition: WBresenham.cpp:195
double composeValue(double newValue, double existingValue) const
Compose the new value for a voxel out of a new computed value and the already existing marking.
Definition: WBresenham.cpp:189
virtual ~WBresenham()
Finishes this raster algo.
Definition: WBresenham.cpp:46
std::vector< double > computeDistances(const size_t voxelNum, const WPosition &start, const WPosition &end) const
Computes the distances for a voxel to the real line segment and also for its supporting voxels.
Definition: WBresenham.cpp:160
virtual void rasterSegment(const WPosition &start, const WPosition &end)
Scans a line segment for voxels which are hit.
Definition: WBresenham.cpp:70
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.