File: app_triangulate.cpp

package info (click to toggle)
gfan 0.3dfsg-1
  • links: PTS
  • area: main
  • in suites: lenny, squeeze
  • size: 2,012 kB
  • ctags: 1,935
  • sloc: cpp: 17,728; makefile: 251
file content (294 lines) | stat: -rw-r--r-- 6,912 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
#include "parser.h"
#include "printer.h"
#include "polynomial.h"
#include "division.h"
#include "lp.h"
#include "gfanapplication.h"
#include "polyhedralcone.h"

#include "polymakefile.h"
#include "determinant.h"

class TriangulateApplication : public GFanApplication
{
  FieldOption theFieldOption;
  StringOption inputOption;
  StringOption outputOption;
public:
  bool includeInDefaultInstallation()
  {
    return false;
  }
  const char *helpText()
  {
    return "This program ........\n";
  }
  TriangulateApplication():
    inputOption("-i","Specify the name of the input file.","polymake.out"),
    outputOption("-o","Specify the name of the output file.","polymake.out2")
  {
    registerOptions();
  }    

  char *name()
  {
    return "_triangulate";
  }

  void printIntList(list<int> const &v)
  {
    FILE *f=Stderr;
    fprintf(f,"{");
    for(list<int>::const_iterator i=v.begin();i!=v.end();i++)
      {
	if(i!=v.begin())fprintf(f," ");
	fprintf(f,"%i",*i);
      }
    fprintf(f,"}\n");
  }
  void printIntListList(list<list<int> > const &l)
  {
    for(list<list<int> >::const_iterator i=l.begin();i!=l.end();i++)
      printIntList(*i);
  }

  typedef list<int> Cone;
  void coneSort(Cone &c)
  {
    c.sort();
  }

  IntegerVectorList coneToVectorList(Cone const &c, IntegerMatrix const &rays)
  {
    IntegerVectorList ret;

    for(Cone::const_iterator i=c.begin();i!=c.end();i++)
      ret.push_back(rays[*i]);

    return ret;
  }

  int coneDim(Cone const &c, IntegerMatrix const &rays)
  {
    return rankOfMatrix(coneToVectorList(c,rays));
  }

  Cone firstSimplex(Cone const &c, IntegerMatrix const &rays)
  {
    Cone ret;
    int d=0;

    for(Cone::const_iterator i=c.begin();i!=c.end();i++)
      {
	ret.push_back(*i);
	if(coneDim(ret,rays)!=ret.size())ret.pop_back();
      }
    return ret;
  }

  IntegerVectorList coneComplement(Cone c, IntegerMatrix const &rays)//returns generators of orth. complement.
  {
    IntegerVectorList equations=coneToVectorList(c,rays);
    IntegerVectorList empty;
    return PolyhedralCone(empty,equations,rays.getWidth()).dualCone().getEquations();
  }

  bool isVisible(int v, Cone const &c, IntegerVectorList const &complement, IntegerMatrix const &rays)
  {
    IntegerVectorList l1=coneToVectorList(c,rays);
    l1.push_back(rays[v]);
    for(IntegerVectorList::const_iterator i=complement.begin();i!=complement.end();i++)
      {
	l1.push_back(*i);
      }
    //    AsciiPrinter(Stderr).printVectorList(l1);
    // fprintf(Stderr,"sgn=%i\n",determinantSign(l1));
    return determinantSign(l1)>0;
  }

  list<Cone> triangulateRek(int d, Cone const &c, IntegerMatrix const &rays)
  {
    list<Cone> ret;

    assert(d>1);
    if(d==2)
      {
	Cone c2;
	
	c2.push_back(*c.begin());
	c2.push_back(*(++(c.begin())));
	ret.push_back(c2);


	//fprintf(Stderr,"Base\n");
	//printIntList(c2);
	return ret;
      }
    list<Cone> boundary=triangulateRek(d-1,c,rays);

    Cone l;
    Cone::const_iterator i;
    for(i=c.begin();i!=c.end();i++)
      {
	l.push_back(*i);
	if(coneDim(l,rays)==d)break;
      }
    assert(i!=c.end());
    //    fprintf(Stderr,"A\n");

    IntegerVectorList complement;
    {
      Cone temp=*boundary.begin();
      temp.push_back(*i);
      complement=coneComplement(temp,rays);

      //      AsciiPrinter(Stderr).printVectorList(complement);

    }
    {
      int N=boundary.size();
      for(list<Cone>::const_iterator i=boundary.begin();--N>=0;i++)
	{
	  Cone temp=*i;
	  int a=temp.back();
	  temp.pop_back();
	  int b=temp.back();
	  temp.pop_back();
	  temp.push_back(a);
	  temp.push_back(b);
	  boundary.push_back(temp);
	  //  fprintf(Stderr,"Boundary lifted from lower dimension:\n");
	  //  printIntListList(boundary);
	}
    }


    for(;i!=c.end();i++)
      {
	{//We are done if we leave the subspace we are working in:
	  bool done=false;
	  for(IntegerVectorList::const_iterator j=complement.begin();j!=complement.end();j++)
	    if(dotLong(*j,rays[*i])!=0)
	      {
		done=true;
		break;
	      }
	  if(done)break;
	}

	for(list<Cone>::iterator j=boundary.begin();j!=boundary.end();j++)
	  if(isVisible(*i,*j,complement,rays))
	    {
	      //     fprintf(Stderr,"Found visible\n");
	      Cone b=*j;
	      list<Cone>::iterator tempj=j;
	      j++;
	      boundary.erase(tempj);
	      j--;
	      
	      
	      for(Cone::const_iterator k=b.begin();k!=b.end();k++)
		{
		  Cone temp;
		  for(Cone::const_iterator l=b.begin();l!=b.end();l++)
		    if(l!=k)
		      temp.push_back(*l);
		    else
		      temp.push_back(*i);
		  {
		    Cone temp2=temp;
		    coneSort(temp2);
		    bool found=false;
		    for(list<Cone>::iterator l=boundary.begin();l!=boundary.end();l++)
		      {
			Cone temp3=*l;
			coneSort(temp3);
			//fprintf(Stderr,"comparing:");
			//printIntList(temp3);
			//printIntList(temp2);
			if(temp3==temp2)
			  {
			    //  fprintf(Stderr,"erasing.");

			    boundary.erase(l);
			    found=true;
			    break;
			  }
		      }
		    if(!found)boundary.push_back(temp);


		    //   fprintf(Stderr,"New boundary:\n");
		    // printIntListList(boundary);
		    // fprintf(Stderr,".\n");

		  }
		}
	      b.push_back(*i);
	      ret.push_back(b);
	    }
      }
    return ret;
  }
  list<Cone> triangulate(Cone c, IntegerMatrix const &rays) //computes a lexicographic triangulation
  {
    coneSort(c);
    return triangulateRek(coneDim(c,rays),c,rays);
  } 

  int main()
  {
    LpSolver::printList(Stderr);
    lpSetSolver("cddgmp");

    PolymakeFile inFile;
    fprintf(Stderr,"Test\n");
    inFile.open(inputOption.getValue());
    fprintf(Stderr,"Test\n");


    int n=inFile.readCardinalProperty("AMBIENT_DIM");
    //int d=inFile.readCardinalProperty("DIM");
    int nRays=inFile.readCardinalProperty("N_RAYS");

    fprintf(Stderr,"%i %i\n",n,nRays);

    IntegerMatrix rays=inFile.readMatrixProperty("RAYS",nRays,n);

    vector<list<int> > cones=inFile.readMatrixIncidenceProperty("MAXIMAL_CONES");

    for(vector<list<int> >::const_iterator i=cones.begin();i!=cones.end();i++)
      printIntList(*i);

    AsciiPrinter(Stderr).printVectorList(rays.getRows());

    vector<Cone> simplicialComplex;

    for(vector<list<int> >::const_iterator i=cones.begin();i!=cones.end();i++)
      {
	//	fprintf(Stderr,"Triangulating:\n");
	//	printIntList(*i);
	list<Cone> coneList=triangulate(*i,rays);
	// fprintf(Stderr,"Result:\n");
	for(list<Cone>::const_iterator j=coneList.begin();j!=coneList.end();j++)
	  {
	    // printIntList(*j);
	    simplicialComplex.push_back(*j);
	  }
      }

    PolymakeFile outFile;
    outFile.create(outputOption.getValue(),"topaz","SimplicialComplex");

    outFile.writeCardinalProperty("N_VERTICES",nRays);
    //    outFile.writeCardinalProperty("DIM",d);
    outFile.writeIncidenceMatrixProperty("INPUT_FACES",simplicialComplex);

    outFile.close();

    return 0;
  }
};

static TriangulateApplication theApplication;