-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathrivermapper.py
executable file
·244 lines (202 loc) · 4.77 KB
/
rivermapper.py
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
#!/usr/bin/env python3
from heapq import heappush, heappop, heapify
import sys
import numpy as np
import imageio
file_input = None
file_output = None
n_args = len(sys.argv)
i = 1
n = 1
sea_level = -128
seed = None
contrast = 4.
bit_depth = 8
draw_linewidth = False
river_limit = 0
while i < n_args:
arg = sys.argv[i]
if len(arg) == 0:
i += 1
continue
if arg[0] == "-":
l = arg[1]
if l == "l":
sea_level = int(sys.argv[i+1])
i += 2
continue
if l == "s":
seed = int(sys.argv[i+1])
i += 2
continue
if l == "c":
contrast = float(sys.argv[i+1])
i += 2
continue
if l == "d":
bit_depth = int(sys.argv[i+1])
i += 2
continue
if l == "w":
draw_linewidth = True
river_limit = int(sys.argv[i+1])
i += 2
continue
if l == "i":
file_input = sys.argv[i+1]
i += 2
continue
if l == "o":
file_output = sys.argv[i+1]
i += 2
continue
else:
if n == 2:
file_output = arg
n += 1
if n == 1:
file_input = arg
n += 1
i += 1
if not file_input:
raise ValueError("No filename given for input")
if not file_output:
raise ValueError("No filename given for output")
if seed:
numpy.random.seed(seed=seed)
sys.setrecursionlimit(65536)
print("Reading image")
heightmap = np.array(imageio.imread(file_input))
shape = heightmap.shape
(X, Y) = shape
print("Finding start points")
visited = np.zeros(shape, dtype=bool)
start_points = []
def add_start_point(x, y):
start_points.append((heightmap[x, y] + np.random.random(), x, y))
visited[x, y] = True
to_explore = 0
for x in range(1, X-1):
for y in range(1, Y-1):
if heightmap[x, y] <= sea_level:
continue
to_explore += 1
if to_explore % 1000000 == 0:
print("Found", str(to_explore // 1000000), "millions points to explore")
if (heightmap[x-1, y] <= sea_level or heightmap[x+1, y] <= sea_level or heightmap[x, y-1] <= sea_level or heightmap[x, y+1] <= sea_level):
add_start_point(x, y)
for x in range(X):
if heightmap[x, 0] > sea_level:
add_start_point(x, 0)
to_explore += 1
if heightmap[x, -1] > sea_level:
add_start_point(x, Y-1)
to_explore += 1
for y in range(1, Y-1):
if heightmap[0, y] > sea_level:
add_start_point(0, y)
to_explore += 1
if heightmap[-1, y] > sea_level:
add_start_point(X-1, y)
to_explore += 1
print("Found", str(len(start_points)), "start points")
heap = start_points[:]
heapify(heap)
print("Building river trees:", str(to_explore), "points to visit")
flow_dirs = np.zeros(shape, dtype=np.int8)
# Directions:
# 1: +x
# 2: +y
# 4: -x
# 8: -y
def try_push(x, y): # try_push does 2 things at once: returning whether water can flow, and push the upward position in heap if yes.
if not visited[x, y]:
h = heightmap[x, y]
if h > sea_level:
heappush(heap, (h + np.random.random(), x, y))
visited[x, y] = True
return True
return False
def process_neighbors(x, y):
dirs = 0
if x > 0 and try_push(x-1, y):
dirs+= 1
if y > 0 and try_push(x, y-1):
dirs += 2
if x < X-1 and try_push(x+1, y):
dirs += 4
if y < Y-1 and try_push(x, y+1):
dirs += 8
flow_dirs[x, y] = dirs
while len(heap) > 0:
t = heappop(heap)
to_explore -= 1
if to_explore % 1000000 == 0:
print(str(to_explore // 1000000), "million points left", "Altitude:", int(t[0]), "Queue:", len(heap))
process_neighbors(t[1], t[2])
visited = None
heightmap = None
print("Calculating water quantity")
waterq = np.ones(shape)
def set_water(x, y):
water = 1
dirs = flow_dirs[x, y]
if dirs % 2 == 1:
water += set_water(x-1, y)
dirs //= 2
if dirs % 2 == 1:
water += set_water(x, y-1)
dirs //= 2
if dirs % 2 == 1:
water += set_water(x+1, y)
dirs //= 2
if dirs % 2 == 1:
water += set_water(x, y+1)
waterq[x, y] = water
return water
maxwater = 0
for start in start_points:
water = set_water(start[1], start[2])
if water > maxwater:
maxwater = water
print("Maximal water quantity:", str(maxwater))
flow_dirs = None
print("Generating image")
power = 1 / contrast
if draw_linewidth:
bit_depth = 1
river_array = np.zeros((X, Y), dtype=bool)
for x in range(X):
for y in range(Y):
q = waterq[x,y]
if q >= river_limit:
rsize = int((q / river_limit)**power)
if rsize > 1:
rsize -= 1
xmin = max(x-rsize, 0)
xmax = min(x+rsize+1, X)
ymin = max(y-rsize, 0)
ymax = min(y+rsize+1,Y)
river_array[xmin:xmax,y] = True
river_array[x,ymin:ymax] = True
else:
river_array[x,y] = True
data = np.uint8(river_array * 255)
else:
if bit_depth <= 8:
bit_depth = 8
dtype = np.uint8
elif bit_depth <= 16:
bit_depth = 16
dtype = np.uint16
elif bit_depth <= 32:
bit_depth = 32
dtype = np.uint32
else:
bit_depth = 64
dtype = np.uint64
maxvalue = 2 ** bit_depth - 1
coeff = maxvalue / (maxwater ** power)
data = np.floor((waterq ** power) * coeff).astype(dtype)
waterq = None
imageio.imwrite(file_output, data)