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.

83 lines
4.3KB

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