in_element.cpp 2.5 KB

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