Skip to content

Commit b4a6363

Browse files
author
Joeperdefloep
committed
update linedrawing for lineslopes >1
1 parent 292a0ba commit b4a6363

File tree

3 files changed

+40
-21
lines changed

3 files changed

+40
-21
lines changed

include/richdem/methods/catchment_delineation_generic.hpp

+31-11
Original file line numberDiff line numberDiff line change
@@ -49,23 +49,43 @@ std::queue<GridCell> queue_from_linepoints(
4949
int deltay = y1-y0;
5050
float error = 0;
5151
float deltaerr = (float)deltay/(float)deltax;
52+
std::cout << "linedrawing deltaerr:" << deltaerr << std::endl;
5253

5354
if (deltaerr<0)
5455
deltaerr = -deltaerr;
5556

5657
RDLOG_MISC<<"Line slope is "<<deltaerr;
57-
int y=y0;
58-
for(int x=x0;x<=x1;x++){
59-
expansion.push(GridCell(x,y));
60-
upslope_cells(x,y)=2;
61-
error+=deltaerr;
62-
if (error>=0.5) {
63-
expansion.push(GridCell(x+1,y));
64-
upslope_cells(x+1,y) = 2;
65-
y += sgn(deltay);
66-
error -= 1;
58+
//if the slope is larger than 1, loop over y?
59+
if (deltaerr<1){
60+
int y=y0;
61+
for(int x=x0;x<=x1;x++){
62+
expansion.push(GridCell(x,y));
63+
upslope_cells(x,y)=2;
64+
error+=deltaerr;
65+
if (error>=0.5) {
66+
expansion.push(GridCell(x+1,y));
67+
upslope_cells(x+1,y) = 2;
68+
y += sgn(deltay);
69+
error -= 1;
70+
}
6771
}
68-
}
72+
} else {
73+
// deltaerr >1
74+
deltaerr = std::abs((float)deltax/(float)deltay);
75+
int x=x0;
76+
for(int y=y0;y<=y1;y++){
77+
expansion.push(GridCell(x,y));
78+
upslope_cells(x,y)=2;
79+
std::cout<< "(" << x << "," << y << ")\n";
80+
error+=deltaerr;
81+
if (error>=0.5) {
82+
expansion.push(GridCell(x,y+1));
83+
upslope_cells(x,y+1) = 2;
84+
y += sgn(deltay);
85+
error -= 1;
86+
}
87+
}
88+
}
6989

7090
return expansion;
7191
}

tests/richdem_unittests

-541 KB
Binary file not shown.

tests/tests.cpp

+9-10
Original file line numberDiff line numberDiff line change
@@ -505,7 +505,7 @@ TEST_CASE("Checking Catchment Delineation Generic"){
505505
SUBCASE("line"){
506506
int x0=0;
507507
int y0=0;
508-
int x1=2;
508+
int x1=0;
509509
int y1=4;
510510

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

518-
const Array2D<uint8_t> upslope_e = {
518+
Array2D<uint8_t> upslope_e = {
519519
{2, 0, 0, 0, 0, 0},
520520
{2, 0, 0, 0, 0, 0},
521521
{2, 0, 0, 0, 0, 0},
522522
{2, 0, 0, 0, 0, 0},
523523
{2, 0, 0, 0, 0, 0},
524+
{0, 0, 0, 0, 0, 0},
524525
};
526+
upslope_e.setNoData(0);
525527
Array2D<uint8_t> upslope(upslope_e,0);
528+
upslope.setNoData(0);
526529

527-
// upslope_e.printAll();
530+
// printAll not working, because we need to cast to unsigned() from uint8_t
528531
std::queue<GridCell> q = queue_from_linepoints(x0,y0,x1,y1,upslope);
529532
for (int y = 0; y< upslope.width(); y++){
530533
for (int x = 0; x<upslope.height(); x++){
531534
std::cout << unsigned(upslope(x,y)) << " ";
532535
}
533536
std::cout << std::endl;
534537
}
535-
// REQUIRE_EQ(q, q_expected);
538+
539+
CHECK(upslope.height() == upslope_e.height());
540+
CHECK(upslope.width() == upslope_e.width());
536541
CHECK(upslope == upslope_e);
537542
}
538543

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

564-
for (int y = 0; y< result.width(); y++){
565-
for (int x = 0; x<result.height(); x++){
566-
std::cout << unsigned(result(x,y)) << " ";
567-
}
568-
std::cout << std::endl;
569-
}
570569
upslope_cells_mflow(expansion, dem, result);
571570
CHECK(result == expected);
572571
}

0 commit comments

Comments
 (0)