volume.cpp 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. // This file is part of libigl, a simple c++ geometry processing library.
  2. //
  3. // Copyright (C) 2014 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 "volume.h"
  9. template <
  10. typename DerivedV,
  11. typename DerivedT,
  12. typename Derivedvol>
  13. IGL_INLINE void igl::volume(
  14. Eigen::PlainObjectBase<DerivedV>& V,
  15. Eigen::PlainObjectBase<DerivedT>& T,
  16. Eigen::PlainObjectBase<Derivedvol>& vol)
  17. {
  18. using namespace Eigen;
  19. const int m = T.rows();
  20. vol.resize(m,1);
  21. for(int t = 0;t<m;t++)
  22. {
  23. const auto & a = V.row(T(t,0));
  24. const auto & b = V.row(T(t,1));
  25. const auto & c = V.row(T(t,2));
  26. const auto & d = V.row(T(t,3));
  27. vol(t) = -(a-d).dot((b-d).cross(c-d))/6.;
  28. }
  29. }
  30. template <
  31. typename DerivedL,
  32. typename Derivedvol>
  33. IGL_INLINE void igl::volume(
  34. Eigen::PlainObjectBase<DerivedL>& L,
  35. Eigen::PlainObjectBase<Derivedvol>& vol)
  36. {
  37. using namespace Eigen;
  38. const int m = L.rows();
  39. typedef typename Derivedvol::Scalar ScalarS;
  40. vol.resize(m,1);
  41. for(int t = 0;t<m;t++)
  42. {
  43. const ScalarS u = L(t,0);
  44. const ScalarS v = L(t,1);
  45. const ScalarS w = L(t,2);
  46. const ScalarS U = L(t,3);
  47. const ScalarS V = L(t,4);
  48. const ScalarS W = L(t,5);
  49. const ScalarS X = (w - U + v)*(U + v + w);
  50. const ScalarS x = (U - v + w)*(v - w + U);
  51. const ScalarS Y = (u - V + w)*(V + w + u);
  52. const ScalarS y = (V - w + u)*(w - u + V);
  53. const ScalarS Z = (v - W + u)*(W + u + v);
  54. const ScalarS z = (W - u + v)*(u - v + W);
  55. const ScalarS a = sqrt(x*Y*Z);
  56. const ScalarS b = sqrt(y*Z*X);
  57. const ScalarS c = sqrt(z*X*Y);
  58. const ScalarS d = sqrt(x*y*z);
  59. vol(t) = sqrt(
  60. (-a + b + c + d)*
  61. ( a - b + c + d)*
  62. ( a + b - c + d)*
  63. ( a + b + c - d))/
  64. (192.*u*v*w);
  65. }
  66. }
  67. #ifndef IGL_HEADER_ONLY
  68. // Explicit template specialization
  69. // generated by autoexplicit.sh
  70. template void igl::volume<Eigen::Matrix<double, -1, 6, 0, -1, 6>, Eigen::Matrix<double, -1, 1, 0, -1, 1> >(Eigen::PlainObjectBase<Eigen::Matrix<double, -1, 6, 0, -1, 6> >&, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, 1, 0, -1, 1> >&);
  71. #endif