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#include <limits>
4#include <string>
5
6namespace sf
7{
8
9// Forward declaration.
10template<typename T>
11class TMatrix22;
12
13// Forward declaration.
14template<typename T>
15class TRectangle2D;
16
20template<typename T>
22{
23 public:
27 typedef T value_type;
28
32 TVector2D() = default;
33
38
42 TVector2D(TVector2D&&) noexcept;
43
49 TVector2D(T xp, T yp);
50
57 TVector2D& assign(T xp, T yp);
58
65
69 TVector2D& operator=(const TVector2D& v) noexcept;
70
74 TVector2D& operator=(TVector2D&& v) noexcept;
75
79 TVector2D& operator*=(const TMatrix22<T>& mtx);
80
84 TVector2D operator-() const;
85
89 TVector2D& operator+=(const TVector2D&);
90
94 TVector2D& operator-=(const TVector2D&);
95
99 TVector2D& operator*=(T c);
100
104 TVector2D& operator/=(T);
105
109 TVector2D operator+(const TVector2D&) const;
110
114 TVector2D operator-(const TVector2D&) const;
115
119 TVector2D operator/(T) const;
120
127 bool isEqual(const TVector2D& v, T tol = tolerance) const;
128
132 bool operator==(const TVector2D&) const;
133
137 bool operator!=(const TVector2D&) const;
138
143 T& operator[](size_t);
144
149 const T& operator[](size_t) const;
150
154 constexpr T* data();
155
159 constexpr const T* data() const;
160
164 constexpr operator T*();
165
169 constexpr operator const T*() const;
170
174 constexpr T x() const;
175
179 constexpr T& x();
180
184 constexpr T y() const;
185
189 constexpr T& y();
190
197 T length() const;
198
203 T lengthSqr() const;
204
210
216
221 TVector2D& scale(T factor);
222
227 TVector2D scaled(T factor) const;
228
233 T crossProduct(const TVector2D&) const;
234
239 T dotProduct(const TVector2D&) const;
240
245 T operator*(const TVector2D& v) const;
246
251 T angle(const TVector2D&) const;
252
257 T angle() const;
258
264
270 T slope() const;
271
277 T distance(const TVector2D&) const;
278
285 T distanceSqr(const TVector2D&) const;
286
291 void updateMin(const TVector2D& vertex);
292
297 void updateMax(const TVector2D& vertex);
298
302 std::string toString() const;
303
312 TVector2D& fromString(const std::string& s, bool ignore_err = false) noexcept(false);
313
319 static constexpr auto tolerance = std::numeric_limits<T>::epsilon() * 10.0;
320
329 static int areOnSameSide(const TVector2D& lp1, const TVector2D& lp2, const TVector2D& p1, const TVector2D& p2);
330
331 protected:
336 {
337 T array[2];
338
340 {
341 T x;
342 T y;
343 } coord;
344 } _data{0, 0};
345
347};
348
349template<typename T>
351{
352 return {v.x() * c, v.y() * c};
353}
354
355template<typename T>
357{
358 return {v.x() * c, v.y() * c};
359}
360
364template<typename T>
365std::istream& operator>>(std::istream& is, TVector2D<T>& v) noexcept(false)
366{
367 std::string s;
368 constexpr auto delimiter = ')';
369 std::getline(is, s, delimiter);
370 v.fromString(s.append(1, delimiter));
371 return is;
372}
373
377template<typename T>
378std::ostream& operator<<(std::ostream& os, const TVector2D<T>& v)
379{
380 return os << v.toString();
381}
382
383}// namespace sf
384
385#ifndef __JETBRAINS_IDE__
386 // Include all inlined functions and template implementations.
387 #include <math/TVector2D.hpp>
388#endif
Generic 2 x 2 matrix template.
Definition TMatrix22.h:16
Class to store and manipulate a rectangle based on floating point values.
Definition TRectangle2D.h:22
2-dimensional vector for math operations.
Definition TVector2D.h:22
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:319
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 axes 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 axes 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.
TVector2D & fromString(const std::string &s, bool ignore_err=false) noexcept(false)
Gets the vector value from the string representation formed like '(1.23,4.56)'. Throws an exception w...
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:27
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:340
T y
Definition TVector2D.h:342
T x
Definition TVector2D.h:341
Storage union of the 2D coordinate making the x,y accessible as an array.
Definition TVector2D.h:336