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.

78 lines
4.1KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2015 - ROLI 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. #pragma once
  18. //==============================================================================
  19. /**
  20. A three-coordinate vector.
  21. */
  22. template <typename Type>
  23. class Vector3D
  24. {
  25. public:
  26. Vector3D() noexcept : x(), y(), z() {}
  27. Vector3D (Type xValue, Type yValue, Type zValue) noexcept : x (xValue), y (yValue), z (zValue) {}
  28. Vector3D (const Vector3D& other) noexcept : x (other.x), y (other.y), z (other.z) {}
  29. Vector3D& operator= (Vector3D other) noexcept { x = other.x; y = other.y; z = other.z; return *this; }
  30. /** Returns a vector that lies along the X axis. */
  31. static Vector3D xAxis() noexcept { return Vector3D ((Type) 1, 0, 0); }
  32. /** Returns a vector that lies along the Y axis. */
  33. static Vector3D yAxis() noexcept { return Vector3D (0, (Type) 1, 0); }
  34. /** Returns a vector that lies along the Z axis. */
  35. static Vector3D zAxis() noexcept { return Vector3D (0, 0, (Type) 1); }
  36. Vector3D& operator+= (Vector3D other) noexcept { x += other.x; y += other.y; z += other.z; return *this; }
  37. Vector3D& operator-= (Vector3D other) noexcept { x -= other.x; y -= other.y; z -= other.z; return *this; }
  38. Vector3D& operator*= (Type scaleFactor) noexcept { x *= scaleFactor; y *= scaleFactor; z *= scaleFactor; return *this; }
  39. Vector3D& operator/= (Type scaleFactor) noexcept { x /= scaleFactor; y /= scaleFactor; z /= scaleFactor; return *this; }
  40. Vector3D operator+ (Vector3D other) const noexcept { return Vector3D (x + other.x, y + other.y, z + other.z); }
  41. Vector3D operator- (Vector3D other) const noexcept { return Vector3D (x - other.x, y - other.y, z - other.z); }
  42. Vector3D operator* (Type scaleFactor) const noexcept { return Vector3D (x * scaleFactor, y * scaleFactor, z * scaleFactor); }
  43. Vector3D operator/ (Type scaleFactor) const noexcept { return Vector3D (x / scaleFactor, y / scaleFactor, z / scaleFactor); }
  44. Vector3D operator-() const noexcept { return Vector3D (-x, -y, -z); }
  45. /** Returns the dot-product of these two vectors. */
  46. Type operator* (Vector3D other) const noexcept { return x * other.x + y * other.y + z * other.z; }
  47. /** Returns the cross-product of these two vectors. */
  48. Vector3D operator^ (Vector3D other) const noexcept { return Vector3D (y * other.z - z * other.y, z * other.x - x * other.z, x * other.y - y * other.x); }
  49. Type length() const noexcept { return std::sqrt (lengthSquared()); }
  50. Type lengthSquared() const noexcept { return x * x + y * y + z * z; }
  51. Vector3D normalised() const noexcept { return *this / length(); }
  52. /** Returns true if the vector is practically equal to the origin. */
  53. bool lengthIsBelowEpsilon() const noexcept
  54. {
  55. const Type epsilon (std::numeric_limits<Type>::epsilon());
  56. return ! (x < -epsilon || x > epsilon || y < -epsilon || y > epsilon || z < -epsilon || z > epsilon);
  57. }
  58. Type x, y, z;
  59. };