~ubuntu-branches/ubuntu/precise/openwalnut/precise

« back to all changes in this revision

Viewing changes to src/modules/voxelizer/WBresenhamDBL.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Sebastian Eichelbaum
  • Date: 2011-06-21 10:26:54 UTC
  • Revision ID: james.westby@ubuntu.com-20110621102654-rq0zf436q949biih
Tags: upstream-1.2.5
ImportĀ upstreamĀ versionĀ 1.2.5

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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 <boost/shared_ptr.hpp>
 
26
 
 
27
#include "core/dataHandler/WGridRegular3D.h"
 
28
#include "core/common/math/WLine.h"
 
29
#include "core/common/math/linearAlgebra/WLinearAlgebra.h"
 
30
#include "WBresenhamDBL.h"
 
31
 
 
32
WBresenhamDBL::WBresenhamDBL( boost::shared_ptr< WGridRegular3D > grid, bool antialiased )
 
33
    : WBresenham( grid, antialiased )
 
34
{
 
35
}
 
36
 
 
37
WBresenhamDBL::~WBresenhamDBL()
 
38
{
 
39
}
 
40
 
 
41
void WBresenhamDBL::rasterSegment( const WPosition& start, const WPosition& end )
 
42
{
 
43
    int i;
 
44
    WValue< int > gridStartPos = m_grid->getVoxelCoord( start );
 
45
    WValue< int > gridEndPos = m_grid->getVoxelCoord( end );
 
46
    int dx = gridEndPos[0] - gridStartPos[0];
 
47
    int dy = gridEndPos[1] - gridStartPos[1];
 
48
    int dz = gridEndPos[2] - gridStartPos[2];
 
49
    int l = std::abs( dx );
 
50
    int m = std::abs( dy );
 
51
    int n = std::abs( dz );
 
52
    int x_inc = ( dx < 0 ) ? -1 : 1;
 
53
    int y_inc = ( dy < 0 ) ? -1 : 1;
 
54
    int z_inc = ( dz < 0 ) ? -1 : 1;
 
55
    double err_1, err_2;
 
56
    int dx2 = l << 1;
 
57
    int dy2 = m << 1;
 
58
    int dz2 = n << 1;
 
59
    WValue< int > voxel = gridStartPos;
 
60
    WPosition gridOffset( 0, 0, 0 );
 
61
    gridOffset[0] = start[0] - gridStartPos[0];
 
62
    gridOffset[1] = start[1] - gridStartPos[1];
 
63
    gridOffset[2] = start[2] - gridStartPos[2];
 
64
 
 
65
    if( ( l >= m ) && ( l >= n ) )
 
66
    {
 
67
        err_1 = dy2 - l + gridOffset[1];
 
68
        err_2 = dz2 - l + gridOffset[2];
 
69
        for( i = 0; i < l; i++ )
 
70
        {
 
71
            markVoxel( voxel, 0, start, end );
 
72
            if( err_1 > 0 )
 
73
            {
 
74
                voxel[1] += y_inc;
 
75
                err_1 -= dx2;
 
76
            }
 
77
            if( err_2 > 0 )
 
78
            {
 
79
                voxel[2] += z_inc;
 
80
                err_2 -= dx2;
 
81
            }
 
82
            // end of antialiased if-else
 
83
            err_1 += dy2;
 
84
            err_2 += dz2;
 
85
            voxel[0] += x_inc;
 
86
        }
 
87
    }
 
88
    else if( ( m >= l ) && ( m >= n ) )
 
89
    {
 
90
        err_1 = dx2 - m + gridOffset[0];
 
91
        err_2 = dz2 - m + gridOffset[2];
 
92
        for( i = 0; i < m; i++ )
 
93
        {
 
94
            markVoxel( voxel, 1, start, end );
 
95
            if( err_1 > 0 )
 
96
            {
 
97
                voxel[0] += x_inc;
 
98
                err_1 -= dy2;
 
99
            }
 
100
            if( err_2 > 0 )
 
101
            {
 
102
                voxel[2] += z_inc;
 
103
                err_2 -= dy2;
 
104
            }
 
105
            err_1 += dx2;
 
106
            err_2 += dz2;
 
107
            voxel[1] += y_inc;
 
108
        }
 
109
    }
 
110
    else
 
111
    {
 
112
        err_1 = dy2 - n + gridOffset[1];
 
113
        err_2 = dx2 - n + gridOffset[0];
 
114
        for( i = 0; i < n; i++ )
 
115
        {
 
116
            markVoxel( voxel, 2, start, end );
 
117
            if( err_1 > 0 )
 
118
            {
 
119
                voxel[1] += y_inc;
 
120
                err_1 -= dz2;
 
121
            }
 
122
            if( err_2 > 0 )
 
123
            {
 
124
                voxel[0] += x_inc;
 
125
                err_2 -= dz2;
 
126
            }
 
127
            err_1 += dy2;
 
128
            err_2 += dx2;
 
129
            voxel[2] += z_inc;
 
130
        }
 
131
    }
 
132
    markVoxel( voxel, -1, start, end );
 
133
}