Files
OpenVulkano/openVulkanoCpp/Math/Range.hpp
2025-11-18 20:05:06 +01:00

71 lines
2.0 KiB
C++

/*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
#pragma once
#include "Math.hpp"
#ifdef _MSC_VER
#undef min
#undef max
#endif
namespace OpenVulkano::Math
{
template<typename T>
class Range
{
public:
T min, max;
Range() = default;
Range(const T& min, const T& max) : min(min), max(max) {}
[[nodiscard]] const T& GetMin() const { return min; }
[[nodiscard]] const T& GetMax() const { return max; }
[[nodiscard]] T& GetMin() { return min; }
[[nodiscard]] T& GetMax() { return max; }
[[nodiscard]] T GetSize() const { return max - min; }
[[nodiscard]] T Clamp(const T& value) const { return Math::Utils::clamp(value, min, max); }
/**
* Checks weather positions is inside the bounds or equal to the bounds.
* @param position Position to be checked
* @return True if inside or on bounds, fals if no.
*/
[[nodiscard]] bool InBounds(const T& position) const
{
if constexpr (std::is_arithmetic_v<T>)
return position >= min & position <= max;
else
return Math::Utils::all(Math::Utils::lessThanEqual(Range<T>::min, position)) && Math::Utils::all(Math::Utils::lessThanEqual(position, Range<T>::max));
}
/**
* Checks weather positions is inside the bounds.
* @param position Position to be checked
* @return True if inside bounds, fals if no.
*/
[[nodiscard]] bool Inside(const T& position) const
{
if constexpr (std::is_arithmetic_v<T>)
return position > min & position < max;
else
return Math::Utils::all(Math::Utils::lessThan(Range<T>::min, position)) && Math::Utils::all(Math::Utils::lessThan(position, Range<T>::max));
}
[[nodiscard]] bool operator <(const T& rhs) const { return max < rhs; }
[[nodiscard]] bool operator<=(const T& rhs) const { return max <= rhs; }
[[nodiscard]] bool operator >(const T& rhs) const { return min > rhs; }
[[nodiscard]] bool operator>=(const T& rhs) const { return min >= rhs; }
};
}