OTB  9.0.0
Orfeo Toolbox
otbInterval.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2005-2022 Centre National d'Etudes Spatiales (CNES)
3  *
4  * This file is part of Orfeo Toolbox
5  *
6  * https://www.orfeo-toolbox.org/
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  */
20 
21 #ifndef otbInterval_h
22 #define otbInterval_h
23 
24 #include "itkIntTypes.h"
25 #include <algorithm>
26 #include <cassert>
27 #include <ostream>
28 
29 namespace otb
30 {
40 class Interval
41 {
42 public:
43  using IndexType = itk::IndexValueType;
44  using SizeType = itk::SizeValueType;
45 
47  constexpr Interval(IndexType l, IndexType u) noexcept
48  : m_lower(l), m_upper(u)
49  {
50 #if defined(__cpp_constexpr) && __cpp_constexpr >= 201304
51  assert(l <= u);
52 #endif
53  }
54 
56  static constexpr Interval OfLength(IndexType low, SizeType len) noexcept{
57  return Interval{low, IndexType(low+len)};
58  }
60 
61  constexpr SizeType size() const noexcept
62  { return m_upper - m_lower; }
63 
64  constexpr bool empty() const noexcept
65  { return m_lower == m_upper; }
66 
67  constexpr IndexType lower() const noexcept
68  { return m_lower; }
69 
70  constexpr IndexType upper() const noexcept
71  { return m_upper; }
72 
79  friend constexpr Interval intersect(
80  Interval const& lhs, Interval const& rhs) noexcept
81  {
82 #if defined(__cpp_constexpr) && __cpp_constexpr >= 201304
83  auto const low = std::max(lhs.lower(), rhs.lower());
84  auto const upp = std::min(lhs.upper(), rhs.upper());
86 
87  return low <= upp ? Interval{low, upp} : Interval{0,0};
88 #else
89  // MSVC version supported is not C++14 compliant
90  return std::max(lhs.lower(), rhs.lower()) <= std::min(lhs.upper(), rhs.upper())
91  ? Interval{std::max(lhs.lower(), rhs.lower()), std::min(lhs.upper(), rhs.upper())}
92  : Interval{0,0};
93 #endif
94  }
95 
99  friend std::ostream & operator<<(std::ostream & os, Interval const& v)
100  {
101  return os << '[' << v.lower() << ".." << v.upper() << '[';
102  }
103 
104 private:
107 };
108 } // otb namespace
109 
110 #endif // otbInterval_h
otb::Interval::IndexType
itk::IndexValueType IndexType
Definition: otbInterval.h:43
otb::Interval::m_lower
IndexType m_lower
Definition: otbInterval.h:105
otb::Interval::lower
constexpr IndexType lower() const noexcept
Definition: otbInterval.h:67
otb::Interval::SizeType
itk::SizeValueType SizeType
Definition: otbInterval.h:44
otb
The "otb" namespace contains all Orfeo Toolbox (OTB) classes.
Definition: otbJoinContainer.h:32
otb::Interval::intersect
constexpr friend Interval intersect(Interval const &lhs, Interval const &rhs) noexcept
Definition: otbInterval.h:79
otb::Interval::OfLength
static constexpr Interval OfLength(IndexType low, SizeType len) noexcept
Definition: otbInterval.h:56
otb::Interval::m_upper
IndexType m_upper
Definition: otbInterval.h:106
otb::Interval::upper
constexpr IndexType upper() const noexcept
Definition: otbInterval.h:70
otb::details::len
std::vcl_size_t len(char)
Definition: otbStringUtilities.h:421
otb::Interval::Interval
constexpr Interval(IndexType l, IndexType u) noexcept
Definition: otbInterval.h:47
otb::Interval::empty
constexpr bool empty() const noexcept
Definition: otbInterval.h:64
otb::Interval::size
constexpr SizeType size() const noexcept
Definition: otbInterval.h:61
otb::Interval::operator<<
friend std::ostream & operator<<(std::ostream &os, Interval const &v)
Definition: otbInterval.h:99
otb::Interval
Definition: otbInterval.h:40