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