Vec3.h 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. // Copyright 2010 Dolphin Emulator Project
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. #pragma once
  4. #include <cmath>
  5. #include <cstdlib>
  6. class Vec3
  7. {
  8. public:
  9. float x, y, z;
  10. Vec3() = default;
  11. explicit Vec3(float f) { x = y = z = f; }
  12. explicit Vec3(const float* f)
  13. {
  14. x = f[0];
  15. y = f[1];
  16. z = f[2];
  17. }
  18. Vec3(const float _x, const float _y, const float _z)
  19. {
  20. x = _x;
  21. y = _y;
  22. z = _z;
  23. }
  24. void set(const float _x, const float _y, const float _z)
  25. {
  26. x = _x;
  27. y = _y;
  28. z = _z;
  29. }
  30. Vec3 operator+(const Vec3& other) const { return Vec3(x + other.x, y + other.y, z + other.z); }
  31. void operator+=(const Vec3& other)
  32. {
  33. x += other.x;
  34. y += other.y;
  35. z += other.z;
  36. }
  37. Vec3 operator-(const Vec3& v) const { return Vec3(x - v.x, y - v.y, z - v.z); }
  38. void operator-=(const Vec3& other)
  39. {
  40. x -= other.x;
  41. y -= other.y;
  42. z -= other.z;
  43. }
  44. Vec3 operator-() const { return Vec3(-x, -y, -z); }
  45. Vec3 operator*(const float f) const { return Vec3(x * f, y * f, z * f); }
  46. Vec3 operator/(const float f) const
  47. {
  48. float invf = (1.0f / f);
  49. return Vec3(x * invf, y * invf, z * invf);
  50. }
  51. void operator/=(const float f) { *this = *this / f; }
  52. float operator*(const Vec3& other) const { return (x * other.x) + (y * other.y) + (z * other.z); }
  53. void operator*=(const float f) { *this = *this * f; }
  54. Vec3 ScaledBy(const Vec3& other) const { return Vec3(x * other.x, y * other.y, z * other.z); }
  55. Vec3 operator%(const Vec3& v) const
  56. {
  57. return Vec3((y * v.z) - (z * v.y), (z * v.x) - (x * v.z), (x * v.y) - (y * v.x));
  58. }
  59. float Length2() const { return (x * x) + (y * y) + (z * z); }
  60. float Length() const { return sqrtf(Length2()); }
  61. float Distance2To(Vec3& other) { return (other - (*this)).Length2(); }
  62. Vec3 Normalized() const { return (*this) / Length(); }
  63. void Normalize() { (*this) /= Length(); }
  64. float& operator[](int i) { return *((&x) + i); }
  65. float operator[](const int i) const { return *((&x) + i); }
  66. bool operator==(const Vec3& other) const { return x == other.x && y == other.y && z == other.z; }
  67. void SetZero()
  68. {
  69. x = 0.0f;
  70. y = 0.0f;
  71. z = 0.0f;
  72. }
  73. };