-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathAccuMethodZhou.cpp
76 lines (73 loc) · 1.97 KB
/
AccuMethodZhou.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 "FlowAccu.h"
#include "utils.h"
#include <iostream>
#include <fstream>
#include <omp.h>
#include <vector>
#include <list>
void accuMethodByZhou(FlowDirection& dirGrid, FlowAccu& accuGrid, double* consumeTime)
{
time_t startTime, endTime;
unsigned int width = dirGrid.Get_NX();
unsigned int height = dirGrid.Get_NY();
fprintf(stdout, "\nUsing Zhou'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);
//Initialize flow accumulation matrix with one
for (unsigned int row = 0; row < height; row++)
{
for (unsigned int col = 0; col < width; col++)
{
if (!dirGrid.is_NoData(row, col))
{
accuGrid.Set_Value(row, col, 1);
}
}
}
for (unsigned int row = 0; row < height; row++)
{
for (unsigned int col = 0; col < width; col++)
{
if (!dirGrid.is_NoData(row, col))
{
if (nipsGrid->asByte(row, col) != 0)
{
continue;
}
unsigned int iRow = row;
unsigned int iCol = col;
int nAccuCellNum = 0;
do
{
int currentNodeAccu = accuGrid.asInt(iRow, iCol);
currentNodeAccu += nAccuCellNum;
accuGrid.Set_Value(iRow, iCol, currentNodeAccu);
nAccuCellNum = currentNodeAccu;
if (nipsGrid->asByte(iRow, iCol) >= 2)
{
nipsGrid->Set_Value(iRow, iCol, nipsGrid->asByte(iRow, iCol) - 1);
break;
}
}
while (dirGrid.nextCell(iRow, iCol, dirGrid.asByte(iRow, iCol)));
}
}
}
endTime = time(NULL);
double timeConsume = difftime(endTime, startTime);
std::cout<<"Time used"<<(int)timeConsume<<" seconds"<<std::endl;
if(consumeTime) *consumeTime = timeConsume;
delete nipsGrid;
}