edge_vectors.cpp 3.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. // This file is part of libigl, a simple c++ geometry processing library.
  2. //
  3. // Copyright (C) 2020 Oded Stein <[email protected]>
  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 "edge_vectors.h"
  9. #include <Eigen/Geometry>
  10. #include "per_face_normals.h"
  11. #include "PI.h"
  12. template<typename DerivedV,typename DerivedF,typename DerivedE,
  13. typename DerivedoE, typename Derivedvec>
  14. IGL_INLINE void
  15. igl::edge_vectors(
  16. const Eigen::MatrixBase<DerivedV> &V,
  17. const Eigen::MatrixBase<DerivedF> &F,
  18. const Eigen::MatrixBase<DerivedE> &E,
  19. const Eigen::MatrixBase<DerivedoE> &oE,
  20. Eigen::PlainObjectBase<Derivedvec> &vec)
  21. {
  22. Eigen::Matrix<typename Derivedvec::Scalar, Eigen::Dynamic, Eigen::Dynamic>
  23. dummy;
  24. edge_vectors<false>(V, F, E, oE, vec, dummy);
  25. }
  26. template<bool computePerpendicular,
  27. typename DerivedV,typename DerivedF,typename DerivedE,
  28. typename DerivedoE, typename DerivedvecParallel,
  29. typename DerivedvecPerpendicular>
  30. IGL_INLINE void
  31. igl::edge_vectors(
  32. const Eigen::MatrixBase<DerivedV> &V,
  33. const Eigen::MatrixBase<DerivedF> &F,
  34. const Eigen::MatrixBase<DerivedE> &E,
  35. const Eigen::MatrixBase<DerivedoE> &oE,
  36. Eigen::PlainObjectBase<DerivedvecParallel> &vecParallel,
  37. Eigen::PlainObjectBase<DerivedvecPerpendicular> &vecPerpendicular)
  38. {
  39. using Scalar = typename DerivedvecParallel::Scalar;
  40. using MatX = Eigen::Matrix<Scalar, Eigen::Dynamic, Eigen::Dynamic>;
  41. assert(E.rows()==F.rows() && "E does not match dimensions of F.");
  42. assert(oE.rows()==F.rows() && "oE does not match dimensions of F.");
  43. assert(E.cols()==3 && F.cols()==3 && oE.cols()==3 &&
  44. "This method is for triangle meshes.");
  45. assert(F.maxCoeff()<V.rows() && "V does not seem to belong to F.");
  46. const typename DerivedE::Scalar m = E.maxCoeff()+1;
  47. //Compute edge-based normal
  48. MatX N, edgeN(m, 3);
  49. edgeN.setZero();
  50. per_face_normals(V, F, N);
  51. for(Eigen::Index i=0; i<E.rows(); ++i) {
  52. for(int j=0; j<3; ++j) {
  53. edgeN.row(E(i,j)) += N.row(i);
  54. }
  55. }
  56. edgeN.rowwise().normalize();
  57. //Compute edge vectors
  58. vecParallel.resize(m, 3);
  59. if(computePerpendicular) { //This should ideally be an if constexpr
  60. vecPerpendicular.resize(m, 3);
  61. }
  62. for(Eigen::Index i=0; i<E.rows(); ++i) {
  63. for(int j=0; j<3; ++j) {
  64. if(oE(i,j)<0) {
  65. continue;
  66. }
  67. const typename DerivedE::Scalar e=E(i,j);
  68. const typename DerivedF::Scalar vi=F(i,(j+1)%3), vj=F(i,(j+2)%3);
  69. vecParallel.row(e) = (V.row(vj)-V.row(vi)).normalized();
  70. if(computePerpendicular) { //This should ideally be an if constexpr
  71. vecPerpendicular.row(e) =
  72. Eigen::AngleAxis<Scalar>(0.5*PI, edgeN.row(e)) *
  73. vecParallel.row(e).transpose();
  74. }
  75. }
  76. }
  77. }
  78. #ifdef IGL_STATIC_LIBRARY
  79. // Explicit template instantiation
  80. template void igl::edge_vectors<true, 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::Matrix<int, -1, -1, 0, -1, -1>, Eigen::Matrix<double, -1, -1, 0, -1, -1>, Eigen::Matrix<double, -1, -1, 0, -1, -1> >(Eigen::MatrixBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> > const&, Eigen::MatrixBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> > const&, Eigen::MatrixBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> > const&, Eigen::MatrixBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> >&, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> >&);
  81. #endif