angles.cpp 1.8 KB

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