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 <models/scanlines/grid.h>
00025 #include <core/exceptions/software.h>
00026
00027 #include <cstring>
00028
00029 using fawkes::point_t;
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047 ScanlineGrid::ScanlineGrid(unsigned int width, unsigned int height,
00048 unsigned int offset_x, unsigned int offset_y,
00049 ROI* roi, bool horizontal_grid)
00050 {
00051 this->roi = NULL;
00052 setGridParams(width, height,
00053 offset_x, offset_y,
00054 roi, horizontal_grid);
00055
00056 }
00057
00058
00059
00060 ScanlineGrid::~ScanlineGrid()
00061 {
00062 if (roi)
00063 {
00064 delete roi;
00065 }
00066 }
00067
00068 point_t
00069 ScanlineGrid::operator*()
00070 {
00071 return coord;
00072 }
00073
00074 point_t*
00075 ScanlineGrid::operator->()
00076 {
00077 return &coord;
00078 }
00079
00080 void
00081 ScanlineGrid::calc_next_coord()
00082 {
00083 if (finished())
00084 return;
00085
00086 if (horizontal_grid)
00087 {
00088 if (static_cast<int>(coord.x) < static_cast<int>(roi->image_width - offset_x))
00089 {
00090 coord.x += offset_x;
00091 }
00092 else
00093 {
00094 if (static_cast<int>(coord.y) < static_cast<int>(roi->image_height - offset_y))
00095 {
00096 coord.x = roi->start.x;
00097 coord.y += offset_y;
00098 }
00099 else
00100 {
00101 more_to_come = false;
00102 }
00103 }
00104 }
00105 else
00106 {
00107 if (static_cast<int>(coord.y) < static_cast<int>(roi->image_height - offset_y))
00108 {
00109 coord.y += offset_y;
00110 }
00111 else
00112 {
00113 if (static_cast<int>(coord.x) < static_cast<int>(roi->image_width - offset_x))
00114 {
00115 coord.x += offset_x;
00116 coord.y = roi->start.y;
00117 }
00118 else
00119 {
00120 more_to_come = false;
00121 }
00122 }
00123 }
00124 }
00125
00126 point_t *
00127 ScanlineGrid::operator++()
00128 {
00129 calc_next_coord();
00130 return &coord;
00131 }
00132
00133 point_t *
00134 ScanlineGrid::operator++(int)
00135 {
00136 memcpy(&tmp_coord, &coord, sizeof(point_t));
00137 calc_next_coord();
00138 return &tmp_coord;
00139 }
00140
00141 bool
00142 ScanlineGrid::finished()
00143 {
00144 return !more_to_come;
00145 }
00146
00147 void
00148 ScanlineGrid::reset()
00149 {
00150 coord.x = roi->start.x;
00151 coord.y = roi->start.y;
00152
00153 more_to_come = true;
00154 }
00155
00156 const char *
00157 ScanlineGrid::get_name()
00158 {
00159 return "ScanlineModel::Grid";
00160 }
00161
00162
00163 unsigned int
00164 ScanlineGrid::get_margin()
00165 {
00166 return (offset_x > offset_y) ? offset_x : offset_y;
00167 }
00168
00169
00170 void
00171 ScanlineGrid::set_robot_pose(float x, float y, float ori)
00172 {
00173
00174 }
00175
00176
00177 void
00178 ScanlineGrid::set_pan_tilt(float pan, float tilt)
00179 {
00180
00181 }
00182
00183 void
00184 ScanlineGrid::set_roi(ROI *roi)
00185 {
00186 delete this->roi;
00187
00188 if (!roi) this->roi = new ROI(0, 0, this->width, this->height, this->width, this->height);
00189 else
00190 {
00191 this->roi = roi;
00192
00193 this->roi->image_width = this->roi->start.x + this->roi->width;
00194 this->roi->image_height = this->roi->start.y + this->roi->height;
00195
00196 if (this->roi->image_width > this->width)
00197 throw fawkes::OutOfBoundsException("ScanlineGrid: ROI is out of grid bounds!", this->roi->image_width, 0, this->width);
00198 if (this->roi->image_height > this->height)
00199 throw fawkes::OutOfBoundsException("ScanlineGrid: ROI is out of grid bounds!", this->roi->image_height, 0, this->height);
00200 }
00201
00202 reset();
00203 }
00204
00205
00206
00207
00208
00209
00210
00211
00212
00213 void
00214 ScanlineGrid::setDimensions(unsigned int width, unsigned int height, ROI* roi)
00215 {
00216 this->width = width;
00217 this->height = height;
00218
00219 set_roi(roi);
00220 }
00221
00222
00223
00224
00225
00226
00227
00228 void
00229 ScanlineGrid::setOffset(unsigned int offset_x, unsigned int offset_y)
00230 {
00231 this->offset_x = offset_x;
00232 this->offset_y = offset_y;
00233
00234 reset();
00235 }
00236
00237
00238
00239
00240
00241
00242
00243
00244
00245
00246
00247
00248
00249
00250 void
00251 ScanlineGrid::setGridParams(unsigned int width, unsigned int height,
00252 unsigned int offset_x, unsigned int offset_y,
00253 ROI* roi, bool horizontal_grid)
00254 {
00255 this->horizontal_grid = horizontal_grid;
00256
00257 setDimensions(width, height, roi);
00258 setOffset (offset_x, offset_y);
00259 }