I am trying to solve an interesting problem (I will code it in JavaScript, but doesn't really matter):
Let's say there are multiple videos as segments of few seconds on different y layers. If videos overlaps in time then the one which is in the top layer will be visible when playing all of them at the same time.
Now, I have a lot of this layers (this is the problem) I want to merge some layers when possible so at the end I will have same number of videos visually displayed in the same way, but less layers.
I will show you an image which will give a better understanding
In this image I took an example of 11 videos, on 11 initial layers.
For example we can see that 2 and 1 can be put on the same layer since they do not overlap and visually the videos will be displayed the same, but for instance 1 and 9 cannot be put together neighter on the 9th layer nor on 1st layer since 7 overaps and will lose the display order (z-index)
If I want to represent this in code:
const orderedSegments = [
[15, 18], // 1
[0.3, 9], // 2
[4, 13], // 3
[8, 14], // 4
[1, 3], // 5
[16, 19.5], // 6
[4.1, 17.5], // 7
[0, 2.9], // 8
[2.9, 11], // 9
[12.5, 19.4], // 10
[11.3, 12] // 11
]
And here is how can look one of possible result which will have only 5 layers but with the same display:
const expectedLayers = [
[[0.3, 9], [15, 18]], // 2, 1
[[1, 3], [4, 13]], // 5, 3
[[8, 14], [16, 19.5]], // 4, 6
[[0, 2.9], [4.1, 17.5]], // 8, 7
[[2.9, 11], [11.3, 12], [12.5, 19.4]] // 9, 11, 10
]
I have thought to sort the segments by the start duration and then to create 1 layer and try to insert into it as much as possible and when not possible anymore to create a new layer... but I'm not sure how to preserve correctly the order.
So that's why I'm asking to see if there are maybe some known algorithms to do such things like merging segments while preserving order.
Thanks for your ideas.
