diff options
author | Jelle Raaijmakers <jelle@gmta.nl> | 2022-04-29 15:09:16 +0200 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2022-05-05 20:50:46 +0200 |
commit | 7db68e118cc4df7f5a5c8cb6a8d238b1c93ccb66 (patch) | |
tree | 102588f88ced150401c325f8090b9da5ea365390 /Userland/Libraries/LibGfx | |
parent | 54108263d6aee0b0087d62e857dc65a8e83d7676 (diff) | |
download | serenity-7db68e118cc4df7f5a5c8cb6a8d238b1c93ccb66.zip |
LibGfx: Add `Vector::to_rounded<T>()`
Diffstat (limited to 'Userland/Libraries/LibGfx')
-rw-r--r-- | Userland/Libraries/LibGfx/VectorN.h | 20 |
1 files changed, 20 insertions, 0 deletions
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 <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; }; + } |