-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathAccuMethodWang.cpp
78 lines (67 loc) · 2.07 KB
/
AccuMethodWang.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#include "DEM.h"
#include "FlowDirection.h"
#include "FlowAccu.h"
#include <vector>
#include <time.h>
#include <iostream>
#include <queue>
#include "utils.h"
void accuMethodByWang(FlowDirection& dirGrid, FlowAccu& accuDem, double* pTimeConsume)
{
time_t startTime, endTime;
unsigned int width = dirGrid.Get_NX();
unsigned int height = dirGrid.Get_NY();
fprintf(stdout, "\nUsing Wang's algorithm to compute the flow accumulation matrix ... \n");
startTime = time(NULL);
height = dirGrid.getHeight();
width = dirGrid.getWidth();
//FlowDirection can also be used as the NIDP matrix
FlowDirection* nipsGrid = new FlowDirection();
nipsGrid->SetHeight(height);
nipsGrid->SetWidth(width);
if (!nipsGrid->Allocate())
{
std::cout<<"Failed to allocate memory for the NIDP matrix!"<<std::endl;
delete nipsGrid;
return;
}
calNips(dirGrid, *nipsGrid);
unsigned int nipsValueEqules0Nums = 0;
unsigned int validNums = 0;
std::queue<unsigned int> que;
for(unsigned int row = 0; row < height; ++row)
{
for(unsigned int col = 0; col < width; ++col)
{
if(!dirGrid.is_NoData(row, col))
{
accuDem.Set_Value(row, col, 1);
++validNums;
if(nipsGrid->asByte(row, col) == 0)
{
++nipsValueEqules0Nums;
que.push(width * row + col);
}
}
}
}
unsigned int curCellIndex = 0;
while(que.size() != 0)
{
curCellIndex = que.front();
que.pop();
unsigned int curRow = curCellIndex / width;
unsigned int curCol = curCellIndex % width;
unsigned int nextRow = curRow;
unsigned int nextCol = curCol;
if(!dirGrid.nextCell(nextRow, nextCol, dirGrid.asByte(curRow, curCol))) continue;
accuDem.Set_Value(nextRow, nextCol, accuDem.asInt(nextRow, nextCol) + accuDem.asInt(curRow, curCol));
nipsGrid->Set_Value(nextRow, nextCol, nipsGrid->asByte(nextRow, nextCol) - 1);
if(nipsGrid->asByte(nextRow, nextCol) == 0) que.push(width * nextRow + nextCol);
}
endTime = time(NULL);
double timeConsume = difftime(endTime, startTime);
std::cout<<"Time used:"<<(int)timeConsume<<" seconds"<<std::endl;
if(pTimeConsume) *pTimeConsume = timeConsume;
delete nipsGrid;
}