Point Cloud Library (PCL)
1.15.1
Toggle main menu visibility
Loading...
Searching...
No Matches
pcl
geometry
line_iterator.h
1
/*
2
* Software License Agreement (BSD License)
3
*
4
* Copyright (c) 2010, Willow Garage, Inc.
5
* All rights reserved.
6
*
7
* Redistribution and use in source and binary forms, with or without
8
* modification, are permitted provided that the following conditions
9
* are met:
10
*
11
* * Redistributions of source code must retain the above copyright
12
* notice, this list of conditions and the following disclaimer.
13
* * Redistributions in binary form must reproduce the above
14
* copyright notice, this list of conditions and the following
15
* disclaimer in the documentation and/or other materials provided
16
* with the distribution.
17
* * Neither the name of Willow Garage, Inc. nor the names of its
18
* contributors may be used to endorse or promote products derived
19
* from this software without specific prior written permission.
20
*
21
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32
* POSSIBILITY OF SUCH DAMAGE.
33
*
34
*/
35
36
#pragma once
37
38
#include "organized_index_iterator.h"
39
40
namespace
pcl
{
41
/**
42
* \brief Organized Index Iterator for iterating over the "pixels" for a given line
43
* using the Bresenham algorithm. Supports 4 and 8 neighborhood connectivity
44
* \note iterator does not visit the given end-point (on purpose).
45
* \author Suat Gedikli <gedikli@willowgarage.com>
46
* \ingroup geometry
47
*/
48
class
LineIterator
:
public
OrganizedIndexIterator
{
49
public
:
50
/** \brief Neighborhood connectivity */
51
enum
Neighborhood
{
Neighbor4
= 4,
Neighbor8
= 8 };
52
53
public
:
54
/**
55
* \brief Constructor
56
* \param x_start column of the start pixel of the line
57
* \param y_start row of the start pixel of the line
58
* \param x_end column of the end pixel of the line
59
* \param y_end row of the end pixel of the line
60
* \param width width of the organized structure e.g. image/cloud/map etc..
61
* \param neighborhood connectivity of the neighborhood
62
*/
63
LineIterator
(
unsigned
x_start,
64
unsigned
y_start,
65
unsigned
x_end,
66
unsigned
y_end,
67
unsigned
width,
68
const
Neighborhood
& neighborhood =
Neighbor8
);
69
70
/** \brief Destructor*/
71
~LineIterator
()
override
;
72
73
void
74
operator++
()
override
;
75
unsigned
76
getRowIndex
()
const override
;
77
unsigned
78
getColumnIndex
()
const override
;
79
bool
80
isValid
()
const override
;
81
void
82
reset
()
override
;
83
84
protected
:
85
/**
86
* \brief initializes the variables for the Bresenham algorithm
87
* \param[in] neighborhood connectivity to the neighborhood. Either 4 or 8
88
*/
89
void
90
init
(
const
Neighborhood
& neighborhood);
91
92
/** \brief current column index*/
93
unsigned
x_
;
94
95
/** \brief current row index*/
96
unsigned
y_
;
97
98
/** \brief column index of first pixel/point*/
99
unsigned
x_start_
;
100
101
/** \brief row index of first pixel/point*/
102
unsigned
y_start_
;
103
104
/** \brief column index of end pixel/point*/
105
unsigned
x_end_
;
106
107
/** \brief row index of end pixel/point*/
108
unsigned
y_end_
;
109
110
// calculated values
111
/** \brief current distance to the line*/
112
int
error_
;
113
114
/** \brief error threshold*/
115
int
error_max_
;
116
117
/** \brief increment of error (distance) value in case of an y-step (if dx > dy)*/
118
int
error_minus_
;
119
120
/** \brief increment of error (distance) value in case of just an x-step (if dx >
121
* dy)*/
122
int
error_plus_
;
123
124
/** \brief increment of column index in case of just an x-step (if dx > dy)*/
125
int
x_plus_
;
126
127
/** \brief increment of row index in case of just an x-step (if dx > dy)*/
128
int
y_plus_
;
129
130
/** \brief increment of column index in case of just an y-step (if dx > dy)*/
131
int
x_minus_
;
132
133
/** \brief increment of row index in case of just an y-step (if dx > dy)*/
134
int
y_minus_
;
135
136
/** \brief increment pixel/point index in case of just an x-step (if dx > dy)*/
137
int
index_plus_
;
138
139
/** \brief increment pixel/point index in case of just an y-step (if dx > dy)*/
140
int
index_minus_
;
141
};
142
143
////////////////////////////////////////////////////////////////////////////////
144
////////////////////////////////////////////////////////////////////////////////
145
////////////////////////////////////////////////////////////////////////////////
146
147
////////////////////////////////////////////////////////////////////////////////
148
inline
LineIterator::LineIterator
(
unsigned
x_start,
149
unsigned
y_start,
150
unsigned
x_end,
151
unsigned
y_end,
152
unsigned
width,
153
const
Neighborhood
& neighborhood)
154
:
OrganizedIndexIterator
(width)
155
,
x_start_
(x_start)
156
,
y_start_
(y_start)
157
,
x_end_
(x_end)
158
,
y_end_
(y_end)
159
{
160
init
(neighborhood);
161
}
162
163
////////////////////////////////////////////////////////////////////////////////
164
inline
LineIterator::~LineIterator
() =
default
;
165
166
////////////////////////////////////////////////////////////////////////////////
167
inline
void
168
LineIterator::init
(
const
Neighborhood
& neighborhood)
169
{
170
x_
=
x_start_
;
171
y_
=
y_start_
;
172
index_
=
x_
*
width_
+
y_
;
173
error_
= 0;
174
175
int
delta_x =
x_end_
-
x_start_
;
176
int
delta_y =
y_end_
-
y_start_
;
177
178
int
x_dir = ((delta_x > 0) ? 1 : -1);
179
int
y_dir = ((delta_y > 0) ? 1 : -1);
180
181
delta_x *= x_dir;
182
delta_y *= y_dir;
183
184
if
(delta_x >= delta_y) {
185
if
(neighborhood ==
Neighbor4
) {
186
error_max_
= delta_x - delta_y;
187
x_minus_
= 0;
188
y_minus_
= y_dir;
189
x_plus_
= x_dir;
190
y_plus_
= 0;
191
192
error_minus_
= -(delta_x * 2);
193
error_plus_
= (delta_y * 2);
194
}
195
else
{
196
error_max_
= delta_x - (delta_y * 2);
197
x_minus_
= x_dir;
198
y_minus_
= y_dir;
199
x_plus_
= x_dir;
200
y_plus_
= 0;
201
202
error_minus_
= (delta_y - delta_x) * 2;
203
error_plus_
= (delta_y * 2);
204
}
205
}
206
else
{
207
if
(neighborhood ==
Neighbor4
) {
208
error_max_
= delta_y - delta_x;
209
x_minus_
= x_dir;
210
y_minus_
= 0;
211
x_plus_
= 0;
212
y_plus_
= y_dir;
213
214
error_minus_
= -(delta_y * 2);
215
error_plus_
= (delta_x * 2);
216
}
217
else
{
218
error_max_
= delta_y - (delta_x * 2);
219
x_minus_
= x_dir;
220
y_minus_
= y_dir;
221
x_plus_
= 0;
222
y_plus_
= y_dir;
223
224
error_minus_
= (delta_x - delta_y) * 2;
225
error_plus_
= (delta_x * 2);
226
}
227
}
228
229
index_minus_
=
x_minus_
+
y_minus_
*
width_
;
230
index_plus_
=
x_plus_
+
y_plus_
*
width_
;
231
}
232
233
////////////////////////////////////////////////////////////////////////////////
234
inline
void
235
LineIterator::operator++
()
236
{
237
if
(
error_
>=
error_max_
) {
238
x_
+=
x_minus_
;
239
y_
+=
y_minus_
;
240
error_
+=
error_minus_
;
241
index_
+=
index_minus_
;
242
}
243
else
{
244
x_
+=
x_plus_
;
245
y_
+=
y_plus_
;
246
error_
+=
error_plus_
;
247
index_
+=
index_plus_
;
248
}
249
}
250
251
////////////////////////////////////////////////////////////////////////////////
252
inline
unsigned
253
LineIterator::getRowIndex
()
const
254
{
255
return
y_
;
256
}
257
258
////////////////////////////////////////////////////////////////////////////////
259
inline
unsigned
260
LineIterator::getColumnIndex
()
const
261
{
262
return
x_
;
263
}
264
265
////////////////////////////////////////////////////////////////////////////////
266
inline
bool
267
LineIterator::isValid
()
const
268
{
269
return
(
x_
!=
x_end_
||
y_
!=
y_end_
);
270
}
271
272
////////////////////////////////////////////////////////////////////////////////
273
inline
void
274
LineIterator::reset
()
275
{
276
x_
=
x_start_
;
277
y_
=
y_start_
;
278
error_
= 0;
279
index_
=
x_
*
width_
+
y_
;
280
}
281
282
}
// namespace pcl
pcl::LineIterator::x_end_
unsigned x_end_
column index of end pixel/point
Definition
line_iterator.h:105
pcl::LineIterator::operator++
void operator++() override
go to next pixel/point in image/cloud
Definition
line_iterator.h:235
pcl::LineIterator::y_minus_
int y_minus_
increment of row index in case of just an y-step (if dx > dy)
Definition
line_iterator.h:134
pcl::LineIterator::getColumnIndex
unsigned getColumnIndex() const override
returns the col index (x-coordinate) of the current pixel/point
Definition
line_iterator.h:260
pcl::LineIterator::index_minus_
int index_minus_
increment pixel/point index in case of just an y-step (if dx > dy)
Definition
line_iterator.h:140
pcl::LineIterator::y_
unsigned y_
current row index
Definition
line_iterator.h:96
pcl::LineIterator::reset
void reset() override
resets the iterator to the beginning of the line
Definition
line_iterator.h:274
pcl::LineIterator::x_minus_
int x_minus_
increment of column index in case of just an y-step (if dx > dy)
Definition
line_iterator.h:131
pcl::LineIterator::getRowIndex
unsigned getRowIndex() const override
returns the row index (y-coordinate) of the current pixel/point
Definition
line_iterator.h:253
pcl::LineIterator::y_end_
unsigned y_end_
row index of end pixel/point
Definition
line_iterator.h:108
pcl::LineIterator::LineIterator
LineIterator(unsigned x_start, unsigned y_start, unsigned x_end, unsigned y_end, unsigned width, const Neighborhood &neighborhood=Neighbor8)
Constructor.
Definition
line_iterator.h:148
pcl::LineIterator::error_minus_
int error_minus_
increment of error (distance) value in case of an y-step (if dx > dy)
Definition
line_iterator.h:118
pcl::LineIterator::error_plus_
int error_plus_
increment of error (distance) value in case of just an x-step (if dx > dy)
Definition
line_iterator.h:122
pcl::LineIterator::~LineIterator
~LineIterator() override
Destructor.
pcl::LineIterator::init
void init(const Neighborhood &neighborhood)
initializes the variables for the Bresenham algorithm
Definition
line_iterator.h:168
pcl::LineIterator::Neighborhood
Neighborhood
Neighborhood connectivity.
Definition
line_iterator.h:51
pcl::LineIterator::Neighbor4
@ Neighbor4
Definition
line_iterator.h:51
pcl::LineIterator::Neighbor8
@ Neighbor8
Definition
line_iterator.h:51
pcl::LineIterator::y_plus_
int y_plus_
increment of row index in case of just an x-step (if dx > dy)
Definition
line_iterator.h:128
pcl::LineIterator::index_plus_
int index_plus_
increment pixel/point index in case of just an x-step (if dx > dy)
Definition
line_iterator.h:137
pcl::LineIterator::x_plus_
int x_plus_
increment of column index in case of just an x-step (if dx > dy)
Definition
line_iterator.h:125
pcl::LineIterator::x_
unsigned x_
current column index
Definition
line_iterator.h:93
pcl::LineIterator::y_start_
unsigned y_start_
row index of first pixel/point
Definition
line_iterator.h:102
pcl::LineIterator::error_max_
int error_max_
error threshold
Definition
line_iterator.h:115
pcl::LineIterator::error_
int error_
current distance to the line
Definition
line_iterator.h:112
pcl::LineIterator::x_start_
unsigned x_start_
column index of first pixel/point
Definition
line_iterator.h:99
pcl::LineIterator::isValid
bool isValid() const override
return whether the current visited pixel/point is valid or not.
Definition
line_iterator.h:267
pcl::OrganizedIndexIterator::OrganizedIndexIterator
OrganizedIndexIterator(unsigned width)
constructor
Definition
organized_index_iterator.h:112
pcl::OrganizedIndexIterator::width_
unsigned width_
the width of the image/cloud
Definition
organized_index_iterator.h:101
pcl::OrganizedIndexIterator::index_
unsigned index_
the index of the current pixel/point
Definition
organized_index_iterator.h:104
pcl
Definition
convolution.h:46