Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
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();
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 }
00158
00159 #endif // NAV_CORE2_BOUNDS_H