1 #ifndef DALI_UINT_16_PAIR_H
2 #define DALI_UINT_16_PAIR_H
5 * Copyright (c) 2022 Samsung Electronics Co., Ltd.
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
22 #include <dali/public-api/math/int-pair.h>
27 * @addtogroup dali_core_math
32 * @brief Simple class for passing around pairs of small unsigned integers.
34 * Use this for integer dimensions and points with limited range such as image
35 * sizes and pixel coordinates where a pair of floating point numbers is
36 * inefficient and illogical (i.e. the data is inherently integer).
37 * One of these can be passed in a single 32 bit integer register on
38 * common architectures.
41 class Uint16Pair : public IntPair<16, false>
45 * @brief Default constructor for the (0, 0) tuple.
48 constexpr Uint16Pair() = default;
51 * @brief Constructor taking separate x and y (width and height) parameters.
53 * @param[in] width The width or X dimension of the tuple. Make sure it is less than 65536.
54 * @param[in] height The height or Y dimension of the tuple. Make sure it is less than 65536.
56 constexpr Uint16Pair(uint32_t width, uint32_t height)
57 : IntPair(static_cast<uint16_t>(width), static_cast<uint16_t>(height))
59 DALI_ASSERT_DEBUG(width < (1u << 16) && "Width parameter not representable.");
60 DALI_ASSERT_DEBUG(height < (1u << 16) && "Height parameter not representable.");
64 * @brief Creates an instance by rounding a floating point vector to closest
67 * Uses a template for loose coupling, to save a header include, and allow any
68 * vector type with .x and .y members to be converted.
70 * @param[in] from Floating point vector2
71 * @return Closest integer value
73 template<typename FLOAT_VECTOR_N_TYPE>
74 static Uint16Pair FromFloatVec2(const FLOAT_VECTOR_N_TYPE& from)
76 DALI_ASSERT_DEBUG(from.x + 0.5f < 65536.0f);
77 DALI_ASSERT_DEBUG(from.y + 0.5f < 65536.0f);
78 return Uint16Pair(from.x + 0.5f, from.y + 0.5f);
82 * @brief Creates an instance by rounding a floating point array to closest
85 * Uses a template to allow any vector type with operator [] to be converted
86 * in addition to plain arrays.
88 * @param[in] from Floating point array
89 * @return Closest integer value
91 template<typename FLOAT_ARRAY>
92 static Uint16Pair FromFloatArray(const FLOAT_ARRAY& from)
94 DALI_ASSERT_DEBUG(from[0] + 0.5f < 65536.0f);
95 DALI_ASSERT_DEBUG(from[1] + 0.5f < 65536.0f);
96 return Uint16Pair(from[0] + 0.5f, from[1] + 0.5f);
100 Uint16Pair(const Uint16Pair&) = default; ///< Default copy constructor
101 Uint16Pair(Uint16Pair&&) noexcept = default; ///< Default move constructor
102 Uint16Pair& operator=(const Uint16Pair&) = default; ///< Default copy assignment operator
103 Uint16Pair& operator=(Uint16Pair&&) noexcept = default; ///< Default move assignment operator
106 // Allow Uint16Pair to be treated as a POD type
108 struct TypeTraits<Uint16Pair> : public BasicTypes<Uint16Pair>
112 IS_TRIVIAL_TYPE = true
121 #endif // DALI_UINT_16_PAIR_H