in_element.cpp 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. // This file is part of libigl, a simple c++ geometry processing library.
  2. //
  3. // Copyright (C) 2015 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 "in_element.h"
  9. #include "parallel_for.h"
  10. template <
  11. typename DerivedV,
  12. typename DerivedEle,
  13. typename DerivedQ,
  14. int DIM,
  15. typename DerivedI
  16. >
  17. IGL_INLINE void igl::in_element(
  18. const Eigen::MatrixBase<DerivedV> & V,
  19. const Eigen::MatrixBase<DerivedEle> & Ele,
  20. const Eigen::MatrixBase<DerivedQ> & Q,
  21. const AABB<DerivedV,DIM> & aabb,
  22. Eigen::PlainObjectBase<DerivedI> & I)
  23. {
  24. const int Qr = Q.rows();
  25. I.setConstant(Qr,1,-1);
  26. parallel_for(Qr,[&](const int e)
  27. {
  28. // find all
  29. const auto R = aabb.find(V,Ele,Q.row(e).eval(),true);
  30. if(!R.empty())
  31. {
  32. I(e) = R[0];
  33. }
  34. },10000);
  35. }
  36. template <typename DerivedV, typename DerivedEle, typename DerivedQ, int DIM, typename Scalar>
  37. IGL_INLINE void igl::in_element(
  38. const Eigen::MatrixBase<DerivedV> & V,
  39. const Eigen::MatrixBase<DerivedEle> & Ele,
  40. const Eigen::MatrixBase<DerivedQ> & Q,
  41. const AABB<DerivedV,DIM> & aabb,
  42. Eigen::SparseMatrix<Scalar> & I)
  43. {
  44. const int Qr = Q.rows();
  45. std::vector<Eigen::Triplet<Scalar> > IJV;
  46. IJV.reserve(Qr);
  47. // #pragma omp parallel for if (Qr>10000)
  48. for(int e = 0;e<Qr;e++)
  49. {
  50. // find all
  51. const auto R = aabb.find(V,Ele,Q.row(e).eval(),false);
  52. for(const auto r : R)
  53. {
  54. // #pragma omp critical
  55. IJV.push_back(Eigen::Triplet<Scalar>(e,r,1));
  56. }
  57. }
  58. I.resize(Qr,Ele.rows());
  59. I.setFromTriplets(IJV.begin(),IJV.end());
  60. }
  61. #ifdef IGL_STATIC_LIBRARY
  62. // Explicit template instantiation
  63. template
  64. void igl::in_element
  65. <
  66. Eigen::MatrixXd,
  67. Eigen::MatrixXi,
  68. Eigen::MatrixXd,
  69. 3,
  70. Eigen::VectorXi>
  71. (
  72. const Eigen::MatrixBase<Eigen::MatrixXd> & V,
  73. const Eigen::MatrixBase<Eigen::MatrixXi> & Ele,
  74. const Eigen::MatrixBase<Eigen::MatrixXd> & Q,
  75. const AABB<Eigen::MatrixXd,3> & aabb,
  76. Eigen::PlainObjectBase<Eigen::VectorXi> & I);
  77. #endif