File: schlegel.rules

package info (click to toggle)
polymake 4.14-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 35,888 kB
  • sloc: cpp: 168,933; perl: 43,407; javascript: 31,575; ansic: 3,007; java: 2,654; python: 632; sh: 268; xml: 117; makefile: 61
file content (531 lines) | stat: -rw-r--r-- 19,964 bytes parent folder | download | duplicates (2)
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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
#  Copyright (c) 1997-2024
#  Ewgenij Gawrilow, Michael Joswig, and the polymake team
#  Technische Universität Berlin, Germany
#  https://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.
#-------------------------------------------------------------------------------


# A Schlegel diagram of a polytope.
# @tparam Scalar default [[Rational]]
# @relates objects/Polytope
declare object SchlegelDiagram<Scalar=Rational> {

# The facet number in the original polytope, giving the projection plane.
property FACET : Int = 0;

# Zoom factor.
property ZOOM : Scalar = 9/10;

# The intersection point of the projection facet and the view ray.
property FACET_POINT : Vector<Scalar>;

# The center point of the projection, lying outside the polytope.
property VIEWPOINT : Vector<Scalar>;

# A point on the view ray lying inside the polytope.
property INNER_POINT : Vector<Scalar>;

# Matrix of a projective transformation mapping the whole polytope into the [[FACET]]
# The points belonging to this facet stay fixed.

property TRANSFORM : Matrix<Scalar>;


# Coordinates in affine 3-space of the vertices which
# correspond to a 3-dimensional (Schlegel-) projection of a 4-polytope.

property VERTICES : Matrix<Float>;


# Rotation matrix making the projection facet coinciding with (0 0 0 -1)
# We want a negatively oriented coordinate system
# since the view point lies on the negative side of the facet.

property ROTATION : Matrix<Float>;


# Default point on the projection facet is its barycenter
rule FACET_POINT : FACET {
   $this->FACET_POINT=facet($this->parent, $this->FACET)->VERTEX_BARYCENTER;
}

method transform_points {
   my ($this, $points)=@_;
   convert_to<Float>(dehomogenize($points * $this->TRANSFORM));
}

method transform_and_rotate_points {
   my ($this, $points)=@_;
   &transform_points * $this->ROTATION;
}

}

object Polytope {

# @category Visualization
# Holds one special projection (the Schlegel diagram) of the polytope.
property SCHLEGEL_DIAGRAM : SchlegelDiagram<Scalar> : multiple;

rule initial : SCHLEGEL_DIAGRAM.FACET, N_FACETS {
   if ($this->SCHLEGEL_DIAGRAM->FACET < 0 || $this->SCHLEGEL_DIAGRAM->FACET >= $this->N_FACETS) {
      die "Schlegel Diagram FACET index out of valid range 0..", $this->N_FACETS-1, "\n";
   }
}

rule SCHLEGEL_DIAGRAM(any).INNER_POINT = VERTEX_BARYCENTER;

rule SCHLEGEL_DIAGRAM.TRANSFORM, SCHLEGEL_DIAGRAM.VIEWPOINT : VERTICES, FACETS, DUAL_GRAPH.ADJACENCY, \
     SCHLEGEL_DIAGRAM.FACET, SCHLEGEL_DIAGRAM.ZOOM, SCHLEGEL_DIAGRAM.FACET_POINT, SCHLEGEL_DIAGRAM.INNER_POINT {
   schlegel_transform($this->SCHLEGEL_DIAGRAM, $this);
}
precondition : BOUNDED;

rule SCHLEGEL_DIAGRAM.ROTATION : FACETS, SCHLEGEL_DIAGRAM.FACET {
   $this->SCHLEGEL_DIAGRAM->ROTATION(temporary)=rotate_hyperplane($this->FACETS->[$this->SCHLEGEL_DIAGRAM->FACET], -1);
}

rule SCHLEGEL_DIAGRAM.VERTICES : VERTICES, SCHLEGEL_DIAGRAM.TRANSFORM, SCHLEGEL_DIAGRAM.ROTATION {
   $this->SCHLEGEL_DIAGRAM->VERTICES(temporary)=$this->SCHLEGEL_DIAGRAM->transform_and_rotate_points($this->VERTICES);
}

}

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

# @topic objects/Visual::SchlegelDiagram
# @category Visualization
# Visualization of the Schlegel diagram of a polytope.
# @relates SchlegelDiagram

package Visual::SchlegelDiagram;
use Polymake::Struct (
   [ '@ISA' => 'Container' ],
   [ '$SchlegelObject' => '#%' ],
);

# where to store the view transformation matrix etc.
method representative { $_[0]->SchlegelObject }

# the diagram itself as an embedded graph
method basis_graph { $_[0]->elements->[0] }

# Color of the Schlegel projection facet (RGB)
custom $proj_facet="0 0 0";

# Color of the facets on the Schlegel projection (RGB)
custom $solid_color="0 0 255";

##########################################################################################
#
# LP-related supplements

# Illustrate the behavior of a linear objective function on the polytope.
# Superpose the drawing with the directed graph induced by the objective function.
# @param LinearProgram lp a LinearProgram object attached to the polytope.
# @return Visual::SchlegelDiagram

user_method DIRECTED_GRAPH(; LinearProgram) {
   my ($self, $lp)=@_;
   if (defined $lp) {
      if ($lp->parent != $self->SchlegelObject->parent) {
         croak("LinearProgram object does not belong to this Polytope");
      }
   } else {
      $lp=$self->SchlegelObject->parent->LP;
   }

   $self->Name.=" DIRECTED with LP " . $lp->name;
   my $bg = $self->basis_graph;
   $bg->Graph = $lp->DIRECTED_GRAPH;
   $bg->ArrowStyle=1;

   # draw the edges orthogonal to the LP gradient as thin lines without arrows
   my $orthG = ($self->SchlegelObject->parent->GRAPH->ADJACENCY - $bg->Graph->ADJACENCY);
   if ($orthG->nodes) {
      my $eth= defined($bg->EdgeThickness)
               ? ref($bg->EdgeThickness)
                 ? sub { $bg->EdgeThickness->(@_)/3 }
                 : $bg->EdgeThickness/3
               : 1.0/3.0;
      my $graph = new graph::Graph(ADJACENCY => $orthG);
      push @{$self->elements},
           new Visual::Graph( Name => $self->SchlegelObject->parent->name." - edges orthogonal to LP gradient",
                              Graph => $graph,
                              Coord => $bg->Coord,
                              NodeStyle => "hidden",
                              EdgeColor => $bg->EdgeColor,
                              EdgeThickness => $eth,
                            );
   }

   visualize($self);
}


# Illustrate the behavior of a linear objective function on the polytope.
# Color the vertices according to the values of the objective function.
# @param LinearProgram lp a LinearProgram object attached to the polytope.
# @option [complete color] Color min minimal vertex color (default: yellow)
# @option [complete color] Color max maximal vertex color (default: red)
# @return Visual::SchlegelDiagram

user_method VERTEX_COLORS(; LinearProgram { min => $Visual::Color::min, max => $Visual::Color::max }) {
   my ($self, $lp, $decor)=@_;
   if (defined $lp) {
      if ($lp->parent != $self->SchlegelObject->parent) {
         croak("LinearProgram object does not belong to this Polytope");
      }
   } else {
      $lp=$self->SchlegelObject->parent->LP;
   }
   $_=get_RGB($_) for values %$decor;
   $self->basis_graph->NodeColor=vertex_colors($self->SchlegelObject->parent, $lp, $decor);
   visualize($self);
}

# Illustrate the behavior of a linear objective function on the polytope.
# The vertices belonging to [[MINIMAL_FACE]] and [[MAXIMAL_FACE]] are drawn in distinct colors
# @param LinearProgram lp a LinearProgram object attached to the polytope.
# @option [complete color] Color min minimal face decoration (default: yellow vertices and/or facets)
# @option [complete color] Color max maximal face decoration (default: red vertices and/or facets)
# @return Visual::SchlegelDiagram

user_method MIN_MAX_FACE(; LinearProgram { min => $Visual::Color::min, max => $Visual::Color::max }) {
   my ($self, $lp, $decor)=@_;
   if (defined $lp) {
      if ($lp->parent != $self->SchlegelObject->parent) {
         croak("LinearProgram object does not belong to this Polytope");
      }
   } else {
      $lp=$self->SchlegelObject->parent->LP;
   }
   foreach (values %$decor) {
      unless (is_like_hash($_)) {
         my $c = get_RGB($_);
         $_ = { NodeColor => $c };
      }
   }
   $self->basis_graph->add_node_subset($lp->MINIMAL_FACE, $decor->{min});
   $self->basis_graph->add_node_subset($lp->MAXIMAL_FACE, $decor->{max});
   visualize($self);
}

##########################################################################################
#
#  Triangulations and other complex-like drawings

# Draw the edges of the [[Visual::Polytope::TRIANGULATION_BOUNDARY|TRIANGULATION_BOUNDARY]]
# @options %Visual::Graph::decorations
# @return Visual::SchlegelDiagram

user_method TRIANGULATION_BOUNDARY(%Visual::Graph::decorations) {
   my ($self, $decor)=@_;
   my $P=$self->SchlegelObject->parent;
   my $G=$self->basis_graph;
   $self->Name="Triangulation Boundary of ".$P->name;
   my $th=delete $decor->{EdgeThickness} || 0.5;
   my $eth= defined($G->EdgeThickness)
               ? ref($G->EdgeThickness)
                 ? sub { $G->EdgeThickness->(@_)*$th }
                 : $G->EdgeThickness*$th
               : $th;
   my $adj = new GraphAdjacency($P->TRIANGULATION->BOUNDARY->GRAPH->ADJACENCY - $P->GRAPH->ADJACENCY);
   my $graph = new graph::Graph(ADJACENCY => $adj);
   push @{$self->elements},
        new Visual::Graph(
            Name => $P->name."-inner-edges",
            Graph => $graph,
            Coord => $G->Coord,
            NodeStyle => "hidden",
            EdgeThickness => $eth,
            $decor
        );
   visualize($self);
}

# Draw the boundary simplices of the triangulation as solid tetrahedra.
# @options %Visual::Polygons::decorations
# @return Visual::SchlegelDiagram

user_method TRIANGULATION_BOUNDARY_SOLID(%Visual::Polygons::decorations) {
   my ($self, $decor)=@_;
   my $G=$self->basis_graph;
   $G->VertexStyle = "hidden";
   my $P=$self->SchlegelObject->parent;
   my $Signs=triang_sign($P->TRIANGULATION->BOUNDARY->FACETS, $P->TRIANGULATION->BOUNDARY->FACET_TRIANGULATIONS, $P->VERTICES, $P->REL_INT_POINT);
   my $vertex_labels= delete $decor->{VertexLabels};
   if ($vertex_labels ne "hidden") {
      $vertex_labels=$G->VertexLabels;
   } else {
      undef $vertex_labels;
   }
   $decor->{FacetColor} ||= $solid_color;

   $self->Name="Triangulation Boundary of ".$P->name;
   my $i=-1;
   push @{$self->elements},
        [ map { ++$i; map { substr($_->Name,0,0)="Facet $i of "; $_ }             
          $P->VisualSimplices([map $P->TRIANGULATION->BOUNDARY->FACETS->[$_], @$_ ],$G->Coord,$Signs->[$i],$vertex_labels,$decor) }
              @{$P->TRIANGULATION->BOUNDARY->FACET_TRIANGULATIONS} ];
   visualize($self);
}

# Draw the facets of the Schlegel diagram as polytopes.
# @options %Visual::Polygons::decorations
# @return Visual::SchlegelDiagram

user_method SOLID(%Visual::Polygons::decorations) {
   my ($self, $decor)=@_;
   my $G=$self->basis_graph;
   $G->VertexStyle = "hidden";
   my $P=$self->SchlegelObject->parent;
   my @facets;
   for (my ($i,$end)=(0,$P->N_FACETS); $i<$end; ++$i) {
      next if $i==$self->SchlegelObject->FACET;
      my $F=facet($P,$i);
      push @facets,
           new Visual::Polygons( Name => "Facet #$i of ".$P->name,
                                 Vertices => $G->Coord->minor($P->VERTICES_IN_FACETS->[$i],All),
                                 VertexLabels => $F->VERTEX_LABELS,
                                 Facets => $F->VIF_CYCLIC_NORMAL,
                                 FacetNeighbors => $F->NEIGHBOR_FACETS_CYCLIC_NORMAL,
                                 FacetColor => $solid_color,
                                 Closed => 1,
                                 NEdges => $F->N_EDGES,
                                 $decor,
                               );
   }
   push @{$self->elements}, \@facets;
   visualize($self);
}

# Visualize the construction of a 3D Schlegel diagram, that is, the Viewpoint, the 3-polytope and
# the projection onto one facet.
# @options %Visual::Polygons::decorations
# @return Visual::SchlegelDiagram

user_method CONSTRUCTION(%Visual::Polygons::decorations) {
   my ($self, $decor)=@_;
   my $P=$self->SchlegelObject->parent;
   die "polytope must be 3-dimensional.\n" if $P->CONE_DIM-1 != 3;

   $self->basis_graph->Coord=$self->SchlegelObject->transform_points($P->VERTICES);
   $self->basis_graph->EdgeThickness=1.5;
   $self->basis_graph->Name="Schlegel diagram of ".$P->name;
   my $VP = $P->VISUAL( EdgeThickness => 1, FacetStyle => "hidden", VertexStyle => "hidden" )->elements->[0];
   my $Cgraph = new GraphAdjacency($P->N_VERTICES+1);
   $Cgraph->edge(0,$_) for 1 .. $P->N_VERTICES;
   my $G = new graph::Graph(ADJACENCY => $Cgraph);
   my $Vpoint=dehomogenize(convert_to<Float>($self->SchlegelObject->VIEWPOINT));
   my $CG=new Visual::Graph( Name =>  "Projection Rays of ". $P->name,
                             Graph => $G,
                             Coord => $Vpoint / $VP->Vertices,
                             NodeStyle => "hidden",
                             EdgeColor => "0 0 0",
                             EdgeThickness => 0.25,
                           );
   my $VV=new Visual::PointSet( Name => "Viewpoint of Schlegel diagram of ".$P->name,
                                Points => new Matrix<Float>($Vpoint),
                                PointThickness => 2,
                                PointColor => "0 0 255",
                                PointLabels => ["ViewPoint"],
                              );
   compose($self, $VP, $CG, $VV);
}

#################################################################################
package Visual::SchlegelTransform;
use Polymake::Struct (
   [ '@ISA' => 'DynamicCoords' ],
   [ new => '$$' ],
   [ '$points' => '#2' ],
   [ '$polytope' => '#1 ->parent' ],
   [ '$basis_graph' => '#2 -> rows' ],
);

# perform static computations
sub compute {
   my ($self)=@_;
   $self->source->transform_and_rotate_points($self->points);
}

# run interactive calculator
sub run {
   my ($self, $window)=@_;
   $self->coord=[ 0..$self->basis_graph-1 ];
   $self->client_object=schlegel_interactive($self->source, $self->points);
   $window->client_port=$self->client_object->port;
}

sub feedback {
   my ($self, $cmd, $pipe, $viewer, $window)=@_;
   if ($cmd =~ s/^f\s+//) {
      my $vertices=new Set([ $cmd =~ /\d+/g ]);
      my ($f, @other_facets)=@{common_rows($self->polytope->VERTICES_IN_FACETS, $vertices)};
      if (@other_facets) {
         print $pipe "e ", $window->id, " Selected vertices belong to more than one facet\n";
      } elsif (!defined($f)) {
         print $pipe "e ", $window->id, " Selected vertices do not belong to a single facet\n";
      } elsif ($viewer->find_window(sub { grep { instanceof SchlegelTransform($_) and $_->source->FACET==$f } @{$_[0]->feedback_listener} })) {
         print $pipe "e ", $window->id, " Schlegel projection on the facet $f is already being displayed\n";
      } else {
         $viewer->visualize($self->polytope->SCHLEGEL_DIAGRAM(FACET=>$f,temporary)->VISUAL);
      }
      1
   } elsif ($cmd eq "S\n") {
      $self->source=$self->polytope->SCHLEGEL_DIAGRAM(FACET => $self->source->FACET)=$self->client_object->store;
      foreach (@{$window->feedback_listener}) {
         if (instanceof Visual::Transformation($_)) {
            $_->new_representative($self->source);
            last;
         }
      }
      1
   } else {
      0
   }
}

method cols { $_[0]->points->cols-2 }

method minor($ all_rows_or_cols) { pop; new Minor(@_) }

method add_points(Visual::SchlegelTransform) {
   my ($self, $other)=@_;
   return if $other==$self;
   if ($other->source != $self->source) {
      croak( "Can't mix two different Schlegel projections in one drawing" );
   }
   my $offset=$self->points->rows;
   my @indices=$offset..$offset+$other->basis_graph-1;
   $self->points=$self->points / $other->points;
   bless $other, "Visual::SchlegelTransform::Minor";
   $other->source=$self;
   $other->points=\@indices;
}

method add_points(Visual::SchlegelTransform::Minor) {
   my ($self, $other)=@_;
   my $other_source=$other->source;
   return if $other_source==$self;
   if ($other_source->source != $self) {
      croak( "Can't mix two different Schlegel projections in one drawing" );
   }
   my $offset=$other_source->points->[0];
   $other->source=$self;
   $other->points=[ map { $offset+$_ } @{$other->points} ];
}

package Visual::SchlegelTransform::Minor;
use Polymake::Struct (
   [ '@ISA' => 'SchlegelTransform' ],
   [ '$polytope' => '#1 ->polytope' ],
   [ '$basis_graph' => 'undef' ],
);

sub compute {
   my ($self)=@_;
   if (ref($self->source->coord) eq "ARRAY") {
      # dynamic context: pure indices needed
      $self->points
   } else {
      my $dummy=@{$self->source};               # enforce computation
      $self->source->coord->minor($self->points,All);
   }
}

method cols { $_[0]->source->cols }

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

object SchlegelDiagram {

# Draw the Schlegel diagram.
# @option Visual::Graph::decorations proj_facet decorations for the edges of the projection face
# @options %Visual::Graph::decorations
# @return Visual::SchlegelDiagram

user_method VISUAL({ proj_facet => undef }, %Visual::Graph::decorations) {
   my ($this, $proj_facet_decor, $decor)=@_;
   if (!$this->parent->BOUNDED) {
      die "polytope ", $this->parent->name, " is not BOUNDED and therefore cannot be visualized\n";
   }

   my $basis_graph=new Visual::Graph( Name => $this->parent->name,
                                      Graph => $this->parent->GRAPH,
                                      VertexLabels => $this->parent->lookup("VERTEX_LABELS"),
                                      Coord => (new Visual::SchlegelTransform($this, $this->parent->VERTICES)),
                                      EdgeColor => $Visual::Color::facets,
                                      $decor );

   my $proj_facet=$this->parent->VERTICES_IN_FACETS->[$this->FACET];
   $proj_facet_decor= $proj_facet_decor->{proj_facet} || { EdgeColor => $Visual::SchlegelDiagram::proj_facet };
   while (my ($decor_key, $value)=each %$proj_facet_decor) {
      if ($decor_key =~ /^Edge/) {
         $basis_graph->merge($decor_key, sub {
                                my $e=shift;
                                $e && exists $proj_facet->{$e->from_node} && exists $proj_facet->{$e->to_node} ? $value : undef;
                             });
      } else {
         die "only edge attributes (EdgeColor, EdgeThickness, EdgeStyle) can be specified for the projection face\n";
      }
   }

   visualize( new Visual::SchlegelDiagram( Name => "Schlegel diagram of " . $this->parent->name . " on facet " . $this->FACET,
                                           SchlegelObject => $this,
                                           $basis_graph ));
}

}
#################################################################################

object Polytope {

# @category Visualization
# Initial properties of the Schlegel diagram to be displayed.
options %schlegel_init=(
  # Int  index of the projection facet, see [[Visual::SchlegelDiagram::FACET]]
  FACET => undef,
  # Rational  zoom factor, see [[Visual::SchlegelDiagram::ZOOM]]
  ZOOM => undef,
  # Vector  see [[Visual::SchlegelDiagram::FACET_POINT]]
  FACET_POINT => undef,
  # Vector  see [[Visual::SchlegelDiagram::INNER_POINT]]
  INNER_POINT => undef,
);

# @category Visualization
# Create a Schlegel diagram and draw it.
# @options %schlegel_init
# @option Visual::Graph::decorations proj_facet decorations for the edges of the projection face 
# @options %Visual::Wire::decorations
# @return Visual::SchlegelDiagram

user_method SCHLEGEL(%schlegel_init,  { proj_facet => undef },  %Visual::Wire::decorations) : VERTICES, VERTICES_IN_FACETS, N_FACETS, GRAPH {
   my ($this, $schlegel_init, $proj_facet_decor, $decor)=@_;
   if (!defined(wantarray) && !keys %$schlegel_init and my @SD=@{$this->lookup("SCHLEGEL_DIAGRAM")}) {
      $_->VISUAL($proj_facet_decor, $decor) for @SD;
   } else {
      $this->SCHLEGEL_DIAGRAM($schlegel_init, temporary)->VISUAL($proj_facet_decor, $decor);
   }
}

}


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