Gamp v0.0.7-54-gccdc599
Gamp: Graphics, Audio, Multimedia and Processing
Loading...
Searching...
No Matches
vec2i.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_VEC2I_HPP_
13#define JAU_MATH_VEC2I_HPP_
14
15#include <cmath>
16#include <cstdarg>
17#include <cassert>
18#include <string>
19#include <algorithm>
20#include <iostream>
21
22#include <jau/float_math.hpp>
23#include <jau/type_concepts.hpp>
24
25namespace jau::math {
26
27 /** \addtogroup Math
28 *
29 * @{
30 */
31
32 /**
33 * 2D vector using two value_type components.
34 *
35 * Component and overall alignment is natural as sizeof(value_type),
36 * i.e. sizeof(value_type) == alignof(value_type)
37 */
38 template<jau::req::packed_integral Value_type>
39 class alignas(sizeof(Value_type)) Vector2I {
40 public:
43 typedef const value_type* const_pointer;
47 typedef const value_type* const_iterator;
48
49 /** value alignment is sizeof(value_type) */
50 constexpr static int value_alignment = sizeof(value_type);
51
52 /** Number of value_type components */
53 constexpr static const size_t components = 2;
54
55 /** Size in bytes with value_alignment */
56 constexpr static const size_t byte_size = components * sizeof(value_type);
57
58 typedef typename jau::float_bytes<sizeof(value_type)>::type float_type;
59
60 constexpr static const value_type zero = value_type(0);
61 constexpr static const value_type one = value_type(1);
62
65
66 constexpr Vector2I() noexcept
67 : x(zero), y(zero) {}
68
69 constexpr Vector2I(const value_type v) noexcept
70 : x(v), y(v) {}
71
72 constexpr Vector2I(const value_type x_, const value_type y_) noexcept
73 : x(x_), y(y_) {}
74
75 constexpr Vector2I(const Vector2I& o) noexcept = default;
76 constexpr Vector2I(Vector2I&& o) noexcept = default;
77 constexpr Vector2I& operator=(const Vector2I&) noexcept = default;
78 constexpr Vector2I& operator=(Vector2I&&) noexcept = default;
79
80 constexpr Vector2I copy() noexcept { return Vector2I(*this); }
81
82 /** Returns read-only component */
83 constexpr value_type operator[](size_t i) const noexcept {
84 assert(i < 2);
85 return (&x)[i];
86 }
87
88 explicit operator const_pointer() const noexcept { return &x; }
89 constexpr const_iterator cbegin() const noexcept { return &x; }
90
91 /** Returns writeable reference to component */
92 constexpr reference operator[](size_t i) noexcept {
93 assert(i < 2);
94 return (&x)[i];
95 }
96
97 explicit operator pointer() noexcept { return &x; }
98 constexpr iterator begin() noexcept { return &x; }
99
100 /** xy = this, returns xy. */
101 constexpr iterator get(iterator xy) const noexcept {
102 xy[0] = x;
103 xy[1] = y;
104 return xy;
105 }
106
107 constexpr bool operator==(const Vector2I& rhs ) const noexcept {
108 if( this == &rhs ) {
109 return true;
110 }
111 return x == rhs.x && y == rhs.y;
112 }
113 /** TODO
114 constexpr bool operator<=>(const vec2i_t& rhs ) const noexcept {
115 return ...
116 } */
117
118 size_t hashCode() const noexcept {
119 // 31 * x == (x << 5) - x
120 const size_t hash = x;
121 return ((hash << 5) - hash) + y;
122 }
123
124 constexpr Vector2I& set(const value_type vx, const value_type vy) noexcept
125 { x=vx; y=vy; return *this; }
126
127 /** this = xy, returns this. */
128 constexpr Vector2I& set(const_iterator xy) noexcept
129 { x=xy[0]; y=xy[1]; return *this; }
130
131 /** this = this + {d.x, d.y}, component wise. Returns this. */
132 constexpr Vector2I& add(const Vector2I& d) noexcept
133 { x+=d.x; y+=d.y; return *this; }
134
135 /** this = this + {sx, sy}, component wise. Returns this. */
136 constexpr Vector2I& add(const value_type dx, const value_type dy) noexcept
137 { x+=dx; y+=dy; return *this; }
138
139 /** this = this * {s.x, s.y}, component wise. Returns this. */
140 constexpr Vector2I& mul(const Vector2I& s) noexcept
141 { x*=s.x; y*=s.y; return *this; }
142
143 /** this = this * {sx, sy}, component wise. Returns this. */
144 constexpr Vector2I& mul(const value_type sx, const value_type sy) noexcept
145 { x*=sx; y*=sy; return *this; }
146
147 /** this = this * s, component wise. Returns this. */
148 constexpr Vector2I& scale(const value_type s) noexcept
149 { x*=s; y*=s; return *this; }
150
151 /** this = this + rhs, component wise. Returns this. */
152 constexpr Vector2I& operator+=(const Vector2I& rhs ) noexcept {
153 x+=rhs.x; y+=rhs.y;
154 return *this;
155 }
156
157 /** this = this - rhs, component wise. Returns this. */
158 constexpr Vector2I& operator-=(const Vector2I& rhs ) noexcept {
159 x-=rhs.x; y-=rhs.y;
160 return *this;
161 }
162
163 /**
164 * this = this * {s.x, s.y}, component wise.
165 * @param s scale factor
166 * @return this instance
167 */
168 constexpr Vector2I& operator*=(const Vector2I& s) noexcept {
169 x*=s.x; y*=s.y;
170 return *this;
171 }
172 /**
173 * this = this / {s.x, s.y}, component wise.
174 * @param s scale factor
175 * @return this instance
176 */
177 constexpr Vector2I& operator/=(const Vector2I& s) noexcept {
178 x/=s.x; y/=s.y;
179 return *this;
180 }
181
182 /**
183 * this = this * s, component wise.
184 * @param s scale factor
185 * @return this instance
186 */
187 constexpr Vector2I& operator*=(const value_type s ) noexcept {
188 x*=s; y*=s;
189 return *this;
190 }
191
192 /**
193 * this = this * s, component wise.
194 * @param s scale factor
195 * @return this instance
196 */
197 constexpr Vector2I& operator/=(const value_type s ) noexcept {
198 x/=s; y/=s;
199 return *this;
200 }
201
202 constexpr_cxx26 void rotate(const float_type radians, const Vector2I& ctr) {
203 const float_type cos = std::cos(radians);
204 const float_type sin = std::sin(radians);
205 rotate(sin, cos, ctr);
206 }
207 void rotate(const float_type sin, const float_type cos, const Vector2I& ctr) {
208 const float_type x0 = static_cast<float_type>(x - ctr.x);
209 const float_type y0 = static_cast<float_type>(y - ctr.y);
210 const value_type tmp = jau::round_to_int<float_type>( x0 * cos - y0 * sin ) + ctr.x;
211 y = jau::round_to_int<float_type>( x0 * sin + y0 * cos ) + ctr.y;
212 x = tmp;
213 }
214
215 std::string toString() const noexcept { return std::to_string(x)+", "+std::to_string(y); }
216
217 constexpr bool is_zero() const noexcept {
218 return jau::is_zero(x) && jau::is_zero(y);
219 }
220
221 /**
222 * Return the squared length of this vector, a.k.a the squared <i>norm</i> or squared <i>magnitude</i>
223 */
224 constexpr value_type length_sq() const noexcept {
225 return x*x + y*y;
226 }
227
228 /**
229 * Return the length of this vector, a.k.a the <i>norm</i> or <i>magnitude</i>
230 */
231 constexpr value_type length() const noexcept {
232 // return jau::round_to_int<float_type>( std::sqrt<float_type>( length_sq() ) );
233 return jau::round_to_int<float_type>( std::sqrt( static_cast<float_type> ( length_sq() )) );
234 }
235
236 /** Normalize this vector in place, returns *this */
237 constexpr Vector2I& normalize() noexcept {
238 const value_type lengthSq = length_sq();
239 if ( jau::is_zero( lengthSq ) ) {
240 x = zero;
241 y = zero;
242 } else {
243 const float_type invSqr = one / static_cast<float_type>( std::sqrt(lengthSq) );
244 x = jau::round_to_int<float_type>( x * invSqr );
245 y = jau::round_to_int<float_type>( x * invSqr );
246 }
247 return *this;
248 }
249
250
251 bool intersects(const Vector2I& o) {
252 return x == o.x && y == o.y;
253 }
254 };
255
256 template<jau::req::packed_integral T>
257 constexpr Vector2I<T> operator+(const Vector2I<T>& lhs, const Vector2I<T>& rhs ) noexcept {
258 // Returning a Vector2I<T> object from the returned reference of operator+=()
259 // may hinder copy-elision or "named return value optimization" (NRVO).
260 // return Vector2I<T>(lhs) += rhs;
261
262 // Returning named object allows copy-elision (NRVO),
263 // only one object is created 'on target'.
264 Vector2I<T> r(lhs); r += rhs; return r;
265 }
266
267 template<jau::req::packed_integral T>
268 constexpr Vector2I<T> operator-(const Vector2I<T>& lhs, const Vector2I<T>& rhs ) noexcept {
269 Vector2I<T> r(lhs); r -= rhs; return r;
270 }
271
272 template<jau::req::packed_integral T>
273 constexpr Vector2I<T> operator-(const Vector2I<T>& lhs) noexcept {
274 Vector2I<T> r(lhs);
275 r *= -1;
276 return r;
277 }
278
279 template<jau::req::packed_integral T>
280 constexpr Vector2I<T> operator*(const Vector2I<T>& lhs, const float s ) noexcept {
281 Vector2I<T> r(lhs); r *= s; return r;
282 }
283
284 template<jau::req::packed_integral T>
285 constexpr Vector2I<T> operator*(const float s, const Vector2I<T>& rhs) noexcept {
286 Vector2I<T> r(rhs); r *= s; return r;
287 }
288
289 template<jau::req::packed_integral T>
290 constexpr Vector2I<T> operator/(const Vector2I<T>& lhs, const float s ) noexcept {
291 Vector2I<T> r(lhs); r /= s; return r;
292 }
293
294 template<jau::req::packed_integral T>
295 constexpr Vector2I<T> operator/(const T s, const Vector2I<T>& rhs) noexcept {
296 Vector2I<T> r(rhs);
297 r.x=s/r.x; r.y=s/r.y;
298 return r;
299 }
300
301 template<jau::req::packed_integral T>
302 constexpr Vector2I<T> min(const Vector2I<T>& lhs, const Vector2I<T>& rhs) noexcept {
303 return Vector2I<T>(std::min(lhs.x, rhs.x), std::min(lhs.y, rhs.y));
304 }
305
306 template<jau::req::packed_integral T>
307 constexpr Vector2I<T> max(const Vector2I<T>& lhs, const Vector2I<T>& rhs) noexcept {
308 return Vector2I<T>(std::max(lhs.x, rhs.x), std::max(lhs.y, rhs.y));
309 }
310
311 template<jau::req::packed_integral T>
312 constexpr Vector2I<T> abs(const Vector2I<T>& lhs) noexcept {
313 return Vector2I<T>(std::abs(lhs.x), std::abs(lhs.y));
314 }
315
316 template<jau::req::packed_integral T>
317 std::ostream& operator<<(std::ostream& out, const Vector2I<T>& v) noexcept {
318 return out << v.toString();
319 }
320
322 static_assert(2 == Vec2i::components);
323 static_assert(sizeof(int) == Vec2i::value_alignment);
324 static_assert(sizeof(int) == alignof(Vec2i));
325 static_assert(sizeof(int)*2 == Vec2i::byte_size);
326 static_assert(sizeof(int)*2 == sizeof(Vec2i));
327
328 /**
329 * Point2I alias of Vector2I
330 */
331 template<jau::req::packed_integral T>
333
335 static_assert(2 == Point2i::components);
336 static_assert(sizeof(int) == Point2i::value_alignment);
337 static_assert(sizeof(int) == alignof(Point2i));
338 static_assert(sizeof(int)*2 == Point2i::byte_size);
339 static_assert(sizeof(int)*2 == sizeof(Point2i));
340
342 static_assert(2 == Point2i32::components);
343 static_assert(sizeof(int32_t) == Point2i32::value_alignment);
344 static_assert(sizeof(int32_t) == alignof(Point2i32));
345 static_assert(sizeof(int32_t)*2 == Point2i32::byte_size);
346 static_assert(sizeof(int32_t)*2 == sizeof(Point2i32));
347
349 static_assert(2 == Point2u32::components);
350 static_assert(sizeof(uint32_t) == Point2u32::value_alignment);
351 static_assert(sizeof(uint32_t) == alignof(Point2u32));
352 static_assert(sizeof(uint32_t)*2 == Point2u32::byte_size);
353 static_assert(sizeof(uint32_t)*2 == sizeof(Point2u32));
354
355 /**@}*/
356
357} // namespace jau::math
358
359#endif /* JAU_MATH_VEC2I_HPP_ */
360
2D vector using two value_type components.
Definition vec2i.hpp:39
constexpr bool operator==(const Vector2I &rhs) const noexcept
Definition vec2i.hpp:107
const value_type * const_iterator
Definition vec2i.hpp:47
constexpr Vector2I & normalize() noexcept
Normalize this vector in place, returns *this.
Definition vec2i.hpp:237
size_t hashCode() const noexcept
TODO constexpr bool operator<=>(const vec2i_t& rhs ) const noexcept { return ... }...
Definition vec2i.hpp:118
constexpr Vector2I & scale(const value_type s) noexcept
this = this * s, component wise.
Definition vec2i.hpp:148
constexpr Vector2I & operator/=(const Vector2I &s) noexcept
this = this / {s.x, s.y}, component wise.
Definition vec2i.hpp:177
constexpr Vector2I(const value_type v) noexcept
Definition vec2i.hpp:69
constexpr_cxx26 void rotate(const float_type radians, const Vector2I &ctr)
Definition vec2i.hpp:202
constexpr Vector2I & mul(const value_type sx, const value_type sy) noexcept
this = this * {sx, sy}, component wise.
Definition vec2i.hpp:144
constexpr Vector2I & operator+=(const Vector2I &rhs) noexcept
this = this + rhs, component wise.
Definition vec2i.hpp:152
static constexpr const size_t components
Definition vec2i.hpp:53
const value_type & const_reference
Definition vec2i.hpp:45
constexpr const_iterator cbegin() const noexcept
Definition vec2i.hpp:89
constexpr Vector2I & set(const_iterator xy) noexcept
this = xy, returns this.
Definition vec2i.hpp:128
std::string toString() const noexcept
Definition vec2i.hpp:215
const value_type * const_pointer
Definition vec2i.hpp:43
void rotate(const float_type sin, const float_type cos, const Vector2I &ctr)
Definition vec2i.hpp:207
constexpr Vector2I & set(const value_type vx, const value_type vy) noexcept
Definition vec2i.hpp:124
constexpr Vector2I & mul(const Vector2I &s) noexcept
this = this * {s.x, s.y}, component wise.
Definition vec2i.hpp:140
static constexpr int value_alignment
Definition vec2i.hpp:50
constexpr value_type operator[](size_t i) const noexcept
Returns read-only component.
Definition vec2i.hpp:83
constexpr value_type length() const noexcept
Return the length of this vector, a.k.a the norm or magnitude
Definition vec2i.hpp:231
value_type & reference
Definition vec2i.hpp:44
constexpr Vector2I & operator=(const Vector2I &) noexcept=default
constexpr Vector2I & operator*=(const value_type s) noexcept
this = this * s, component wise.
Definition vec2i.hpp:187
constexpr Vector2I & operator/=(const value_type s) noexcept
this = this * s, component wise.
Definition vec2i.hpp:197
static constexpr const value_type one
Definition vec2i.hpp:61
bool intersects(const Vector2I &o)
Definition vec2i.hpp:251
constexpr Vector2I & add(const value_type dx, const value_type dy) noexcept
this = this + {sx, sy}, component wise.
Definition vec2i.hpp:136
constexpr Vector2I & operator*=(const Vector2I &s) noexcept
this = this * {s.x, s.y}, component wise.
Definition vec2i.hpp:168
constexpr Vector2I & operator-=(const Vector2I &rhs) noexcept
this = this - rhs, component wise.
Definition vec2i.hpp:158
constexpr Vector2I copy() noexcept
Definition vec2i.hpp:80
value_type * iterator
Definition vec2i.hpp:46
constexpr Vector2I & operator=(Vector2I &&) noexcept=default
constexpr Vector2I & add(const Vector2I &d) noexcept
this = this + {d.x, d.y}, component wise.
Definition vec2i.hpp:132
constexpr Vector2I(Vector2I &&o) noexcept=default
static constexpr const size_t byte_size
Definition vec2i.hpp:56
jau::float_bytes< sizeof(value_type)>::type float_type
Definition vec2i.hpp:58
constexpr bool is_zero() const noexcept
Definition vec2i.hpp:217
constexpr Vector2I() noexcept
Definition vec2i.hpp:66
constexpr Vector2I(const Vector2I &o) noexcept=default
static constexpr const value_type zero
Definition vec2i.hpp:60
constexpr iterator get(iterator xy) const noexcept
xy = this, returns xy.
Definition vec2i.hpp:101
constexpr reference operator[](size_t i) noexcept
Returns writeable reference to component.
Definition vec2i.hpp:92
constexpr iterator begin() noexcept
Definition vec2i.hpp:98
Value_type value_type
Definition vec2i.hpp:41
constexpr value_type length_sq() const noexcept
Return the squared length of this vector, a.k.a the squared norm or squared magnitude
Definition vec2i.hpp:224
constexpr Vector2I(const value_type x_, const value_type y_) noexcept
Definition vec2i.hpp:72
value_type * pointer
Definition vec2i.hpp:42
#define constexpr_cxx26
constexpr jau::sint_bytes_t< sizeof(T)> round_to_int(const T v) noexcept
Returns the rounded value cast to signed int.
constexpr bool is_zero(const T &a, const T &epsilon=std::numeric_limits< T >::epsilon()) noexcept
Returns true if the given value is less than epsilon, w/ epsilon > 0.
Point2I< int > Point2i
Definition vec2i.hpp:334
constexpr Quaternion< T > operator-(const Quaternion< T > &lhs, const Quaternion< T > &rhs) noexcept
Vector2I< int > Vec2i
Definition vec2i.hpp:321
constexpr Matrix4< T > operator*(const Matrix4< T > &lhs, const Matrix4< T > &rhs) noexcept
Definition mat4f.hpp:1949
constexpr Vector2F< T > abs(const Vector2F< T > &lhs) noexcept
Definition vec2f.hpp:393
constexpr Quaternion< T > operator+(const Quaternion< T > &lhs, const Quaternion< T > &rhs) noexcept
constexpr Vector2F< T > max(const Vector2F< T > &lhs, const Vector2F< T > &rhs) noexcept
Definition vec2f.hpp:388
Vector2I< T > Point2I
Point2I alias of Vector2I.
Definition vec2i.hpp:332
Point2I< int32_t > Point2i32
Definition vec2i.hpp:341
std::ostream & operator<<(std::ostream &out, const Matrix4< T > &v) noexcept
Definition mat4f.hpp:1964
constexpr Vector2F< T > min(const Vector2F< T > &lhs, const Vector2F< T > &rhs) noexcept
Definition vec2f.hpp:383
Point2I< uint32_t > Point2u32
Definition vec2i.hpp:348
constexpr Vector2F< T > operator/(const Vector2F< T > &lhs, const T s) noexcept
Definition vec2f.hpp:371
uint8_t Value_type