SLProject  4.2.000
A platform independent 3D computer graphics framework for desktop OS, Android, iOS and online in web browsers
SLVec2.h
Go to the documentation of this file.
1 /**
2  * \file math/SLVec2.h
3  * \date July 2014
4  * \authors Marcus Hudritsch
5  * \copyright http://opensource.org/licenses/GPL-3.0
6  * \remarks Please use clangformat to format the code. See more code style on
7  * https://github.com/cpvrlab/SLProject4/wiki/SLProject-Coding-Style
8 */
9 
10 #ifndef SLVEC2_H
11 #define SLVEC2_H
12 
13 #include <SLMath.h>
14 #include <Utils.h>
15 
16 //-----------------------------------------------------------------------------
17 //! 2D vector template class for standard 2D vector algebra.
18 /*!
19 2D vector template class and type definitions for 2D vectors;
20 \n Use SLVec2f for a specific float type vector
21 \n Use SLVec2d for a specific double type vector
22 \n Use SLVec2r for a precision independent real type.
23 */
24 // clang-format off
25 template<class T>
26 class SLVec2
27 {
28  public:
29  union
30  { struct {T x, y;};
31  struct {T comp[2];};
32  };
33 
34  SLVec2 () {x=0;y=0;}
35  SLVec2 (const T X,
36  const T Y) {x=X;y=Y;}
37  SLVec2 (const T v[2]) {x=v[0]; y=v[1];}
38  SLVec2 (const SLVec2& v) {x=v.x; y=v.y;}
39 
40  void set (const T X,
41  const T Y) {x=X; y=Y;}
42  void set (const T v[2]) {x=v[0]; y=v[1];}
43  void set (const SLVec2& v) {x=v.x; y=v.y;}
44 
45  // Component wise compare
46  inline SLbool operator == (const SLVec2& v) const {return (x==v.x && y==v.y);}
47  inline SLbool operator != (const SLVec2& v) const {return (x!=v.x || y!=v.y);}
48  inline SLbool operator <= (const SLVec2& v) const {return (x<=v.x && y<=v.y);}
49  inline SLbool operator >= (const SLVec2& v) const {return (x>=v.x && y>=v.y);}
50  inline SLbool operator < (const SLVec2& v) const {return (x<v.x && y<v.y);}
51  inline SLbool operator > (const SLVec2& v) const {return (x>v.x && y>v.y);}
52  inline SLbool operator <= (const T v) const {return (x<=v && y<=v);}
53  inline SLbool operator >= (const T v) const {return (x>=v && y>=v);}
54  inline SLbool operator < (const T v) const {return (x< v && y< v);}
55  inline SLbool operator > (const T v) const {return (x> v && y> v);}
56 
57  // Operators with temp. allocation
58  inline SLVec2 operator + (const SLVec2& v) const {return SLVec2(x+v.x, y+v.y);}
59  inline SLVec2 operator - (const SLVec2& v) const {return SLVec2(x-v.x, y-v.y);}
60  inline SLVec2 operator - () const {return SLVec2(-x, -y);}
61  inline T operator * (const SLVec2& v) const {return x*v.x+y*v.y;} //dot
62  inline SLVec2 operator * (const T s) const {return SLVec2(x*s, y*s);}
63  inline SLVec2 operator / (const T s) const {return SLVec2(x/s, y/s);}
64  inline SLVec2 operator & (const SLVec2& v) const {return SLVec2(x*v.x, y*v.y);}
65  friend inline
66  SLVec2 operator * (T s, const SLVec2& v) {return SLVec2(v.x*s, v.y*s);}
67 
68  // Assign operators
69  SLVec2& operator = (const SLVec2& v) {x=v.x; y=v.y; return *this;}
70  SLVec2& operator += (const SLVec2& v) {x+=v.x; y+=v.y; return *this;}
71  SLVec2& operator -= (const SLVec2& v) {x-=v.x; y-=v.y; return *this;}
72  SLVec2& operator *= (const T s) {x*=s; y*=s; return *this;}
73  SLVec2& operator /= (const T s) {x/=s; y/=s; return *this;}
74  SLVec2& operator &= (const SLVec2& v) {x*=v.x; y*=v.y; return *this;}
75 
76  // Stream output operator
77  friend std::ostream& operator << (std::ostream& output,
78  const SLVec2& v){output<<"["<<v.x<<","<<v.y<<"]"; return output;}
79 
80  // Operations without temp. allocation
81  void add (const SLVec2& a,
82  const SLVec2& b) {x=a.x+b.x; y=a.y+b.y;}
83  void sub (const SLVec2& a,
84  const SLVec2& b) {x=a.x-b.x; y=a.y-b.y;}
85  void scale (const T s) {x*=s; y*=s;}
86  T dot (const SLVec2& v) {return x*v.x+y*v.y;}
87  T length () const {return ((T)sqrt(x*x+y*y));}
88  T lengthSqr () const {return (x*x+y*y); }
89  SLVec2& normalize () {T L=length();
90  if (L>0) {x/=L; y/=L;}
91  return *this;}
92  void clampMinMax (const T min,
93  const T max) {x = (x>max)?max : (x<min)?min : x;
94  y = (y>max)?max : (y<min)?min : y;}
95 
96  //! Calculates polar coords with radius & angle phi in radians (-pi < phi < pi)
97  void toPolar (T& r, T& phiRAD) {r = length();
98  phiRAD = atan2(y,x);}
99 
100  //! Calculates the vector from polar coords r & phi in radians (-pi < phi < pi)
101  void fromPolar (T r, T phiRAD) {x = r * cos(phiRAD);
102  y = r * sin(phiRAD);}
103 
104  //! Calculate the barycentric coordinate uv of the point within a triangle ABC
106  {
107  // Formula from: https://en.wikipedia.org/wiki/Barycentric_coordinate_system
108  T xAC = A.x-C.x, xC = x-C.x, xCB = C.x-B.x;
109  T yAC = A.y-C.y, yBC = B.y-C.y, yC = y-C.y, yCA = C.y-A.y;
110 
111  T u = (yBC*xC + xCB*yC) / (yBC*xAC + xCB*yAC);
112  T v = (yCA*xC + xAC*yC) / (yBC*xAC + xCB*yAC);
113 
114  return SLVec2(u,v);
115  }
116 
117  //! Calculate the absolute to the vector v
118  T diff (const SLVec2& v) {return Utils::abs(x-v.x) +
119  Utils::abs(y-v.y);}
120  void setMin (const SLVec2& v) {if (v.x < x) x=v.x;
121  if (v.y < y) y=v.y;}
122  void setMax (const SLVec2& v) {if (v.x > x) x=v.x;
123  if (v.y > y) y=v.y;}
124  SLbool isZero () {return (x==0 && y==0);}
125  void print (const char* str=nullptr) {if (str) SLMATH_LOG("%s",str);
126  SLMATH_LOG("% 3.3f, % 3.3f",x, y);}
127 
128  //! Conversion to string
129  SLstring toString (SLstring delimiter = ", ", int decimals = 2)
130  { return Utils::toString(x,decimals) + delimiter +
131  Utils::toString(y,decimals);
132  }
133 
134  static
136 };
137 //-----------------------------------------------------------------------------
138 template<class T> SLVec2<T> SLVec2<T>::ZERO = SLVec2<T>(0,0);
139 //-----------------------------------------------------------------------------
142 
143 typedef vector<SLVec2f> SLVVec2f;
144 
145 #ifdef SL_HAS_DOUBLE
146 typedef SLVec2<SLdouble> SLVec2d;
147 typedef vector<SLVec2d> SLVVec2d;
148 #endif
149 //-----------------------------------------------------------------------------
150 #endif
bool SLbool
Definition: SL.h:175
string SLstring
Definition: SL.h:158
#define SLMATH_LOG(...)
Definition: SLMath.h:70
SLScene * s
Definition: SLScene.h:31
vector< SLVec2f > SLVVec2f
Definition: SLVec2.h:143
SLVec2< SLint > SLVec2i
Definition: SLVec2.h:140
SLVec2< SLfloat > SLVec2f
Definition: SLVec2.h:141
The SLScene class represents the top level instance holding the scene structure.
Definition: SLScene.h:47
2D vector template class for standard 2D vector algebra.
Definition: SLVec2.h:27
void clampMinMax(const T min, const T max)
Definition: SLVec2.h:92
SLVec2 operator/(const T s) const
Definition: SLVec2.h:63
T comp[2]
Definition: SLVec2.h:31
SLVec2 & normalize()
Definition: SLVec2.h:89
SLbool operator<(const SLVec2 &v) const
Definition: SLVec2.h:50
T operator*(const SLVec2 &v) const
Definition: SLVec2.h:61
void sub(const SLVec2 &a, const SLVec2 &b)
Definition: SLVec2.h:83
SLVec2 & operator-=(const SLVec2 &v)
Definition: SLVec2.h:71
static SLVec2 ZERO
Definition: SLVec2.h:135
T dot(const SLVec2 &v)
Definition: SLVec2.h:86
SLbool operator>=(const SLVec2 &v) const
Definition: SLVec2.h:49
T lengthSqr() const
Definition: SLVec2.h:88
SLbool operator<=(const SLVec2 &v) const
Definition: SLVec2.h:48
void setMax(const SLVec2 &v)
Definition: SLVec2.h:122
SLVec2 & operator/=(const T s)
Definition: SLVec2.h:73
SLstring toString(SLstring delimiter=", ", int decimals=2)
Conversion to string.
Definition: SLVec2.h:129
SLVec2 barycentricCoords(SLVec2 A, SLVec2 B, SLVec2 C)
Calculate the barycentric coordinate uv of the point within a triangle ABC.
Definition: SLVec2.h:105
SLbool operator>(const SLVec2 &v) const
Definition: SLVec2.h:51
void print(const char *str=nullptr)
Definition: SLVec2.h:125
SLbool isZero()
Definition: SLVec2.h:124
SLVec2 & operator=(const SLVec2 &v)
Definition: SLVec2.h:69
SLVec2(const SLVec2 &v)
Definition: SLVec2.h:38
SLbool operator==(const SLVec2 &v) const
Definition: SLVec2.h:46
void setMin(const SLVec2 &v)
Definition: SLVec2.h:120
void toPolar(T &r, T &phiRAD)
Calculates polar coords with radius & angle phi in radians (-pi < phi < pi)
Definition: SLVec2.h:97
T y
Definition: SLVec2.h:30
T diff(const SLVec2 &v)
Calculate the absolute to the vector v.
Definition: SLVec2.h:118
void add(const SLVec2 &a, const SLVec2 &b)
Definition: SLVec2.h:81
void set(const T v[2])
Definition: SLVec2.h:42
SLbool operator!=(const SLVec2 &v) const
Definition: SLVec2.h:47
SLVec2(const T v[2])
Definition: SLVec2.h:37
SLVec2()
Definition: SLVec2.h:34
SLVec2 & operator*=(const T s)
Definition: SLVec2.h:72
SLVec2 & operator+=(const SLVec2 &v)
Definition: SLVec2.h:70
T x
Definition: SLVec2.h:30
SLVec2 operator-() const
Definition: SLVec2.h:60
void set(const SLVec2 &v)
Definition: SLVec2.h:43
void set(const T X, const T Y)
Definition: SLVec2.h:40
friend std::ostream & operator<<(std::ostream &output, const SLVec2 &v)
Definition: SLVec2.h:77
void fromPolar(T r, T phiRAD)
Calculates the vector from polar coords r & phi in radians (-pi < phi < pi)
Definition: SLVec2.h:101
SLVec2 & operator&=(const SLVec2 &v)
Definition: SLVec2.h:74
SLVec2 operator+(const SLVec2 &v) const
Definition: SLVec2.h:58
SLVec2 operator&(const SLVec2 &v) const
Definition: SLVec2.h:64
T length() const
Definition: SLVec2.h:87
SLVec2(const T X, const T Y)
Definition: SLVec2.h:35
void scale(const T s)
Definition: SLVec2.h:85
T abs(T a)
Definition: Utils.h:249
string toString(float f, int roundedDecimals)
Returns a string from a float with max. one trailing zero.
Definition: Utils.cpp:92