Sector2.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. // David Eberly, Geometric Tools, Redmond WA 98052
  2. // Copyright (c) 1998-2020
  3. // Distributed under the Boost Software License, Version 1.0.
  4. // https://www.boost.org/LICENSE_1_0.txt
  5. // https://www.geometrictools.com/License/Boost/LICENSE_1_0.txt
  6. // Version: 4.0.2019.08.13
  7. #pragma once
  8. #include <Mathematics/Vector2.h>
  9. // A solid sector is the intersection of a disk and a 2D cone. The disk
  10. // has center C, radius R, and contains points X for which |X-C| <= R. The
  11. // 2D cone has vertex C, unit-length axis direction D, angle A in (0,pi)
  12. // measured from D, and contains points X for which
  13. // Dot(D,(X-C)/|X-C|) >= cos(A). Sector points X satisfy both inequality
  14. // constraints.
  15. namespace WwiseGTE
  16. {
  17. template <typename Real>
  18. class Sector2
  19. {
  20. public:
  21. // Construction and destruction. The default constructor sets the
  22. // vertex to (0,0), radius to 1, axis direction to (1,0), and angle
  23. // to pi, all of which define a disk.
  24. Sector2()
  25. :
  26. vertex(Vector2<Real>::Zero()),
  27. radius((Real)1),
  28. direction(Vector2<Real>::Unit(0)),
  29. angle((Real)GTE_C_PI),
  30. cosAngle((Real)-1),
  31. sinAngle((Real)0)
  32. {
  33. }
  34. Sector2(Vector2<Real> const& inVertex, Real inRadius,
  35. Vector2<Real> const& inDirection, Real inAngle)
  36. :
  37. vertex(inVertex),
  38. radius(inRadius),
  39. direction(inDirection)
  40. {
  41. SetAngle(inAngle);
  42. }
  43. // Set the angle and cos(angle) simultaneously.
  44. void SetAngle(Real inAngle)
  45. {
  46. angle = inAngle;
  47. cosAngle = std::cos(angle);
  48. sinAngle = std::sin(angle);
  49. }
  50. // Test whether P is in the sector.
  51. bool Contains(Vector2<Real> const& p) const
  52. {
  53. Vector2<Real> diff = p - vertex;
  54. Real length = Length(diff);
  55. return length <= radius && Dot(direction, diff) >= length * cosAngle;
  56. }
  57. // The cosine and sine of the angle are used in queries, so all o
  58. // angle, cos(angle), and sin(angle) are stored. If you set 'angle'
  59. // via the public members, you must set all to be consistent. You
  60. // can also call SetAngle(...) to ensure consistency.
  61. Vector2<Real> vertex;
  62. Real radius;
  63. Vector2<Real> direction;
  64. Real angle, cosAngle, sinAngle;
  65. public:
  66. // Comparisons to support sorted containers.
  67. bool operator==(Sector2 const& sector) const
  68. {
  69. return vertex == sector.vertex && radius == sector.radius
  70. && direction == sector.direction && angle == sector.angle;
  71. }
  72. bool operator!=(Sector2 const& sector) const
  73. {
  74. return !operator==(sector);
  75. }
  76. bool operator< (Sector2 const& sector) const
  77. {
  78. if (vertex < sector.vertex)
  79. {
  80. return true;
  81. }
  82. if (vertex > sector.vertex)
  83. {
  84. return false;
  85. }
  86. if (radius < sector.radius)
  87. {
  88. return true;
  89. }
  90. if (radius > sector.radius)
  91. {
  92. return false;
  93. }
  94. if (direction < sector.direction)
  95. {
  96. return true;
  97. }
  98. if (direction > sector.direction)
  99. {
  100. return false;
  101. }
  102. return angle < sector.angle;
  103. }
  104. bool operator<=(Sector2 const& sector) const
  105. {
  106. return !sector.operator<(*this);
  107. }
  108. bool operator> (Sector2 const& sector) const
  109. {
  110. return sector.operator<(*this);
  111. }
  112. bool operator>=(Sector2 const& sector) const
  113. {
  114. return !operator<(sector);
  115. }
  116. };
  117. }