File: qhull.rules

package info (click to toggle)
polymake 4.12-3
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 35,992 kB
  • sloc: cpp: 168,768; perl: 43,375; javascript: 31,575; ansic: 3,007; java: 2,654; python: 633; sh: 268; xml: 117; makefile: 61
file content (158 lines) | stat: -rw-r--r-- 4,844 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
#  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.
#-------------------------------------------------------------------------------

CREDIT qhull
  The Quickhull algorithm for convex hulls.
  Copyright by The Geometry Center, University of Minnesota
  http://www.qhull.org/

# path to the qhull executable
custom $qhull;

CONFIGURE {
   find_program($qhull, "qhull");
}

object Polytope<Float> {

# @category Convex hull computation
# Use the [[wiki:external_software#qhull]] program for convex hull computation.
label qhull

sub run_qhull_primal {
   my ($points) = @_;
   my $input = new TempTextFile;
   print $input "begin\n", $points->rows, " ", $points->cols, " real\n", dense($points), "end\n";

   open my $output, "$qhull Fd n i <$input |"
     or die "can't start $qhull: $!\n";

   local $_ = <$output>;	# dimension
   my ($d)= /(\d+)/;
   $_ = <$output>;		# number of facets
   my ($n)= /(\d+)/;
   my $FACETS = new Matrix<Float>($n, $d);  --$d;

   # toggle the signs and move the facet offset in the first position
   for (my $i = 0; $i < $n; ++$i) {
      defined($_ = <$output>) or die "premature end of input\n";
      my $f = new Vector<Float>($_);
      @{$FACETS->[$i]} = (-$f->[$d], @{ -($f->slice(sequence(0,$d))) });
   }

   $_ = <$output>;
   /(\d+)/ && $1 == $n or die "inconsistency in qhull output\n";
   my @VIF; $#VIF = $n-1;
   for (my $i = 0; $i < $n; ++$i) {
      defined($_ = <$output>) or die "premature end of input\n";
      chomp;
      $VIF[$i] = "{$_}";
   }
   ($n, $FACETS, \@VIF);
}

rule qhull.convex_hull.primal, qhull.convex_hull: FACETS, N_FACETS, VERTICES_IN_FACETS : VERTICES {
   my ($n, $FACETS, $VIF)=run_qhull_primal($this->VERTICES);
   $this->FACETS=$FACETS;
   $this->N_FACETS=$n;
   $this->VERTICES_IN_FACETS=$VIF;
}
precondition : BOUNDED;
precondition : FULL_DIM;
weight 4.10;
incurs FacetPerm;

# @notest
rule qhull.convex_hull.primal, qhull.convex_hull: FACETS, N_FACETS, POINTS_IN_FACETS : POINTS {
   my ($n, $FACETS, $VIF) = run_qhull_primal($this->POINTS);
   $this->FACETS = $FACETS;
   $this->N_FACETS = $n;
   $this->POINTS_IN_FACETS = $VIF;
}
precondition : BOUNDED;
precondition : FULL_DIM;
weight 4.10;
incurs FacetPerm;

sub run_qhull_dual {
   my ($rip, $ineqs)=@_;
   my $input = new TempTextFile;
   print $input $rip->dim-1, " 1\n" , dense(dehomogenize($rip)),
                "\nbegin\n", $ineqs->rows, " ", $ineqs->cols, " real\n", dense($ineqs), "end\n";

   #Fp = vertex coordinates 
   #FN = vertices in inequalties/facets
   #Fx = facet defining inequalities
   open my $output, "$qhull Fd H Fp FN <$input |"
     or die "can't start $qhull: $!\n";

   local $_ = <$output>;	# dimension
   my ($d)= /(\d+)/;
   $_ = <$output>;		# number of vertices
   my ($n) = /(\d+)/;
   my $vertices = new Matrix<Float>($n,$d+1);

   # read vertices
   for (my $i = 0; $i < $n; ++$i) {
      defined($_ = <$output>) or die "premature end of input\n";
      my $v = new Vector<Float>($_);
      @{$vertices->[$i]} = (1, @$v);
   }
   
   $_ = <$output>;
   my ($m) = /(\d+)/;
   my @vertices_in_ineqs; 
   $#vertices_in_ineqs = $m - 1;
   for (my $i = 0; $i < $m; ++$i) {
      defined($_ = <$output>) or die "premature end of input\n";
      chomp;
      my @vii = $_ =~ /(\d+)/g;
      splice @vii, 0, 1;
      $vertices_in_ineqs[$i]="{@vii}";
   }
   ($n, $vertices, \@vertices_in_ineqs);
}

rule qhull.convex_hull.dual, qhull.convex_hull: VERTICES, N_VERTICES, VERTICES_IN_FACETS : FACETS, REL_INT_POINT {
   my ($n, $vertices, $vif) = run_qhull_dual($this->REL_INT_POINT,$this->FACETS);
   $this->VERTICES = $vertices;
   $this->N_VERTICES = $n;
   $this->VERTICES_IN_FACETS = $vif;
}
precondition : BOUNDED;
precondition : FULL_DIM;
weight 4.10;
incurs VertexPerm;

# @notest
rule qhull.convex_hull.dual, qhull.convex_hull: VERTICES, N_VERTICES, VERTICES_IN_INEQUALITIES : INEQUALITIES, REL_INT_POINT {
   my ($n, $vertices, $vii) = run_qhull_dual($this->REL_INT_POINT,$this->INEQUALITIES);
   $this->VERTICES = $vertices;
   $this->N_VERTICES = $n;
   $this->VERTICES_IN_INEQUALITIES = $vii;
}
precondition : BOUNDED;
precondition : FULL_DIM;
weight 4.10;
incurs VertexPerm;

}


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