Gazebo Math

API Reference

6.15.1
gz/math/Interval.hh
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2022 Open Source Robotics Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16 */
17 #ifndef GZ_MATH_INTERVAL_HH_
18 #define GZ_MATH_INTERVAL_HH_
19 
20 #include <cmath>
21 #include <limits>
22 #include <ostream>
23 #include <type_traits>
24 #include <utility>
25 
26 #include <gz/math/config.hh>
27 
28 namespace ignition
29 {
30  namespace math
31  {
32  // Inline bracket to help doxygen filtering.
33  inline namespace IGNITION_MATH_VERSION_NAMESPACE {
34  //
43  template <typename T>
44  class Interval
45  {
47  public: static const Interval<T> &Unbounded;
48 
50  public: Interval() = default;
51 
59  public: constexpr Interval(
60  T _leftValue, bool _leftClosed,
61  T _rightValue, bool _rightClosed)
62  : leftValue(std::move(_leftValue)),
63  rightValue(std::move(_rightValue)),
64  leftClosed(_leftClosed),
65  rightClosed(_rightClosed)
66  {
67  }
68 
73  public: static constexpr Interval<T>
74  Open(T _leftValue, T _rightValue)
75  {
76  return Interval<T>(
77  std::move(_leftValue), false,
78  std::move(_rightValue), false);
79  }
80 
85  public: static constexpr Interval<T>
86  LeftClosed(T _leftValue, T _rightValue)
87  {
88  return Interval<T>(
89  std::move(_leftValue), true,
90  std::move(_rightValue), false);
91  }
92 
97  public: static constexpr Interval<T>
98  RightClosed(T _leftValue, T _rightValue)
99  {
100  return Interval<T>(
101  std::move(_leftValue), false,
102  std::move(_rightValue), true);
103  }
104 
109  public: static constexpr Interval<T>
110  Closed(T _leftValue, T _rightValue)
111  {
112  return Interval<T>{
113  std::move(_leftValue), true,
114  std::move(_rightValue), true};
115  }
116 
119  public: const T &LeftValue() const { return this->leftValue; }
120 
123  public: bool IsLeftClosed() const { return this->leftClosed; }
124 
127  public: const T &RightValue() const { return this->rightValue; }
128 
131  public: bool IsRightClosed() const { return this->rightClosed; }
132 
137  public: bool Empty() const
138  {
139  if (this->leftClosed && this->rightClosed)
140  {
141  return this->rightValue < this->leftValue;
142  }
143  return this->rightValue <= this->leftValue;
144  }
145 
149  public: bool Contains(const T &_value) const
150  {
151  if (this->leftClosed && this->rightClosed)
152  {
153  return this->leftValue <= _value && _value <= this->rightValue;
154  }
155  if (this->leftClosed)
156  {
157  return this->leftValue <= _value && _value < this->rightValue;
158  }
159  if (this->rightClosed)
160  {
161  return this->leftValue < _value && _value <= this->rightValue;
162  }
163  return this->leftValue < _value && _value < this->rightValue;
164  }
165 
169  public: bool Contains(const Interval<T> &_other) const
170  {
171  if (this->Empty() || _other.Empty())
172  {
173  return false;
174  }
175  if (!this->leftClosed && _other.leftClosed)
176  {
177  if (_other.leftValue <= this->leftValue)
178  {
179  return false;
180  }
181  }
182  else
183  {
184  if (_other.leftValue < this->leftValue)
185  {
186  return false;
187  }
188  }
189  if (!this->rightClosed && _other.rightClosed)
190  {
191  if (this->rightValue <= _other.rightValue)
192  {
193  return false;
194  }
195  }
196  else
197  {
198  if (this->rightValue < _other.rightValue)
199  {
200  return false;
201  }
202  }
203  return true;
204  }
205 
209  public: bool Intersects(const Interval<T> &_other) const
210  {
211  if (this->Empty() || _other.Empty())
212  {
213  return false;
214  }
215  if (this->rightClosed && _other.leftClosed)
216  {
217  if (this->rightValue < _other.leftValue)
218  {
219  return false;
220  }
221  }
222  else
223  {
224  if (this->rightValue <= _other.leftValue)
225  {
226  return false;
227  }
228  }
229  if (_other.rightClosed && this->leftClosed)
230  {
231  if (_other.rightValue < this->leftValue)
232  {
233  return false;
234  }
235  }
236  else
237  {
238  if (_other.rightValue <= this->leftValue)
239  {
240  return false;
241  }
242  }
243  return true;
244  }
245 
249  public: bool operator==(const Interval<T> &_other) const
250  {
251  return this->Contains(_other) && _other.Contains(*this);
252  }
253 
257  public: bool operator!=(const Interval<T> &_other) const
258  {
259  return !this->Contains(_other) || !_other.Contains(*this);
260  }
261 
266  public: friend std::ostream &operator<<(
267  std::ostream &_out, const gz::math::Interval<T> &_interval)
268  {
269  return _out << (_interval.leftClosed ? "[" : "(")
270  << _interval.leftValue << ", " << _interval.rightValue
271  << (_interval.rightClosed ? "]" : ")");
272  }
273 
275  private: T leftValue{0};
277  private: T rightValue{0};
279  private: bool leftClosed{false};
281  private: bool rightClosed{false};
282  };
283 
284  namespace detail {
285  template<typename T>
286  constexpr Interval<T> gUnboundedInterval =
289  } // namespace detail
290  template<typename T>
291  const Interval<T> &Interval<T>::Unbounded = detail::gUnboundedInterval<T>;
292 
295  }
296  }
297 }
298 
299 #endif
static constexpr Interval< T > Open(T _leftValue, T _rightValue)
Make an open interval (_leftValue, _rightValue)
Definition: gz/math/Interval.hh:74
static const Interval< T > & Unbounded
An unbounded interval (-∞, ∞)
Definition: gz/math/Interval.hh:47
static constexpr Interval< T > RightClosed(T _leftValue, T _rightValue)
Make a right-closed interval (_leftValue, _rightValue].
Definition: gz/math/Interval.hh:98
Definition: gz/math/AdditivelySeparableScalarField3.hh:27
const T & LeftValue() const
Get the leftmost interval value.
Definition: gz/math/Interval.hh:119
T move(T... args)
The Interval class represents a range of real numbers. Intervals may be open (a, b),...
Definition: gz/math/Interval.hh:44
bool operator!=(const Interval< T > &_other) const
Inequality test operator.
Definition: gz/math/Interval.hh:257
const T & RightValue() const
Get the rightmost interval value.
Definition: gz/math/Interval.hh:127
bool Contains(const T &_value) const
Check if the interval contains _value
Definition: gz/math/Interval.hh:149
STL class.
bool Empty() const
Check if the interval is empty Some examples of empty intervals include (a, a), [a,...
Definition: gz/math/Interval.hh:137
Interval()=default
Constructor.
static constexpr Interval< T > Closed(T _leftValue, T _rightValue)
Make a closed interval [_leftValue, _rightValue].
Definition: gz/math/Interval.hh:110
friend std::ostream & operator<<(std::ostream &_out, const Interval< T > &_interval)
Stream insertion operator.
Definition: gz/math/Interval.hh:266
STL namespace.
bool operator==(const Interval< T > &_other) const
Equality test operator.
Definition: gz/math/Interval.hh:249
static constexpr Interval< T > LeftClosed(T _leftValue, T _rightValue)
Make a left-closed interval [_leftValue, _rightValue)
Definition: gz/math/Interval.hh:86
bool IsLeftClosed() const
Check if the interval is left-closed.
Definition: gz/math/Interval.hh:123
bool Intersects(const Interval< T > &_other) const
Check if the interval intersects _other interval.
Definition: gz/math/Interval.hh:209
bool IsRightClosed() const
Check if the interval is right-closed.
Definition: gz/math/Interval.hh:131
bool Contains(const Interval< T > &_other) const
Check if the interval contains _other interval.
Definition: gz/math/Interval.hh:169
constexpr Interval(T _leftValue, bool _leftClosed, T _rightValue, bool _rightClosed)
Constructor.
Definition: gz/math/Interval.hh:59