line.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.h>
00036 #include <nav_grid_iterators/line/bresenham.h>
00037 #include <nav_grid_iterators/line/ray_trace.h>
00038 #include <nav_grid/coordinate_conversion.h>
00039 
00040 namespace nav_grid_iterators
00041 {
00042 Line::Line(const nav_grid::NavGridInfo* info, double x0, double y0, double x1, double y1,
00043            bool include_last_index, bool bresenham)
00044   : BaseIterator(info), x0_(x0), y0_(y0), x1_(x1), y1_(y1), include_last_index_(include_last_index),
00045     bresenham_(bresenham), start_index_(0, 0), end_index_(0, 0)
00046 {
00047   constructIterator();
00048 
00049   // Convenience variables to avoid mismatched comparisons
00050   signed_width_ = static_cast<int>(info->width);
00051   signed_height_ = static_cast<int>(info->height);
00052 
00053   // Cache the end index
00054   nav_grid::SignedIndex end = internal_iterator_->getFinalIndex();
00055   end_index_.x = end.x;
00056   end_index_.y = end.y;
00057 
00058   // Iterate to first valid index
00059   nav_grid::SignedIndex sindex = **internal_iterator_;
00060   while (!internal_iterator_->isFinished() && !inBounds(sindex))
00061   {
00062     internal_iterator_->increment();
00063     sindex = **internal_iterator_;
00064   }
00065 
00066   // If all the indices are invalid, explicitly set the start index to be invalid
00067   if (internal_iterator_->isFinished())
00068   {
00069     start_index_ = end_index_;
00070   }
00071   else
00072   {
00073     start_index_.x = sindex.x;
00074     start_index_.y = sindex.y;
00075   }
00076 
00077   index_ = start_index_;
00078 }
00079 
00080 Line::Line(const Line& other)
00081   : Line(other.info_, other.index_, other.x0_, other.y0_, other.x1_, other.y1_, other.include_last_index_,
00082          other.bresenham_, other.start_index_, other.end_index_)
00083 {
00084 }
00085 
00086 Line::Line(const nav_grid::NavGridInfo* info, const nav_grid::Index& index, double x0, double y0, double x1, double y1,
00087            bool include_last_index, bool bresenham, nav_grid::Index start_index, nav_grid::Index end_index)
00088   : BaseIterator(info, index), x0_(x0), y0_(y0), x1_(x1), y1_(y1), include_last_index_(include_last_index),
00089     bresenham_(bresenham), start_index_(start_index), end_index_(end_index)
00090 {
00091   constructIterator();
00092   signed_width_ = static_cast<int>(info->width);
00093   signed_height_ = static_cast<int>(info->height);
00094 }
00095 
00096 Line& Line::operator=(const Line& other)
00097 {
00098   info_ = other.info_;
00099   index_ = other.index_;
00100   x0_ = other.x0_;
00101   y0_ = other.y0_;
00102   x1_ = other.x1_;
00103   y1_ = other.y1_;
00104   include_last_index_ = other.include_last_index_;
00105   bresenham_ = other.bresenham_;
00106   start_index_ = other.start_index_;
00107   end_index_ = other.end_index_;
00108   signed_width_ = other.signed_width_;
00109   signed_height_ = other.signed_height_;
00110   constructIterator();
00111   return *this;
00112 }
00113 
00114 void Line::constructIterator()
00115 {
00116   // translate coordinates into grid coordinates
00117   double local_x0, local_y0, local_x1, local_y1;
00118   worldToGrid(*info_, x0_, y0_, local_x0, local_y0);
00119   worldToGrid(*info_, x1_, y1_, local_x1, local_y1);
00120 
00121   if (bresenham_)
00122   {
00123     internal_iterator_.reset(new Bresenham(local_x0, local_y0, local_x1, local_y1, include_last_index_));
00124   }
00125   else
00126   {
00127     internal_iterator_.reset(new RayTrace(local_x0, local_y0, local_x1, local_y1, include_last_index_));
00128   }
00129 }
00130 
00131 bool Line::inBounds(const nav_grid::SignedIndex& sindex)
00132 {
00133   return sindex.x >= 0 && sindex.y >= 0 && sindex.x < signed_width_ && sindex.y < signed_height_;
00134 }
00135 
00136 Line Line::begin() const
00137 {
00138   return Line(info_, start_index_, x0_, y0_, x1_, y1_, include_last_index_, bresenham_, start_index_, end_index_);
00139 }
00140 
00141 Line Line::end() const
00142 {
00143   return Line(info_, end_index_, x0_, y0_, x1_, y1_, include_last_index_, bresenham_, start_index_, end_index_);
00144 }
00145 
00146 void Line::increment()
00147 {
00148   internal_iterator_->increment();
00149   nav_grid::SignedIndex sindex = **internal_iterator_;
00150   if (!internal_iterator_->isFinished() && !inBounds(sindex))
00151   {
00152     index_ = end_index_;
00153   }
00154   else
00155   {
00156     index_.x = sindex.x;
00157     index_.y = sindex.y;
00158   }
00159 }
00160 
00161 bool Line::fieldsEqual(const Line& other)
00162 {
00163   return x0_ == other.x0_ && y0_ == other.y0_ && x1_ == other.x1_ && y1_ == other.y1_ &&
00164          include_last_index_ == other.include_last_index_ && bresenham_ == other.bresenham_;
00165 }
00166 
00167 }  // namespace nav_grid_iterators


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