mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-11-22 07:30:19 +00:00
LibGfx: Add Vector::to_rounded<T>()
This commit is contained in:
parent
54108263d6
commit
7db68e118c
Notes:
sideshowbarker
2024-07-17 11:17:56 +09:00
Author: https://github.com/gmta Commit: https://github.com/SerenityOS/serenity/commit/7db68e118c Pull-request: https://github.com/SerenityOS/serenity/pull/13835 Reviewed-by: https://github.com/sunverwerth ✅
1 changed files with 20 additions and 0 deletions
|
@ -1,5 +1,6 @@
|
|||
/*
|
||||
* Copyright (c) 2020, Stephan Unverwerth <s.unverwerth@serenityos.org>
|
||||
* Copyright (c) 2022, Jelle Raaijmakers <jelle@gmta.nl>
|
||||
* Copyright (c) 2022, the SerenityOS developers.
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
|
@ -27,8 +28,12 @@
|
|||
#endif
|
||||
|
||||
namespace Gfx {
|
||||
|
||||
template<size_t N, typename T>
|
||||
requires(N >= 2 && N <= 4) class VectorN final {
|
||||
template<size_t U, typename V>
|
||||
friend class VectorN;
|
||||
|
||||
static_assert(LOOP_UNROLL_N >= N, "Unroll the entire loop for performance.");
|
||||
|
||||
public:
|
||||
|
@ -226,7 +231,22 @@ public:
|
|||
return String::formatted("[{},{},{},{}]", x(), y(), z(), w());
|
||||
}
|
||||
|
||||
template<typename U>
|
||||
[[nodiscard]] VectorN<N, U> to_rounded() const
|
||||
{
|
||||
VectorN<N, U> result;
|
||||
UNROLL_LOOP
|
||||
for (auto i = 0u; i < N; ++i) {
|
||||
if constexpr (IsSame<T, float>)
|
||||
result.m_data[i] = static_cast<U>(lrintf(m_data[i]));
|
||||
else
|
||||
result.m_data[i] = static_cast<U>(lrint(m_data[i]));
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
private:
|
||||
AK::Array<T, N> m_data;
|
||||
};
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue