1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
|
/*
* Author: Sven Gothel <sgothel@jausoft.com>
* Copyright (c) 2022-2024 Gothel Software e.K.
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#ifndef JAU_VEC2F_HPP_
#define JAU_VEC2F_HPP_
#include <cmath>
#include <cstdarg>
#include <cstdint>
#include <limits>
#include <string>
#include <iostream>
#include <jau/float_math.hpp>
namespace jau::math {
/** \addtogroup Math
*
* @{
*/
/**
* 2D vector using two float components.
*/
class Vec2f {
public:
float x;
float y;
static constexpr Vec2f from_length_angle(const float magnitude, const float radians) noexcept {
return Vec2f(magnitude * std::cos(radians), magnitude * std::sin(radians));
}
constexpr Vec2f() noexcept
: x(0), y(0) {}
constexpr Vec2f(const float x_, const float y_) noexcept
: x(x_), y(y_) {}
constexpr Vec2f(const Vec2f& o) noexcept = default;
constexpr Vec2f(Vec2f&& o) noexcept = default;
constexpr Vec2f& operator=(const Vec2f&) noexcept = default;
constexpr Vec2f& operator=(Vec2f&&) noexcept = default;
/** Returns read-only component w/o boundary check */
float operator[](size_t i) const noexcept {
return reinterpret_cast<const float*>(this)[i];
}
/** Returns writeable reference to component w/o boundary check */
float& operator[](size_t i) noexcept {
return reinterpret_cast<float*>(this)[i];
}
/** xy = this, returns xy. */
float* get(float xy[/*2*/]) const noexcept {
xy[0] = x;
xy[1] = y;
return xy;
}
constexpr bool operator==(const Vec2f& rhs ) const noexcept {
if( this == &rhs ) {
return true;
}
return jau::is_zero(x - rhs.x) && jau::is_zero(y - rhs.y);
}
/** TODO
constexpr bool operator<=>(const vec2f_t& rhs ) const noexcept {
return ...
} */
constexpr Vec2f& set(const float vx, const float vy) noexcept
{ x=vx; y=vy; return *this; }
constexpr Vec2f& add(const float dx, const float dy) noexcept
{ x+=dx; y+=dy; return *this; }
constexpr Vec2f& operator+=(const Vec2f& rhs ) noexcept {
x+=rhs.x; y+=rhs.y;
return *this;
}
constexpr Vec2f& operator-=(const Vec2f& rhs ) noexcept {
x-=rhs.x; y-=rhs.y;
return *this;
}
/**
* Scale this vector with given scale factor
* @param s scale factor
* @return this instance
*/
constexpr Vec2f& operator*=(const float s ) noexcept {
x*=s; y*=s;
return *this;
}
/**
* Divide this vector with given scale factor
* @param s scale factor
* @return this instance
*/
constexpr Vec2f& operator/=(const float s ) noexcept {
x/=s; y/=s;
return *this;
}
/** Rotates this vector in place, returns *this */
Vec2f& rotate(const float radians, const Vec2f& ctr) noexcept {
return rotate(std::sin(radians), std::cos(radians), ctr);
}
/** Rotates this vector in place, returns *this */
constexpr Vec2f& rotate(const float sin, const float cos, const Vec2f& ctr) noexcept {
const float x0 = x - ctr.x;
const float y0 = y - ctr.y;
x = x0 * cos - y0 * sin + ctr.x;
y = x0 * sin + y0 * cos + ctr.y;
return *this;
}
/** Rotates this vector in place, returns *this */
Vec2f& rotate(const float radians) noexcept {
return rotate(std::sin(radians), std::cos(radians));
}
/** Rotates this vector in place, returns *this */
constexpr Vec2f& rotate(const float sin, const float cos) noexcept {
const float x0 = x;
x = x0 * cos - y * sin;
y = x0 * sin + y * cos;
return *this;
}
std::string toString() const noexcept { return std::to_string(x)+" / "+std::to_string(y); }
constexpr bool is_zero() const noexcept {
return jau::is_zero(x) && jau::is_zero(y);
}
/**
* Return the squared length of this vector, a.k.a the squared <i>norm</i> or squared <i>magnitude</i>
*/
constexpr float length_sq() const noexcept {
return x*x + y*y;
}
/**
* Return the length of this vector, a.k.a the <i>norm</i> or <i>magnitude</i>
*/
constexpr float length() const noexcept {
return std::sqrt(length_sq());
}
/**
* Return the direction angle of this vector in radians
*/
float angle() const noexcept {
// Utilize atan2 taking y=sin(a) and x=cos(a), resulting in proper direction angle for all quadrants.
return std::atan2( y, x );
}
/** Normalize this vector in place, returns *this */
constexpr Vec2f& normalize() noexcept {
const float lengthSq = length_sq();
if ( jau::is_zero( lengthSq ) ) {
x = 0.0f;
y = 0.0f;
} else {
const float invSqr = 1.0f / std::sqrt(lengthSq);
x *= invSqr;
y *= invSqr;
}
return *this;
}
/**
* Return the squared distance between this vector and the given one.
* <p>
* When comparing the relative distance between two points it is usually sufficient to compare the squared
* distances, thus avoiding an expensive square root operation.
* </p>
*/
constexpr float dist_sq(const Vec2f& o) const noexcept {
const float dx = x - o.x;
const float dy = y - o.y;
return dx*dx + dy*dy;
}
/**
* Return the distance between this vector and the given one.
*/
constexpr float dist(const Vec2f& o) const noexcept {
return std::sqrt(dist_sq(o));
}
/**
* Return the dot product of this vector and the given one
* @return the dot product as float
*/
constexpr float dot(const Vec2f& o) const noexcept {
return x*o.x + y*o.y;
}
/**
* Returns cross product of this vectors and the given one, i.e. *this x o.
*
* The 2D cross product is identical with the 2D perp dot product.
*
* @return the resulting scalar
*/
constexpr float cross(const Vec2f& o) const noexcept {
return x * o.y - y * o.x;
}
/**
* Return the cosines of the angle between two vectors
*/
constexpr float cos_angle(const Vec2f& o) const noexcept {
return dot(o) / ( length() * o.length() ) ;
}
/**
* Return the angle between two vectors in radians
*/
float angle(const Vec2f& o) const noexcept {
return std::acos( cos_angle(o) );
}
/**
* Return the counter-clock-wise (CCW) normal of this vector, i.e. perp(endicular) vector
*/
Vec2f normal_ccw() const noexcept {
return Vec2f(-y, x);
}
bool intersects(const Vec2f& o) const noexcept {
const float eps = std::numeric_limits<float>::epsilon();
if( std::abs(x-o.x) >= eps || std::abs(y-o.y) >= eps ) {
return false;
}
return true;
}
};
typedef Vec2f Point2f;
constexpr Vec2f operator+(const Vec2f& lhs, const Vec2f& rhs ) noexcept {
return Vec2f(lhs) += rhs;
}
constexpr Vec2f operator-(const Vec2f& lhs, const Vec2f& rhs ) noexcept {
return Vec2f(lhs) -= rhs;
}
constexpr Vec2f operator*(const Vec2f& lhs, const float s ) noexcept {
return Vec2f(lhs) *= s;
}
constexpr Vec2f operator*(const float s, const Vec2f& rhs) noexcept {
return Vec2f(rhs) *= s;
}
constexpr Vec2f operator/(const Vec2f& lhs, const float s ) noexcept {
return Vec2f(lhs) /= s;
}
std::ostream& operator<<(std::ostream& out, const Vec2f& v) noexcept {
return out << v.toString();
}
/**
* Simple compound denoting a ray.
* <p>
* A ray, also known as a half line, consists out of it's <i>origin</i>
* and <i>direction</i>. Hence it is bound to only the <i>origin</i> side,
* where the other end is +infinitive.
* <pre>
* R(t) = R0 + Rd * t with R0 origin, Rd direction and t > 0.0
* </pre>
* </p>
*/
class Ray2f {
public:
/** Origin of Ray. */
Point2f orig;
/** Normalized direction vector of ray. */
Vec2f dir;
std::string toString() const noexcept { return "Ray[orig "+orig.toString()+", dir "+dir.toString() +"]"; }
};
std::ostream& operator<<(std::ostream& out, const Ray2f& v) noexcept {
return out << v.toString();
}
/**@}*/
} // namespace jau::math
#endif /* JAU_VEC2F_HPP_ */
|