per_edge_normals.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. // This file is part of libigl, a simple c++ geometry processing library.
  2. //
  3. // Copyright (C) 2015 Alec Jacobson <alecjacobson@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 "all_edges.h"
  9. #include "doublearea.h"
  10. #include "per_edge_normals.h"
  11. #include "get_seconds.h"
  12. #include "per_face_normals.h"
  13. #include "unique_simplices.h"
  14. #include <vector>
  15. template <
  16. typename DerivedV,
  17. typename DerivedF,
  18. typename DerivedFN,
  19. typename DerivedN,
  20. typename DerivedE,
  21. typename DerivedEMAP>
  22. IGL_INLINE void igl::per_edge_normals(
  23. const Eigen::PlainObjectBase<DerivedV>& V,
  24. const Eigen::PlainObjectBase<DerivedF>& F,
  25. const PerEdgeNormalsWeightingType weighting,
  26. const Eigen::PlainObjectBase<DerivedFN>& FN,
  27. Eigen::PlainObjectBase<DerivedN> & N,
  28. Eigen::PlainObjectBase<DerivedE> & E,
  29. Eigen::PlainObjectBase<DerivedEMAP> & EMAP)
  30. {
  31. using namespace Eigen;
  32. using namespace std;
  33. assert(F.cols() == 3 && "Faces must be triangles");
  34. // number of faces
  35. const int m = F.rows();
  36. // All occurances of directed edges
  37. MatrixXi allE;
  38. all_edges(F,allE);
  39. // Find unique undirected edges and mapping
  40. VectorXi _;
  41. unique_simplices(allE,E,_,EMAP);
  42. // now sort(allE,2) == E(EMAP,:), that is, if EMAP(i) = j, then E.row(j) is
  43. // the undirected edge corresponding to the directed edge allE.row(i).
  44. Eigen::VectorXd W;
  45. switch(weighting)
  46. {
  47. case PER_EDGE_NORMALS_WEIGHTING_TYPE_UNIFORM:
  48. // Do nothing
  49. break;
  50. default:
  51. assert(false && "Unknown weighting type");
  52. case PER_EDGE_NORMALS_WEIGHTING_TYPE_DEFAULT:
  53. case PER_EDGE_NORMALS_WEIGHTING_TYPE_AREA:
  54. {
  55. doublearea(V,F,W);
  56. break;
  57. }
  58. }
  59. N.setZero(E.rows(),3);
  60. for(int f = 0;f<m;f++)
  61. {
  62. for(int c = 0;c<3;c++)
  63. {
  64. if(weighting == PER_EDGE_NORMALS_WEIGHTING_TYPE_UNIFORM)
  65. {
  66. N.row(EMAP(f+c*m)) += FN.row(f);
  67. }else
  68. {
  69. N.row(EMAP(f+c*m)) += W(f) * FN.row(f);
  70. }
  71. }
  72. }
  73. }
  74. template <
  75. typename DerivedV,
  76. typename DerivedF,
  77. typename DerivedN,
  78. typename DerivedE,
  79. typename DerivedEMAP>
  80. IGL_INLINE void igl::per_edge_normals(
  81. const Eigen::PlainObjectBase<DerivedV>& V,
  82. const Eigen::PlainObjectBase<DerivedF>& F,
  83. const PerEdgeNormalsWeightingType weighting,
  84. Eigen::PlainObjectBase<DerivedN> & N,
  85. Eigen::PlainObjectBase<DerivedE> & E,
  86. Eigen::PlainObjectBase<DerivedEMAP> & EMAP)
  87. {
  88. Eigen::Matrix<typename DerivedN::Scalar,Eigen::Dynamic,3> FN;
  89. per_face_normals(V,F,FN);
  90. return per_edge_normals(V,F,weighting,FN,N,E,EMAP);
  91. }
  92. template <
  93. typename DerivedV,
  94. typename DerivedF,
  95. typename DerivedN,
  96. typename DerivedE,
  97. typename DerivedEMAP>
  98. IGL_INLINE void igl::per_edge_normals(
  99. const Eigen::PlainObjectBase<DerivedV>& V,
  100. const Eigen::PlainObjectBase<DerivedF>& F,
  101. Eigen::PlainObjectBase<DerivedN> & N,
  102. Eigen::PlainObjectBase<DerivedE> & E,
  103. Eigen::PlainObjectBase<DerivedEMAP> & EMAP)
  104. {
  105. return
  106. per_edge_normals(V,F,PER_EDGE_NORMALS_WEIGHTING_TYPE_DEFAULT,N,E,EMAP);
  107. }
  108. #ifdef IGL_STATIC_LIBRARY
  109. // Explicit template specialization
  110. template void igl::per_edge_normals<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::Matrix<int, -1, -1, 0, -1, -1>, Eigen::Matrix<int, -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> >&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> >&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, 1, 0, -1, 1> >&);
  111. template void igl::per_edge_normals<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::Matrix<double, -1, -1, 0, -1, -1>, Eigen::Matrix<int, -1, -1, 0, -1, -1>, Eigen::Matrix<int, -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&, igl::PerEdgeNormalsWeightingType, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> >&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> >&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, 1, 0, -1, 1> >&);
  112. #endif