jaulib v1.3.8
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/math/vec2i.hpp>
19
20namespace jau::math {
21
22 /** \addtogroup Math
23 *
24 * @{
25 */
26
27 /**
28 * Rectangle with x, y, width and height value_type components.
29 *
30 * Component and overall alignment is natural as sizeof(value_type),
31 * i.e. sizeof(value_type) == alignof(value_type)
32 */
33 template<typename Value_type,
34 std::enable_if_t<std::is_integral_v<Value_type> &&
35 sizeof(Value_type) == alignof(Value_type), bool> = true>
36 class alignas(sizeof(Value_type)) RectI {
37 public:
40 typedef const value_type* const_pointer;
44 typedef const value_type* const_iterator;
45
46 /** value alignment is sizeof(value_type) */
47 constexpr static int value_alignment = sizeof(value_type);
48
49 /** Number of value_type components */
50 constexpr static const size_t components = 4;
51
52 /** Size in bytes with value_alignment */
53 constexpr static const size_t byte_size = components * sizeof(value_type);
54
55 private:
56 value_type m_x;
57 value_type m_y;
58 value_type m_width;
59 value_type m_height;
60
61 public:
62 constexpr RectI() noexcept
63 : m_x(0), m_y(0), m_width(0), m_height(0) {}
64
65 constexpr RectI(const value_type xywh[/*4*/]) noexcept{
66 set(xywh);
67 }
68
69 constexpr RectI(const Vector2I<value_type>& pos, const Vector2I<value_type>& size) noexcept {
70 set(pos, size);
71 }
72
73 constexpr RectI(const value_type x, const value_type y, const value_type width, const value_type height) noexcept {
74 set(x, y, width, height);
75 }
76
77 constexpr RectI(const RectI& o) noexcept = default;
78 constexpr RectI(RectI&& o) noexcept = default;
79 constexpr RectI& operator=(const RectI&) noexcept = default;
80 constexpr RectI& operator=(RectI&&) noexcept = default;
81
82 constexpr bool operator==(const RectI& rhs ) const noexcept {
83 if( this == &rhs ) {
84 return true;
85 }
86 return m_x == rhs.m_x && m_y == rhs.m_y &&
87 m_width == rhs.m_width &&
88 m_height == rhs.m_height;
89 }
90 /** TODO
91 constexpr std::strong_ordering operator<=>(const Recti_t& rhs) const noexcept {
92 return ...
93 } */
94
95 constexpr RectI& set(const Vector2I<value_type>& pos, const Vector2I<value_type>& size) noexcept
96 { m_x = pos.x; m_y = pos.y; m_width = size.x; m_height= size.y; return *this; }
97
98 /** this = { x, y, width, height }, returns this. */
99 constexpr RectI& set(const value_type x, const value_type y, const value_type width, const value_type height) noexcept
100 { m_x = x; m_y = y; m_width = width; m_height= height; return *this; }
101
102 /** this = xywh, returns this. */
103 constexpr RectI& set(const_iterator xywh) noexcept
104 { m_x = xywh[0]; m_y = xywh[1]; m_width = xywh[2]; m_height= xywh[3]; return *this; }
105
106 /** xywh = this, returns xywh. */
107 iterator get(iterator xywh) const noexcept
108 { xywh[0] = m_x; xywh[1] = m_y; xywh[2] = m_width; xywh[3] = m_height; return xywh; }
109
110 constexpr value_type x() const noexcept { return m_x; }
111 constexpr value_type y() const noexcept { return m_y; }
112 constexpr value_type width() const noexcept { return m_width; }
113 constexpr value_type height() const noexcept { return m_height; }
114 constexpr Vector2I<value_type> getPosition() const noexcept { return Vector2I<value_type>(m_x, m_y); }
115 constexpr Vector2I<value_type> getSize() const noexcept { return Vector2I<value_type>(m_width, m_height); }
116
117 constexpr void setX(const value_type x) noexcept { m_x = x; }
118 constexpr void setY(const value_type y) noexcept { m_y = y; }
119 constexpr void setWidth(const value_type width) noexcept { m_width = width; }
120 constexpr void setHeight(const value_type height) noexcept { m_height = height; }
121 constexpr void setPosition(const Vector2I<value_type>& pos) noexcept { m_x = pos.x; m_y = pos.y; }
122 constexpr void setSize(const Vector2I<value_type>& size) noexcept { m_width = size.x; m_height = size.y; }
123
124 /** Return true if area is zero. */
125 constexpr bool is_zero() const noexcept {
126 return 0 == m_width || 0 == m_height;
127 }
128
129 std::string toString() const noexcept
130 { return std::to_string(m_x)+"/"+std::to_string(m_y)+" "+std::to_string(m_width)+"x"+std::to_string(m_height); }
131 };
132
133 template<typename T,
134 std::enable_if_t<std::numeric_limits<T>::is_integer, bool> = true>
135 std::ostream& operator<<(std::ostream& out, const RectI<T>& v) noexcept {
136 return out << v.toString();
137 }
138
140 static_assert(4 == Recti::components);
141 static_assert(sizeof(int) == Recti::value_alignment);
142 static_assert(sizeof(int) == alignof(Recti));
143 static_assert(sizeof(int)*4 == sizeof(Recti));
144
145/**@}*/
146
147} // namespace jau::math
148
149#endif /* JAU_MATH_RECTI2F_HPP_ */
150
Rectangle with x, y, width and height value_type components.
Definition recti.hpp:36
constexpr value_type y() const noexcept
Definition recti.hpp:111
constexpr RectI(const value_type xywh[]) noexcept
Definition recti.hpp:65
iterator get(iterator xywh) const noexcept
xywh = this, returns xywh.
Definition recti.hpp:107
constexpr Vector2I< value_type > getPosition() const noexcept
Definition recti.hpp:114
constexpr RectI & set(const_iterator xywh) noexcept
this = xywh, returns this.
Definition recti.hpp:103
const value_type & const_reference
Definition recti.hpp:42
constexpr void setPosition(const Vector2I< value_type > &pos) noexcept
Definition recti.hpp:121
std::string toString() const noexcept
Definition recti.hpp:129
constexpr void setHeight(const value_type height) noexcept
Definition recti.hpp:120
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:99
constexpr Vector2I< value_type > getSize() const noexcept
Definition recti.hpp:115
constexpr RectI & operator=(RectI &&) noexcept=default
static constexpr int value_alignment
Definition recti.hpp:47
constexpr RectI(const RectI &o) noexcept=default
constexpr value_type height() const noexcept
Definition recti.hpp:113
value_type * iterator
Definition recti.hpp:43
constexpr RectI(const Vector2I< value_type > &pos, const Vector2I< value_type > &size) noexcept
Definition recti.hpp:69
value_type & reference
Definition recti.hpp:41
constexpr value_type x() const noexcept
Definition recti.hpp:110
constexpr bool is_zero() const noexcept
Return true if area is zero.
Definition recti.hpp:125
static constexpr const size_t components
Definition recti.hpp:50
const value_type * const_iterator
Definition recti.hpp:44
constexpr RectI & operator=(const RectI &) noexcept=default
constexpr void setX(const value_type x) noexcept
Definition recti.hpp:117
constexpr void setSize(const Vector2I< value_type > &size) noexcept
Definition recti.hpp:122
Value_type value_type
Definition recti.hpp:38
constexpr void setY(const value_type y) noexcept
Definition recti.hpp:118
constexpr void setWidth(const value_type width) noexcept
Definition recti.hpp:119
constexpr RectI(RectI &&o) noexcept=default
constexpr RectI & set(const Vector2I< value_type > &pos, const Vector2I< value_type > &size) noexcept
TODO constexpr std::strong_ordering operator<=>(const Recti_t& rhs) const noexcept { return ....
Definition recti.hpp:95
constexpr RectI(const value_type x, const value_type y, const value_type width, const value_type height) noexcept
Definition recti.hpp:73
constexpr RectI() noexcept
Definition recti.hpp:62
const value_type * const_pointer
Definition recti.hpp:40
static constexpr const size_t byte_size
Definition recti.hpp:53
value_type * pointer
Definition recti.hpp:39
constexpr value_type width() const noexcept
Definition recti.hpp:112
2D vector using two value_type components.
Definition vec2i.hpp:41
RectI< int > Recti
Definition recti.hpp:139
std::ostream & operator<<(std::ostream &out, const Matrix4< T > &v) noexcept
Definition mat4f.hpp:1924
uint8_t Value_type