ShishGL  1.0
A simple modular cross-platform graphics library
Vector3.hpp
Go to the documentation of this file.
1 /*============================================================================*/
2 #ifndef SHISHGL_VECTOR3_HPP
3 #define SHISHGL_VECTOR3_HPP
4 /*============================================================================*/
5 
6 #include <cmath>
7 #include <ostream>
8 
9 /*============================================================================*/
10 
11 namespace Sh {
12 
13  template <typename T>
14  struct Vector3 {
15  T x, y, z;
16 
17  /* normalize */
19 
20  /* projection */
21  Vector3<T> operator|(const Vector3<T>& right) const;
23 
24  /* reflection relative to the right vector along
25  * the guideline connecting the ends */
26  Vector3<T> operator%(const Vector3<T>& right) const;
28 
29  Vector3<T> operator+(const Vector3<T>& right) const;
31 
33 
34  Vector3<T> operator-(const Vector3<T>& right) const;
36 
37  Vector3<T> operator*(const T& mul) const;
38  Vector3<T>& operator*=(const T& mul);
39 
40  /* dot multiplication */
41  T operator^(const Vector3<T>& right) const;
42  };
43 
44  /*------------------------------------------------------------------------*/
45 
46  double cos(const Vector3<double>& v1, const Vector3<double>& v2);
47 
48  /*------------------------------------------------------------------------*/
49 
50  template <typename T>
51  std::ostream& operator<<(std::ostream& out, const Vector3<T>& vec) {
52  out << "(" << vec.x << "; " << vec.y << "; " << vec.z << ")";
53  return out;
54  }
55 
56  /*------------------------------------------------------------------------*/
57 
58  template <typename T>
60  return Vector3<T>{-x, -y, -z};
61  }
62 
63  /*------------------------------------------------------------------------*/
64 
65  template <typename T>
67  return *this;
68  }
69 
70  template <>
72 
73  /*------------------------------------------------------------------------*/
74 
75  template <typename T>
77  *this += (right - *this) * 2;
78  return *this;
79  }
80 
81  template <typename T>
83  return (Vector3<T>{*this} %= right);
84  }
85 
86  /*------------------------------------------------------------------------*/
87 
88  template <typename T>
90  *this = right * ((*this ^ right) / (right ^ right));
91  return *this;
92  }
93 
94  template <typename T>
96  return (Vector3<T>{*this} |= right);
97  }
98 
99  /*------------------------------------------------------------------------*/
100 
101  template <typename T>
103  x += right.x;
104  y += right.y;
105  z += right.z;
106  return *this;
107  }
108 
109  template <typename T>
111  return (Vector3<T>{*this} += right);
112  }
113 
114  /*------------------------------------------------------------------------*/
115 
116  template <typename T>
118  return (&this += (-right));
119  }
120 
121  template <typename T>
123  return Vector3<T>{*this} + (-right);
124  }
125 
126  /*------------------------------------------------------------------------*/
127 
128  template <typename T>
130  x *= mul; y *= mul; z *= mul;
131  return *this;
132  }
133 
134  template <typename T>
135  Vector3<T> Vector3<T>::operator*(const T& mul) const {
136  return (Vector3<T>{*this} *= mul);
137  }
138 
139  /*------------------------------------------------------------------------*/
140 
141  template <typename T>
142  T Vector3<T>::operator^(const Vector3<T>& right) const {
143  return (x * right.x + y * right.y + z * right.z);
144  }
145 
146  /*------------------------------------------------------------------------*/
147 }
148 
149 /*============================================================================*/
150 #endif //SHISHGL_VECTOR3_HPP
151 /*============================================================================*/
Sh::Vector3::operator*=
Vector3< T > & operator*=(const T &mul)
Definition: Vector3.hpp:129
Sh::Vector3::operator|
Vector3< T > operator|(const Vector3< T > &right) const
Definition: Vector3.hpp:95
Sh::Vector3::z
T z
Definition: Vector3.hpp:15
Sh::Vector3::operator|=
Vector3< T > & operator|=(const Vector3< T > &right)
Definition: Vector3.hpp:89
Sh::Vector3::operator+=
Vector3< T > & operator+=(const Vector3< T > &right)
Definition: Vector3.hpp:102
Sh::Vector3
Definition: Vector3.hpp:14
Sh::Vector3::operator-
Vector3< T > operator-() const
Definition: Vector3.hpp:59
Sh::Vector3::operator^
T operator^(const Vector3< T > &right) const
Definition: Vector3.hpp:142
Sh
Definition: CoreApplication.hpp:10
Sh::operator<<
std::ostream & operator<<(std::ostream &out, const Vector3< T > &vec)
Definition: Vector3.hpp:51
Sh::Vector3::operator-=
Vector3< T > & operator-=(const Vector3< T > &right)
Definition: Vector3.hpp:117
Sh::Vector3::operator-
Vector3< T > operator-(const Vector3< T > &right) const
Definition: Vector3.hpp:122
Sh::Vector3::operator!
Vector3< T > operator!() const
Definition: Vector3.hpp:66
Sh::Vector3::operator%=
Vector3< T > & operator%=(const Vector3< T > &right)
Definition: Vector3.hpp:76
Sh::Vector3::y
T y
Definition: Vector3.hpp:15
Sh::Vector3::operator%
Vector3< T > operator%(const Vector3< T > &right) const
Definition: Vector3.hpp:82
Sh::cos
double cos(const Vector3< double > &v1, const Vector3< double > &v2)
Definition: Vector3.cpp:16
Sh::Vector3::operator*
Vector3< T > operator*(const T &mul) const
Definition: Vector3.hpp:135
Sh::Vector3::x
T x
Definition: Vector3.hpp:15
Sh::Vector3::operator+
Vector3< T > operator+(const Vector3< T > &right) const
Definition: Vector3.hpp:110