File: visual.rules

package info (click to toggle)
polymake 3.0r2-2
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 19,752 kB
  • ctags: 30,928
  • sloc: cpp: 151,785; perl: 32,510; ansic: 3,597; java: 2,654; python: 278; makefile: 181; xml: 103; sh: 79
file content (282 lines) | stat: -rw-r--r-- 9,121 bytes parent folder | download
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
#  Copyright (c) 1997-2015
#  Ewgenij Gawrilow, Michael Joswig (Technische Universitaet Berlin, Germany)
#  http://www.polymake.org
#
#  This program is free software; you can redistribute it and/or modify it
#  under the terms of the GNU General Public License as published by the
#  Free Software Foundation; either version 2, or (at your option) any
#  later version: http://www.gnu.org/licenses/gpl.txt.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#-------------------------------------------------------------------------------

package Visual::Color;

# graph edges: black
custom $graph="0 0 0";

require Visual::Graph;
require Visual::Lattice;

# @topic objects/Visual::Graph
# @category Visualization
# Collection of nodes and edges of an abstract graph amended with visual decoration attributes
# and an optional embedding in 3-d.
# @super Visual::Object

# @topic objects/Visual::Lattice
# @category Visualization
# Collection of nodes (representing faces of a face lattice) and edges (representing the inclusion relation)
# amended with visual decoration attributes and an optional embedding in 2-d.
# @super Visual::Graph

# @category Visualization
# Attributes modifying the appearance of graphs

options %Visual::Graph::decorations=(
   %Visual::Wire::decorations,

   # Matrix<Float>  2-d or 3-d coordinates of the nodes.
   #                If not specified, a random embedding is generated using a pseudo-physical spring model
   Coord => undef,

   # Flexible<RGB>  alias for PointColor
   NodeColor => undef,

   # Flexible<Float>  alias for PointThickness
   NodeThickness => undef,

   # Flexible<RGB>  alias for PointBorderColor
   NodeBorderColor => undef,

   # Flexible<Float>  alias for PointBorderThickness
   NodeBorderThickness => undef,

   # Flexible<String>  alias for PointStyle
   NodeStyle => undef,

   # String  alias for PointLabels
   NodeLabels => undef,
);


# @category Visualization
# Attributes modifying the appearance of face lattices

options %Visual::Lattice::decorations=(
   %Visual::Graph::decorations,

   # Flexible<Int>  How to draw directed edges: 0 (like undirected), 1 (with an arrow pointing towards the edge),
   #                or -1 (with an arrow pointing against the edge).  Default is 1.
   ArrowStyle => undef,

   # Array<String>  Labels of atoms, to use as building blocks for node labels.  By default the ordinal numbers are taken.
   AtomLabels => undef,
);

###############################################################################
#
# Home-made 3-d spring embedder with variations
#
package Visual::GraphEmbedding;

# Tuning parameters for the graph embedder.
# Almost all are multiplicative factors; the comments describe
# the effect of INCREASING of the corresponding parameter.
custom %graph_parameters=(
   scale => 1,           # Float  the average edge length increases
   inertion => 0.1,      # Float  the computations might converge more slowly, but more steadily
   viscosity => 1,       # Float  improves the convergence at the risk of being stuck at a bad local minimum
   balance => 10,        # Float  the non-neighbor nodes are more spread away from each other
   'max-iterations' => 10000,  # Int  The iteration limit. Not multiplicative!
);

# Additional tuning parameters for the interactive spring embedder.
custom %interactive_parameters=(
   eps => 1e-4           # Float  the relative movement to be accepted as "no movement"
);

@ISA=qw( Visual::DynamicCoords );

# perform static computations
sub compute {
   my ($self)=@_;
   $self->merge_options(\%graph_parameters);
   spring_embedder($self->source, $self->options);
}

# run interactive embedder
sub run {
   my ($self, $window)=@_;
   my $n=$self->source->nodes;
   
   $self->merge_options(\%graph_parameters);
   $self->merge_options(\%interactive_parameters);
   if (!is_connected($self->source)) { 
      $self->source=component_connector($self->source);
   }    
   $self->coord=[0..$n-1];
   $self->client_object=interactive_spring_embedder($self->source, $self->options);
   $window->client_port=$self->client_object->port;
}

# copy a graph and add a fully connected vertex to it
sub component_connector {
   my ($self)=@_;
   $self=new props::Graph($self);
   $self->squeeze;
   $self->add_node;
   my $n=$self->nodes-1;
      for (my $v=0; $v<$n; ++$v) {
         $self->add_edge($v,$n);
      }
   return ($self);
}

sub cols { 3 }

###############################################################################

package Visual::HDEmbedding;

use Polymake::Struct (
   [ '@ISA' => 'DynamicCoords' ],
   '@label_width',
);

sub compute {
   my ($self)=@_;
   hd_embedder($self->source, $self->label_width, $self->options);
}

sub cols { 2 }

###############################################################################

package application;

# Graph object, { optional parameters } => Visual::GraphEmbedding
function spring_embedding_3d {
    my $g_undir=new Graph<Undirected>(ADJACENCY=>$_[0]);
    $_[0]=$g_undir->ADJACENCY;
    new Visual::GraphEmbedding(@_);
}

# FaceLattice object, { optional parameters } => Visual::HDEmbedding
function hd_embedding {
   new Visual::HDEmbedding(@_);
}

function enforce_static(Visual::Graph) {
   enforce_static_coord($_[0], "Vertices");
}

###############################################################################

object Graph {

# Visualizes the graph.
# Decorations may include ''Coord'', disabling the default spring embedder.
# @options %Visual::Graph::decorations
# @option Int seed random seed value for the spring embedder
# @return Visual::Graph

user_method VISUAL(%Visual::Graph::decorations, { seed => undef }) : ADJACENCY {
   my ($this, $decor, $seed)=@_;
   my $coord=delete $decor->{Coord};
   if (defined $coord) {
      if (instanceof Matrix($coord)) {
         $coord=convert_to<Float>($coord);
      } else {
         $coord=new Matrix<Float>($coord);
      }
   } else {
      $coord=spring_embedding_3d($this->ADJACENCY->has_gaps ? renumber_nodes($this->ADJACENCY) : $this->ADJACENCY, $seed);
   }
   visualize( new Visual::Graph( Name => $this->name,
                                 Graph => $this,
                                 NodeLabels => $this->lookup("NODE_LABELS"),
                                 Coord => $coord,
                                 $decor,
                               ));
}

}

###############################################################################

object FaceLattice {

# Visualize the FaceLattice.
# @options %Visual::Lattice::decorations
# @option Int seed random seed value for the node placement
# @return Visual::Lattice

user_method VISUAL(%Visual::Lattice::decorations, { seed => undef }) {
   my ($this, $decor, $seed)=@_;
   my $top_node=$this->top_node;
   my $VL=new Visual::Lattice( Name => $this->name,
                               Graph => $this,
                               Faces => $this->FACES,
                               top_node => $top_node,
                               ArrowStyle => 0,
                               NodeBorderColor => "0 0 0",
                               Coord => hd_embedding($this,$seed),
                               $decor
                             );
   my $black_top_node=sub { $_[0]==$top_node ? "0 0 0" : undef };
   $VL->merge(NodeColor => $black_top_node);
   if (exists $decor->{NodeBorderColor}) { $VL->merge(NodeBorderColor => $black_top_node); }
   visualize($VL);
}

# Visualize the dual FaceLattice.
# @options %Visual::Lattice::decorations
# @option Int seed random seed value for the node placement
# @return Visual::Lattice
user_method VISUAL_DUAL(%Visual::Lattice::decorations, { seed => undef }) {
   my ($this, $decor, $seed)=@_;
   my $top_node=$this->top_node;
   my $VL=new Visual::Lattice( Name => "Dual to ".$this->name,
                               Graph => $this,
                               Faces => $this->dual_faces,
                               Mode => "dual",
                               top_node => $top_node,
                               ArrowStyle => 0,
                               NodeBorderColor => "0 0 0",
                               Coord => hd_embedding($this,$seed),
                               $decor
                             );
   my $black_top_node=sub { $_[0]==$top_node ? "0 0 0" : undef };
   $VL->merge(NodeColor => $black_top_node);
   if (exists $decor->{NodeBorderColor}) { $VL->merge(NodeBorderColor => $black_top_node); }
   visualize($VL);
}

}

# @category Visualization
# Write a graph in LEDA input format.
# @param props::Graph G
user_function LEDA_graph(props::Graph $) {
   my ($G, $out)=@_;
   my $n=$G->nodes;
   print $out <<".", ("|{}|\n")x$n, $G->edges, "\n";
LEDA.GRAPH


$n
.
   for (my $e=entire(edges($G)); $e; ++$e) {
      print $out $e->from_node, " ", $e->to_node, " 0 |{}|\n";
   }
}

# Local Variables:
# mode: perl
# cperl-indent-level:3
# indent-tabs-mode:nil
# End: