round_cone_signed_distance.cpp 3.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. // This file is part of libigl, a simple c++ geometry processing library.
  2. //
  3. // Copyright (C) 2025 Alec Jacobson <[email protected]>
  4. //
  5. // This Source Code Form is subject to the terms of the Mozilla Public License
  6. // v. 2.0. If a copy of the MPL was not distributed with this file, You can
  7. // obtain one at http://mozilla.org/MPL/2.0/.
  8. #include "round_cone_signed_distance.h"
  9. #include "sign.h"
  10. template <
  11. typename Derivedp,
  12. typename Deriveda,
  13. typename Derivedb>
  14. IGL_INLINE typename Derivedp::Scalar igl::round_cone_signed_distance(
  15. const Eigen::MatrixBase<Derivedp> & p,
  16. const Eigen::MatrixBase<Deriveda> & a,
  17. const Eigen::MatrixBase<Derivedb> & b,
  18. const typename Derivedp::Scalar & r1,
  19. const typename Derivedp::Scalar & r2)
  20. {
  21. // https://iquilezles.org/articles/distfunctions/
  22. using Scalar = typename Derivedp::Scalar;
  23. using RowVector3S = Eigen::Matrix<Scalar, 1, 3>;
  24. // sampling independent computations (only depend on shape)
  25. const RowVector3S ba = b - a;
  26. const Scalar l2 = ba.dot(ba);
  27. const Scalar rr = r1 - r2;
  28. const Scalar a2 = l2 - rr*rr;
  29. const Scalar il2 = 1.0/l2;
  30. return round_cone_signed_distance(p,a,r1,r2,ba,l2,rr,a2,il2);
  31. }
  32. template <
  33. typename Derivedp,
  34. typename Deriveda,
  35. typename Derivedba>
  36. IGL_INLINE typename Derivedp::Scalar igl::round_cone_signed_distance(
  37. const Eigen::MatrixBase<Derivedp> & p,
  38. const Eigen::MatrixBase<Deriveda> & a,
  39. const typename Derivedp::Scalar & r1,
  40. const typename Derivedp::Scalar & r2,
  41. const Eigen::MatrixBase<Derivedba> & ba,
  42. const typename Derivedp::Scalar & l2,
  43. const typename Derivedp::Scalar & rr,
  44. const typename Derivedp::Scalar & a2,
  45. const typename Derivedp::Scalar & il2)
  46. {
  47. using Scalar = typename Derivedp::Scalar;
  48. using RowVector3S = Eigen::Matrix<Scalar, 1, 3>;
  49. // sampling dependant computations
  50. const RowVector3S pa = p - a;
  51. const Scalar y = pa.dot(ba);
  52. const Scalar z = y - l2;
  53. const Scalar x2 = ( pa*l2 - ba*y ).squaredNorm();
  54. const Scalar y2 = y*y*l2;
  55. const Scalar z2 = z*z*l2;
  56. // single square root!
  57. const Scalar k = sign(rr)*rr*rr*x2;
  58. if( sign(z)*a2*z2>k ) return sqrt(x2 + z2) *il2 - r2;
  59. if( sign(y)*a2*y2<k ) return sqrt(x2 + y2) *il2 - r1;
  60. return (sqrt(x2*a2*il2)+y*rr)*il2 - r1;
  61. }
  62. #ifdef IGL_STATIC_LIBRARY
  63. // Explicit template instantiation
  64. template Eigen::Block<Eigen::Matrix<double, 3, 3, 1, 3, 3>, 1, 3, true>::Scalar igl::round_cone_signed_distance<Eigen::Block<Eigen::Matrix<double, 3, 3, 1, 3, 3>, 1, 3, true>, Eigen::Block<Eigen::Matrix<double, 3, 3, 1, 3, 3>, 1, 3, true>, Eigen::Block<Eigen::Matrix<double, 3, 3, 1, 3, 3>, 1, 3, true>>(Eigen::MatrixBase<Eigen::Block<Eigen::Matrix<double, 3, 3, 1, 3, 3>, 1, 3, true>> const&, Eigen::MatrixBase<Eigen::Block<Eigen::Matrix<double, 3, 3, 1, 3, 3>, 1, 3, true>> const&, Eigen::MatrixBase<Eigen::Block<Eigen::Matrix<double, 3, 3, 1, 3, 3>, 1, 3, true>> const&, Eigen::Block<Eigen::Matrix<double, 3, 3, 1, 3, 3>, 1, 3, true>::Scalar const&, Eigen::Block<Eigen::Matrix<double, 3, 3, 1, 3, 3>, 1, 3, true>::Scalar const&);
  65. template Eigen::Matrix<double, 1, 3, 1, 1, 3>::Scalar igl::round_cone_signed_distance<Eigen::Matrix<double, 1, 3, 1, 1, 3>, Eigen::Block<Eigen::Matrix<double, 3, 3, 1, 3, 3> const, 1, 3, true>, Eigen::Block<Eigen::Matrix<double, 3, 3, 1, 3, 3> const, 1, 3, true>>(Eigen::MatrixBase<Eigen::Matrix<double, 1, 3, 1, 1, 3>> const&, Eigen::MatrixBase<Eigen::Block<Eigen::Matrix<double, 3, 3, 1, 3, 3> const, 1, 3, true>> const&, Eigen::Matrix<double, 1, 3, 1, 1, 3>::Scalar const&, Eigen::Matrix<double, 1, 3, 1, 1, 3>::Scalar const&, Eigen::MatrixBase<Eigen::Block<Eigen::Matrix<double, 3, 3, 1, 3, 3> const, 1, 3, true>> const&, Eigen::Matrix<double, 1, 3, 1, 1, 3>::Scalar const&, Eigen::Matrix<double, 1, 3, 1, 1, 3>::Scalar const&, Eigen::Matrix<double, 1, 3, 1, 1, 3>::Scalar const&, Eigen::Matrix<double, 1, 3, 1, 1, 3>::Scalar const&);
  66. #endif