doublearea.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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. // Projected area helper
  28. const auto & proj_doublearea =
  29. [&V,&F](const int x, const int y, const int f)->double
  30. {
  31. auto rx = V(F(f,0),x)-V(F(f,2),x);
  32. auto sx = V(F(f,1),x)-V(F(f,2),x);
  33. auto ry = V(F(f,0),y)-V(F(f,2),y);
  34. auto sy = V(F(f,1),y)-V(F(f,2),y);
  35. return rx*sy - ry*sx;
  36. };
  37. switch(dim)
  38. {
  39. case 3:
  40. {
  41. dblA = Eigen::PlainObjectBase<DeriveddblA>::Zero(m,1);
  42. for(int f = 0;f<F.rows();f++)
  43. {
  44. for(int d = 0;d<3;d++)
  45. {
  46. double dblAd = proj_doublearea(d,(d+1)%3,f);
  47. dblA(f) += dblAd*dblAd;
  48. }
  49. }
  50. dblA = dblA.array().sqrt().eval();
  51. break;
  52. }
  53. case 2:
  54. {
  55. dblA.resize(m,1);
  56. for(int f = 0;f<m;f++)
  57. {
  58. dblA(f) = proj_doublearea(0,1,f);
  59. }
  60. break;
  61. }
  62. default:
  63. {
  64. edge_lengths(V,F,l);
  65. return doublearea(l,dblA);
  66. }
  67. }
  68. }
  69. template <
  70. typename DerivedA,
  71. typename DerivedB,
  72. typename DerivedC,
  73. typename DerivedD>
  74. IGL_INLINE void doublearea(
  75. const Eigen::PlainObjectBase<DerivedA> & A,
  76. const Eigen::PlainObjectBase<DerivedB> & B,
  77. const Eigen::PlainObjectBase<DerivedC> & C,
  78. Eigen::PlainObjectBase<DerivedD> & D)
  79. {
  80. assert(A.cols() == 2 && "corners should be 2d");
  81. assert(B.cols() == 2 && "corners should be 2d");
  82. assert(C.cols() == 2 && "corners should be 2d");
  83. assert(A.rows() == B.rows() && "corners should have same length");
  84. assert(A.rows() == C.rows() && "corners should have same length");
  85. const auto & R = A-C;
  86. const auto & S = B-C;
  87. D = R.col(0).array()*S.col(1).array() - R.col(1).array()*S.col(0).array();
  88. }
  89. template <
  90. typename DerivedA,
  91. typename DerivedB,
  92. typename DerivedC>
  93. IGL_INLINE typename DerivedA::Scalar igl::doublearea_single(
  94. const Eigen::PlainObjectBase<DerivedA> & A,
  95. const Eigen::PlainObjectBase<DerivedB> & B,
  96. const Eigen::PlainObjectBase<DerivedC> & C)
  97. {
  98. auto r = A-C;
  99. auto s = B-C;
  100. return r(0)*s(1) - r(1)*s(0);
  101. }
  102. template <typename Derivedl, typename DeriveddblA>
  103. IGL_INLINE void igl::doublearea(
  104. const Eigen::PlainObjectBase<Derivedl> & ul,
  105. Eigen::PlainObjectBase<DeriveddblA> & dblA)
  106. {
  107. using namespace Eigen;
  108. using namespace std;
  109. // Only support triangles
  110. assert(ul.cols() == 3);
  111. // Number of triangles
  112. const int m = ul.rows();
  113. Eigen::PlainObjectBase<Derivedl> l;
  114. MatrixXi _;
  115. sort(ul,2,false,l,_);
  116. // semiperimeters
  117. Matrix<typename Derivedl::Scalar,Dynamic,1> s = l.rowwise().sum()*0.5;
  118. assert(s.rows() == m);
  119. // resize output
  120. dblA.resize(l.rows(),1);
  121. for(int i = 0;i<m;i++)
  122. {
  123. //// Heron's formula for area
  124. //const typename Derivedl::Scalar arg =
  125. // s(i)*(s(i)-l(i,0))*(s(i)-l(i,1))*(s(i)-l(i,2));
  126. //assert(arg>=0);
  127. //dblA(i) = 2.0*sqrt(arg);
  128. // Kahan's Heron's formula
  129. const typename Derivedl::Scalar arg =
  130. (l(i,0)+(l(i,1)+l(i,2)))*
  131. (l(i,2)-(l(i,0)-l(i,1)))*
  132. (l(i,2)+(l(i,0)-l(i,1)))*
  133. (l(i,0)+(l(i,1)-l(i,2)));
  134. dblA(i) = 2.0*0.25*sqrt(arg);
  135. assert( l(i,2) - (l(i,0)-l(i,1)) && "FAILED KAHAN'S ASSERTION");
  136. assert(dblA(i) == dblA(i) && "DOUBLEAREA() PRODUCED NaN");
  137. }
  138. }
  139. #ifdef IGL_STATIC_LIBRARY
  140. // Explicit template specialization
  141. // generated by autoexplicit.sh
  142. 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> >&);
  143. // generated by autoexplicit.sh
  144. 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> >&);
  145. template void igl::doublearea<Eigen::Matrix<double, -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<double, -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> >&);
  146. 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> >&);
  147. 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> >&);
  148. 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> >&);
  149. 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> >&);
  150. 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&);
  151. #endif