1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 18:47:45 +00:00

LibGfx: Add Vector::to_rounded<T>()

This commit is contained in:
Jelle Raaijmakers 2022-04-29 15:09:16 +02:00 committed by Andreas Kling
parent 54108263d6
commit 7db68e118c

View file

@ -1,5 +1,6 @@
/* /*
* Copyright (c) 2020, Stephan Unverwerth <s.unverwerth@serenityos.org> * Copyright (c) 2020, Stephan Unverwerth <s.unverwerth@serenityos.org>
* Copyright (c) 2022, Jelle Raaijmakers <jelle@gmta.nl>
* Copyright (c) 2022, the SerenityOS developers. * Copyright (c) 2022, the SerenityOS developers.
* *
* SPDX-License-Identifier: BSD-2-Clause * SPDX-License-Identifier: BSD-2-Clause
@ -27,8 +28,12 @@
#endif #endif
namespace Gfx { namespace Gfx {
template<size_t N, typename T> template<size_t N, typename T>
requires(N >= 2 && N <= 4) class VectorN final { 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."); static_assert(LOOP_UNROLL_N >= N, "Unroll the entire loop for performance.");
public: public:
@ -226,7 +231,22 @@ public:
return String::formatted("[{},{},{},{}]", x(), y(), z(), w()); 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: private:
AK::Array<T, N> m_data; AK::Array<T, N> m_data;
}; };
} }