/usr/share/perl5/Graph/Easy/Layout/Force.pm is in libgraph-easy-perl 0.75-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 | #############################################################################
# Force-based layouter for Graph::Easy.
#
# (c) by Tels 2004-2007.
#############################################################################
package Graph::Easy::Layout::Force;
$VERSION = '0.75';
#############################################################################
#############################################################################
package Graph::Easy;
use strict;
use warnings;
use Graph::Easy::Util qw(ord_values);
sub _layout_force
{
# Calculate for each node the force on it, then move them accordingly.
# When things have settled, stop.
my ($self) = @_;
# For each node, calculate the force actiing on it, seperated into two
# components along the X and Y axis:
# XXX TODO: replace with all contained nodes + groups
my @nodes = $self->nodes();
return if @nodes == 0;
my $root = $self->root_node();
if (!defined $root)
{
# find a suitable root node
$root = $nodes[0];
}
# this node never moves
$root->{_pinned} = undef;
$root->{x} = 0;
$root->{y} = 0;
# get the "gravity" force
my $gx = 0; my $gy = 0;
my $flow = $self->flow();
if ($flow == 0)
{
$gx = 1;
}
elsif ($flow == 90)
{
$gy = -1;
}
elsif ($flow == 270)
{
$gy = 1;
}
else # ($flow == 180)
{
$gx = -1;
}
my @particles;
# set initial positions
for my $n (@nodes)
{
# the net force on this node is the gravity
$n->{_x_force} = $gx;
$n->{_y_force} = $gy;
if ($root == $n || defined $n->{origin})
{
# nodes that are relative to another are "pinned"
$n->{_pinned} = undef;
}
else
{
$n->{x} = rand(100);
$n->{y} = rand(100);
push @particles, $n;
}
}
my $energy = 1;
while ($energy > 0.1)
{
$energy = 0;
for my $n (@particles)
{
# reset forces on this node
$n->{_x_force} = 0;
$n->{_y_force} = 0;
# Add forces of all other nodes. We need to include pinned nodes here,
# too, since a moving node might get near a pinned one and get repelled.
for my $n2 (@nodes)
{
next if $n2 == $n; # don't repel yourself
my $dx = ($n->{x} - $n2->{x});
my $dy = ($n->{y} - $n2->{y});
my $r = $dx * $dx + $dy * $dy;
$r = 0.01 if $r < 0.01; # too small?
if ($r < 4)
{
# not too big
$n->{_x_force} += 1 / $dx * $dx;
$n->{_y_force} += 1 / $dy * $dy;
my $dx2 = 1 / $dx * $dx;
my $dy2 = 1 / $dy * $dy;
print STDERR "# Force between $n->{name} and $n2->{name}: fx $dx2, fy $dy2\n";
}
}
# for all edges connected at this node
for my $e (ord_values ( $n->{edges} ))
{
# exclude self-loops
next if $e->{from} == $n && $e->{to} == $n;
# get the other end-point of this edge
my $n2 = $e->{from}; $n2 = $e->{to} if $n2 == $n;
# XXX TODO
# we should "connect" the edges to the appropriate port so that
# they excert an off-center force
my $dx = -($n->{x} - $n2->{x}) / 2;
my $dy = -($n->{y} - $n2->{y}) / 2;
print STDERR "# Spring force between $n->{name} and $n2->{name}: fx $dx, fy $dy\n";
$n->{_x_force} += $dx;
$n->{_y_force} += $dy;
}
print STDERR "# $n->{name}: Summed force: fx $n->{_x_force}, fy $n->{_y_force}\n";
# for grid-like layouts, add a small force drawing this node to the gridpoint
# 0.7 => 1 - 0.7 => 0.3
# 1.2 => 1 - 1.2 => -0.2
my $dx = int($n->{x} + 0.5) - $n->{x};
$n->{_x_force} += $dx;
my $dy = int($n->{y} + 0.5) - $n->{y};
$n->{_y_force} += $dy;
print STDERR "# $n->{name}: Final force: fx $n->{_x_force}, fy $n->{_y_force}\n";
$energy += $n->{_x_force} * $n->{_x_force} + $n->{_x_force} * $n->{_y_force};
print STDERR "# Net energy: $energy\n";
}
# after having calculated all forces, move the nodes
for my $n (@particles)
{
my $dx = $n->{_x_force};
$dx = 5 if $dx > 5; # limit it
$n->{x} += $dx;
my $dy = $n->{_y_force};
$dy = 5 if $dy > 5; # limit it
$n->{y} += $dy;
print STDERR "# $n->{name}: Position $n->{x}, $n->{y}\n";
}
sleep(1); print STDERR "\n";
}
for my $n (@nodes)
{
delete $n->{_x_force};
delete $n->{_y_force};
}
$self;
}
1;
__END__
=head1 NAME
Graph::Easy::Layout::Force - Force-based layouter for Graph::Easy
=head1 SYNOPSIS
use Graph::Easy;
my $graph = Graph::Easy->new();
$graph->add_edge ('Bonn', 'Berlin');
$graph->add_edge ('Bonn', 'Ulm');
$graph->add_edge ('Ulm', 'Berlin');
$graph->layout( type => 'force' );
print $graph->as_ascii( );
# prints:
# +------------------------+
# | v
# +------+ +-----+ +--------+
# | Bonn | --> | Ulm | --> | Berlin |
# +------+ +-----+ +--------+
=head1 DESCRIPTION
C<Graph::Easy::Layout::Force> contains routines that calculate a
force-based layout for a graph.
Nodes repell each other, while edges connecting them draw them together.
The layouter calculates the forces on each node, then moves them around
according to these forces until things have settled down.
Used automatically by Graph::Easy.
=head1 EXPORT
Exports nothing.
=head1 SEE ALSO
L<Graph::Easy>.
=head1 METHODS
This module injects the following methods into Graph::Easy:
=head2 _layout_force()
Calculates the node position with a force-based method.
=head1 AUTHOR
Copyright (C) 2004 - 2007 by Tels L<http://bloodgate.com>.
See the LICENSE file for information.
=cut
|