summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/tex/generic/pgf/graphdrawing/lua/pgf/gd/trees/ReingoldTilford1981.lua
blob: 8eba23a2106dddd8d9c3e10accdc0a69a8e3dfbb (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
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
-- Copyright 2012 by Till Tantau
--
-- 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

-- @release $Header$



---
-- @section subsubsection {The Reingold--Tilford Layout}
--
-- @end

local ReingoldTilford1981 = {}

-- Imports
local layered = require "pgf.gd.layered"
local declare = require("pgf.gd.interface.InterfaceToAlgorithms").declare
local Storage = require "pgf.gd.lib.Storage"

---
declare {
  key       = "tree layout",
  algorithm = ReingoldTilford1981,
  
  preconditions = {
    connected = true,
    tree      = true
  },

  postconditions = {
    upward_oriented = true
  },

  documentation_in = "pgf.gd.trees.doc"
}


---
declare {
  key    = "missing nodes get space",
  type   = "boolean",
  documentation_in = "pgf.gd.trees.doc"
}



---
declare {
  key     = "significant sep",
  type    = "length",
  initial = "0",
  documentation_in = "pgf.gd.trees.doc"
}


---
declare {
  key  = "binary tree layout",
  use = {
    { key = "tree layout" },
    { key = "minimum number of children" , value=2 },
    { key = "significant sep", value = 10 },
  },
  documentation_in = "pgf.gd.trees.doc"
}
    
---
declare {
  key = "extended binary tree layout",
  use = {
    { key = "tree layout" },
    { key = "minimum number of children" , value=2 },
    { key = "missing nodes get space" },
    { key = "significant sep", value = 0 },
  },
  documentation_in = "pgf.gd.trees.doc"
}




-- Now comes the implementation:
  
function ReingoldTilford1981:run()

  local root = self.spanning_tree.root

  local layers = Storage.new()
  local descendants = Storage.new()
  
  self.extended_version = self.digraph.options['missing nodes get space']
  
  self:precomputeDescendants(root, 1, layers, descendants)
  self:computeHorizontalPosition(root, layers, descendants)
  layered.arrange_layers_by_baselines(layers, self.adjusted_bb, self.ugraph)

end


function ReingoldTilford1981:precomputeDescendants(node, depth, layers, descendants)
  local my_descendants = { node }

  for _,arc in ipairs(self.spanning_tree:outgoing(node)) do
    local head = arc.head
    self:precomputeDescendants(head, depth+1, layers, descendants)
    for _,d in ipairs(descendants[head]) do
      my_descendants[#my_descendants + 1] = d
    end
  end
  
  layers[node] = depth
  descendants[node] = my_descendants
end



function ReingoldTilford1981:computeHorizontalPosition(node, layers, descendants)
  
  local children = self.spanning_tree:outgoing(node)

  node.pos.x = 0

  local child_depth = layers[node] + 1

  if #children > 0 then
    -- First, compute positions for all children:
    for i=1,#children do
      self:computeHorizontalPosition(children[i].head, layers, descendants)
    end
    
    -- Now, compute minimum distances and shift them
    local right_borders = {}

    for i=1,#children-1 do
      
      local local_right_borders = {}
      
      -- Advance "right border" of the subtree rooted at
      -- the i-th child
      for _,d in ipairs(descendants[children[i].head]) do
        local layer = layers[d]
        local x     = d.pos.x          
        if self.extended_version or not (layer > child_depth and d.kind == "dummy") then
          if not right_borders[layer] or right_borders[layer].pos.x < x then
            right_borders[layer] = d
          end
          if not local_right_borders[layer] or local_right_borders[layer].pos.x < x then
            local_right_borders[layer] = d
          end
        end
      end

      local left_borders = {}
      -- Now left for i+1 st child
      for _,d in ipairs(descendants[children[i+1].head]) do
        local layer = layers[d]
        local x     = d.pos.x          
        if self.extended_version or not (layer > child_depth and d.kind == "dummy") then
          if not left_borders[layer] or left_borders[layer].pos.x > x then
            left_borders[layer] = d
          end
        end
      end

      -- Now walk down the lines and try to find out what the minimum
      -- distance needs to be.

      local shift = -math.huge
      local first_dist = left_borders[child_depth].pos.x - local_right_borders[child_depth].pos.x
      local is_significant = false

      for layer,n2 in pairs(left_borders) do
        local n1 = right_borders[layer]
        if n1 then
          shift = math.max(
            shift, 
            layered.ideal_sibling_distance(self.adjusted_bb, self.ugraph, n1, n2) + n1.pos.x - n2.pos.x
          )
        end
        if local_right_borders[layer] then
          if layer > child_depth and
            (left_borders[layer].pos.x - local_right_borders[layer].pos.x <= first_dist) then 
            is_significant = true
          end
        end
      end

      if is_significant then
        shift = shift + self.ugraph.options['significant sep']
      end

      -- Shift all nodes in the subtree by shift:
      for _,d in ipairs(descendants[children[i+1].head]) do
        d.pos.x = d.pos.x + shift
      end
    end
    
    -- Finally, position root in the middle:
    node.pos.x = (children[1].head.pos.x + children[#children].head.pos.x) / 2
  end
end



return ReingoldTilford1981