Fawkes API  Fawkes Development Version
grid.cpp
00001 
00002 /***************************************************************************
00003  *  grid.cpp - Implementation of the grid scanline model
00004  *
00005  *  Created: 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 <fvmodels/scanlines/grid.h>
00025 #include <core/exceptions/software.h>
00026 
00027 #include <cstring>
00028 
00029 using fawkes::point_t;
00030 
00031 namespace firevision {
00032 #if 0 /* just to make Emacs auto-indent happy */
00033 }
00034 #endif
00035 
00036 /** @class ScanlineGrid <fvmodels/scanlines/grid.h>
00037  * Scanline Grid.
00038  * A grid as scanline points. The crossings of the lines are the scanline
00039  * points.
00040  */
00041 
00042 /** Constructor.
00043  * @param width width of grid
00044  * @param height height of grid
00045  * @param offset_x x offset between lines
00046  * @param offset_y y offset between lines
00047  * @param roi the grid will only be calculated within the roi (if NULL the roi
00048  *            will be from 0,0 to width,height).
00049  * @param horizontal_grid if true x will be increased before y
00050  */
00051 ScanlineGrid::ScanlineGrid(unsigned int width, unsigned int height,
00052                            unsigned int offset_x, unsigned int offset_y,
00053                            ROI* roi, bool horizontal_grid)
00054 {
00055   this->roi = NULL;
00056   setGridParams(width, height,
00057                 offset_x, offset_y,
00058                 roi, horizontal_grid);
00059   //reset is done in setGridParams ()
00060 }
00061 
00062 /** Destructor
00063  */
00064 ScanlineGrid::~ScanlineGrid()
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   if (!roi) this->roi = new ROI(0, 0, this->width, this->height, this->width, this->height);
00187   else
00188   {
00189     this->roi = roi;
00190     //Use roi's image width/height as grid boundary (to simplify the "exceeds-boundaries"-test)
00191     this->roi->image_width  = this->roi->start.x + this->roi->width;
00192     this->roi->image_height = this->roi->start.y + this->roi->height;
00193 
00194     if (this->roi->image_width > this->width)
00195       throw fawkes::OutOfBoundsException("ScanlineGrid: ROI is out of grid bounds!", this->roi->image_width, 0, this->width);
00196     if (this->roi->image_height > this->height)
00197       throw fawkes::OutOfBoundsException("ScanlineGrid: ROI is out of grid bounds!", this->roi->image_height, 0, this->height);
00198   }
00199 
00200   reset();
00201 }
00202 
00203 /** Set dimensions.
00204  * Set width and height of scanline grid. Implicitly resets the grid.
00205  * @param width width
00206  * @param height height
00207  * @param roi the grid will only be calculated within the roi (if NULL the roi
00208  *            will be from 0,0 to width,height). The object will be deleted by
00209  *            ScanlineGrid!
00210  */
00211 void
00212 ScanlineGrid::setDimensions(unsigned int width, unsigned int height, ROI* roi)
00213 {
00214   this->width  = width;
00215   this->height = height;
00216 
00217   set_roi(roi);
00218 }
00219 
00220 
00221 /** Set offset.
00222  * Set X and Y offset by which the pointer in the grid is advanced. Implicitly resets the grid.
00223  * @param offset_x offset_x
00224  * @param offset_y offset_y
00225  */
00226 void
00227 ScanlineGrid::setOffset(unsigned int offset_x, unsigned int offset_y)
00228 {
00229   this->offset_x = offset_x;
00230   this->offset_y = offset_y;
00231 
00232   reset();
00233 }
00234 
00235 
00236 /** Set all grid parameters.
00237  * Set width, height, X and Y offset by which the pointer in the grid is advanced.
00238  * Implicitly resets the grid.
00239  * @param width width
00240  * @param height height
00241  * @param offset_x offset_x
00242  * @param offset_y offset_y
00243  * @param roi the grid will only be calculated within the roi (if NULL the roi
00244  *            will be from 0,0 to width,height). The object will be deleted by
00245  *            ScanlineGrid!
00246  * @param horizontal_grid if true x will be increased before y
00247  */
00248 void
00249 ScanlineGrid::setGridParams(unsigned int width, unsigned int height,
00250                             unsigned int offset_x, unsigned int offset_y,
00251                             ROI* roi, bool horizontal_grid)
00252 {
00253   this->horizontal_grid = horizontal_grid;
00254 
00255   setDimensions(width, height, roi);
00256   setOffset (offset_x, offset_y);
00257 }
00258 
00259 } // end namespace firevision