doublearea.cpp 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. #include "doublearea.h"
  2. #include "edge_lengths.h"
  3. #include <cassert>
  4. template <typename DerivedV, typename DerivedF, typename DeriveddblA>
  5. IGL_INLINE void igl::doublearea(
  6. const Eigen::PlainObjectBase<DerivedV> & V,
  7. const Eigen::PlainObjectBase<DerivedF> & F,
  8. Eigen::PlainObjectBase<DeriveddblA> & dblA)
  9. {
  10. // Only support triangles
  11. assert(F.cols() == 3);
  12. // Compute edge lengths
  13. Eigen::PlainObjectBase<DerivedV> l;
  14. edge_lengths(V,F,l);
  15. return doublearea(l,dblA);
  16. }
  17. template <typename Derivedl, typename DeriveddblA>
  18. IGL_INLINE void igl::doublearea(
  19. const Eigen::PlainObjectBase<Derivedl> & l,
  20. Eigen::PlainObjectBase<DeriveddblA> & dblA)
  21. {
  22. // Only support triangles
  23. assert(l.cols() == 3);
  24. // Number of triangles
  25. const int m = l.rows();
  26. // semiperimeters
  27. Eigen::PlainObjectBase<Derivedl> s = l.rowwise().sum()*0.5;
  28. assert(s.rows() == m);
  29. // resize output
  30. dblA.resize(l.rows(),1);
  31. // Heron's formula for area
  32. for(int i = 0;i<m;i++)
  33. {
  34. dblA(i) = 2.0*sqrt(s(i)*(s(i)-l(i,0))*(s(i)-l(i,1))*(s(i)-l(i,2)));
  35. }
  36. }
  37. #ifndef IGL_HEADER_ONLY
  38. template void igl::doublearea<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> >&);
  39. template void igl::doublearea<Eigen::Matrix<double, -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<double, -1, 1, 0, -1, 1> >&);
  40. template void igl::doublearea<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<int, -1, 1, 0, -1, 1> >&);
  41. #endif