angles.cpp 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. // This file is part of libigl, a simple c++ geometry processing library.
  2. //
  3. // Copyright (C) 2015 Daniele Panozzo <daniele.panozzo@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 "angles.h"
  9. #include <cassert>
  10. template <
  11. typename DerivedV,
  12. typename DerivedF,
  13. typename Derivedtheta>
  14. void igl::angles(
  15. const Eigen::PlainObjectBase<DerivedV>& V,
  16. const Eigen::PlainObjectBase<DerivedF>& F,
  17. Eigen::PlainObjectBase<Derivedtheta>& theta)
  18. {
  19. theta.resize(F.rows(),F.cols());
  20. auto corner = [](const Eigen::PlainObjectBase<DerivedV>& x, const Eigen::PlainObjectBase<DerivedV>& y, const Eigen::PlainObjectBase<DerivedV>& z)
  21. {
  22. Eigen::RowVector3d v1 = (x-y).normalized();
  23. Eigen::RowVector3d v2 = (z-y).normalized();
  24. // http://stackoverflow.com/questions/10133957/signed-angle-between-two-vectors-without-a-reference-plane
  25. double s = v1.cross(v2).norm();
  26. double c = v1.dot(v2);
  27. return atan2(s, c);
  28. };
  29. for(unsigned i=0; i<F.rows(); ++i)
  30. {
  31. for(unsigned j=0; j<F.cols(); ++j)
  32. {
  33. theta(i,j) = corner(
  34. V.row(F(i,int(j-1+F.cols())%F.cols())),
  35. V.row(F(i,j)),
  36. V.row(F(i,(j+1+F.cols())%F.cols()))
  37. );
  38. }
  39. }
  40. }
  41. #ifdef IGL_STATIC_LIBRARY
  42. // Explicit template specialization
  43. #endif