diff --git a/src/patches.cpp b/src/patches.cpp index 4ad970b29..669129b02 100644 --- a/src/patches.cpp +++ b/src/patches.cpp @@ -17,27 +17,87 @@ #include "spatRaster.h" -void patch_search(const std::vector &m, std::vector &patches, const int &i, const long &ncol, const int &patch, const size_t &dirs) { +void patch_search_wrap(const std::vector &m, std::vector &patches, const int &i, const long &ncol, const int &patch, const size_t &dirs) { // DFS - std::vector directions; + std::vector directions, dirfirst, dirlast; if (dirs==4) { directions = {-ncol, ncol, -1, 1}; + dirfirst = {-ncol, ncol, ncol-1, 1}; + dirlast = {-ncol, ncol, -1, 1-ncol}; } else { directions = {-ncol, ncol, -1, 1, -ncol-1, -ncol+1, ncol-1, ncol+1}; + dirfirst = {-ncol, ncol, ncol-1, 1, -1, -ncol+1, ncol+ncol-1, ncol+1}; + dirlast = {-ncol, ncol, -1, 1-ncol, -ncol-1, -ncol-ncol+1, ncol-1, 1}; } size_t ncell = m.size(); patches[i] = patch; - for (size_t d=0; d= 0 && j < ncell && (!std::isnan(m[j])) && std::isnan(patches[j]) && m[j] == m[i]) { - patch_search(m, patches, j, ncol, patch, dirs); - } - } + if ((i % ncol) == 0) { //firstcol + for (size_t d=0; d= 0 && j < ncell && (!std::isnan(m[j])) && std::isnan(patches[j]) && m[j] == m[i]) { + patch_search_wrap(m, patches, j, ncol, patch, dirs); + } + } + } else if (((i+1) % ncol) == 0) { // lastcol + for (size_t d=0; d= 0 && j < ncell && (!std::isnan(m[j])) && std::isnan(patches[j]) && m[j] == m[i]) { + patch_search_wrap(m, patches, j, ncol, patch, dirs); + } + } + } else { + for (size_t d=0; d= 0 && j < ncell && (!std::isnan(m[j])) && std::isnan(patches[j]) && m[j] == m[i]) { + patch_search_wrap(m, patches, j, ncol, patch, dirs); + } + } + } } +void patch_search(const std::vector &m, std::vector &patches, const int &i, const long &ncol, const int &patch, const size_t &dirs) { +// DFS + + std::vector directions, dirfirst, dirlast; + if (dirs==4) { + directions = {-ncol, ncol, -1, 1}; + dirfirst = {-ncol, ncol, 1}; + dirlast = {-ncol, ncol, -1}; + } else { + directions = {-ncol, ncol, -1, 1, -ncol-1, -ncol+1, ncol-1, ncol+1}; + dirfirst = {-ncol, ncol, 1, -ncol+1, ncol+1}; + dirlast = {-ncol, ncol, -1, -ncol-1, ncol-1}; + } + + size_t ncell = m.size(); + patches[i] = patch; + if ((i % ncol) == 0) { //firstcol + for (size_t d=0; d= 0 && j < ncell && (!std::isnan(m[j])) && std::isnan(patches[j]) && m[j] == m[i]) { + patch_search(m, patches, j, ncol, patch, dirs); + } + } + } else if (((i+1) % ncol) == 0) { // lastcol + for (size_t d=0; d= 0 && j < ncell && (!std::isnan(m[j])) && std::isnan(patches[j]) && m[j] == m[i]) { + patch_search(m, patches, j, ncol, patch, dirs); + } + } + } else { + for (size_t d=0; d= 0 && j < ncell && (!std::isnan(m[j])) && std::isnan(patches[j]) && m[j] == m[i]) { + patch_search(m, patches, j, ncol, patch, dirs); + } + } + } +} + SpatRaster SpatRaster::patches(size_t dirs, SpatOptions &opt) { SpatRaster out = geometry(1, false); @@ -101,13 +161,22 @@ SpatRaster SpatRaster::patches(size_t dirs, SpatOptions &opt) { if (!out.writeBlock(patches, i)) return out; } */ - std::vector patches(nrow() * nc, NAN); readValues(v, 0, nrow(), 0, nc); - for (size_t j=0; j