quad_grid.cpp 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. // This file is part of libigl, a simple c++ geometry processing library.
  2. //
  3. // Copyright (C) 2019 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 "quad_grid.h"
  9. #include "grid.h"
  10. template<
  11. typename DerivedV,
  12. typename DerivedQ,
  13. typename DerivedE>
  14. IGL_INLINE void igl::quad_grid(
  15. const int nx,
  16. const int ny,
  17. Eigen::PlainObjectBase<DerivedV> & V,
  18. Eigen::PlainObjectBase<DerivedQ> & Q,
  19. Eigen::PlainObjectBase<DerivedE> & E)
  20. {
  21. grid(Eigen::Vector2i(nx,ny),V);
  22. return igl::quad_grid(nx,ny,Q,E);
  23. }
  24. template<
  25. typename DerivedQ,
  26. typename DerivedE>
  27. IGL_INLINE void igl::quad_grid(
  28. const int nx,
  29. const int ny,
  30. Eigen::PlainObjectBase<DerivedQ> & Q,
  31. Eigen::PlainObjectBase<DerivedE> & E)
  32. {
  33. Eigen::MatrixXi I(nx,ny);
  34. Q.resize( (nx-1)*(ny-1),4);
  35. E.resize((nx-1)*ny + (ny-1)*nx,2);
  36. {
  37. int v = 0;
  38. int q = 0;
  39. int e = 0;
  40. // Ordered to match igl::grid
  41. for(int y = 0;y<ny;y++)
  42. {
  43. for(int x = 0;x<nx;x++)
  44. {
  45. //// Add a vertex
  46. //V(v,0) = (-1.0) + double(x)/double(nx-1) * (1.0 - (-1.0));
  47. //V(v,2) = (-1.0) + double(y)/double(ny-1) * (1.0 - (-1.0));
  48. I(x,y) = v;
  49. v++;
  50. // Add a verical edge
  51. if(y>0)
  52. {
  53. E(e,0) = I(x,y);
  54. E(e,1) = I(x,y-1);
  55. e++;
  56. }
  57. // Add a horizontal edge
  58. if(x>0)
  59. {
  60. E(e,0) = I(x,y);
  61. E(e,1) = I(x-1,y);
  62. e++;
  63. }
  64. // Add two triangles
  65. if(x>0 && y>0)
  66. {
  67. // -1,0----0,0
  68. // | / |
  69. // | / |
  70. // | / |
  71. // | / |
  72. // -1,-1---0,-1
  73. Q(q,0) = I(x-0,y-0);
  74. Q(q,1) = I(x-1,y-0);
  75. Q(q,2) = I(x-1,y-1);
  76. Q(q,3) = I(x-0,y-1);
  77. q++;
  78. //F(f,2) = I(x-0,y-0);
  79. //F(f,1) = I(x-1,y-0);
  80. //F(f,0) = I(x-1,y-1);
  81. //f++;
  82. //F(f,2) = I(x-0,y-0);
  83. //F(f,1) = I(x-1,y-1);
  84. //F(f,0) = I(x-0,y-1);
  85. //f++;
  86. }
  87. }
  88. }
  89. }
  90. }
  91. #ifdef IGL_STATIC_LIBRARY
  92. // Explicit template instantiation
  93. template void igl::quad_grid<Eigen::Matrix<double, -1, -1, 0, -1, -1>, Eigen::Matrix<int, -1, -1, 0, -1, -1>, Eigen::Matrix<int, -1, -1, 0, -1, -1> >(int, int, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> >&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> >&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> >&);
  94. #endif