-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwaylay.py
executable file
·230 lines (182 loc) · 7.78 KB
/
waylay.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
#!/usr/bin/env python
import sys
import math, numbers
import PythonMagick
class Waypoint:
def __init__(self, text, x, y, z, isOn=True, color="none"):
# x and z are position, text is the text
# y is unecessary
self.x = int(x)
self.z = int(z)
self.text = text.strip() # remove leading and trailing whitespace
# color is the text color, isOn is if it is enabled
self.color = color.strip() # remove eol
self.isOn = [True if isOn == "true" else False][0]
def __str__(self):
return "{} @ ({},{})".format(self.text, self.x, self.z)
class CalibrationPoint:
# stores a point that links map pxl coords to world coords to overlay pxl coords
def __init__(self, mapX, mapZ, worldX, worldZ):
self.mapX = int(mapX)
self.mapZ = int(mapZ)
self.worldX = int(worldX)
self.worldZ = int(worldZ)
# NaN for invalidity (check with math.isnan())
self.overlayX = float('NaN')
self.overlayZ = float('NaN')
def mapPos(self, mapX, mapZ):
self.mapX = int(mapX)
self.mapZ = int(mapZ)
def overlayPos(self, overlayX, overlayZ):
self.overlayX = int(overlayX)
self.overlayZ = int(overlayZ)
def __str__(self):
return "Map({},{}) == World({},{}) == Overlay({},{})".format( \
self.mapX, self.mapZ, self.worldX, self.worldZ, self.overlayX, self.overlayZ)
def main(mapFilename, waypointsFilename, calibrationFilename):
# quadrant 4 of world is positive (i.e., SE, and negative is impossible)
# quadrant 4 of map is positive (no negative possible)
#map = PythonMagick.Image("mapFilename")
#mapSize = (image.size().width(), image.size().height())
(scale, calibPnt) = calibratePoints(calibrationFilename)
#mapSize = (10624, 10336)
(overlay, anchor) = makeWaypointLayer(waypointsFilename, scale)
# calibration point goes from world to map
# anchor point has a world point, now calculate its corresponding map point
xDiff = (calibPnt.worldX - anchor.worldX) * scale['x']
zDiff = (calibPnt.worldZ - anchor.worldZ) * scale['z']
anchor.mapPos(round(calibPnt.mapX - xDiff, 0), round(calibPnt.mapZ - zDiff, 0))
print(anchor)
print("Reading {} ... ".format(mapFilename), end="")
map = PythonMagick.Image(mapFilename)
print("done\nWriting overlay ... ", end="")
map.composite(overlay, anchor.mapX + anchor.overlayX, anchor.mapZ + anchor.overlayZ,
PythonMagick.CompositeOperator.OverCompositeOp)
# TODO check if need to add overlayX/overlayZ (for now it works since they're both 0)
print("done\nSaving final map to map.png ... ", end="")
map.write("map.png")
print("done")
#info(map)
def choose(n, k):
"""
A fast way to calculate binomial coefficients by Andrew Dalke (contrib).
http://stackoverflow.com/questions/3025162/statistics-combinations-in-python
"""
if 0 <= k <= n:
ntok = 1
ktok = 1
for t in range(1, min(k, n - k) + 1):
ntok *= n
ktok *= t
n -= 1
return ntok // ktok
else:
return 0
def calibratePoints(calibrationFilename):
""" Returns a world to map scale """
points = []
with open(calibrationFilename) as file:
for line in file:
points.append(CalibrationPoint(*line.split(":")))
xDiffs = []
zDiffs = []
for i in range(len(points)-1, -1, -1): # start from valid upper index, to 0, step down
for j in range(i-1, -1, -1):
xDiffs.append(math.fabs((points[i].mapX - points[j].mapX)/(points[i].worldX - points[j].worldX)))
# python 3 yields floats for division
zDiffs.append(math.fabs((points[i].mapZ - points[j].mapZ)/(points[i].worldZ - points[j].worldZ)))
# division in python 3 returns a float
# numbers should never be negative
# assert ((min(xDiffs) > 0) and (min(zDiffs) > 0))
numPairs = choose(len(points), 2)
scale = {}
scale['x'] = round(sum(xDiffs)/numPairs, 5) # average, then truncate
scale['z'] = round(sum(zDiffs)/numPairs, 5)
print("{} = {}\n{} = {}".format(xDiffs,scale['x'],zDiffs,scale['z']))
return (scale, points[0]) # the first point in the list
def makeWaypointLayer(waypointsFilename, scale):
# need to know how many pixels in the map that one block is
waypoints = []
with open(waypointsFilename) as file:
for line in file:
waypoints.append(Waypoint(*line.split(":")[:6]))
# ':' is delimiter
# ignore Death point's extra entry
# get the padding that divides evenly when inverse-scaled
# guaranteed to be at least one, since range goes through all divisors between multiples
padding = {'x': 200, 'z': 20}
# -- code for (exact) integer scaling
#temp = [i for i in range(scale['x']) if divmod(padding['x'] + i, scale['x']) == 0][0]
#padding['x'] += temp
#temp = [i for i in range(scale['z']) if divmod(padding['z'] + i, scale['z']) == 0][0]
#padding['z'] += temp
xWorld = [wp.x for wp in waypoints]
zWorld = [wp.z for wp in waypoints]
minX = min(xWorld)
maxX = max(xWorld)
minZ = min(zWorld)
maxZ = max(zWorld)
# calculate overlay (world) dimensions + padding
# scale could be non-integer, so we round result and convert to integer
xDim = int(round(abs(maxX - minX)*scale['x'] + padding['x'],0))
zDim = int(round(abs(maxZ - minZ)*scale['z'] + padding['z'],0))
# NW is negative, so we need the minX and minZ to equal (0,0)
# -- code for (exact) integer scaling
#anchor = CalibrationPoint(0,0, minX - divmod(padding['x'], scale['x'])[0],
# minZ - divmod(padding['z'], scale['z'])[0])
anchor = CalibrationPoint(0, 0, minX - int(padding['x']//scale['x']),
minZ - int(padding['z']//scale['z']))
# the error comes from here
anchor.overlayPos(0,0)
print("waypoint overlay is {}x{}".format(xDim, zDim))
world = PythonMagick.Image(PythonMagick.Geometry(xDim, zDim), "none")
# * to unpack; none for no color
assert (isinstance(anchor.overlayX, numbers.Integral) and isinstance(anchor.overlayZ, numbers.Integral))
print("Writing waypoints ... ", end="")
for waypoint in waypoints:
wpImg = makeWaypoint(waypoint) # automatic garbage collection
# get distance from anchor and translate position
# since anchor is the upper-left, we can get the absolute distance then add it
xDiff = int(abs(round((anchor.worldX - waypoint.x)*scale['x'], 0)))
zDiff = int(abs(round((anchor.worldZ - waypoint.z)*scale['z'], 0)))
# center the image
xDiff -= int(wpImg.size().width()//2) # floor division
zDiff -= int(wpImg.size().height()//2)
# http://stackoverflow.com/questions/7793186/drawing-text-in-pythonmagick
#print("{} ... ".format(waypoint.text), end="")
world.composite(wpImg, anchor.overlayX + xDiff, anchor.overlayZ + zDiff,
PythonMagick.CompositeOperator.OverCompositeOp)
print("done")
# write
# for wp in waypoints:
# wpImage =
# image add wpImage ( -6000x + wp.x, -5000y + wp.y) # temporarily make everything positive
# how to shift wp which has world pxl coords of x, y to map pxl coords of a,b
# add text overlay to a-x, b-y ((0,0) of world pxl onto map pxl)
return (world, anchor)
def makeWaypoint(waypoint):
# create a label, then position (without centering)
wpImg = PythonMagick.Image(PythonMagick.Geometry(200,20), "none")
#wpImg.backgroundColor(PythonMagick.Color("#00000080"))
# pen color
wpImg.fillColor("#{}".format(waypoint.color))
wpImg.annotate(waypoint.text, PythonMagick.GravityType.CenterGravity)
# remove extra, then draw border around text
wpImg.trim()
wpImg.borderColor("black")
wpImg.border()
wpImg.opacity(0x8080)
#wpImg.resize(PythonMagick.Geometry(wpImg.size().width(), wpImg.size().height()))
#print("{} {} {}x{}".format(wpImg.fileName(), wpImg.magick(), wpImg.size().width(), wpImg.size().height()))
return wpImg
def transformWorldToMap(worldPos):
# pass a tuple
return mapPos
def info(image):
print("{} {} {}x{}".format(image.fileName(), image.magick(), image.size().width(), image.size().height()))
print(dir(PythonMagick.Image()))
if __name__ == "__main__":
main(*sys.argv[1:]) # takes 3 arguments
# read image
# create a transparent layer that has text labels from waypoint data
sys.exit(0)