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