Skip to content

Commit

Permalink
r.random.cells: treat distance as minimum distance (#1797)
Browse files Browse the repository at this point in the history
* r.random.cells: treat distance as minimum distance
* r.random.cells: adjust test cases
* r.random.cells: format test_random_cells.py
  • Loading branch information
metzm authored and neteler committed Jan 12, 2022
1 parent 00870f9 commit f533a10
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 11 deletions.
16 changes: 8 additions & 8 deletions raster/r.random.cells/indep.c
Expand Up @@ -34,7 +34,7 @@ void Indep(void)
Out[DRow][DCol] = ++Found;
for (R = DRow; R < Rs; R++) {
RowDist = NS * (R - DRow);
if (RowDist > MaxDistSq) {
if (RowDist >= MaxDistSq) {
R = Rs;
}
else {
Expand All @@ -45,7 +45,7 @@ void Indep(void)
G_debug(3, "(ColDist):%.12lf", ColDist);
G_debug(3, "(MaxDistSq):%.12lf", MaxDistSq);

if (MaxDistSq >= RowDistSq + ColDist * ColDist) {
if (MaxDistSq > RowDistSq + ColDist * ColDist) {
if (0 != FlagGet(Cells, R, C)) {
G_debug(2, "unset()");
FLAG_UNSET(Cells, R, C);
Expand All @@ -62,14 +62,14 @@ void Indep(void)
G_debug(2, "it1()");
for (R = DRow - 1; R >= 0; R--) {
RowDist = NS * (DRow - R);
if (RowDist > MaxDistSq) {
if (RowDist >= MaxDistSq) {
R = 0;
}
else {
RowDistSq = RowDist * RowDist;
for (C = DCol; C < Cs; C++) {
ColDist = EW * (C - DCol);
if (MaxDistSq >= RowDistSq + ColDist * ColDist) {
if (MaxDistSq > RowDistSq + ColDist * ColDist) {
if (0 != FlagGet(Cells, R, C)) {
G_debug(2, "unset()");
FLAG_UNSET(Cells, R, C);
Expand All @@ -86,14 +86,14 @@ void Indep(void)
G_debug(2, "it2()");
for (R = DRow; R < Rs; R++) {
RowDist = NS * (R - DRow);
if (RowDist > MaxDistSq) {
if (RowDist >= MaxDistSq) {
R = Rs;
}
else {
RowDistSq = RowDist * RowDist;
for (C = DCol - 1; C >= 0; C--) {
ColDist = EW * (DCol - C);
if (MaxDistSq >= RowDistSq + ColDist * ColDist) {
if (MaxDistSq > RowDistSq + ColDist * ColDist) {
if (0 != FlagGet(Cells, R, C)) {
G_debug(2, "unset()");
FLAG_UNSET(Cells, R, C);
Expand All @@ -110,14 +110,14 @@ void Indep(void)
G_debug(2, "it3()");
for (R = DRow - 1; R >= 0; R--) {
RowDist = NS * (DRow - R);
if (RowDist > MaxDistSq) {
if (RowDist >= MaxDistSq) {
R = 0;
}
else {
RowDistSq = RowDist * RowDist;
for (C = DCol - 1; C >= 0; C--) {
ColDist = EW * (DCol - C);
if (MaxDistSq >= RowDistSq + ColDist * ColDist) {
if (MaxDistSq > RowDistSq + ColDist * ColDist) {
if (0 != FlagGet(Cells, R, C)) {
G_debug(2, "unset()");
FLAG_UNSET(Cells, R, C);
Expand Down
4 changes: 2 additions & 2 deletions raster/r.random.cells/init.c
Expand Up @@ -73,8 +73,8 @@ void Init()
G_debug(1, "(CellCount):%d", CellCount);

sscanf(Distance->answer, "%lf", &MaxDist);
if (MaxDist < 0.0)
G_fatal_error(_("Distance must be >= 0.0"));
if (MaxDist <= 0.0)
G_fatal_error(_("Distance must be > 0.0"));

G_debug(3, "(MaxDist):%.12lf", MaxDist);
MaxDistSq = MaxDist * MaxDist;
Expand Down
4 changes: 3 additions & 1 deletion raster/r.random.cells/testsuite/test_random_cells.py
Expand Up @@ -61,7 +61,9 @@ def test_fill_all(self):
)

def test_fill_some(self):
self.assertModule("r.random.cells", output=self.some_rast, distance=2, seed=100)
self.assertModule(
"r.random.cells", output=self.some_rast, distance=2.00001, seed=100
)
self.to_remove.append(self.some_rast)
self.assertRasterFitsUnivar(
self.some_rast, reference=dict(cells=self.n_cells, min=1)
Expand Down

0 comments on commit f533a10

Please sign in to comment.