File: DoublyConnectedEdgeList.h

package info (click to toggle)
polymake 4.14-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 35,888 kB
  • sloc: cpp: 168,933; perl: 43,407; javascript: 31,575; ansic: 3,007; java: 2,654; python: 632; sh: 268; xml: 117; makefile: 61
file content (678 lines) | stat: -rw-r--r-- 19,614 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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
/* Copyright (c) 1997-2024
   Ewgenij Gawrilow, Michael Joswig, and the polymake team
   Technische Universität Berlin, Germany
   https://polymake.org

   This program is free software; you can redistribute it and/or modify it
   under the terms of the GNU General Public License as published by the
   Free Software Foundation; either version 2, or (at your option) any
   later version: http://www.gnu.org/licenses/gpl.txt.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
--------------------------------------------------------------------------------
*/

#pragma once

#include "polymake/client.h"
#include "polymake/Vector.h"
#include "polymake/Array.h"
#include "polymake/Matrix.h"
#include "polymake/Integer.h"
#include "polymake/Rational.h"
#include "polymake/SparseMatrix.h"
#include "polymake/Set.h"
#include "polymake/IncidenceMatrix.h"
#include "polymake/linalg.h"



namespace polymake {
namespace graph {
namespace dcel {
class DoublyConnectedEdgeList;
}

using DoublyConnectedEdgeList = dcel::DoublyConnectedEdgeList;

DoublyConnectedEdgeList conway_ambo_impl(const DoublyConnectedEdgeList& old);
DoublyConnectedEdgeList conway_kis_impl(const DoublyConnectedEdgeList& old);
DoublyConnectedEdgeList conway_snub_impl(const DoublyConnectedEdgeList& old);
DoublyConnectedEdgeList conway_CG_fundtri2(DoublyConnectedEdgeList& old);
DoublyConnectedEdgeList conway_CG_fundtri3(DoublyConnectedEdgeList& old);
namespace dcel {
template<typename Container> class HalfEdgeTemplate;
template<typename Container> class VertexTemplate;
template<typename Container> class FaceTemplate;
using HalfEdge = HalfEdgeTemplate<DoublyConnectedEdgeList>;
using Face = FaceTemplate<DoublyConnectedEdgeList>;
using Vertex = VertexTemplate<DoublyConnectedEdgeList>;

template<typename Container>
class VertexTemplate {
private:
   const Container* container;
   HalfEdgeTemplate<Container>* incidentEdge;

   friend Container;

public:
   VertexTemplate()
      : container(nullptr)
      , incidentEdge(nullptr) {};

   HalfEdgeTemplate<Container>* getIncidentEdge() const
   {
      return incidentEdge;
   }

   void setContainer(const Container* c){
      container = c;
   }

   Int getID() const {
      return container->getVertexId(this);
   }

   void setIncidentEdge(HalfEdgeTemplate<Container>* edge)
   {
      incidentEdge = edge;
   }
};


template<typename Container>
class FaceTemplate {
private:
   const Container* container;
   HalfEdge* half_edge;
   Rational det_coord;

   friend Container;

public:
   FaceTemplate()
      : container(nullptr)
      , half_edge(nullptr) {}

   HalfEdge* getHalfEdge() const
   {
      return half_edge;
   }

   void setContainer(const Container* c){
      container = c;
   }

   Int getID() const {
      return container->getFaceId(this);
   }

   void setHalfEdge(HalfEdge* edge)
   {
      half_edge = edge;
   }

   bool operator==(const FaceTemplate& other) const
   {
      return half_edge == other.half_edge;
   }

   const Rational& getDetCoord() const
   {
      return det_coord;
   }

   void setDetCoord(const Rational& new_det_coord)
   {
      det_coord = new_det_coord;
   }
};

template<typename Container>
class HalfEdgeTemplate {
private:
   const Container* container;
   HalfEdgeTemplate* twin;
   HalfEdgeTemplate* next;
   HalfEdgeTemplate* prev;
   Vertex* head;
   Face* face;
   Rational length;

   friend Container;

public:
   HalfEdgeTemplate()
      : container(nullptr)
      , twin(nullptr)
      , next(nullptr)
      , prev(nullptr)
      , head(nullptr)
      , face(nullptr)
      , length(1) {}

   bool operator==(const HalfEdgeTemplate& other) const
   {
      return twin == other.twin && next == other.next;
   }

   const Rational& getLength() const
   {
      return length;
   }

   void setContainer(const Container* c){
      container = c;
   }

   Int getID() const {
      return container->getHalfEdgeId(this);
   }

   void setLength(const Rational& newLength)
   {
      length = newLength;
   }

   HalfEdgeTemplate* getTwin()
   {
      return twin;
   }

   const HalfEdgeTemplate* getTwin() const
   {
      return twin;
   }

   void setTwin(HalfEdgeTemplate* newTwin)
   {
      twin = newTwin;
      newTwin->twin = this;
   }

   HalfEdgeTemplate* getNext()
   {
      return next;
   }

   const HalfEdgeTemplate* getNext() const
   {
      return next;
   }

   void setNext(HalfEdgeTemplate* newNext)
   {
      next = newNext;
      newNext->prev = this;
   }

   HalfEdgeTemplate* getPrev()
   {
      return prev;
   }

   const HalfEdgeTemplate* getPrev() const
   {
      return prev;
   }

   void setPrev(HalfEdgeTemplate* newPrev)
   {
      prev = newPrev;
      newPrev->next = this;
   }

   Vertex* getHead()
   {
      return head;
   }

   const Vertex* getHead() const
   {
      return head;
   }

   void setHead(Vertex* newHead)
   {
      head = newHead;
      newHead->setIncidentEdge(this);
   }

   void setFace(Face* newFace)
   {
      face = newFace;
      newFace->setHalfEdge(this);
   }

   Face* getFace()
   {
      return face;
   }
   const Face* getFace() const
   {
      return face;
   }
};

class DoublyConnectedEdgeList {
public:

private:
   mutable Matrix<Int> matrix_representation;
   static constexpr Int null_id() { return std::numeric_limits<Int>::max(); }

   using flip_sequence = std::list<Int>;

private:
   Array<Vertex> vertices;
   Array<HalfEdge> halfedges;
   Array<Face> faces;
   bool with_faces;

public:
   DoublyConnectedEdgeList() = default;

   DoublyConnectedEdgeList(const DoublyConnectedEdgeList& list){
      copy_from(list);
   }

   DoublyConnectedEdgeList &operator=(const DoublyConnectedEdgeList& other){
      this->copy_from(other);
      return *this;
   }

   void copy_from(const DoublyConnectedEdgeList& other)
   {
      with_faces = other.with_faces;
      if(with_faces){
         resize(other.vertices.size(), other.halfedges.size(), other.faces.size());
      } else {
         resize(other.vertices.size(), other.halfedges.size());
      }
      for(Int i=0; i<vertices.size(); i++){
         vertices[i].incidentEdge = &halfedges[other.vertices[i].getIncidentEdge()->getID()];
      }
      for(Int i=0; i<halfedges.size(); i++){
         halfedges[i].twin = &halfedges[other.halfedges[i].getTwin()->getID()];
         halfedges[i].next = &halfedges[other.halfedges[i].getNext()->getID()];
         halfedges[i].prev = &halfedges[other.halfedges[i].getPrev()->getID()];
         halfedges[i].head = &vertices[other.halfedges[i].getHead()->getID()];
         halfedges[i].length = other.halfedges[i].getLength();
         if(with_faces)
            halfedges[i].face = &faces[other.halfedges[i].getFace()->getID()];
      }
      if(with_faces){
         for(Int i=0; i<faces.size(); i++){
            faces[i].half_edge = &halfedges[other.faces[i].getHalfEdge()->getID()];
            faces[i].det_coord = other.faces[i].getDetCoord();
         }
      }
   }
   friend DoublyConnectedEdgeList polymake::graph::conway_CG_fundtri2(DoublyConnectedEdgeList& old);
   
   friend DoublyConnectedEdgeList polymake::graph::conway_CG_fundtri3(DoublyConnectedEdgeList& old);

   friend DoublyConnectedEdgeList polymake::graph::conway_ambo_impl(const DoublyConnectedEdgeList& old);

   // DoublyConnectedEdgeList kis(const DoublyConnectedEdgeList& old);
   friend DoublyConnectedEdgeList polymake::graph::conway_kis_impl(const DoublyConnectedEdgeList& old);

   // DoublyConnectedEdgeList snub(const DoublyConnectedEdgeList& old);
   friend DoublyConnectedEdgeList polymake::graph::conway_snub_impl(const DoublyConnectedEdgeList& old);

   DoublyConnectedEdgeList dual() const;

   bool operator==(const DoublyConnectedEdgeList& other) const{
      return other.toMatrixInt() == toMatrixInt();
   }

   /*
   const Array<Vertex>& getVertices() const
   {
      return vertices;
   }

   const Array<HalfEdge>& getHalfEdges() const
   {
      return halfedges;
   }
   */

   const Array<Face>& getFaces() const
   {
      return faces;
   }

   // get the number of vertices corresponding to an DCEL input array
   static Int getNumVert(const Matrix<Int>& half_edge_list);

   // get the number of triangles corresponding to an DCEL input array
   static Int getNumTriangs(const Matrix<Int>& half_edge_list);

   // Construct a DCEL out of a given half edge list.
   // The ith element in dcel_data is [i.head, (i+1).head, i.next, (i+1).next, i.face, (i+1).face].
   // The latter two entries may be omitted if no faces are specified
   explicit DoublyConnectedEdgeList(const Matrix<Int>& half_edge_list);

   DoublyConnectedEdgeList(const Array<Array<Int>>& vif_cyclic_normals);

   DoublyConnectedEdgeList(const Matrix<Int>& half_edge_list, const Vector<Rational>& coords);

   const Matrix<Int>& toMatrixInt() const;

   Array<Array<Int>> faces_as_cycles() const;

private:
   void populate(const Matrix<Int>& half_edge_list);
   void populate();
   void resize();
   void resize(Int nvert, Int nhe);
   void resize(Int nvert, Int nhe, Int nfaces);

   void connect_halfedges(HalfEdge* prev, HalfEdge* next){
      prev->setNext(next);
      next->setPrev(prev);
   }

   friend struct pm::spec_object_traits< pm::Serialized< polymake::graph::dcel::DoublyConnectedEdgeList > >;
   // set the incidences of an edge (which are two half edges) according to the input
   void setEdgeIncidences(Int halfEdgeId, Int headId, Int twinHeadId, Int nextId, Int twinNextId);

   // set face incidences of an edge
   void setFaceIncidences(Int half_edge_id, Int face_id, Int twin_face_id);

   void verifyHalfedge(Int& counter, const std::pair<Int, Int>& key, Map<std::pair<Int, Int>, Int>& existing_edges);

   void insert_container();

public:
   // return the halfedges-vertices incidence matrix
   SparseMatrix<Int> EdgeVertexIncidenceMatrix() const;

   // return true if the edge of index 'edgeId' is flippable, the two half edges have id 'edgeId' and 'edgeId'+1
   bool isFlippable(Int edgeId) const;

   // flip half edge and its twin ccw
   void flipHalfEdge(HalfEdge* halfEdge);

   void flipEdgeWithFaces(Int edge_id);

   // flip edge of index 'edgeId'
   void flipEdge(Int edgeId);

   // unflip half edge and its twin ccw
   void unflipHalfEdge(HalfEdge* halfEdge);

   // unflip edge of index 'edgeId'
   void unflipEdge(Int edgeId);

   // return the total number of vertices
   Int getNumVertices() const
   {
      return this->vertices.size();
   }

   // returns the number of half edges
   Int getNumHalfEdges() const
   {
      return this->halfedges.size();
   }

   // returns the number of edges
   Int getNumEdges() const
   {
      return getNumHalfEdges()/2;
   }

   // returns the number of faces
   Int getNumFaces() const
   {
      return faces.size();
   }

   // return the index of the given vertex
   Int getVertexId(const Vertex* vertex) const
   {
      if (vertex >= vertices.begin() && vertex < vertices.end())
         return vertex - vertices.begin();
      return null_id();
   }

   // return the index of the given halfedge
   Int getHalfEdgeId(const HalfEdge* halfEdge) const
   {
      if (halfEdge >= halfedges.begin() && halfEdge < halfedges.end())
         return halfEdge - halfedges.begin();
      return null_id();
   }

   // return a pointer to the half edge with the given id
   const HalfEdge* getHalfEdge(const Int id) const
   {
      return &halfedges[id];
   }

   const Face* getFace(const Int face_id) const
   {
      return &faces[face_id];
   }

   Int getFaceId(const Face* face) const
   {
      if (face >= faces.begin() && face < faces.end())
         return face - faces.begin();
      return null_id();
   }

   /* return the indices of the half edges that form a quadrilateral around the half edge of index id

            k
           / \            half edge ik has index id
          / | \
         /  |  \          the output vector gives the id's of the surrounding quad as [ij, jk, kl, il]
       l \  |  / j
          \ | /
           \ /
            i
   */
   std::array<Int, 8> getQuadId(Int id) const;

   // set the lengths of the edges according to the input vector
   void setMetric(const Vector<Rational>& metric);

   // set the A-coordinates of according to the input vector
   void setAcoords(const Vector<Rational>& acoords);

   // return the lengths of the edges
   Vector<Rational> edgeLengths() const;

   // calculte the inequalities that define the secondary cone
   Matrix<Rational> DelaunayInequalities() const;

   // for each valid facet of the secondary cone we collect the indices of those edges whose Delaunay inequalities define that facet
   Array<flip_sequence> flippableEdges(const flip_sequence& list_arg = std::list<Int>()) const;

   Matrix<Rational> coneFacets() const;

   // normalize the Vector in the positive orthant by dividing by its 1-norm
   template <typename TVec>
   static
   auto normalize(const GenericVector<TVec>& v)
   {
      return v / accumulate(v.top(), operations::add());
   }

   // normalize a matrix rowwise
   template <typename TMatrix>
   static
   Matrix<Rational> normalize(const GenericMatrix<TMatrix, Rational>& m)
   {
      Matrix<Rational> result(m);
      for (auto v = entire(rows(result)); !v.at_end(); ++v) {
         *v /= accumulate(*v, operations::add());
      }
      return result;
   }

   Set<Vector<Rational>> coneRays() const;

   // check if the facet is a potential candidate to flip the corresponding edges in the triangulation of the surface
   // we exclude the far facet ( 1 : 0 : ... : 0 ) and the coordinate hyperplanes ( 0 : ... : 1 : 0 : ... : 0 ) as well as ( 0 : ... : 0 )
   template <typename TVec>
   static bool validFacet(const GenericVector<TVec, Rational>& facet_normal)
   {
      return nonZeros(facet_normal) > 1;
   }

   template <typename TVec>
   static Int nonZeros(const GenericVector<TVec>& facet_normal)
   {
      Int non_zeros = 0;
      for (auto it = entire(facet_normal.top()); !it.at_end(); ++it)
         if (!is_zero(*it)) ++non_zeros;
      return non_zeros;
   }

   // flip the edges of the given indices and in the given order: false = [left->right], true = [right->left]
   // return the flip_sequence, where possible former flips are included if given as optional input
   flip_sequence flipEdges_and_give_flips(const flip_sequence& edgeIds, flip_sequence former_flips = flip_sequence(), bool reverse = false);

   // flip the edges of the given indices and in the given order: false = [left->right], true = [right->left]
   void flipEdges(const flip_sequence& edgeIds, bool reverse = false);

   // we flip those edges whose inequalities are equivalent to the facet normal until the there is no such edge, we only flip through valid facets
   flip_sequence flipThroughFace(const Vector<Rational>& facet_normal, flip_sequence former_flips = flip_sequence());

   // return the index of the first Delaunay inequality matrix that is equivalent to the given inequality "ineq"; return -1 if there is no such row
   Int first_equiv_row(const Vector<Rational>& ineq) const;

   // return true if the two vectors define the same non-degenerate half space
   bool is_equiv(const Vector<Rational>& ineq_a, const Vector<Rational>& ineq_b) const;

   // check if the the edge with index id is Delaunay after scaling the horocycles by the weights
   bool is_Delaunay(Int id, const Vector<Rational>& weights) const;

   // check if the triangulation is Delaunay w.r.t. the given weights, return id of the first edge that is not Delaunay or -1 if the triangulation is Delaunay
   Int is_Delaunay(const Vector<Rational>& weights) const;

   Vector<Int> DelaunayConditions(const Vector<Rational>& weights) const;

   // the flip algorithm, we flip edges that are non-Delaunay w.r.t. the weights as long as there are some
   flip_sequence flipToDelaunayAlt(const Vector<Rational>& weights);

   // return the angle sum of the vertex of index id
   Rational angleSum(Int id) const;

   // return the angle sum vector
   Vector<Rational> angleVector() const;

   // each face gets a new index = face_id + numHalfEdges
   // the triangleMap maps each edge_id to the index of its corresponding face
   const Map<Int, Int> triangleMap() const;

   template<typename ptr>
   void print_id_if_not_null(std::ostringstream& bos, const ptr* p, bool comma) const {
      if(p != nullptr) wrap(bos) << p->getID();
      else wrap(bos) << "NULL";
      if(comma) wrap(bos) << " ";
   }

   void debug_print() const {
      std::ostringstream bos;
      wrap(bos) << "VERTICES (incidentEdge)" << endl;
      for(const auto& vertex : vertices){
         wrap(bos) << vertex.getID() << ": ";
         print_id_if_not_null(bos, vertex.getIncidentEdge(), true);
         bos << endl;
      }
      wrap(bos) << "HALFEDGES" << endl;
      wrap(bos) << to_string();
      wrap(bos) << "FACES (half_edge)" << endl;
      for(const auto& face : faces){
         wrap(bos) << face.getID() << ": ";
         print_id_if_not_null(bos, face.getHalfEdge(), true);
         bos << endl;
      }
      cout << bos.str() << endl;
   }


   std::string to_string() const {
      std::ostringstream bos;
      wrap(bos) << "N_vertices: " << getNumVertices() << endl;
      if(with_faces){
         wrap(bos) << "Halfedges list: (edgeIndex: twinIndex nextIndex prevIndex headIndex faceIndex)" << endl;
      } else {
         wrap(bos) << "Halfedges list: (edgeIndex: twinIndex nextIndex prevIndex headIndex)" << endl;
      }
      for(const auto& he : halfedges){
         wrap(bos) << he.getID() << ": ";
         print_id_if_not_null(bos, he.getTwin(), true);
         print_id_if_not_null(bos, he.getNext(), true);
         print_id_if_not_null(bos, he.getPrev(), true);
         print_id_if_not_null(bos, he.getHead(), true);
         if(with_faces){
            print_id_if_not_null(bos, he.getFace(), true);
         }
         wrap(bos) << "(";
         if(he.getPrev() != nullptr) print_id_if_not_null(bos, he.getPrev()->getHead(), false);
         else wrap(bos) << "NOPREV";
         wrap(bos) << " ---> ";
         print_id_if_not_null(bos, he.getHead(), false);
         wrap(bos) << ")";
         wrap(bos) << "" << endl;
      }
      return bos.str();
   }

   template <typename Output> friend
   Output& operator<< (GenericOutput<Output>& out, const DoublyConnectedEdgeList& me)
   {
      out.top() << me.to_string();
      return out.top();
   }

}; // end class DoublyConnectedEdgeList

} // namespace dcel
} // end graph namespace
} // end polymake namespace


namespace pm{
   template<>
      struct spec_object_traits< Serialized< polymake::graph::dcel::DoublyConnectedEdgeList > > :
      spec_object_traits<is_composite> {

         typedef polymake::graph::dcel::DoublyConnectedEdgeList masquerade_for;

         typedef Matrix<Int> elements;

         template <typename Me, typename Visitor>
            static void visit_elements(Me& me, Visitor& v) //for data_load
            {
               v << me.matrix_representation;
               me.resize();
               me.populate();
            }

         template <typename Visitor>
            static void visit_elements(const pm::Serialized<masquerade_for>& me, Visitor& v) //for data_save
            {
               me.toMatrixInt();
               v << me.matrix_representation;
            }
      };

}


// Local Variables:
// mode:C++
// c-basic-offset:3
// indent-tabs-mode:nil
// End: