sort_triangles.cpp 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. // This file is part of libigl, a simple c++ geometry processing library.
  2. //
  3. // Copyright (C) 2013 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 "sort_triangles.h"
  9. #include "project.h"
  10. #include "../sort_triangles.h"
  11. #include "../opengl/OpenGL_convenience.h"
  12. #include "../sort.h"
  13. #include "../slice.h"
  14. #include "../barycenter.h"
  15. #include <iostream>
  16. template <
  17. typename DerivedV,
  18. typename DerivedF,
  19. typename DerivedFF,
  20. typename DerivedI>
  21. void igl::opengl2::sort_triangles(
  22. const Eigen::PlainObjectBase<DerivedV> & V,
  23. const Eigen::PlainObjectBase<DerivedF> & F,
  24. Eigen::PlainObjectBase<DerivedFF> & FF,
  25. Eigen::PlainObjectBase<DerivedI> & I)
  26. {
  27. using namespace Eigen;
  28. using namespace std;
  29. // Put model, projection, and viewport matrices into double arrays
  30. Matrix4d MV;
  31. Matrix4d P;
  32. glGetDoublev(GL_MODELVIEW_MATRIX, MV.data());
  33. glGetDoublev(GL_PROJECTION_MATRIX, P.data());
  34. if(V.cols() == 3)
  35. {
  36. Matrix<typename DerivedV::Scalar, DerivedV::RowsAtCompileTime,4> hV;
  37. hV.resize(V.rows(),4);
  38. hV.block(0,0,V.rows(),V.cols()) = V;
  39. hV.col(3).setConstant(1);
  40. return igl::sort_triangles(hV,F,MV,P,FF,I);
  41. }else
  42. {
  43. return igl::sort_triangles(V,F,MV,P,FF,I);
  44. }
  45. }
  46. template <
  47. typename DerivedV,
  48. typename DerivedF,
  49. typename DerivedFF,
  50. typename DerivedI>
  51. void igl::opengl2::sort_triangles_slow(
  52. const Eigen::PlainObjectBase<DerivedV> & V,
  53. const Eigen::PlainObjectBase<DerivedF> & F,
  54. Eigen::PlainObjectBase<DerivedFF> & FF,
  55. Eigen::PlainObjectBase<DerivedI> & I)
  56. {
  57. using namespace Eigen;
  58. using namespace std;
  59. // Barycenter, centroid
  60. Eigen::Matrix<typename DerivedV::Scalar, DerivedF::RowsAtCompileTime,1> D,sD;
  61. Eigen::Matrix<typename DerivedV::Scalar, DerivedF::RowsAtCompileTime,3> BC;
  62. D.resize(F.rows(),3);
  63. barycenter(V,F,BC);
  64. for(int f = 0;f<F.rows();f++)
  65. {
  66. Eigen::Matrix<typename DerivedV::Scalar, 3,1> bc,pbc;
  67. bc = BC.row(f);
  68. project(bc,pbc);
  69. D(f) = pbc(2);
  70. }
  71. sort(D,1,false,sD,I);
  72. slice(F,I,1,FF);
  73. }
  74. #ifdef IGL_STATIC_LIBRARY
  75. // Explicit template specialization
  76. template void igl::opengl2::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> >&);
  77. template void igl::opengl2::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> >&);
  78. #endif