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.

77 lines
3.8KB

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