ImathVecAlgo.h 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. //
  2. // SPDX-License-Identifier: BSD-3-Clause
  3. // Copyright Contributors to the OpenEXR Project.
  4. //
  5. //
  6. // Algorithms applied to or in conjunction with points (Imath::Vec2
  7. // and Imath::Vec3).
  8. //
  9. // The assumption made is that these functions are called much
  10. // less often than the basic point functions or these functions
  11. // require more support classes.
  12. //
  13. #ifndef INCLUDED_IMATHVECALGO_H
  14. #define INCLUDED_IMATHVECALGO_H
  15. #include "ImathNamespace.h"
  16. #include "ImathVec.h"
  17. IMATH_INTERNAL_NAMESPACE_HEADER_ENTER
  18. /// @cond Doxygen_Suppress
  19. //
  20. // Note: doxygen doesn't understand these templates, so omit these
  21. // functions from the docs.
  22. //
  23. /// Find the projection of vector `t` onto vector `s` (`Vec2`, `Vec3`, `Vec4`)
  24. ///
  25. /// Only defined for floating-point types, e.g. `V2f`, `V3d`, etc.
  26. template <class Vec,
  27. IMATH_ENABLE_IF(!std::is_integral<typename Vec::BaseType>::value)>
  28. IMATH_CONSTEXPR14 inline Vec
  29. project (const Vec& s, const Vec& t) IMATH_NOEXCEPT
  30. {
  31. Vec sNormalized = s.normalized();
  32. return sNormalized * (sNormalized ^ t);
  33. }
  34. /// Find a vector that is perpendicular to `s` and
  35. /// in the same plane as `s` and `t` (`Vec2`, `Vec3`, `Vec4`)
  36. ///
  37. /// Only defined for floating-point types, e.g. `V2f`, `V3d`, etc.
  38. template <class Vec,
  39. IMATH_ENABLE_IF(!std::is_integral<typename Vec::BaseType>::value)>
  40. constexpr inline Vec
  41. orthogonal (const Vec& s, const Vec& t) IMATH_NOEXCEPT
  42. {
  43. return t - project (s, t);
  44. }
  45. /// Find the direction of a ray `s` after reflection
  46. /// off a plane with normal `t` (`Vec2`, `Vec3`, `Vec4`)
  47. ///
  48. /// Only defined for floating-point types, e.g. `V2f`, `V3d`, etc.
  49. template <class Vec,
  50. IMATH_ENABLE_IF(!std::is_integral<typename Vec::BaseType>::value)>
  51. constexpr inline Vec
  52. reflect (const Vec& s, const Vec& t) IMATH_NOEXCEPT
  53. {
  54. return s - typename Vec::BaseType (2) * (s - project (t, s));
  55. }
  56. /// @endcond
  57. /// Find the vertex of triangle `(v0, v1, v2)` that is closest to point `p`
  58. /// (`Vec2`, `Vec3`, `Vec4`)
  59. template <class Vec>
  60. IMATH_CONSTEXPR14 Vec
  61. closestVertex (const Vec& v0, const Vec& v1, const Vec& v2, const Vec& p) IMATH_NOEXCEPT
  62. {
  63. Vec nearest = v0;
  64. typename Vec::BaseType neardot = (v0 - p).length2();
  65. typename Vec::BaseType tmp = (v1 - p).length2();
  66. if (tmp < neardot)
  67. {
  68. neardot = tmp;
  69. nearest = v1;
  70. }
  71. tmp = (v2 - p).length2();
  72. if (tmp < neardot)
  73. {
  74. neardot = tmp;
  75. nearest = v2;
  76. }
  77. return nearest;
  78. }
  79. IMATH_INTERNAL_NAMESPACE_HEADER_EXIT
  80. #endif // INCLUDED_IMATHVECALGO_H