sort_triangles.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. #include "sort_triangles.h"
  2. #include "barycenter.h"
  3. #include "sort.h"
  4. #include "sortrows.h"
  5. #include "slice.h"
  6. #include "round.h"
  7. #include "colon.h"
  8. #include "matlab_format.h"
  9. #include "OpenGL_convenience.h"
  10. #include <iostream>
  11. template <
  12. typename DerivedV,
  13. typename DerivedF,
  14. typename DerivedMV,
  15. typename DerivedP,
  16. typename DerivedFF,
  17. typename DerivedI>
  18. IGL_INLINE void igl::sort_triangles(
  19. const Eigen::PlainObjectBase<DerivedV> & V,
  20. const Eigen::PlainObjectBase<DerivedF> & F,
  21. const Eigen::PlainObjectBase<DerivedMV> & MV,
  22. const Eigen::PlainObjectBase<DerivedP> & P,
  23. Eigen::PlainObjectBase<DerivedFF> & FF,
  24. Eigen::PlainObjectBase<DerivedI> & I)
  25. {
  26. using namespace Eigen;
  27. using namespace igl;
  28. using namespace std;
  29. // Barycenter, centroid
  30. Eigen::Matrix<typename DerivedV::Scalar, DerivedF::RowsAtCompileTime,1> D,sD;
  31. Eigen::Matrix<typename DerivedV::Scalar, DerivedF::RowsAtCompileTime,4> BC,PBC;
  32. barycenter(V,F,BC);
  33. D = BC*(MV.transpose()*P.transpose().eval().col(2));
  34. sort(D,1,false,sD,I);
  35. //// Closest corner
  36. //Eigen::Matrix<typename DerivedV::Scalar, DerivedF::RowsAtCompileTime,1> D,sD;
  37. //D.setConstant(F.rows(),1,-1e26);
  38. //for(int c = 0;c<3;c++)
  39. //{
  40. // Eigen::Matrix<typename DerivedV::Scalar, DerivedF::RowsAtCompileTime,4> C;
  41. // Eigen::Matrix<typename DerivedV::Scalar, DerivedF::RowsAtCompileTime,1> DC;
  42. // C.resize(F.rows(),4);
  43. // for(int f = 0;f<F.rows();f++)
  44. // {
  45. // C(f,0) = V(F(f,c),0);
  46. // C(f,1) = V(F(f,c),1);
  47. // C(f,2) = V(F(f,c),2);
  48. // C(f,3) = 1;
  49. // }
  50. // DC = C*(MV.transpose()*P.transpose().eval().col(2));
  51. // D = (DC.array()>D.array()).select(DC,D).eval();
  52. //}
  53. //sort(D,1,false,sD,I);
  54. //// Closest corner with tie breaks
  55. //Eigen::Matrix<typename DerivedV::Scalar, DerivedF::RowsAtCompileTime,3> D,sD,ssD;
  56. //D.resize(F.rows(),3);
  57. //for(int c = 0;c<3;c++)
  58. //{
  59. // Eigen::Matrix<typename DerivedV::Scalar, DerivedF::RowsAtCompileTime,4> C;
  60. // C.resize(F.rows(),4);
  61. // for(int f = 0;f<F.rows();f++)
  62. // {
  63. // C(f,0) = V(F(f,c),0);
  64. // C(f,1) = V(F(f,c),1);
  65. // C(f,2) = V(F(f,c),2);
  66. // C(f,3) = 1;
  67. // }
  68. // D.col(c) = C*(MV.transpose()*P.transpose().eval().col(2));
  69. //}
  70. //VectorXi _;
  71. //sort(D,2,false,sD,_);
  72. //sortrows(sD,false,ssD,I);
  73. slice(F,I,1,FF);
  74. }
  75. template <
  76. typename DerivedV,
  77. typename DerivedF,
  78. typename DerivedFF,
  79. typename DerivedI>
  80. void igl::sort_triangles(
  81. const Eigen::PlainObjectBase<DerivedV> & V,
  82. const Eigen::PlainObjectBase<DerivedF> & F,
  83. Eigen::PlainObjectBase<DerivedFF> & FF,
  84. Eigen::PlainObjectBase<DerivedI> & I)
  85. {
  86. using namespace Eigen;
  87. using namespace igl;
  88. using namespace std;
  89. // Put model, projection, and viewport matrices into double arrays
  90. Matrix4d MV;
  91. Matrix4d P;
  92. glGetDoublev(GL_MODELVIEW_MATRIX, MV.data());
  93. glGetDoublev(GL_PROJECTION_MATRIX, P.data());
  94. if(V.cols() == 3)
  95. {
  96. Matrix<typename DerivedV::Scalar, DerivedV::RowsAtCompileTime,4> hV;
  97. hV.resize(V.rows(),4);
  98. hV.block(0,0,V.rows(),V.cols()) = V;
  99. hV.col(3).setConstant(1);
  100. return sort_triangles(hV,F,MV,P,FF,I);
  101. }else
  102. {
  103. return sort_triangles(V,F,MV,P,FF,I);
  104. }
  105. }
  106. #include "project.h"
  107. #include <iostream>
  108. template <
  109. typename DerivedV,
  110. typename DerivedF,
  111. typename DerivedFF,
  112. typename DerivedI>
  113. void igl::sort_triangles_slow(
  114. const Eigen::PlainObjectBase<DerivedV> & V,
  115. const Eigen::PlainObjectBase<DerivedF> & F,
  116. Eigen::PlainObjectBase<DerivedFF> & FF,
  117. Eigen::PlainObjectBase<DerivedI> & I)
  118. {
  119. using namespace Eigen;
  120. using namespace igl;
  121. using namespace std;
  122. // Barycenter, centroid
  123. Eigen::Matrix<typename DerivedV::Scalar, DerivedF::RowsAtCompileTime,1> D,sD;
  124. Eigen::Matrix<typename DerivedV::Scalar, DerivedF::RowsAtCompileTime,3> BC;
  125. D.resize(F.rows(),3);
  126. barycenter(V,F,BC);
  127. for(int f = 0;f<F.rows();f++)
  128. {
  129. Eigen::Matrix<typename DerivedV::Scalar, 3,1> bc,pbc;
  130. bc = BC.row(f);
  131. project(bc,pbc);
  132. D(f) = pbc(2);
  133. }
  134. sort(D,1,false,sD,I);
  135. slice(F,I,1,FF);
  136. }
  137. #ifndef IGL_HEADER_ONLY
  138. // Explicit template instanciation
  139. template void igl::sort_triangles<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::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> >&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, 1, 0, -1, 1> >&);
  140. template void igl::sort_triangles_slow<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::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> >&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, 1, 0, -1, 1> >&);
  141. #endif