tet_tet_adjacency.cpp 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. // This file is part of libigl, a simple c++ geometry processing library.
  2. //
  3. // Copyright (C) 2018 Oded Stein <oded.stein@columbia.edu>
  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 "tet_tet_adjacency.h"
  9. #include "parallel_for.h"
  10. #include <array>
  11. #include <vector>
  12. template <typename DerivedT, typename DerivedTT, typename DerivedTTi>
  13. IGL_INLINE void
  14. igl::tet_tet_adjacency(
  15. const Eigen::MatrixBase<DerivedT>& T,
  16. Eigen::PlainObjectBase<DerivedTT>& TT,
  17. Eigen::PlainObjectBase<DerivedTTi>& TTi)
  18. {
  19. assert(T.cols()==4 && "Tets have four vertices.");
  20. //Preprocess
  21. using Array = std::array<typename DerivedT::Scalar, 5>;
  22. std::vector<Array> TTT(4*T.rows());
  23. const auto loop_f = [&](const int t) {
  24. TTT[4*t] = {T(t,0),T(t,1),T(t,2),t,0};
  25. TTT[4*t+1] = {T(t,0),T(t,1),T(t,3),t,1};
  26. TTT[4*t+2] = {T(t,1),T(t,2),T(t,3),t,2};
  27. TTT[4*t+3] = {T(t,2),T(t,0),T(t,3),t,3};
  28. for(int i=0; i<4; ++i)
  29. std::sort(TTT[4*t+i].begin(), TTT[4*t+i].begin()+3);
  30. };
  31. //for(int t=0; t<T.rows(); ++t)
  32. // loop_f(t);
  33. igl::parallel_for(T.rows(), loop_f);
  34. std::sort(TTT.begin(),TTT.end());
  35. //Compute TT and TTi
  36. TT.setConstant(T.rows(), T.cols(), -1);
  37. TTi.setConstant(T.rows(), T.cols(), -1);
  38. for(int i=1; i<TTT.size(); ++i) {
  39. const Array& r1 = TTT[i-1];
  40. const Array& r2 = TTT[i];
  41. if((r1[0]==r2[0]) && (r1[1]==r2[1]) && (r1[2]==r2[2])) {
  42. TT(r1[3],r1[4]) = r2[3];
  43. TT(r2[3],r2[4]) = r1[3];
  44. TTi(r1[3],r1[4]) = r2[4];
  45. TTi(r2[3],r2[4]) = r1[4];
  46. }
  47. }
  48. }
  49. template <typename DerivedT, typename DerivedTT>
  50. IGL_INLINE void
  51. igl::tet_tet_adjacency(
  52. const Eigen::MatrixBase<DerivedT>& T,
  53. Eigen::PlainObjectBase<DerivedTT>& TT)
  54. {
  55. DerivedTT TTi;
  56. tet_tet_adjacency(T, TT, TTi);
  57. }
  58. #ifdef IGL_STATIC_LIBRARY
  59. template void igl::tet_tet_adjacency<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> >&);
  60. #endif