-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathAccuMethodRecursive.cpp
54 lines (49 loc) · 1.68 KB
/
AccuMethodRecursive.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include "DEM.h"
#include "FlowDirection.h"
#include "FlowAccu.h"
#include "utils.h"
#include <time.h>
int computeAccumulation(FlowDirection& dirGrid, FlowAccu& accuGrid, unsigned int curRow, unsigned int curCol);
void accuMethodByRecursive(FlowDirection& dirGrid, FlowAccu& accuGrid, double* pTimeConsume)
{
unsigned int width = dirGrid.getWidth();
unsigned int height = dirGrid.getHeight();
time_t startTime, endTime;
fprintf(stdout, "\nUsing Recursive algorithm to compute the flow accumulation matrix ... \n");
startTime = time(NULL);
for(unsigned int row = 0; row < height; ++row)
{
for(unsigned int col = 0; col < width; ++col)
{
if(dirGrid.is_NoData(row, col)) continue;
accuGrid.Set_Value(row, col, 0);
computeAccumulation(dirGrid, accuGrid, row, col);
}
}
endTime = time(NULL);
double timeConsume = difftime(endTime, startTime);
if(pTimeConsume) *pTimeConsume = timeConsume;
fprintf(stdout, "Time used:%d seconds\n", (int)timeConsume);
}
int computeAccumulation(FlowDirection& dirGrid, FlowAccu& accuGrid, unsigned int curRow, unsigned int curCol)
{
static int indexAndDir[] = {16, 32, 64, 128, 1, 2, 4, 8};
if(accuGrid.asInt(curRow, curCol) < 1)
{
accuGrid.Set_Value(curRow, curCol, 1);
for(int i = 0; i < 8; ++i)
{
unsigned int iRow = Get_rowTo(i, curRow);
unsigned int iCol = Get_colTo(i, curCol);
if(dirGrid.is_InGrid(iRow, iCol)
&& !dirGrid.is_NoData(iRow, iCol)
&& (dirGrid.asByte(iRow, iCol) == indexAndDir[i]))
{
int neighborValue = computeAccumulation(dirGrid, accuGrid, iRow, iCol);
accuGrid.Set_Value(curRow, curCol,
accuGrid.asInt(curRow, curCol) + neighborValue);
}
}
}
return accuGrid.asInt(curRow, curCol);
}