|
|
|
|
#ifndef Magnum_Math_FunctionsBatch_h
|
|
|
|
|
#define Magnum_Math_FunctionsBatch_h
|
|
|
|
|
/*
|
|
|
|
|
This file is part of Magnum.
|
|
|
|
|
|
|
|
|
|
Copyright © 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019,
|
|
|
|
|
2020, 2021, 2022, 2023, 2024, 2025
|
|
|
|
|
Vladimír Vondruš <mosra@centrum.cz>
|
|
|
|
|
|
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a
|
|
|
|
|
copy of this software and associated documentation files (the "Software"),
|
|
|
|
|
to deal in the Software without restriction, including without limitation
|
|
|
|
|
the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
|
|
|
|
and/or sell copies of the Software, and to permit persons to whom the
|
|
|
|
|
Software is furnished to do so, subject to the following conditions:
|
|
|
|
|
|
|
|
|
|
The above copyright notice and this permission notice shall be included
|
|
|
|
|
in all copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
|
|
|
|
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
|
|
|
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
|
|
|
|
DEALINGS IN THE SOFTWARE.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
/** @file
|
|
|
|
|
* @brief Batch functions usable with scalar and vector types
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include <initializer_list>
|
|
|
|
|
/* std::declval() is said to be in <utility> but libstdc++, libc++ and MSVC STL
|
|
|
|
|
all have it directly in <type_traits> because it just makes sense */
|
|
|
|
|
#include <type_traits>
|
|
|
|
|
#include <Corrade/Containers/StridedArrayView.h>
|
|
|
|
|
|
|
|
|
|
#include "Magnum/Math/Functions.h"
|
|
|
|
|
|
|
|
|
|
#ifdef MAGNUM_BUILD_DEPRECATED
|
|
|
|
|
/* Some APIs returned std::pair before */
|
|
|
|
|
#include <Corrade/Containers/PairStl.h>
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
namespace Magnum { namespace Math {
|
|
|
|
|
|
|
|
|
|
namespace Implementation {
|
|
|
|
|
|
|
|
|
|
/** @todo Utility/Algorithms.h has a similar (but different) variant of this,
|
|
|
|
|
maybe turn that into some public utility once we have one more use case? */
|
|
|
|
|
template<class T, class View = decltype(Containers::Implementation::ErasedArrayViewConverter<typename std::remove_reference<T&&>::type>::from(std::declval<T&&>()))> static auto stridedArrayViewTypeFor(T&&) -> typename std::remove_const<typename View::Type>::type;
|
|
|
|
|
template<class T> static typename std::remove_const<T>::type stridedArrayViewTypeFor(const Containers::ArrayView<T>&);
|
|
|
|
|
template<class T> static typename std::remove_const<T>::type stridedArrayViewTypeFor(const Containers::StridedArrayView1D<T>&);
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
@{ @name Batch functions
|
|
|
|
|
|
|
|
|
|
These functions process an ubounded range of values, as opposed to single
|
|
|
|
|
vectors or scalars.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
@brief If any number in the range is a positive or negative infinity
|
|
|
|
|
|
|
|
|
|
For scalar types returns @cpp true @ce as soon as it finds an infinite value,
|
|
|
|
|
@cpp false @ce otherwise. For vector types, returns @ref BitVector with bits
|
|
|
|
|
set to @cpp 1 @ce if any value has that component infinite. If the range is
|
|
|
|
|
empty, returns @cpp false @ce or a @ref BitVector with no bits set.
|
|
|
|
|
@see @ref isInf(T), @ref Constants::inf()
|
|
|
|
|
*/
|
|
|
|
|
template<class T> auto isInf(const Containers::StridedArrayView1D<const T>& range) -> decltype(isInf(std::declval<T>())) {
|
|
|
|
|
if(range.isEmpty()) return {};
|
|
|
|
|
|
|
|
|
|
/* For scalars, this loop exits once any value is infinity. For vectors
|
|
|
|
|
the loop accumulates the bits and exits as soon as all bits are set
|
|
|
|
|
or the input is exhausted */
|
|
|
|
|
auto out = isInf(range[0]); /* bool or BitVector */
|
|
|
|
|
for(std::size_t i = 1; i != range.size(); ++i) {
|
|
|
|
|
if(out) break;
|
|
|
|
|
out = out || isInf(range[i]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return out;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
@overload
|
|
|
|
|
@m_since{2020,06}
|
|
|
|
|
|
|
|
|
|
Converts @p range to @relativeref{Corrade,Containers::StridedArrayView1D} and
|
|
|
|
|
calls the above overload. Works with any type that's convertible to
|
|
|
|
|
@relativeref{Corrade,Containers::StridedArrayView}.
|
|
|
|
|
*/
|
|
|
|
|
template<class Iterable, class T = decltype(Implementation::stridedArrayViewTypeFor(std::declval<Iterable&&>()))> inline auto isInf(Iterable&& range) -> decltype(isInf(std::declval<T>())) {
|
|
|
|
|
/* Specifying the template explicitly to avoid recursion into the generic
|
|
|
|
|
function again */
|
|
|
|
|
return isInf<T>(Containers::StridedArrayView1D<const T>{range});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** @overload */
|
|
|
|
|
template<class T> inline auto isInf(std::initializer_list<T> list) -> decltype(isInf(std::declval<T>())) {
|
|
|
|
|
/* Specifying the template explicitly to avoid recursion into the generic
|
|
|
|
|
function again */
|
|
|
|
|
return isInf<T>(Containers::stridedArrayView(list));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** @overload */
|
|
|
|
|
template<class T, std::size_t size> inline auto isInf(const T(&array)[size]) -> decltype(isInf(std::declval<T>())) {
|
|
|
|
|
/* Specifying the template explicitly to avoid recursion into the generic
|
|
|
|
|
function again */
|
|
|
|
|
return isInf<T>(Containers::StridedArrayView1D<const T>{array});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
@brief If any number in the range is a NaN
|
|
|
|
|
|
|
|
|
|
For scalar types returns @cpp true @ce as soon as it finds a NaN value,
|
|
|
|
|
@cpp false @ce otherwise. For vector types, returns @ref BitVector with bits
|
|
|
|
|
set to @cpp 1 @ce if any value has that component NaN. If the range is empty,
|
|
|
|
|
returns @cpp false @ce or a @ref BitVector with no bits set.
|
|
|
|
|
@see @ref isNan(T), @ref Constants::nan()
|
|
|
|
|
*/
|
|
|
|
|
template<class T> inline auto isNan(const Containers::StridedArrayView1D<const T>& range) -> decltype(isNan(std::declval<T>())) {
|
|
|
|
|
if(range.isEmpty()) return {};
|
|
|
|
|
|
|
|
|
|
/* For scalars, this loop exits once any value is infinity. For vectors
|
|
|
|
|
the loop accumulates the bits and exits as soon as all bits are set
|
|
|
|
|
or the input is exhausted */
|
|
|
|
|
auto out = isNan(range[0]); /* bool or BitVector */
|
|
|
|
|
for(std::size_t i = 1; i != range.size(); ++i) {
|
|
|
|
|
if(out) break;
|
|
|
|
|
out = out || isNan(range[i]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return out;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
@overload
|
|
|
|
|
@m_since{2020,06}
|
|
|
|
|
|
|
|
|
|
Converts @p range to @relativeref{Corrade,Containers::StridedArrayView1D} and
|
|
|
|
|
calls the above overload. Works with any type that's convertible to
|
|
|
|
|
@relativeref{Corrade,Containers::StridedArrayView}.
|
|
|
|
|
*/
|
|
|
|
|
/* See isInf() for why arrayView() and not stridedArrayView() */
|
|
|
|
|
template<class Iterable, class T = decltype(Implementation::stridedArrayViewTypeFor(std::declval<Iterable&&>()))> inline auto isNan(Iterable&& range) -> decltype(isNan(std::declval<T>())) {
|
|
|
|
|
/* Specifying the template explicitly to avoid recursion into the generic
|
|
|
|
|
function again */
|
|
|
|
|
return isNan<T>(Containers::StridedArrayView1D<const T>{range});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** @overload */
|
|
|
|
|
template<class T> inline auto isNan(std::initializer_list<T> list) -> decltype(isNan(std::declval<T>())) {
|
|
|
|
|
/* Specifying the template explicitly to avoid recursion into the generic
|
|
|
|
|
function again */
|
|
|
|
|
return isNan<T>(Containers::stridedArrayView(list));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** @overload */
|
|
|
|
|
template<class T, std::size_t size> inline auto isNan(const T(&array)[size]) -> decltype(isNan(std::declval<T>())) {
|
|
|
|
|
/* Specifying the template explicitly to avoid recursion into the generic
|
|
|
|
|
function again */
|
|
|
|
|
return isNan<T>(Containers::StridedArrayView1D<const T>{array});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
namespace Implementation {
|
|
|
|
|
/* Non-floating-point types, the first is a non-NaN for sure */
|
|
|
|
|
template<class T, bool any> constexpr Containers::Pair<std::size_t, T> firstNonNan(Containers::StridedArrayView1D<const T> range, std::false_type, std::integral_constant<bool, any>) {
|
|
|
|
|
return {0, range.front()};
|
|
|
|
|
}
|
|
|
|
|
/* Floating-point scalars, return the first that's not NaN */
|
|
|
|
|
template<class T> inline Containers::Pair<std::size_t, T> firstNonNan(Containers::StridedArrayView1D<const T> range, std::true_type, std::false_type) {
|
|
|
|
|
/* Find the first non-NaN value to compare against. If all are NaN,
|
|
|
|
|
return the last value so the following loop in min/max/minmax()
|
|
|
|
|
doesn't even execute. */
|
|
|
|
|
for(std::size_t i = 0; i != range.size(); ++i)
|
|
|
|
|
if(!isNan(range[i])) return {i, range[i]};
|
|
|
|
|
return {range.size() - 1, range.back()};
|
|
|
|
|
}
|
|
|
|
|
/* Floating-point vectors. Try to gather non-NaN values for each component
|
|
|
|
|
and exit as soon as all are found (or the input is exhausted). Return
|
|
|
|
|
the index of first item with at least one non-NaN value as we need to go
|
|
|
|
|
through all at least partially valid values again anyway in order to
|
|
|
|
|
apply the min/max/minmax operation. I expect the cases of heavily
|
|
|
|
|
NaN-filled vectors (and thus the need to loop twice through most of the
|
|
|
|
|
range) to be very rare, so this shouldn't be a problem. */
|
|
|
|
|
template<class T> inline Containers::Pair<std::size_t, T> firstNonNan(Containers::StridedArrayView1D<const T> range, std::true_type, std::true_type) {
|
|
|
|
|
T out = range[0];
|
|
|
|
|
std::size_t firstValid = 0;
|
|
|
|
|
for(std::size_t i = 1; i != range.size(); ++i) {
|
|
|
|
|
BitVector<T::Size> nans = isNan(out);
|
|
|
|
|
if(nans.none()) break;
|
|
|
|
|
if(nans.all() && firstValid + 1 == i) ++firstValid;
|
|
|
|
|
out = Math::lerp(out, range[i], isNan(out));
|
|
|
|
|
}
|
|
|
|
|
return {firstValid, out};
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
@brief Minimum of a range
|
|
|
|
|
|
|
|
|
|
If the range is empty, returns default-constructed value. <em>NaN</em>s are
|
|
|
|
|
ignored, unless the range is all <em>NaN</em>s.
|
|
|
|
|
@see @ref min(T, T), @ref isNan(const Containers::StridedArrayView1D<const T>&)
|
|
|
|
|
*/
|
|
|
|
|
template<class T> inline T min(const Containers::StridedArrayView1D<const T>& range) {
|
|
|
|
|
if(range.isEmpty()) return {};
|
|
|
|
|
|
|
|
|
|
Containers::Pair<std::size_t, T> iOut = Implementation::firstNonNan(range, IsFloatingPoint<T>{}, IsVector<T>{});
|
|
|
|
|
for(++iOut.first(); iOut.first() != range.size(); ++iOut.first())
|
|
|
|
|
iOut.second() = Math::min(iOut.second(), range[iOut.first()]);
|
|
|
|
|
|
|
|
|
|
return iOut.second();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
@overload
|
|
|
|
|
@m_since{2020,06}
|
|
|
|
|
|
|
|
|
|
Converts @p range to @relativeref{Corrade,Containers::StridedArrayView1D} and
|
|
|
|
|
calls the above overload. Works with any type that's convertible to
|
|
|
|
|
@relativeref{Corrade,Containers::StridedArrayView}.
|
|
|
|
|
*/
|
|
|
|
|
template<class Iterable, class T = decltype(Implementation::stridedArrayViewTypeFor(std::declval<Iterable&&>()))> inline T min(Iterable&& range) {
|
|
|
|
|
/* Specifying the template explicitly to avoid recursion into the generic
|
|
|
|
|
function again */
|
|
|
|
|
return min<T>(Containers::StridedArrayView1D<const T>{range});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** @overload */
|
|
|
|
|
template<class T> inline T min(std::initializer_list<T> list) {
|
|
|
|
|
/* Specifying the template explicitly to avoid recursion into the generic
|
|
|
|
|
function again */
|
|
|
|
|
return min<T>(Containers::stridedArrayView(list));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** @overload */
|
|
|
|
|
template<class T, std::size_t size> inline T min(const T(&array)[size]) {
|
|
|
|
|
/* Specifying the template explicitly to avoid recursion into the generic
|
|
|
|
|
function again */
|
|
|
|
|
return min<T>(Containers::StridedArrayView1D<const T>{array});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
@brief Maximum of a range
|
|
|
|
|
|
|
|
|
|
If the range is empty, returns default-constructed value. <em>NaN</em>s are
|
|
|
|
|
ignored, unless the range is all <em>NaN</em>s.
|
|
|
|
|
@see @ref max(T, T), @ref isNan(const Containers::StridedArrayView1D<const T>&)
|
|
|
|
|
*/
|
|
|
|
|
template<class T> inline T max(const Containers::StridedArrayView1D<const T>& range) {
|
|
|
|
|
if(range.isEmpty()) return {};
|
|
|
|
|
|
|
|
|
|
Containers::Pair<std::size_t, T> iOut = Implementation::firstNonNan(range, IsFloatingPoint<T>{}, IsVector<T>{});
|
|
|
|
|
for(++iOut.first(); iOut.first() != range.size(); ++iOut.first())
|
|
|
|
|
iOut.second() = Math::max(iOut.second(), range[iOut.first()]);
|
|
|
|
|
|
|
|
|
|
return iOut.second();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
@overload
|
|
|
|
|
@m_since{2020,06}
|
|
|
|
|
|
|
|
|
|
Converts @p range to @relativeref{Corrade,Containers::StridedArrayView1D} and
|
|
|
|
|
calls the above overload. Works with any type that's convertible to
|
|
|
|
|
@relativeref{Corrade,Containers::StridedArrayView}.
|
|
|
|
|
*/
|
|
|
|
|
template<class Iterable, class T = decltype(Implementation::stridedArrayViewTypeFor(std::declval<Iterable&&>()))> inline T max(Iterable&& range) {
|
|
|
|
|
/* Specifying the template explicitly to avoid recursion into the generic
|
|
|
|
|
function again */
|
|
|
|
|
return max<T>(Containers::StridedArrayView1D<const T>{range});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** @overload */
|
|
|
|
|
template<class T> inline T max(std::initializer_list<T> list) {
|
|
|
|
|
/* Specifying the template explicitly to avoid recursion into the generic
|
|
|
|
|
function again */
|
|
|
|
|
return max<T>(Containers::stridedArrayView(list));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** @overload */
|
|
|
|
|
template<class T, std::size_t size> inline T max(const T(&array)[size]) {
|
|
|
|
|
/* Specifying the template explicitly to avoid recursion into the generic
|
|
|
|
|
function again */
|
|
|
|
|
return max<T>(Containers::StridedArrayView1D<const T>{array});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
namespace Implementation {
|
Convert all argument / return SFINAE into template argument SFINAE.
Similarly as done in Aug 2024 in Corrade. When these were a part of the
function signature, they ended up being encoded into the exported
symbol. There are still cases of StridedArrayView slice() having
enable_if in the signature, which amounts to about 18 kB symbols in all
libMagnum*-d.so libraries, but apart from that this is the state before:
$ strings libMagnum*-d.so | grep enable_if | grep -v slice | wc -c
29591
And this is after. All of those are coming from STL, thus from
old or deprecated APIs that still use std::vector, std::tuple and such,
and from the few std::sort() uses.
$ strings libMagnum*-d.so | grep enable_if | grep -v slice | wc -c
4103
In a non-deprecated build it's just this, which is a 10x reduction.
Can't really do much about these maybe exceút for implementing my own
swap() specializations (sigh?), but I think it's fine.
$ strings libMagnum*-d.so | grep enable_if | grep -v slice | wc -c
2904
I also made it consistently use
typename std::enable_if<..., int>::type = 0
instead of
class = typename std::enable_if<...>::type
because the former works correctly also in presence of overloads and
having it used consistently everywhere makes it easier to grep & change
later. All SFINAE is now also excluded from Doxygen output, because it
doesn't make much sense there. It's better to just explain the
restriction in words than with this nasty hack.
1 year ago
|
|
|
template<class T, typename std::enable_if<IsScalar<T>::value, int>::type = 0> inline void minmax(T& min, T& max, T value) {
|
|
|
|
|
if(value < min)
|
|
|
|
|
min = value;
|
|
|
|
|
else if(value > max)
|
|
|
|
|
max = value;
|
|
|
|
|
}
|
|
|
|
|
template<std::size_t size, class T> inline void minmax(Vector<size, T>& min, Vector<size, T>& max, const Vector<size, T>& value) {
|
|
|
|
|
for(std::size_t i = 0; i != size; ++i)
|
|
|
|
|
minmax(min[i], max[i], value[i]);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
@brief Minimum and maximum of a range
|
|
|
|
|
|
|
|
|
|
If the range is empty, returns default-constructed values. <em>NaN</em>s are
|
|
|
|
|
ignored, unless the range is all <em>NaN</em>s.
|
|
|
|
|
@see @ref minmax(T, T),
|
|
|
|
|
@ref Range::Range(const Containers::Pair<VectorType, VectorType>&),
|
|
|
|
|
@ref isNan(const Containers::StridedArrayView1D<const T>&)
|
|
|
|
|
*/
|
|
|
|
|
template<class T> inline Containers::Pair<T, T> minmax(const Containers::StridedArrayView1D<const T>& range) {
|
|
|
|
|
if(range.isEmpty()) return {};
|
|
|
|
|
|
|
|
|
|
Containers::Pair<std::size_t, T> iOut = Implementation::firstNonNan(range, IsFloatingPoint<T>{}, IsVector<T>{});
|
|
|
|
|
T min{iOut.second()}, max{iOut.second()};
|
|
|
|
|
for(++iOut.first(); iOut.first() != range.size(); ++iOut.first())
|
|
|
|
|
Implementation::minmax(min, max, range[iOut.first()]);
|
|
|
|
|
|
|
|
|
|
return {min, max};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
@overload
|
|
|
|
|
@m_since{2020,06}
|
|
|
|
|
|
|
|
|
|
Converts @p range to @relativeref{Corrade,Containers::StridedArrayView1D} and
|
|
|
|
|
calls the above overload. Works with any type that's convertible to
|
|
|
|
|
@relativeref{Corrade,Containers::StridedArrayView}.
|
|
|
|
|
*/
|
|
|
|
|
template<class Iterable, class T = decltype(Implementation::stridedArrayViewTypeFor(std::declval<Iterable&&>()))> inline Containers::Pair<T, T> minmax(Iterable&& range) {
|
|
|
|
|
/* Specifying the template explicitly to avoid recursion into the generic
|
|
|
|
|
function again */
|
|
|
|
|
return minmax<T>(Containers::StridedArrayView1D<const T>{range});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** @overload */
|
|
|
|
|
template<class T> inline Containers::Pair<T, T> minmax(std::initializer_list<T> list) {
|
|
|
|
|
/* Specifying the template explicitly to avoid recursion into the generic
|
|
|
|
|
function again */
|
|
|
|
|
return minmax<T>(Containers::stridedArrayView(list));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** @overload */
|
|
|
|
|
template<class T, std::size_t size> inline Containers::Pair<T, T> minmax(const T(&array)[size]) {
|
|
|
|
|
/* Specifying the template explicitly to avoid recursion into the generic
|
|
|
|
|
function again */
|
|
|
|
|
return minmax<T>(Containers::StridedArrayView1D<const T>{array});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Since 1.8.17, the original short-hand group closing doesn't work anymore.
|
|
|
|
|
FFS. */
|
|
|
|
|
/**
|
|
|
|
|
* @}
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
}}
|
|
|
|
|
|
|
|
|
|
#endif
|