internal_angles.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. // This file is part of libigl, a simple c++ geometry processing library.
  2. //
  3. // Copyright (C) 2015 Alec Jacobson <alecjacobson@gmail.com>
  4. // Copyright (C) 2015 Daniele Panozzo <daniele.panozzo@gmail.com>
  5. //
  6. // This Source Code Form is subject to the terms of the Mozilla Public License
  7. // v. 2.0. If a copy of the MPL was not distributed with this file, You can
  8. // obtain one at http://mozilla.org/MPL/2.0/.
  9. #include "internal_angles.h"
  10. #include "edge_lengths.h"
  11. #include "get_seconds.h"
  12. template <typename DerivedV, typename DerivedF, typename DerivedK>
  13. IGL_INLINE void igl::internal_angles(
  14. const Eigen::PlainObjectBase<DerivedV>& V,
  15. const Eigen::PlainObjectBase<DerivedF>& F,
  16. Eigen::PlainObjectBase<DerivedK> & K)
  17. {
  18. using namespace Eigen;
  19. using namespace std;
  20. if(F.cols() == 3)
  21. {
  22. // Edge lengths
  23. Matrix<
  24. typename DerivedV::Scalar,
  25. DerivedF::RowsAtCompileTime,
  26. DerivedF::ColsAtCompileTime> L;
  27. edge_lengths(V,F,L);
  28. assert(F.cols() == 3 && "F should contain triangles");
  29. internal_angles(L,K);
  30. }else
  31. {
  32. assert(V.cols() == 3 && "If F contains non-triangle facets, V must be 3D");
  33. K.resize(F.rows(),F.cols());
  34. auto corner = [](
  35. const Eigen::PlainObjectBase<DerivedV>& x,
  36. const Eigen::PlainObjectBase<DerivedV>& y,
  37. const Eigen::PlainObjectBase<DerivedV>& z)
  38. {
  39. Eigen::RowVector3d v1 = (x-y).normalized();
  40. Eigen::RowVector3d v2 = (z-y).normalized();
  41. // http://stackoverflow.com/questions/10133957/signed-angle-between-two-vectors-without-a-reference-plane
  42. double s = v1.cross(v2).norm();
  43. double c = v1.dot(v2);
  44. return atan2(s, c);
  45. };
  46. for(unsigned i=0; i<F.rows(); ++i)
  47. {
  48. for(unsigned j=0; j<F.cols(); ++j)
  49. {
  50. K(i,j) = corner(
  51. V.row(F(i,int(j-1+F.cols())%F.cols())),
  52. V.row(F(i,j)),
  53. V.row(F(i,(j+1+F.cols())%F.cols()))
  54. );
  55. }
  56. }
  57. }
  58. }
  59. template <typename DerivedL, typename DerivedK>
  60. IGL_INLINE void igl::internal_angles(
  61. const Eigen::PlainObjectBase<DerivedL>& L,
  62. Eigen::PlainObjectBase<DerivedK> & K)
  63. {
  64. assert(L.cols() == 3 && "Edge-lengths should come from triangles");
  65. const size_t m = L.rows();
  66. K.resize(m,3);
  67. //for(int d = 0;d<3;d++)
  68. //{
  69. // const auto & s1 = L.col(d).array();
  70. // const auto & s2 = L.col((d+1)%3).array();
  71. // const auto & s3 = L.col((d+2)%3).array();
  72. // K.col(d) = ((s3.square() + s2.square() - s1.square())/(2.*s3*s2)).acos();
  73. //}
  74. // Minimum number of iterms per openmp thread
  75. #ifndef IGL_OMP_MIN_VALUE
  76. # define IGL_OMP_MIN_VALUE 1000
  77. #endif
  78. #pragma omp parallel for if (m>IGL_OMP_MIN_VALUE)
  79. for(size_t f = 0;f<m;f++)
  80. {
  81. for(size_t d = 0;d<3;d++)
  82. {
  83. const auto & s1 = L(f,d);
  84. const auto & s2 = L(f,(d+1)%3);
  85. const auto & s3 = L(f,(d+2)%3);
  86. K(f,d) = acos((s3*s3 + s2*s2 - s1*s1)/(2.*s3*s2));
  87. }
  88. }
  89. }
  90. #ifdef IGL_STATIC_LIBRARY
  91. // Explicit template specialization
  92. template void igl::internal_angles<Eigen::Matrix<double, -1, -1, 0, -1, -1>, Eigen::Matrix<int, -1, -1, 0, -1, -1>, Eigen::Matrix<double, -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<double, -1, -1, 0, -1, -1> >&);
  93. template void igl::internal_angles<Eigen::Matrix<double, -1, 3, 1, -1, 3>, Eigen::Matrix<unsigned int, -1, -1, 1, -1, -1>, Eigen::Matrix<double, -1, -1, 0, -1, -1> >(Eigen::PlainObjectBase<Eigen::Matrix<double, -1, 3, 1, -1, 3> > const&, Eigen::PlainObjectBase<Eigen::Matrix<unsigned int, -1, -1, 1, -1, -1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> >&);
  94. template void igl::internal_angles<Eigen::Matrix<double, -1, 3, 0, -1, 3>, Eigen::Matrix<int, -1, 3, 0, -1, 3>, Eigen::Matrix<double, -1, -1, 0, -1, -1> >(Eigen::PlainObjectBase<Eigen::Matrix<double, -1, 3, 0, -1, 3> > const&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, 3, 0, -1, 3> > const&, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> >&);
  95. #endif