Skip to content

Commit

Permalink
update linedrawing for lineslopes >1
Browse files Browse the repository at this point in the history
  • Loading branch information
Joeperdefloep committed Jun 2, 2022
1 parent 292a0ba commit b4a6363
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 21 deletions.
42 changes: 31 additions & 11 deletions include/richdem/methods/catchment_delineation_generic.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,23 +49,43 @@ std::queue<GridCell> queue_from_linepoints(
int deltay = y1-y0;
float error = 0;
float deltaerr = (float)deltay/(float)deltax;
std::cout << "linedrawing deltaerr:" << deltaerr << std::endl;

if (deltaerr<0)
deltaerr = -deltaerr;

RDLOG_MISC<<"Line slope is "<<deltaerr;
int y=y0;
for(int x=x0;x<=x1;x++){
expansion.push(GridCell(x,y));
upslope_cells(x,y)=2;
error+=deltaerr;
if (error>=0.5) {
expansion.push(GridCell(x+1,y));
upslope_cells(x+1,y) = 2;
y += sgn(deltay);
error -= 1;
//if the slope is larger than 1, loop over y?
if (deltaerr<1){
int y=y0;
for(int x=x0;x<=x1;x++){
expansion.push(GridCell(x,y));
upslope_cells(x,y)=2;
error+=deltaerr;
if (error>=0.5) {
expansion.push(GridCell(x+1,y));
upslope_cells(x+1,y) = 2;
y += sgn(deltay);
error -= 1;
}
}
}
} else {
// deltaerr >1
deltaerr = std::abs((float)deltax/(float)deltay);
int x=x0;
for(int y=y0;y<=y1;y++){
expansion.push(GridCell(x,y));
upslope_cells(x,y)=2;
std::cout<< "(" << x << "," << y << ")\n";
error+=deltaerr;
if (error>=0.5) {
expansion.push(GridCell(x,y+1));
upslope_cells(x,y+1) = 2;
y += sgn(deltay);
error -= 1;
}
}
}

return expansion;
}
Expand Down
Binary file removed tests/richdem_unittests
Binary file not shown.
19 changes: 9 additions & 10 deletions tests/tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -505,7 +505,7 @@ TEST_CASE("Checking Catchment Delineation Generic"){
SUBCASE("line"){
int x0=0;
int y0=0;
int x1=2;
int x1=0;
int y1=4;

std::queue<GridCell> q_expected;
Expand All @@ -515,24 +515,29 @@ TEST_CASE("Checking Catchment Delineation Generic"){
q_expected.emplace(0,3);
q_expected.emplace(0,4);

const Array2D<uint8_t> upslope_e = {
Array2D<uint8_t> upslope_e = {
{2, 0, 0, 0, 0, 0},
{2, 0, 0, 0, 0, 0},
{2, 0, 0, 0, 0, 0},
{2, 0, 0, 0, 0, 0},
{2, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0},
};
upslope_e.setNoData(0);
Array2D<uint8_t> upslope(upslope_e,0);
upslope.setNoData(0);

// upslope_e.printAll();
// printAll not working, because we need to cast to unsigned() from uint8_t
std::queue<GridCell> q = queue_from_linepoints(x0,y0,x1,y1,upslope);
for (int y = 0; y< upslope.width(); y++){
for (int x = 0; x<upslope.height(); x++){
std::cout << unsigned(upslope(x,y)) << " ";
}
std::cout << std::endl;
}
// REQUIRE_EQ(q, q_expected);

CHECK(upslope.height() == upslope_e.height());
CHECK(upslope.width() == upslope_e.width());
CHECK(upslope == upslope_e);
}

Expand Down Expand Up @@ -561,12 +566,6 @@ TEST_CASE("Checking Catchment Delineation Generic"){
result.setNoData(0);
expected.setNoData(0); //the function sets nodata to 0

for (int y = 0; y< result.width(); y++){
for (int x = 0; x<result.height(); x++){
std::cout << unsigned(result(x,y)) << " ";
}
std::cout << std::endl;
}
upslope_cells_mflow(expansion, dem, result);
CHECK(result == expected);
}
Expand Down

0 comments on commit b4a6363

Please sign in to comment.