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