File: tree.rb

package info (click to toggle)
ruby-rubyvis 0.6.1%2Bdfsg1-2
  • links: PTS, VCS
  • area: main
  • in suites: buster, stretch
  • size: 1,808 kB
  • ctags: 679
  • sloc: ruby: 11,114; makefile: 2
file content (267 lines) | stat: -rw-r--r-- 7,770 bytes parent folder | download | duplicates (3)
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
module Rubyvis
  class Layout
    # Alias for Rubyvis::Layout::Tree 
    def self.Tree
      Rubyvis::Layout::Tree
    end
    # Implements a node-link tree diagram using the Reingold-Tilford "tidy"
    # tree layout algorithm. The specific algorithm used by this layout is based on
    # <a href="http://citeseer.ist.psu.edu/buchheim02improving.html">"Improving
    # Walker's Algorithm to Run in Linear Time"</A> by C. Buchheim, M. J&uuml;nger
    # &amp; S. Leipert, Graph Drawing 2002. This layout supports both cartesian and
    # radial orientations orientations for node-link diagrams.
    #
    # <p>The tree layout supports a "group" property, which if true causes siblings
    # to be positioned closer together than unrelated nodes at the same depth. The
    # layout can be configured using the <tt>depth</tt> and <tt>breadth</tt>
    # properties, which control the increments in pixel space between nodes in both
    # dimensions, similar to the indent layout.
    #
    # <p>For more details on how to use this layout, see
    # Rubyvis::Layout::Hierarchy
    class Tree < Hierarchy
      @properties=Hierarchy.properties.dup
      def initialize
        super
      end
      
      ##
      # :attr: breadth
      # The offset between siblings nodes; defaults to 15.
            
      ##
      # :attr: depth
      # The offset between parent and child nodes; defaults to 60.
      #
      
      ##
      # :attr: orient
      # The orientation. The default orientation is "top", which means that the root
      # node is placed on the top edge, leaf nodes appear at the bottom, and internal
      # nodes are in-between. The following orientations are supported:<ul>
      #
      # <li>left - left-to-right.
      # <li>right - right-to-left.
      # <li>top - top-to-bottom.
      # <li>bottom - bottom-to-top.
      # <li>radial - radially, with the root at the center.</ul>
      #
      
      ##
      # :group:
      # The sibling grouping, i.e., whether differentiating space is placed between
      # sibling groups. The default is 1 (or true), causing sibling leaves to be
      # separated by one breadth offset. Setting this to false (or 0) causes
      # non-siblings to be adjacent.
      
      attr_accessor_dsl :group, :breadth, :depth, :orient
      
      # Default properties for tree layouts. The default orientation is "top",
      # the default group parameter is 1, and the default breadth and depth
      # offsets are 15 and 60 respectively.
      def self.defaults
        Rubyvis::Layout::Tree.new.mark_extend(Rubyvis::Layout::Hierarchy.defaults).
        group(1).
        breadth(15).
        depth(60).
        orient("top")
      end
      
      def first_walk(v)
        l,r,a=nil,nil,nil
        if (!v.first_child)
          l= v.previous_sibling
          v.prelim = l.prelim + distance(v.depth, true)  if l
        else    
          l = v.first_child
          r = v.last_child
          a = l # default ancestor
          v.each_child {|c| 
            first_walk(c)
            a = apportion(c, a)
          }
          execute_shifts(v)
          midpoint = 0.5 * (l.prelim + r.prelim)
          l = v.previous_sibling
          if l
            v.prelim = l.prelim + distance(v.depth, true)
            v.mod = v.prelim - midpoint
          else
            v.prelim = midpoint
          end
        end
      end
      def second_walk(v,m,depth)
        v.breadth = v.prelim + m
        m += v.mod
        v.each_child {|c|
          second_walk(c, m, depth)
        }
      end
      def apportion(v,a)
        w = v.previous_sibling
        if w
          vip = v
          vop = v
          vim = w
          vom = v.parent_node.first_child
          sip = vip.mod
          sop = vop.mod
          sim = vim.mod
          som = vom.mod
          nr = next_right(vim)
          nl = next_left(vip)
          while (nr and nl) do
            vim = nr
            vip = nl
            vom = next_left(vom)
            vop = next_right(vop)
            vop.ancestor = v
            shift = (vim.prelim + sim) - (vip.prelim + sip) + distance(vim.depth, false)
            if (shift > 0)
              move_subtree(ancestor(vim, v, a), v, shift)
              sip += shift
              sop += shift
            end
            sim += vim.mod
            sip += vip.mod
            som += vom.mod
            sop += vop.mod
            nr = next_right(vim)
            nl = next_left(vip)
          end
          
          if (nr and !next_right(vop))
            vop.thread = nr
            vop.mod += sim - sop
          end
          if (nl and !next_left(vom))
            vom.thread = nl
            vom.mod += sip - som
            a = v
          end
        end
        a
      end
      
      def next_left(v)
        v.first_child ? v.first_child : v.thread
      end
      def next_right(v)
        v.last_child ? v.last_child : v.thread
      end
      
      def move_subtree(wm, wp, shift) 
        subtrees = wp.number - wm.number
        wp.change -= shift / subtrees.to_f
        wp.shift += shift
        wm.change += shift / subtrees.to_f
        wp.prelim += shift
        wp.mod += shift
      end
      
      def execute_shifts(v) 
        shift = 0
        change = 0
        c=v.last_child
        while c        
          c.prelim += shift
          c.mod += shift
          change += c.change
          shift += c.shift + change
          c = c.previous_sibling          
        end
      end
      def ancestor(vim, v, a) 
        (vim.ancestor.parent_node == v.parent_node) ? vim.ancestor : a
      end

      def distance(depth, siblings) 
        (siblings ? 1 : (@_group + 1)).to_f / ((@_orient == "radial") ? depth : 1)
      end

      
      def mid_angle(n)
        (@_orient == "radial") ? n.breadth.to_f / depth : 0;
      end
      
      #/** @private */
      def x(n)
        case @_orient
          when "left"
            n.depth;
          when "right"
            @_w - n.depth;
          when "top"
            n.breadth + @_w / 2.0
          when "bottom"
            n.breadth + @_w / 2.0
          when "radial"
            @_w / 2.0 + n.depth * Math.cos(mid_angle(n))
        end
      end
      
      #/** @private */
      def y(n)
        case @_orient
          when "left"
            n.breadth + @_h / 2.0
          when "right"
            n.breadth + @_h / 2.0
          when "top"
            n.depth;
          when "bottom"
            @_h - n.depth
          when "radial"
            @_h / 2.0 + n.depth * Math.sin(mid_angle(n))
        end
      end

      
      
      def build_implied(s)
        return nil if hierarchy_build_implied(s)        
        @_nodes = s.nodes
        @_orient = s.orient
        @_depth = s.depth
        @_breadth = s.breadth
        @_group = s.group
        @_w = s.width
        @_h = s.height
        root=@_nodes[0]
        root.visit_after {|v,i|
          v.ancestor = v
          v.prelim = 0
          v.mod = 0
          v.change = 0
          v.shift = 0
          v.number = v.previous_sibling ? (v.previous_sibling.number + 1) : 0
          v.depth = i
        }
        #/* Compute the layout using Buchheim et al.'s algorithm. */
        first_walk(root)
        second_walk(root, -root.prelim, 0)
        
        root.visit_after {|v,i|
          v.breadth *= breadth
          v.depth *= depth
          v.mid_angle = mid_angle(v)
          v.x = x(v)
          v.y = y(v)
          v.mid_angle += Math::PI if (v.first_child)
          v.breadth=nil
          v.depth=nil
          v.ancestor=nil
          v.prelim=nil
          v.mod=nil
          v.change=nil
          v.shift=nil
          v.number=nil
          v.thread=nil
        }
        
        
      end
    end
  end
end