| 1 | package Treemap::Squarified; | ||
| 2 | |||
| 3 | use 5.006; | ||
| 4 | use strict; | ||
| 5 | use warnings; | ||
| 6 | use Carp; | ||
| 7 | |||
| 8 | require Exporter; | ||
| 9 | require Treemap; | ||
| 10 | |||
| 11 | our @ISA = qw(Treemap Exporter); | ||
| 12 | our @EXPORT_OK = ( ); | ||
| 13 | our @EXPORT = qw( ); | ||
| 14 | our $VERSION = '0.02'; | ||
| 15 | |||
| 16 | |||
| 17 | # ------------------------------------------ | ||
| 18 | # Methods: | ||
| 19 | # ------------------------------------------ | ||
| 20 | |||
| 21 | # | ||
| 22 | # Input: a tree of array-hashes | ||
| 23 | # Output: none, sent to callbacks | ||
| 24 | # | ||
| 25 | sub _map | ||
| 26 | { | ||
| 27 | my $self = shift; | ||
| 28 | my ( @p, @q, $tree ); | ||
| 29 | my $debug = undef; | ||
| 30 | ( $tree, $p[0], $p[1], $q[0], $q[1] ) = @_; | ||
| 31 | |||
| 32 | $self->{DEBUG} && print STDERR "Drawing space for $tree->{name}:\n\t@p, @q\n"; | ||
| 33 | |||
| 34 | # Draw our rectangle | ||
| 35 | $self->{ OUTPUT }->rect( $p[0], $p[1], $q[0], $q[1], $tree->{colour} ); | ||
| 36 | |||
| 37 | # Non-empty Set, Descend | ||
| 38 | if( $tree->{children} ) | ||
| 39 | { | ||
| 40 | my ( $pt, $qt ) = $self->_shrink( \@p, \@q, $self->{PADDING} ); | ||
| 41 | my @p = @{$pt}; my @q = @{$qt}; | ||
| 42 | |||
| 43 | $self->{DEBUG} && print STDERR "\tI have " . scalar( @{$tree->{children}} ) . " children... "; | ||
| 44 | |||
| 45 | # Check number of children | ||
| 46 | # If < 3, two slices on the longest side is optimal aspect ratio | ||
| 47 | if( scalar(@{$tree->{children}}) < 3 ) | ||
| 48 | { | ||
| 49 | $self->{DEBUG} && print STDERR "SLICE.\n"; | ||
| 50 | |||
| 51 | my ( @r, @s, $o, $width ); | ||
| 52 | $o = ( abs($p[0]-$q[0]) > abs($p[1]-$q[1]) ? 0 : 1 ); | ||
| 53 | @r = @p; | ||
| 54 | @s = @q; | ||
| 55 | $width = abs( $s[$o] - $r[$o] ); | ||
| 56 | foreach my $child( @{$tree->{children}} ) | ||
| 57 | { | ||
| 58 | $s[$o] = $r[$o] + $width * | ||
| 59 | ( $child->{size} / $tree->{size} ) if( $tree->{size} > 0 ); | ||
| 60 | { | ||
| 61 | my ( $st, $rt ) = $self->_shrink( \@s, \@r, $self->{SPACING} ); | ||
| 62 | my @s = @{$st}; my @r = @{$rt}; | ||
| 63 | $self->_map( $child, $r[0], $r[1], $s[0], $s[1] ); | ||
| 64 | } | ||
| 65 | $r[$o] = $s[$o]; | ||
| 66 | } | ||
| 67 | } | ||
| 68 | # Otherwise, find optimal aspect ratio | ||
| 69 | else | ||
| 70 | { | ||
| 71 | $self->{DEBUG} && print STDERR "SQUARIFY.\n"; | ||
| 72 | |||
| 73 | # Sort children by size, descending | ||
| 74 | my @indices = 0..( scalar( @{$tree->{children}} ) - 1 ); | ||
| 75 | my @sorted_children = sort { $tree->{children}->[$b]->{size} <=> | ||
| 76 | $tree->{children}->[$a]->{size} } | ||
| 77 | @indices; | ||
| 78 | |||
| 79 | # Fetch each entry and compute the aspect ratio when their areas are | ||
| 80 | # combined. | ||
| 81 | # | ||
| 82 | # height (h), and area (a) are our "fixed" values, and width (w) will | ||
| 83 | # change based on the current 'a'. | ||
| 84 | # | ||
| 85 | # So: | ||
| 86 | # a = h*w | ||
| 87 | # w = a/h | ||
| 88 | # | ||
| 89 | # And: | ||
| 90 | # aspect = w/h | ||
| 91 | # | ||
| 92 | # Therefore: | ||
| 93 | # aspect = (a/h)/h | ||
| 94 | # = a / h**2 | ||
| 95 | # | ||
| 96 | |||
| 97 | my ( $area, $parent_area, $parent_aspect, $usable_width, $usable_height, @j, @k, $o ); | ||
| 98 | $area = 0; | ||
| 99 | $parent_area = $tree->{size}; | ||
| 100 | @j = @p; | ||
| 101 | @k = @q; | ||
| 102 | $o = ( abs($j[0]-$k[0]) > abs($j[1]-$k[1]) ? 0 : 1 ); | ||
| 103 | $usable_width = 0; | ||
| 104 | |||
| 105 | # Only run if these children consume space, and we indeed have children | ||
| 106 | while( $parent_area > 0 && @sorted_children > 0 ) | ||
| 107 | { | ||
| 108 | # Remove area that was consumed by 'special children' (see below) | ||
| 109 | $parent_area -= $area; | ||
| 110 | |||
| 111 | # Reset consumed area | ||
| 112 | $area = 0; | ||
| 113 | |||
| 114 | # Determine new boundary | ||
| 115 | $j[$o] = $j[$o] + $usable_width; | ||
| 116 | |||
| 117 | # Exit loop we've run out of pixel drawing space (prevents division | ||
| 118 | # by zero errors in aspect ratio calculations) | ||
| 119 | last if ( $j[0] == $k[0] || $j[1] == $k[1] ); | ||
| 120 | |||
| 121 | # Determine new orientation based on new boundary | ||
| 122 | $o = ( abs($j[0]-$k[0]) > abs($j[1]-$k[1]) ? 0 : 1 ); | ||
| 123 | |||
| 124 | # Determine new parent aspect ratio based on new boundary | ||
| 125 | $parent_aspect = ( | ||
| 126 | abs( $j[$o] - $k[$o] ) / | ||
| 127 | abs( $j[($o xor 1)] - $k[($o xor 1)] ) | ||
| 128 | ); | ||
| 129 | |||
| 130 | # Determine new scaled height based on new aspect and available area | ||
| 131 | my $scaled_height = sqrt( $parent_area / $parent_aspect ); | ||
| 132 | |||
| 133 | # Reset special children to nothing | ||
| 134 | my @special_children; | ||
| 135 | |||
| 136 | # Reset apsect ratio | ||
| 137 | my $aspect = 0; | ||
| 138 | |||
| 139 | while( scalar( @sorted_children ) > 0 ) | ||
| 140 | { | ||
| 141 | my $child = shift( @sorted_children ); | ||
| 142 | push( @special_children, $child ); | ||
| 143 | my $area_test = $area + $tree->{children}->[$child]->{size}; | ||
| 144 | |||
| 145 | # Find worst aspect ratio in this set of special children | ||
| 146 | my $aspect_test = $self->_find_worst( $tree->{children}, \@special_children, $area_test, $scaled_height ); | ||
| 147 | |||
| 148 | # If this aspect ratio is better than the last, keep searching | ||
| 149 | if( $aspect_test > $aspect ) | ||
| 150 | { | ||
| 151 | |||
| 152 | $self->{DEBUG} && print STDERR "\t\t$aspect_test is a BETTER aspect ratio than $aspect\n"; | ||
| 153 | |||
| 154 | # getting warmer, keep searching | ||
| 155 | $area = $area_test; | ||
| 156 | $aspect = $aspect_test; | ||
| 157 | } | ||
| 158 | else | ||
| 159 | { | ||
| 160 | |||
| 161 | $self->{DEBUG} && print STDERR "\t\t$aspect_test is a WORSE aspect ratio than $aspect\n"; | ||
| 162 | |||
| 163 | # nope, last set was better, undo this scenario. | ||
| 164 | pop( @special_children ); | ||
| 165 | unshift( @sorted_children, $child ); | ||
| 166 | # last set was the optimum set for this space, so drop out of | ||
| 167 | # the loop and handle these special children | ||
| 168 | last; | ||
| 169 | } | ||
| 170 | } | ||
| 171 | |||
| 172 | # Handle special children | ||
| 173 | if( @special_children > 0 ) | ||
| 174 | { | ||
| 175 | |||
| 176 | $self->{DEBUG} && print STDERR "\t\t\tHandling Special Children: @special_children\n"; | ||
| 177 | |||
| 178 | my ( @r, @s ); | ||
| 179 | my $o_xor = ( $o xor 1 ); | ||
| 180 | # Amount of width these children are allowed to consume from parent space | ||
| 181 | $usable_width = ($k[$o]-$j[$o]) * ( $area / $parent_area ); | ||
| 182 | # Amount of height these children are allowed to consume from | ||
| 183 | # parent space (all in this case) | ||
| 184 | $usable_height = $k[$o_xor] - $j[$o_xor]; | ||
| 185 | |||
| 186 | @r = @j; | ||
| 187 | @s = @k; | ||
| 188 | $s[$o] = $r[$o] + $usable_width; | ||
| 189 | |||
| 190 | $self->{DEBUG} && print STDERR "\t\t\tUsable Space for Special Children: $usable_width x $usable_height\n"; | ||
| 191 | |||
| 192 | # Each child gets a slice of the available height | ||
| 193 | foreach my $child( @special_children ) | ||
| 194 | { | ||
| 195 | $s[$o_xor] = $r[$o_xor] + $usable_height * | ||
| 196 | ( $tree->{children}->[$child]->{size} / $area ) | ||
| 197 | if( $area > 0 ); | ||
| 198 | { | ||
| 199 | my ( $st, $rt ) = $self->_shrink( \@s, \@r, $self->{SPACING} ); | ||
| 200 | my @s = @{$st}; my @r = @{$rt}; | ||
| 201 | $self->_map( $tree->{children}->[$child], | ||
| 202 | $r[0], $r[1], $s[0], $s[1] | ||
| 203 | ); | ||
| 204 | } | ||
| 205 | $r[$o_xor] = $s[$o_xor]; | ||
| 206 | } | ||
| 207 | } | ||
| 208 | else | ||
| 209 | { | ||
| 210 | $self->{DEBUG} && print STDERR "No special children... awww\n"; | ||
| 211 | } | ||
| 212 | # Continue processing remaining children at top of loop | ||
| 213 | } | ||
| 214 | } | ||
| 215 | } | ||
| 216 | # Draw label | ||
| 217 | $self->{ OUTPUT }->text( $p[0], $p[1], $q[0], $q[1], $tree->{name}, ($tree->{children}?1:undef) ); | ||
| 218 | } | ||
| 219 | |||
| 220 | # Expects the 'height' of the area we're filling | ||
| 221 | # No side-effects. | ||
| 222 | #WARNING WARNING WARNING | ||
| 223 | #WARNING WARNING WARNING | ||
| 224 | # | ||
| 225 | # Hey you already know the area from the parent, why are you re-calculating it? | ||
| 226 | # Change the expected format to be treemap data, rather than an array | ||
| 227 | # | ||
| 228 | #WARNING WARNING WARNING | ||
| 229 | #WARNING WARNING WARNING | ||
| 230 | #WARNING WARNING WARNING | ||
| 231 | sub _find_worst | ||
| 232 | { | ||
| 233 | my $self = shift; | ||
| 234 | my ( $tree, $set, $area, $height ) = @_; | ||
| 235 | |||
| 236 | # Find width | ||
| 237 | my $width = $area / $height; | ||
| 238 | my $width_squared = $width ** 2; | ||
| 239 | |||
| 240 | #IDEAIDEA | ||
| 241 | # | ||
| 242 | # We could optionally determine the average aspect ratio, and use that for | ||
| 243 | # comparison to other sets | ||
| 244 | # | ||
| 245 | #IDEAIDEA | ||
| 246 | # Find worst aspect ratio | ||
| 247 | my $worst = undef; | ||
| 248 | foreach my $item( @{$set} ) | ||
| 249 | { | ||
| 250 | # for our purposes, apsect = w/h, where w>h, but we'll take the inverse | ||
| 251 | # if it exeeds 1 | ||
| 252 | # | ||
| 253 | # aspect = w/h; area = w*h, h = area/w | ||
| 254 | # aspect = w/(area/w) | ||
| 255 | # = w^2/area | ||
| 256 | # | ||
| 257 | |||
| 258 | # An item with a size/area of 0 is the worst possible thing. It's aspect | ||
| 259 | # ratio is infinite, which is ... the worst you could wish for ;) | ||
| 260 | return 0 if $tree->[$item]->{size} == 0; | ||
| 261 | |||
| 262 | my $aspect = $width_squared / $tree->[$item]->{size}; | ||
| 263 | |||
| 264 | # if an aspect ratio is > 1, we take the inverse | ||
| 265 | $aspect = 1 / $aspect if ( $aspect > 1 ); | ||
| 266 | |||
| 267 | if ( $worst ) | ||
| 268 | { | ||
| 269 | $worst = $aspect if ( $aspect < $worst ); | ||
| 270 | } | ||
| 271 | else | ||
| 272 | { | ||
| 273 | $worst = $aspect; | ||
| 274 | } | ||
| 275 | } | ||
| 276 | return $worst; | ||
| 277 | } | ||
| 278 | |||
| 279 | 1; | ||
| 280 | __END__ |