Vector4.h 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  1. /*
  2. Copyright (c) 2013 Daniele Bartolini, Michele Rossi
  3. Copyright (c) 2012 Daniele Bartolini, Simone Boscaratto
  4. Permission is hereby granted, free of charge, to any person
  5. obtaining a copy of this software and associated documentation
  6. files (the "Software"), to deal in the Software without
  7. restriction, including without limitation the rights to use,
  8. copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. copies of the Software, and to permit persons to whom the
  10. Software is furnished to do so, subject to the following
  11. conditions:
  12. The above copyright notice and this permission notice shall be
  13. included in all copies or substantial portions of the Software.
  14. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  15. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  16. OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  17. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  18. HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  19. WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  21. OTHER DEALINGS IN THE SOFTWARE.
  22. */
  23. #pragma once
  24. #include "Assert.h"
  25. #include "Types.h"
  26. #include "MathUtils.h"
  27. namespace crown
  28. {
  29. /// 4D column vector.
  30. struct Vector4
  31. {
  32. public:
  33. float x, y, z, w;
  34. /// Does nothing for efficiency.
  35. Vector4();
  36. /// Initializes all the components to val
  37. Vector4(float val);
  38. /// Constructs from four components
  39. Vector4(float nx, float ny, float nz, float nw);
  40. /// Constructs from array
  41. Vector4(const float v[4]);
  42. Vector4(const Vector4& a);
  43. /// Random access by index
  44. float operator[](uint32_t i) const;
  45. /// Random access by index
  46. float& operator[](uint32_t i);
  47. Vector4 operator+(const Vector4& a) const;
  48. Vector4& operator+=(const Vector4& a);
  49. Vector4 operator-(const Vector4& a) const;
  50. Vector4& operator-=(const Vector4& a);
  51. Vector4 operator*(float k) const;
  52. Vector4& operator*=(float k);
  53. Vector4 operator/(float k) const;
  54. Vector4& operator/=(float k);
  55. /// Dot product
  56. float dot(const Vector4& a) const;
  57. /// For simmetry
  58. friend Vector4 operator*(float k, const Vector4& a);
  59. bool operator==(const Vector4& other) const;
  60. bool operator!=(const Vector4& other) const;
  61. /// Returns whether all the components of this vector are smaller than all of the @a other vector
  62. bool operator<(const Vector4& other) const;
  63. /// Returns whether all the components of this vector are greater than all of the @a other vector
  64. bool operator>(const Vector4& other) const;
  65. /// Returns the vector's length
  66. float length() const;
  67. /// Returns the vector's squared length
  68. float squared_length() const;
  69. /// Sets the vector's length
  70. void set_length(float len);
  71. /// Normalizes the vector
  72. Vector4& normalize();
  73. /// Returns the normalized vector
  74. Vector4 get_normalized() const;
  75. /// Negates the vector (i.e. builds the inverse)
  76. Vector4& negate();
  77. /// Negates the vector (i.e. builds the inverse)
  78. Vector4 operator-() const;
  79. /// Returns the distance
  80. float get_distance_to(const Vector4& a) const;
  81. /// Returns the angle in radians
  82. float get_angle_between(const Vector4& a) const;
  83. /// Sets all components to zero
  84. void zero();
  85. /// Returns the pointer to the vector's data
  86. float* to_float_ptr();
  87. /// Returns the pointer to the vector's data
  88. const float* to_float_ptr() const;
  89. static const Vector4 ZERO;
  90. static const Vector4 ONE;
  91. static const Vector4 XAXIS;
  92. static const Vector4 YAXIS;
  93. static const Vector4 ZAXIS;
  94. static const Vector4 WAXIS;
  95. };
  96. //-----------------------------------------------------------------------------
  97. inline Vector4::Vector4()
  98. {
  99. }
  100. //-----------------------------------------------------------------------------
  101. inline Vector4::Vector4(float val) : x(val), y(val), z(val), w(val)
  102. {
  103. }
  104. //-----------------------------------------------------------------------------
  105. inline Vector4::Vector4(float nx, float ny, float nz, float nw) : x(nx), y(ny), z(nz), w(nw)
  106. {
  107. }
  108. //-----------------------------------------------------------------------------
  109. inline Vector4::Vector4(const float a[4]) : x(a[0]), y(a[1]), z(a[2]), w(a[3])
  110. {
  111. }
  112. //-----------------------------------------------------------------------------
  113. inline Vector4::Vector4(const Vector4& a) : x(a.x), y(a.y), z(a.z), w(a.w)
  114. {
  115. }
  116. //-----------------------------------------------------------------------------
  117. inline float Vector4::operator[](uint32_t i) const
  118. {
  119. CE_ASSERT(i < 4, "Index must be < 4");
  120. return (&x)[i];
  121. }
  122. //-----------------------------------------------------------------------------
  123. inline float& Vector4::operator[](uint32_t i)
  124. {
  125. CE_ASSERT(i < 4, "Index must be < 4");
  126. return (&x)[i];
  127. }
  128. //-----------------------------------------------------------------------------
  129. inline Vector4 Vector4::operator+(const Vector4& a) const
  130. {
  131. return Vector4(x + a.x, y + a.y, z + a.z, w + a.w);
  132. }
  133. //-----------------------------------------------------------------------------
  134. inline Vector4& Vector4::operator+=(const Vector4& a)
  135. {
  136. x += a.x;
  137. y += a.y;
  138. z += a.z;
  139. w += a.w;
  140. return *this;
  141. }
  142. //-----------------------------------------------------------------------------
  143. inline Vector4 Vector4::operator-(const Vector4& a) const
  144. {
  145. return Vector4(x - a.x, y - a.y, z - a.z, w - a.w);
  146. }
  147. //-----------------------------------------------------------------------------
  148. inline Vector4& Vector4::operator-=(const Vector4& a)
  149. {
  150. x -= a.x;
  151. y -= a.y;
  152. z -= a.z;
  153. w -= a.w;
  154. return *this;
  155. }
  156. //-----------------------------------------------------------------------------
  157. inline Vector4 Vector4::operator*(float k) const
  158. {
  159. return Vector4(x * k, y * k, z * k, w * k);
  160. }
  161. //-----------------------------------------------------------------------------
  162. inline Vector4& Vector4::operator*=(float k)
  163. {
  164. x *= k;
  165. y *= k;
  166. z *= k;
  167. w *= k;
  168. return *this;
  169. }
  170. //-----------------------------------------------------------------------------
  171. inline Vector4 Vector4::operator/(float k) const
  172. {
  173. CE_ASSERT(k != (float)0.0, "Division by zero");
  174. float inv = (float)(1.0 / k);
  175. return Vector4(x * inv, y * inv, z * inv, w * inv);
  176. }
  177. //-----------------------------------------------------------------------------
  178. inline Vector4& Vector4::operator/=(float k)
  179. {
  180. CE_ASSERT(k != (float)0.0, "Division by zero");
  181. float inv = (float)(1.0 / k);
  182. x *= inv;
  183. y *= inv;
  184. z *= inv;
  185. w *= inv;
  186. return *this;
  187. }
  188. //-----------------------------------------------------------------------------
  189. inline float Vector4::dot(const Vector4& a) const
  190. {
  191. return x * a.x + y * a.y + z * a.z + w * a.w;
  192. }
  193. //-----------------------------------------------------------------------------
  194. inline Vector4 operator*(float k, const Vector4& a)
  195. {
  196. return a * k;
  197. }
  198. //-----------------------------------------------------------------------------
  199. inline bool Vector4::operator==(const Vector4& other) const
  200. {
  201. return math::equals(x, other.x) && math::equals(y, other.y) && math::equals(z, other.z) && math::equals(w, other.w);
  202. }
  203. //-----------------------------------------------------------------------------
  204. inline bool Vector4::operator!=(const Vector4& other) const
  205. {
  206. return !math::equals(x, other.x) || !math::equals(y, other.y) || !math::equals(z, other.z) || !math::equals(w, other.w);
  207. }
  208. //-----------------------------------------------------------------------------
  209. inline bool Vector4::operator<(const Vector4& other) const
  210. {
  211. return ((x < other.x) && (y < other.y) && (z < other.z) && (w < other.w));
  212. }
  213. //-----------------------------------------------------------------------------
  214. inline bool Vector4::operator>(const Vector4& other) const
  215. {
  216. return ((x > other.x) && (y > other.y) && (z > other.z) && (w > other.w));
  217. }
  218. //-----------------------------------------------------------------------------
  219. inline float Vector4::length() const
  220. {
  221. return math::sqrt(x * x + y * y + z * z + w * w);
  222. }
  223. //-----------------------------------------------------------------------------
  224. inline float Vector4::squared_length() const
  225. {
  226. return x * x + y * y + z * z + w * w;
  227. }
  228. //-----------------------------------------------------------------------------
  229. inline Vector4& Vector4::normalize()
  230. {
  231. float len = length();
  232. if (math::equals(len, (float)0.0))
  233. {
  234. return *this;
  235. }
  236. len = (float)(1.0 / len);
  237. x *= len;
  238. y *= len;
  239. z *= len;
  240. w *= len;
  241. return *this;
  242. }
  243. //-----------------------------------------------------------------------------
  244. inline Vector4 Vector4::get_normalized() const
  245. {
  246. Vector4 tmp(x, y, z, w);
  247. return tmp.normalize();
  248. }
  249. //-----------------------------------------------------------------------------
  250. inline Vector4& Vector4::negate()
  251. {
  252. x = -x;
  253. y = -y;
  254. z = -z;
  255. w = -w;
  256. return *this;
  257. }
  258. //-----------------------------------------------------------------------------
  259. inline Vector4 Vector4::operator-() const
  260. {
  261. return Vector4(-x, -y, -z, -w);
  262. }
  263. //-----------------------------------------------------------------------------
  264. inline float Vector4::get_distance_to(const Vector4& a) const
  265. {
  266. return (*this - a).length();
  267. }
  268. //-----------------------------------------------------------------------------
  269. inline float Vector4::get_angle_between(const Vector4& a) const
  270. {
  271. return math::acos(this->dot(a) / (this->length() * a.length()));
  272. }
  273. //-----------------------------------------------------------------------------
  274. inline void Vector4::zero()
  275. {
  276. x = 0.0;
  277. y = 0.0;
  278. z = 0.0;
  279. w = 0.0;
  280. }
  281. //-----------------------------------------------------------------------------
  282. inline float* Vector4::to_float_ptr()
  283. {
  284. return &x;
  285. }
  286. //-----------------------------------------------------------------------------
  287. inline const float* Vector4::to_float_ptr() const
  288. {
  289. return &x;
  290. }
  291. } // namespace crown