mirror of https://github.com/mosra/magnum.git
Browse Source
Avoids including a (relatively) large ArrayView in Functions.h. Yes, we are now at a point where 500 lines matter.pull/331/head
10 changed files with 242 additions and 143 deletions
@ -0,0 +1,128 @@ |
|||||||
|
#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 |
||||||
|
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 <Corrade/Containers/ArrayView.h> |
||||||
|
|
||||||
|
#include "Magnum/Math/Functions.h" |
||||||
|
|
||||||
|
namespace Magnum { namespace Math { |
||||||
|
|
||||||
|
/**
|
||||||
|
@brief Minimum of a range |
||||||
|
|
||||||
|
If the range is empty, returns default-constructed value. |
||||||
|
@see @ref min(T, T) |
||||||
|
*/ |
||||||
|
template<class T> inline T min(Corrade::Containers::ArrayView<const T> range) { |
||||||
|
if(range.empty()) return {}; |
||||||
|
|
||||||
|
T out(range[0]); |
||||||
|
for(std::size_t i = 1; i != range.size(); ++i) |
||||||
|
out = min(out, range[i]); |
||||||
|
return out; |
||||||
|
} |
||||||
|
|
||||||
|
/** @overload */ |
||||||
|
template<class T> inline T min(std::initializer_list<T> list) { |
||||||
|
return min(Corrade::Containers::ArrayView<const T>{list.begin(), list.size()}); |
||||||
|
} |
||||||
|
|
||||||
|
/** @overload */ |
||||||
|
template<class T, std::size_t size> inline T min(const T(&array)[size]) { |
||||||
|
return min(Corrade::Containers::arrayView(array)); |
||||||
|
} |
||||||
|
|
||||||
|
/**
|
||||||
|
@brief Maximum of a range |
||||||
|
|
||||||
|
If the range is empty, returns default-constructed value. |
||||||
|
*/ |
||||||
|
template<class T> inline T max(Corrade::Containers::ArrayView<const T> range) { |
||||||
|
if(range.empty()) return {}; |
||||||
|
|
||||||
|
T out(range[0]); |
||||||
|
for(std::size_t i = 1; i != range.size(); ++i) |
||||||
|
out = max(out, range[i]); |
||||||
|
return out; |
||||||
|
} |
||||||
|
|
||||||
|
/** @overload */ |
||||||
|
template<class T> inline T max(std::initializer_list<T> list) { |
||||||
|
return max(Corrade::Containers::ArrayView<const T>{list.begin(), list.size()}); |
||||||
|
} |
||||||
|
|
||||||
|
/** @overload */ |
||||||
|
template<class T, std::size_t size> inline T max(const T(&array)[size]) { |
||||||
|
return max(Corrade::Containers::arrayView(array)); |
||||||
|
} |
||||||
|
|
||||||
|
namespace Implementation { |
||||||
|
template<class T> inline typename std::enable_if<std::is_arithmetic<T>::value, void>::type 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. |
||||||
|
@see @ref Range::Range(const std::pair<VectorType, VectorType>&) |
||||||
|
*/ |
||||||
|
template<class T> inline std::pair<T, T> minmax(Corrade::Containers::ArrayView<const T> range) { |
||||||
|
if(range.empty()) return {}; |
||||||
|
|
||||||
|
T min{range[0]}, max{range[0]}; |
||||||
|
for(std::size_t i = 1; i != range.size(); ++i) |
||||||
|
Implementation::minmax(min, max, range[i]); |
||||||
|
|
||||||
|
return {min, max}; |
||||||
|
} |
||||||
|
|
||||||
|
/** @overload */ |
||||||
|
template<class T> inline std::pair<T, T> minmax(std::initializer_list<T> list) { |
||||||
|
return minmax(Corrade::Containers::ArrayView<const T>{list.begin(), list.size()}); |
||||||
|
} |
||||||
|
|
||||||
|
/** @overload */ |
||||||
|
template<class T, std::size_t size> inline std::pair<T, T> minmax(const T(&array)[size]) { |
||||||
|
return minmax(Corrade::Containers::arrayView(array)); |
||||||
|
} |
||||||
|
|
||||||
|
}} |
||||||
|
|
||||||
|
#endif |
||||||
@ -0,0 +1,97 @@ |
|||||||
|
/*
|
||||||
|
This file is part of Magnum. |
||||||
|
|
||||||
|
Copyright © 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019 |
||||||
|
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. |
||||||
|
*/ |
||||||
|
|
||||||
|
#include <Corrade/TestSuite/Tester.h> |
||||||
|
|
||||||
|
#include "Magnum/Math/FunctionsBatch.h" |
||||||
|
#include "Magnum/Math/Vector3.h" |
||||||
|
|
||||||
|
namespace Magnum { namespace Math { namespace Test { namespace { |
||||||
|
|
||||||
|
struct FunctionsBatchTest: Corrade::TestSuite::Tester { |
||||||
|
explicit FunctionsBatchTest(); |
||||||
|
|
||||||
|
void minList(); |
||||||
|
void maxList(); |
||||||
|
void minmaxList(); |
||||||
|
}; |
||||||
|
|
||||||
|
typedef Math::Vector2<Float> Vector2; |
||||||
|
typedef Math::Vector3<Int> Vector3i; |
||||||
|
|
||||||
|
FunctionsBatchTest::FunctionsBatchTest() { |
||||||
|
addTests({&FunctionsBatchTest::minList, |
||||||
|
&FunctionsBatchTest::maxList, |
||||||
|
&FunctionsBatchTest::minmaxList}); |
||||||
|
} |
||||||
|
|
||||||
|
void FunctionsBatchTest::minList() { |
||||||
|
CORRADE_COMPARE(Math::min({5, -2, 9}), -2); |
||||||
|
CORRADE_COMPARE(Math::min({Vector3i(5, -3, 2), |
||||||
|
Vector3i(-2, 14, 7), |
||||||
|
Vector3i(9, -5, 18)}), Vector3i(-2, -5, 2)); |
||||||
|
|
||||||
|
CORRADE_COMPARE(Math::min(std::initializer_list<Vector3i>{}), Vector3i{}); |
||||||
|
|
||||||
|
const Int array[]{5, -2, 9}; |
||||||
|
CORRADE_COMPARE(Math::min(array), -2); |
||||||
|
} |
||||||
|
|
||||||
|
void FunctionsBatchTest::maxList() { |
||||||
|
CORRADE_COMPARE(Math::max({5, -2, 9}), 9); |
||||||
|
CORRADE_COMPARE(Math::max({Vector3i(5, -3, 2), |
||||||
|
Vector3i(-2, 14, 7), |
||||||
|
Vector3i(9, -5, 18)}), Vector3i(9, 14, 18)); |
||||||
|
|
||||||
|
CORRADE_COMPARE(Math::max(std::initializer_list<Vector3i>{}), Vector3i{}); |
||||||
|
|
||||||
|
const Int array[]{5, -2, 9}; |
||||||
|
CORRADE_COMPARE(Math::max(array), 9); |
||||||
|
} |
||||||
|
|
||||||
|
void FunctionsBatchTest::minmaxList() { |
||||||
|
const auto expected = std::make_pair(-3.0f, 2.0f); |
||||||
|
CORRADE_COMPARE(Math::minmax({-1.0f, 2.0f, -3.0f}), expected); |
||||||
|
CORRADE_COMPARE(Math::minmax({-1.0f, -3.0f, 2.0f}), expected); |
||||||
|
CORRADE_COMPARE(Math::minmax({2.0f, -1.0f, -3.0f}), expected); |
||||||
|
CORRADE_COMPARE(Math::minmax({2.0f, -3.0f, -1.0f}), expected); |
||||||
|
CORRADE_COMPARE(Math::minmax({-3.0f, 2.0f, -1.0f}), expected); |
||||||
|
CORRADE_COMPARE(Math::minmax({-3.0f, -1.0f, 2.0f}), expected); |
||||||
|
|
||||||
|
const std::pair<Vector2, Vector2> expectedVec{Vector2{-3.0f, -2.0f}, Vector2{2.0f, 3.0f}}; |
||||||
|
CORRADE_COMPARE(Math::minmax({Vector2{-1.0f, 3.0f}, Vector2{2.0f, 1.0f}, Vector2{-3.0f, -2.0f}}), expectedVec); |
||||||
|
CORRADE_COMPARE(Math::minmax({Vector2{-1.0f, 1.0f}, Vector2{-3.0f, 3.0f}, Vector2{2.0f, -2.0f}}), expectedVec); |
||||||
|
CORRADE_COMPARE(Math::minmax({Vector2{2.0f, -2.0f}, Vector2{-1.0f, 1.0f}, Vector2{-3.0f, 3.0f}}), expectedVec); |
||||||
|
CORRADE_COMPARE(Math::minmax({Vector2{2.0f, 1.0f}, Vector2{-3.0f, -2.0f}, Vector2{-1.0f, 3.0f}}), expectedVec); |
||||||
|
CORRADE_COMPARE(Math::minmax({Vector2{-3.0f, 3.0f}, Vector2{2.0f, -2.0f}, Vector2{-1.0f, 1.0f}}), expectedVec); |
||||||
|
CORRADE_COMPARE(Math::minmax({Vector2{-3.0f, -2.0f}, Vector2{-1.0f, 3.0f}, Vector2{2.0f, 1.0f}}), expectedVec); |
||||||
|
|
||||||
|
const Float array[]{-1.0f, 2.0f, -3.0f}; |
||||||
|
CORRADE_COMPARE(Math::minmax(array), expected); |
||||||
|
} |
||||||
|
|
||||||
|
}}}} |
||||||
|
|
||||||
|
CORRADE_TEST_MAIN(Magnum::Math::Test::FunctionsBatchTest) |
||||||
Loading…
Reference in new issue