-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmap.py
461 lines (372 loc) · 13.3 KB
/
map.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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
from worldLoader import WorldSlice
import numpy as np
import cv2
import maths
from PIL import Image
from scipy import ndimage
# Skeletonize
from skimage import img_as_bool, io, color, morphology
from skan import skeleton_to_csgraph
import matplotlib.pyplot as plt
from collections import Counter
# Test
import random
def findGround(xzStart, xz): # TODO: Change error.
"""
Find the surface at xz using heightmap.
Args:
xzStart (tuple): Starting coordinates of the heightmap (northwest corner).
xz (tuple): Coordinates xz in the Minecraft world.
Returns:
tuple: Coordinates xyz in the Minecraft world.
"""
im = Image.open("heightmap.png")
x = round(xz[0] - xzStart[0])
z = round(xz[-1] - xzStart[-1])
# Alpha is defined as the height ([3]).
width, height = im.size
if x >= width or z >= height:
print("img:", x, z)
print(width, height)
print(xzStart, xz)
try:
return xz[0], (im.getpixel((x, z))[2]) - 1, xz[-1]
except:
print("Error getpixel in map.py:42 with ", x, z)
return None
def areaCoordinates(xyz1, xyz2):
"""
Transform an area into a start point and a distance. Work with xyz
coordinates and xz coordinates.
Args:
xyz1 (tuple): Coordinates, whatever the direction.
xyz2 (tuple): Coordinates, whatever the direction.
Returns:
tuple: xzStart, xzDistance
"""
xzStart = (min(xyz1[0], xyz2[0]), min(xyz1[-1], xyz2[-1]))
xzDistance = (
(abs(xyz1[0] - xyz2[0])),
(abs(xyz1[-1] - xyz2[-1])),
)
return xzStart, xzDistance
def irlToMc(xzStart, xz):
x = round(xzStart[0] + xz[0])
z = round(xzStart[1] + xz[1])
return (x, z)
def heightmap(
xzStart,
xzDistance,
mapName="heightmap.png",
biomeName="heightmap_biome.png",
):
"""
Generate a heightmap using nbt data.
Args:
xzStart (tuple): xz coordinates of the northwest corner of the
area to scan.
xzDistance (tuple): xz distance of the southwest corner from the
northwest corner.
Returns:
heightmap.png
>>> heightmap((-256, -256), (512, 512))
"""
heightmap = Image.new(
"RGBA",
(xzDistance[0], xzDistance[1]),
"red",
)
heightmapBiome = Image.new(
"RGBA",
(xzDistance[0], xzDistance[1]),
"red",
)
slice = WorldSlice((xzStart[0], xzStart[1], xzDistance[0], xzDistance[1]))
heightmapData = list(
np.array(slice.heightmaps["MOTION_BLOCKING_NO_LEAVES"], dtype=np.uint8)
)
for x in range(0, xzDistance[0]):
for z in range(0, xzDistance[1]):
y = heightmapData[x][z]
biomeId = slice.getBiomeAt((xzStart[0] + x, 0, xzStart[1] + z))
block = slice.getBlockAt((xzStart[0] + x, y, xzStart[1] + z))
heightmapBiome.putpixel((x, z), heightmapColor(y, biomeId, block))
heightmap.putpixel((x, z), (y, y, y))
heightmap.save(mapName)
heightmapBiome.save(biomeName)
def heightmapColor(y, biomeId, block): # TODO: Refactoring.
neutral = [
16,
26,
12,
129,
1,
3,
131,
162,
2,
17,
37,
39,
35,
36,
163,
164,
]
# Hills: 13, 17, 18, 19, 22, 28, 31, 33, 156, 161
water = [
0,
7,
10,
11,
24,
44,
45,
46,
47,
48,
49,
50,
]
waterBlocks = [
"minecraft:water",
"minecraft:seagrass",
"minecraft:tall_seagrass",
"minecraft:kelp_plant",
]
if (biomeId in water) or (block in waterBlocks):
return 0, 0, 0, 0
if biomeId in neutral:
return 255, 255, 255, 255
else:
return 0, 0, 0, 0
def blur(image, name="medianBlur.png", factor=5):
img = cv2.imread(image)
img = cv2.medianBlur(img, factor)
plt.imsave(name, img, cmap="gray", format="png")
def sobel(image, name="heightmap_sobel.png"):
# Open the image
img = np.array(Image.open(image)).astype(np.uint8)
# Apply gray scale
gray_img = np.round(
0.299 * img[:, :, 0] + 0.587 * img[:, :, 1] + 0.114 * img[:, :, 2]
).astype(np.uint8)
# Sobel Operator
h, w = gray_img.shape
# define filters
horizontal = np.array([[-1, 0, 1], [-2, 0, 2], [-1, 0, 1]]) # s2
vertical = np.array([[-1, -2, -1], [0, 0, 0], [1, 2, 1]]) # s1
# define images with 0s
newhorizontalImage = np.zeros((h, w))
newverticalImage = np.zeros((h, w))
newgradientImage = np.zeros((h, w))
# offset by 1
for i in range(1, h - 1):
for j in range(1, w - 1):
horizontalGrad = (
(horizontal[0, 0] * gray_img[i - 1, j - 1])
+ (horizontal[0, 1] * gray_img[i - 1, j])
+ (horizontal[0, 2] * gray_img[i - 1, j + 1])
+ (horizontal[1, 0] * gray_img[i, j - 1])
+ (horizontal[1, 1] * gray_img[i, j])
+ (horizontal[1, 2] * gray_img[i, j + 1])
+ (horizontal[2, 0] * gray_img[i + 1, j - 1])
+ (horizontal[2, 1] * gray_img[i + 1, j])
+ (horizontal[2, 2] * gray_img[i + 1, j + 1])
)
newhorizontalImage[i - 1, j - 1] = abs(horizontalGrad)
verticalGrad = (
(vertical[0, 0] * gray_img[i - 1, j - 1])
+ (vertical[0, 1] * gray_img[i - 1, j])
+ (vertical[0, 2] * gray_img[i - 1, j + 1])
+ (vertical[1, 0] * gray_img[i, j - 1])
+ (vertical[1, 1] * gray_img[i, j])
+ (vertical[1, 2] * gray_img[i, j + 1])
+ (vertical[2, 0] * gray_img[i + 1, j - 1])
+ (vertical[2, 1] * gray_img[i + 1, j])
+ (vertical[2, 2] * gray_img[i + 1, j + 1])
)
newverticalImage[i - 1, j - 1] = abs(verticalGrad)
# Edge Magnitude
mag = np.sqrt(pow(horizontalGrad, 2.0) + pow(verticalGrad, 2.0))
newgradientImage[i - 1, j - 1] = mag
plt.imsave(
"heightmap_sobel.png", newgradientImage, cmap="gray", format="png"
)
def canny(image, name="heightmap_canny.png"):
# Open the image
img = cv2.imread(image)
# Apply Canny
edges = cv2.Canny(img, 100, 200, 3, L2gradient=True)
plt.imsave(name, edges, cmap="gray", format="png")
def mapData(
heightmap, heightmap_biome, heightmap_sobel, name="heightmap_data.png"
):
im = Image.open(heightmap)
width, height = im.size
sobel = Image.open(heightmap_sobel)
img = sobel.load()
biome = Image.open(heightmap_biome)
out = biome.load()
for x in range(0, width):
for z in range(0, height):
if img[x, z][0] >= 25:
out[x, z] = (0, 0, 0, 255)
biome.save(name)
def skel(image, name=None, debug=False):
"""
Transform a biome heightmap to a skeleton.
https://jni.github.io/skan/api/skan.csr.html
Args:
image (str): Path of the image.
Returns:
list: List of tuples. Coordinates of each intersections of the
skeleton.
Create an image of the skeleton.
"""
img = img_as_bool(color.rgb2gray(color.rgba2rgb(io.imread(image))))
out = morphology.skeletonize(img)
# out = morphology.medial_axis(img)
pixel_graph, coordinates, degrees = skeleton_to_csgraph(out)
pixel_graph = pixel_graph.tocoo()
pixel_graph_row = list(pixel_graph.row)
pixel_graph_col = list(pixel_graph.col)
pixel_graph_data = list(pixel_graph.data)
# List of lists. Inverted coordinates.
coordinates = list(coordinates)
for i in range(len(coordinates)):
coordinates[i] = (coordinates[i][1], coordinates[i][0])
if debug:
f, (ax0, ax1) = plt.subplots(1, 2)
ax0.imshow(img, cmap="gray", interpolation="nearest")
ax1.imshow(out, cmap="gray", interpolation="nearest")
plt.show()
if name != None:
plt.imsave(name, out, cmap="gray", format="png")
return pixel_graph_row, pixel_graph_col, pixel_graph_data, coordinates
def findNextElements(key, pixel_graph_row, pixel_graph_col):
"""Find the very nearest elements"""
line = []
values = np.array(pixel_graph_row)
searchval = key
ind = np.where(values == searchval)[0]
for i in range(len(ind)):
if pixel_graph_row[ind[i]] == key:
line.append(pixel_graph_col[ind[i]])
return line
def parseGraph(pixel_graph_row, pixel_graph_col):
lines = []
intersections = [] # Center of intersections[i] is center[i]
center = []
for key, value in sorted(
Counter(pixel_graph_row).items(), key=lambda kv: kv[1], reverse=True
):
# Start from the biggest intersections.
if value != 2: # We don't want to be in the middle of a line.
line = findLine(key, pixel_graph_row, pixel_graph_col)
# We have now all the connected points if it's an
# intersection. We need to find the line.
if value != 1:
# It's not an endpoint.
center.append(key)
intersections.append(line)
for i in line:
line = findLine(
i,
pixel_graph_row,
pixel_graph_col,
)
if i in line:
# The key is inside the result : it's a line.
alreadyInside = False
for l in lines:
# Verification if not already inside.
if Counter(l) == Counter(line):
alreadyInside = True
# print(line, "inside", lines)
if alreadyInside == False:
lines.append(line)
else:
# The key is not inside the result, it's an
# intersection directly connected to the key.
line = [key, i]
alreadyInside = False
for l in lines:
# Verification if not already inside.
if Counter(l) == Counter(line):
alreadyInside = True
# print(line, "inside", lines)
if alreadyInside == False:
lines.append(line)
return lines, intersections, center
def findLine(key, pixel_graph_row, pixel_graph_col):
nextKeys = findNextElements(key, pixel_graph_row, pixel_graph_col)
if len(nextKeys) >= 3: # Intersections.
return nextKeys
if len(nextKeys) == 2 or len(nextKeys) == 1: # In line or endpoints.
line = []
line.append(key)
line.insert(0, nextKeys[0])
if len(nextKeys) == 2:
line.insert(len(line), nextKeys[1])
nextKeys = line[0], line[-1]
while len(nextKeys) == 2 or len(nextKeys) == 1:
extremity = []
for key in nextKeys:
nextKeys = findNextElements(
key, pixel_graph_row, pixel_graph_col
)
if len(nextKeys) <= 2:
# Add the neighbors that is not already in the line.
for element in nextKeys:
if element not in line:
extremity.append(element)
line.append(element)
if len(nextKeys) >= 3:
# Add the intersection only.
extremity.append(key)
nextKeys = []
for key in extremity:
ends = findNextElements(
key, pixel_graph_row, pixel_graph_col
)
if len(ends) == 2:
nextKeys.append(key)
return line
def sortPointDistance(points, distance, debugImage=None): # ?
"""
Sort points depending of the distance.
Args:
points (dict): Keys: To be associated with the distance. Values:
Coordinates corresponding to the key category.
distance (dict): Keys: To be associated with the points. Values:
Distance between all the points.
Returns:
list: List of tuples. Coordinates only.
dict: Keys: The category/tag. Values: Coordinates that belong to
the category.
"""
tag = {}
for key in points.keys():
tag[key] = []
savedPoints = []
savedPointsWithTag = {}
for key, value in sorted(
distance.items(), key=lambda kv: kv[1], reverse=True
):
if value != -1:
for coord in points[key]:
if all(
maths.distance2D(allPoint, coord) > value
for allPoint in savedPoints
):
savedPoints.append(coord)
if savedPointsWithTag.get(key) == None:
savedPointsWithTag[key] = []
savedPointsWithTag[key].append(coord)
if debugImage:
img = Image.open(debugImage)
for i in range(len(savedPoints)):
img.putpixel(savedPoints[i], (255, 0, 0))
img.save("debugImage.png", "PNG")
return savedPoints, savedPointsWithTag