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 {
48 template <typename NumericType>
50 {
51 public:
56  {
57  reset();
58  }
59 
67  GenericBounds(NumericType x0, NumericType y0, NumericType x1, NumericType y1)
68  : min_x_(x0), min_y_(y0), max_x_(x1), max_y_(y1) {}
69 
73  void reset()
74  {
75  min_x_ = min_y_ = std::numeric_limits<NumericType>::max();
76  max_x_ = max_y_ = std::numeric_limits<NumericType>::lowest(); // -max
77  }
78 
82  void touch(NumericType x, NumericType y)
83  {
84  min_x_ = std::min(x, min_x_);
85  min_y_ = std::min(y, min_y_);
86  max_x_ = std::max(x, max_x_);
87  max_y_ = std::max(y, max_y_);
88  }
89 
97  void update(NumericType x0, NumericType y0, NumericType x1, NumericType y1)
98  {
99  min_x_ = std::min(x0, min_x_);
100  min_y_ = std::min(y0, min_y_);
101  max_x_ = std::max(x1, max_x_);
102  max_y_ = std::max(y1, max_y_);
103  }
104 
110  {
111  update(other.min_x_, other.min_y_, other.max_x_, other.max_y_);
112  }
113 
117  bool isEmpty() const
118  {
119  return min_x_ > max_x_ && min_y_ > max_y_;
120  }
121 
125  std::string toString() const
126  {
127  if (!isEmpty())
128  {
129  return "(" + std::to_string(min_x_) + "," + std::to_string(min_y_) + "):(" +
130  std::to_string(max_x_) + "," + std::to_string(max_y_) + ")";
131  }
132  else
133  {
134  return "(empty bounds)";
135  }
136  }
137 
138  NumericType getMinX() const { return min_x_; }
139  NumericType getMinY() const { return min_y_; }
140  NumericType getMaxX() const { return max_x_; }
141  NumericType getMaxY() const { return max_y_; }
142 
143 protected:
144  NumericType min_x_, min_y_, max_x_, max_y_;
145 };
146 
148 
149 class UIntBounds : public GenericBounds<unsigned int>
150 {
151 public:
153  unsigned int getWidth() const { return max_x_ - min_x_ + 1; }
154  unsigned int getHeight() const { return max_y_ - min_y_ + 1; }
155 };
156 
157 } // namespace nav_core2
158 
159 #endif // NAV_CORE2_BOUNDS_H
NumericType max_y_
Definition: bounds.h:144
GenericBounds(NumericType x0, NumericType y0, NumericType x1, NumericType y1)
Constructor for a non-empty initial bounds.
Definition: bounds.h:67
NumericType getMaxX() const
Definition: bounds.h:140
NumericType getMinX() const
Definition: bounds.h:138
GenericBounds()
Constructor for an empty bounds.
Definition: bounds.h:55
NumericType getMinY() const
Definition: bounds.h:139
NumericType min_x_
Definition: bounds.h:144
NumericType min_y_
Definition: bounds.h:144
void reset()
Reset the bounds to be empty.
Definition: bounds.h:73
void touch(NumericType x, NumericType y)
Update the bounds to include the point (x, y)
Definition: bounds.h:82
void update(NumericType x0, NumericType y0, NumericType x1, NumericType y1)
Update the bounds to include points (x0, y0) and (x1, y1)
Definition: bounds.h:97
bool isEmpty() const
Returns true if the range is empty.
Definition: bounds.h:117
void merge(const GenericBounds< NumericType > &other)
Update the bounds to include the entirety of another bounds object.
Definition: bounds.h:109
std::string toString() const
Returns a string representation of the bounds.
Definition: bounds.h:125
Templatized class that represents a two dimensional bounds with ranges [min_x, max_x] [min_y...
Definition: bounds.h:49
unsigned int getHeight() const
Definition: bounds.h:154
NumericType max_x_
Definition: bounds.h:144
unsigned int getWidth() const
Definition: bounds.h:153
NumericType getMaxY() const
Definition: bounds.h:141


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