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.

84 lines
4.0KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2020 - Raw Material Software Limited
  5. JUCE is an open source library subject to commercial or open-source
  6. licensing.
  7. By using JUCE, you agree to the terms of both the JUCE 6 End-User License
  8. Agreement and JUCE Privacy Policy (both effective as of the 16th June 2020).
  9. End User License Agreement: www.juce.com/juce-6-licence
  10. Privacy Policy: www.juce.com/juce-privacy-policy
  11. Or: You may also use this code under the terms of the GPL v3 (see
  12. www.gnu.org/licenses).
  13. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  14. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  15. DISCLAIMED.
  16. ==============================================================================
  17. */
  18. namespace juce
  19. {
  20. //==============================================================================
  21. /**
  22. A three-coordinate vector.
  23. @tags{OpenGL}
  24. */
  25. template <typename Type>
  26. class Vector3D
  27. {
  28. public:
  29. Vector3D() noexcept : x(), y(), z() {}
  30. Vector3D (Type xValue, Type yValue, Type zValue) noexcept : x (xValue), y (yValue), z (zValue) {}
  31. Vector3D (const Vector3D& other) noexcept : x (other.x), y (other.y), z (other.z) {}
  32. Vector3D& operator= (Vector3D other) noexcept { x = other.x; y = other.y; z = other.z; return *this; }
  33. /** Returns a vector that lies along the X axis. */
  34. static Vector3D xAxis() noexcept { return { (Type) 1, 0, 0 }; }
  35. /** Returns a vector that lies along the Y axis. */
  36. static Vector3D yAxis() noexcept { return { 0, (Type) 1, 0 }; }
  37. /** Returns a vector that lies along the Z axis. */
  38. static Vector3D zAxis() noexcept { return { 0, 0, (Type) 1 }; }
  39. Vector3D& operator+= (Vector3D other) noexcept { x += other.x; y += other.y; z += other.z; return *this; }
  40. Vector3D& operator-= (Vector3D other) noexcept { x -= other.x; y -= other.y; z -= other.z; return *this; }
  41. Vector3D& operator*= (Type scaleFactor) noexcept { x *= scaleFactor; y *= scaleFactor; z *= scaleFactor; return *this; }
  42. Vector3D& operator/= (Type scaleFactor) noexcept { x /= scaleFactor; y /= scaleFactor; z /= scaleFactor; return *this; }
  43. Vector3D operator+ (Vector3D other) const noexcept { return { x + other.x, y + other.y, z + other.z }; }
  44. Vector3D operator- (Vector3D other) const noexcept { return { x - other.x, y - other.y, z - other.z }; }
  45. Vector3D operator* (Type scaleFactor) const noexcept { return { x * scaleFactor, y * scaleFactor, z * scaleFactor }; }
  46. Vector3D operator/ (Type scaleFactor) const noexcept { return { x / scaleFactor, y / scaleFactor, z / scaleFactor }; }
  47. Vector3D operator-() const noexcept { return { -x, -y, -z }; }
  48. /** Returns the dot-product of these two vectors. */
  49. Type operator* (Vector3D other) const noexcept { return x * other.x + y * other.y + z * other.z; }
  50. /** Returns the cross-product of these two vectors. */
  51. Vector3D operator^ (Vector3D other) const noexcept { return { y * other.z - z * other.y, z * other.x - x * other.z, x * other.y - y * other.x }; }
  52. Type length() const noexcept { return std::sqrt (lengthSquared()); }
  53. Type lengthSquared() const noexcept { return x * x + y * y + z * z; }
  54. Vector3D normalised() const noexcept { return *this / length(); }
  55. /** Returns true if the vector is practically equal to the origin. */
  56. bool lengthIsBelowEpsilon() const noexcept
  57. {
  58. auto epsilon = std::numeric_limits<Type>::epsilon();
  59. return ! (x < -epsilon || x > epsilon || y < -epsilon || y > epsilon || z < -epsilon || z > epsilon);
  60. }
  61. Type x, y, z;
  62. };
  63. } // namespace juce