polylib 5.22.8
testlib.c
Go to the documentation of this file.
1/* main.c
2 COPYRIGHT
3 Both this software and its documentation are
4
5 Copyright 1993 by IRISA /Universite de Rennes I - France,
6 Copyright 1995,1996 by BYU
7 all rights reserved.
8
9
10 This file along with polyhedron.c and vector.c do the following functions:
11 - Extraction of a minimal set of constraints from some set of constraints
12 - Intersection of two convexes
13 - Application of a linear function to some convex
14 - Verification that a convex is included in some other convex
15
16 They are compiled together into an executable file called "test".
17 The file test.in contains sample input data for the program.
18 The file test.out contains the output produced by the program.
19
20 This directory also contains a makefile to build and run the test.
21
22 This file is a tutorial on how to use the library. The comments
23 explain whats going on. You can use this file as a pattern for your
24 own program. Good Luck !
25
26 --Doran
27*/
28
29#include <stdio.h>
30
31#include <polylib/polylib.h>
32
33
34int main() {
35
36 Matrix *a, *b, *t;
37 Polyhedron *A, *B, *C, *D;
38
39 printf("Polyhedral Library Test\n\n");
40
41 /* read in a matrix containing your equations */
42 /* for example, run this program and type in these five lines:
43 4 4
44 1 0 1 -1
45 1 -1 0 6
46 1 0 -1 7
47 1 1 0 -2
48 This is a matrix for the following inequalities
49 1 = inequality, 0x + 1y -1 >=0 --> y >= 1
50 1 = inequality, -1x + 0y +6 >=0 --> x <= 6
51 1 = inequality, 0x + -1y +7 >=0 --> y <= 7
52 1 = inequality, 1x + 0y -2 >=0 --> x >= 2
53 If the first number is a 0 instead of a 1, then that constraint
54 is an 'equality' instead of an 'inequality'.
55 */
56 a = Matrix_Read();
57
58 /* read in a second matrix containing a second set of constraints:
59 for example :
60 4 4
61 1 1 0 -1
62 1 -1 0 3
63 1 0 -1 5
64 1 0 1 -2
65 */
66 b = Matrix_Read();
67
68 /* Convert the constraints to a Polyhedron.
69 This operation 1. Computes the dual ray/vertice form of the
70 system, and 2. Eliminates redundant constraints and reduces
71 them to a minimal form.
72 */
73 A = Constraints2Polyhedron(a, 200);
74 B = Constraints2Polyhedron(b, 200);
75
76 /* the 200 is the size of the working space (in terms of number
77 of rays) that is allocated temporarily
78 -- you can enlarge or reduce it as needed */
79
80 /* There is likewise a rays to polyhedron procedure */
81
82 /* Since you are done with the matrices a and b, be a good citizen
83 and clean up your garbage */
84 Matrix_Free(a);
85 Matrix_Free(b);
86
87 /* If you want the the reduced set of equations back, you can
88 either learn to read the polyhedron structure (not hard,
89 look in "types.h"...
90 or you can get the matrix back in the same format it started
91 in... */
94
95 /* Take a look at them if you want */
96 printf("\na =");
97 Matrix_Print(stdout,P_VALUE_FMT,a);
98 printf("\nb =");
99 Matrix_Print(stdout,P_VALUE_FMT,b);
100
101 Matrix_Free(a);
102 Matrix_Free(b);
103
104 /* To intersect the two systems, use the polyhedron formats... */
105 C = DomainIntersection(A, B, 200);
106
107 /* Again, the 200 is the size of the working space */
108
109 /* This time, lets look a the polyhedron itself... */
110 printf("\nC = A and B =");
112
113 /*
114 * The operations DomainUnion, DomainDifference, and DomainConvex
115 * are also available
116 */
117
118 /*
119 * Read in a third matrix containing a transformation matrix,
120 * this one swaps the indices (x,y --> y,x):
121 * 3 3
122 * 0 1 0
123 * 1 0 0
124 * 0 0 1
125 */
126
127
128 t = Matrix_Read();
129
130 /* Take the preimage (transform the equations) of the domain C to
131 get D. Are you catching on to this 200 thing yet ??? */
132
133 D = Polyhedron_Preimage(C, t, 200);
134
135 /* cleanup please */
136 Matrix_Free(t);
137
138 printf("\nD = transformed C =");
140 Domain_Free(D);
141
142 /* in a similar way, Polyhedron_Image(dom, mat, 200), takes the image
143 of dom under matrix mat (transforms the vertices/rays) */
144
145 /* The function PolyhedronIncludes(Pol1, Pol2) returns 1 if Pol1
146 includes (covers) Pol2, and a 0 otherwise */
147
148 if (PolyhedronIncludes(A,C))
149 printf("\nWe expected A to cover C since C = A intersect B\n");
150 if (!PolyhedronIncludes(C,B))
151 printf("and C does not cover B...\n");
152
153 /* Final note: some functions are defined for Domains, others
154 * for Polyhedrons. A domain is simply a list of polyhedrons.
155 * Every polyhedron structure has a "next" pointer used to
156 * make a list of polyhedrons... For instance, the union of
157 * two disjoint polyhedra is a domain consisting of two polyhedra.
158 * If you want the convex domain... you have to call
159 * DomainConvex(Pol1, 200) explicity.
160 * Note that includes does not work on domains, only on simple
161 * polyhedrons...
162 * Thats the end of the demo... write me if you have questions.
163 * And remember to clean up...
164 */
165
166 Domain_Free(A);
167 Domain_Free(B);
168 Domain_Free(C);
169
170 return 0;
171}
172
173
Matrix * Matrix_Read(void)
Definition: matrix.c:209
void Matrix_Print(FILE *Dst, const char *Format, Matrix *Mat)
Definition: matrix.c:115
void Matrix_Free(Matrix *Mat)
Definition: matrix.c:71
int PolyhedronIncludes(Polyhedron *Pol1, Polyhedron *Pol2)
Definition: polyhedron.c:2404
Polyhedron * Polyhedron_Preimage(Polyhedron *Pol, Matrix *Func, unsigned NbMaxRays)
Definition: polyhedron.c:4113
Polyhedron * DomainIntersection(Polyhedron *Pol1, Polyhedron *Pol2, unsigned NbMaxRays)
Return the intersection of two polyhedral domains 'Pol1' and 'Pol2'.
Definition: polyhedron.c:2637
Matrix * Polyhedron2Constraints(Polyhedron *Pol)
Definition: polyhedron.c:2060
Polyhedron * Constraints2Polyhedron(Matrix *Constraints, unsigned NbMaxRays)
Given a matrix of constraints ('Constraints'), construct and return a polyhedron.
Definition: polyhedron.c:1905
void Polyhedron_Print(FILE *Dst, const char *Format, const Polyhedron *Pol)
Definition: polyhedron.c:1639
void Domain_Free(Polyhedron *Pol)
Definition: polyhedron.c:1626
Definition: types.h:75
int main()
Definition: testlib.c:34
#define P_VALUE_FMT
Definition: types.h:39