barycentric_coordinates.cpp 8.5 KB

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