Control MIDI in Ableton Live with a MYO armband
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.

Vector3.hpp 2.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. // Copyright (C) 2013-2014 Thalmic Labs Inc.
  2. // Distributed under the Myo SDK license agreement. See LICENSE.txt for details.
  3. #pragma once
  4. #define _USE_MATH_DEFINES
  5. #include <cmath>
  6. namespace myo {
  7. /// A vector of three components.
  8. /// This type provides very basic functionality to store a three dimensional vector that's sufficient to retrieve
  9. /// the data to be placed in a full featured vector type. A few common vector operations, such as dot product and
  10. /// cross product, are also provided.
  11. template<typename T>
  12. class Vector3 {
  13. public:
  14. /// Construct a vector of all zeroes.
  15. Vector3()
  16. {
  17. _data[0] = 0;
  18. _data[1] = 0;
  19. _data[2] = 0;
  20. }
  21. /// Construct a vector with the three provided components.
  22. Vector3(T x, T y, T z)
  23. {
  24. _data[0] = x;
  25. _data[1] = y;
  26. _data[2] = z;
  27. }
  28. /// Construct a vector with the same components as \a other.
  29. Vector3(const Vector3& other)
  30. {
  31. *this = other;
  32. }
  33. /// Set the components of this vector to be the same as \a other.
  34. Vector3& operator=(const Vector3& other)
  35. {
  36. _data[0] = other._data[0];
  37. _data[1] = other._data[1];
  38. _data[2] = other._data[2];
  39. return *this;
  40. }
  41. /// Return a copy of the component of this vector at \a index, which should be 0, 1, or 2.
  42. T operator[](unsigned int index) const
  43. {
  44. return _data[index];
  45. }
  46. /// Return the x-component of this vector.
  47. T x() const { return _data[0]; }
  48. /// Return the y-component of this vector.
  49. T y() const { return _data[1]; }
  50. /// Return the z-component of this vector.
  51. T z() const { return _data[2]; }
  52. /// Return the magnitude of this vector.
  53. T magnitude() const
  54. {
  55. return std::sqrt(x() * x() + y() * y() + z() * z());
  56. }
  57. /// Return a normalized copy of this vector.
  58. Vector3 normalized() const
  59. {
  60. T norm = magnitude();
  61. return Vector3(x() / norm, y() / norm, z() / norm);
  62. }
  63. /// Return the dot product of this vector and \a rhs.
  64. T dot(const Vector3& rhs) const
  65. {
  66. return x() * rhs.x() + y() * rhs.y() + z() * rhs.z();
  67. }
  68. /// Return the cross product of this vector and \a rhs.
  69. Vector3 cross(const Vector3& rhs) const
  70. {
  71. return Vector3(
  72. y() * rhs.z() - z() * rhs.y(),
  73. z() * rhs.x() - x() * rhs.z(),
  74. x() * rhs.y() - y() * rhs.x()
  75. );
  76. }
  77. /// Return the angle between this vector and \a rhs, in radians.
  78. T angleTo(const Vector3& rhs) const
  79. {
  80. return std::acos(dot(rhs) / (magnitude() * rhs.magnitude()));
  81. }
  82. private:
  83. T _data[3];
  84. };
  85. } // namespace myo