Debian Codegraph @

addons/alpha_3.cpp

raw symbols SCIP
1 #include <phat/boundary_matrix.h>
2
3 #include <CGAL/Exact_predicates_exact_constructions_kernel.h>
4 #include <CGAL/Delaunay_triangulation_3.h>
5 #include <CGAL/Triangulation_hierarchy_3.h>
6
7 #include <CGAL/Triangulation_vertex_base_with_info_3.h>
8 #include <CGAL/Triangulation_cell_base_with_info_3.h>
9
10 #include <fstream>
11 #include <list>
12 #include <cassert>
13 #include <sstream>
14
15 template<typename Index_>
16 struct Vertex_info_3 {
17
18 typedef Index_ Index;
19
20 Vertex_info_3() {
21 index_=boost::none;
22 }
23
24 bool has_index() {
25 return index_;
26 }
27
28 Index index() {
29 CGAL_assertion(has_index());
30 return index_.get();
31 }
32
33 void set_index(Index I) {
34 index_=I;
35 }
36
37 private:
38 boost::optional<Index> index_;
39 };
40
41
42 template<typename Index_, typename FT_>
43 struct Cell_info_3 {
44
45 typedef Index_ Index;
46 typedef FT_ FT;
47
48 Cell_info_3() {
49 for(std::size_t i=0; i<6; i++) {
50 edge_index_[i] = boost::none;
51 }
52 for(std::size_t i=0; i<4; i++) {
53 facet_index_[i] = boost::none;
54 }
55 }
56
57 int edge_conv(int i, int j) {
58 if(i>j) std::swap(i,j);
59 if(i==0 && j==1) return 0;
60 if(i==0 && j==2) return 1;
61 if(i==0 && j==3) return 2;
62 if(i==1 && j==2) return 3;
63 if(i==1 && j==3) return 4;
64 if(i==2 && j==3) return 5;
65 }
66
67
68 bool has_edge_index(int i, int j) {
69 return edge_index_[edge_conv(i,j)];
70 }
71
72 Index edge_index(int i, int j) {
73 CGAL_assertion(has_edge_index(i,j));
74 int k = edge_conv(i,j);
75 return edge_index_[k].get();
76 }
77
78 bool has_facet_index(int i) {
79 CGAL_assertion(i>=0 && i<4);
80 return facet_index_[i];
81 }
82
83 Index facet_index(int i) {
84 CGAL_assertion(has_facet_index(i));
85 return facet_index_[i].get();
86 }
87
88 void set_edge_index(int i, int j, Index I) {
89 edge_index_[edge_conv(i,j)]=I;
90 }
91
92 void set_facet_index(int i, Index I) {
93 facet_index_[i]=I;
94 }
95
96 FT filtration_value;
97 FT filtration_value_of_facet[4];
98
99 private:
100
101
102
103 boost::optional<Index> edge_index_[6];
104 boost::optional<Index> facet_index_[4];
105
106 };
107
108 typedef phat::dimension Dim;
109 typedef std::vector<Dim> Dim_container;
110 typedef phat::index Index;
111 typedef phat::column Column;
112 typedef std::vector<std::vector<Index> > Matrix;
113
114 typedef CGAL::Exact_predicates_exact_constructions_kernel Gt;
115 typedef Gt::FT FT;
116
117 typedef CGAL::Triangulation_cell_base_with_info_3<Cell_info_3<Index,FT>,Gt> Cb;
118 typedef CGAL::Triangulation_vertex_base_with_info_3<Vertex_info_3<Index>,Gt> Vb;
119 //typedef CGAL::Triangulation_hierarchy_vertex_base_3<Vb> Vbh;
120 typedef CGAL::Triangulation_data_structure_3<Vb,Cb> Tds;
121 typedef CGAL::Delaunay_triangulation_3<Gt,Tds> DT_3;
122 typedef Gt::Point_3 Point;
123
124
125 typedef DT_3::Vertex_handle Vertex_handle;
126 typedef DT_3::Edge Edge;
127 typedef DT_3::Facet Facet;
128 typedef DT_3::Cell_handle Cell_handle;
129
130 typedef DT_3::Cell_circulator Cell_circulator;
131 typedef DT_3::Facet_circulator Facet_circulator;
132
133
134 void set_index_of_edge(const DT_3& dt, const Edge& e, Index I) {
135
136 Vertex_handle v1 = e.first->vertex(e.second);
137 Vertex_handle v2 = e.first->vertex(e.third);
138
139 Cell_circulator ch=dt.incident_cells(e);
140 Cell_circulator ch_start=ch;
141 int count=0;
142 do {
143 ch->info().set_edge_index(ch->index(v1),ch->index(v2),I);
144 ch++;
145 count++;
146 } while(ch!=ch_start);
147 //std::cout << "Did " << count << " updates" << std::endl;
148 }
149
150 void set_index_of_facet(const DT_3& dt, const Facet& f, Index I) {
151
152 f.first->info().set_facet_index(f.second,I);
153 Facet mf = dt.mirror_facet(f);
154 mf.first->info().set_facet_index(mf.second,I);
155
156 }
157
158 template<typename Triple>
159 struct Sort_triples {
160
161 bool operator() (const Triple& a, const Triple& b) {
162 if(a.first < b.first) return true;
163 if(a.first > b.first) return false;
164 return a.second < b.second;
165 }
166
167 };
168
169 // Returns whether the facet facet is attached to the cell
170 // that is used to represent facet
171 bool edge_attached_to(const DT_3& dt, const Edge& edge, const Facet& facet) {
172
173 if(dt.is_infinite(facet)) {
174 return false;
175 }
176
177 Vertex_handle v1 = edge.first->vertex(edge.second);
178 Vertex_handle v2 = edge.first->vertex(edge.third);
179
180 Cell_handle cell = facet.first;
181 int i1 = facet.second;
182 int i2 = cell->index(v1);
183 int i3 = cell->index(v2);
184 CGAL_assertion(i1!=i2);
185 CGAL_assertion(i1!=i3);
186 CGAL_assertion(i2!=i3);
187 int j = 0;
188
189 while(j==i1 || j==i2 || j==i3) {
190 j++;
191 }
192 // j is the index of the third point of the facet
193 Vertex_handle w = cell->vertex(j);
194
195 return CGAL::side_of_bounded_sphere(v1->point(),v2->point(),w->point())==CGAL::ON_BOUNDED_SIDE;
196
197 }
198
199
200 // Returns whether the facet facet is attached to the cell
201 // that is used to represent facet
202 bool triangle_attached_to(const DT_3& dt, const Facet& facet) {
203 Cell_handle cell = facet.first;
204 int index = facet.second;
205
206 if(dt.is_infinite(cell)) {
207 return false;
208 }
209
210 Vertex_handle v1 = cell->vertex((index+1)%4);
211 Vertex_handle v2 = cell->vertex((index+2)%4);
212 Vertex_handle v3 = cell->vertex((index+3)%4);
213
214 Vertex_handle w = cell->vertex(facet.second);
215
216 return CGAL::side_of_bounded_sphere(v1->point(),v2->point(),v3->point(),w->point())==CGAL::ON_BOUNDED_SIDE;
217 }
218
219
220
221 int main(int argc, char** argv)
222 {
223
224 if(argc<2) {
225 std::cerr << "Need input file" << std::endl;
226 std::exit(1);
227 }
228
229 std::list<Point> lp;
230 Point p;
231
232 //read input
233 std::ifstream is(argv[1]);
234 std::string next_line;
235 while( getline( is, next_line ) ) {
236 if( next_line != "" && next_line[ 0 ] != '#' ) {
237 //std::cerr << next_line;
238 std::stringstream sstr(next_line);
239 sstr >> p;
240 lp.push_back(p);
241 }
242 }
243
244 std::cerr << "Read " << lp.size() << " points" << std::endl;
245
246 std::cerr << "Compute Delaunay triangulation..." << std::endl;
247 DT_3 dt(lp.begin(),lp.end());
248 // for (std::list<Point>::iterator it = lp.begin(); it != lp.end(); ++it) {
249 // dt.insert(*it);
250 // }/Users/uli/Downloads/neptune-raw.off/782_neptune-raw.pts.txt
251
252 std::cerr << "Compute circumradii..." << std::endl;
253
254 typedef CGAL::Triple<FT,int,CGAL::Object> Triple;
255
256 std::vector<Triple > circumradii;
257
258 for (DT_3::Finite_cells_iterator cit = dt.finite_cells_begin();
259 cit != dt.finite_cells_end(); cit++) {
260 Cell_handle ch(cit);
261 Vertex_handle v1 = cit->vertex(0);
262 Vertex_handle v2 = cit->vertex(1);
263 Vertex_handle v3 = cit->vertex(2);
264 Vertex_handle v4 = cit->vertex(3);
265 FT circumradius = CGAL::squared_radius(v1->point(),v2->point(),v3->point(),v4->point());
266 circumradii.push_back(CGAL::make_triple(circumradius,3,CGAL::make_object(ch)));
267 ch->info().filtration_value = circumradius;
268 }
269
270 for (DT_3::Finite_facets_iterator f = dt.finite_facets_begin();
271 f != dt.finite_facets_end(); f++) {
272
273 // Check Gabrielness (attachment) of the triangle to its incident tetrahedra
274 bool is_attached = false;
275 FT filtration_value;
276
277 {
278 Cell_handle cell = f->first;
279 int index = f->second;
280 if( triangle_attached_to(dt,*f) ) {
281 filtration_value = cell->info().filtration_value;
282 is_attached=true;
283 }
284 }
285
286 {
287 if(! is_attached) {
288 Facet g = dt.mirror_facet(*f);
289 Cell_handle cell = g.first;
290 int index = g.second;
291 if( triangle_attached_to(dt,g) ) {
292 filtration_value = cell->info().filtration_value;
293 is_attached=true;
294 }
295 }
296 }
297 if(! is_attached) {
298 Cell_handle cell = f->first;
299 int index = f->second;
300 filtration_value = CGAL::squared_radius(cell->vertex((index+1)%4)->point(),
301 cell->vertex((index+2)%4)->point(),
302 cell->vertex((index+3)%4)->point());
303 }
304 circumradii.push_back(CGAL::make_triple(filtration_value,2,CGAL::make_object(*f)));
305 // Store filtration values in info object for further use
306 f->first->info().filtration_value_of_facet[f->second] = filtration_value;
307 Facet g = dt.mirror_facet(*f);
308 g.first->info().filtration_value_of_facet[g.second] = filtration_value;
309 }
310
311
312 for (DT_3::Finite_edges_iterator edge = dt.finite_edges_begin();
313 edge != dt.finite_edges_end(); edge++) {
314
315 // Check Gabrielness of the edge
316 bool is_attached = false;
317 FT filtration_value;
318
319 Facet_circulator f_start = dt.incident_facets(*edge);
320 Facet_circulator f_it = f_start;
321 f_it++;
322 while( f_it!=f_start ) {
323 if(edge_attached_to(dt,*edge,*f_it)) {
324 Cell_handle cell = f_it->first;
325 int index = f_it->second;
326 FT new_filtration_value = cell->info().filtration_value_of_facet[index];
327 // Now, maybe the edge is already attached to another triangle (can this really happen?)
328 if(is_attached) {
329 filtration_value = CGAL::min(filtration_value,new_filtration_value);
330 } else {
331 filtration_value = new_filtration_value;
332 }
333 is_attached=true;
334 }
335 f_it++;
336 }
337
338
339 if(! is_attached) {
340 Vertex_handle v1 = edge->first->vertex(edge->second);
341 Vertex_handle v2 = edge->first->vertex(edge->third);
342 filtration_value = CGAL::squared_radius(v1->point(),v2->point());
343 }
344
345 circumradii.push_back(CGAL::make_triple(filtration_value,1,CGAL::make_object(*edge)));
346 }
347
348
349
350
351 for (DT_3::Finite_vertices_iterator vertex = dt.finite_vertices_begin();
352 vertex != dt.finite_vertices_end(); vertex++) {
353 Vertex_handle vh(vertex);
354 circumradii.push_back(CGAL::make_triple(FT(0),0,CGAL::make_object(vh)));
355 }
356
357
358 std::cerr << "Sort circumradii..." << std::endl;
359
360 std::sort(circumradii.begin(),circumradii.end(),Sort_triples<Triple>());
361
362 std::cerr << "Filtration of size " << circumradii.size() << std::endl;
363
364 phat::boundary_matrix< phat::vector_vector > boundary_matrix;
365
366 Vertex_handle v;
367 Edge e;
368 Facet f;
369 Cell_handle c;
370
371 std::size_t filtration_index = 0;
372 std::size_t filtration_size = circumradii.size();
373
374 boundary_matrix.set_num_cols( filtration_size );
375
376 Index curr_index = 0;
377
378 for(std::vector<Triple>::const_iterator it = circumradii.begin();
379 it != circumradii.end(); it++) {
380
381 if(filtration_index % 100000 == 0) {
382 std::cerr << filtration_index << " of " << filtration_size
383 << std::endl;
384 }
385 filtration_index++;
386
387 const CGAL::Object& obj = it->third;
388
389 Column col;
390
391 if(CGAL::assign(v,obj)) {
392 //std::cout << "Vertex " << it->second << std::endl;
393 boundary_matrix.set_dim(curr_index, 0);
394 v->info().set_index(curr_index);
395 boundary_matrix.set_col(curr_index, col);
396 }
397 if(CGAL::assign(e,obj)) {
398 //std::cout << "Edge " << it->second << std::endl;
399 boundary_matrix.set_dim(curr_index, 1);
400 Vertex_handle v1 = e.first->vertex(e.second);
401 CGAL_assertion(v1->info().has_index());
402 Vertex_handle v2 = e.first->vertex(e.third);
403 CGAL_assertion(v2->info().has_index());
404 Index i1 = v1->info().index();
405 Index i2 = v2->info().index();
406 if(i1>i2) {
407 std::swap(v1,v2);
408 std::swap(i1,i2);
409 }
410 col.push_back(i1);
411 col.push_back(i2);
412 boundary_matrix.set_col(curr_index, col);
413 set_index_of_edge(dt,e,curr_index);
414 }
415 if(CGAL::assign(f,obj)) {
416 //std::cout << "Facet " << it->second << std::endl;
417 boundary_matrix.set_dim(curr_index, 2);
418
419 Index i1= f.first->info().edge_index( (f.second+1)%4, (f.second+2)%4 );
420 col.push_back(i1);
421 Index i2= f.first->info().edge_index( (f.second+1)%4, (f.second+3)%4 );
422 col.push_back(i2);
423 Index i3= f.first->info().edge_index( (f.second+2)%4, (f.second+3)%4 );
424 col.push_back(i3);
425 std::sort(col.begin(),col.end());
426 boundary_matrix.set_col(curr_index, col);
427 set_index_of_facet(dt,f,curr_index);
428
429 }
430 if(CGAL::assign(c,obj)) {
431 //std::cout << "Cell " << it->second << std::endl;
432 boundary_matrix.set_dim(curr_index, 3);
433 col.push_back(c->info().facet_index(0));
434 col.push_back(c->info().facet_index(1));
435 col.push_back(c->info().facet_index(2));
436 col.push_back(c->info().facet_index(3));
437 std::sort(col.begin(),col.end());
438 boundary_matrix.set_col(curr_index, col);
439 }
440 curr_index++;
441 }
442
443 boundary_matrix.save_ascii("alpha_filtration.bin");
444
445 //phat::write(std::cout,M,dim_container);
446
447 return 0;
448 }