OpenWalnut  1.5.0dev
WBresenhamDBL.cpp
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 #include <memory>
26 
27 
28 #include "WBresenhamDBL.h"
29 #include "core/common/math/WLine.h"
30 #include "core/common/math/linearAlgebra/WPosition.h"
31 #include "core/dataHandler/WGridRegular3D.h"
32 
33 WBresenhamDBL::WBresenhamDBL( std::shared_ptr< WGridRegular3D > grid, bool antialiased )
34  : WBresenham( grid, antialiased )
35 {
36 }
37 
39 {
40 }
41 
42 void WBresenhamDBL::rasterSegment( const WPosition& start, const WPosition& end )
43 {
44  int i;
45  WVector3i gridStartPos = m_grid->getVoxelCoord( start );
46  WVector3i gridEndPos = m_grid->getVoxelCoord( end );
47  int dx = gridEndPos[0] - gridStartPos[0];
48  int dy = gridEndPos[1] - gridStartPos[1];
49  int dz = gridEndPos[2] - gridStartPos[2];
50  int l = std::abs( dx );
51  int m = std::abs( dy );
52  int n = std::abs( dz );
53  int x_inc = ( dx < 0 ) ? -1 : 1;
54  int y_inc = ( dy < 0 ) ? -1 : 1;
55  int z_inc = ( dz < 0 ) ? -1 : 1;
56  double err_1, err_2;
57  int dx2 = l << 1;
58  int dy2 = m << 1;
59  int dz2 = n << 1;
60  WVector3i voxel = gridStartPos;
61  WPosition gridOffset( 0, 0, 0 );
62  gridOffset[0] = start[0] - gridStartPos[0];
63  gridOffset[1] = start[1] - gridStartPos[1];
64  gridOffset[2] = start[2] - gridStartPos[2];
65 
66  if( ( l >= m ) && ( l >= n ) )
67  {
68  err_1 = dy2 - l + gridOffset[1];
69  err_2 = dz2 - l + gridOffset[2];
70  for( i = 0; i < l; i++ )
71  {
72  markVoxel( voxel, 0, start, end );
73  if( err_1 > 0 )
74  {
75  voxel[1] += y_inc;
76  err_1 -= dx2;
77  }
78  if( err_2 > 0 )
79  {
80  voxel[2] += z_inc;
81  err_2 -= dx2;
82  }
83  // end of antialiased if-else
84  err_1 += dy2;
85  err_2 += dz2;
86  voxel[0] += x_inc;
87  }
88  }
89  else if( ( m >= l ) && ( m >= n ) )
90  {
91  err_1 = dx2 - m + gridOffset[0];
92  err_2 = dz2 - m + gridOffset[2];
93  for( i = 0; i < m; i++ )
94  {
95  markVoxel( voxel, 1, start, end );
96  if( err_1 > 0 )
97  {
98  voxel[0] += x_inc;
99  err_1 -= dy2;
100  }
101  if( err_2 > 0 )
102  {
103  voxel[2] += z_inc;
104  err_2 -= dy2;
105  }
106  err_1 += dx2;
107  err_2 += dz2;
108  voxel[1] += y_inc;
109  }
110  }
111  else
112  {
113  err_1 = dy2 - n + gridOffset[1];
114  err_2 = dx2 - n + gridOffset[0];
115  for( i = 0; i < n; i++ )
116  {
117  markVoxel( voxel, 2, start, end );
118  if( err_1 > 0 )
119  {
120  voxel[1] += y_inc;
121  err_1 -= dz2;
122  }
123  if( err_2 > 0 )
124  {
125  voxel[0] += x_inc;
126  err_2 -= dz2;
127  }
128  err_1 += dy2;
129  err_2 += dx2;
130  voxel[2] += z_inc;
131  }
132  }
133  markVoxel( voxel, -1, start, end );
134 }
virtual ~WBresenhamDBL()
Finishes this raster algo.
WBresenhamDBL(std::shared_ptr< WGridRegular3D > grid, bool antialiased=true)
Initializes new raster algo.
void rasterSegment(const WPosition &start, const WPosition &stop)
Scans a line segment for voxels which are hit.
Implements basic Bresenham algorithm for rasterization.
Definition: WBresenham.h:43
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
A fixed size matrix class.
Definition: WMatrixFixed.h:150
This only is a 3d double vector.
std::shared_ptr< WGridRegular3D > m_grid
The grid is used for the following purposes: