Segment.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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/Vector.h>
  9. // The segment is represented by (1-t)*P0 + t*P1, where P0 and P1 are the
  10. // endpoints of the segment and 0 <= t <= 1. Some algorithms prefer a
  11. // centered representation that is similar to how oriented bounding boxes are
  12. // defined. This representation is C + s*D, where C = (P0 + P1)/2 is the
  13. // center of the segment, D = (P1 - P0)/|P1 - P0| is a unit-length direction
  14. // vector for the segment, and |t| <= e. The value e = |P1 - P0|/2 is the
  15. // extent (or radius or half-length) of the segment.
  16. namespace WwiseGTE
  17. {
  18. template <int N, typename Real>
  19. class Segment
  20. {
  21. public:
  22. // Construction and destruction. The default constructor sets p0 to
  23. // (-1,0,...,0) and p1 to (1,0,...,0). NOTE: If you set p0 and p1;
  24. // compute C, D, and e; and then recompute q0 = C-e*D and q1 = C+e*D,
  25. // numerical round-off errors can lead to q0 not exactly equal to p0
  26. // and q1 not exactly equal to p1.
  27. Segment()
  28. {
  29. p[1].MakeUnit(0);
  30. p[0] = -p[1];
  31. }
  32. Segment(Vector<N, Real> const& p0, Vector<N, Real> const& p1)
  33. :
  34. p{ p0, p1 }
  35. {
  36. }
  37. Segment(std::array<Vector<N, Real>, 2> const& inP)
  38. :
  39. p(inP)
  40. {
  41. }
  42. Segment(Vector<N, Real> const& center, Vector<N, Real> const& direction, Real extent)
  43. {
  44. SetCenteredForm(center, direction, extent);
  45. }
  46. // Manipulation via the centered form.
  47. void SetCenteredForm(Vector<N, Real> const& center,
  48. Vector<N, Real> const& direction, Real extent)
  49. {
  50. p[0] = center - extent * direction;
  51. p[1] = center + extent * direction;
  52. }
  53. void GetCenteredForm(Vector<N, Real>& center,
  54. Vector<N, Real>& direction, Real& extent) const
  55. {
  56. center = (Real)0.5 * (p[0] + p[1]);
  57. direction = p[1] - p[0];
  58. extent = (Real)0.5 * Normalize(direction);
  59. }
  60. // Public member access.
  61. std::array<Vector<N, Real>, 2> p;
  62. public:
  63. // Comparisons to support sorted containers.
  64. bool operator==(Segment const& segment) const
  65. {
  66. return p == segment.p;
  67. }
  68. bool operator!=(Segment const& segment) const
  69. {
  70. return p != segment.p;
  71. }
  72. bool operator< (Segment const& segment) const
  73. {
  74. return p < segment.p;
  75. }
  76. bool operator<=(Segment const& segment) const
  77. {
  78. return p <= segment.p;
  79. }
  80. bool operator> (Segment const& segment) const
  81. {
  82. return p > segment.p;
  83. }
  84. bool operator>=(Segment const& segment) const
  85. {
  86. return p >= segment.p;
  87. }
  88. };
  89. // Template aliases for convenience.
  90. template <typename Real>
  91. using Segment2 = Segment<2, Real>;
  92. template <typename Real>
  93. using Segment3 = Segment<3, Real>;
  94. }