1 /* cdd_both_reps.c: compute reduced H and V representation of polytope
2 by Volker Braun <vbraun@stp.dias.ie>
4 The input is taken from stdin and can be either a
5 H or V representation, not necessarily reduced.
7 based on testcdd1.c, redcheck.c, and of course the cdd library
8 written by Komei Fukuda, fukuda@ifor.math.ethz.ch
9 Standard ftp site: ftp.ifor.math.ethz.ch, Directory: pub/fukuda/cdd
12 /* This program is free software; you can redistribute it and/or modify
13 it under the terms of the GNU General Public License as published by
14 the Free Software Foundation; either version 2 of the License, or
15 (at your option) any later version.
17 This program is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 GNU General Public License for more details.
22 You should have received a copy of the GNU General Public License
23 along with this program; if not, write to the Free Software
24 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
39 void compute_adjacency(dd_MatrixPtr Rep, dd_ErrorType* err_ptr)
41 dd_SetFamilyPtr AdjacencyGraph;
42 if (*err_ptr != dd_NoError) return;
44 switch (Rep->representation) {
46 printf("Facet graph\n");
49 printf("Vertex graph\n");
52 printf("unknown representation type!\n");
54 printf("This should be unreachable!\n");
58 /* Output adjacency of vertices/rays/lines */
59 if (Rep->rowsize > 0) { /* workaround for bug with empty polyhedron */
60 /* compute adjacent vertices/rays/lines */
61 AdjacencyGraph = dd_Matrix2Adjacency(Rep, err_ptr);
62 if (*err_ptr == dd_NoError) {
63 dd_WriteSetFamily(stdout,AdjacencyGraph);
64 dd_FreeSetFamily(AdjacencyGraph);
76 void minimal_Vrep_Hrep(dd_MatrixPtr M,
77 dd_MatrixPtr* Vrep_ptr, dd_MatrixPtr* Hrep_ptr,
78 dd_ErrorType* err_ptr)
82 dd_rowset impl_linset,redset;
83 dd_MatrixPtr Vrep, Hrep;
85 if (*err_ptr != dd_NoError) return;
87 /* compute the second representation */
88 poly = dd_DDMatrix2Poly(M, err_ptr);
89 if (*err_ptr != dd_NoError) return;
91 if (*err_ptr == dd_NoError) {
92 /* compute canonical H-representation */
93 Hrep = dd_CopyInequalities(poly);
94 if (Hrep->rowsize > 0) { /* workaround for bug with empty matrix */
95 dd_MatrixCanonicalize(&Hrep, &impl_linset, &redset, &newpos, err_ptr);
96 if (*err_ptr == dd_NoError) {
98 set_free(impl_linset);
102 if (*err_ptr == dd_NoError) (*Hrep_ptr) = Hrep;
105 if (*err_ptr == dd_NoError) {
106 /* compute canonical V-representation */
107 Vrep = dd_CopyGenerators(poly);
108 if (Vrep->rowsize > 0) { /* workaround for bug with empty matrix */
109 dd_MatrixCanonicalize(&Vrep, &impl_linset, &redset, &newpos, err_ptr);
110 if (*err_ptr == dd_NoError) {
112 set_free(impl_linset);
116 if (*err_ptr == dd_NoError) (*Vrep_ptr) = Vrep;
119 dd_FreePolyhedra(poly);
123 void print_both_reps(dd_MatrixPtr Vrep, dd_MatrixPtr Hrep)
125 /* Output V-representation */
126 dd_WriteMatrix(stdout,Vrep);
129 /* Output H-representation */
130 dd_WriteMatrix(stdout,Hrep);
135 void compute_both_reps(dd_MatrixPtr M, dd_ErrorType* err_ptr)
137 dd_MatrixPtr Vrep, Hrep;
138 minimal_Vrep_Hrep(M, &Vrep, &Hrep, err_ptr);
139 if (*err_ptr != dd_NoError) return;
141 print_both_reps(Vrep, Hrep);
147 void compute_all(dd_MatrixPtr M, dd_ErrorType* err_ptr)
149 dd_MatrixPtr Vrep, Hrep;
150 minimal_Vrep_Hrep(M, &Vrep, &Hrep, err_ptr);
151 if (*err_ptr != dd_NoError) return;
153 print_both_reps(Vrep, Hrep);
154 compute_adjacency(Vrep, err_ptr);
155 compute_adjacency(Hrep, err_ptr);
162 void usage(char *name)
164 printf("No known option specified, I don't know what to do!\n"
167 "where --option is precisely one of the following:\n\n"
168 " --all: Compute everything.\n"
169 " This will compute minimal H-,V-representation and vertex and facet graph.\n"
171 " --reps: Compute both a minimal H- and minimal V-representation.\n"
173 " --adjacency: Compute adjacency information only.\n"
174 " The input is assumed to be a minimal representation, as, for example, computed\n"
175 " by --reps. Warning, you will not get the correct answer if the input\n"
176 " representation is not minimal! The output is the vertex or facet graph,\n"
177 " depending on the input.\n"
179 "The input data is a H- or V-representation in cdd's ine/ext format and\n"
180 "is in each case read from stdin.\n",
185 enum command_line_arguments { ALL, REPS, ADJACENCY };
188 int parse_arguments(char* arg, enum command_line_arguments* option)
190 if (strcmp(arg,"--all")==0) {
194 if (strcmp(arg,"--reps")==0) {
198 if (strcmp(arg,"--adjacency")==0) {
202 printf("Unknown option: %s\n", arg);
207 int main(int argc, char *argv[])
209 dd_ErrorType err=dd_NoError;
211 enum command_line_arguments option;
213 if (argc!=2 || parse_arguments(argv[1],&option)) {
218 dd_set_global_constants();
220 /* Read data from stdin */
221 M = dd_PolyFile2Matrix(stdin, &err);
222 if (err != dd_NoError) {
223 printf("I was unable to parse the input data!\n");
224 dd_WriteErrorMessages(stdout,err);
225 dd_free_global_constants();
234 compute_both_reps(M,&err);
237 compute_adjacency(M,&err);
240 printf("unreachable option %d\n", option);
241 exit(3); /* unreachable */
246 if (err != dd_NoError) {
247 dd_WriteErrorMessages(stdout,err);
250 dd_free_global_constants();