Path: blob/master/build/pkgs/cddlib/patches/cdd_both_reps.c
8818 views
/* cdd_both_reps.c: compute reduced H and V representation of polytope1by Volker Braun <[email protected]>23The input is taken from stdin and can be either a4H or V representation, not necessarily reduced.56based on testcdd1.c, redcheck.c, and of course the cdd library7written by Komei Fukuda, [email protected]8Standard ftp site: ftp.ifor.math.ethz.ch, Directory: pub/fukuda/cdd9*/1011/* This program is free software; you can redistribute it and/or modify12it under the terms of the GNU General Public License as published by13the Free Software Foundation; either version 2 of the License, or14(at your option) any later version.1516This program is distributed in the hope that it will be useful,17but WITHOUT ANY WARRANTY; without even the implied warranty of18MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the19GNU General Public License for more details.2021You should have received a copy of the GNU General Public License22along with this program; if not, write to the Free Software23Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.24*/2526#include "setoper.h"27#include "cdd.h"28#include <stdio.h>29#include <stdlib.h>30#include <time.h>31#include <math.h>32#include <string.h>333435363738void compute_adjacency(dd_MatrixPtr Rep, dd_ErrorType* err_ptr)39{40dd_SetFamilyPtr AdjacencyGraph;41if (*err_ptr != dd_NoError) return;4243switch (Rep->representation) {44case dd_Inequality:45printf("Facet graph\n");46break;47case dd_Generator:48printf("Vertex graph\n");49break;50case dd_Unspecified:51printf("unknown representation type!\n");52default:53printf("This should be unreachable!\n");54exit(2);55}5657/* Output adjacency of vertices/rays/lines */58if (Rep->rowsize > 0) { /* workaround for bug with empty polyhedron */59/* compute adjacent vertices/rays/lines */60AdjacencyGraph = dd_Matrix2Adjacency(Rep, err_ptr);61if (*err_ptr == dd_NoError) {62dd_WriteSetFamily(stdout,AdjacencyGraph);63dd_FreeSetFamily(AdjacencyGraph);64}65} else {66printf("begin\n");67printf(" 0 0\n");68printf("end\n");69}7071printf("\n");72}737475void minimal_Vrep_Hrep(dd_MatrixPtr M,76dd_MatrixPtr* Vrep_ptr, dd_MatrixPtr* Hrep_ptr,77dd_ErrorType* err_ptr)78{79dd_PolyhedraPtr poly;80dd_rowindex newpos;81dd_rowset impl_linset,redset;82dd_MatrixPtr Vrep, Hrep;8384if (*err_ptr != dd_NoError) return;8586/* compute the second representation */87poly = dd_DDMatrix2Poly(M, err_ptr);88if (*err_ptr != dd_NoError) return;8990if (*err_ptr == dd_NoError) {91/* compute canonical H-representation */92Hrep = dd_CopyInequalities(poly);93if (Hrep->rowsize > 0) { /* workaround for bug with empty matrix */94dd_MatrixCanonicalize(&Hrep, &impl_linset, &redset, &newpos, err_ptr);95if (*err_ptr == dd_NoError) {96set_free(redset);97set_free(impl_linset);98free(newpos);99}100}101if (*err_ptr == dd_NoError) (*Hrep_ptr) = Hrep;102}103104if (*err_ptr == dd_NoError) {105/* compute canonical V-representation */106Vrep = dd_CopyGenerators(poly);107if (Vrep->rowsize > 0) { /* workaround for bug with empty matrix */108dd_MatrixCanonicalize(&Vrep, &impl_linset, &redset, &newpos, err_ptr);109if (*err_ptr == dd_NoError) {110set_free(redset);111set_free(impl_linset);112free(newpos);113}114}115if (*err_ptr == dd_NoError) (*Vrep_ptr) = Vrep;116}117118dd_FreePolyhedra(poly);119}120121122void print_both_reps(dd_MatrixPtr Vrep, dd_MatrixPtr Hrep)123{124/* Output V-representation */125dd_WriteMatrix(stdout,Vrep);126printf("\n");127128/* Output H-representation */129dd_WriteMatrix(stdout,Hrep);130printf("\n");131}132133134void compute_both_reps(dd_MatrixPtr M, dd_ErrorType* err_ptr)135{136dd_MatrixPtr Vrep, Hrep;137minimal_Vrep_Hrep(M, &Vrep, &Hrep, err_ptr);138if (*err_ptr != dd_NoError) return;139140print_both_reps(Vrep, Hrep);141dd_FreeMatrix(Hrep);142dd_FreeMatrix(Vrep);143}144145146void compute_all(dd_MatrixPtr M, dd_ErrorType* err_ptr)147{148dd_MatrixPtr Vrep, Hrep;149minimal_Vrep_Hrep(M, &Vrep, &Hrep, err_ptr);150if (*err_ptr != dd_NoError) return;151152print_both_reps(Vrep, Hrep);153compute_adjacency(Vrep, err_ptr);154compute_adjacency(Hrep, err_ptr);155dd_FreeMatrix(Hrep);156dd_FreeMatrix(Vrep);157}158159160161void usage(char *name)162{163printf("No known option specified, I don't know what to do!\n"164"Usage:\n"165"%s --option\n"166"where --option is precisely one of the following:\n\n"167" --all: Compute everything.\n"168" This will compute minimal H-,V-representation and vertex and facet graph.\n"169"\n"170" --reps: Compute both a minimal H- and minimal V-representation.\n"171"\n"172" --adjacency: Compute adjacency information only.\n"173" The input is assumed to be a minimal representation, as, for example, computed\n"174" by --reps. Warning, you will not get the correct answer if the input\n"175" representation is not minimal! The output is the vertex or facet graph,\n"176" depending on the input.\n"177"\n"178"The input data is a H- or V-representation in cdd's ine/ext format and\n"179"is in each case read from stdin.\n",180name);181}182183184enum command_line_arguments { ALL, REPS, ADJACENCY };185186187int parse_arguments(char* arg, enum command_line_arguments* option)188{189if (strcmp(arg,"--all")==0) {190*option = ALL;191return 0;192}193if (strcmp(arg,"--reps")==0) {194*option = REPS;195return 0;196}197if (strcmp(arg,"--adjacency")==0) {198*option = ADJACENCY;199return 0;200}201printf("Unknown option: %s\n", arg);202return 1;203}204205206int main(int argc, char *argv[])207{208dd_ErrorType err=dd_NoError;209dd_MatrixPtr M;210enum command_line_arguments option;211212if (argc!=2 || parse_arguments(argv[1],&option)) {213usage(argv[0]);214return 0;215}216217dd_set_global_constants();218219/* Read data from stdin */220M = dd_PolyFile2Matrix(stdin, &err);221if (err != dd_NoError) {222printf("I was unable to parse the input data!\n");223dd_WriteErrorMessages(stdout,err);224dd_free_global_constants();225return 1;226}227228switch (option) {229case ALL:230compute_all(M,&err);231break;232case REPS:233compute_both_reps(M,&err);234break;235case ADJACENCY:236compute_adjacency(M,&err);237break;238default:239printf("unreachable option %d\n", option);240exit(3); /* unreachable */241}242243/* cleanup */244dd_FreeMatrix(M);245if (err != dd_NoError) {246dd_WriteErrorMessages(stdout,err);247}248249dd_free_global_constants();250return 0;251}252253254255256257