IntrOrientedBox3Cone3.h 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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/Cone.h>
  9. #include <Mathematics/OrientedBox.h>
  10. #include <Mathematics/IntrAlignedBox3Cone3.h>
  11. // Test for intersection of a box and a cone. The cone can be infinite
  12. // 0 <= minHeight < maxHeight = std::numeric_limits<Real>::max()
  13. // or finite (cone frustum)
  14. // 0 <= minHeight < maxHeight < std::numeric_limits<Real>::max().
  15. // The algorithm is described in
  16. // https://www.geometrictools.com/Documentation/IntersectionBoxCone.pdf
  17. // and reports an intersection only when th intersection set has positive
  18. // volume. For example, let the box be outside the cone. If the box is
  19. // below the minHeight plane at the cone vertex and just touches the cone
  20. // vertex, no intersection is reported. If the box is above the maxHeight
  21. // plane and just touches the disk capping the cone, either at a single
  22. // point, a line segment of points or a polygon of points, no intersection
  23. // is reported.
  24. // TODO: These queries were designed when an infinite cone was defined
  25. // by choosing maxHeight of std::numeric_limits<Real>::max(). The Cone<N,Real>
  26. // class has been redesigned not to use std::numeric_limits to allow for
  27. // arithmetic systems that do not have representations for infinities
  28. // (such as BSNumber and BSRational). The intersection queries need to be
  29. // rewritten for the new class design. FOR NOW, the queries will work with
  30. // float/double when you create a cone using the cone-frustum constructor
  31. // Cone(ray, angle, minHeight, std::numeric_limits<Real>::max()).
  32. namespace WwiseGTE
  33. {
  34. template <typename Real>
  35. class TIQuery<Real, OrientedBox<3, Real>, Cone<3, Real>>
  36. :
  37. public TIQuery<Real, AlignedBox<3, Real>, Cone<3, Real>>
  38. {
  39. public:
  40. struct Result
  41. :
  42. public TIQuery<Real, AlignedBox<3, Real>, Cone<3, Real>>::Result
  43. {
  44. // No additional information to compute.
  45. };
  46. Result operator()(OrientedBox<3, Real> const& box, Cone<3, Real> const& cone)
  47. {
  48. // Transform the cone and box so that the cone vertex is at the
  49. // origin and the box is axis aligned. This allows us to call the
  50. // base class operator()(...).
  51. Vector<3, Real> diff = box.center - cone.ray.origin;
  52. Vector<3, Real> xfrmBoxCenter
  53. {
  54. Dot(box.axis[0], diff),
  55. Dot(box.axis[1], diff),
  56. Dot(box.axis[2], diff)
  57. };
  58. AlignedBox<3, Real> xfrmBox;
  59. xfrmBox.min = xfrmBoxCenter - box.extent;
  60. xfrmBox.max = xfrmBoxCenter + box.extent;
  61. Cone<3, Real> xfrmCone = cone;
  62. for (int i = 0; i < 3; ++i)
  63. {
  64. xfrmCone.ray.origin[i] = (Real)0;
  65. xfrmCone.ray.direction[i] = Dot(box.axis[i], cone.ray.direction);
  66. }
  67. // Test for intersection between the aligned box and the cone.
  68. auto bcResult = TIQuery<Real, AlignedBox<3, Real>, Cone<3, Real>>::operator()(xfrmBox, xfrmCone);
  69. Result result;
  70. result.intersect = bcResult.intersect;
  71. return result;
  72. }
  73. };
  74. // Template alias for convenience.
  75. template <typename Real>
  76. using TIOrientedBox3Cone3 = TIQuery<Real, OrientedBox<3, Real>, Cone<3, Real>>;
  77. }