-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathFlowDirection.cpp
109 lines (104 loc) · 1.77 KB
/
FlowDirection.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#include "FlowDirection.h"
#include "utils.h"
bool FlowDirection::Allocate()
{
delete[] pDir;
pDir=new unsigned char[width*height];
if (pDir == NULL)
{
return false;
}
else
{
setNoData(pDir, width * height, NODATA);
return true;
}
}
void FlowDirection::freeMem()
{
delete[] pDir;
pDir = NULL;
}
unsigned char FlowDirection::asByte(int row, int col) const
{
return pDir[row*width+col];
}
void FlowDirection::Set_Value(int row,int col, unsigned char z)
{
pDir[row*width+col]=z;
}
bool FlowDirection::is_NoData(int row, int col) const
{
if (pDir[row*width+col] == NODATA) return true;
return false;
}
void FlowDirection::Assign_NoData()
{
for (int i = 0; i < width*height; i++)
pDir[i] = NODATA;
}
int FlowDirection::Get_NY() const
{
return height;
}
int FlowDirection::Get_NX() const
{
return width;
}
unsigned char* FlowDirection::getData() const
{
return pDir;
}
void FlowDirection::SetHeight(int height)
{
this->height = height;
}
void FlowDirection::SetWidth(int width)
{
this->width = width;
}
bool FlowDirection::is_InGrid(int row, int col) const
{
if ((row >=0 && row < height) && (col >= 0 && col < width))
{
return true;
}
else
{
return false;
}
}
float FlowDirection::getLength(unsigned int dir)
{
if ((dir & 0x1) == 1)
{
return 1.41421f;
}
else return 1.0f;
}
bool FlowDirection::nextCell(unsigned int& row, unsigned int& col, unsigned char dir) const
{
static int indexDir[] = {1, 2, 4, 8, 16, 32, 64, 128};
int iDir = -1;
for (int i = 0; i < 8; i++)
{
if (indexDir[i] == dir)
{
iDir = i;
break;
}
}
if (iDir == -1) return false;
int iRow = Get_rowTo(iDir, row);
int iCol = Get_colTo(iDir, col);
if (is_InGrid(iRow, iCol) && !is_NoData(iRow, iCol))
{
row = iRow;
col = iCol;
return true;
}
else
{
return false;
}
}