line_grid.cpp
00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #include "line_grid.h"
00025
00026 #include <fvutils/base/roi.h>
00027 #include <fvutils/draw/drawer.h>
00028 #include <core/exceptions/software.h>
00029
00030 #include <cstring>
00031
00032 using fawkes::point_t;
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055 ScanlineLineGrid::ScanlineLineGrid(unsigned int width, unsigned int height,
00056 unsigned int offset_hor, unsigned int offset_ver,
00057 ROI* roi, unsigned int gap)
00058 {
00059 __roi = NULL;
00060 __next_pixel = gap + 1;
00061 set_grid_params(width, height,
00062 offset_hor, offset_ver, roi);
00063
00064 }
00065
00066
00067
00068 ScanlineLineGrid::~ScanlineLineGrid()
00069 {
00070 delete __roi;
00071 }
00072
00073 point_t
00074 ScanlineLineGrid::operator*()
00075 {
00076 return *__cur;
00077 }
00078
00079 point_t*
00080 ScanlineLineGrid::operator->()
00081 {
00082 return &*__cur;
00083 }
00084
00085 void
00086 ScanlineLineGrid::calc_coords()
00087 {
00088 __point_list.clear();
00089 bool more_to_come = true;
00090 point_t coord;
00091 unsigned int next_px;
00092
00093 if (__offset_hor > 0)
00094 {
00095 more_to_come = true;
00096 next_px = std::min(__next_pixel, __offset_ver ? __offset_ver : __width);
00097 coord.x = __roi->start.x;
00098 coord.y = __roi->start.y + ((__roi->height - 1) % __offset_hor) / 2;
00099 __point_list.push_back(coord);
00100
00101 while (more_to_come) {
00102 if (coord.x < (__roi->image_width - next_px))
00103 {
00104 coord.x += next_px;
00105 }
00106 else
00107 {
00108 if (coord.y < (__roi->image_height - __offset_hor))
00109 {
00110 coord.x = __roi->start.x;
00111 coord.y += __offset_hor;
00112 }
00113 else
00114 {
00115 more_to_come = false;
00116 }
00117 }
00118
00119 if (more_to_come) __point_list.push_back(coord);
00120 }
00121 }
00122
00123 if (__offset_ver > 0)
00124 {
00125 more_to_come = true;
00126 next_px = std::min(__next_pixel, __offset_hor ? __offset_hor : __height);
00127 coord.x = __roi->start.x + ((__roi->width - 1) % __offset_ver) / 2;
00128 coord.y = __roi->start.y;
00129 __point_list.push_back(coord);
00130
00131 while (more_to_come) {
00132 if (coord.y < (__roi->image_height - next_px))
00133 {
00134 coord.y += next_px;
00135 }
00136 else
00137 {
00138 if (coord.x < (__roi->image_width - __offset_ver))
00139 {
00140 coord.x += __offset_ver;
00141 coord.y = __roi->start.y;
00142 }
00143 else
00144 {
00145 more_to_come = false;
00146 }
00147 }
00148
00149 if (more_to_come) __point_list.push_back(coord);
00150 }
00151 }
00152
00153 reset();
00154 }
00155
00156 point_t *
00157 ScanlineLineGrid::operator++()
00158 {
00159 if (__cur != __point_list.end()) ++__cur;
00160 return __cur != __point_list.end() ? &*__cur : &__point_list.back();
00161 }
00162
00163 point_t *
00164 ScanlineLineGrid::operator++(int)
00165 {
00166 if (__cur != __point_list.end()) {
00167 point_t *res = &*__cur++;
00168 return res;
00169 }
00170 else return &__point_list.back();
00171 }
00172
00173 bool
00174 ScanlineLineGrid::finished()
00175 {
00176 return __cur == __point_list.end();
00177 }
00178
00179 void
00180 ScanlineLineGrid::reset()
00181 {
00182 __cur = __point_list.begin();
00183 }
00184
00185 const char *
00186 ScanlineLineGrid::get_name()
00187 {
00188 return "ScanlineModel::LineGrid";
00189 }
00190
00191
00192 unsigned int
00193 ScanlineLineGrid::get_margin()
00194 {
00195 return std::max(__offset_ver, __offset_hor);
00196 }
00197
00198
00199 void
00200 ScanlineLineGrid::set_robot_pose(float x, float y, float ori)
00201 {
00202
00203 }
00204
00205
00206 void
00207 ScanlineLineGrid::set_pan_tilt(float pan, float tilt)
00208 {
00209
00210 }
00211
00212
00213
00214
00215
00216
00217
00218
00219
00220
00221
00222 void
00223 ScanlineLineGrid::set_dimensions(unsigned int width, unsigned int height, ROI* roi)
00224 {
00225 __width = width;
00226 __height = height;
00227
00228 set_roi(roi);
00229 }
00230
00231
00232
00233
00234
00235
00236 void
00237 ScanlineLineGrid::set_roi(ROI* roi)
00238 {
00239 delete __roi;
00240
00241 if (!roi) __roi = new ROI(0, 0, __width, __height, __width, __height);
00242 else
00243 {
00244 __roi = roi;
00245
00246 __roi->set_image_width(__roi->start.x + __roi->width);
00247 __roi->set_image_height(__roi->start.y + __roi->height);
00248
00249 if (__roi->image_width > __width)
00250 throw fawkes::OutOfBoundsException("ScanlineLineGrid: ROI is out of grid bounds!", __roi->image_width, 0, __width);
00251 if (__roi->image_height > __height)
00252 throw fawkes::OutOfBoundsException("ScanlineLineGrid: ROI is out of grid bounds!", __roi->image_height, 0, __height);
00253 }
00254
00255 calc_coords();
00256 }
00257
00258
00259
00260
00261
00262
00263
00264
00265 void
00266 ScanlineLineGrid::set_offset(unsigned int offset_hor, unsigned int offset_ver)
00267 {
00268 __offset_hor = offset_hor;
00269 __offset_ver = offset_ver;
00270
00271 calc_coords();
00272 }
00273
00274
00275
00276
00277
00278
00279
00280
00281
00282
00283
00284
00285
00286
00287
00288 void
00289 ScanlineLineGrid::set_grid_params(unsigned int width, unsigned int height,
00290 unsigned int offset_hor, unsigned int offset_ver,
00291 ROI* roi)
00292 {
00293 __offset_hor = offset_hor;
00294 __offset_ver = offset_ver;
00295
00296 set_dimensions(width, height, roi);
00297 }