bounds.h
Go to the documentation of this file.
1 /*
2  * Software License Agreement (BSD License)
3  *
4  * Copyright (c) 2018, Locus Robotics
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * * Redistributions of source code must retain the above copyright
12  * notice, this list of conditions and the following disclaimer.
13  * * Redistributions in binary form must reproduce the above
14  * copyright notice, this list of conditions and the following
15  * disclaimer in the documentation and/or other materials provided
16  * with the distribution.
17  * * Neither the name of the copyright holder nor the names of its
18  * contributors may be used to endorse or promote products derived
19  * from this software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25  * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32  * POSSIBILITY OF SUCH DAMAGE.
33  */
34 
35 #ifndef NAV_CORE2_BOUNDS_H
36 #define NAV_CORE2_BOUNDS_H
37 
38 #include <algorithm>
39 #include <limits>
40 #include <string>
41 
42 namespace nav_core2
43 {
51 template <typename NumericType>
52 inline bool inRange(const NumericType value, const NumericType min_value, const NumericType max_value)
53 {
54  return min_value <= value && value <= max_value;
55 }
56 
61 template <typename NumericType>
63 {
64 public:
69  {
70  reset();
71  }
72 
80  GenericBounds(NumericType x0, NumericType y0, NumericType x1, NumericType y1)
81  : min_x_(x0), min_y_(y0), max_x_(x1), max_y_(y1) {}
82 
86  void reset()
87  {
88  min_x_ = min_y_ = std::numeric_limits<NumericType>::max();
89  max_x_ = max_y_ = std::numeric_limits<NumericType>::lowest(); // -max
90  }
91 
95  void touch(NumericType x, NumericType y)
96  {
97  min_x_ = std::min(x, min_x_);
98  min_y_ = std::min(y, min_y_);
99  max_x_ = std::max(x, max_x_);
100  max_y_ = std::max(y, max_y_);
101  }
102 
110  void update(NumericType x0, NumericType y0, NumericType x1, NumericType y1)
111  {
112  min_x_ = std::min(x0, min_x_);
113  min_y_ = std::min(y0, min_y_);
114  max_x_ = std::max(x1, max_x_);
115  max_y_ = std::max(y1, max_y_);
116  }
117 
123  {
124  update(other.min_x_, other.min_y_, other.max_x_, other.max_y_);
125  }
126 
130  bool isEmpty() const
131  {
132  return min_x_ > max_x_ && min_y_ > max_y_;
133  }
134 
138  bool contains(NumericType x, NumericType y) const
139  {
140  return inRange(x, min_x_, max_x_) && inRange(y, min_y_, max_y_);
141  }
142 
146  bool overlaps(const GenericBounds<NumericType>& other) const
147  {
148  return !isEmpty() && !other.isEmpty()
149  && max_y_ >= other.min_y_ && min_y_ <= other.max_y_
150  && max_x_ >= other.min_x_ && min_x_ <= other.max_x_;
151  }
152 
156  bool operator==(const GenericBounds<NumericType>& other) const
157  {
158  return min_x_ == other.min_x_ && min_y_ == other.min_y_ &&
159  max_x_ == other.max_x_ && max_y_ == other.max_y_;
160  }
161 
162  bool operator!=(const GenericBounds<NumericType>& other) const
163  {
164  return !operator==(other);
165  }
166 
170  std::string toString() const
171  {
172  if (!isEmpty())
173  {
174  return "(" + std::to_string(min_x_) + "," + std::to_string(min_y_) + "):(" +
175  std::to_string(max_x_) + "," + std::to_string(max_y_) + ")";
176  }
177  else
178  {
179  return "(empty bounds)";
180  }
181  }
182 
183  NumericType getMinX() const { return min_x_; }
184  NumericType getMinY() const { return min_y_; }
185  NumericType getMaxX() const { return max_x_; }
186  NumericType getMaxY() const { return max_y_; }
187 
188 protected:
189  NumericType min_x_, min_y_, max_x_, max_y_;
190 };
191 
193 
194 inline unsigned int getDimension(unsigned int min_v, unsigned int max_v)
195 {
196  return (min_v > max_v) ? 0 : max_v - min_v + 1;
197 }
198 
199 class UIntBounds : public GenericBounds<unsigned int>
200 {
201 public:
203  unsigned int getWidth() const { return getDimension(min_x_, max_x_); }
204  unsigned int getHeight() const { return getDimension(min_y_, max_y_); }
205 };
206 
207 } // namespace nav_core2
208 
209 #endif // NAV_CORE2_BOUNDS_H
NumericType max_y_
Definition: bounds.h:189
GenericBounds(NumericType x0, NumericType y0, NumericType x1, NumericType y1)
Constructor for a non-empty initial bounds.
Definition: bounds.h:80
NumericType getMaxX() const
Definition: bounds.h:185
bool operator!=(const GenericBounds< NumericType > &other) const
Definition: bounds.h:162
bool overlaps(const GenericBounds< NumericType > &other) const
returns true if the two bounds overlap each other
Definition: bounds.h:146
NumericType getMinX() const
Definition: bounds.h:183
GenericBounds()
Constructor for an empty bounds.
Definition: bounds.h:68
NumericType getMinY() const
Definition: bounds.h:184
NumericType min_x_
Definition: bounds.h:189
NumericType min_y_
Definition: bounds.h:189
void reset()
Reset the bounds to be empty.
Definition: bounds.h:86
bool contains(NumericType x, NumericType y) const
Returns true if the point is inside this range.
Definition: bounds.h:138
unsigned int getDimension(unsigned int min_v, unsigned int max_v)
Definition: bounds.h:194
void touch(NumericType x, NumericType y)
Update the bounds to include the point (x, y)
Definition: bounds.h:95
bool operator==(const GenericBounds< NumericType > &other) const
comparison operator that requires all fields are equal
Definition: bounds.h:156
void update(NumericType x0, NumericType y0, NumericType x1, NumericType y1)
Update the bounds to include points (x0, y0) and (x1, y1)
Definition: bounds.h:110
bool isEmpty() const
Returns true if the range is empty.
Definition: bounds.h:130
bool inRange(const NumericType value, const NumericType min_value, const NumericType max_value)
Templatized method for checking if a value falls inside a one-dimensional range.
Definition: bounds.h:52
void merge(const GenericBounds< NumericType > &other)
Update the bounds to include the entirety of another bounds object.
Definition: bounds.h:122
std::string toString() const
Returns a string representation of the bounds.
Definition: bounds.h:170
Templatized class that represents a two dimensional bounds with ranges [min_x, max_x] [min_y...
Definition: bounds.h:62
unsigned int getHeight() const
Definition: bounds.h:204
NumericType max_x_
Definition: bounds.h:189
unsigned int getWidth() const
Definition: bounds.h:203
NumericType getMaxY() const
Definition: bounds.h:186


nav_core2
Author(s):
autogenerated on Sun Jan 10 2021 04:08:27