00001 /* 00002 * Copyright (c) 2012, Willow Garage, Inc. 00003 * All rights reserved. 00004 * 00005 * Redistribution and use in source and binary forms, with or without 00006 * modification, are permitted provided that the following conditions are met: 00007 * 00008 * * Redistributions of source code must retain the above copyright 00009 * notice, this list of conditions and the following disclaimer. 00010 * * Redistributions in binary form must reproduce the above copyright 00011 * notice, this list of conditions and the following disclaimer in the 00012 * documentation and/or other materials provided with the distribution. 00013 * * Neither the name of the Willow Garage, Inc. nor the names of its 00014 * contributors may be used to endorse or promote products derived from 00015 * this software without specific prior written permission. 00016 * 00017 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 00018 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 00019 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 00020 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 00021 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 00022 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 00023 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 00024 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 00025 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 00026 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 00027 * POSSIBILITY OF SUCH DAMAGE. 00028 */ 00029 #ifndef LINE_ITERATOR_H 00030 #define LINE_ITERATOR_H 00031 00032 #include <stdlib.h> 00033 00034 namespace base_local_planner 00035 { 00036 00038 class LineIterator 00039 { 00040 public: 00041 LineIterator( int x0, int y0, int x1, int y1 ) 00042 : x0_( x0 ) 00043 , y0_( y0 ) 00044 , x1_( x1 ) 00045 , y1_( y1 ) 00046 , x_( x0 ) // X and Y start of at first endpoint. 00047 , y_( y0 ) 00048 , deltax_( abs( x1 - x0 )) 00049 , deltay_( abs( y1 - y0 )) 00050 , curpixel_( 0 ) 00051 { 00052 if( x1_ >= x0_ ) // The x-values are increasing 00053 { 00054 xinc1_ = 1; 00055 xinc2_ = 1; 00056 } 00057 else // The x-values are decreasing 00058 { 00059 xinc1_ = -1; 00060 xinc2_ = -1; 00061 } 00062 00063 if( y1_ >= y0_) // The y-values are increasing 00064 { 00065 yinc1_ = 1; 00066 yinc2_ = 1; 00067 } 00068 else // The y-values are decreasing 00069 { 00070 yinc1_ = -1; 00071 yinc2_ = -1; 00072 } 00073 00074 if( deltax_ >= deltay_ ) // There is at least one x-value for every y-value 00075 { 00076 xinc1_ = 0; // Don't change the x when numerator >= denominator 00077 yinc2_ = 0; // Don't change the y for every iteration 00078 den_ = deltax_; 00079 num_ = deltax_ / 2; 00080 numadd_ = deltay_; 00081 numpixels_ = deltax_; // There are more x-values than y-values 00082 } 00083 else // There is at least one y-value for every x-value 00084 { 00085 xinc2_ = 0; // Don't change the x for every iteration 00086 yinc1_ = 0; // Don't change the y when numerator >= denominator 00087 den_ = deltay_; 00088 num_ = deltay_ / 2; 00089 numadd_ = deltax_; 00090 numpixels_ = deltay_; // There are more y-values than x-values 00091 } 00092 } 00093 00094 bool isValid() const 00095 { 00096 return curpixel_ <= numpixels_; 00097 } 00098 00099 void advance() 00100 { 00101 num_ += numadd_; // Increase the numerator by the top of the fraction 00102 if( num_ >= den_ ) // Check if numerator >= denominator 00103 { 00104 num_ -= den_; // Calculate the new numerator value 00105 x_ += xinc1_; // Change the x as appropriate 00106 y_ += yinc1_; // Change the y as appropriate 00107 } 00108 x_ += xinc2_; // Change the x as appropriate 00109 y_ += yinc2_; // Change the y as appropriate 00110 00111 curpixel_++; 00112 } 00113 00114 int getX() const { return x_; } 00115 int getY() const { return y_; } 00116 00117 int getX0() const { return x0_; } 00118 int getY0() const { return y0_; } 00119 00120 int getX1() const { return x1_; } 00121 int getY1() const { return y1_; } 00122 00123 private: 00124 int x0_; 00125 int y0_; 00126 int x1_; 00127 int y1_; 00128 00129 int x_; 00130 int y_; 00131 00132 int deltax_; 00133 int deltay_; 00134 00135 int curpixel_; 00136 00137 int xinc1_, xinc2_, yinc1_, yinc2_; 00138 int den_, num_, numadd_, numpixels_; 00139 }; 00140 00141 } // end namespace base_local_planner 00142 00143 #endif // LINE_ITERATOR_H