File: pack.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 (332 lines) | stat: -rw-r--r-- 9,374 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
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
module Rubyvis
  class Layout
    # Alias for Rubyvis::Layout::Indent
    def self.Pack
      Rubyvis::Layout::Pack
    end

    # Implements a hierarchical layout using circle-packing. The meaning of
    # the exported mark prototypes changes slightly in the space-filling
    # implementation:<ul>
    #
    # <li><tt>node</tt> - for rendering nodes; typically a Rubyvis::Dot.
    #
    # <p><li><tt>link</tt> - unsupported; undefined. Links are encoded implicitly
    # in the arrangement of the space-filling nodes.
    #
    # <p><li><tt>label</tt> - for rendering node labels; typically a
    # Rubyvis::Label.
    #
    # </ul>The pack layout support dynamic sizing for leaf nodes, if a
    # {@link #size} psuedo-property is specified. The default size function returns
    # 1, causing all leaf nodes to be sized equally, and all internal nodes to be
    # sized by the number of leaf nodes they have as descendants.
    #
    # <p>The size function can be used in conjunction with the order property,
    # which allows the nodes to the sorted by the computed size. Note: for sorting
    # based on other data attributes, simply use the default <tt>null</tt> for the
    # order property, and sort the nodes beforehand using the {@link pv.Dom}
    # operator.
    #
    # <p>For more details on how to use this layout, see
    # Rubyvis::Layout::Hierarchy.
    #
    # @extends pv.Layout.Hierarchy
    # @see <a href="http://portal.acm.org/citation.cfm?id=1124772.1124851"
    # >"Visualization of large hierarchical data by circle packing"</a> by W. Wang,
    # H. Wang, G. Dai, and H. Wang, ACM CHI 2006.
    #/
    class Pack < Hierarchy
      @properties=Hierarchy.properties.dup
      def initialize
        super
        @node.
        shape_radius(lambda {|n| n.radius }).
        stroke_style("rgb(31, 119, 180)").
        fill_style("rgba(31, 119, 180, 0.25)")


        @node_label.text_align("center")

        @link=nil

        @radius = lambda { 1 }
      end


      ##
      # :attr: spacing
      # The spacing parameter; defaults to 1, which provides a little bit of padding
      # between sibling nodes and the enclosing circle. Larger values increase the
      # spacing, by making the sibling nodes smaller; a value of zero makes the leaf
      # nodes as large as possible, with no padding on enclosing circles.
      #
      # @type number

      ##
      # :attr: order
      # The sibling node order. The default order is <tt>null</tt>, which means to
      # use the sibling order specified by the nodes property as-is. A value of
      # "ascending" will sort siblings in ascending order of size, while "descending"
      # will do the reverse. For sorting based on data attributes other than size,
      # use the default <tt>null</tt> for the order property, and sort the nodes
      # beforehand using the {@link pv.Dom} operator.
      #
      # @see pv.Dom.Node#sort


      attr_accessor_dsl :spacing, :order

      ##
      # Default properties for circle-packing layouts. The default spacing parameter
      # is 1 and the default order is "ascending".
      #
      def self.defaults
        Rubyvis::Layout::Pack.new.mark_extend(Rubyvis::Layout::Hierarchy.defaults).
        spacing(1).
        order("ascending")
      end


      # TODO is it possible for spacing to operate in pixel space?
      # Right now it appears to be multiples of the smallest radius.

      ##
      # Specifies the sizing function. By default, a sizing function is disabled and
      # all nodes are given constant size. The sizing function is invoked for each
      # leaf node in the tree (passed to the constructor).
      #
      # <p>For example, if the tree data structure represents a file system, with
      # files as leaf nodes, and each file has a <tt>bytes</tt> attribute, you can
      # specify a size function as:
      #
      # <pre>    .size(function(d) d.bytes)</pre>
      #
      # As with other properties, a size function may specify additional arguments to
      # access the data associated with the layout and any enclosing panels.
      #
      # @param {function} f the new sizing function.
      # @returns {pv.Layout.Pack} this.
      def size(f)
        if f.is_a? Proc
          @radius=lambda {|*args| Math.sqrt(f.js_apply(self,args))}
        else
          f=Math.sqrt(f)
          @radius=lambda {f}
        end
        self
      end
      ## @private Compute the radii of the leaf nodes. #/

      def radii(nodes)
        stack=Mark.stack
        stack.unshift(nil)
        nodes.each {|c|
          if !c.first_child
            stack[0]=c
            c.radius = @radius.js_apply(self, stack)
          end
        }
        stack.shift
      end
      def pack_tree(n)
        nodes = []
        c=n.first_child
        while(c)
          c.radius=pack_tree(c) if c.first_child
          c.n=c._p=c
          nodes.push(c)
          c=c.next_sibling
        end

        # Sort.
        case @s.order
        when "ascending"
          nodes.sort {|a,b| a.radius<=>b.radius}
        when 'descending'
          nodes.sort {|a,b| b.radius<=>a.radius}
        when 'reverse'
          nodes.reverse
        end

        return pack_circle(nodes)
      end
      def bound(n)
        @x_min = [n.x - n.radius, @x_min].min
        @x_max = [n.x + n.radius, @x_max].max
        @y_min = [n.y - n.radius, @y_min].min
        @y_max = [n.y + n.radius, @y_max].max
      end
      def insert(a,b)
        c = a.n
        a.n = b
        b._p = a
        b.n = c
        c._p = b
      end
      def splice(a, b)
        a.n = b
        b._p = a
      end
      def intersects(a, b)
        dx = b.x - a.x
        dy = b.y - a.y
        dr = a.radius + b.radius
        (dr * dr - dx * dx - dy * dy) > 0.001 # within epsilon
      end

      ## @private #/
      def place(a, b, c)
        da = b.radius + c.radius
        db = a.radius + c.radius
        dx = b.x - a.x
        dy = b.y - a.y
        dc = Math.sqrt(dx * dx + dy * dy)
        cos = (db * db + dc * dc - da * da) / (2.0 * db * dc)
        theta = Math.acos(cos)
        x = cos * db
        h = Math.sin(theta) * db
        dx = dx/dc
        dy = dy/dc
        c.x = a.x + x * dx + h * dy
        c.y = a.y + x * dy - h * dx
      end

      # @private #/
      def transform(n, x, y, k)
        c=n.first_child
        while(c) do
          c.x += n.x
          c.y += n.y
          transform(c, x, y, k)
          c=c.next_sibling
        end
        n.x = x + k * n.x
        n.y = y + k * n.y
        n.radius *= k
        n.mid_angle=0 # Undefined on protovis
      end


      def pack_circle(nodes)
        @x_min = Infinity
        @x_max = -Infinity
        @y_min = Infinity
        @y_max = -Infinity
        a=b=c=j=k=nil


        # Create first node.
        a = nodes[0];
        a.x = -a.radius
        a.y = 0
        bound(a)

        # Create second node. #/
        if (nodes.size > 1)
          b = nodes[1]
          b.x = b.radius
          b.y = 0
          bound(b)

          # Create third node and build chain.
          if (nodes.size > 2)
            c = nodes[2]
            place(a, b, c)
            bound(c)
            insert(a, c)
            a._p = c
            insert(c, b)
            b = a.n

            # Now iterate through the rest.
            i=3
            while(i < nodes.size) do
              c=nodes[i]
              place(a, b, c)

              # Search for the closest intersection. #/
              isect = 0
              s1 = 1
              s2 = 1

              j=b.n
              while(j!=b) do
                if (intersects(j, c))
                  isect=1
                  break
                end
                j=j.n
                s1+=1
              end

              if isect==1
                k=a._p
                while(k!=j._p) do
                  if(intersects(k,c))
                    if(s2 < s1)
                      isect=-1
                      j=k
                    end
                    break
                  end
                  k=k._p
                  s2+=1
                end
              end


              # Update node chain. #/
              if (isect == 0)
                insert(a, c)
                b = c
                bound(c)
              elsif (isect > 0)
                splice(a, j)
                b = j
                i-=1
              elsif (isect < 0)
                splice(j, b)
                a = j
                i-=1
              end
              i+=1
            end


          end
        end

        # Re-center the circles and return the encompassing radius. #/
        cx = (@x_min + @x_max) / 2.0
        cy = (@y_min + @y_max) / 2.0
        cr = 0
        nodes.each do |n|
          n.x -= cx
          n.y -= cy
          cr = [cr, n.radius + Math.sqrt(n.x * n.x + n.y * n.y)].max
        end
        cr + @s.spacing
      end


      def build_implied(s)
        return nil if hierarchy_build_implied(s)
        @s=s
        nodes = s.nodes
        root = nodes[0]
        radii(nodes)

        # Recursively compute the layout. #/
        root.x = 0
        root.y = 0
        root.radius = pack_tree(root)

        w = self.width
        h = self.height
        k = 1.0 / [2.0 * root.radius / w, 2.0 * root.radius / h].max
        transform(root, w / 2.0, h / 2.0, k)
      end
    end
  end
end