jaulib v1.4.0-2-g788cf73
Jau Support Library (C++, Java, ..)
Loading...
Searching...
No Matches
recti.hpp
Go to the documentation of this file.
1/*
2 * Author: Sven Gothel <sgothel@jausoft.com>
3 * Copyright Gothel Software e.K.
4 *
5 * SPDX-License-Identifier: MIT
6 *
7 * This Source Code Form is subject to the terms of the MIT License
8 * If a copy of the MIT was not distributed with this file,
9 * you can obtain one at https://opensource.org/license/mit/.
10 */
11
12#ifndef JAU_MATH_RECTI2F_HPP_
13#define JAU_MATH_RECTI2F_HPP_
14
15#include <iostream>
16
17#include <jau/int_math.hpp>
18#include <jau/type_concepts.hpp>
19#include <jau/math/vec2i.hpp>
20
21namespace jau::math {
22
23 /** \addtogroup Math
24 *
25 * @{
26 */
27
28 /**
29 * Rectangle with x, y, width and height value_type components.
30 *
31 * Component and overall alignment is natural as sizeof(value_type),
32 * i.e. sizeof(value_type) == alignof(value_type)
33 */
34 template<jau::req::packed_integral Value_type>
35 class alignas(sizeof(Value_type)) RectI {
36 public:
39 typedef const value_type* const_pointer;
43 typedef const value_type* const_iterator;
44
45 /** value alignment is sizeof(value_type) */
46 constexpr static int value_alignment = sizeof(value_type);
47
48 /** Number of value_type components */
49 constexpr static const size_t components = 4;
50
51 /** Size in bytes with value_alignment */
52 constexpr static const size_t byte_size = components * sizeof(value_type);
53
54 private:
55 value_type m_x;
56 value_type m_y;
57 value_type m_width;
58 value_type m_height;
59
60 public:
61 constexpr RectI() noexcept
62 : m_x(0), m_y(0), m_width(0), m_height(0) {}
63
64 constexpr RectI(const value_type xywh[/*4*/]) noexcept{
65 set(xywh);
66 }
67
68 constexpr RectI(const Vector2I<value_type>& pos, const Vector2I<value_type>& size) noexcept {
69 set(pos, size);
70 }
71
72 constexpr RectI(const value_type x, const value_type y, const value_type width, const value_type height) noexcept {
73 set(x, y, width, height);
74 }
75
76 constexpr RectI(const RectI& o) noexcept = default;
77 constexpr RectI(RectI&& o) noexcept = default;
78 constexpr RectI& operator=(const RectI&) noexcept = default;
79 constexpr RectI& operator=(RectI&&) noexcept = default;
80
81 constexpr bool operator==(const RectI& rhs ) const noexcept {
82 if( this == &rhs ) {
83 return true;
84 }
85 return m_x == rhs.m_x && m_y == rhs.m_y &&
86 m_width == rhs.m_width &&
87 m_height == rhs.m_height;
88 }
89 /** TODO
90 constexpr std::strong_ordering operator<=>(const Recti_t& rhs) const noexcept {
91 return ...
92 } */
93
94 size_t hashCode() const noexcept {
95 const size_t sum1 = size_t(m_x) + size_t(m_height);
96 const size_t sum2 = size_t(m_width) + size_t(m_y);
97 const size_t val1 = sum1 * (sum1 + 1)/2 + size_t(m_x);
98 const size_t val2 = sum2 * (sum2 + 1)/2 + size_t(m_y);
99 const size_t sum3 = val1 + val2;
100 return sum3 * (sum3 + 1)/2 + val2;
101 }
102
103 constexpr RectI& set(const Vector2I<value_type>& pos, const Vector2I<value_type>& size) noexcept
104 { m_x = pos.x; m_y = pos.y; m_width = size.x; m_height= size.y; return *this; }
105
106 /** this = { x, y, width, height }, returns this. */
107 constexpr RectI& set(const value_type x, const value_type y, const value_type width, const value_type height) noexcept
108 { m_x = x; m_y = y; m_width = width; m_height= height; return *this; }
109
110 /** this = xywh, returns this. */
111 constexpr RectI& set(const_iterator xywh) noexcept
112 { m_x = xywh[0]; m_y = xywh[1]; m_width = xywh[2]; m_height= xywh[3]; return *this; }
113
114 /** xywh = this, returns xywh. */
115 iterator get(iterator xywh) const noexcept
116 { xywh[0] = m_x; xywh[1] = m_y; xywh[2] = m_width; xywh[3] = m_height; return xywh; }
117
118 constexpr value_type x() const noexcept { return m_x; }
119 constexpr value_type y() const noexcept { return m_y; }
120 constexpr value_type width() const noexcept { return m_width; }
121 constexpr value_type height() const noexcept { return m_height; }
122 constexpr Vector2I<value_type> getPosition() const noexcept { return Vector2I<value_type>(m_x, m_y); }
123 constexpr Vector2I<value_type> getSize() const noexcept { return Vector2I<value_type>(m_width, m_height); }
124
125 constexpr void setX(const value_type x) noexcept { m_x = x; }
126 constexpr void setY(const value_type y) noexcept { m_y = y; }
127 constexpr void setWidth(const value_type width) noexcept { m_width = width; }
128 constexpr void setHeight(const value_type height) noexcept { m_height = height; }
129 constexpr void setPosition(const Vector2I<value_type>& pos) noexcept { m_x = pos.x; m_y = pos.y; }
130 constexpr void setSize(const Vector2I<value_type>& size) noexcept { m_width = size.x; m_height = size.y; }
131
132 /** Return true if area is zero. */
133 constexpr bool is_zero() const noexcept {
134 return 0 == m_width || 0 == m_height;
135 }
136
137 std::string toString() const noexcept
138 { return std::to_string(m_x)+"/"+std::to_string(m_y)+" "+std::to_string(m_width)+"x"+std::to_string(m_height); }
139 };
140
141 template<jau::req::packed_integral T>
142 std::ostream& operator<<(std::ostream& out, const RectI<T>& v) noexcept {
143 return out << v.toString();
144 }
145
147 static_assert(4 == Recti::components);
148 static_assert(sizeof(int) == Recti::value_alignment);
149 static_assert(sizeof(int) == alignof(Recti));
150 static_assert(sizeof(int)*4 == sizeof(Recti));
151
152/**@}*/
153
154} // namespace jau::math
155
156#endif /* JAU_MATH_RECTI2F_HPP_ */
157
Rectangle with x, y, width and height value_type components.
Definition recti.hpp:35
constexpr RectI(const value_type xywh[]) noexcept
Definition recti.hpp:64
constexpr RectI() noexcept
Definition recti.hpp:61
constexpr value_type x() const noexcept
Definition recti.hpp:118
constexpr Vector2I< value_type > getSize() const noexcept
Definition recti.hpp:123
constexpr value_type height() const noexcept
Definition recti.hpp:121
constexpr void setY(const value_type y) noexcept
Definition recti.hpp:126
constexpr RectI & set(const Vector2I< value_type > &pos, const Vector2I< value_type > &size) noexcept
Definition recti.hpp:103
static constexpr int value_alignment
Definition recti.hpp:46
size_t hashCode() const noexcept
TODO constexpr std::strong_ordering operator<=>(const Recti_t& rhs) const noexcept { return ....
Definition recti.hpp:94
constexpr RectI(const RectI &o) noexcept=default
constexpr void setWidth(const value_type width) noexcept
Definition recti.hpp:127
Value_type value_type
Definition recti.hpp:37
const value_type * const_iterator
Definition recti.hpp:43
constexpr RectI(RectI &&o) noexcept=default
constexpr RectI(const value_type x, const value_type y, const value_type width, const value_type height) noexcept
Definition recti.hpp:72
constexpr RectI & set(const value_type x, const value_type y, const value_type width, const value_type height) noexcept
this = { x, y, width, height }, returns this.
Definition recti.hpp:107
value_type * pointer
Definition recti.hpp:38
constexpr void setHeight(const value_type height) noexcept
Definition recti.hpp:128
constexpr RectI & operator=(const RectI &) noexcept=default
std::string toString() const noexcept
Definition recti.hpp:137
const value_type * const_pointer
Definition recti.hpp:39
constexpr RectI & operator=(RectI &&) noexcept=default
const value_type & const_reference
Definition recti.hpp:41
constexpr void setX(const value_type x) noexcept
Definition recti.hpp:125
static constexpr const size_t byte_size
Definition recti.hpp:52
value_type * iterator
Definition recti.hpp:42
constexpr Vector2I< value_type > getPosition() const noexcept
Definition recti.hpp:122
iterator get(iterator xywh) const noexcept
xywh = this, returns xywh.
Definition recti.hpp:115
constexpr RectI & set(const_iterator xywh) noexcept
this = xywh, returns this.
Definition recti.hpp:111
constexpr void setPosition(const Vector2I< value_type > &pos) noexcept
Definition recti.hpp:129
constexpr value_type width() const noexcept
Definition recti.hpp:120
constexpr RectI(const Vector2I< value_type > &pos, const Vector2I< value_type > &size) noexcept
Definition recti.hpp:68
constexpr bool is_zero() const noexcept
Return true if area is zero.
Definition recti.hpp:133
constexpr void setSize(const Vector2I< value_type > &size) noexcept
Definition recti.hpp:130
value_type & reference
Definition recti.hpp:40
static constexpr const size_t components
Definition recti.hpp:49
constexpr value_type y() const noexcept
Definition recti.hpp:119
2D vector using two value_type components.
Definition vec2i.hpp:39
std::ostream & operator<<(std::ostream &out, const Matrix4< T > &v) noexcept
Definition mat4f.hpp:1964
RectI< int > Recti
Definition recti.hpp:146
uint8_t Value_type