-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathobject_3d.py
773 lines (588 loc) · 28.2 KB
/
object_3d.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
import helper
import os
import ror_zip_file
import topography
import triangulate
import config
import ror_tobj_file
import ror_odef_file
VERTEX_PER_WALL = 4
object_index = 0
unsupported_roof_shape = {}
warn_gabled_roof = True
warn_hipped_roof = True
def create_all_object_file(nodes, height=config.data["building_level_height"], z=0.0,
wall_texture=config.data["wall_texture"], top_texture=config.data["top_texture"],
scale=1.0,
is_barrier=False, half_barrier=False, wall_texture_generator=None,
top_texture_generator=None,
roof_shape=None,
roof_height=None,
barrier_width=config.data["barrier_width"],
display_name=None,
group_z=None):
global object_index
if height is None:
height = config.data["building_level_height"]
# Find the highest z for all nodes of the object
base_z = config.data["ground_line"]
if topography.is_enabled():
base_z = 0.0
for node in nodes:
node_z = topography.get_z(node.lon, node.lat)
if node_z > base_z:
base_z = node_z
if z is None:
z = 0.0
is_object_above_ground = False
# Make object on the ground super high to avoid them to "float" when they are near a water coast
if z <= 0.0:
height += base_z
else:
z += base_z
is_object_above_ground = True
# Make sure objects from a bigger object use the same z
if group_z is not None:
z += (group_z - base_z)
need_roof = False
if is_roof_shape_supported(roof_shape) and roof_height is not None:
height -= roof_height
need_roof = True
object_index = object_index + 1
obj_name = "obj" + str(object_index)
if len(nodes) > 1:
if hasattr(nodes[0], 'lat'):
center_x, center_y, width, length, vertex = get_info_from_input_data(nodes, scale=scale, is_node=True,
keep_all_nodes=is_barrier)
else:
center_x, center_y, width, length, vertex = get_info_from_input_data(nodes, scale=scale, is_node=False,
keep_all_nodes=is_barrier)
if is_barrier is True:
vertex = create_additional_vertex_for_barrier(vertex, half_barrier, barrier_width)
else:
center_x, center_y, width, length, vertex = create_vertex_for_pillar(nodes[0])
wall_vertex_index, wall_face_qty, wall_vertex_str, wall_face_str = generate_wall(vertex, height)
if wall_vertex_str is None:
return None
if wall_texture_generator is not None:
wall_texture = wall_texture_generator(width, length)
if top_texture_generator is not None:
top_texture = top_texture_generator(width, length)
floor_face_qty = 0
floor_vertex_str = ""
floor_face_str = ""
if wall_texture == top_texture: # no submeshes with the same texture is allowed, so concatenate wall and top meshes
if need_roof:
top_vertex_index, top_face_qty, top_vertex_str, top_face_str = generate_roof(roof_shape, vertex, height,
roof_height,
wall_vertex_index)
else:
top_vertex_index, top_face_qty, top_vertex_str, top_face_str = generate_ceiling(vertex, height,
wall_vertex_index,
is_barrier)
if is_object_above_ground:
top_vertex_index, floor_face_qty, floor_vertex_str, floor_face_str = generate_ceiling(vertex, 0.0,
top_vertex_index,
is_barrier,
normal=-1.0)
generate_mesh_file(
[{"vertex_index": top_vertex_index, "face_qty": wall_face_qty + top_face_qty + floor_face_qty,
"vertex_str": wall_vertex_str + top_vertex_str + floor_vertex_str,
"face_str": wall_face_str + top_face_str + floor_face_str, "texture": wall_texture}], obj_name)
else:
if need_roof:
top_vertex_index, top_face_qty, top_vertex_str, top_face_str = generate_roof(roof_shape, vertex, height,
roof_height,
0)
else:
top_vertex_index, top_face_qty, top_vertex_str, top_face_str = generate_ceiling(vertex, height,
0, is_barrier)
if is_object_above_ground:
top_vertex_index, floor_face_qty, floor_vertex_str, floor_face_str = generate_ceiling(vertex,
0.0,
top_vertex_index,
is_barrier,
normal=-1.0)
generate_mesh_file(
[{"vertex_index": wall_vertex_index, "face_qty": wall_face_qty, "vertex_str": wall_vertex_str,
"face_str": wall_face_str, "texture": wall_texture},
{"vertex_index": top_vertex_index, "face_qty": top_face_qty + floor_face_qty,
"vertex_str": top_vertex_str + floor_vertex_str,
"face_str": top_face_str + floor_face_str, "texture": top_texture}], obj_name)
ror_tobj_file.add_object(center_x, center_y, z, 0.0, 0.0, 0.0, obj_name, display_name)
ror_odef_file.create_file(obj_name, collision=True)
def generate_wall(vertex, height):
vertex_str = ""
face_str = ""
vertex_index = 0
face_qty = 0
vertex_qty = len(vertex)
for i in range(vertex_qty):
# Create 4 vertex for a single wall
# 1-3
# | |
# 0-2
v0 = [vertex[i][0], vertex[i][1], 0.0]
v1 = [vertex[i][0], vertex[i][1], height]
v2 = [vertex[(i + 1) % vertex_qty][0], vertex[(i + 1) % vertex_qty][1],
0.0]
v3 = [vertex[(i + 1) % vertex_qty][0], vertex[(i + 1) % vertex_qty][1],
height]
vertex_str += create_vertex_str(v0, v2, v1, 0.0, 0.0)
vertex_str += create_vertex_str(v1, v0, v3, 0.0, 1.0)
vertex_str += create_vertex_str(v2, v3, v0, 1.0, 0.0)
vertex_str += create_vertex_str(v3, v1, v2, 1.0, 1.0)
# Create 2 faces (triangle) per wall
vertex_index = i * VERTEX_PER_WALL
face_str += create_face(vertex_index + 1, vertex_index, vertex_index + 2)
face_str += create_face(vertex_index + 2, vertex_index + 3, vertex_index + 1)
face_qty += 2
vertex_index += VERTEX_PER_WALL
return vertex_index, face_qty, vertex_str, face_str
def generate_ceiling(vertex, height, vertex_index, is_barrier, normal=1.0):
if is_barrier is False:
return generate_ceiling_for_building(vertex, height, vertex_index, normal)
else:
return generate_ceiling_for_barrier(vertex, height, vertex_index, normal)
def generate_ceiling_for_building(vertex2d, height, vertex_index, normal=1.0):
face_qty = 0
vertex_str = ""
face_str = ""
vertex = vertex2d[::-1] if triangulate.IsClockwise(
vertex2d) else vertex2d[:]
while len(vertex) >= 3:
ear = triangulate.GetEar(vertex)
if not ear:
break
# Add Z axis
if len(ear[0]) == 2:
ear[0].append(height)
else:
ear[0][2] = height
if len(ear[1]) == 2:
ear[1].append(height)
else:
ear[1][2] = height
if len(ear[2]) == 2:
ear[2].append(height)
else:
ear[2][2] = height
# TODO U,V are wrong here
vertex_str += create_vertex_with_normal_str(ear[0], [0.0, 0.0, normal], 0.0, 0.0)
vertex_str += create_vertex_with_normal_str(ear[1], [0.0, 0.0, normal], 0.0, 1.0)
vertex_str += create_vertex_with_normal_str(ear[2], [0.0, 0.0, normal], 1.0, 0.0)
face_str += create_face(vertex_index + 1, vertex_index + 2, vertex_index + 0)
vertex_index += 3
face_qty += 1
return vertex_index, face_qty, vertex_str, face_str
def generate_ceiling_for_barrier(vertex2d, height, vertex_index, normal=1.0):
face_qty = 0
vertex_str = ""
face_str = ""
# for barrier, the first half vertices are one side, the last half the other side
index = 0
vlen = len(vertex2d)
for v in vertex2d:
# 2 faces per first half vertex
v1 = vertex2d[index]
v2 = vertex2d[index + 1]
v3 = vertex2d[vlen - 1 - index]
v1.append(height)
v2.append(height)
v3.append(height)
vertex_str += create_vertex_with_normal_str(v1, [0.0, 0.0, normal], 0.0, 0.0)
vertex_str += create_vertex_with_normal_str(v2, [0.0, 0.0, normal], 0.0, 1.0)
vertex_str += create_vertex_with_normal_str(v3, [0.0, 0.0, normal], 1.0, 0.0)
face_str += create_face(vertex_index + 0, vertex_index + 1, vertex_index + 2)
vertex_index += 3
face_qty += 1
v1 = vertex2d[index + 1]
v2 = vertex2d[vlen - 1 - index]
v3 = vertex2d[vlen - 2 - index]
v1.append(height)
v2.append(height)
v3.append(height)
vertex_str += create_vertex_with_normal_str(v1, [0.0, 0.0, 1.0], 0.0, 0.0)
vertex_str += create_vertex_with_normal_str(v2, [0.0, 0.0, 1.0], 0.0, 1.0)
vertex_str += create_vertex_with_normal_str(v3, [0.0, 0.0, 1.0], 1.0, 0.0)
face_str += create_face(vertex_index + 0, vertex_index + 2, vertex_index + 1)
vertex_index += 3
face_qty += 1
index += 1
if index >= (vlen / 2) - 1:
break
return vertex_index, face_qty, vertex_str, face_str
def is_roof_shape_supported(shape):
if shape in unsupported_roof_shape:
return False
if shape is None:
return False
if shape == "pyramidal" or shape == "flat" or shape == "gabled" or shape == "hipped":
return True
else:
unsupported_roof_shape[shape] = True
print("Unsupported roof shape", shape)
return False
def generate_roof(shape, vertex2d, height, roof_height, vertex_index):
if shape == "pyramidal":
return generate_roof_pyramidal(vertex2d, height, roof_height, vertex_index)
if shape == "flat":
return generate_ceiling(vertex2d, height, vertex_index, is_barrier=False)
if shape == "gabled":
return generate_roof_gabled(vertex2d, height, roof_height, vertex_index)
if shape == "hipped":
return generate_roof_hipped(vertex2d, height, roof_height, vertex_index)
def generate_roof_pyramidal(vertex2d, height, roof_height, vertex_index):
face_qty = 0
vertex_str = ""
face_str = ""
# find highest and lowest x and y
x_high = vertex2d[0][0]
x_low = vertex2d[0][0]
y_high = vertex2d[0][1]
y_low = vertex2d[0][1]
for v in vertex2d:
if v[0] > x_high:
x_high = v[0]
if v[0] < x_low:
x_low = v[0]
if v[1] > y_high:
y_high = v[1]
if v[1] < y_low:
y_low = v[1]
# calculate top coordinates
top_x = (x_high + x_low) / 2
top_y = (y_high + y_low) / 2
top_vertex = [top_x, top_y, height + roof_height]
index = 0
for v in vertex2d:
v1 = vertex2d[index]
v2 = vertex2d[(index + 1) % len(vertex2d)]
v1.append(height)
v2.append(height)
norm = helper.calc_norm([v1, v2, top_vertex])
vertex_str += create_vertex_with_normal_str(v1, [norm[0], norm[1], norm[2]], 0.0, 0.0)
vertex_str += create_vertex_with_normal_str(v2, [norm[0], norm[1], norm[2]], 0.0, 1.0)
vertex_str += create_vertex_with_normal_str(top_vertex, [norm[0], norm[1], norm[2]], 1.0, 0.0)
face_str += create_face(vertex_index + 0, vertex_index + 1, vertex_index + 2)
vertex_index += 3
face_qty += 1
index += 1
return vertex_index, face_qty, vertex_str, face_str
def generate_roof_gabled(vertex2d, height, roof_height, vertex_index):
if len(vertex2d) != 4:
# FIXME
global warn_gabled_roof
if warn_gabled_roof:
print("Generating gabled roof for something else than 4 vertices is not implemented")
warn_gabled_roof = False
return generate_ceiling(vertex2d, height, vertex_index, is_barrier=False)
face_qty = 0
vertex_str = ""
face_str = ""
index = 0
# Always use the smallest edge
dist1 = ((vertex2d[1][0] - vertex2d[0][0]) * (vertex2d[1][0] - vertex2d[0][0])) + (
(vertex2d[1][1] - vertex2d[0][1]) * (vertex2d[1][1] - vertex2d[0][1]))
dist2 = ((vertex2d[2][0] - vertex2d[1][0]) * (vertex2d[2][0] - vertex2d[1][0])) + (
(vertex2d[2][1] - vertex2d[1][1]) * (vertex2d[2][1] - vertex2d[1][1]))
new_vertex2d = []
if dist1 > dist2:
for i in range(1, len(vertex2d)):
new_vertex2d.append(vertex2d[i - 1])
new_vertex2d.insert(0, vertex2d[-1])
vertex2d = list(new_vertex2d)
# Calculate top vertices
top1 = [(vertex2d[0][0] + vertex2d[1][0]) / 2, (vertex2d[0][1] + vertex2d[1][1]) / 2, height + roof_height]
top2 = [(vertex2d[2][0] + vertex2d[3][0]) / 2, (vertex2d[2][1] + vertex2d[3][1]) / 2, height + roof_height]
v3d = []
for v in vertex2d:
v.append(height)
v3d.append(v)
# first face
v = [v3d[0], top1, v3d[3]]
norm = helper.calc_norm(v)
vertex_str += create_vertex_with_normal_str(v[0], [norm[0], norm[1], norm[2]], 0.0, 0.0)
vertex_str += create_vertex_with_normal_str(v[1], [norm[0], norm[1], norm[2]], 0.0, 1.0)
vertex_str += create_vertex_with_normal_str(v[2], [norm[0], norm[1], norm[2]], 1.0, 0.0)
face_str += create_face(vertex_index + 0, vertex_index + 1, vertex_index + 2)
vertex_index += 3
face_qty += 1
index += 1
# second face
v = [v3d[1], v3d[2], top1]
norm = helper.calc_norm(v)
vertex_str += create_vertex_with_normal_str(v[0], [norm[0], norm[1], norm[2]], 0.0, 0.0)
vertex_str += create_vertex_with_normal_str(v[1], [norm[0], norm[1], norm[2]], 0.0, 1.0)
vertex_str += create_vertex_with_normal_str(v[2], [norm[0], norm[1], norm[2]], 1.0, 0.0)
face_str += create_face(vertex_index + 0, vertex_index + 1, vertex_index + 2)
vertex_index += 3
face_qty += 1
index += 1
# third face
v = [v3d[2], top2, top1]
norm = helper.calc_norm(v)
vertex_str += create_vertex_with_normal_str(v[0], [norm[0], norm[1], norm[2]], 0.0, 0.0)
vertex_str += create_vertex_with_normal_str(v[1], [norm[0], norm[1], norm[2]], 0.0, 1.0)
vertex_str += create_vertex_with_normal_str(v[2], [norm[0], norm[1], norm[2]], 1.0, 0.0)
face_str += create_face(vertex_index + 0, vertex_index + 1, vertex_index + 2)
vertex_index += 3
face_qty += 1
index += 1
# fourth face
v = [top2, v3d[3], top1]
norm = helper.calc_norm(v)
vertex_str += create_vertex_with_normal_str(v[0], [norm[0], norm[1], norm[2]], 0.0, 0.0)
vertex_str += create_vertex_with_normal_str(v[1], [norm[0], norm[1], norm[2]], 0.0, 1.0)
vertex_str += create_vertex_with_normal_str(v[2], [norm[0], norm[1], norm[2]], 1.0, 0.0)
face_str += create_face(vertex_index + 0, vertex_index + 1, vertex_index + 2)
vertex_index += 3
face_qty += 1
index += 1
return vertex_index, face_qty, vertex_str, face_str
def generate_roof_hipped(vertex2d, height, roof_height, vertex_index):
if len(vertex2d) != 4:
global warn_hipped_roof
if warn_hipped_roof:
print("Generating hipped roof for something else than 4 vertices is not implemented")
warn_hipped_roof = False
return generate_ceiling(vertex2d, height, vertex_index, is_barrier=False)
face_qty = 0
vertex_str = ""
face_str = ""
index = 0
# Always use the smallest edge
dist1 = ((vertex2d[1][0] - vertex2d[0][0]) * (vertex2d[1][0] - vertex2d[0][0])) + (
(vertex2d[1][1] - vertex2d[0][1]) * (vertex2d[1][1] - vertex2d[0][1]))
dist2 = ((vertex2d[2][0] - vertex2d[1][0]) * (vertex2d[2][0] - vertex2d[1][0])) + (
(vertex2d[2][1] - vertex2d[1][1]) * (vertex2d[2][1] - vertex2d[1][1]))
new_vertex2d = []
if dist1 > dist2:
for i in range(1, len(vertex2d)):
new_vertex2d.append(vertex2d[i - 1])
new_vertex2d.insert(0, vertex2d[-1])
vertex2d = list(new_vertex2d)
top1 = [(vertex2d[0][0] + vertex2d[1][0]) / 2, (vertex2d[0][1] + vertex2d[1][1]) / 2, height + roof_height]
top2 = [(vertex2d[2][0] + vertex2d[3][0]) / 2, (vertex2d[2][1] + vertex2d[3][1]) / 2, height + roof_height]
top_dist_x = top1[0] - top2[0]
top_dist_y = top1[1] - top2[1]
new_top1_x = top2[0] + (float(config.data["roof_hipped_percentage"]) * top_dist_x)
new_top2_x = top1[0] - (float(config.data["roof_hipped_percentage"]) * top_dist_x)
new_top1_y = top2[1] + (float(config.data["roof_hipped_percentage"]) * top_dist_y)
new_top2_y = top1[1] - (float(config.data["roof_hipped_percentage"]) * top_dist_y)
top1[0] = new_top1_x
top1[1] = new_top1_y
top2[0] = new_top2_x
top2[1] = new_top2_y
v3d = []
for v in vertex2d:
v.append(height)
v3d.append(v)
# first face
v = [v3d[0], top1, v3d[3]]
norm = helper.calc_norm(v)
vertex_str += create_vertex_with_normal_str(v[0], [norm[0], norm[1], norm[2]], 0.0, 0.0)
vertex_str += create_vertex_with_normal_str(v[1], [norm[0], norm[1], norm[2]], 0.0, 1.0)
vertex_str += create_vertex_with_normal_str(v[2], [norm[0], norm[1], norm[2]], 1.0, 0.0)
face_str += create_face(vertex_index + 0, vertex_index + 1, vertex_index + 2)
vertex_index += 3
face_qty += 1
index += 1
# second face
v = [v3d[1], v3d[2], top1]
norm = helper.calc_norm(v)
vertex_str += create_vertex_with_normal_str(v[0], [norm[0], norm[1], norm[2]], 0.0, 0.0)
vertex_str += create_vertex_with_normal_str(v[1], [norm[0], norm[1], norm[2]], 0.0, 1.0)
vertex_str += create_vertex_with_normal_str(v[2], [norm[0], norm[1], norm[2]], 1.0, 0.0)
face_str += create_face(vertex_index + 0, vertex_index + 1, vertex_index + 2)
vertex_index += 3
face_qty += 1
index += 1
# third face
v = [v3d[2], top2, top1]
norm = helper.calc_norm(v)
vertex_str += create_vertex_with_normal_str(v[0], [norm[0], norm[1], norm[2]], 0.0, 0.0)
vertex_str += create_vertex_with_normal_str(v[1], [norm[0], norm[1], norm[2]], 0.0, 1.0)
vertex_str += create_vertex_with_normal_str(v[2], [norm[0], norm[1], norm[2]], 1.0, 0.0)
face_str += create_face(vertex_index + 0, vertex_index + 1, vertex_index + 2)
vertex_index += 3
face_qty += 1
index += 1
# fourth face
v = [top2, v3d[3], top1]
norm = helper.calc_norm(v)
vertex_str += create_vertex_with_normal_str(v[0], [norm[0], norm[1], norm[2]], 0.0, 0.0)
vertex_str += create_vertex_with_normal_str(v[1], [norm[0], norm[1], norm[2]], 0.0, 1.0)
vertex_str += create_vertex_with_normal_str(v[2], [norm[0], norm[1], norm[2]], 1.0, 0.0)
face_str += create_face(vertex_index + 0, vertex_index + 1, vertex_index + 2)
vertex_index += 3
face_qty += 1
index += 1
# hipped face 1
v = [v3d[0], v3d[1], top1]
norm = helper.calc_norm(v)
vertex_str += create_vertex_with_normal_str(v[0], [norm[0], norm[1], norm[2]], 0.0, 0.0)
vertex_str += create_vertex_with_normal_str(v[1], [norm[0], norm[1], norm[2]], 0.0, 1.0)
vertex_str += create_vertex_with_normal_str(v[2], [norm[0], norm[1], norm[2]], 1.0, 0.0)
face_str += create_face(vertex_index + 0, vertex_index + 1, vertex_index + 2)
vertex_index += 3
face_qty += 1
index += 1
# hipped face 2
v = [v3d[2], v3d[3], top2]
norm = helper.calc_norm(v)
vertex_str += create_vertex_with_normal_str(v[0], [norm[0], norm[1], norm[2]], 0.0, 0.0)
vertex_str += create_vertex_with_normal_str(v[1], [norm[0], norm[1], norm[2]], 0.0, 1.0)
vertex_str += create_vertex_with_normal_str(v[2], [norm[0], norm[1], norm[2]], 1.0, 0.0)
face_str += create_face(vertex_index + 0, vertex_index + 1, vertex_index + 2)
vertex_index += 3
face_qty += 1
index += 1
return vertex_index, face_qty, vertex_str, face_str
def get_info_from_input_data(nodes, scale=1.0, is_node=True, keep_all_nodes=False):
all_vertex = []
min_x = 9999999.0
min_y = 9999999.0
max_x = -9999999.0
max_y = -9999999.0
# First and last nodes are sometimes the same. In this case skip the last node unless specified otherwise
if keep_all_nodes is False:
if nodes[0] == nodes[-1]:
nodes.pop()
for node in nodes:
if is_node is True:
x = helper.lon_to_x(node.lon)
y = helper.lat_to_y(node.lat)
else:
x = node[0]
y = node[1]
if x < min_x:
min_x = x
if x > max_x:
max_x = x
if y < min_y:
min_y = y
if y > max_y:
max_y = y
all_vertex.append([x, y, 0.0])
# Coordinate of object on map
center_x = (max_x + min_x) / 2
center_y = (max_y + min_y) / 2
centered_vertex = []
# Make sure vertices are centered on 0,0
for v in all_vertex:
# 'Y' axis is inverted on RoR map
centered_vertex.append([(v[0] - center_x) * scale, (-(v[1] - center_y)) * scale])
# Make sure faces are correctly oriented
if triangulate.IsClockwise(centered_vertex):
centered_vertex.reverse()
return center_x, center_y, max_x - min_x, max_y - min_y, centered_vertex
def create_additional_vertex_for_barrier(vertex, half_barrier, barrier_width):
# Create vertices "around" each segment
loop = False
if vertex[0] == vertex[-1]:
# With a looped barrier, we calculate all vertex with intersections.
# As we need 3 points to calculate an intersection, we had the missing point here.
# If not looped, we just draw normals of the first and last vertex
loop = True
last_vertex = vertex[-2]
second_vertex = vertex[1]
vertex.insert(0, last_vertex) # For intersection on first vertex
vertex.append(second_vertex) # for intersection on last vertex
first_side_vertex = []
opposite_side_vertex = []
if loop is False:
first_vertex_normal = helper.calc_normal(vertex[0], vertex[1], barrier_width / 2.0)
first_vertex_normal_coord_1 = (vertex[0][0] + first_vertex_normal[0], vertex[0][1] + first_vertex_normal[1])
first_side_vertex.append(list(first_vertex_normal_coord_1))
first_vertex_normal_coord_2 = (vertex[0][0] - first_vertex_normal[0], vertex[0][1] - first_vertex_normal[1])
opposite_side_vertex.append(list(first_vertex_normal_coord_2))
def calc_normal_coord(origin_vertex, normal, factor):
return origin_vertex[0] + (factor * normal[0]), origin_vertex[1] + (factor * normal[1])
def calc_intersection(v1, v2, v3, factor, width):
start_vertex_normal = helper.calc_normal(v1, v2, width / 2.0)
start_parallel_p1 = calc_normal_coord(v1, start_vertex_normal, factor)
start_parallel_p2 = calc_normal_coord(v2, start_vertex_normal, factor)
end_vertex_normal = helper.calc_normal(v2, v3, width / 2.0)
end_parallel_p1 = calc_normal_coord(v3, end_vertex_normal, factor)
end_parallel_p2 = calc_normal_coord(v2, end_vertex_normal, factor)
return helper.intersect_line((start_parallel_p1, start_parallel_p2), (end_parallel_p1, end_parallel_p2))
while len(vertex) >= 3:
# Calculate intersections around current segment and next segment
start_vertex = vertex.pop(0)
next_vertex = vertex[0]
end_vertex = vertex[1]
# First side
f = 1.0 # Draw all around the barrier equally
if half_barrier:
f = 0.0 # Draw only one side of the barrier
intersection_1 = calc_intersection(start_vertex, next_vertex, end_vertex, f, barrier_width)
# second side
f = -1.0 # Draw all around the barrier equally
if half_barrier:
f = -2.0 # Draw only one side of the barrier
intersection_2 = calc_intersection(start_vertex, next_vertex, end_vertex, f, barrier_width)
if intersection_1 is None or intersection_2 is None:
continue
first_side_vertex.append(list(intersection_1))
opposite_side_vertex.append(list(intersection_2))
if loop is False:
last_vertex_normal = helper.calc_normal(vertex[-2], vertex[-1], barrier_width / 2.0)
last_vertex_normal_coord_1 = (vertex[-1][0] + last_vertex_normal[0], vertex[-1][1] + last_vertex_normal[1])
first_side_vertex.append(list(last_vertex_normal_coord_1))
last_vertex_normal_coord_2 = (vertex[-1][0] - last_vertex_normal[0], vertex[-1][1] - last_vertex_normal[1])
opposite_side_vertex.append(list(last_vertex_normal_coord_2))
first_side_vertex.reverse()
centered_vertex = opposite_side_vertex + first_side_vertex
return centered_vertex
def create_vertex_for_pillar(node):
x = helper.lon_to_x(node.lon)
y = helper.lat_to_y(node.lat)
length = config.data["barrier_width"] / 2.0
vertex = [[length, length], [length, -length],
[-length, -length], [-length, length]]
return x, y, config.data["barrier_width"], config.data["barrier_width"], vertex
def create_vertex_str(v0, v1, v2, u, v):
vertex_str = "<vertex>\n"
vertex_str = vertex_str + "<position x=\"" + str(v0[0]) + "\" y=\"" + str(v0[1]) + "\" z=\"" + str(
v0[2]) + "\" />\n"
normal = helper.calc_norm([v0, v1, v2])
vertex_str = vertex_str + "<normal x=\"" + str(normal[0]) + "\" y=\"" + str(normal[1]) + "\" z=\"" + str(
normal[2]) + "\" />\n"
vertex_str = vertex_str + "<texcoord u=\"" + str(u) + "\" v=\"" + str(v) + "\" />"
return vertex_str + "</vertex>\n"
def create_vertex_with_normal_str(v0, normal, u, v):
vertex_str = "<vertex>\n"
vertex_str = vertex_str + "<position x=\"" + str(v0[0]) + "\" y=\"" + str(v0[1]) + "\" z=\"" + str(
v0[2]) + "\" />\n"
vertex_str = vertex_str + "<normal x=\"" + str(normal[0]) + "\" y=\"" + str(normal[1]) + "\" z=\"" + str(
normal[2]) + "\" />\n"
vertex_str = vertex_str + "<texcoord u=\"" + str(u) + "\" v=\"" + str(v) + "\" />"
return vertex_str + "</vertex>\n"
def create_face(i0, i1, i2):
return "<face v1=\"" + str(i0) + "\" v2=\"" + str(
i1) + "\" v3=\"" + str(
i2) + "\" />\n"
def generate_mesh_file(submesh, obj_name):
mesh_str = "<mesh>\n"
mesh_str += "<submeshes>\n"
for s in submesh:
mesh_str += "<submesh material=\"" + s[
"texture"] + "\" usesharedvertices=\"false\" use32bitindexes=\"false\" operationtype=\"triangle_list\">\n"
mesh_str += "<faces count=\"" + str(s["face_qty"]) + "\">\n"
mesh_str += s["face_str"]
mesh_str += "</faces>\n"
mesh_str += "<geometry vertexcount=\"" + str(s["vertex_index"]) + "\">\n"
mesh_str += "<vertexbuffer positions=\"true\" normals=\"true\" texture_coord_dimensions_0=\"float2\" texture_coords=\"1\">"
mesh_str += s["vertex_str"]
mesh_str += "</vertexbuffer>\n"
mesh_str += "</geometry>\n"
mesh_str += "</submesh>\n"
mesh_str += "</submeshes>\n"
mesh_str += "</mesh>\n"
mesh_file_name = obj_name + ".mesh"
with open(config.data["work_path"] + mesh_file_name + ".xml", "w") as mesh_file:
mesh_file.write(mesh_str)
os.system(
"OgreXMLConverter " + config.data["work_path"] + obj_name + ".mesh.xml > /dev/null")
ror_zip_file.add_to_zip_file_list(obj_name + ".mesh")