summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/tex/generic/pgf/graphdrawing/lua/pgf/gd/experimental/evolving/layered.lua
blob: dc1e8ab571cc719daa060456c02c9bf8e0de3569 (plain)
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
-- Copyright 2012 by Till Tantau
-- Copyright 2015 by Malte Skambath
--
-- This file may be distributed an/or modified
--
-- 1. under the LaTeX Project Public License and/or
-- 2. under the GNU Public License
--
-- See the file doc/generic/pgf/licenses/LICENSE for more information




local temporallayered = {}

-- Namespace

--require("pgf.gd").layered = layered
--require("pgf.gd.experimental.evolving").layered = layered

-- Import
local lib = require "pgf.gd.lib"
local Storage = require "pgf.gd.lib.Storage"
local layered = require "pgf.gd.layered"

-- 
-- This file defines some basic functions to compute and/or set the
-- ideal distances between nodes of any kind of layered drawing of a
-- graph.



---
-- Position nodes in layers using baselines
--
-- @param layers A |Storage| object assigning layers to vertices.
-- @param paddings A |Storage| object storing the computed distances
-- (paddings). 
-- @param graph The graph in which the nodes reside
-- @param snapshots The list of snapshots over which the overlaying evolving
--                  graph exists
function temporallayered.arrange_layers_by_baselines (layers, paddings, graph, snapshots, vertex_snapshots)
  assert(vertex_snapshots, "vertex_snapshots must not be nil")
  --local layer_vertices = Storage.newTableStorage()
  local snapshots_layers = Storage.newTableStorage()
  local count_layers = 0
  -- Decompose into layers:
  for _,v in ipairs(graph.vertices) do
    local layer_vertices = snapshots_layers[vertex_snapshots[v]] or {}
    if layer_vertices[layers[v]] == nil then
      assert( layers[v], "layer of node " .. v.name .. " has not been computed.")
      layer_vertices[layers[v]] = {}
    end
    table.insert(layer_vertices[layers[v]], v)
    count_layers = math.max(count_layers, layers[v])
  end

  if count_layers > 0 then
    
    
    -- Now compute ideal distances and store
    local height = 0
    
    for _, s in ipairs(snapshots) do
      local layer_vertices = snapshots_layers[s]
      if #layer_vertices > 0 then -- sanity check
	for _,v in ipairs(layer_vertices[1]) do
	  v.pos.y = 0
	end
      end
    end
    
    for i=2, count_layers do
      local distance = 0
      for _, s in ipairs(snapshots) do
	local layer_vertices = snapshots_layers[s]
	if #layer_vertices >= i then
	  distance = math.max(
	    distance,
	    layered.baseline_distance(
	      paddings,
	      s,
	      layer_vertices[i-1],
	      layer_vertices[i]))
	end
      end
      
      height = height + distance
      
      for _, s in ipairs(snapshots) do
	local layer_vertices = snapshots_layers[s]
	if #layer_vertices >= i then
	  for _,v in ipairs(layer_vertices[i]) do
	    v.pos.y = height 
	  end
	end
      end
    end
  end  
end




-- Done

return temporallayered