Orfeo Toolbox  3.16
itkBresenhamLine.txx
Go to the documentation of this file.
1 /*=========================================================================
2 
3  Program: Insight Segmentation & Registration Toolkit
4  Module: $RCSfile: itkBresenhamLine.txx,v $
5  Language: C++
6  Date: $Date: 2009-06-03 12:48:05 $
7  Version: $Revision: 1.2 $
8 
9  Copyright (c) Insight Software Consortium. All rights reserved.
10  See ITKCopyright.txt or http://www.itk.org/HTML/Copyright.htm for details.
11 
12  This software is distributed WITHOUT ANY WARRANTY; without even
13  the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14  PURPOSE. See the above copyright notices for more information.
15 
16 =========================================================================*/
17 
18 #ifndef __itkBresenhamLine_txx
19 #define __itkBresenhamLine_txx
20 
21 #include "itkBresenhamLine.h"
22 
23 namespace itk {
24 
25 template<unsigned int VDimension>
26 typename BresenhamLine<VDimension>::OffsetArray BresenhamLine<VDimension>
27 ::BuildLine(LType Direction, unsigned int length)
28 {
29  // copied from the line iterator
31  // The dimension with the largest difference between start and end
32  unsigned int m_MainDirection;
33 
34  // Accumulated error for the other dimensions
35  IndexType m_AccumulateError;
36 
37  // Increment for the error for each step. Two times the difference between
38  // start and end
39  IndexType m_IncrementError;
40 
41  // If enough is accumulated for a dimension, the index has to be
42  // incremented. Will be the number of pixels in the line
43  IndexType m_MaximalError;
44 
45  // Direction of increment. -1 or 1
46  IndexType m_OverflowIncrement;
47 
48  // After an overflow, the accumulated error is reduced again. Will be
49  // two times the number of pixels in the line
50  IndexType m_ReduceErrorAfterIncrement;
51 
52  OffsetArray result(length);
53 
54  IndexType m_CurrentImageIndex, StartIndex, LastIndex;
55  Direction.Normalize();
56  // we are going to start at 0
57  m_CurrentImageIndex.Fill(0);
58  StartIndex.Fill(0);
59  for (unsigned i = 0; i<VDimension;i++)
60  {
61  LastIndex[i] = (IndexValueType)(length*Direction[i]);
62  }
63  // Find the dominant direction
64  IndexValueType maxDistance = 0;
65  unsigned int maxDistanceDimension = 0;
66  for (unsigned i = 0; i<VDimension;i++)
67  {
68  IndexValueType distance = abs(LastIndex[i]);
69  if (distance > maxDistance)
70  {
71  maxDistance = distance;
72  maxDistanceDimension = i;
73  }
74  m_IncrementError[i] = 2*distance;
75  m_OverflowIncrement[i] = (LastIndex[i] < 0 ? -1 : 1);
76  }
77  m_MainDirection = maxDistanceDimension;
78  m_MaximalError.Fill(maxDistance);
79  m_ReduceErrorAfterIncrement.Fill(2*maxDistance);
80  m_AccumulateError.Fill(0);
81  unsigned int steps = 1;
82  result[0] = m_CurrentImageIndex - StartIndex;
83  while (steps < length)
84  {
85  // This part is from ++ in LineConstIterator
86  // We need to modify m_AccumulateError, m_CurrentImageIndex, m_IsAtEnd
87  for (unsigned int i = 0; i < VDimension; ++i)
88  {
89  if (i == m_MainDirection)
90  {
91 
92  m_CurrentImageIndex[i] += m_OverflowIncrement[i];
93  }
94  else
95  {
96  m_AccumulateError[i] += m_IncrementError[i];
97  if (m_AccumulateError[i] >= m_MaximalError[i])
98  {
99  m_CurrentImageIndex[i] += m_OverflowIncrement[i];
100  m_AccumulateError[i] -= m_ReduceErrorAfterIncrement[i];
101  }
102  }
103  }
104 
105  result[steps] = m_CurrentImageIndex - StartIndex; // produce an offset
106 
107  ++steps;
108  }
109  return(result);
110 }
111 
112 } // namespace itk
113 
114 
115 #endif

Generated at Sat Feb 2 2013 23:30:29 for Orfeo Toolbox with doxygen 1.8.1.1