-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhierarchy.c
90 lines (76 loc) · 2.16 KB
/
hierarchy.c
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
#include <stdio.h>
#include <stdlib.h>
#include "global.h"
#define SHED(row, col) shed_map->cells.int32[(row) * ncols + (col)]
struct hierarchy *analyze_hierarchy(struct raster_map *shed_map,
struct outlet_list *outlet_l)
{
struct hierarchy *hier = malloc(sizeof *hier);
int nrows = shed_map->nrows, ncols = shed_map->ncols;
int i;
hier->n = outlet_l->n;
hier->self = malloc(sizeof *hier->self * hier->n);
hier->up = malloc(sizeof *hier->up * hier->n);
hier->down = malloc(sizeof *hier->down * hier->n);
#pragma omp parallel for schedule(dynamic)
for (i = 0; i < outlet_l->n; i++) {
int row = outlet_l->row[i];
int col = outlet_l->col[i];
hier->self[i] = outlet_l->id[i];
switch (outlet_l->dir[i]) {
case NW:
row--;
col--;
break;
case N:
row--;
break;
case NE:
row--;
col++;
break;
case W:
col--;
break;
case E:
col++;
break;
case SW:
row++;
col--;
break;
case S:
row++;
break;
case SE:
row++;
col++;
break;
}
hier->down[i] = row < 0 || row >= nrows || col < 0 || col >= ncols ||
SHED(row, col) == SUBWATERSHED_NULL ? HIERARCHY_NULL : SHED(row,
col);
}
#pragma omp parallel for schedule(dynamic)
for (i = 0; i < hier->n; i++) {
int self = hier->self[i];
int j;
for (j = 0; j < hier->n && hier->down[j] != self; j++) ;
if (j == hier->n)
hier->up[i] = HIERARCHY_NULL;
else
hier->up[i] = hier->self[j];
}
return hier;
}
int write_hierarchy(const char *hier_path, struct hierarchy *hier)
{
FILE *fp;
int i;
if (!(fp = fopen(hier_path, "w")))
return 1;
for (i = 0; i < hier->n; i++)
fprintf(fp, "%d %d %d\n", hier->self[i], hier->up[i], hier->down[i]);
fclose(fp);
return 0;
}