internal_angles.cpp 4.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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 "parallel_for.h"
  12. #include "get_seconds.h"
  13. template <typename DerivedV, typename DerivedF, typename DerivedK>
  14. IGL_INLINE void igl::internal_angles(
  15. const Eigen::PlainObjectBase<DerivedV>& V,
  16. const Eigen::PlainObjectBase<DerivedF>& F,
  17. Eigen::PlainObjectBase<DerivedK> & K)
  18. {
  19. using namespace Eigen;
  20. using namespace std;
  21. typedef typename DerivedV::Scalar Scalar;
  22. if(F.cols() == 3)
  23. {
  24. // Edge lengths
  25. Matrix<
  26. Scalar,
  27. DerivedF::RowsAtCompileTime,
  28. DerivedF::ColsAtCompileTime> L;
  29. edge_lengths(V,F,L);
  30. assert(F.cols() == 3 && "F should contain triangles");
  31. internal_angles(L,K);
  32. }else
  33. {
  34. assert(V.cols() == 3 && "If F contains non-triangle facets, V must be 3D");
  35. K.resize(F.rows(),F.cols());
  36. auto corner = [](
  37. const typename DerivedV::ConstRowXpr & x,
  38. const typename DerivedV::ConstRowXpr & y,
  39. const typename DerivedV::ConstRowXpr & z)
  40. {
  41. typedef Eigen::Matrix<Scalar,1,3> RowVector3S;
  42. RowVector3S v1 = (x-y).normalized();
  43. RowVector3S v2 = (z-y).normalized();
  44. // http://stackoverflow.com/questions/10133957/signed-angle-between-two-vectors-without-a-reference-plane
  45. Scalar s = v1.cross(v2).norm();
  46. Scalar c = v1.dot(v2);
  47. return atan2(s, c);
  48. };
  49. for(unsigned i=0; i<F.rows(); ++i)
  50. {
  51. for(unsigned j=0; j<F.cols(); ++j)
  52. {
  53. K(i,j) = corner(
  54. V.row(F(i,int(j-1+F.cols())%F.cols())),
  55. V.row(F(i,j)),
  56. V.row(F(i,(j+1+F.cols())%F.cols()))
  57. );
  58. }
  59. }
  60. }
  61. }
  62. template <typename DerivedL, typename DerivedK>
  63. IGL_INLINE void igl::internal_angles(
  64. const Eigen::PlainObjectBase<DerivedL>& L,
  65. Eigen::PlainObjectBase<DerivedK> & K)
  66. {
  67. typedef typename DerivedL::Index Index;
  68. assert(L.cols() == 3 && "Edge-lengths should come from triangles");
  69. const Index m = L.rows();
  70. K.resize(m,3);
  71. parallel_for(
  72. m,
  73. [&L,&K](const Index f)
  74. {
  75. for(size_t d = 0;d<3;d++)
  76. {
  77. const auto & s1 = L(f,d);
  78. const auto & s2 = L(f,(d+1)%3);
  79. const auto & s3 = L(f,(d+2)%3);
  80. K(f,d) = acos((s3*s3 + s2*s2 - s1*s1)/(2.*s3*s2));
  81. }
  82. },
  83. 1000l);
  84. }
  85. #ifdef IGL_STATIC_LIBRARY
  86. // Explicit template specialization
  87. // generated by autoexplicit.sh
  88. template void igl::internal_angles<Eigen::Matrix<float, -1, 3, 1, -1, 3>, Eigen::Matrix<unsigned int, -1, 3, 1, -1, 3>, Eigen::Matrix<float, -1, 3, 0, -1, 3> >(Eigen::PlainObjectBase<Eigen::Matrix<float, -1, 3, 1, -1, 3> > const&, Eigen::PlainObjectBase<Eigen::Matrix<unsigned int, -1, 3, 1, -1, 3> > const&, Eigen::PlainObjectBase<Eigen::Matrix<float, -1, 3, 0, -1, 3> >&);
  89. 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> >&);
  90. 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> >&);
  91. 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> >&);
  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, 3, 0, -1, 3> >(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, 3, 0, -1, 3> >&);
  93. 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, 3, 0, -1, 3> >(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, 3, 0, -1, 3> >&);
  94. #endif