-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpcg.py
2003 lines (1831 loc) · 84.4 KB
/
pcg.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
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
from base import *
def find_data(name: str, data: list):
for dic in data:
if dic["name"] == name:
return dic
raise ValueError("No such data")
def euler2quaternion(x: float, y: float, z: float) -> Tuple[float, float, float, float]:
x, y, z = x / 2, y / 2, z / 2
c1, s1 = cos(x), sin(x)
c2, s2 = cos(y), sin(y)
c3, s3 = cos(z), sin(z)
return (
s1 * c2 * c3 - c1 * s2 * s3,
c1 * s2 * c3 + s1 * c2 * s3,
c1 * c2 * s3 - s1 * s2 * c3,
c1 * c2 * c3 + s1 * s2 * s3,
)
def xy2idx(x: float, y: float) -> Tuple[float, float]:
return (x + RL) * MAP_W / ((W + 2) * RL), (y + RL) * MAP_H / ((H + 2) * RL)
def eu_dist(p1, p2):
return sqrt((p1[0] - p2[0]) ** 2 + (p1[1] - p2[1]) ** 2)
def ang_dist(a1, a2):
a1 = (a1 % (2 * pi) + 2 * pi) % (2 * pi)
a2 = (a2 % (2 * pi) + 2 * pi) % (2 * pi)
return min(abs(a1 - a2), 2 * pi - abs(a1 - a2))
def p(arg1, arg2=None):
if arg2 is None:
return np.array(arg1)
return np.array([arg1, arg2])
def get_height(height_map: List[List[float]], x: float, y: float) -> float:
# note that height map is based on an extended grid (W+2)*(H+2)
idx_x, idx_y = xy2idx(x, y)
x1, y1 = int(idx_x), int(idx_y)
x2, y2 = x1 + 1, y1 + 1
if x1 < 0 or x2 >= MAP_W or y1 < 0 or y2 >= MAP_H:
return WATER_HEIGHT
xx, yy = idx_x - x1, idx_y - y1
w1, w2, w3, w4 = (1 - xx) * (1 - yy), (1 - xx) * yy, xx * (1 - yy), xx * yy
return max(
min(w1 * height_map[x1][y1] + w2 * height_map[x1][y2] + w3 * height_map[x2][y1] + w4 * height_map[x2][y2], 1),
0,
)
def output_height_map(height_map: List[List[float]], width: int, height: int, name: str, suffix: str) -> None:
# use cv2 to convert height_map to a width*height png
# height_map(0,0) should be the bottom left corner, width is x, height is y
assert width >= height
img = np.zeros((width, width, 3), np.uint8)
for i in range(width):
for j in range(height):
grey = round(height_map[i][j] * 255)
img[width - j - 1][i] = (grey, grey, grey)
outpath = "outputs/" + name + suffix + ".png"
cv2.imwrite(outpath, img)
def output_label_map(label_map: List[List[List[float]]], width: int, height: int, name: str, suffix: str) -> None:
# use cv2 to convert label_map to a width*height png
# label_map(0,0) should be the bottom left corner, width is x, height is y
assert width >= height
img = np.zeros((width, width, 3), np.uint8)
for i in range(width):
for j in range(height):
label = label_map[i][j]
label = [label[1] + label[2], label[3], label[4]]
sum = label[0] + label[1] + label[2]
if sum == 0:
img[width - j - 1][i] = (0, 0, 0)
else:
if sum > 1:
label = [label[0] / sum, label[1] / sum, label[2] / sum]
lb = label[0]
lg = label[1] / (1 - label[0]) if label[0] < 1 else 0
lr = label[2] / (1 - label[0] - label[1]) if label[0] + label[1] < 1 else 0
img[width - j - 1][i] = (round(lb * 255), round(lg * 255), round(lr * 255))
outpath = "outputs/" + name + suffix + ".png"
cv2.imwrite(outpath, img)
def output_scene(
all_obj_infos: Tuple[List[Tuple[float, float, float, float]], str],
view_points: List[Tuple[float, float, float, float, float]],
data: dict,
gen_idx: int,
suffix: str,
) -> None:
type2infos = {}
for obj_infos, obj_type in all_obj_infos:
if obj_type not in type2infos:
type2infos[obj_type] = []
type2infos[obj_type].extend(obj_infos)
out_tree = []
for obj_type in type2infos:
type_dict = {}
type_data = None
for key in data:
for dic in data[key]:
if dic["name"] == obj_type:
type_data = dic
break
type_dict["name"] = obj_type
type_dict["path"] = type_data["path"]
base_x, base_y, base_z = (
type_data["cbottom"][0],
type_data["cbottom"][1],
type_data["cbottom"][2],
)
transforms = []
for obj_info in type2infos[obj_type]:
# print(obj_info)
transform = {}
forw = obj_info[3]
scalex, scaley, scalez = 1, 1, 1
if obj_type == "Flower_A" or obj_type == "Flower_B" or obj_type == "Flower_C" or obj_type == "Flower_D":
rand_scale = random.uniform(1.5, 2)
scalex = scaley = scalez = rand_scale
elif (
obj_type == "Lotus_A"
or obj_type == "Lotus_B"
or obj_type == "Lotus_Flower_A"
or obj_type == "Lotus_Flower_B"
):
rand_scale = random.uniform(1.2, 1.8)
scalex = scaley = scalez = rand_scale
elif obj_type == "Bamboo_A" or obj_type == "Bamboo_B" or obj_type == "Bamboo_C":
scaley = 1.3
elif obj_type == "Building_A" or obj_type == "Building_B" or obj_type == "Building_C":
scaley = 1.3
elif obj_type == "Wall_400x300":
scaley = 1.5
elif obj_type == "BushBig":
rand_scale = random.uniform(0.7, 1.5)
scalex = scalez = rand_scale
scaley = rand_scale * random.uniform(1.6, 2.5)
elif obj_type == "Plant_A" or obj_type == "Plant_B" or obj_type == "shaggy_soldier":
scalex = scaley = scalez = 1.5
elif obj_type == "SM_SquareBush":
scaley = 1.5
elif obj_type == "TH_Rock_A" or obj_type == "TH_Rock_B":
rand_scale = random.uniform(2, 3)
scalex = scaley = scalez = rand_scale
elif obj_type == "Rock_A" or obj_type == "Rock_B" or obj_type == "Rock_C":
rand_scale = random.uniform(7.5, 10)
scaley = rand_scale
scalex = scalez = 5
elif obj_type == "hugetree":
rand_scale = random.uniform(1.5, 2)
scalex = scaley = scalez = rand_scale
elif obj_type == "Bush01" or obj_type == "Bush02" or obj_type == "Bush03":
rand_scale = random.uniform(1.5, 2)
scalex = scaley = scalez = rand_scale
elif obj_type == "SM_RoundBush" or obj_type == "SM_RoundBush2":
scalex = scaley = scalez = 1.3
rotated_x, rotated_z = base_x * cos(-forw) - base_z * sin(-forw), base_x * sin(-forw) + base_z * cos(-forw)
transform["position"] = {
"x": obj_info[0] + rotated_x * scalex,
"y": obj_info[1] - base_y,
"z": obj_info[2] - rotated_z * scalez,
}
quaternion = euler2quaternion(0, -forw, 0)
transform["rotation"] = {"x": quaternion[0], "y": quaternion[1], "z": quaternion[2], "w": quaternion[3]}
transform["scale"] = {"x": scalex, "y": scaley, "z": scalez}
transforms.append(transform)
type_dict["transforms"] = transforms
out_tree.append(type_dict)
out = {"tree": out_tree}
out["height_map_path"] = "height_map_" + str(gen_idx) + suffix + ".png"
out["label_map_path"] = "label_map_" + str(gen_idx) + suffix + ".png"
out["max_height"] = MAX_HEIGHT
out["water_height"] = WATER_HEIGHT * MAX_HEIGHT
out["map_width"] = MAP_W
out["map_height"] = MAP_H
out["real_width"] = (W + 2) * RL
out["real_height"] = (H + 2) * RL * MAP_W / MAP_H
out["width_offset"] = -RL
out["height_offset"] = -RL
out_viewpoints = []
for i in range(len(view_points)):
out_viewpoints.append(
{
"x": view_points[i][0],
"y": view_points[i][1],
"z": view_points[i][2],
"xrot": view_points[i][3],
"yrot": view_points[i][4],
}
)
out["viewpoints"] = out_viewpoints
outpath = "outputs/scene_" + str(gen_idx) + suffix + ".json"
with open(outpath, "w") as outf:
json.dump(out, outf)
def random_placing(
poly_: Polygon, size_list: List[Tuple[float, float]], ratio: float, override: bool, buffer: float = 0
) -> List[Tuple[float, float, int]]:
"""
randomly place objects in the polygon
"""
poly = poly_.buffer(-buffer) if buffer > 0 else deepcopy(poly_)
if poly.area<0.1:
return []
target_area = poly.area * ratio
now_area = 0
type_areas = [size[0] * size[1] for size in size_list]
point_and_types = []
minx, minz, maxx, maxz = poly.bounds
num_types = len(size_list)
continuous_fail = 0
while True:
typ = random.randint(0, num_types - 1)
x = random.uniform(minx, maxx)
z = random.uniform(minz, maxz)
if poly.contains(Point(x, z)):
valid = True
if not override:
for px, pz, p_typ in point_and_types:
if (
abs(px - x) < (size_list[p_typ][0] + size_list[typ][0]) / 2
and abs(pz - z) < (size_list[p_typ][1] + size_list[typ][1]) / 2
):
valid = False
break
if valid:
point_and_types.append((x, z, typ))
now_area += type_areas[typ]
continuous_fail = 0
if now_area >= target_area:
break
else:
continuous_fail += 1
if continuous_fail > 50:
override = True
return point_and_types
def grid_random_placing(
poly_: Polygon, size_list: List[Tuple[float, float]], gx: float, gz: float
) -> List[Tuple[float, float, int]]:
"""
gx, gz: size of the grid
randomly place objects in each grid
"""
poly = deepcopy(poly_)
point_and_types = []
minx, minz, maxx, maxz = poly.bounds
wnum, hnum = int((maxx - minx) / gx), int((maxz - minz) / gz)
if wnum == 0 or hnum == 0:
return []
gx, gz = (maxx - minx) / wnum, (maxz - minz) / hnum
num_types = len(size_list)
for i in range(wnum):
for j in range(hnum):
typ = random.randint(0, num_types - 1)
xl, xh, zl, zh = minx + i * gx, minx + (i + 1) * gx, minz + j * gz, minz + (j + 1) * gz
mx, mz = size_list[typ][0], size_list[typ][1]
for k in range(10):
x, z = random.uniform(xl + mx / 2, xh - mx / 2), random.uniform(zl + mz / 2, zh - mz / 2)
if xh - xl <= mx:
x = (xh + xl) / 2
if zh - zl <= mz:
z = (zh + zl) / 2
if poly.contains(Point(x, z)):
point_and_types.append((x, z, typ))
break
return point_and_types
def group_random_placing(
poly_: Polygon,
size_list: List[Tuple[float, float]],
ratio: float,
buffer: float,
group_range: float,
group_num: int,
) -> List[Tuple[float, float, int]]:
poly = poly_.buffer(-group_range - buffer)
target_area = poly.area * ratio
now_area = 0
type_areas = [size[0] * size[1] for size in size_list]
point_and_types = []
minx, minz, maxx, maxz = poly.bounds
num_types = len(size_list)
while True:
typ = random.randint(0, num_types - 1)
x = random.uniform(minx, maxx)
z = random.uniform(minz, maxz)
if poly.contains(Point(x, z)):
for i in range(group_num):
rand_angle = random.uniform(0, 2 * pi)
rand_radius = random.uniform(0, group_range)
new_x = x + rand_radius * cos(rand_angle)
new_z = z + rand_radius * sin(rand_angle)
point_and_types.append((new_x, new_z, typ))
now_area += type_areas[typ]
if now_area >= target_area:
break
if now_area >= target_area:
break
return point_and_types
def maze_random_placing(
poly_: Polygon, size_list: List[Tuple[float, float]], gx: float, gz: float, ratio: float
) -> List[Tuple[float, float, int]]:
"""
gx, gz: size of the grid
randomly generate a maze and place objects (not exceeding ratio)
"""
poly = deepcopy(poly_)
point_and_types = []
minx, minz, maxx, maxz = poly.bounds
wnum, hnum = int((maxx - minx) / gx), int((maxz - minz) / gz)
if wnum == 0 or hnum == 0:
return []
gx, gz = (maxx - minx) / wnum, (maxz - minz) / hnum
target_area = poly.area * ratio
target_num = int(target_area / (gx * gz)) + 1
num_types = len(size_list)
road = set()
dxy = [0, 2, 0, -2, 0]
def maze_dfs(curr_pos):
road.add(curr_pos)
stack = [curr_pos]
while stack:
curr_pos = stack.pop()
p = [0, 1, 2, 3]
random.shuffle(p)
for i in p:
next_pos = (curr_pos[0] + dxy[i], curr_pos[1] + dxy[i + 1])
if 0 <= next_pos[0] < wnum and 0 <= next_pos[1] < hnum and next_pos not in road:
road.add(((curr_pos[0] + next_pos[0]) // 2, (curr_pos[1] + next_pos[1]) // 2))
road.add(next_pos)
stack.append(next_pos)
maze_dfs((0, 0))
candidates = []
for i in range(wnum):
for j in range(hnum):
x, z = (i + 0.5) * gx + minx, (j + 0.5) * gz + minz
if poly.contains(Point(x, z)) and ((i, j) not in road):
candidates.append((x, z))
random.shuffle(candidates)
for i in range(min(target_num, len(candidates))):
typ = random.randint(0, num_types - 1)
point_and_types.append((candidates[i][0], candidates[i][1], typ))
return point_and_types
def along_bordered_placing(poly_: Polygon, mx: float, mz: float, step: float, layers: int) -> List[Tuple[float, float]]:
"""
mx, mz: boundbox of the object
step: distance between two objects
layers: number of layers
place objects along the border of the polygon
"""
poly = deepcopy(poly_)
points = []
for k in range(layers):
if k == 0:
poly = poly.buffer(-max(mx, mz) / 2)
else:
poly = poly.buffer(-step)
if not isinstance(poly, Polygon):
break
boundary_ring = poly.exterior
layer_num = max(int(boundary_ring.length / step) - 1, 1)
for i in range(layer_num):
point = boundary_ring.interpolate(i * step)
points.append((point.x, point.y))
return points
def override_height(
height_map: List[List[float]], poly: Polygon, height: float, buffer: float
) -> List[Tuple[int, int]]:
"""
modify values in height_map
region in poly will be set to height
units near poly will be smoothed, buffer is the distance
"""
points = [xy2idx(x, y) for x, y in poly.exterior.coords]
idx_poly = Polygon(points)
# decide what units are covered by the idx_poly
minx, miny, maxx, maxy = idx_poly.bounds
ratio = max(MAP_W, MAP_H) / (RL * max(W, H))
x1, z1 = int(minx - buffer * ratio), int(miny - buffer * ratio)
x2, z2 = int(maxx + buffer * ratio) + 1, int(maxy + buffer * ratio) + 1
idxs = []
for i in range(x1, x2):
for j in range(z1, z2):
if i < 0 or i >= MAP_W or j < 0 or j >= MAP_H:
continue
pcenter = Point(i + 0.5, j + 0.5)
if idx_poly.contains(pcenter):
height_map[i][j] = height
idxs.append((i, j))
else:
dis = pcenter.distance(idx_poly) / ratio
if dis < buffer:
height_map[i][j] = height * (1 - dis / buffer) + height_map[i][j] * dis / buffer
return idxs
def along_line_placing(
point_locs: List[Tuple[float, float]], step: float, width: float
) -> List[Tuple[float, float, float]]:
loc_and_forws = []
last_idx, last_loc, last_forw = 0, p(deepcopy(point_locs[0])), None
while True:
next_idx = None
for i in range(last_idx, len(point_locs)):
next_idx = i
if eu_dist(last_loc, point_locs[i]) > step:
break
if eu_dist(last_loc, point_locs[next_idx]) <= 0.25:
break
forw_vec = (point_locs[next_idx][0] - last_loc[0], point_locs[next_idx][1] - last_loc[1])
forw = np.arctan2(forw_vec[1], forw_vec[0])
sp, ep = None, None
if last_forw is None:
sp, ep = last_loc, last_loc + step * p(cos(forw), sin(forw))
else:
a_dist = ang_dist(forw, last_forw)
if eu_dist(last_loc, point_locs[next_idx]) + sin(a_dist / 2) * width * 0.75 < step:
ep = point_locs[next_idx]
sp = ep - step * p(cos(forw), sin(forw))
else:
sp = last_loc - sin(a_dist / 2) * width * p(cos(forw), sin(forw)) * 0.75
ep = sp + step * p(cos(forw), sin(forw))
midx, midy = (sp[0] + ep[0]) / 2, (sp[1] + ep[1]) / 2
loc_and_forws.append((midx, midy, forw))
xl, yl, xh, yh = (
min(sp[0], ep[0]) - 1e-3,
min(sp[1], ep[1]) - 1e-3,
max(sp[0], ep[0]) + 1e-3,
max(sp[1], ep[1]) + 1e-3,
)
xlast, ylast = point_locs[-1][0], point_locs[-1][1]
if xl <= xlast <= xh and yl <= ylast <= yh:
break
last_loc, last_forw, last_idx = ep, forw, next_idx
return loc_and_forws
def build_zigzagbridge(
height_map: List[List[float]], point_locs: List[Tuple[float, float]], data: dict
) -> List[Tuple[float, float, float, float]]:
step, height, width = data["size"][0], data["size"][1], data["size"][2]
loc_and_forws = along_line_placing(point_locs, step, width)
start_obj, end_obj = loc_and_forws[0], loc_and_forws[-1]
sp = p(start_obj[0], start_obj[1]) - step / 2 * p(cos(start_obj[2]), sin(start_obj[2]))
ep = p(end_obj[0], end_obj[1]) + step / 2 * p(cos(end_obj[2]), sin(end_obj[2]))
lakeside_h = max(min(get_height(height_map, sp[0], sp[1]), get_height(height_map, ep[0], ep[1])), 0.1)
base_height = lakeside_h * MAX_HEIGHT - (height - 1)
obj_infos = []
for loc_and_forw in loc_and_forws:
obj_infos.append((loc_and_forw[0], base_height, loc_and_forw[1], loc_and_forw[2]))
return obj_infos
def build_bridge(
height_map: List[List[float]], point_locs: List[Tuple[float, float]], data: dict
) -> Tuple[List[Tuple[float, float, float]], List[Tuple[int, int]]]:
length = data["size"][0]
edge_length = 0
for i in range(len(point_locs) - 1):
edge_length += eu_dist(point_locs[i], point_locs[i + 1])
mid_idx, now_length = -1, 0
for i in range(len(point_locs) - 1):
if now_length / edge_length >= 0.5:
mid_idx = i
break
now_length += eu_dist(point_locs[i], point_locs[i + 1])
idx1, idx2 = deepcopy(mid_idx), deepcopy(mid_idx)
while True:
if idx1 > 0:
idx1 -= 1
if eu_dist(point_locs[idx1], point_locs[idx2]) >= length:
break
if idx2 < len(point_locs) - 1:
idx2 += 1
if eu_dist(point_locs[idx1], point_locs[idx2]) >= length:
break
sp, ep = p(point_locs[idx1]), p(point_locs[idx2])
midp, forw = (sp + ep) / 2, np.arctan2(ep[1] - sp[1], ep[0] - sp[0])
sp, ep = midp - length / 2 * p(cos(forw), sin(forw)), midp + length / 2 * p(cos(forw), sin(forw))
poly1, poly2 = None, None
edges1 = [(point_locs[i], point_locs[i + 1]) for i in range(idx1)] + [(point_locs[idx1], sp)]
edges2 = [(ep, point_locs[idx2])] + [(point_locs[i], point_locs[i + 1]) for i in range(idx2, len(point_locs) - 1)]
for edge in edges1:
p1, p2 = p(edge[0]), p(edge[1])
tang = p(p2[0] - p1[0], p2[1] - p1[1])
tang = tang / np.linalg.norm(tang)
norm = p(p2[1] - p1[1], p1[0] - p2[0])
norm = norm / np.linalg.norm(norm)
poly = Polygon(
[
p1 + norm * MAIN_ROAD_WIDTH / 2 - tang,
p1 - norm * MAIN_ROAD_WIDTH / 2 - tang,
p2 - norm * MAIN_ROAD_WIDTH / 2 + tang,
p2 + norm * MAIN_ROAD_WIDTH / 2 + tang,
]
)
if poly1 is None:
poly1 = poly
else:
poly1 = poly1.union(poly)
for edge in edges2:
p1, p2 = p(edge[0]), p(edge[1])
tang = p(p2[0] - p1[0], p2[1] - p1[1])
tang = tang / np.linalg.norm(tang)
norm = p(p2[1] - p1[1], p1[0] - p2[0])
norm = norm / np.linalg.norm(norm)
poly = Polygon(
[
p1 + norm * MAIN_ROAD_WIDTH / 2 - tang,
p1 - norm * MAIN_ROAD_WIDTH / 2 - tang,
p2 - norm * MAIN_ROAD_WIDTH / 2 + tang,
p2 + norm * MAIN_ROAD_WIDTH / 2 + tang,
]
)
if poly2 is None:
poly2 = poly
else:
poly2 = poly2.union(poly)
poly1, poly2 = poly1.simplify(0, False), poly2.simplify(0, False)
base_height_ratio = 0.1
road_idxs = override_height(height_map, poly1, base_height_ratio, 3) + override_height(
height_map, poly2, base_height_ratio, 3
)
obj_infos = [(midp[0], base_height_ratio * MAX_HEIGHT - 1.4, midp[1], forw)]
return obj_infos, road_idxs
def build_wall(
height_map: List[List[float]], point_locs: List[Tuple[float, float]], data: dict
) -> List[Tuple[float, float, float, float]]:
step, width = 4, data["size"][2]
locs_and_forws = along_line_placing(point_locs, step, width)
obj_infos = []
for loc_and_forw in locs_and_forws:
midp, forw = p(loc_and_forw[0], loc_and_forw[1]), loc_and_forw[2]
sp, ep = midp - step / 2 * p(cos(forw), sin(forw)), midp + step / 2 * p(cos(forw), sin(forw))
h1, h2 = get_height(height_map, sp[0], sp[1]), get_height(height_map, ep[0], ep[1])
obj_infos.append((midp[0], min(h1, h2) * MAX_HEIGHT, midp[1], forw))
return obj_infos
def build_entrance(
height_map: List[List[float]],
point_locs: List[Tuple[float, float]],
data: dict,
entrance_points: List[Tuple[float, float]],
) -> List[Tuple[float, float, float, float]]:
step, width = 4, data["size"][2]
locs_and_forws = along_line_placing(point_locs, step, width)
obj_infos = []
for loc_and_forw in locs_and_forws:
midp, forw = p(loc_and_forw[0], loc_and_forw[1]), loc_and_forw[2]
far_from_entrance = True
for entrance_point in entrance_points:
if eu_dist(midp, entrance_point) < 5:
far_from_entrance = False
break
if far_from_entrance:
sp, ep = midp - step / 2 * p(cos(forw), sin(forw)), midp + step / 2 * p(cos(forw), sin(forw))
h1, h2 = get_height(height_map, sp[0], sp[1]), get_height(height_map, ep[0], ep[1])
obj_infos.append((midp[0], min(h1, h2) * MAX_HEIGHT, midp[1], forw))
return obj_infos
def add_lotus(
height_map: List[List[float]], poly: Polygon, datas: List[dict]
) -> List[Tuple[List[Tuple[float, float, float, float]], str]]:
size_list = [(dat["size"][0], dat["size"][2]) for dat in datas]
if poly.area > 4000:
return []
point_and_types = random_placing(poly, size_list, 0.12, True)
obj_infos_dict = {}
for typ_idx in range(len(datas)):
obj_infos_dict[typ_idx] = []
model_height = WATER_HEIGHT * MAX_HEIGHT - 0.05
for point_and_type in point_and_types:
x, z, typ = point_and_type
if get_height(height_map, x, z) < WATER_HEIGHT - 0.005:
obj_infos_dict[typ].append((x, model_height, z, random.uniform(0, 2 * pi)))
obj_infos_list = []
for typ_idx in obj_infos_dict:
obj_infos_list.append((obj_infos_dict[typ_idx], datas[typ_idx]["name"]))
return obj_infos_list
def add_lakerock(
height_map: List[List[float]], poly: Polygon, datas: List[dict]
) -> List[Tuple[List[Tuple[float, float, float, float]], str]]:
size_list = [(dat["size"][0], dat["size"][2]) for dat in datas]
point_and_types = random_placing(poly, size_list, 0.001, True)
obj_infos_dict = {}
for typ_idx in range(len(datas)):
obj_infos_dict[typ_idx] = []
base_height = WATER_HEIGHT * MAX_HEIGHT - 0.6
for point_and_type in point_and_types:
x, z, typ = point_and_type
if get_height(height_map, x, z) < WATER_HEIGHT - 0.005:
obj_infos_dict[typ].append((x, base_height + random.uniform(-0.2, 0.2), z, random.uniform(0, 2 * pi)))
obj_infos_list = []
for typ_idx in obj_infos_dict:
obj_infos_list.append((obj_infos_dict[typ_idx], datas[typ_idx]["name"]))
return obj_infos_list
def add_rockmaze(
height_map: List[List[float]],
poly: Polygon,
hill_rock_data: List[dict],
lake_rock_data: List[dict],
tree_data: List[dict],
) -> List[Tuple[List[Tuple[float, float, float, float]], str]]:
hillrock_size_list = [(dat["size"][0], dat["size"][2]) for dat in hill_rock_data]
# lakerock_size_list = [(dat["size"][0], dat["size"][2]) for dat in lake_rock_data]
tree_size_list = [(dat["size"][0], dat["size"][2]) for dat in tree_data]
# lakerock_point_and_types = maze_random_placing(poly, lakerock_size_list, 3, 3, 0.02)
hillrock_point_and_types = group_random_placing(poly, hillrock_size_list, 0.004, 2, 2, 3)
tree_point_and_types = random_placing(poly, tree_size_list, 0.2, True)
hillrock_obj_infos_dict, lakerock_obj_infos_dict, tree_obj_infos_dict = {}, {}, {}
for typ_idx in range(len(hill_rock_data)):
hillrock_obj_infos_dict[typ_idx] = []
for typ_idx in range(len(lake_rock_data)):
lakerock_obj_infos_dict[typ_idx] = []
for typ_idx in range(len(tree_data)):
tree_obj_infos_dict[typ_idx] = []
for point_and_type in hillrock_point_and_types:
x, z, typ = point_and_type
y = get_height(height_map, x, z) * MAX_HEIGHT
hillrock_obj_infos_dict[typ].append((x, y, z, random.uniform(0, 2 * pi)))
# for point_and_type in lakerock_point_and_types:
# x, z, typ = point_and_type
# x += random.uniform(-1, 1)
# z += random.uniform(-1, 1)
# y = get_height(height_map, x, z) * MAX_HEIGHT
# lakerock_obj_infos_dict[typ].append((x, y, z, random.uniform(0, 2 * pi)))
for point_and_type in tree_point_and_types:
x, z, typ = point_and_type
y = get_height(height_map, x, z) * MAX_HEIGHT
tree_obj_infos_dict[typ].append((x, y, z, random.uniform(0, 2 * pi)))
obj_infos_list = []
for typ_idx in hillrock_obj_infos_dict:
obj_infos_list.append((hillrock_obj_infos_dict[typ_idx], hill_rock_data[typ_idx]["name"]))
for typ_idx in lakerock_obj_infos_dict:
obj_infos_list.append((lakerock_obj_infos_dict[typ_idx], lake_rock_data[typ_idx]["name"]))
for typ_idx in tree_obj_infos_dict:
obj_infos_list.append((tree_obj_infos_dict[typ_idx], tree_data[typ_idx]["name"]))
return obj_infos_list
def add_bushes(
height_map: List[List[float]], poly: Polygon, rectbush_datas: List[dict], bigbush_datas: List[dict]
) -> List[Tuple[List[Tuple[float, float, float, float]], str]]:
rectbush_size_list = [(dat["size"][0], dat["size"][2]) for dat in rectbush_datas]
bigbush_size_list = [(dat["size"][0], dat["size"][2]) for dat in bigbush_datas]
poly_area = poly.area
point_and_types = None
typeflag = 0
if poly_area < 500:
point_and_types = grid_random_placing(poly, rectbush_size_list, 2, 2)
else:
if poly_area < 2000 and random.random() < 0.7:
point_and_types = maze_random_placing(poly, rectbush_size_list, 2, 2, 0.45)
else:
typeflag = 1
point_and_types = random_placing(poly, bigbush_size_list, 0.3, True, 3)
obj_infos_dict = {}
length = len(rectbush_datas) if typeflag == 0 else len(bigbush_datas)
for typ_idx in range(length):
obj_infos_dict[typ_idx] = []
for point_and_type in point_and_types:
x, z, typ = point_and_type
y = get_height(height_map, x, z) * MAX_HEIGHT
obj_infos_dict[typ].append((x, y, z, random.uniform(0, 2 * pi)))
obj_infos_list = []
for typ_idx in obj_infos_dict:
if typeflag == 0:
obj_infos_list.append((obj_infos_dict[typ_idx], rectbush_datas[typ_idx]["name"]))
else:
obj_infos_list.append((obj_infos_dict[typ_idx], bigbush_datas[typ_idx]["name"]))
return obj_infos_list
def add_few_trees(
height_map: List[List[float]], poly: Polygon, datas: List[dict]
) -> List[Tuple[List[Tuple[float, float, float, float]], str]]:
size_list = [(dat["size"][0], dat["size"][2]) for dat in datas]
point_and_types = random_placing(poly, size_list, 0.2, True)
obj_infos_dict = {}
for typ_idx in range(len(datas)):
obj_infos_dict[typ_idx] = []
for point_and_type in point_and_types:
x, z, typ = point_and_type
y = get_height(height_map, x, z) * MAX_HEIGHT - 0.2
obj_infos_dict[typ].append((x, y, z, random.uniform(0, 2 * pi)))
obj_infos_list = []
for typ_idx in obj_infos_dict:
obj_infos_list.append((obj_infos_dict[typ_idx], datas[typ_idx]["name"]))
return obj_infos_list
def add_bamboos(
height_map: List[List[float]], poly: Polygon, datas: List[dict]
) -> List[Tuple[List[Tuple[float, float, float, float]], str]]:
size_list = [(dat["size"][0], dat["size"][2]) for dat in datas]
poly_area = poly.area
point_and_types = None
if poly_area < 1000:
point_and_types = grid_random_placing(poly, size_list, 2, 2)
else:
point_and_types = random_placing(poly, size_list, 0.6, True)
obj_infos_dict = {}
for typ_idx in range(len(datas)):
obj_infos_dict[typ_idx] = []
for point_and_type in point_and_types:
x, z, typ = point_and_type
y = get_height(height_map, x, z) * MAX_HEIGHT
obj_infos_dict[typ].append((x, y, z, random.uniform(0, 2 * pi)))
obj_infos_list = []
for typ_idx in obj_infos_dict:
obj_infos_list.append((obj_infos_dict[typ_idx], datas[typ_idx]["name"]))
return obj_infos_list
def add_trees(
height_map: List[List[float]], poly: Polygon, datas: List[dict]
) -> List[Tuple[List[Tuple[float, float, float, float]], str]]:
size_list = [(dat["size"][0], dat["size"][2]) for dat in datas]
poly_area = poly.area
point_and_types = random_placing(poly, size_list, 1.0, True)
obj_infos_dict = {}
for typ_idx in range(len(datas)):
obj_infos_dict[typ_idx] = []
for point_and_type in point_and_types:
x, z, typ = point_and_type
y = get_height(height_map, x, z) * MAX_HEIGHT - 0.2
obj_infos_dict[typ].append((x, y, z, random.uniform(0, 2 * pi)))
obj_infos_list = []
for typ_idx in obj_infos_dict:
obj_infos_list.append((obj_infos_dict[typ_idx], datas[typ_idx]["name"]))
return obj_infos_list
def add_pavilion(
terrain_labels: List[List[List[float]]],
height_map: List[List[float]],
poly: Polygon,
pav_datas: List[dict],
tree_datas: List[dict],
) -> List[Tuple[List[Tuple[float, float, float, float]], str]]: # TODO
poly_area = poly.area
obj_infos_list = []
existing_polys = []
minx, minz, maxx, maxz = poly.bounds
# add pavilions
bminx, bminz, bmaxx, bmaxz = minx + 4, minz + 4, maxx - 4, maxz - 4
pav_size_list = [(dat["size"][0], dat["size"][2]) for dat in pav_datas]
num_pavs = min(3, int(poly_area / 300))
for k in range(num_pavs):
x, y, z, pav_poly, pav_type = [None for _ in range(5)]
for att in range(100):
rand_type = random.randint(0, len(pav_size_list) - 1)
cx, cz = random.uniform(bminx, bmaxx), random.uniform(bminz, bmaxz)
xl, xh, zl, zh = (
cx - pav_size_list[rand_type][0] / 2,
cx + pav_size_list[rand_type][0] / 2,
cz - pav_size_list[rand_type][1] / 2,
cz + pav_size_list[rand_type][1] / 2,
)
building_poly = Polygon([(xl, zl), (xl, zh), (xh, zh), (xh, zl)])
if poly.contains(building_poly):
valid = True
for ex_poly in existing_polys:
if building_poly.distance(ex_poly) < 2:
valid = False
break
if valid:
heights = [
get_height(height_map, xl, zl),
get_height(height_map, xl, zh),
get_height(height_map, xh, zl),
get_height(height_map, xh, zh),
]
if max(heights) - min(heights) > 0.01:
continue
x, y, z, pav_poly, pav_type = cx, sum(heights) / 4, cz, building_poly, rand_type
break
if x is None:
continue
labeling_area(terrain_labels, height_map, pav_poly, 4, 0.2)
existing_polys.append(pav_poly)
y *= MAX_HEIGHT
if pav_datas[pav_type]["name"] == "Pavillion_Medium":
y -= 1
obj_infos_list.append(([(x, y, z, random.uniform(0, 2 * pi))], pav_datas[pav_type]["name"]))
# add trees
tree_area = deepcopy(poly)
for ex_poly in existing_polys:
tree_area = tree_area.difference(ex_poly)
tree_size_list = [(dat["size"][0], dat["size"][2]) for dat in tree_datas]
point_and_types = random_placing(tree_area, tree_size_list, 1, True)
for point_and_type in point_and_types:
x, z, typ = point_and_type
y = get_height(height_map, x, z) * MAX_HEIGHT - 0.2
obj_infos_list.append(([(x, y, z, random.uniform(0, 2 * pi))], tree_datas[typ]["name"]))
return obj_infos_list
def add_hugetree(
height_map: List[List[float]], poly: Polygon, datas: List[dict]
) -> List[Tuple[List[Tuple[float, float, float, float]], str]]:
size_list = [(dat["size"][0], dat["size"][2]) for dat in datas]
poly_area = poly.area
point_and_types = random_placing(poly, size_list, 0.1, True, 5)
obj_infos_dict = {}
for typ_idx in range(len(datas)):
obj_infos_dict[typ_idx] = []
for point_and_type in point_and_types:
x, z, typ = point_and_type
y = get_height(height_map, x, z) * MAX_HEIGHT - 0.2
obj_infos_dict[typ].append((x, y, z, random.uniform(0, 2 * pi)))
obj_infos_list = []
for typ_idx in obj_infos_dict:
obj_infos_list.append((obj_infos_dict[typ_idx], datas[typ_idx]["name"]))
return obj_infos_list
def add_plantbeds(
terrain_labels: List[List[List[float]]],
height_map: List[List[float]],
poly: Polygon,
flower_datas: List[dict],
bush_datas: List[dict],
) -> List[Tuple[List[Tuple[float, float, float, float]], str]]:
flower_size_list, bush_size_list = [(dat["size"][0], dat["size"][2]) for dat in flower_datas], [
(dat["size"][0], dat["size"][2]) for dat in bush_datas
]
num_flower_types, num_bush_types = len(flower_size_list), len(bush_size_list)
rx, rz, gx, gz = 7.5, 6, 9.5, 7.5
minx, miny, maxx, maxy = poly.bounds
wnum, hnum = int((maxx - minx) / gx), int((maxy - miny) / gz)
is_flower = [[0 for j in range(hnum)] for i in range(wnum)]
pattern = random.randint(0, 15)
for i in range(wnum):
for j in range(hnum):
if (
(i % 2 == 0 and j % 2 == 0 and pattern == 0)
or (i % 2 == 0 and j % 2 == 1 and pattern == 1)
or (i % 2 == 1 and j % 2 == 0 and pattern == 2)
or (i % 2 == 1 and j % 2 == 1 and pattern == 3)
):
is_flower[i][j] = 1
elif (
(i % 2 == 0 and pattern == 4)
or (i % 2 == 1 and pattern == 5)
or (j % 2 == 0 and pattern == 6)
or (j % 2 == 1 and pattern == 7)
):
is_flower[i][j] = 1
elif (
(i % 4 < 2 and j % 4 < 2 and pattern == 8)
or (i % 4 < 2 and j % 4 >= 2 and pattern == 9)
or (i % 4 >= 2 and j % 4 < 2 and pattern == 10)
or (i % 4 >= 2 and j % 4 >= 2 and pattern == 11)
):
is_flower[i][j] = 1
elif pattern >= 12 and random.random() < 0.5:
is_flower[i][j] = 1
obj_infos_list = []
for i in range(wnum):
for j in range(hnum):
cx, cz = minx + (i + 0.5) * gx, miny + (j + 0.5) * gz
xl, xh, zl, zh = cx - rx / 2, cx + rx / 2, cz - rz / 2, cz + rz / 2
bed_poly = Polygon([(xl, zl), (xl, zh), (xh, zh), (xh, zl)])
if poly.contains(bed_poly):
labeling_area(terrain_labels, height_map, bed_poly, 4, 0.2)
if is_flower[i][j] == 0:
typ = random.randint(0, num_bush_types - 1)
point_and_types = grid_random_placing(bed_poly, [bush_size_list[typ]], 2, 2)
obj_infos = []
for point_and_type in point_and_types:
x, z, _ = point_and_type
y = get_height(height_map, x, z) * MAX_HEIGHT
obj_infos.append((x, y, z, random.uniform(0, 2 * pi)))
obj_infos_list.append((obj_infos, bush_datas[typ]["name"]))
else:
typ = random.randint(0, num_flower_types - 1)
point_and_types = random_placing(bed_poly, [flower_size_list[typ]], 0.3, False)
obj_infos = []
for point_and_type in point_and_types:
x, z, _ = point_and_type
y = get_height(height_map, x, z) * MAX_HEIGHT - 0.05
obj_infos.append((x, y, z, random.uniform(0, 2 * pi)))
obj_infos_list.append((obj_infos, flower_datas[typ]["name"]))
return obj_infos_list
def add_treelines(
terrain_labels: List[List[List[float]]], height_map: List[List[float]], poly: Polygon, datas: List[dict]
) -> List[Tuple[List[Tuple[float, float, float, float]], str]]:
size_list = [(dat["size"][0], dat["size"][2]) for dat in datas]
num_types = len(size_list)
rx, rz, gx, gz = 2, 2, 6, 6
minx, miny, maxx, maxy = poly.bounds
wnum, hnum = int((maxx - minx) / gx), int((maxy - miny) / gz)
obj_infos_list = []
for i in range(wnum):
for j in range(hnum):
cx, cz = minx + (i + 0.5) * gx, miny + (j + 0.5) * gz
xl, xh, zl, zh = cx - rx / 2, cx + rx / 2, cz - rz / 2, cz + rz / 2
bed_poly = Polygon([(xl, zl), (xl, zh), (xh, zh), (xh, zl)])
if poly.contains(bed_poly):
labeling_area(terrain_labels, height_map, bed_poly, 4, 0.2)
typ = random.randint(0, num_types - 1)
y = get_height(height_map, cx, cz) * MAX_HEIGHT - 0.2
obj_infos_list.append(([(cx, y, cz, random.uniform(0, 2 * pi))], datas[typ]["name"]))
return obj_infos_list
def add_building(
height_map: List[List[float]],
poly: Polygon,
buid_datas: List[dict],
statue_datas: List[dict],
tree_datas: List[dict],
) -> List[Tuple[List[Tuple[float, float, float, float]], str]]: # TODO
poly_area = poly.area
obj_infos_list = []
existing_polys = []
minx, minz, maxx, maxz = poly.bounds
# add main building (Building_A)
num_main_building = 1
if poly_area > 3000: