-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstreamlineClass.py
More file actions
813 lines (693 loc) · 34.2 KB
/
Copy pathstreamlineClass.py
File metadata and controls
813 lines (693 loc) · 34.2 KB
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
class StreamPoint:
x = 0
y = 0
def __init__(self, arg_x, arg_y):
self = self
self.x = float(arg_x)
self.y = float(arg_y)
class AdjacentStreamline:
id = -1
distance = -1
def __init__(self, ids, distance):
self = self
self.id = ids
self.distance = distance
class Streamline:
id = -1
length = 0
streamPoint = []
# New AdjacentStreamline Structure
left_id = -1
right_id = -1
# Old Adjacent Structure
left = [] # store id and distance
right = []
left_point = [] # only store id
right_point = []
def __init__(self, arg_length, streampointlist):
self = self
self.length = arg_length
self.streamPoint = streampointlist
def set_id(self, ids):
self = self
self.id = ids
def set_left_adjacent(self, left_arg):
self = self
self.left = left_arg
def set_right_adjacent(self, right_arg):
self = self
self.right = right_arg
def set_left_point(self, left_args):
self = self
self.left_point = left_args
def set_right_point(self, right_args):
self = self
self.right_point = right_args
class StreamlineFrame:
frameX = 0
frameY = 0
streamline_size = 0
streamline = [] # List of Streamlines
def __init__(self, streamline_list):
self = self
self.streamline = streamline_list
class CorrespondingLine:
distance = -1 # Distance Of IndexA and IndexB
reverse = -1 # Reverse Streamline A =1, Reverse StreamlineB =2
grow = 0 # Streamline Growing = 1 , Streamline Shrink = -1 , Default Off = 0
grow_speed = 0
indexA = -1 # Corresponding StreamlineA
indexB = -1 # Corresponding StreamlineB
split_num = -1 # Streamline split simulation flow
def __init__(self, indexA, indexB, distances, reverse=-1, grow=0, grow_speed=0):
self = self
self.indexA = indexA
self.indexB = indexB
self.distance = distances
self.reverse = reverse
self.grow = grow
self.grow_speed = grow_speed
class CorrespondingPoint:
pointA = -1
pointB = -1
def __init__(self, pointA, pointB):
self = self
self.pointA = pointA
self.pointB = pointB
class CorrespondingPair:
corresponding_list = []
def __init__(self, corresponding_list):
self = self
self.corresponding_list = corresponding_list
def shown(self):
for i in range(len(self.corresponding_list)):
print("A:" + str(self.corresponding_list[i].indexA) + " B:" + str(
self.corresponding_list[i].indexB) + " Distance:" + str(
self.corresponding_list[i].distance) + " Grow:" + str(
self.corresponding_list[i].grow) + " Grow_Speed:" + str(
self.corresponding_list[i].grow_speed) + " Reverse:" + str(
self.corresponding_list[i].reverse), flush=True)
def add(self, indexA, indexB, distance=5, reverse=-1):
corresponding_line = CorrespondingLine(indexA, indexB, distance, reverse)
self.corresponding_list.append(corresponding_line)
def remove(self, indexA=-1, indexB=-1, distance=-1):
if indexA >= 0:
for i in range(len(self.corresponding_list)):
if self.corresponding_list[i].indexA == indexA:
self.corresponding_list.pop(i)
return
if indexB >= 0:
for i in range(len(self.corresponding_list)):
if self.corresponding_list[i].indexB == indexB:
self.corresponding_list.pop(i)
return
def reverse(self, indexA=-1, indexB=-1, reverse=1):
if indexA >= 0:
for i in range(len(self.corresponding_list)):
if self.corresponding_list[i].indexA == indexA:
self.corresponding_list[i].reverse = reverse
return
if indexB >= 0:
for i in range(len(self.corresponding_list)):
if self.corresponding_list[i].indexB == indexB:
self.corresponding_list[i].reverse = reverse
return
# Streamline Appear
def grow(self, indexA=-1, indexB=-1, reverse=-1, speed=0):
if indexA >= 0:
for i in range(len(self.corresponding_list)):
if self.corresponding_list[i].indexA == indexA:
self.corresponding_list[i].grow = 1
self.corresponding_list[i].indexB = -1
self.corresponding_list[i].reverse = reverse
self.corresponding_list[i].grow_speed = speed
return
self.add(indexA, indexB, distance=15, reverse=reverse)
self.grow(indexA, indexB, reverse, speed)
if indexB >= 0:
for i in range(len(self.corresponding_list)):
if self.corresponding_list[i].indexB == indexB:
self.corresponding_list[i].grow = 1
self.corresponding_list[i].indexA = -1
self.corresponding_list[i].reverse = reverse
self.corresponding_list[i].grow_speed = speed
return
self.add(indexA, indexB, distance=15, reverse=reverse)
self.grow(indexA, indexB, reverse, speed)
# Streamline Disappear
def shrink(self, indexA=-1, indexB=-1, reverse=-1, speed=0):
if indexA >= 0:
for i in range(len(self.corresponding_list)):
if self.corresponding_list[i].indexA == indexA:
self.corresponding_list[i].grow = -1
self.corresponding_list[i].indexB = -1
self.corresponding_list[i].reverse = reverse
self.corresponding_list[i].grow_speed = speed
return
self.add(indexA, indexB, distance=15, reverse=reverse)
self.shrink(indexA, indexB, reverse, speed)
if indexB >= 0:
for i in range(len(self.corresponding_list)):
if self.corresponding_list[i].indexB == indexB:
self.corresponding_list[i].grow = -1
self.corresponding_list[i].indexA = -1
self.corresponding_list[i].reverse = reverse
self.corresponding_list[i].grow_speed = speed
return
self.add(indexA, indexB, distance=15, reverse=reverse)
self.shrink(indexA, indexB, reverse, speed)
def repairA(self, indexA, indexB, distance=-1, reverse=-1):
if indexA >= 0:
for i in range(len(self.corresponding_list)):
if self.corresponding_list[i].indexA == indexA:
self.corresponding_list[i].indexB = indexB
if distance:
self.corresponding_list[i].distance = distance
if reverse:
self.corresponding_list[i].reverse = reverse
return
def repairB(self, indexA, indexB, distance=-1, reverse=-1):
if indexB >= 0:
for i in range(len(self.corresponding_list)):
if self.corresponding_list[i].indexB == indexB:
self.corresponding_list[i].indexA = indexA
if distance:
self.corresponding_list[i].distance = distance
if reverse:
self.corresponding_list[i].reverse = reverse
return
def readStreamline(fileLocation):
t = open(fileLocation, "r")
# Clean Pointer Memory -- To Avoid Data Point To Same Memory
streamlistrand = []
streamlistrand.clear()
# Clean Pointer Memory End
streamlineFrame = StreamlineFrame(streamlistrand)
streamlineFrame.frameX = float(t.readline())
streamlineFrame.frameY = float(t.readline())
streamlineFrame.streamline_size = int(t.readline())
t.readline() # Remove First Line
streamlist = []
streamlist.clear()
i = 0
j = 0
previous_j = 0
for line in t:
if line == "\n":
len = j - previous_j
streamlinenew = Streamline(len, streamlist[previous_j:j])
streamlineFrame.streamline.append(streamlinenew)
previous_j = j
i = i + 1
else:
sx, sy = line.split(" ")
streampoint = StreamPoint(sx, sy)
streamlist.append(streampoint)
j = j + 1
# Last Line
len = j - previous_j
streamlinenew = Streamline(len, streamlist[previous_j:j])
streamlineFrame.streamline.append(streamlinenew)
t.close()
# Debug
# # print(streamlineFrame.frameX)
# # print(streamlineFrame.streamline[0].streamPoint[0].x)
return streamlineFrame
def showStreamline(streamlineFrame):
print(str(streamlineFrame.frameX))
print(str(streamlineFrame.frameY))
print(str(streamlineFrame.streamline_size))
for i in range(len(streamlineFrame.streamline)):
print(str(i) + ":")
for j in range(len(streamlineFrame.streamline[i].streamPoint)):
print(str(streamlineFrame.streamline[i].streamPoint[j].x), end=' ')
print(str(streamlineFrame.streamline[i].streamPoint[j].y))
return
def get_streamline_index(streamFrame, id):
for i in range(len(streamFrame.streamline)):
if id == streamFrame.streamline[i].id:
return i
return -1
# Streamline Mode
def writeStreamline(streamlineFrame, fileName):
f = open(fileName, "w")
f.write(str(streamlineFrame.frameX) + "\n")
f.write(str(streamlineFrame.frameY) + "\n")
f.write(str(streamlineFrame.streamline_size) + "\n")
for i in range(len(streamlineFrame.streamline)):
f.write("\n")
for j in range(len(streamlineFrame.streamline[i].streamPoint)):
f.write(str(streamlineFrame.streamline[i].streamPoint[j].x))
f.write(" ")
f.write(str(streamlineFrame.streamline[i].streamPoint[j].y))
f.write("\n")
return
# Write Clip Seeds
def write_clip_seeds(streamlineFrame, fileName):
f = open(fileName, "w")
for i in range(len(streamlineFrame.streamline)):
length = int((len(streamlineFrame.streamline[i].streamPoint) / 2))
seed_x = (streamlineFrame.streamline[i].streamPoint[length].x / streamlineFrame.frameX) * 100
seed_y = (streamlineFrame.streamline[i].streamPoint[length].y / streamlineFrame.frameY) * 100
# seed_x = (streamlineFrame.streamline[i].streamPoint[length].x / 1280) * 100
# seed_y = (streamlineFrame.streamline[i].streamPoint[length].y / 720) * 100
f.write(str(seed_x))
f.write(" ")
f.write(str(seed_y))
f.write("\n")
return
# Write Default V-paint Vec File (Without Split Mode)
def writeStreamline_svg(fileName, corresponding_list, frameA, frameB, interpolate_frame,
canvasValue="850 857", bgImg="", play_mode="loop"):
# Variable Settings
canvasSize = canvasValue
imagePath = bgImg
playMode = play_mode
f = open(fileName, "w")
f_edge_id = open(fileName[:-3] + "_ID.txt", "w")
f_edge_id.write("###Streamline Vec Edge Id Reference###\n")
f_edge_id.write("###'Global-ID'->'Streamline-ID'###\n\n")
# Write Out Pre-Define Variable
f.write('<?xml version="1.0" encoding="UTF-8"?>\n')
f.write('<!-- Created with Streamline Corresponding Program @Yayapipi -->\n')
f.write('\n<vec version="1.7">\n')
f.write('<playback framerange="0 ')
f.write(str(interpolate_frame))
f.write('" fps="24" subframeinbetweening="off" playmode="' + playMode + '"/>\n')
f.write('<canvas position="0 0" size="' + canvasSize + '"/>\n')
f.write('<layer name="Layer 1" visible="true">\n\t')
f.write('<background color="rgba(255,255,255,1)" image="' + imagePath + '" position="0 0" '
'size="cover" repeat="norepeat" opacity="1" hold="yes"/>\n')
# Define Id Variable
global_id = 0
before_start_vertex_id = 0
after_start_vertex_id = 0
before_end_vertex_id = 0
after_end_vertex_id = 0
before_edge_id = 0
after_edge_id = 0
before_inBetween_start_vertex_id = 0
after_inBetween_end_vertex_id = 0
color_value = "rgba(0,0,0,1)"
stroke_width = 6
# Start Writing Corresponding Streamline Data
f.write('<objects>\n\t')
for i in range(len(corresponding_list)):
# ---Streamline A---
duplicate_triggerA = 0
if corresponding_list[i].grow == -1 or corresponding_list[i].indexA == -1:
frame_index = interpolate_frame - corresponding_list[i].grow_speed
duplicate_triggerA = 1
else:
frame_index = 0
if corresponding_list[i].reverse == 1:
frameA.streamline[corresponding_list[i].indexA].streamPoint.reverse()
if corresponding_list[i].reverse == 2:
frameB.streamline[corresponding_list[i].indexB].streamPoint.reverse()
# 1.Start Vertex
f.write('<vertex\n\t\t')
f.write('id="' + str(global_id) + '"\n\t\t')
f.write('frame="' + str(frame_index) + '"\n\t\t')
if duplicate_triggerA == 1:
position_x = frameB.streamline[corresponding_list[i].indexB].streamPoint[0].x
position_y = frameB.streamline[corresponding_list[i].indexB].streamPoint[0].y
elif duplicate_triggerA == 0:
position_x = frameA.streamline[corresponding_list[i].indexA].streamPoint[0].x
position_y = frameA.streamline[corresponding_list[i].indexA].streamPoint[0].y
f.write('position="' + str(position_x) + ' ' + str(position_y) + '"\n\t\t')
f.write('color="' + color_value + '"/>\n\t')
before_start_vertex_id = global_id
global_id += 1
# 2.End Vertex
f.write('<vertex\n\t\t')
f.write('id="' + str(global_id) + '"\n\t\t')
f.write('frame="' + str(frame_index) + '"\n\t\t')
if duplicate_triggerA == 1:
last_point_num = len(frameB.streamline[corresponding_list[i].indexB].streamPoint)
position_x = frameB.streamline[corresponding_list[i].indexB].streamPoint[last_point_num - 1].x
position_y = frameB.streamline[corresponding_list[i].indexB].streamPoint[last_point_num - 1].y
elif duplicate_triggerA == 0:
last_point_num = len(frameA.streamline[corresponding_list[i].indexA].streamPoint)
position_x = frameA.streamline[corresponding_list[i].indexA].streamPoint[last_point_num - 1].x
position_y = frameA.streamline[corresponding_list[i].indexA].streamPoint[last_point_num - 1].y
f.write('position="' + str(position_x) + ' ' + str(position_y) + '"\n\t\t')
f.write('color="' + color_value + '"/>\n\t')
before_end_vertex_id = global_id
global_id += 1
# 3.Streamline Edge Curve
f.write('<edge\n\t\t')
f.write('id="' + str(global_id) + '"\n\t\t')
f.write('frame="' + str(frame_index) + '"\n\t\t')
f.write('startvertex="' + str(before_start_vertex_id) + '"\n\t\t')
f.write('endvertex="' + str(before_end_vertex_id) + '"\n\t\t')
f.write('curve="xywdense(5')
if duplicate_triggerA == 1:
for j in range(len(frameB.streamline[corresponding_list[i].indexB].streamPoint)):
position_x = frameB.streamline[corresponding_list[i].indexB].streamPoint[j].x
position_y = frameB.streamline[corresponding_list[i].indexB].streamPoint[j].y
f.write(' ' + str(position_x) + ',' + str(position_y) + ',' + str(stroke_width))
elif duplicate_triggerA == 0:
for j in range(len(frameA.streamline[corresponding_list[i].indexA].streamPoint)):
position_x = frameA.streamline[corresponding_list[i].indexA].streamPoint[j].x
position_y = frameA.streamline[corresponding_list[i].indexA].streamPoint[j].y
f.write(' ' + str(position_x) + ',' + str(position_y) + ',' + str(stroke_width))
f.write(')"\n\t\t')
f.write('color="' + color_value + '"/>\n\t')
f_edge_id.write(str(global_id) + "->" + str(corresponding_list[i].indexA) + "\n")
before_edge_id = global_id
global_id += 1
# ---Streamline B---
duplicate_trigger = 0
if corresponding_list[i].grow == 1 or corresponding_list[i].indexB == -1:
frameB_index = 0 + corresponding_list[i].grow_speed
duplicate_trigger = 1
else:
frameB_index = interpolate_frame
# 4.Start Vertex
f.write('<vertex\n\t\t')
f.write('id="' + str(global_id) + '"\n\t\t')
f.write('frame="' + str(frameB_index) + '"\n\t\t')
if duplicate_trigger == 1:
position_x = frameA.streamline[corresponding_list[i].indexA].streamPoint[0].x
position_y = frameA.streamline[corresponding_list[i].indexA].streamPoint[0].y
elif duplicate_trigger == 0:
position_x = frameB.streamline[corresponding_list[i].indexB].streamPoint[0].x
position_y = frameB.streamline[corresponding_list[i].indexB].streamPoint[0].y
f.write('position="' + str(position_x) + ' ' + str(position_y) + '"\n\t\t')
f.write('color="' + color_value + '"/>\n\t')
after_start_vertex_id = global_id
global_id += 1
# 5.End Vertex
f.write('<vertex\n\t\t')
f.write('id="' + str(global_id) + '"\n\t\t')
f.write('frame="' + str(frameB_index) + '"\n\t\t')
if duplicate_trigger == 1:
last_point_num = len(frameA.streamline[corresponding_list[i].indexA].streamPoint)
position_x = frameA.streamline[corresponding_list[i].indexA].streamPoint[last_point_num - 1].x
position_y = frameA.streamline[corresponding_list[i].indexA].streamPoint[last_point_num - 1].y
elif duplicate_trigger == 0:
last_point_num = len(frameB.streamline[corresponding_list[i].indexB].streamPoint)
position_x = frameB.streamline[corresponding_list[i].indexB].streamPoint[last_point_num - 1].x
position_y = frameB.streamline[corresponding_list[i].indexB].streamPoint[last_point_num - 1].y
f.write('position="' + str(position_x) + ' ' + str(position_y) + '"\n\t\t')
f.write('color="' + color_value + '"/>\n\t')
after_end_vertex_id = global_id
global_id += 1
# 6.Streamline Edge Curve
f.write('<edge\n\t\t')
f.write('id="' + str(global_id) + '"\n\t\t')
f.write('frame="' + str(frameB_index) + '"\n\t\t')
f.write('startvertex="' + str(after_start_vertex_id) + '"\n\t\t')
f.write('endvertex="' + str(after_end_vertex_id) + '"\n\t\t')
f.write('curve="xywdense(5')
if duplicate_trigger == 1:
for j in range(len(frameA.streamline[corresponding_list[i].indexA].streamPoint)):
position_x = frameA.streamline[corresponding_list[i].indexA].streamPoint[j].x
position_y = frameA.streamline[corresponding_list[i].indexA].streamPoint[j].y
f.write(' ' + str(position_x) + ',' + str(position_y) + ',' + str(stroke_width))
elif duplicate_trigger == 0:
for j in range(len(frameB.streamline[corresponding_list[i].indexB].streamPoint)):
position_x = frameB.streamline[corresponding_list[i].indexB].streamPoint[j].x
position_y = frameB.streamline[corresponding_list[i].indexB].streamPoint[j].y
f.write(' ' + str(position_x) + ',' + str(position_y) + ',' + str(stroke_width))
f.write(')"\n\t\t')
f.write('color="' + color_value + '"/>\n\t')
f_edge_id.write(str(global_id) + "->" + str(corresponding_list[i].indexB) + "\n")
after_edge_id = global_id
global_id += 1
# ---InBetween---
# 7.InBetween Start Vertex
f.write('<inbetweenvertex\n\t\t')
f.write('id="' + str(global_id) + '"\n\t\t')
f.write('beforevertex="' + str(before_start_vertex_id) + '"\n\t\t')
f.write('aftervertex="' + str(after_start_vertex_id) + '"\n\t\t')
f.write('color="' + color_value + '"/>\n\t')
before_inBetween_start_vertex_id = global_id
global_id += 1
# 8.InBetween End Vertex
f.write('<inbetweenvertex\n\t\t')
f.write('id="' + str(global_id) + '"\n\t\t')
f.write('beforevertex="' + str(before_end_vertex_id) + '"\n\t\t')
f.write('aftervertex="' + str(after_end_vertex_id) + '"\n\t\t')
f.write('color="' + color_value + '"/>\n\t')
after_inBetween_start_vertex_id = global_id
global_id += 1
# 9.InBetween Streamline Curve Edge
f.write('<inbetweenedge\n\t\t')
f.write('id="' + str(global_id) + '"\n\t\t')
f.write('beforepath="[' + str(before_edge_id) + '+]"\n\t\t')
f.write('afterpath="' + str(after_edge_id) + '+]"\n\t\t')
f.write('startanimatedvertex="[' + str(before_inBetween_start_vertex_id) + ']"\n\t\t')
f.write('endanimatedvertex="' + str(after_inBetween_start_vertex_id) + ']"\n\t\t')
f.write('color="' + color_value + '"/>\n')
global_id += 1
f.write('</objects>\n')
f.write('</layer>\n')
f.write('</vec>')
f.close()
f_edge_id.close()
return
# Write Vec File Including Streamline Split Flow Simulation
def writeStl_Simulation_svg(fileName, corresponding_list, frameA, frameB, interpolate_frame,
canvasValue="850 857", bgImg="", play_mode="loop"):
# Variable Settings
canvasSize = canvasValue
imagePath = bgImg
playMode = play_mode
f = open(fileName, "w")
f_edge_id = open(fileName[:-3] + "_ID.txt", "w")
f_edge_id.write("###Streamline Vec Edge Id Reference###\n")
f_edge_id.write("###'Global-ID'->'Streamline-ID'###\n\n")
# Write Out Pre-Define Variable
f.write('<?xml version="1.0" encoding="UTF-8"?>\n')
f.write('<!-- Created with Streamline Corresponding Program @Yayapipi feat. Hsin-Wei -->\n')
f.write('\n<vec version="1.7">\n')
f.write('<playback framerange="0 ')
f.write(str(interpolate_frame))
f.write('" fps="24" subframeinbetweening="off" playmode="' + playMode + '"/>\n')
f.write('<canvas position="0 0" size="' + canvasSize + '"/>\n')
f.write('<layer name="Layer 1" visible="true">\n\t')
f.write('<background color="rgba(255,255,255,1)" image="' + imagePath + '" position="0 0" '
'size="cover" repeat="norepeat" opacity="1" hold="yes"/>\n')
# Define Id Variable
global_id = 0
streamlineA_split_index = []
streamlineB_split_index = []
streamlineA_vertex_id = []
streamlineB_vertex_id = []
streamlineA_edge_id = []
streamlineB_edge_id = []
inbetween_vertex_id = []
loop_num = 1
color_value = "rgba(0,0,0,1)"
stroke_width = 6
# Start Writing Corresponding Streamline Data
f.write('<objects>\n\t')
for i in range(len(corresponding_list)):
# Initialize Variable
streamlineA_split_index.clear()
streamlineB_split_index.clear()
streamlineA_vertex_id.clear()
streamlineB_vertex_id.clear()
streamlineA_edge_id.clear()
streamlineB_edge_id.clear()
inbetween_vertex_id.clear()
loop_num = 1
# -- Split Streamline Vertex --
head_split = 0 # Split Threshold Edge For The Head [0- Directly From Head , 1- Small Edge]
tail_split = 1 # Split Threshold Edge For The Tail [1- Directly To Tail , 2- Small Edge]
if corresponding_list[i].split_num > 0:
streamlineA_split_index.append(0)
streamlineA_split_index.append(head_split)
index = len(frameA.streamline[corresponding_list[i].indexA].streamPoint) / corresponding_list[i].split_num
index = int(index)
for j in range(corresponding_list[i].split_num - 1):
value = streamlineA_split_index[-1] + index
if value > len(frameA.streamline[corresponding_list[i].indexA].streamPoint):
break
else:
streamlineA_split_index.append(value)
streamlineA_split_index.append(len(frameA.streamline[corresponding_list[i].indexA].streamPoint) - tail_split)
streamlineA_split_index.append(len(frameA.streamline[corresponding_list[i].indexA].streamPoint) - 1)
streamlineB_split_index = streamlineA_split_index[:]
loop_num = len(streamlineA_split_index)
else:
streamlineA_split_index.clear()
streamlineA_split_index.append(0)
streamlineA_split_index.append(len(frameA.streamline[corresponding_list[i].indexA].streamPoint) - 1)
streamlineB_split_index.clear()
streamlineB_split_index.append(0)
streamlineB_split_index.append(len(frameB.streamline[corresponding_list[i].indexB].streamPoint) - 1)
loop_num = 2
# ---Streamline A---
duplicate_triggerA = 0
if corresponding_list[i].grow == -1 or corresponding_list[i].indexA == -1:
frame_index = interpolate_frame - corresponding_list[i].grow_speed
duplicate_triggerA = 1
else:
frame_index = 0
if corresponding_list[i].reverse == 1:
frameA.streamline[corresponding_list[i].indexA].streamPoint.reverse()
if corresponding_list[i].reverse == 2:
frameB.streamline[corresponding_list[i].indexB].streamPoint.reverse()
for j in range(loop_num):
# 1. Vertex
f.write('<vertex\n\t\t')
f.write('id="' + str(global_id) + '"\n\t\t')
f.write('frame="' + str(frame_index) + '"\n\t\t')
if duplicate_triggerA == 1:
position_x = frameB.streamline[corresponding_list[i].indexB].streamPoint[streamlineB_split_index[j]].x
position_y = frameB.streamline[corresponding_list[i].indexB].streamPoint[streamlineB_split_index[j]].y
elif duplicate_triggerA == 0:
position_x = frameA.streamline[corresponding_list[i].indexA].streamPoint[streamlineA_split_index[j]].x
position_y = frameA.streamline[corresponding_list[i].indexA].streamPoint[streamlineA_split_index[j]].y
f.write('position="' + str(position_x) + ' ' + str(position_y) + '"\n\t\t')
f.write('color="' + color_value + '"/>\n\t')
streamlineA_vertex_id.append(global_id)
global_id += 1
for k in range(loop_num - 1):
# 2. Streamline Edge Curve
f.write('<edge\n\t\t')
f.write('id="' + str(global_id) + '"\n\t\t')
f.write('frame="' + str(frame_index) + '"\n\t\t')
f.write('startvertex="' + str(streamlineA_vertex_id[k]) + '"\n\t\t')
f.write('endvertex="' + str(streamlineA_vertex_id[k + 1]) + '"\n\t\t')
f.write('curve="xywdense(5')
if duplicate_triggerA == 1:
for j in range(streamlineB_split_index[k], streamlineB_split_index[k + 1]):
position_x = frameB.streamline[corresponding_list[i].indexB].streamPoint[j].x
position_y = frameB.streamline[corresponding_list[i].indexB].streamPoint[j].y
f.write(' ' + str(position_x) + ',' + str(position_y) + ',' + str(stroke_width))
elif duplicate_triggerA == 0:
for j in range(streamlineA_split_index[k], streamlineA_split_index[k + 1]):
position_x = frameA.streamline[corresponding_list[i].indexA].streamPoint[j].x
position_y = frameA.streamline[corresponding_list[i].indexA].streamPoint[j].y
f.write(' ' + str(position_x) + ',' + str(position_y) + ',' + str(stroke_width))
f.write(')"\n\t\t')
f.write('color="' + color_value + '"/>\n\t')
f_edge_id.write(str(global_id) + "->" + str(corresponding_list[i].indexA) + "\n")
streamlineA_edge_id.append(global_id)
global_id += 1
# ---Streamline B---
duplicate_trigger = 0
if corresponding_list[i].grow == 1 or corresponding_list[i].indexB == -1:
frameB_index = 0 + corresponding_list[i].grow_speed
duplicate_trigger = 1
else:
frameB_index = interpolate_frame
for j in range(loop_num):
# 1. Vertex
f.write('<vertex\n\t\t')
f.write('id="' + str(global_id) + '"\n\t\t')
f.write('frame="' + str(frameB_index) + '"\n\t\t')
if duplicate_trigger == 1:
position_x = frameA.streamline[corresponding_list[i].indexA].streamPoint[streamlineA_split_index[j]].x
position_y = frameA.streamline[corresponding_list[i].indexA].streamPoint[streamlineA_split_index[j]].y
elif duplicate_trigger == 0:
position_x = frameB.streamline[corresponding_list[i].indexB].streamPoint[streamlineB_split_index[j]].x
position_y = frameB.streamline[corresponding_list[i].indexB].streamPoint[streamlineB_split_index[j]].y
f.write('position="' + str(position_x) + ' ' + str(position_y) + '"\n\t\t')
f.write('color="' + color_value + '"/>\n\t')
streamlineB_vertex_id.append(global_id)
global_id += 1
for k in range(loop_num - 1):
# 2. Streamline Edge Curve
f.write('<edge\n\t\t')
f.write('id="' + str(global_id) + '"\n\t\t')
f.write('frame="' + str(frameB_index) + '"\n\t\t')
f.write('startvertex="' + str(streamlineB_vertex_id[k]) + '"\n\t\t')
f.write('endvertex="' + str(streamlineB_vertex_id[k + 1]) + '"\n\t\t')
f.write('curve="xywdense(5')
if duplicate_trigger == 1:
for j in range(streamlineA_split_index[k], streamlineA_split_index[k+1]):
position_x = frameA.streamline[corresponding_list[i].indexA].streamPoint[j].x
position_y = frameA.streamline[corresponding_list[i].indexA].streamPoint[j].y
f.write(' ' + str(position_x) + ',' + str(position_y) + ',' + str(stroke_width))
elif duplicate_trigger == 0:
for j in range(streamlineB_split_index[k], streamlineB_split_index[k+1]):
position_x = frameB.streamline[corresponding_list[i].indexB].streamPoint[j].x
position_y = frameB.streamline[corresponding_list[i].indexB].streamPoint[j].y
f.write(' ' + str(position_x) + ',' + str(position_y) + ',' + str(stroke_width))
f.write(')"\n\t\t')
f.write('color="' + color_value + '"/>\n\t')
f_edge_id.write(str(global_id) + "->" + str(corresponding_list[i].indexB) + "\n")
streamlineB_edge_id.append(global_id)
global_id += 1
# ---InBetween---
if corresponding_list[i].split_num > 0:
for k in range(loop_num-1):
# 3.1 InBetween Vertex
f.write('<inbetweenvertex\n\t\t')
f.write('id="' + str(global_id) + '"\n\t\t')
f.write('beforevertex="' + str(streamlineA_vertex_id[k]) + '"\n\t\t')
f.write('aftervertex="' + str(streamlineB_vertex_id[k+1]) + '"\n\t\t')
f.write('color="' + color_value + '"/>\n\t')
inbetween_vertex_id.append(global_id)
global_id += 1
else:
for k in range(loop_num):
# 3.2 InBetween Vertex
f.write('<inbetweenvertex\n\t\t')
f.write('id="' + str(global_id) + '"\n\t\t')
f.write('beforevertex="' + str(streamlineA_vertex_id[k]) + '"\n\t\t')
f.write('aftervertex="' + str(streamlineB_vertex_id[k]) + '"\n\t\t')
f.write('color="' + color_value + '"/>\n\t')
inbetween_vertex_id.append(global_id)
global_id += 1
if corresponding_list[i].split_num > 0:
for k in range(len(streamlineA_edge_id) -1):
# 4.1 InBetween Streamline Curve Edge
f.write('<inbetweenedge\n\t\t')
f.write('id="' + str(global_id) + '"\n\t\t')
f.write('beforepath="[' + str(streamlineA_edge_id[k]) + '+]"\n\t\t')
f.write('afterpath="' + str(streamlineB_edge_id[k+1]) + '+]"\n\t\t')
f.write('startanimatedvertex="[' + str(inbetween_vertex_id[k]) + ']"\n\t\t')
f.write('endanimatedvertex="' + str(inbetween_vertex_id[k+1]) + ']"\n\t\t')
f.write('color="' + color_value + '"/>\n')
global_id += 1
else:
for k in range(len(streamlineA_edge_id) ):
# 4.2 InBetween Streamline Curve Edge
f.write('<inbetweenedge\n\t\t')
f.write('id="' + str(global_id) + '"\n\t\t')
f.write('beforepath="[' + str(streamlineA_edge_id[k]) + '+]"\n\t\t')
f.write('afterpath="' + str(streamlineB_edge_id[k]) + '+]"\n\t\t')
f.write('startanimatedvertex="[' + str(inbetween_vertex_id[k]) + ']"\n\t\t')
f.write('endanimatedvertex="' + str(inbetween_vertex_id[k+1]) + ']"\n\t\t')
f.write('color="' + color_value + '"/>\n')
global_id += 1
f.write('</objects>\n')
f.write('</layer>\n')
f.write('</vec>')
f.close()
f_edge_id.close()
return
# Streamline Index Id is Wrong, need to recalculate
def write_fading_file(corresponding_list, initial_frame, end_frame, grow_rate):
opacity_rate = 1 / grow_rate
i = initial_frame
opacity_shrink = 1.0
while i <= initial_frame + grow_rate:
filename = "fading_" + str(i) + ".txt"
f = open(filename, "w")
for j in range(len(corresponding_list)):
if corresponding_list[j].grow == 1:
f.write(str(corresponding_list[j].indexA) + ' ')
f.write(str(opacity_shrink) + '\n')
i += 1
opacity_shrink -= opacity_rate
if opacity_shrink <= 0:
opacity_shrink = 0.0
i = end_frame - grow_rate
opacity_grow = 0.0
while i <= end_frame:
filename = "fading_" + str(i) + ".txt"
f = open(filename, "w")
for j in range(len(corresponding_list)):
if corresponding_list[j].grow == -1:
f.write(str(j) + ' ')
f.write(str(opacity_grow) + '\n')
i += 1
opacity_grow += opacity_rate
if opacity_grow >= 1:
opacity_grow = 1.0