Basic Image AlgorithmS Library 2.8.0

BresenhamCircleEighth.hh

00001 /*
00002 This file is part of the BIAS library (Basic ImageAlgorithmS).
00003 
00004 Copyright (C) 2003-2009    (see file CONTACT for details)
00005   Multimediale Systeme der Informationsverarbeitung
00006   Institut fuer Informatik
00007   Christian-Albrechts-Universitaet Kiel
00008 
00009 
00010 BIAS is free software; you can redistribute it and/or modify
00011 it under the terms of the GNU Lesser General Public License as published by
00012 the Free Software Foundation; either version 2.1 of the License, or
00013 (at your option) any later version.
00014 
00015 BIAS is distributed in the hope that it will be useful,
00016 but WITHOUT ANY WARRANTY; without even the implied warranty of
00017 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018 GNU Lesser General Public License for more details.
00019 
00020 You should have received a copy of the GNU Lesser General Public License
00021 along with BIAS; if not, write to the Free Software
00022 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00023 */
00024 
00025 
00026 #ifndef __BresenhamCircleEighth_hh__
00027 #define __BresenhamCircleEighth_hh__
00028 #include "bias_config.h"
00029 
00030 namespace BIAS {
00031 
00032   /**
00033    * @brief Just like BresenhamCircle but only computes 1/8 of the circle.
00034    * @relates BresenhamCircle
00035    * @ingroup g_image_other
00036    * @author Felix Woelk, clean-up + refactoring by Robert Wulff 02/2010
00037    */
00038   class BresenhamCircleEighth {
00039 
00040   public:
00041 
00042     /**
00043      * @brief Default constructor. You have to call Init() by yourself before
00044      *        calling GetNext().
00045      */
00046     inline BresenhamCircleEighth() {
00047     }
00048 
00049 
00050     /**
00051      * @brief Constructor.
00052      *
00053      * @param center
00054      *        the center for this BresenhamCircleEighth
00055      * @param radius
00056      *        the radius for this BresenhamCircleEighth
00057      */
00058     inline BresenhamCircleEighth(int center[2], int radius) {
00059       Init(center, radius);
00060     }
00061 
00062 
00063     /**
00064      * @brief Destructor.
00065      */
00066     inline ~BresenhamCircleEighth() {
00067     }
00068 
00069 
00070     /**
00071      * @brief Initialises this BresenhamCircleEighth with new center and radius.
00072      *
00073      * @param center
00074      *        the new center for this BresenhamCircleEighth
00075      *
00076      * @param radius
00077      *        the new radius for this BresenhamCircleEighth
00078      */
00079     inline void Init(int center[2], int radius) {
00080       center_[0] = center[0];
00081       center_[1] = center[1];
00082       radius_    = radius;
00083 
00084       current_[0] = 0;
00085       current_[1] = radius_;
00086 
00087       d_       = 1 - radius_;
00088       deltaE_  = 3;
00089       deltaSE_ = -2 * radius_ + 5;
00090 
00091       // let GetNext return the starting point first
00092       current_[0]--;
00093       if (d_ >= 0) {
00094         current_[1]++;
00095       }
00096 #ifdef BIAS_DEBUG
00097       initialized_ = true;
00098 #endif
00099     }
00100 
00101 
00102     /**
00103      * @brief Returns the coordinate of the next point on the 1/8 circle.
00104      *
00105      * Can be called in a while loop:
00106      *
00107      * int nextCoords[2];
00108      * while (bresCircEighth.GetNext(nextCoords)){
00109      *   // draw nextCoords in image
00110      * }
00111      *
00112      * @param next
00113      *        the returned coordinate of the next point on the 1/8 circle
00114      *
00115      * @return true if there are still points left on the 1/8 circle or
00116      *         false if the 1/8 circle is finished
00117      */
00118     inline bool GetNext(int next[2]) {
00119 #ifdef BIAS_DEBUG
00120       if (!initialized_)
00121         BIASERR("call Init before GetNext");
00122 #endif
00123 
00124       bool result = true;
00125 
00126       if (current_[1] > current_[0]){
00127         if (d_ < 0){
00128           d_ += deltaE_;
00129           deltaE_ += 2;
00130           deltaSE_ += 2;
00131         } else {
00132           d_ += deltaSE_;
00133           deltaE_ += 2;
00134           deltaSE_ += 4;
00135           current_[1]--;
00136         }
00137 
00138         current_[0]++;
00139         next[0] = current_[0] + center_[0];
00140         next[1] = current_[1] + center_[1];
00141       } else {
00142 #ifdef BIAS_DEBUG
00143         initialized_ = false;
00144 #endif
00145         result = false;
00146       }
00147       return result;
00148     }
00149 
00150 
00151   protected:
00152 
00153     int center_[2]; // center coo
00154     int radius_;
00155     int current_[2];
00156     int d_;
00157     int deltaE_;
00158     int deltaSE_;
00159 
00160 #ifdef BIAS_DEBUG
00161     bool initialized_;
00162 #endif
00163   };
00164 
00165 } // namespace
00166 
00167 #endif // __BresenhamCircleEighth_hh__
 All Classes Functions Variables Typedefs Enumerations Enumerator Friends