diff --git a/Base/Axis/Bin.cpp b/Base/Axis/Bin.cpp index 20cfc217562d7fe7e534e8505001822c24faaa99..ce246ee5310d9de202b478f26b723af5c1099a95 100644 --- a/Base/Axis/Bin.cpp +++ b/Base/Axis/Bin.cpp @@ -36,3 +36,13 @@ Bin1D::Bin1D(double lower, double upper) { ASSERT(m_lower <= m_upper); } + +std::optional<Bin1D> Bin1D::clipped_or_nil(double lower, double upper) const +{ + ASSERT(lower <= upper); + if (upper < m_lower || m_upper < lower) + return {}; + if (lower <= m_lower && m_upper <= upper) + return *this; + return Bin1D(std::max(lower, m_lower), std::min(upper, m_upper)); +} diff --git a/Base/Axis/Bin.h b/Base/Axis/Bin.h index ebc49750647f7db679165273cdcc25c9768b6fd9..4270b5b79cb83aab560660d74dcc5009a35b51a8 100644 --- a/Base/Axis/Bin.h +++ b/Base/Axis/Bin.h @@ -15,6 +15,8 @@ #ifndef BORNAGAIN_BASE_AXIS_BIN_H #define BORNAGAIN_BASE_AXIS_BIN_H +#include <optional> + //! An real-valued interval. class Bin1D { @@ -30,6 +32,8 @@ public: bool operator==(const Bin1D& other) const = default; + std::optional<Bin1D> clipped_or_nil(double lower, double upper) const; + private: Bin1D(double lower, double upper);