Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
Bresenham.hh
1 /*
2 This file is part of the BIAS library (Basic ImageAlgorithmS).
3 
4 Copyright (C) 2003-2009 (see file CONTACT for details)
5  Multimediale Systeme der Informationsverarbeitung
6  Institut fuer Informatik
7  Christian-Albrechts-Universitaet Kiel
8 
9 
10 BIAS 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 2.1 of the License, or
13 (at your option) any later version.
14 
15 BIAS 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 BIAS; if not, write to the Free Software
22 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 */
24 
25 
26 #ifndef __Bresenham_hh__
27 #define __Bresenham_hh__
28 
29 #include <bias_config.h>
30 
31 
32 #include <Base/ImageUtils/EightWaySymmetry.hh>
33 
34 
35 namespace BIAS{
36  /** @class Bresenham
37  * @brief Scans a line using Bresenhams integer arithmetic algorithm.
38  * Either Init() or the according constructor have to be called befor GetNext().
39  * @author Felix Woelk
40  * @ingroup g_image_other
41  */
42  class Bresenham {
43  public:
44  inline Bresenham();
45 
46  inline Bresenham(const int start[2], const int end[2]);
47 
48  inline Bresenham(const unsigned int start[2], const unsigned int end[2]);
49 
50  inline Bresenham(const double start[2], const double end[2]);
51 
52  inline ~Bresenham();
53 
54  inline void Init(const int start[2], const int end[2]);
55 
56  inline void Init(const unsigned int start[2], const unsigned int end[2])
57  { Init((int *)start, (int *)end); };
58 
59  inline void Init(const double start[2], const double end[2]);
60 
61  inline bool GetNext(int next[2]);
62 
63  inline bool GetNext(unsigned int next[2])
64  { return GetNext((int *)next); };
65 
66  inline bool GetNext(double next[2]);
67 
68  protected:
70 
72 
73  int _iStart[2];
74  int _iEnd[2];
78  int _iDx;
79  int _iDy;
80  double _dDx;
81  double _dDy;
82  long int _iEps;
83 #ifdef BIAS_DEBUG
85 #endif
86  };
87 
88 
89 
90 
92  {
93 #ifdef BIAS_DEBUG
94  _bInitialized=false;
95 #endif
96  }
97 
99  {}
100 
101  Bresenham::Bresenham(const int start[2], const int end[2])
102  {
103  Init(start, end);
104 #ifdef BIAS_DEBUG
105  _bInitialized=true;
106 #endif
107  }
108 
109  Bresenham::Bresenham(const unsigned int start[2], const unsigned int end[2])
110  {
111  Init(start, end);
112 #ifdef BIAS_DEBUG
113  _bInitialized=true;
114 #endif
115  }
116 
117  Bresenham::Bresenham(const double start[2], const double end[2])
118  {
119  Init(start, end);
120 #ifdef BIAS_DEBUG
121  _bInitialized=true;
122 #endif
123  }
124 
125  void Bresenham::Init(const int start[2], const int end[2])
126  {
127  _iStart[0]=start[0];
128  _iStart[1]=start[1];
129  _iEnd[0]=end[0];
130  _iEnd[1]=end[1];
131  _eLineType=_iSym.DetermineLineType(start[0], start[1], end[0], end[1]);
136  _iDy = _iTransformedEnd[1] - _iTransformedStart[1];
137  // let GetNext return the starting point first
138  _iEps = -_iDy;
140  _dDx = _dDy = 0.0;
141 #ifdef BIAS_DEBUG
142  _bInitialized=true;
143 #endif
144  }
145 
146  void Bresenham::Init(const double start[2], const double end[2])
147  {
148  // fixes uninitialized usage warning, compiler does not notice
149  // that this gets ultimately initialized via call-by-reference
150  double transfstart[2] = { 0.0, 0.0 };
152 
153  _iStart[0]=(int)rint(start[0]);
154  _iStart[1]=(int)rint(start[1]);
155  _iEnd[0]=(int)rint(end[0]);
156  _iEnd[1]=(int)rint(end[1]);
157 
159  _iEnd[1]);
160  /* std::cerr <<"start: ("<<start[0]<<", "<<start[1]<<") end :("
161  <<end[0]<<", "<<end[1]<<") line type: "<<_eLineType<<std::endl;*/
166  _iDy = _iTransformedEnd[1] - _iTransformedStart[1];
167  dSym.Transform(_eLineType, start, transfstart);
168 
169  _dDx=(double)_iTransformedStart[0]-transfstart[0];
170  _dDy=(double)_iTransformedStart[1]-transfstart[1];
171  _iEps = (int)rint(((double)(_iDy)*_dDx)+((double)(_iDy)*_dDy));
172  // let GetNext return the starting point first
173  _iEps -= _iDy;
175  /* std::cerr <<"_iStart: (" <<_iStart[0]<<", "<<_iStart[1]
176  <<") transfstart: ("<<transfstart[0]<<", "<<transfstart[1]
177  <<") _iDx: "<<_iDx<<" _iDy: "<<_iDy
178  <<" _dDx: "<<_dDx<<" _dDy: "<<_dDy<<" _iEps: "<<_iEps<<std::endl;*/
179 
180 #ifdef BIAS_DEBUG
181  _bInitialized=true;
182 #endif
183  }
184 
185  bool Bresenham::GetNext(int next[2])
186  {
187 #ifdef BIAS_DEBUG
188  if (!_bInitialized){
189  BIASERR("call Init befor GetNext");
190  BIASABORT;
191  }
192 #endif
193  bool result=true;
195  _iEps += _iDy;
196  if ((_iEps << 1) >= _iDx){
198  _iEps -= _iDx;
199  }
202 #ifdef BIAS_DEBUG
203  _bInitialized=false;
204 #endif
205  result=false;
206  } else {
208  result=true;
209  }
210  return result;
211  }
212 
213  bool Bresenham::GetNext(double next[2])
214  {
215  bool result;
216  int inext[2];
217  result = GetNext(inext);
218  next[0]=(double)inext[0] + _dDx;
219  next[1]=(double)inext[1] + _dDy;
220 
221  return result;
222  }
223 
224 } // namespace
225 
226 
227 #endif // __Bresenham_hh__
void InverseTransform(enum TLineType &type, const T &Transformedx, const T &Transformedy, T &Originalx, T &Originaly)
executes the inverse transformation to the above
int _iTransformedCurrent[2]
Definition: Bresenham.hh:75
enum TLineType DetermineLineType(double &angle)
assumes angle is between (-pi, pi] as given by atan2
long int _iEps
Definition: Bresenham.hh:82
int _iStart[2]
Definition: Bresenham.hh:73
EightWaySymmetry< int > _iSym
Definition: Bresenham.hh:69
bool GetNext(unsigned int next[2])
Definition: Bresenham.hh:63
bool _bInitialized
Definition: Bresenham.hh:84
int _iTransformedEnd[2]
Definition: Bresenham.hh:77
void Init(const unsigned int start[2], const unsigned int end[2])
Definition: Bresenham.hh:56
bool GetNext(int next[2])
Definition: Bresenham.hh:185
int _iTransformedStart[2]
Definition: Bresenham.hh:76
void Transform(enum TLineType &type, const T &Originalx, const T &Originaly, T &Transformedx, T &Transformedy)
transforms Original according to type in a way that Transformed lies on a line with slope in [0...
Scans a line using Bresenhams integer arithmetic algorithm.
Definition: Bresenham.hh:42
enum TLineType _eLineType
Definition: Bresenham.hh:71
void Init(const int start[2], const int end[2])
Definition: Bresenham.hh:125
TLineType
direction of line if start is in coordinate origin as given by compass