Fawkes API  Fawkes Development Version
grid.cpp
1 
2 /***************************************************************************
3  * grid.cpp - Implementation of the grid scanline model
4  *
5  * Created: Tue Feb 22 10:36:39 2005
6  * Copyright 2005 Tim Niemueller [www.niemueller.de]
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/grid.h>
26 
27 #include <cstring>
28 
29 using fawkes::upoint_t;
30 
31 namespace firevision {
32 
33 /** @class ScanlineGrid <fvmodels/scanlines/grid.h>
34  * Scanline Grid.
35  * A grid as scanline points. The crossings of the lines are the scanline
36  * points.
37  */
38 
39 /** Constructor.
40  * @param width width of grid
41  * @param height height of grid
42  * @param offset_x x offset between lines
43  * @param offset_y y offset between lines
44  * @param roi the grid will only be calculated within the roi (if NULL the roi
45  * will be from 0,0 to width,height).
46  * @param horizontal_grid if true x will be increased before y
47  */
48 ScanlineGrid::ScanlineGrid(unsigned int width,
49  unsigned int height,
50  unsigned int offset_x,
51  unsigned int offset_y,
52  ROI * roi,
53  bool horizontal_grid)
54 {
55  this->roi = NULL;
56  setGridParams(width, height, offset_x, offset_y, roi, horizontal_grid);
57  //reset is done in setGridParams ()
58 }
59 
60 /** Destructor
61  */
63 {
64  delete this->roi;
65 }
66 
69 {
70  return coord;
71 }
72 
73 upoint_t *
75 {
76  return &coord;
77 }
78 
79 void
80 ScanlineGrid::calc_next_coord()
81 {
82  if (finished())
83  return;
84 
85  if (horizontal_grid) {
86  if (static_cast<int>(coord.x) < static_cast<int>(roi->image_width - offset_x)) {
87  coord.x += offset_x;
88  } else {
89  if (static_cast<int>(coord.y) < static_cast<int>(roi->image_height - offset_y)) {
90  coord.x = roi->start.x;
91  coord.y += offset_y;
92  } else {
93  more_to_come = false;
94  }
95  }
96  } else // vertical grid
97  {
98  if (static_cast<int>(coord.y) < static_cast<int>(roi->image_height - offset_y)) {
99  coord.y += offset_y;
100  } else {
101  if (static_cast<int>(coord.x) < static_cast<int>(roi->image_width - offset_x)) {
102  coord.x += offset_x;
103  coord.y = roi->start.y;
104  } else {
105  more_to_come = false;
106  }
107  }
108  }
109 }
110 
111 upoint_t *
113 {
114  calc_next_coord();
115  return &coord;
116 }
117 
118 upoint_t *
120 {
121  memcpy(&tmp_coord, &coord, sizeof(upoint_t));
122  calc_next_coord();
123  return &tmp_coord;
124 }
125 
126 bool
128 {
129  return !more_to_come;
130 }
131 
132 void
134 {
135  coord.x = roi->start.x;
136  coord.y = roi->start.y;
137 
138  more_to_come = true;
139 }
140 
141 const char *
143 {
144  return "ScanlineModel::Grid";
145 }
146 
147 unsigned int
149 {
150  return (offset_x > offset_y) ? offset_x : offset_y;
151 }
152 
153 void
154 ScanlineGrid::set_robot_pose(float x, float y, float ori)
155 {
156  // ignored
157 }
158 
159 void
160 ScanlineGrid::set_pan_tilt(float pan, float tilt)
161 {
162  // ignored
163 }
164 
165 void
167 {
168  if (!roi)
169  this->roi = new ROI(0, 0, this->width, this->height, this->width, this->height);
170  else {
171  if (!this->roi) {
172  this->roi = new ROI(roi);
173  } else {
174  *(this->roi) = *roi;
175  }
176  //Use roi's image width/height as grid boundary (to simplify the "exceeds-boundaries"-test)
177  this->roi->image_width = this->roi->start.x + this->roi->width;
178  this->roi->image_height = this->roi->start.y + this->roi->height;
179 
180  if (this->roi->image_width > this->width)
181  throw fawkes::OutOfBoundsException("ScanlineGrid: ROI is out of grid bounds!",
182  this->roi->image_width,
183  0,
184  this->width);
185  if (this->roi->image_height > this->height)
186  throw fawkes::OutOfBoundsException("ScanlineGrid: ROI is out of grid bounds!",
187  this->roi->image_height,
188  0,
189  this->height);
190  }
191 
192  reset();
193 }
194 
195 /** Set dimensions.
196  * Set width and height of scanline grid. Implicitly resets the grid.
197  * @param width width
198  * @param height height
199  * @param roi the grid will only be calculated within the roi (if NULL the roi
200  * will be from 0,0 to width,height). The object will be deleted by
201  * ScanlineGrid!
202  */
203 void
204 ScanlineGrid::setDimensions(unsigned int width, unsigned int height, ROI *roi)
205 {
206  this->width = width;
207  this->height = height;
208 
209  set_roi(roi);
210 }
211 
212 /** Set offset.
213  * Set X and Y offset by which the pointer in the grid is advanced. Implicitly resets the grid.
214  * @param offset_x offset_x
215  * @param offset_y offset_y
216  */
217 void
218 ScanlineGrid::setOffset(unsigned int offset_x, unsigned int offset_y)
219 {
220  this->offset_x = offset_x;
221  this->offset_y = offset_y;
222 
223  reset();
224 }
225 
226 /** Set all grid parameters.
227  * Set width, height, X and Y offset by which the pointer in the grid is advanced.
228  * Implicitly resets the grid.
229  * @param width width
230  * @param height height
231  * @param offset_x offset_x
232  * @param offset_y offset_y
233  * @param roi the grid will only be calculated within the roi (if NULL the roi
234  * will be from 0,0 to width,height). The object will be deleted by
235  * ScanlineGrid!
236  * @param horizontal_grid if true x will be increased before y
237  */
238 void
239 ScanlineGrid::setGridParams(unsigned int width,
240  unsigned int height,
241  unsigned int offset_x,
242  unsigned int offset_y,
243  ROI * roi,
244  bool horizontal_grid)
245 {
246  this->horizontal_grid = horizontal_grid;
247 
248  setDimensions(width, height, roi);
249  setOffset(offset_x, offset_y);
250 }
251 
252 } // 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
unsigned int image_width
width of image that contains this ROI
Definition: roi.h:121
unsigned int image_height
height of image that contains this ROI
Definition: roi.h:123
virtual ~ScanlineGrid()
Destructor.
Definition: grid.cpp:62
void reset()
Reset model.
Definition: grid.cpp:133
virtual void set_roi(ROI *roi=NULL)
Set the region-of-interest.
Definition: grid.cpp:166
virtual void set_robot_pose(float x, float y, float ori)
Set the robot's pose.
Definition: grid.cpp:154
ScanlineGrid(unsigned int width, unsigned int height, unsigned int offset_x, unsigned int offset_y, ROI *roi=NULL, bool horizontal_grid=true)
Constructor.
Definition: grid.cpp:48
virtual void set_pan_tilt(float pan, float tilt)
Set camera's pan/tilt values.
Definition: grid.cpp:160
unsigned int get_margin()
Get margin around points.
Definition: grid.cpp:148
void setDimensions(unsigned int width, unsigned int height, ROI *roi=NULL)
Set dimensions.
Definition: grid.cpp:204
const char * get_name()
Get name of scanline model.
Definition: grid.cpp:142
fawkes::upoint_t * operator->()
Get pointer to current point.
Definition: grid.cpp:74
bool finished()
Check if all desired points have been processed.
Definition: grid.cpp:127
fawkes::upoint_t operator*()
Get the current coordinate.
Definition: grid.cpp:68
void setOffset(unsigned int offset_x, unsigned int offset_y)
Set offset.
Definition: grid.cpp:218
void setGridParams(unsigned int width, unsigned int height, unsigned int offset_x, unsigned int offset_y, ROI *roi=NULL, bool horizontal_grid=true)
Set all grid parameters.
Definition: grid.cpp:239
fawkes::upoint_t * operator++()
Postfix ++ operator.
Definition: grid.cpp:112
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