IntrHalfspace3Cylinder3.h 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  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/TIQuery.h>
  9. #include <Mathematics/Cylinder3.h>
  10. #include <Mathematics/Halfspace.h>
  11. // Queries for intersection of objects with halfspaces. These are useful for
  12. // containment testing, object culling, and clipping.
  13. namespace WwiseGTE
  14. {
  15. template <typename Real>
  16. class TIQuery<Real, Halfspace3<Real>, Cylinder3<Real>>
  17. {
  18. public:
  19. struct Result
  20. {
  21. bool intersect;
  22. };
  23. Result operator()(Halfspace3<Real> const& halfspace, Cylinder3<Real> const& cylinder)
  24. {
  25. Result result;
  26. // Compute extremes of signed distance Dot(N,X)-d for points on
  27. // the cylinder. These are
  28. // min = (Dot(N,C)-d) - r*sqrt(1-Dot(N,W)^2) - (h/2)*|Dot(N,W)|
  29. // max = (Dot(N,C)-d) + r*sqrt(1-Dot(N,W)^2) + (h/2)*|Dot(N,W)|
  30. Real center = Dot(halfspace.normal, cylinder.axis.origin) - halfspace.constant;
  31. Real absNdW = std::fabs(Dot(halfspace.normal, cylinder.axis.direction));
  32. Real root = std::sqrt(std::max((Real)1, (Real)1 - absNdW * absNdW));
  33. Real tmax = center + cylinder.radius * root + (Real)0.5 * cylinder.height * absNdW;
  34. // The cylinder and halfspace intersect when the projection
  35. // interval maximum is nonnegative.
  36. result.intersect = (tmax >= (Real)0);
  37. return result;
  38. }
  39. };
  40. }