triangulated_grid.cpp 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. // This file is part of libigl, a simple c++ geometry processing library.
  2. //
  3. // Copyright (C) 2016 Alec Jacobson <[email protected]>
  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 "triangulated_grid.h"
  9. #include "grid.h"
  10. #include <cassert>
  11. template <
  12. typename XType,
  13. typename YType,
  14. typename DerivedGV,
  15. typename DerivedGF>
  16. IGL_INLINE void igl::triangulated_grid(
  17. const XType & nx,
  18. const YType & ny,
  19. Eigen::PlainObjectBase<DerivedGV> & GV,
  20. Eigen::PlainObjectBase<DerivedGF> & GF)
  21. {
  22. Eigen::Matrix<XType,2,1> res(nx,ny);
  23. igl::grid(res,GV);
  24. return igl::triangulated_grid(nx,ny,GF);
  25. };
  26. template <
  27. typename XType,
  28. typename YType,
  29. typename DerivedGF>
  30. IGL_INLINE void igl::triangulated_grid(
  31. const XType & nx,
  32. const YType & ny,
  33. Eigen::PlainObjectBase<DerivedGF> & GF)
  34. {
  35. GF.resize((nx-1)*(ny-1)*2,3);
  36. for(int y = 0;y<ny-1;y++)
  37. {
  38. for(int x = 0;x<nx-1;x++)
  39. {
  40. // index of southwest corner
  41. const XType sw = (x +nx*(y+0));
  42. const XType se = (x+1+nx*(y+0));
  43. const XType ne = (x+1+nx*(y+1));
  44. const XType nw = (x +nx*(y+1));
  45. // Index of first triangle in this square
  46. const XType gf = 2*(x+(nx-1)*y);
  47. GF(gf+0,0) = sw;
  48. GF(gf+0,1) = se;
  49. GF(gf+0,2) = nw;
  50. GF(gf+1,0) = se;
  51. GF(gf+1,1) = ne;
  52. GF(gf+1,2) = nw;
  53. }
  54. }
  55. }
  56. #ifdef IGL_STATIC_LIBRARY
  57. // Explicit template instantiation
  58. // generated by autoexplicit.sh
  59. template void igl::triangulated_grid<int, int, Eigen::Matrix<double, -1, -1, 0, -1, -1>, Eigen::Matrix<int, -1, -1, 0, -1, -1> >(int const&, int const&, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> >&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> >&);
  60. template void igl::triangulated_grid<int, int, Eigen::Matrix<int, -1, -1, 0, -1, -1>>(int const&, int const&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, -1, 0, -1, -1>>&);
  61. #endif