The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

82 lines
4.1KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2013 - Raw Material Software Ltd.
  5. Permission is granted to use this software under the terms of either:
  6. a) the GPL v2 (or any later version)
  7. b) the Affero GPL v3
  8. Details of these licenses can be found at: www.gnu.org/licenses
  9. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  11. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  12. ------------------------------------------------------------------------------
  13. To release a closed-source product which uses JUCE, commercial licenses are
  14. available: visit www.juce.com for more information.
  15. ==============================================================================
  16. */
  17. #ifndef JUCE_VECTOR3D_H_INCLUDED
  18. #define JUCE_VECTOR3D_H_INCLUDED
  19. //==============================================================================
  20. /**
  21. A three-coordinate vector.
  22. */
  23. template <typename Type>
  24. class Vector3D
  25. {
  26. public:
  27. Vector3D() noexcept : x(), y(), z() {}
  28. Vector3D (const Type& xValue, const Type& yValue, const Type& zValue) noexcept : x (xValue), y (yValue), z (zValue) {}
  29. Vector3D (const Vector3D& other) noexcept : x (other.x), y (other.y), z (other.z) {}
  30. Vector3D& operator= (const Vector3D& other) noexcept { x = other.x; y = other.y; z = other.z; return *this; }
  31. /** Returns a vector that lies along the X axis. */
  32. static Vector3D xAxis() noexcept { return Vector3D ((Type) 1, 0, 0); }
  33. /** Returns a vector that lies along the Y axis. */
  34. static Vector3D yAxis() noexcept { return Vector3D (0, (Type) 1, 0); }
  35. /** Returns a vector that lies along the Z axis. */
  36. static Vector3D zAxis() noexcept { return Vector3D (0, 0, (Type) 1); }
  37. Vector3D& operator+= (const Vector3D& other) noexcept { x += other.x; y += other.y; z += other.z; return *this; }
  38. Vector3D& operator-= (const Vector3D& other) noexcept { x -= other.x; y -= other.y; z -= other.z; return *this; }
  39. Vector3D& operator*= (const Type& scaleFactor) noexcept { x *= scaleFactor; y *= scaleFactor; z *= scaleFactor; return *this; }
  40. Vector3D& operator/= (const Type& scaleFactor) noexcept { x /= scaleFactor; y /= scaleFactor; z /= scaleFactor; return *this; }
  41. Vector3D operator+ (const Vector3D& other) const noexcept { return Vector3D (x + other.x, y + other.y, z + other.z); }
  42. Vector3D operator- (const Vector3D& other) const noexcept { return Vector3D (x - other.x, y - other.y, z - other.z); }
  43. Vector3D operator* (const Type& scaleFactor) const noexcept { return Vector3D (x * scaleFactor, y * scaleFactor, z * scaleFactor); }
  44. Vector3D operator/ (const Type& scaleFactor) const noexcept { return Vector3D (x / scaleFactor, y / scaleFactor, z / scaleFactor); }
  45. Vector3D operator-() const noexcept { return Vector3D (-x, -y, -z); }
  46. /** Returns the dot-product of these two vectors. */
  47. Type operator* (const Vector3D& other) const noexcept { return x * other.x + y * other.y + z * other.z; }
  48. /** Returns the cross-product of these two vectors. */
  49. Vector3D operator^ (const Vector3D& other) const noexcept { return Vector3D (y * other.z - z * other.y, z * other.x - x * other.z, x * other.y - y * other.x); }
  50. Type length() const noexcept { return std::sqrt (lengthSquared()); }
  51. Type lengthSquared() const noexcept { return x * x + y * y + z * z; }
  52. Vector3D normalised() const noexcept { return *this / length(); }
  53. /** Returns true if the vector is practically equal to the origin. */
  54. bool lengthIsBelowEpsilon() const noexcept
  55. {
  56. const Type epsilon (std::numeric_limits<Type>::epsilon());
  57. return ! (x < -epsilon || x > epsilon || y < -epsilon || y > epsilon || z < -epsilon || z > epsilon);
  58. }
  59. Type x, y, z;
  60. };
  61. #endif // JUCE_VECTOR3D_H_INCLUDED