jaulib v1.3.6
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 (c) 2022-2024 Gothel Software e.K.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining
6 * a copy of this software and associated documentation files (the
7 * "Software"), to deal in the Software without restriction, including
8 * without limitation the rights to use, copy, modify, merge, publish,
9 * distribute, sublicense, and/or sell copies of the Software, and to
10 * permit persons to whom the Software is furnished to do so, subject to
11 * the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be
14 * included in all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 */
24#ifndef JAU_RECTI2F_HPP_
25#define JAU_RECTI2F_HPP_
26
27#include <iostream>
28
29#include <jau/int_math.hpp>
30#include <jau/math/vec2i.hpp>
31
32namespace jau::math {
33
34 /** \addtogroup Math
35 *
36 * @{
37 */
38
39 /**
40 * Rectangle with x, y, width and height value_type components.
41 *
42 * Component and overall alignment is natural as sizeof(value_type),
43 * i.e. sizeof(value_type) == alignof(value_type)
44 */
45 template<typename Value_type,
46 std::enable_if_t<std::is_integral_v<Value_type> &&
47 sizeof(Value_type) == alignof(Value_type), bool> = true>
48 class alignas(sizeof(Value_type)) RectI {
49 public:
52 typedef const value_type* const_pointer;
56 typedef const value_type* const_iterator;
57
58 /** value alignment is sizeof(value_type) */
59 constexpr static int value_alignment = sizeof(value_type);
60
61 /** Number of value_type components */
62 constexpr static const size_t components = 4;
63
64 /** Size in bytes with value_alignment */
65 constexpr static const size_t byte_size = components * sizeof(value_type);
66
67 private:
68 value_type m_x;
69 value_type m_y;
70 value_type m_width;
71 value_type m_height;
72
73 public:
74 constexpr RectI() noexcept
75 : m_x(0), m_y(0), m_width(0), m_height(0) {}
76
77 constexpr RectI(const value_type xywh[/*4*/]) noexcept{
78 set(xywh);
79 }
80
81 constexpr RectI(const Vector2I<value_type>& pos, const Vector2I<value_type>& size) noexcept {
82 set(pos, size);
83 }
84
85 constexpr RectI(const value_type x, const value_type y, const value_type width, const value_type height) noexcept {
86 set(x, y, width, height);
87 }
88
89 constexpr RectI(const RectI& o) noexcept = default;
90 constexpr RectI(RectI&& o) noexcept = default;
91 constexpr RectI& operator=(const RectI&) noexcept = default;
92 constexpr RectI& operator=(RectI&&) noexcept = default;
93
94 constexpr bool operator==(const RectI& rhs ) const noexcept {
95 if( this == &rhs ) {
96 return true;
97 }
98 return m_x == rhs.m_x && m_y == rhs.m_y &&
99 m_width == rhs.m_width &&
100 m_height == rhs.m_height;
101 }
102 /** TODO
103 constexpr bool operator<=>(const Recti_t& rhs ) const noexcept {
104 return ...
105 } */
106
107 constexpr RectI& set(const Vector2I<value_type>& pos, const Vector2I<value_type>& size) noexcept
108 { m_x = pos.x; m_y = pos.y; m_width = size.x; m_height= size.y; return *this; }
109
110 /** this = { x, y, width, height }, returns this. */
111 constexpr RectI& set(const value_type x, const value_type y, const value_type width, const value_type height) noexcept
112 { m_x = x; m_y = y; m_width = width; m_height= height; return *this; }
113
114 /** this = xywh, returns this. */
115 constexpr RectI& set(const_iterator xywh) noexcept
116 { m_x = xywh[0]; m_y = xywh[1]; m_width = xywh[2]; m_height= xywh[3]; return *this; }
117
118 /** xywh = this, returns xywh. */
119 iterator get(iterator xywh) const noexcept
120 { xywh[0] = m_x; xywh[1] = m_y; xywh[2] = m_width; xywh[3] = m_height; return xywh; }
121
122 constexpr value_type x() const noexcept { return m_x; }
123 constexpr value_type y() const noexcept { return m_y; }
124 constexpr value_type width() const noexcept { return m_width; }
125 constexpr value_type height() const noexcept { return m_height; }
126 constexpr Vector2I<value_type> getPosition() const noexcept { return Vector2I<value_type>(m_x, m_y); }
127 constexpr Vector2I<value_type> getSize() const noexcept { return Vector2I<value_type>(m_width, m_height); }
128
129 constexpr void setX(const value_type x) noexcept { m_x = x; }
130 constexpr void setY(const value_type y) noexcept { m_y = y; }
131 constexpr void setWidth(const value_type width) noexcept { m_width = width; }
132 constexpr void setHeight(const value_type height) noexcept { m_height = height; }
133 constexpr void setPosition(const Vector2I<value_type>& pos) noexcept { m_x = pos.x; m_y = pos.y; }
134 constexpr void setSize(const Vector2I<value_type>& size) noexcept { m_width = size.x; m_height = size.y; }
135
136 /** Return true if area is zero. */
137 constexpr bool is_zero() const noexcept {
138 return 0 == m_width || 0 == m_height;
139 }
140
141 std::string toString() const noexcept
142 { return std::to_string(m_x)+"/"+std::to_string(m_y)+" "+std::to_string(m_width)+"x"+std::to_string(m_height); }
143 };
144
145 template<typename T,
146 std::enable_if_t<std::numeric_limits<T>::is_integer, bool> = true>
147 std::ostream& operator<<(std::ostream& out, const RectI<T>& v) noexcept {
148 return out << v.toString();
149 }
150
152 static_assert(4 == Recti::components);
153 static_assert(sizeof(int) == Recti::value_alignment);
154 static_assert(sizeof(int) == alignof(Recti));
155 static_assert(sizeof(int)*4 == sizeof(Recti));
156
157/**@}*/
158
159} // namespace jau::math
160
161#endif /* JAU_RECTI2F_HPP_ */
162
Rectangle with x, y, width and height value_type components.
Definition recti.hpp:48
constexpr value_type y() const noexcept
Definition recti.hpp:123
constexpr RectI(const value_type xywh[]) noexcept
Definition recti.hpp:77
iterator get(iterator xywh) const noexcept
xywh = this, returns xywh.
Definition recti.hpp:119
constexpr Vector2I< value_type > getPosition() const noexcept
Definition recti.hpp:126
constexpr RectI & set(const_iterator xywh) noexcept
this = xywh, returns this.
Definition recti.hpp:115
const value_type & const_reference
Definition recti.hpp:54
constexpr void setPosition(const Vector2I< value_type > &pos) noexcept
Definition recti.hpp:133
std::string toString() const noexcept
Definition recti.hpp:141
constexpr void setHeight(const value_type height) noexcept
Definition recti.hpp:132
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:111
constexpr Vector2I< value_type > getSize() const noexcept
Definition recti.hpp:127
constexpr RectI & operator=(RectI &&) noexcept=default
static constexpr int value_alignment
Definition recti.hpp:59
constexpr RectI(const RectI &o) noexcept=default
constexpr value_type height() const noexcept
Definition recti.hpp:125
value_type * iterator
Definition recti.hpp:55
constexpr RectI(const Vector2I< value_type > &pos, const Vector2I< value_type > &size) noexcept
Definition recti.hpp:81
value_type & reference
Definition recti.hpp:53
constexpr value_type x() const noexcept
Definition recti.hpp:122
constexpr bool is_zero() const noexcept
Return true if area is zero.
Definition recti.hpp:137
static constexpr const size_t components
Definition recti.hpp:62
const value_type * const_iterator
Definition recti.hpp:56
constexpr RectI & operator=(const RectI &) noexcept=default
constexpr void setX(const value_type x) noexcept
Definition recti.hpp:129
constexpr void setSize(const Vector2I< value_type > &size) noexcept
Definition recti.hpp:134
Value_type value_type
Definition recti.hpp:50
constexpr void setY(const value_type y) noexcept
Definition recti.hpp:130
constexpr void setWidth(const value_type width) noexcept
Definition recti.hpp:131
constexpr RectI(RectI &&o) noexcept=default
constexpr RectI & set(const Vector2I< value_type > &pos, const Vector2I< value_type > &size) noexcept
TODO constexpr bool operator<=>(const Recti_t& rhs ) const noexcept { return ... }...
Definition recti.hpp:107
constexpr RectI(const value_type x, const value_type y, const value_type width, const value_type height) noexcept
Definition recti.hpp:85
constexpr RectI() noexcept
Definition recti.hpp:74
const value_type * const_pointer
Definition recti.hpp:52
static constexpr const size_t byte_size
Definition recti.hpp:65
value_type * pointer
Definition recti.hpp:51
constexpr value_type width() const noexcept
Definition recti.hpp:124
2D vector using two value_type components.
Definition vec2i.hpp:53
RectI< int > Recti
Definition recti.hpp:151
std::ostream & operator<<(std::ostream &out, const Matrix4< T > &v) noexcept
Definition mat4f.hpp:1881
uint8_t Value_type