forked from bhuvvaan/dreambooth-training
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rendering_user_defined_warehouses.py
270 lines (247 loc) · 7.56 KB
/
rendering_user_defined_warehouses.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
import pyglet
import numpy as np
from pyglet.gl import *
import math
import OpenGL.GL as gl
# Define colors
_BLACK = (0, 0, 0)
_WHITE = (255, 255, 255)
_DARKSLATEBLUE = (72, 61, 139)
_SKY_BLUE = (135, 206, 235)
_TEAL = (0, 128, 128)
_DARKORANGE = (255, 140, 0)
_RED = (255, 0, 0)
_GOAL_COLOR = (60, 60, 60)
_PINK = (255, 192, 203) # Define color for workstations
_ORANGE = (255, 165, 0) # Define color for home locations
_BACKGROUND_COLOR = _WHITE
_GRID_COLOR = _BLACK
_SHELF_COLOR = _BLACK
_ENDPOINT_COLOR = _SKY_BLUE
_WORKSTATION_COLOR = _PINK
_HOME_COLOR = _ORANGE
_AGENT_COLOR = _DARKORANGE
_AGENT_LOADED_COLOR = _RED
_AGENT_DIR_COLOR = _BLACK
# Define the layout
layout = """
..x@.......@..
..@.@@.@..@x..
.@.@xx@x@..@@.
.xx@@@[email protected].
.@@.@[email protected]..@.
..x..@.@@@@x@.
.@@.@@.@x.@@..
.x.@xx@@x@xx..
.@.........@..
"""
# Parse the layout
layout = layout.strip().split("\n")
rows = len(layout)
cols = len(layout[0])
# Define the size of each cell
cell_size = 30
# Create a window without padding
window = pyglet.window.Window(
width=cols * (cell_size + 1),
height=rows * (cell_size + 1)
)
# Create a batch for efficient rendering
batch = pyglet.graphics.Batch()
# Create shapes based on the layout
shelves = []
goals = []
endpoints = []
workstations = []
home_locations = []
for y, row in enumerate(layout):
for x, char in enumerate(row):
if char == 'x':
shelves.append((x, y))
elif char == 'g':
goals.append((x, y))
elif char == '@':
endpoints.append((x, y))
elif char == 'w':
workstations.append((x, y))
elif char == 'h':
home_locations.append((x, y))
# Normalize color values to [0, 1]
def normalize_color(color):
return tuple(c / 255.0 for c in color)
@window.event
def on_draw():
window.clear()
gl.glClearColor(*normalize_color(_BACKGROUND_COLOR), 1.0) # Normalize and set alpha to 1.0
gl.glClear(gl.GL_COLOR_BUFFER_BIT)
window.switch_to()
window.dispatch_events()
# Draw grid
batch = pyglet.graphics.Batch()
for r in range(rows + 1):
batch.add(
2,
gl.GL_LINES,
None,
(
"v2f",
(
0, # LEFT X
(cell_size + 1) * r + 1, # Y
(cell_size + 1) * cols, # RIGHT X
(cell_size + 1) * r + 1, # Y
),
),
("c3B", (*_GRID_COLOR, *_GRID_COLOR)),
)
for c in range(cols + 1):
batch.add(
2,
gl.GL_LINES,
None,
(
"v2f",
(
(cell_size + 1) * c + 1, # X
0, # BOTTOM Y
(cell_size + 1) * c + 1, # X
(cell_size + 1) * rows, # TOP Y
),
),
("c3B", (*_GRID_COLOR, *_GRID_COLOR)),
)
batch.draw()
# Draw shelves
batch = pyglet.graphics.Batch()
for x, y in shelves:
y = rows - y - 1 # pyglet rendering is reversed
batch.add(
4,
gl.GL_QUADS,
None,
(
"v2f",
(
(cell_size + 1) * x + 1, # TL - X
(cell_size + 1) * y + 1, # TL - Y
(cell_size + 1) * (x + 1), # TR - X
(cell_size + 1) * y + 1, # TR - Y
(cell_size + 1) * (x + 1), # BR - X
(cell_size + 1) * (y + 1), # BR - Y
(cell_size + 1) * x + 1, # BL - X
(cell_size + 1) * (y + 1), # BL - Y
),
),
("c3B", 4 * _SHELF_COLOR),
)
batch.draw()
# Draw goals
batch = pyglet.graphics.Batch()
for x, y in goals:
y = rows - y - 1 # pyglet rendering is reversed
batch.add(
4,
gl.GL_QUADS,
None,
(
"v2f",
(
(cell_size + 1) * x + 1, # TL - X
(cell_size + 1) * y + 1, # TL - Y
(cell_size + 1) * (x + 1), # TR - X
(cell_size + 1) * y + 1, # TR - Y
(cell_size + 1) * (x + 1), # BR - X
(cell_size + 1) * (y + 1), # BR - Y
(cell_size + 1) * x + 1, # BL - X
(cell_size + 1) * (y + 1), # BL - Y
),
),
("c3B", 4 * _GOAL_COLOR),
)
batch.draw()
# Draw endpoints
batch = pyglet.graphics.Batch()
for x, y in endpoints:
y = rows - y - 1 # pyglet rendering is reversed
batch.add(
4,
gl.GL_QUADS,
None,
(
"v2f",
(
(cell_size + 1) * x + 1, # TL - X
(cell_size + 1) * y + 1, # TL - Y
(cell_size + 1) * (x + 1), # TR - X
(cell_size + 1) * y + 1, # TR - Y
(cell_size + 1) * (x + 1), # BR - X
(cell_size + 1) * (y + 1), # BR - Y
(cell_size + 1) * x + 1, # BL - X
(cell_size + 1) * (y + 1), # BL - Y
),
),
("c3B", 4 * _ENDPOINT_COLOR),
)
batch.draw()
# Draw workstations
batch = pyglet.graphics.Batch()
for x, y in workstations:
y = rows - y - 1 # pyglet rendering is reversed
batch.add(
4,
gl.GL_QUADS,
None,
(
"v2f",
(
(cell_size + 1) * x + 1, # TL - X
(cell_size + 1) * y + 1, # TL - Y
(cell_size + 1) * (x + 1), # TR - X
(cell_size + 1) * y + 1, # TR - Y
(cell_size + 1) * (x + 1), # BR - X
(cell_size + 1) * (y + 1), # BR - Y
(cell_size + 1) * x + 1, # BL - X
(cell_size + 1) * (y + 1), # BL - Y
),
),
("c3B", 4 * _WORKSTATION_COLOR),
)
batch.draw()
# Draw home locations
batch = pyglet.graphics.Batch()
for x, y in home_locations:
y = rows - y - 1 # pyglet rendering is reversed
batch.add(
4,
gl.GL_QUADS,
None,
(
"v2f",
(
(cell_size + 1) * x + 1, # TL - X
(cell_size + 1) * y + 1, # TL - Y
(cell_size + 1) * (x + 1), # TR - X
(cell_size + 1) * y + 1, # TR - Y
(cell_size + 1) * (x + 1), # BR - X
(cell_size + 1) * (y + 1), # BR - Y
(cell_size + 1) * x + 1, # BL - X
(cell_size + 1) * (y + 1), # BL - Y
),
),
("c3B", 4 * _HOME_COLOR),
)
batch.draw()
# Save the image to a custom folder with a descriptive name
import os
# Create a folder named 'warehouse_layouts' if it doesn't exist
save_folder = 'valid_warehouse_layouts'
os.makedirs(save_folder, exist_ok=True)
# Generate a descriptive filename
filename = f'valid warehouse layouts: map elites paper 12.png'
# Combine the folder and filename
save_path = os.path.join(save_folder, filename)
# Save the image
pyglet.image.get_buffer_manager().get_color_buffer().save(save_path)
print(f"Warehouse layout saved as: {save_path}")
# Run the application
pyglet.app.run()