barycentric_coordinates.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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::PlainObjectBase<DerivedP> & P,
  57. const Eigen::PlainObjectBase<DerivedA> & A,
  58. const Eigen::PlainObjectBase<DerivedB> & B,
  59. const Eigen::PlainObjectBase<DerivedC> & C,
  60. Eigen::PlainObjectBase<DerivedL> & L)
  61. {
  62. using namespace Eigen;
  63. assert(P.cols() == 2 && "query must be in 2d");
  64. assert(A.cols() == 2 && "corners must be in 2d");
  65. assert(B.cols() == 2 && "corners must be in 2d");
  66. assert(C.cols() == 2 && "corners must be in 2d");
  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. typedef Matrix<typename DerivedL::Scalar,DerivedL::RowsAtCompileTime,1>
  71. VectorXS;
  72. // Total area
  73. VectorXS dblA,LA,LB,LC;
  74. doublearea(P,B,C,LA);
  75. doublearea(A,P,C,LB);
  76. doublearea(A,B,P,LC);
  77. doublearea(A,B,C,dblA);
  78. L.resize(P.rows(),3);
  79. L<<LA,LB,LC;
  80. L.array().colwise() /= dblA.array();
  81. }
  82. #ifdef IGL_STATIC_LIBRARY
  83. 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> >&);
  84. #endif