1 #ifndef __DALI_UINT_16_PAIR_H__
2 #define __DALI_UINT_16_PAIR_H__
5 * Copyright (c) 2015 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.
25 #include <dali/public-api/common/dali-common.h>
26 #include <dali/public-api/common/type-traits.h>
31 * @addtogroup dali_core_math
36 * @brief Simple class for passing around pairs of small unsigned integers.
38 * Use this for integer dimensions and points with limited range such as image
39 * sizes and pixel coordinates where a pair of floating point numbers is
40 * inefficient and illogical (i.e. the data is inherently integer).
41 * One of these can be passed in a single 32 bit integer register on
42 * common architectures.
49 * @brief Default constructor for the (0, 0) tuple.
52 Uint16Pair() : mData(0) {}
55 * @brief Constructor taking separate x and y (width and height) parameters.
57 * @param[in] width The width or X dimension of the tuple. Make sure it is less than 65536,
58 * @param[in] height The height or Y dimension of the tuple. Make sure it is less than 65536,
60 Uint16Pair( uint32_t width, uint32_t height )
62 DALI_ASSERT_DEBUG( width < ( 1u << 16 ) && "Width parameter not representable." );
63 DALI_ASSERT_DEBUG( height < ( 1u << 16 ) && "Height parameter not representable." );
65 /* Do equivalent of the code below with one aligned memory access:
66 * mComponents[0] = width;
67 * mComponents[1] = height;
68 * Unit tests make sure this is equivalent.
70 mData = (height << 16u) + width;
74 * @brief Copy constructor.
76 * @param[in] rhs A reference to assign
78 Uint16Pair( const Uint16Pair& rhs )
84 * @brief Sets the width.
86 * @param[in] width The x dimension to be stored in this 2-tuple.
88 void SetWidth( uint16_t width )
90 mComponents[0] = width;
94 * @brief @returns the x dimension stored in this 2-tuple.
97 uint16_t GetWidth() const
99 return mComponents[0];
103 * @brief Sets the height.
105 * @param[in] height The y dimension to be stored in this 2-tuple.
107 void SetHeight( uint16_t height )
109 mComponents[1] = height;
113 * @brief Returns the y dimension stored in this 2-tuple.
117 uint16_t GetHeight() const
119 return mComponents[1];
123 * @brief Sets the x dimension (same as width).
125 * @param[in] x The x dimension to be stored in this 2-tuple.
127 void SetX( uint16_t x )
133 * @brief Returns the x dimension stored in this 2-tuple.
137 uint16_t GetX() const
139 return mComponents[0];
143 * @brief Sets the y dimension (same as height).
145 * @param[in] y The y dimension to be stored in this 2-tuple.
147 void SetY( uint16_t y )
153 * @brief Returns the y dimension stored in this 2-tuple.
157 uint16_t GetY() const
159 return mComponents[1];
163 * @brief Assignment operator.
165 * @param[in] rhs Handle to an object
166 * @return A reference to this
168 Uint16Pair& operator=( const Uint16Pair& rhs )
178 * @brief Equality operator.
180 * @param[in] rhs A reference for comparison
181 * @return True if same
183 bool operator==( const Uint16Pair& rhs ) const
185 return mData == rhs.mData;
189 * @brief Inequality operator.
191 * @param[in] rhs A reference for comparison
192 * @return True if different
194 bool operator!=( const Uint16Pair& rhs ) const
196 return mData != rhs.mData;
200 * @brief Less than comparison operator for storing in collections (not geometrically
203 * @param[in] rhs A reference for comparison
204 * @return True if less
206 bool operator<( const Uint16Pair& rhs ) const
208 return mData < rhs.mData;
212 * @brief Greater than comparison operator for storing in collections (not
213 * geometrically meaningful).
215 * @param[in] rhs A reference for comparison
216 * @return True if greater
218 bool operator>( const Uint16Pair& rhs ) const
220 return mData > rhs.mData;
224 * @brief Create an instance by rounding a floating point vector to closest
227 * Uses a template for loose coupling, to save a header include, and allow any
228 * vector type with .x and .y members to be converted.
230 * @param[in] from Floating point vector2
231 * @return Closest integer value.
233 template<typename FLOAT_VECTOR_N_TYPE>
234 static Uint16Pair FromFloatVec2( const FLOAT_VECTOR_N_TYPE& from )
236 DALI_ASSERT_DEBUG( from.x + 0.5f < 65536.0f );
237 DALI_ASSERT_DEBUG( from.y + 0.5f < 65536.0f );
238 return Uint16Pair( from.x + 0.5f, from.y + 0.5f );
242 * @brief Create an instance by rounding a floating point array to closest
245 * Uses a template to allow any vector type with operator [] to be converted
246 * in addition to plain arrays.
248 * @param[in] from Floating point array
249 * @return Closest integer value.
251 template<typename FLOAT_ARRAY>
252 static Uint16Pair FromFloatArray( const FLOAT_ARRAY& from )
254 DALI_ASSERT_DEBUG( from[0] + 0.5f < 65536.0f );
255 DALI_ASSERT_DEBUG( from[1] + 0.5f < 65536.0f );
256 return Uint16Pair( from[0] + 0.5f, from[1] + 0.5f );
262 // Addressable view of X and Y:
263 uint16_t mComponents[2];
264 // Packed view of X and Y to force alignment and allow a faster copy:
269 // Allow Uint16Pair to be treated as a POD type
270 template <> struct TypeTraits< Uint16Pair > : public BasicTypes< Uint16Pair > { enum { IS_TRIVIAL_TYPE = true }; };
277 #endif // __DALI_UINT_16_PAIR_H__