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 <core/exceptions/software.h>
25 #include <fvmodels/scanlines/line_grid.h>
26 #include <fvutils/base/roi.h>
27 #include <fvutils/draw/drawer.h>
28 
29 #include <cstring>
30 
31 using fawkes::upoint_t;
32 
33 namespace firevision {
34 
35 /** @class ScanlineLineGrid <fvmodels/scanlines/line_grid.h>
36  * Grid of scan lines.
37  * A grid of scan lines (i.e. horizontal and/or vertical lines) instead of only
38  * points on the grid crossings.
39  * The behavior of the ScanlineGrid (grid.h) class can be modeled if offset_hor
40  * is set to the same value as offset_x in the Grid class, offset_ver = 0 and
41  * gap is set to offset_y - 1. The advantage of doing this is a performance gain
42  * as the LineGrid is pre-calculated and getting the next point is only an
43  * iterator increment.
44  */
45 
46 /** Constructor.
47  * @param width Width of grid (most likely equal to image_width)
48  * @param height Height of grid (most likely equal to image_height)
49  * @param offset_hor Offset between horizontal lines (set to 0 to get only vertical lines)
50  * @param offset_ver Offset between vertical lines (set to 0 to get only horizontal lines)
51  * @param roi The grid will only be calculated within the roi (if NULL the grid gets
52  * calculated over the complete width/height).
53  * The provided object will be deleted by ScanlineLineGrid!
54  * @param gap Gap between two points on the line
55  */
57  unsigned int height,
58  unsigned int offset_hor,
59  unsigned int offset_ver,
60  ROI * roi,
61  unsigned int gap)
62 {
63  roi_ = NULL;
64  next_pixel_ = gap + 1;
65  set_grid_params(width, height, offset_hor, offset_ver, roi);
66  //reset is done in set_grid_params ()
67 }
68 
69 /** Destructor
70  */
72 {
73  delete roi_;
74 }
75 
77 {
78  return *cur_;
79 }
80 
82 {
83  return &*cur_;
84 }
85 
86 void
87 ScanlineLineGrid::calc_coords()
88 {
89  point_list_.clear();
90  bool more_to_come = true;
91  upoint_t coord;
92  unsigned int next_px;
93 
94  if (offset_hor_ > 0) //horizontal lines
95  {
96  more_to_come = true;
97  next_px = std::min(next_pixel_, offset_ver_ ? offset_ver_ : width_);
98  coord.x = roi_->start.x;
99  coord.y = roi_->start.y
100  + ((roi_->height - 1) % offset_hor_) / 2; //Center the horizontal lines in the image
101  point_list_.push_back(coord);
102 
103  while (more_to_come) {
104  if (coord.x < (roi_->image_width - next_px)) {
105  coord.x += next_px;
106  } else {
107  if (coord.y < (roi_->image_height - offset_hor_)) {
108  coord.x = roi_->start.x;
109  coord.y += offset_hor_;
110  } else {
111  more_to_come = false;
112  }
113  }
114 
115  if (more_to_come)
116  point_list_.push_back(coord);
117  }
118  }
119 
120  if (offset_ver_ > 0) //vertical lines
121  {
122  more_to_come = true;
123  next_px = std::min(next_pixel_, offset_hor_ ? offset_hor_ : height_);
124  coord.x = roi_->start.x
125  + ((roi_->width - 1) % offset_ver_) / 2; //Center the vertical lines in the image
126  coord.y = roi_->start.y;
127  point_list_.push_back(coord);
128 
129  while (more_to_come) {
130  if (coord.y < (roi_->image_height - next_px)) {
131  coord.y += next_px;
132  } else {
133  if (coord.x < (roi_->image_width - offset_ver_)) {
134  coord.x += offset_ver_;
135  coord.y = roi_->start.y;
136  } else {
137  more_to_come = false;
138  }
139  }
140 
141  if (more_to_come)
142  point_list_.push_back(coord);
143  }
144  }
145 
146  reset();
147 }
148 
149 upoint_t *
151 {
152  if (cur_ != point_list_.end())
153  ++cur_;
154  return cur_ != point_list_.end() ? &*cur_ : &point_list_.back();
155 }
156 
157 upoint_t *
159 {
160  if (cur_ != point_list_.end()) {
161  upoint_t *res = &*cur_++;
162  return res;
163  } else
164  return &point_list_.back();
165 }
166 
167 bool
169 {
170  return cur_ == point_list_.end();
171 }
172 
173 void
175 {
176  cur_ = point_list_.begin();
177 }
178 
179 const char *
181 {
182  return "ScanlineModel::LineGrid";
183 }
184 
185 unsigned int
187 {
188  return std::max(offset_ver_, offset_hor_);
189 }
190 
191 void
192 ScanlineLineGrid::set_robot_pose(float x, float y, float ori)
193 {
194  // ignored
195 }
196 
197 void
198 ScanlineLineGrid::set_pan_tilt(float pan, float tilt)
199 {
200  // ignored
201 }
202 
203 /** Sets the dimensions of the grid.
204  * Set width and height of scanline grid. Implicitly resets the grid.
205  *
206  * @param width Width of grid (most likely equal to image_width)
207  * @param height Height of grid (most likely equal to image_height)
208  * @param roi The grid will only be calculated within the roi (if NULL the grid gets
209  * calculated over the complete width/height).
210  * The provided object will be deleted by ScanlineLineGrid!
211  */
212 void
213 ScanlineLineGrid::set_dimensions(unsigned int width, unsigned int height, ROI *roi)
214 {
215  width_ = width;
216  height_ = height;
217 
218  set_roi(roi);
219 }
220 
221 /** Sets the region-of-interest.
222  * @param roi The grid will only be calculated within the roi (if NULL the grid gets
223  * calculated over the complete width/height).
224  * The provided object will be deleted by ScanlineLineGrid!
225  */
226 void
228 {
229  delete roi_;
230 
231  if (!roi)
232  roi_ = new ROI(0, 0, width_, height_, width_, height_);
233  else {
234  roi_ = roi;
235  //Use roi image width/height as grid boundary
236  roi_->set_image_width(roi_->start.x + roi_->width);
237  roi_->set_image_height(roi_->start.y + roi_->height);
238 
239  if (roi_->image_width > width_)
240  throw fawkes::OutOfBoundsException("ScanlineLineGrid: ROI is out of grid bounds!",
241  roi_->image_width,
242  0,
243  width_);
244  if (roi_->image_height > height_)
245  throw fawkes::OutOfBoundsException("ScanlineLineGrid: ROI is out of grid bounds!",
246  roi_->image_height,
247  0,
248  height_);
249  }
250 
251  calc_coords();
252 }
253 
254 /** Sets offset.
255  * Set horizontal and vertical offset by which the pointer in the grid is advanced.
256  * This function implicitly resets the grid.
257  *
258  * @param offset_hor Offset between horizontal lines (set to 0 to get only vertical lines)
259  * @param offset_ver Offset between vertical lines (set to 0 to get only horizontal lines)
260  */
261 void
262 ScanlineLineGrid::set_offset(unsigned int offset_hor, unsigned int offset_ver)
263 {
264  offset_hor_ = offset_hor;
265  offset_ver_ = offset_ver;
266 
267  calc_coords();
268 }
269 
270 /** Set all grid parameters.
271  * Set width, height, horizontal and vertical offset by which the pointer in the
272  * grid is advanced.
273  * Implicitly resets the grid.
274  *
275  * @param width Width of grid (most likely equal to image_width)
276  * @param height Height of grid (most likely equal to image_height)
277  * @param offset_hor Offset between horizontal lines (set to 0 to get only vertical lines)
278  * @param offset_ver Offset between vertical lines (set to 0 to get only horizontal lines)
279  * @param roi The grid will only be calculated within the roi (if NULL the grid gets
280  * calculated over the complete width/height).
281  * The provided object will be deleted by ScanlineLineGrid!
282  */
283 void
285  unsigned int height,
286  unsigned int offset_hor,
287  unsigned int offset_ver,
288  ROI * roi)
289 {
290  offset_hor_ = offset_hor;
291  offset_ver_ = offset_ver;
292 
293  set_dimensions(width, height, roi);
294 }
295 
296 } // end namespace firevision
virtual ~ScanlineLineGrid()
Destructor.
Definition: line_grid.cpp:71
void reset()
Reset model.
Definition: line_grid.cpp:174
fawkes::upoint_t start
ROI start.
Definition: roi.h:115
unsigned int y
y coordinate
Definition: types.h:37
unsigned int x
x coordinate
Definition: types.h:36
unsigned int width
ROI width.
Definition: roi.h:117
virtual void set_offset(unsigned int offset_x, unsigned int offset_y)
Sets offset.
Definition: line_grid.cpp:262
Region of interest.
Definition: roi.h:54
fawkes::upoint_t operator*()
Get the current coordinate.
Definition: line_grid.cpp:76
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:197
unsigned int image_width
width of image that contains this ROI
Definition: roi.h:121
virtual void set_dimensions(unsigned int width, unsigned int height, ROI *roi=NULL)
Sets the dimensions of the grid.
Definition: line_grid.cpp:213
bool finished()
Check if all desired points have been processed.
Definition: line_grid.cpp:168
unsigned int image_height
height of image that contains this ROI
Definition: roi.h:123
void set_image_width(unsigned int image_width)
Set full image width.
Definition: roi.cpp:177
virtual void set_robot_pose(float x, float y, float ori)
Set the robot's pose.
Definition: line_grid.cpp:192
const char * get_name()
Get name of scanline model.
Definition: line_grid.cpp:180
Point with cartesian coordinates as unsigned integers.
Definition: types.h:34
unsigned int get_margin()
Get margin around points.
Definition: line_grid.cpp:186
virtual void set_roi(ROI *roi=NULL)
Sets the region-of-interest.
Definition: line_grid.cpp:227
unsigned int height
ROI height.
Definition: roi.h:119
fawkes::upoint_t * operator++()
Postfix ++ operator.
Definition: line_grid.cpp:150
Index out of bounds.
Definition: software.h:85
virtual void set_pan_tilt(float pan, float tilt)
Set camera's pan/tilt values.
Definition: line_grid.cpp:198
fawkes::upoint_t * operator->()
Get pointer to current point.
Definition: line_grid.cpp:81
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:56
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:284