jaulib v1.3.0
Jau Support Library (C++, Java, ..)
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
31namespace jau::math {
32
33 /** \addtogroup Math
34 *
35 * @{
36 */
37
38 /**
39 * Rectangle with x, y, width and height integer components.
40 */
41 template<typename Value_type,
42 std::enable_if_t<std::is_integral_v<Value_type>, bool> = true>
43 class RectI {
44 public:
47 typedef const value_type* const_pointer;
51 typedef const value_type* const_iterator;
52
53 private:
54 value_type m_x;
55 value_type m_y;
56 value_type m_width;
57 value_type m_height;
58
59 public:
60 constexpr RectI() noexcept
61 : m_x(0), m_y(0), m_width(0), m_height(0) {}
62
63 constexpr RectI(const value_type xywh[/*4*/]) noexcept{
64 set(xywh);
65 }
66
67 constexpr RectI(const value_type x, const value_type y, const value_type width, const value_type height) noexcept {
68 set(x, y, width, height);
69 }
70
71 constexpr RectI(const RectI& o) noexcept = default;
72 constexpr RectI(RectI&& o) noexcept = default;
73 constexpr RectI& operator=(const RectI&) noexcept = default;
74 constexpr RectI& operator=(RectI&&) noexcept = default;
75
76 constexpr bool operator==(const RectI& rhs ) const noexcept {
77 if( this == &rhs ) {
78 return true;
79 }
80 return m_x == rhs.m_x && m_y == rhs.m_y &&
81 m_width == rhs.m_width &&
82 m_height == rhs.m_height;
83 }
84 /** TODO
85 constexpr bool operator<=>(const Recti_t& rhs ) const noexcept {
86 return ...
87 } */
88
89 /** this = { x, y, width, height }, returns this. */
90 constexpr RectI& set(const value_type x, const value_type y, const value_type width, const value_type height) noexcept
91 { m_x = x; m_y = y; m_width = width; m_height= height; return *this; }
92
93 /** this = xywh, returns this. */
94 constexpr RectI& set(const_iterator xywh) noexcept
95 { m_x = xywh[0]; m_y = xywh[1]; m_width = xywh[2]; m_height= xywh[3]; return *this; }
96
97 /** xywh = this, returns xywh. */
98 iterator get(iterator xywh) const noexcept
99 { xywh[0] = m_x; xywh[1] = m_y; xywh[2] = m_width; xywh[3] = m_height; return xywh; }
100
101 constexpr value_type x() const noexcept { return m_x; }
102 constexpr value_type y() const noexcept { return m_y; }
103 constexpr value_type width() const noexcept { return m_width; }
104 constexpr value_type height() const noexcept { return m_height; }
105
106 constexpr void setX(const value_type x) noexcept { m_x = x; }
107 constexpr void setY(const value_type y) noexcept { m_y = y; }
108 constexpr void setWidth(const value_type width) noexcept { m_width = width; }
109 constexpr void setHeight(const value_type height) noexcept { m_height = height; }
110
111 /** Return true if area is zero. */
112 constexpr bool is_zero() const noexcept {
113 return 0 == m_width || 0 == m_height;
114 }
115
116 std::string toString() const noexcept
117 { return std::to_string(m_x)+" / "+std::to_string(m_y)+" "+std::to_string(m_width)+" x "+std::to_string(m_height); }
118 };
119
120 template<typename T,
121 std::enable_if_t<std::numeric_limits<T>::is_integer, bool> = true>
122 std::ostream& operator<<(std::ostream& out, const RectI<T>& v) noexcept {
123 return out << v.toString();
124 }
125
127 static_assert(alignof(int) == alignof(Recti));
128 static_assert(sizeof(int)*4 == sizeof(Recti));
129
130/**@}*/
131
132} // namespace jau::math
133
134#endif /* JAU_RECTI2F_HPP_ */
135
Rectangle with x, y, width and height integer components.
Definition: recti.hpp:43
constexpr value_type y() const noexcept
Definition: recti.hpp:102
constexpr RectI(const value_type xywh[]) noexcept
Definition: recti.hpp:63
iterator get(iterator xywh) const noexcept
xywh = this, returns xywh.
Definition: recti.hpp:98
constexpr RectI & set(const_iterator xywh) noexcept
this = xywh, returns this.
Definition: recti.hpp:94
const value_type & const_reference
Definition: recti.hpp:49
std::string toString() const noexcept
Definition: recti.hpp:116
constexpr void setHeight(const value_type height) noexcept
Definition: recti.hpp:109
constexpr RectI & set(const value_type x, const value_type y, const value_type width, const value_type height) noexcept
TODO constexpr bool operator<=>(const Recti_t& rhs ) const noexcept { return ... }.
Definition: recti.hpp:90
constexpr RectI & operator=(RectI &&) noexcept=default
constexpr RectI(const RectI &o) noexcept=default
constexpr value_type height() const noexcept
Definition: recti.hpp:104
value_type * iterator
Definition: recti.hpp:50
value_type & reference
Definition: recti.hpp:48
constexpr value_type x() const noexcept
Definition: recti.hpp:101
constexpr bool is_zero() const noexcept
Return true if area is zero.
Definition: recti.hpp:112
const value_type * const_iterator
Definition: recti.hpp:51
constexpr RectI & operator=(const RectI &) noexcept=default
constexpr void setX(const value_type x) noexcept
Definition: recti.hpp:106
Value_type value_type
Definition: recti.hpp:45
constexpr void setY(const value_type y) noexcept
Definition: recti.hpp:107
constexpr void setWidth(const value_type width) noexcept
Definition: recti.hpp:108
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:67
constexpr RectI() noexcept
Definition: recti.hpp:60
const value_type * const_pointer
Definition: recti.hpp:47
value_type * pointer
Definition: recti.hpp:46
constexpr value_type width() const noexcept
Definition: recti.hpp:103
std::string to_string(const alphabet &v) noexcept
Definition: base_codec.hpp:97
RectI< int > Recti
Definition: recti.hpp:126
std::ostream & operator<<(std::ostream &out, const Matrix4< T > &v) noexcept
Definition: mat4f.hpp:1877
uint8_t Value_type