bounds.h
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 #ifndef NAV_CORE2_BOUNDS_H
00036 #define NAV_CORE2_BOUNDS_H
00037 
00038 #include <algorithm>
00039 #include <limits>
00040 #include <string>
00041 
00042 namespace nav_core2
00043 {
00048 template <typename NumericType>
00049 struct GenericBounds
00050 {
00051 public:
00055   GenericBounds()
00056   {
00057     reset();
00058   }
00059 
00067   GenericBounds(NumericType x0, NumericType y0, NumericType x1, NumericType y1)
00068     : min_x_(x0), min_y_(y0), max_x_(x1), max_y_(y1) {}
00069 
00073   void reset()
00074   {
00075     min_x_ = min_y_ = std::numeric_limits<NumericType>::max();
00076     max_x_ = max_y_ = std::numeric_limits<NumericType>::lowest();  // -max
00077   }
00078 
00082   void touch(NumericType x, NumericType y)
00083   {
00084     min_x_ = std::min(x, min_x_);
00085     min_y_ = std::min(y, min_y_);
00086     max_x_ = std::max(x, max_x_);
00087     max_y_ = std::max(y, max_y_);
00088   }
00089 
00097   void update(NumericType x0, NumericType y0, NumericType x1, NumericType y1)
00098   {
00099     min_x_ = std::min(x0, min_x_);
00100     min_y_ = std::min(y0, min_y_);
00101     max_x_ = std::max(x1, max_x_);
00102     max_y_ = std::max(y1, max_y_);
00103   }
00104 
00109   void merge(const GenericBounds<NumericType>& other)
00110   {
00111     update(other.min_x_, other.min_y_, other.max_x_, other.max_y_);
00112   }
00113 
00117   bool isEmpty() const
00118   {
00119     return min_x_ > max_x_ && min_y_ > max_y_;
00120   }
00121 
00125   std::string toString() const
00126   {
00127     if (!isEmpty())
00128     {
00129       return "(" + std::to_string(min_x_) + "," + std::to_string(min_y_) + "):(" +
00130              std::to_string(max_x_) + "," + std::to_string(max_y_) + ")";
00131     }
00132     else
00133     {
00134       return "(empty bounds)";
00135     }
00136   }
00137 
00138   NumericType getMinX() const { return min_x_; }
00139   NumericType getMinY() const { return min_y_; }
00140   NumericType getMaxX() const { return max_x_; }
00141   NumericType getMaxY() const { return max_y_; }
00142 
00143 protected:
00144   NumericType min_x_, min_y_, max_x_, max_y_;
00145 };
00146 
00147 using Bounds = GenericBounds<double>;
00148 
00149 class UIntBounds : public GenericBounds<unsigned int>
00150 {
00151 public:
00152   using GenericBounds<unsigned int>::GenericBounds;
00153   unsigned int getWidth() const { return max_x_ - min_x_ + 1; }
00154   unsigned int getHeight() const { return max_y_ - min_y_ + 1; }
00155 };
00156 
00157 }  // namespace nav_core2
00158 
00159 #endif  // NAV_CORE2_BOUNDS_H


nav_core2
Author(s):
autogenerated on Wed Jun 26 2019 20:09:31