barycentric_coordinates.cpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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 "barycentric_coordinates.h"
  9. #include "volume.h"
  10. #include "doublearea.h"
  11. template <
  12. typename DerivedP,
  13. typename DerivedA,
  14. typename DerivedB,
  15. typename DerivedC,
  16. typename DerivedD,
  17. typename DerivedL>
  18. IGL_INLINE void igl::barycentric_coordinates(
  19. const Eigen::PlainObjectBase<DerivedP> & P,
  20. const Eigen::PlainObjectBase<DerivedA> & A,
  21. const Eigen::PlainObjectBase<DerivedB> & B,
  22. const Eigen::PlainObjectBase<DerivedC> & C,
  23. const Eigen::PlainObjectBase<DerivedD> & D,
  24. Eigen::PlainObjectBase<DerivedL> & L)
  25. {
  26. using namespace Eigen;
  27. assert(P.cols() == 3 && "query must be in 3d");
  28. assert(A.cols() == 3 && "corners must be in 3d");
  29. assert(B.cols() == 3 && "corners must be in 3d");
  30. assert(C.cols() == 3 && "corners must be in 3d");
  31. assert(D.cols() == 3 && "corners must be in 3d");
  32. assert(P.rows() == A.rows() && "Must have same number of queries as corners");
  33. assert(A.rows() == B.rows() && "Corners must be same size");
  34. assert(A.rows() == C.rows() && "Corners must be same size");
  35. assert(A.rows() == D.rows() && "Corners must be same size");
  36. typedef Matrix<typename DerivedL::Scalar,DerivedL::RowsAtCompileTime,1>
  37. VectorXS;
  38. // Total volume
  39. VectorXS vol,LA,LB,LC,LD;
  40. volume(B,D,C,P,LA);
  41. volume(A,C,D,P,LB);
  42. volume(A,D,B,P,LC);
  43. volume(A,B,C,P,LD);
  44. volume(A,B,C,D,vol);
  45. L.resize(P.rows(),4);
  46. L<<LA,LB,LC,LD;
  47. L.array().colwise() /= vol.array();
  48. }
  49. template <
  50. typename DerivedP,
  51. typename DerivedA,
  52. typename DerivedB,
  53. typename DerivedC,
  54. typename DerivedL>
  55. IGL_INLINE void igl::barycentric_coordinates(
  56. const Eigen::MatrixBase<DerivedP> & P,
  57. const Eigen::MatrixBase<DerivedA> & A,
  58. const Eigen::MatrixBase<DerivedB> & B,
  59. const Eigen::MatrixBase<DerivedC> & C,
  60. Eigen::PlainObjectBase<DerivedL> & L)
  61. {
  62. using namespace Eigen;
  63. #ifndef NDEBUG
  64. const int DIM = P.cols();
  65. assert(A.cols() == DIM && "corners must be in same dimension as query");
  66. assert(B.cols() == DIM && "corners must be in same dimension as query");
  67. assert(C.cols() == DIM && "corners must be in same dimension as query");
  68. assert(P.rows() == A.rows() && "Must have same number of queries as corners");
  69. assert(A.rows() == B.rows() && "Corners must be same size");
  70. assert(A.rows() == C.rows() && "Corners must be same size");
  71. #endif
  72. // http://gamedev.stackexchange.com/a/23745
  73. typedef
  74. Eigen::Array<
  75. typename DerivedP::Scalar,
  76. DerivedP::RowsAtCompileTime,
  77. DerivedP::ColsAtCompileTime>
  78. ArrayS;
  79. typedef
  80. Eigen::Array<
  81. typename DerivedP::Scalar,
  82. DerivedP::RowsAtCompileTime,
  83. 1>
  84. VectorS;
  85. const ArrayS v0 = B.array() - A.array();
  86. const ArrayS v1 = C.array() - A.array();
  87. const ArrayS v2 = P.array() - A.array();
  88. VectorS d00 = (v0*v0).rowwise().sum();
  89. VectorS d01 = (v0*v1).rowwise().sum();
  90. VectorS d11 = (v1*v1).rowwise().sum();
  91. VectorS d20 = (v2*v0).rowwise().sum();
  92. VectorS d21 = (v2*v1).rowwise().sum();
  93. VectorS denom = d00 * d11 - d01 * d01;
  94. L.resize(P.rows(),3);
  95. L.col(1) = (d11 * d20 - d01 * d21) / denom;
  96. L.col(2) = (d00 * d21 - d01 * d20) / denom;
  97. L.col(0) = 1.0f -(L.col(1) + L.col(2)).array();
  98. }
  99. #ifdef IGL_STATIC_LIBRARY
  100. template void igl::barycentric_coordinates<Eigen::Matrix<double, -1, -1, 0, -1, -1>, Eigen::Matrix<double, -1, -1, 0, -1, -1>, Eigen::Matrix<double, -1, -1, 0, -1, -1>, Eigen::Matrix<double, -1, -1, 0, -1, -1>, 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> > const&, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> >&);
  101. template void igl::barycentric_coordinates<Eigen::Matrix<double, 1, 3, 1, 1, 3>, Eigen::Matrix<double, 1, 3, 1, 1, 3>, Eigen::Matrix<double, 1, 3, 1, 1, 3>, Eigen::Matrix<double, 1, 3, 1, 1, 3>, Eigen::Matrix<double, 1, 3, 1, 1, 3> >(Eigen::MatrixBase<Eigen::Matrix<double, 1, 3, 1, 1, 3> > const&, Eigen::MatrixBase<Eigen::Matrix<double, 1, 3, 1, 1, 3> > const&, Eigen::MatrixBase<Eigen::Matrix<double, 1, 3, 1, 1, 3> > const&, Eigen::MatrixBase<Eigen::Matrix<double, 1, 3, 1, 1, 3> > const&, Eigen::PlainObjectBase<Eigen::Matrix<double, 1, 3, 1, 1, 3> >&);
  102. template void igl::barycentric_coordinates<Eigen::Matrix<double, 1, 2, 1, 1, 2>, Eigen::Block<Eigen::Matrix<double, -1, -1, 0, -1, -1> const, 1, -1, false>, Eigen::Block<Eigen::Matrix<double, -1, -1, 0, -1, -1> const, 1, -1, false>, Eigen::Block<Eigen::Matrix<double, -1, -1, 0, -1, -1> const, 1, -1, false>, Eigen::Matrix<double, 1, 3, 1, 1, 3> >(Eigen::MatrixBase<Eigen::Matrix<double, 1, 2, 1, 1, 2> > const&, Eigen::MatrixBase<Eigen::Block<Eigen::Matrix<double, -1, -1, 0, -1, -1> const, 1, -1, false> > const&, Eigen::MatrixBase<Eigen::Block<Eigen::Matrix<double, -1, -1, 0, -1, -1> const, 1, -1, false> > const&, Eigen::MatrixBase<Eigen::Block<Eigen::Matrix<double, -1, -1, 0, -1, -1> const, 1, -1, false> > const&, Eigen::PlainObjectBase<Eigen::Matrix<double, 1, 3, 1, 1, 3> >&);
  103. template void igl::barycentric_coordinates<Eigen::Matrix<double, 1, 3, 1, 1, 3>, Eigen::Block<Eigen::Matrix<double, -1, -1, 0, -1, -1> const, 1, -1, false>, Eigen::Block<Eigen::Matrix<double, -1, -1, 0, -1, -1> const, 1, -1, false>, Eigen::Block<Eigen::Matrix<double, -1, -1, 0, -1, -1> const, 1, -1, false>, Eigen::Matrix<double, 1, 3, 1, 1, 3> >(Eigen::MatrixBase<Eigen::Matrix<double, 1, 3, 1, 1, 3> > const&, Eigen::MatrixBase<Eigen::Block<Eigen::Matrix<double, -1, -1, 0, -1, -1> const, 1, -1, false> > const&, Eigen::MatrixBase<Eigen::Block<Eigen::Matrix<double, -1, -1, 0, -1, -1> const, 1, -1, false> > const&, Eigen::MatrixBase<Eigen::Block<Eigen::Matrix<double, -1, -1, 0, -1, -1> const, 1, -1, false> > const&, Eigen::PlainObjectBase<Eigen::Matrix<double, 1, 3, 1, 1, 3> >&);
  104. template void igl::barycentric_coordinates<Eigen::Matrix<double, -1, -1, 0, -1, -1>, Eigen::Matrix<double, -1, -1, 0, -1, -1>, Eigen::Matrix<double, -1, -1, 0, -1, -1>, Eigen::Matrix<double, -1, -1, 0, -1, -1>, Eigen::Matrix<double, -1, -1, 0, -1, -1> >(Eigen::MatrixBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> > const&, Eigen::MatrixBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> > const&, Eigen::MatrixBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> > const&, Eigen::MatrixBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> >&);
  105. #endif