polygon_corners.cpp 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. // This file is part of libigl, a simple c++ geometry processing library.
  2. //
  3. // Copyright (C) 2020 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 "polygon_corners.h"
  9. template <
  10. typename PType,
  11. typename DerivedI,
  12. typename DerivedC>
  13. IGL_INLINE void igl::polygon_corners(
  14. const std::vector<std::vector<PType> > & P,
  15. Eigen::PlainObjectBase<DerivedI> & I,
  16. Eigen::PlainObjectBase<DerivedC> & C)
  17. {
  18. typedef typename DerivedI::Scalar IType;
  19. // JD: Honestly you could do a first loop over P, compute C, and then fill the
  20. // entries of I directly. No need for guesses and push_back(), or the extra
  21. // copy at the end. That would be more efficient.
  22. std::vector<IType> vI;vI.reserve(P.size()*4);
  23. C.resize(P.size()+1);
  24. C(0) = 0;
  25. for(size_t p = 0;p<P.size();p++)
  26. {
  27. C(p+1) = C(p)+P[p].size();
  28. for(size_t c = 0;c<P[p].size();c++)
  29. {
  30. vI.push_back(P[p][c]);
  31. }
  32. }
  33. I = Eigen::Map<DerivedI>(vI.data(),vI.size());
  34. }
  35. template <
  36. typename DerivedQ,
  37. typename DerivedI,
  38. typename DerivedC>
  39. IGL_INLINE void igl::polygon_corners(
  40. const Eigen::MatrixBase<DerivedQ> & Q,
  41. Eigen::PlainObjectBase<DerivedI> & I,
  42. Eigen::PlainObjectBase<DerivedC> & C)
  43. {
  44. I.resize(Q.size());
  45. C.resize(Q.rows()+1);
  46. Eigen::Index c = 0;
  47. C(0) = 0;
  48. for(Eigen::Index p = 0;p<Q.rows();p++)
  49. {
  50. Eigen::Index np = 0;
  51. for(Eigen::Index i = 0;i<Q.cols();i++)
  52. {
  53. if(Q(p,i) == -1){ break;}
  54. I(c++) = Q(p,i);
  55. np++;
  56. }
  57. C(p+1) = C(p)+np;
  58. }
  59. I.conservativeResize(c);
  60. }
  61. #ifdef IGL_STATIC_LIBRARY
  62. // Explicit template instantiation
  63. // generated by autoexplicit.sh
  64. template void igl::polygon_corners<Eigen::Matrix<int, -1, -1, 0, -1, -1>, Eigen::Matrix<int, -1, 1, 0, -1, 1>, Eigen::Matrix<int, -1, 1, 0, -1, 1> >(Eigen::MatrixBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, 1, 0, -1, 1> >&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, 1, 0, -1, 1> >&);
  65. // generated by autoexplicit.sh
  66. template void igl::polygon_corners<int, Eigen::Matrix<int, -1, 1, 0, -1, 1>, Eigen::Matrix<int, -1, 1, 0, -1, 1> >(std::vector<std::vector<int, std::allocator<int> >, std::allocator<std::vector<int, std::allocator<int> > > > const&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, 1, 0, -1, 1> >&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, 1, 0, -1, 1> >&);
  67. #endif