flood_fill.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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 "flood_fill.h"
  9. #include <limits>
  10. template <typename Derivedres, typename DerivedS>
  11. IGL_INLINE void igl::flood_fill(
  12. const Eigen::MatrixBase<Derivedres>& res,
  13. Eigen::PlainObjectBase<DerivedS> & S)
  14. {
  15. typedef typename DerivedS::Scalar Scalar;
  16. const auto flood = [&res,&S] (
  17. const int xi,
  18. const int yi,
  19. const int zi,
  20. const int signed_xi,
  21. const int signed_yi,
  22. const int signed_zi,
  23. const Scalar s)
  24. {
  25. // flood fill this value back on this row
  26. for(int bxi = xi;signed_xi<--bxi;)
  27. {
  28. S(bxi+int(res(0))*(yi + int(res(1))*zi)) = s;
  29. }
  30. // flood fill this value back on any previous rows
  31. for(int byi = yi;signed_yi<--byi;)
  32. {
  33. for(int xi = 0;xi<res(0);xi++)
  34. {
  35. S(xi+int(res(0))*(byi + int(res(1))*zi)) = s;
  36. }
  37. }
  38. // flood fill this value back on any previous "sheets"
  39. for(int bzi = zi;signed_zi<--bzi;)
  40. {
  41. for(int yi = 0;yi<res(1);yi++)
  42. {
  43. for(int xi = 0;xi<res(0);xi++)
  44. {
  45. S(xi+int(res(0))*(yi + int(res(1))*bzi)) = s;
  46. }
  47. }
  48. }
  49. };
  50. int signed_zi = -1;
  51. Scalar s = std::numeric_limits<Scalar>::quiet_NaN();
  52. for(int zi = 0;zi<res(2);zi++)
  53. {
  54. int signed_yi = -1;
  55. if(zi != 0)
  56. {
  57. s = S(0+int(res(0))*(0 + int(res(1))*(zi-1)));
  58. }
  59. for(int yi = 0;yi<res(1);yi++)
  60. {
  61. // index of last signed item on this row
  62. int signed_xi = -1;
  63. if(yi != 0)
  64. {
  65. s = S(0+int(res(0))*(yi-1 + int(res(1))*zi));
  66. }
  67. for(int xi = 0;xi<res(0);xi++)
  68. {
  69. int i = xi+res(0)*(yi + res(1)*zi);
  70. if(S(i)!=S(i))
  71. {
  72. if(s == s)
  73. {
  74. S(i) = s;
  75. }
  76. continue;
  77. }
  78. s = S(i);
  79. flood(xi,yi,zi,signed_xi,signed_yi,signed_zi,s);
  80. // remember this position
  81. signed_xi = xi;
  82. signed_yi = yi;
  83. signed_zi = zi;
  84. }
  85. }
  86. }
  87. }
  88. #ifdef IGL_STATIC_LIBRARY
  89. // Explicit template instantiation
  90. // generated by autoexplicit.sh
  91. template void igl::flood_fill<Eigen::Matrix<int, 1, 3, 1, 1, 3>, Eigen::Matrix<float, -1, 1, 0, -1, 1> >(Eigen::MatrixBase<Eigen::Matrix<int, 1, 3, 1, 1, 3> > const&, Eigen::PlainObjectBase<Eigen::Matrix<float, -1, 1, 0, -1, 1> >&);
  92. // generated by autoexplicit.sh
  93. template void igl::flood_fill<Eigen::Matrix<int, 3, 1, 0, 3, 1>, Eigen::Matrix<float, -1, 1, 0, -1, 1> >(Eigen::MatrixBase<Eigen::Matrix<int, 3, 1, 0, 3, 1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<float, -1, 1, 0, -1, 1> >&);
  94. // generated by autoexplicit.sh
  95. template void igl::flood_fill<Eigen::Matrix<int, 3, 1, 0, 3, 1>, Eigen::Matrix<double, -1, 1, 0, -1, 1> >(Eigen::MatrixBase<Eigen::Matrix<int, 3, 1, 0, 3, 1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, 1, 0, -1, 1> >&);
  96. // generated by autoexplicit.sh
  97. template void igl::flood_fill<Eigen::Matrix<int, 1, 3, 1, 1, 3>, Eigen::Matrix<double, -1, 1, 0, -1, 1> >(Eigen::MatrixBase<Eigen::Matrix<int, 1, 3, 1, 1, 3> > const&, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, 1, 0, -1, 1> >&);
  98. template void igl::flood_fill<Eigen::Matrix<float, 1, 3, 1, 1, 3>, Eigen::Matrix<float, -1, 1, 0, -1, 1> >(Eigen::MatrixBase<Eigen::Matrix<float, 1, 3, 1, 1, 3> > const&, Eigen::PlainObjectBase<Eigen::Matrix<float, -1, 1, 0, -1, 1> >&);
  99. #endif