flipped_triangles.cpp 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. // This file is part of libigl, a simple c++ geometry processing library.
  2. //
  3. // Copyright (C) 2016 Michael Rabinovich <michaelrabinovich27@gmail.com@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 "flipped_triangles.h"
  9. #include "list_to_matrix.h"
  10. #include <vector>
  11. template <typename DerivedV, typename DerivedF, typename DerivedX>
  12. IGL_INLINE void igl::flipped_triangles(
  13. const Eigen::PlainObjectBase<DerivedV> & V,
  14. const Eigen::PlainObjectBase<DerivedF> & F,
  15. Eigen::PlainObjectBase<DerivedX> & X)
  16. {
  17. assert(V.cols() == 2 && "V should contain 2D positions");
  18. std::vector<typename DerivedX::Scalar> flip_idx;
  19. for (int i = 0; i < F.rows(); i++)
  20. {
  21. // https://www.cs.cmu.edu/~quake/robust.html
  22. typedef Eigen::Matrix<typename DerivedV::Scalar,1,2> RowVector2S;
  23. RowVector2S v1_n = V.row(F(i,0));
  24. RowVector2S v2_n = V.row(F(i,1));
  25. RowVector2S v3_n = V.row(F(i,2));
  26. Eigen::Matrix<typename DerivedV::Scalar,3,3> T2_Homo;
  27. T2_Homo.col(0) << v1_n(0),v1_n(1),1.;
  28. T2_Homo.col(1) << v2_n(0),v2_n(1),1.;
  29. T2_Homo.col(2) << v3_n(0),v3_n(1),1.;
  30. double det = T2_Homo.determinant();
  31. assert(det == det && "det should not be NaN");
  32. if (det < 0)
  33. {
  34. flip_idx.push_back(i);
  35. }
  36. }
  37. igl::list_to_matrix(flip_idx,X);
  38. }
  39. template <typename DerivedV, typename DerivedF>
  40. IGL_INLINE Eigen::VectorXi igl::flipped_triangles(
  41. const Eigen::PlainObjectBase<DerivedV> & V,
  42. const Eigen::PlainObjectBase<DerivedF> & F)
  43. {
  44. Eigen::VectorXi X;
  45. flipped_triangles(V,F,X);
  46. return X;
  47. }
  48. #ifdef IGL_STATIC_LIBRARY
  49. // Explicit template instantiation
  50. template void igl::flipped_triangles<Eigen::Matrix<double, -1, -1, 0, -1, -1>, Eigen::Matrix<int, -1, -1, 0, -1, -1>, Eigen::Matrix<int, -1, 1, 0, -1, 1> >(Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, 1, 0, -1, 1> >&);
  51. template Eigen::Matrix<int, -1, 1, 0, -1, 1> igl::flipped_triangles<Eigen::Matrix<double, -1, -1, 0, -1, -1>, Eigen::Matrix<int, -1, -1, 0, -1, -1> >(Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> > const&);
  52. #endif