doublearea.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. // This file is part of libigl, a simple c++ geometry processing library.
  2. //
  3. // Copyright (C) 2013 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 "doublearea.h"
  9. #include "edge_lengths.h"
  10. #include "sort.h"
  11. #include <cassert>
  12. #include <iostream>
  13. template <typename DerivedV, typename DerivedF, typename DeriveddblA>
  14. IGL_INLINE void igl::doublearea(
  15. const Eigen::PlainObjectBase<DerivedV> & V,
  16. const Eigen::PlainObjectBase<DerivedF> & F,
  17. Eigen::PlainObjectBase<DeriveddblA> & dblA)
  18. {
  19. const int dim = V.cols();
  20. // Only support triangles
  21. assert(F.cols() == 3);
  22. const int m = F.rows();
  23. // Compute edge lengths
  24. Eigen::PlainObjectBase<DerivedV> l;
  25. // "Lecture Notes on Geometric Robustness" Shewchuck 09, Section 3.1
  26. // http://www.cs.berkeley.edu/~jrs/meshpapers/robnotes.pdf
  27. switch(dim)
  28. {
  29. case 3:
  30. {
  31. dblA = Eigen::PlainObjectBase<DeriveddblA>::Zero(m,1);
  32. for(int d = 0;d<3;d++)
  33. {
  34. Eigen::Matrix<typename DerivedV::Scalar, DerivedV::RowsAtCompileTime, 2> Vd(V.rows(),2);
  35. Vd << V.col(d), V.col((d+1)%3);
  36. Eigen::PlainObjectBase<DeriveddblA> dblAd;
  37. doublearea(Vd,F,dblAd);
  38. dblA.array() += dblAd.array().square();
  39. }
  40. dblA = dblA.array().sqrt().eval();
  41. break;
  42. }
  43. case 2:
  44. {
  45. dblA.resize(m,1);
  46. for(int f = 0;f<m;f++)
  47. {
  48. auto r = V.row(F(f,0))-V.row(F(f,2));
  49. auto s = V.row(F(f,1))-V.row(F(f,2));
  50. dblA(f) = r(0)*s(1) - r(1)*s(0);
  51. }
  52. break;
  53. }
  54. default:
  55. {
  56. edge_lengths(V,F,l);
  57. return doublearea(l,dblA);
  58. }
  59. }
  60. }
  61. template <
  62. typename DerivedA,
  63. typename DerivedB,
  64. typename DerivedC,
  65. typename DerivedD>
  66. IGL_INLINE void doublearea(
  67. const Eigen::PlainObjectBase<DerivedA> & A,
  68. const Eigen::PlainObjectBase<DerivedB> & B,
  69. const Eigen::PlainObjectBase<DerivedC> & C,
  70. Eigen::PlainObjectBase<DerivedD> & D)
  71. {
  72. assert(A.cols() == 2 && "corners should be 2d");
  73. assert(B.cols() == 2 && "corners should be 2d");
  74. assert(C.cols() == 2 && "corners should be 2d");
  75. assert(A.rows() == B.rows() && "corners should have same length");
  76. assert(A.rows() == C.rows() && "corners should have same length");
  77. const auto & R = A-C;
  78. const auto & S = B-C;
  79. D = R.col(0).array()*S.col(1).array() - R.col(1).array()*S.col(0).array();
  80. }
  81. template <
  82. typename DerivedA,
  83. typename DerivedB,
  84. typename DerivedC>
  85. IGL_INLINE typename DerivedA::Scalar igl::doublearea_single(
  86. const Eigen::PlainObjectBase<DerivedA> & A,
  87. const Eigen::PlainObjectBase<DerivedB> & B,
  88. const Eigen::PlainObjectBase<DerivedC> & C)
  89. {
  90. auto r = A-C;
  91. auto s = B-C;
  92. return r(0)*s(1) - r(1)*s(0);
  93. }
  94. template <typename Derivedl, typename DeriveddblA>
  95. IGL_INLINE void igl::doublearea(
  96. const Eigen::PlainObjectBase<Derivedl> & ul,
  97. Eigen::PlainObjectBase<DeriveddblA> & dblA)
  98. {
  99. using namespace Eigen;
  100. using namespace std;
  101. // Only support triangles
  102. assert(ul.cols() == 3);
  103. // Number of triangles
  104. const int m = ul.rows();
  105. Eigen::PlainObjectBase<Derivedl> l;
  106. MatrixXi _;
  107. sort(ul,2,false,l,_);
  108. // semiperimeters
  109. Matrix<typename Derivedl::Scalar,Dynamic,1> s = l.rowwise().sum()*0.5;
  110. assert(s.rows() == m);
  111. // resize output
  112. dblA.resize(l.rows(),1);
  113. for(int i = 0;i<m;i++)
  114. {
  115. //// Heron's formula for area
  116. //const typename Derivedl::Scalar arg =
  117. // s(i)*(s(i)-l(i,0))*(s(i)-l(i,1))*(s(i)-l(i,2));
  118. //assert(arg>=0);
  119. //dblA(i) = 2.0*sqrt(arg);
  120. // Kahan's Heron's formula
  121. const typename Derivedl::Scalar arg =
  122. (l(i,0)+(l(i,1)+l(i,2)))*
  123. (l(i,2)-(l(i,0)-l(i,1)))*
  124. (l(i,2)+(l(i,0)-l(i,1)))*
  125. (l(i,0)+(l(i,1)-l(i,2)));
  126. dblA(i) = 2.0*0.25*sqrt(arg);
  127. assert( l(i,2) - (l(i,0)-l(i,1)) && "FAILED KAHAN'S ASSERTION");
  128. assert(dblA(i) == dblA(i) && "DOUBLEAREA() PRODUCED NaN");
  129. }
  130. }
  131. #ifdef IGL_STATIC_LIBRARY
  132. // Explicit template specialization
  133. // generated by autoexplicit.sh
  134. template void igl::doublearea<Eigen::Matrix<double, -1, 3, 0, -1, 3>, Eigen::Matrix<double, -1, 1, 0, -1, 1> >(Eigen::PlainObjectBase<Eigen::Matrix<double, -1, 3, 0, -1, 3> > const&, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, 1, 0, -1, 1> >&);
  135. // generated by autoexplicit.sh
  136. template void igl::doublearea<Eigen::Matrix<float, -1, 3, 1, -1, 3>, Eigen::Matrix<unsigned int, -1, -1, 1, -1, -1>, Eigen::Matrix<double, -1, 1, 0, -1, 1> >(Eigen::PlainObjectBase<Eigen::Matrix<float, -1, 3, 1, -1, 3> > const&, Eigen::PlainObjectBase<Eigen::Matrix<unsigned int, -1, -1, 1, -1, -1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, 1, 0, -1, 1> >&);
  137. 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> >&);
  138. 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> >&);
  139. 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> >&);
  140. template void igl::doublearea<Eigen::Matrix<double, -1, 3, 0, -1, 3>, Eigen::Matrix<int, -1, 3, 0, -1, 3>, Eigen::Matrix<double, -1, 1, 0, -1, 1> >(Eigen::PlainObjectBase<Eigen::Matrix<double, -1, 3, 0, -1, 3> > const&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, 3, 0, -1, 3> > const&, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, 1, 0, -1, 1> >&);
  141. template Eigen::Matrix<double, 2, 1, 0, 2, 1>::Scalar igl::doublearea_single<Eigen::Matrix<double, 2, 1, 0, 2, 1>, Eigen::Matrix<double, 2, 1, 0, 2, 1>, Eigen::Matrix<double, 2, 1, 0, 2, 1> >(Eigen::PlainObjectBase<Eigen::Matrix<double, 2, 1, 0, 2, 1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<double, 2, 1, 0, 2, 1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<double, 2, 1, 0, 2, 1> > const&);
  142. #endif