Fawkes API  Fawkes Development Version
line_grid.cpp
1 
2 /***************************************************************************
3  * line_grid.cpp - Implementation of the line grid scanline model
4  *
5  * Created: Wed Mar 25 17:31:00 2009
6  * Copyright 2009 Christof Rath <c.rath@student.tugraz.at>
7  *
8  ****************************************************************************/
9 
10 /* This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version. A runtime exception applies to
14  * this software (see LICENSE.GPL_WRE file mentioned below for details).
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  * GNU Library General Public License for more details.
20  *
21  * Read the full text in the LICENSE.GPL_WRE file in the doc directory.
22  */
23 
24 #include <fvmodels/scanlines/line_grid.h>
25 
26 #include <fvutils/base/roi.h>
27 #include <fvutils/draw/drawer.h>
28 #include <core/exceptions/software.h>
29 
30 #include <cstring>
31 
32 using fawkes::upoint_t;
33 
34 namespace firevision {
35 #if 0 /* just to make Emacs auto-indent happy */
36 }
37 #endif
38 
39 /** @class ScanlineLineGrid <fvmodels/scanlines/line_grid.h>
40  * Grid of scan lines.
41  * A grid of scan lines (i.e. horizontal and/or vertical lines) instead of only
42  * points on the grid crossings.
43  * The behavior of the ScanlineGrid (grid.h) class can be modeled if offset_hor
44  * is set to the same value as offset_x in the Grid class, offset_ver = 0 and
45  * gap is set to offset_y - 1. The advantage of doing this is a performance gain
46  * as the LineGrid is pre-calculated and getting the next point is only an
47  * iterator increment.
48  */
49 
50 /** Constructor.
51  * @param width Width of grid (most likely equal to image_width)
52  * @param height Height of grid (most likely equal to image_height)
53  * @param offset_hor Offset between horizontal lines (set to 0 to get only vertical lines)
54  * @param offset_ver Offset between vertical lines (set to 0 to get only horizontal lines)
55  * @param roi The grid will only be calculated within the roi (if NULL the grid gets
56  * calculated over the complete width/height).
57  * The provided object will be deleted by ScanlineLineGrid!
58  * @param gap Gap between two points on the line
59  */
60 ScanlineLineGrid::ScanlineLineGrid(unsigned int width, unsigned int height,
61  unsigned int offset_hor, unsigned int offset_ver,
62  ROI* roi, unsigned int gap)
63 {
64  __roi = NULL;
65  __next_pixel = gap + 1;
66  set_grid_params(width, height,
67  offset_hor, offset_ver, roi);
68  //reset is done in set_grid_params ()
69 }
70 
71 /** Destructor
72  */
74 {
75  delete __roi;
76 }
77 
80 {
81  return *__cur;
82 }
83 
84 upoint_t*
86 {
87  return &*__cur;
88 }
89 
90 void
91 ScanlineLineGrid::calc_coords()
92 {
93  __point_list.clear();
94  bool more_to_come = true;
95  upoint_t coord;
96  unsigned int next_px;
97 
98  if (__offset_hor > 0) //horizontal lines
99  {
100  more_to_come = true;
101  next_px = std::min(__next_pixel, __offset_ver ? __offset_ver : __width);
102  coord.x = __roi->start.x;
103  coord.y = __roi->start.y + ((__roi->height - 1) % __offset_hor) / 2; //Center the horizontal lines in the image
104  __point_list.push_back(coord);
105 
106  while (more_to_come) {
107  if (coord.x < (__roi->image_width - next_px))
108  {
109  coord.x += next_px;
110  }
111  else
112  {
113  if (coord.y < (__roi->image_height - __offset_hor))
114  {
115  coord.x = __roi->start.x;
116  coord.y += __offset_hor;
117  }
118  else
119  {
120  more_to_come = false;
121  }
122  }
123 
124  if (more_to_come) __point_list.push_back(coord);
125  }
126  }
127 
128  if (__offset_ver > 0) //vertical lines
129  {
130  more_to_come = true;
131  next_px = std::min(__next_pixel, __offset_hor ? __offset_hor : __height);
132  coord.x = __roi->start.x + ((__roi->width - 1) % __offset_ver) / 2; //Center the vertical lines in the image
133  coord.y = __roi->start.y;
134  __point_list.push_back(coord);
135 
136  while (more_to_come) {
137  if (coord.y < (__roi->image_height - next_px))
138  {
139  coord.y += next_px;
140  }
141  else
142  {
143  if (coord.x < (__roi->image_width - __offset_ver))
144  {
145  coord.x += __offset_ver;
146  coord.y = __roi->start.y;
147  }
148  else
149  {
150  more_to_come = false;
151  }
152  }
153 
154  if (more_to_come) __point_list.push_back(coord);
155  }
156  }
157 
158  reset();
159 }
160 
161 upoint_t *
163 {
164  if (__cur != __point_list.end()) ++__cur;
165  return __cur != __point_list.end() ? &*__cur : &__point_list.back();
166 }
167 
168 upoint_t *
170 {
171  if (__cur != __point_list.end()) {
172  upoint_t *res = &*__cur++;
173  return res;
174  }
175  else return &__point_list.back();
176 }
177 
178 bool
180 {
181  return __cur == __point_list.end();
182 }
183 
184 void
186 {
187  __cur = __point_list.begin();
188 }
189 
190 const char *
192 {
193  return "ScanlineModel::LineGrid";
194 }
195 
196 
197 unsigned int
199 {
200  return std::max(__offset_ver, __offset_hor);
201 }
202 
203 
204 void
205 ScanlineLineGrid::set_robot_pose(float x, float y, float ori)
206 {
207  // ignored
208 }
209 
210 
211 void
212 ScanlineLineGrid::set_pan_tilt(float pan, float tilt)
213 {
214  // ignored
215 }
216 
217 
218 /** Sets the dimensions of the grid.
219  * Set width and height of scanline grid. Implicitly resets the grid.
220  *
221  * @param width Width of grid (most likely equal to image_width)
222  * @param height Height of grid (most likely equal to image_height)
223  * @param roi The grid will only be calculated within the roi (if NULL the grid gets
224  * calculated over the complete width/height).
225  * The provided object will be deleted by ScanlineLineGrid!
226  */
227 void
228 ScanlineLineGrid::set_dimensions(unsigned int width, unsigned int height, ROI* roi)
229 {
230  __width = width;
231  __height = height;
232 
233  set_roi(roi);
234 }
235 
236 /** Sets the region-of-interest.
237  * @param roi The grid will only be calculated within the roi (if NULL the grid gets
238  * calculated over the complete width/height).
239  * The provided object will be deleted by ScanlineLineGrid!
240  */
241 void
243 {
244  delete __roi;
245 
246  if (!roi) __roi = new ROI(0, 0, __width, __height, __width, __height);
247  else
248  {
249  __roi = roi;
250  //Use roi image width/height as grid boundary
251  __roi->set_image_width(__roi->start.x + __roi->width);
252  __roi->set_image_height(__roi->start.y + __roi->height);
253 
254  if (__roi->image_width > __width)
255  throw fawkes::OutOfBoundsException("ScanlineLineGrid: ROI is out of grid bounds!", __roi->image_width, 0, __width);
256  if (__roi->image_height > __height)
257  throw fawkes::OutOfBoundsException("ScanlineLineGrid: ROI is out of grid bounds!", __roi->image_height, 0, __height);
258  }
259 
260  calc_coords();
261 }
262 
263 /** Sets offset.
264  * Set horizontal and vertical offset by which the pointer in the grid is advanced.
265  * This function implicitly resets the grid.
266  *
267  * @param offset_hor Offset between horizontal lines (set to 0 to get only vertical lines)
268  * @param offset_ver Offset between vertical lines (set to 0 to get only horizontal lines)
269  */
270 void
271 ScanlineLineGrid::set_offset(unsigned int offset_hor, unsigned int offset_ver)
272 {
273  __offset_hor = offset_hor;
274  __offset_ver = offset_ver;
275 
276  calc_coords();
277 }
278 
279 
280 /** Set all grid parameters.
281  * Set width, height, horizontal and vertical offset by which the pointer in the
282  * grid is advanced.
283  * Implicitly resets the grid.
284  *
285  * @param width Width of grid (most likely equal to image_width)
286  * @param height Height of grid (most likely equal to image_height)
287  * @param offset_hor Offset between horizontal lines (set to 0 to get only vertical lines)
288  * @param offset_ver Offset between vertical lines (set to 0 to get only horizontal lines)
289  * @param roi The grid will only be calculated within the roi (if NULL the grid gets
290  * calculated over the complete width/height).
291  * The provided object will be deleted by ScanlineLineGrid!
292  */
293 void
294 ScanlineLineGrid::set_grid_params(unsigned int width, unsigned int height,
295  unsigned int offset_hor, unsigned int offset_ver,
296  ROI* roi)
297 {
298  __offset_hor = offset_hor;
299  __offset_ver = offset_ver;
300 
301  set_dimensions(width, height, roi);
302 }
303 
304 } // end namespace firevision
virtual ~ScanlineLineGrid()
Destructor.
Definition: line_grid.cpp:73
void reset()
Reset model.
Definition: line_grid.cpp:185
fawkes::upoint_t start
ROI start.
Definition: roi.h:119
unsigned int y
y coordinate
Definition: types.h:36
unsigned int x
x coordinate
Definition: types.h:35
unsigned int width
ROI width.
Definition: roi.h:121
virtual void set_offset(unsigned int offset_x, unsigned int offset_y)
Sets offset.
Definition: line_grid.cpp:271
Region of interest.
Definition: roi.h:58
fawkes::upoint_t operator*()
Get the current coordinate.
Definition: line_grid.cpp:79
void set_image_height(unsigned int image_height)
Set full image height Set the height of the image that contains this ROI.
Definition: roi.cpp:210
unsigned int image_width
width of image that contains this ROI
Definition: roi.h:125
virtual void set_dimensions(unsigned int width, unsigned int height, ROI *roi=NULL)
Sets the dimensions of the grid.
Definition: line_grid.cpp:228
bool finished()
Check if all desired points have been processed.
Definition: line_grid.cpp:179
unsigned int image_height
height of image that contains this ROI
Definition: roi.h:127
void set_image_width(unsigned int image_width)
Set full image width.
Definition: roi.cpp:188
virtual void set_robot_pose(float x, float y, float ori)
Set the robot&#39;s pose.
Definition: line_grid.cpp:205
const char * get_name()
Get name of scanline model.
Definition: line_grid.cpp:191
Point with cartesian coordinates as unsigned integers.
Definition: types.h:34
unsigned int get_margin()
Get margin around points.
Definition: line_grid.cpp:198
virtual void set_roi(ROI *roi=NULL)
Sets the region-of-interest.
Definition: line_grid.cpp:242
unsigned int height
ROI height.
Definition: roi.h:123
fawkes::upoint_t * operator++()
Postfix ++ operator.
Definition: line_grid.cpp:162
Index out of bounds.
Definition: software.h:88
virtual void set_pan_tilt(float pan, float tilt)
Set camera&#39;s pan/tilt values.
Definition: line_grid.cpp:212
fawkes::upoint_t * operator->()
Get pointer to current point.
Definition: line_grid.cpp:85
ScanlineLineGrid(unsigned int width, unsigned int height, unsigned int offset_hor, unsigned int offset_ver, ROI *roi=NULL, unsigned int gap=0)
Constructor.
Definition: line_grid.cpp:60
virtual void set_grid_params(unsigned int width, unsigned int height, unsigned int offset_hor, unsigned int offset_ver, ROI *roi=NULL)
Set all grid parameters.
Definition: line_grid.cpp:294