Gazebo Math

API Reference

7.4.0
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
28namespace gz
29{
30 namespace math
31 {
32 // Inline bracket to help doxygen filtering.
33 inline namespace GZ_MATH_VERSION_NAMESPACE {
34 //
43 template <typename T>
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 =
287 Interval<T>::Open(-std::numeric_limits<T>::infinity(),
289 } // namespace detail
290 template<typename T>
291 const Interval<T> &Interval<T>::Unbounded = detail::gUnboundedInterval<T>;
292
295 }
296 }
297}
298
299#endif