grid.cpp

00001
00002 /***************************************************************************
00003  *  grid.cpp - Implementation of the grid scanline model
00004  *
00005  *  Generated: Tue Feb 22 10:36:39 2005
00006  *  Copyright  2005  Tim Niemueller [www.niemueller.de]
00007  *
00008  ****************************************************************************/
00009
00010 /*  This program is free software; you can redistribute it and/or modify
00011  *  it under the terms of the GNU General Public License as published by
00012  *  the Free Software Foundation; either version 2 of the License, or
00013  *  (at your option) any later version. A runtime exception applies to
00014  *  this software (see LICENSE.GPL_WRE file mentioned below for details).
00015  *
00016  *  This program is distributed in the hope that it will be useful,
00017  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00018  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00019  *  GNU Library General Public License for more details.
00020  *
00021  *  Read the full text in the LICENSE.GPL_WRE file in the doc directory.
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 /** @class ScanlineGrid <models/scanlines/grid.h>
00032  * Scanline Grid.
00033  * A grid as scanline points. The crossings of the lines are the scanline
00034  * points.
00035  */
00036 
00037 /** Constructor.
00038  * @param width width of grid
00039  * @param height height of grid
00040  * @param offset_x x offset between lines
00041  * @param offset_y y offset between lines
00042  * @param roi the grid will only be calculated within the roi (if NULL the roi
00043  *            will be from 0,0 to width,height). The object will be deleted by
00044  *            ScanlineGrid!
00045  * @param horizontal_grid if true x will be increased before y
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   //reset is done in setGridParams ()
00056 }
00057 
00058 /** Destructor
00059  */
00060 ScanlineGrid::~ScanlineGrid()
00061 {
00062   if (roi) //Has to be set, but still...
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 // vertical grid
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   // ignored
00174 }
00175
00176
00177 void
00178 ScanlineGrid::set_pan_tilt(float pan, float tilt)
00179 {
00180   // ignored
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     //Use roi's image width/height as grid boundary (to simplify the "exceeds-boundaries"-test)
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 /** Set dimensions.
00206  * Set width and height of scanline grid. Implicitly resets the grid.
00207  * @param width width
00208  * @param height height
00209  * @param roi the grid will only be calculated within the roi (if NULL the roi
00210  *            will be from 0,0 to width,height). The object will be deleted by
00211  *            ScanlineGrid!
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 /** Set offset.
00224  * Set X and Y offset by which the pointer in the grid is advanced. Implicitly resets the grid.
00225  * @param offset_x offset_x
00226  * @param offset_y offset_y
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 /** Set all grid parameters.
00239  * Set width, height, X and Y offset by which the pointer in the grid is advanced.
00240  * Implicitly resets the grid.
00241  * @param width width
00242  * @param height height
00243  * @param offset_x offset_x
00244  * @param offset_y offset_y
00245  * @param roi the grid will only be calculated within the roi (if NULL the roi
00246  *            will be from 0,0 to width,height). The object will be deleted by
00247  *            ScanlineGrid!
00248  * @param horizontal_grid if true x will be increased before y
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 }