bresenham.cpp
Go to the documentation of this file.
00001 /*
00002  * Software License Agreement (BSD License)
00003  *
00004  *  Copyright (c) 2018, Locus Robotics
00005  *  All rights reserved.
00006  *
00007  *  Redistribution and use in source and binary forms, with or without
00008  *  modification, are permitted provided that the following conditions
00009  *  are met:
00010  *
00011  *   * Redistributions of source code must retain the above copyright
00012  *     notice, this list of conditions and the following disclaimer.
00013  *   * Redistributions in binary form must reproduce the above
00014  *     copyright notice, this list of conditions and the following
00015  *     disclaimer in the documentation and/or other materials provided
00016  *     with the distribution.
00017  *   * Neither the name of the copyright holder nor the names of its
00018  *     contributors may be used to endorse or promote products derived
00019  *     from this software without specific prior written permission.
00020  *
00021  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00022  *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00023  *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00024  *  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00025  *  COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00026  *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00027  *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00028  *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00029  *  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00030  *  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00031  *  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00032  *  POSSIBILITY OF SUCH DAMAGE.
00033  */
00034 
00035 #include <nav_grid_iterators/line/bresenham.h>
00036 #include <cmath>
00037 
00038 namespace nav_grid_iterators
00039 {
00040 
00041 Bresenham::Bresenham(int x0, int y0, int x1, int y1, bool include_last_index)
00042   : AbstractLineIterator(nav_grid::SignedIndex(x0, y0)), x0_(x0), y0_(y0), x1_(x1), y1_(y1),
00043     include_last_index_(include_last_index)
00044 {
00045   int dx = std::abs(x1_ - x0_);
00046   int dy = std::abs(y1_ - y0_);
00047   int xsign = x1_ >= x0_ ? 1 : -1;
00048   int ysign = y1_ >= y0_ ? 1 : -1;
00049 
00050   if (dx >= dy)      // There is at least one x-value for every y-value
00051   {
00052     loop_inc_x_ = xsign;
00053     error_inc_x_ = 0;       // Don't change the x when numerator >= denominator
00054     loop_inc_y_ = 0;        // Don't change the y for every iteration
00055     error_inc_y_ = ysign;
00056 
00057     denominator_ = dx;
00058     numerator_ = dx / 2;
00059     numerator_inc_ = dy;
00060   }
00061   else               // There is at least one y-value for every x-value
00062   {
00063     loop_inc_x_ = 0;        // Don't change the x for every iteration
00064     error_inc_x_ = xsign;
00065     loop_inc_y_ = ysign;
00066     error_inc_y_ = 0;       // Don't change the y when numerator >= denominator
00067 
00068     denominator_ = dy;
00069     numerator_ = dy / 2;
00070     numerator_inc_ = dx;
00071   }
00072 }
00073 
00074 Bresenham::Bresenham(const nav_grid::SignedIndex& index,
00075            int x0, int y0, int x1, int y1, bool include_last_index,
00076            int error_inc_x, int loop_inc_x, int error_inc_y, int loop_inc_y,
00077            int denominator, int numerator, int numerator_inc)
00078   : AbstractLineIterator(index), x0_(x0), y0_(y0), x1_(x1), y1_(y1), include_last_index_(include_last_index),
00079     error_inc_x_(error_inc_x), loop_inc_x_(loop_inc_x), error_inc_y_(error_inc_y), loop_inc_y_(loop_inc_y),
00080     denominator_(denominator), numerator_(numerator), numerator_inc_(numerator_inc)
00081 {
00082 }
00083 
00084 Bresenham Bresenham::begin() const
00085 {
00086   return Bresenham(nav_grid::SignedIndex(x0_, y0_), x0_, y0_, x1_, y1_, include_last_index_,
00087               error_inc_x_, loop_inc_x_, error_inc_y_, loop_inc_y_,
00088               denominator_, numerator_, numerator_inc_);
00089 }
00090 
00091 Bresenham Bresenham::end() const
00092 {
00093   Bresenham end(nav_grid::SignedIndex(x1_, y1_), x0_, y0_, x1_, y1_, include_last_index_,
00094            error_inc_x_, loop_inc_x_, error_inc_y_, loop_inc_y_,
00095            denominator_, numerator_, numerator_inc_);
00096 
00097   // If we want the last_index, return an iterator that is whatever is one-past the end coordinates
00098   if (include_last_index_)
00099     end.increment();
00100   return end;
00101 }
00102 
00103 void Bresenham::increment()
00104 {
00105   numerator_ += numerator_inc_;       // Increase the numerator by the top of the fraction
00106   if (numerator_ >= denominator_)     // Check if numerator >= denominator
00107   {
00108     numerator_ -= denominator_;       // Calculate the new numerator value
00109     index_.x += error_inc_x_;         // Change the x as appropriate
00110     index_.y += error_inc_y_;         // Change the y as appropriate
00111   }
00112   index_.x += loop_inc_x_;           // Change the x as appropriate
00113   index_.y += loop_inc_y_;           // Change the y as appropriate
00114 }
00115 
00116 nav_grid::SignedIndex Bresenham::getFinalIndex() const
00117 {
00118   return end().index_;
00119 }
00120 }  // namespace nav_grid_iterators


nav_grid_iterators
Author(s):
autogenerated on Wed Jun 26 2019 20:09:45