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 
68 {
69  return coord;
70 }
71 
73 {
74  return &coord;
75 }
76 
77 void
78 ScanlineGrid::calc_next_coord()
79 {
80  if (finished())
81  return;
82 
83  if (horizontal_grid) {
84  if (static_cast<int>(coord.x) < static_cast<int>(roi->image_width - offset_x)) {
85  coord.x += offset_x;
86  } else {
87  if (static_cast<int>(coord.y) < static_cast<int>(roi->image_height - offset_y)) {
88  coord.x = roi->start.x;
89  coord.y += offset_y;
90  } else {
91  more_to_come = false;
92  }
93  }
94  } else // vertical grid
95  {
96  if (static_cast<int>(coord.y) < static_cast<int>(roi->image_height - offset_y)) {
97  coord.y += offset_y;
98  } else {
99  if (static_cast<int>(coord.x) < static_cast<int>(roi->image_width - offset_x)) {
100  coord.x += offset_x;
101  coord.y = roi->start.y;
102  } else {
103  more_to_come = false;
104  }
105  }
106  }
107 }
108 
109 upoint_t *
111 {
112  calc_next_coord();
113  return &coord;
114 }
115 
116 upoint_t *
118 {
119  memcpy(&tmp_coord, &coord, sizeof(upoint_t));
120  calc_next_coord();
121  return &tmp_coord;
122 }
123 
124 bool
126 {
127  return !more_to_come;
128 }
129 
130 void
132 {
133  coord.x = roi->start.x;
134  coord.y = roi->start.y;
135 
136  more_to_come = true;
137 }
138 
139 const char *
141 {
142  return "ScanlineModel::Grid";
143 }
144 
145 unsigned int
147 {
148  return (offset_x > offset_y) ? offset_x : offset_y;
149 }
150 
151 void
152 ScanlineGrid::set_robot_pose(float x, float y, float ori)
153 {
154  // ignored
155 }
156 
157 void
158 ScanlineGrid::set_pan_tilt(float pan, float tilt)
159 {
160  // ignored
161 }
162 
163 void
165 {
166  if (!roi)
167  this->roi = new ROI(0, 0, this->width, this->height, this->width, this->height);
168  else {
169  if (!this->roi) {
170  this->roi = new ROI(roi);
171  } else {
172  *(this->roi) = *roi;
173  }
174  //Use roi's image width/height as grid boundary (to simplify the "exceeds-boundaries"-test)
175  this->roi->image_width = this->roi->start.x + this->roi->width;
176  this->roi->image_height = this->roi->start.y + this->roi->height;
177 
178  if (this->roi->image_width > this->width)
179  throw fawkes::OutOfBoundsException("ScanlineGrid: ROI is out of grid bounds!",
180  this->roi->image_width,
181  0,
182  this->width);
183  if (this->roi->image_height > this->height)
184  throw fawkes::OutOfBoundsException("ScanlineGrid: ROI is out of grid bounds!",
185  this->roi->image_height,
186  0,
187  this->height);
188  }
189 
190  reset();
191 }
192 
193 /** Set dimensions.
194  * Set width and height of scanline grid. Implicitly resets the grid.
195  * @param width width
196  * @param height height
197  * @param roi the grid will only be calculated within the roi (if NULL the roi
198  * will be from 0,0 to width,height). The object will be deleted by
199  * ScanlineGrid!
200  */
201 void
202 ScanlineGrid::setDimensions(unsigned int width, unsigned int height, ROI *roi)
203 {
204  this->width = width;
205  this->height = height;
206 
207  set_roi(roi);
208 }
209 
210 /** Set offset.
211  * Set X and Y offset by which the pointer in the grid is advanced. Implicitly resets the grid.
212  * @param offset_x offset_x
213  * @param offset_y offset_y
214  */
215 void
216 ScanlineGrid::setOffset(unsigned int offset_x, unsigned int offset_y)
217 {
218  this->offset_x = offset_x;
219  this->offset_y = offset_y;
220 
221  reset();
222 }
223 
224 /** Set all grid parameters.
225  * Set width, height, X and Y offset by which the pointer in the grid is advanced.
226  * Implicitly resets the grid.
227  * @param width width
228  * @param height height
229  * @param offset_x offset_x
230  * @param offset_y offset_y
231  * @param roi the grid will only be calculated within the roi (if NULL the roi
232  * will be from 0,0 to width,height). The object will be deleted by
233  * ScanlineGrid!
234  * @param horizontal_grid if true x will be increased before y
235  */
236 void
237 ScanlineGrid::setGridParams(unsigned int width,
238  unsigned int height,
239  unsigned int offset_x,
240  unsigned int offset_y,
241  ROI * roi,
242  bool horizontal_grid)
243 {
244  this->horizontal_grid = horizontal_grid;
245 
246  setDimensions(width, height, roi);
247  setOffset(offset_x, offset_y);
248 }
249 
250 } // end namespace firevision
virtual void set_pan_tilt(float pan, float tilt)
Set camera's pan/tilt values.
Definition: grid.cpp:158
virtual ~ScanlineGrid()
Destructor.
Definition: grid.cpp:62
fawkes::upoint_t * operator++()
Postfix ++ operator.
Definition: grid.cpp:110
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
Region of interest.
Definition: roi.h:54
virtual void set_roi(ROI *roi=NULL)
Set the region-of-interest.
Definition: grid.cpp:164
void reset()
Reset model.
Definition: grid.cpp:131
virtual void set_robot_pose(float x, float y, float ori)
Set the robot's pose.
Definition: grid.cpp:152
const char * get_name()
Get name of scanline model.
Definition: grid.cpp:140
unsigned int image_width
width of image that contains this ROI
Definition: roi.h:121
unsigned int get_margin()
Get margin around points.
Definition: grid.cpp:146
unsigned int image_height
height of image that contains this ROI
Definition: roi.h:123
void setOffset(unsigned int offset_x, unsigned int offset_y)
Set offset.
Definition: grid.cpp:216
Point with cartesian coordinates as unsigned integers.
Definition: types.h:34
bool finished()
Check if all desired points have been processed.
Definition: grid.cpp:125
fawkes::upoint_t operator*()
Get the current coordinate.
Definition: grid.cpp:67
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
unsigned int height
ROI height.
Definition: roi.h:119
void setDimensions(unsigned int width, unsigned int height, ROI *roi=NULL)
Set dimensions.
Definition: grid.cpp:202
fawkes::upoint_t * operator->()
Get pointer to current point.
Definition: grid.cpp:72
Index out of bounds.
Definition: software.h:85
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:237