diff --git a/Userland/Libraries/LibGfx/VectorN.h b/Userland/Libraries/LibGfx/VectorN.h index 6e07a3b49d..abbe3372bb 100644 --- a/Userland/Libraries/LibGfx/VectorN.h +++ b/Userland/Libraries/LibGfx/VectorN.h @@ -1,5 +1,6 @@ /* * Copyright (c) 2020, Stephan Unverwerth + * Copyright (c) 2022, Jelle Raaijmakers * Copyright (c) 2022, the SerenityOS developers. * * SPDX-License-Identifier: BSD-2-Clause @@ -27,8 +28,12 @@ #endif namespace Gfx { + template requires(N >= 2 && N <= 4) class VectorN final { + template + 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 + [[nodiscard]] VectorN to_rounded() const + { + VectorN result; + UNROLL_LOOP + for (auto i = 0u; i < N; ++i) { + if constexpr (IsSame) + result.m_data[i] = static_cast(lrintf(m_data[i])); + else + result.m_data[i] = static_cast(lrint(m_data[i])); + } + return result; + } + private: AK::Array m_data; }; + }