Scanframe Modular Application 0.1.0
Loading...
Searching...
No Matches
TVector2D.h
Go to the documentation of this file.
1// ReSharper disable file CppNonExplicitConversionOperator
2#pragma once
3
4#include <limits>
5#include <string>
6
7namespace sf
8{
9
10// Forward declaration.
11template<typename T>
12class TMatrix22;
13
14// Forward declaration.
15template<typename T>
16class TRectangle2D;
17
21template<typename T>
23{
24 public:
28 typedef T value_type;
29
33 TVector2D() = default;
34
39
43 TVector2D(TVector2D&&) noexcept;
44
50 TVector2D(T xp, T yp);
51
58 TVector2D& assign(T xp, T yp);
59
66
70 TVector2D& operator=(const TVector2D& v) noexcept;
71
75 TVector2D& operator=(TVector2D&& v) noexcept;
76
80 TVector2D& operator*=(const TMatrix22<T>& mtx);
81
85 TVector2D operator-() const;
86
90 TVector2D& operator+=(const TVector2D&);
91
95 TVector2D& operator-=(const TVector2D&);
96
100 TVector2D& operator*=(T c);
101
105 TVector2D& operator/=(T);
106
110 TVector2D operator+(const TVector2D&) const;
111
115 TVector2D operator-(const TVector2D&) const;
116
120 TVector2D operator/(T) const;
121
128 bool isEqual(const TVector2D& v, T tol = tolerance) const;
129
133 bool operator==(const TVector2D&) const;
134
138 bool operator!=(const TVector2D&) const;
139
144 T& operator[](size_t);
145
150 const T& operator[](size_t) const;
151
155 constexpr T* data();
156
160 constexpr const T* data() const;
161
165 constexpr operator T*();
166
170 constexpr operator const T*() const;
171
175 constexpr T x() const;
176
180 constexpr T& x();
181
185 constexpr T y() const;
186
190 constexpr T& y();
191
198 T length() const;
199
204 T lengthSqr() const;
205
211
217
222 TVector2D& scale(T factor);
223
228 TVector2D scaled(T factor) const;
229
234 T crossProduct(const TVector2D&) const;
235
240 T dotProduct(const TVector2D&) const;
241
246 T operator*(const TVector2D& v) const;
247
252 T angle(const TVector2D&) const;
253
258 T angle() const;
259
265
271 T slope() const;
272
278 T distance(const TVector2D&) const;
279
286 T distanceSqr(const TVector2D&) const;
287
292 void updateMin(const TVector2D& vertex);
293
298 void updateMax(const TVector2D& vertex);
299
303 std::string toString() const;
304
310 TVector2D& fromString(const std::string& s) noexcept(false);
311
317 static constexpr auto tolerance = std::numeric_limits<T>::epsilon() * 10.0;
318
327 static int areOnSameSide(const TVector2D& lp1, const TVector2D& lp2, const TVector2D& p1, const TVector2D& p2);
328
329 protected:
334 {
335 T array[2];
337 {
338 T x;
339 T y;
340 } coord;
341 } _data{0, 0};
342
343 friend class TRectangle2D<T>;
344};
345
346template<typename T>
348{
349 return {v.x() * c, v.y() * c};
350}
351
352template<typename T>
354{
355 return {v.x() * c, v.y() * c};
356}
357
361template<typename T>
362std::istream& operator>>(std::istream& is, TVector2D<T>& v) noexcept(false)
363{
364 std::string s;
365 constexpr auto delimiter = ')';
366 std::getline(is, s, delimiter);
367 v.fromString(s.append(1, delimiter));
368 return is;
369}
370
374template<typename T>
375std::ostream& operator<<(std::ostream& os, const TVector2D<T>& v)
376{
377 return os << v.toString();
378}
379
380}// namespace sf
381
382// Include all inlined functions and template implementations.
383#include <math/TVector2D.hpp>
Generic 2 x 2 matrix template.
Definition TMatrix22.h:16
2-dimensional vector for math operations.
Definition TVector2D.h:23
T lengthSqr() const
Gets the squared length or magnitude of the vector.
TVector2D(const TVector2D &v)
Copy constructor.
T distanceSqr(const TVector2D &) const
Gets the squared distance between 2 given points. Avoids taking an expensive sqrt call....
static constexpr auto tolerance
Tolerance for when comparing in the equal operator. Empirical chosen epsilon multiplier to make it wo...
Definition TVector2D.h:317
TVector2D & fromString(const std::string &s) noexcept(false)
Gets the vector value from the string representation formed like '(1.23,4.56)'. Throws an exception w...
T dotProduct(const TVector2D &) const
Gets the dot (in) product of 2 vectors.
TVector2D(TVector2D &&) noexcept
Move constructor.
TVector2D scaled(T factor) const
Scales the vector by multiplying all axis with the passed factor.
static int areOnSameSide(const TVector2D &lp1, const TVector2D &lp2, const TVector2D &p1, const TVector2D &p2)
Checks where 2 points lie in relation to a given line given by 2 points.
std::string toString() const
Gets the string representation of the 2D vector formed like '(1.23,4.56)'.
TVector2D & scale(T factor)
Scales the vector by multiplying all axis with the passed factor.
constexpr T * data()
Gets a const pointer to the data.
TVector2D normalized() const
Gets a normalized vector also called a unit-vector, made of length 1.
T angleNormalized() const
Returns a normalized positive angle of function angle().
TVector2D & assign(T xp, T yp)
Assignment of new coordinate values.
T angle(const TVector2D &) const
Gets the angle between the two vectors.
union sf::TVector2D::data_type _data
TVector2D()=default
Default constructor.
T slope() const
Gets the slope (coefficient) of the line formed with the origin (0, 0). When it is an infinite value ...
T crossProduct(const TVector2D &) const
Gets the cross (out) product of 2 vectors.
void updateMax(const TVector2D &vertex)
Copy only those values of x or y which are larger.
T length() const
Gets the length or magnitude of the vector.
TVector2D & normalize()
Normalizes the vector also called a unit-vector, set to length 1.
constexpr T x() const
Gets the x-coordinate value.
constexpr T y() const
Gets the y-coordinate value.
T distance(const TVector2D &) const
Gets the distance between this and the passed vector. Note that this has to return a double because i...
T value_type
Type declaration of the coordinate storage values.
Definition TVector2D.h:28
bool isEqual(const TVector2D &v, T tol=tolerance) const
Compares the passed vector within the set tolerance.
void updateMin(const TVector2D &vertex)
Copy only those values of x or y which are smaller.
Definition Application.h:10
_GII_FUNC std::istream & operator>>(std::istream &is, ResultData &)
Stream operator for setting up this instance with a setup std::string.
TMatrix44< T > operator*(const TMatrix44< T > &lm, const TMatrix44< T > &rm)
Multiplies to matrices into a single one.
_GII_FUNC std::ostream & operator<<(std::ostream &os, const ResultData &)
Stream operator for the setup std::string.
Definition TVector2D.h:337
T y
Definition TVector2D.h:339
T x
Definition TVector2D.h:338
Storage union of the 2D coordinate making the x,y accessible as an array.
Definition TVector2D.h:334