File: connected_sum.tcc

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 (127 lines) | stat: -rw-r--r-- 3,814 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
/* 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.
--------------------------------------------------------------------------------
*/

#include "polymake/topaz/connected_sum.h"
#include "polymake/topaz/complex_tools.h"
#include <sstream>

namespace polymake { namespace topaz {

template <typename Complex_1, typename Complex_2>
std::list<Set<Int>> connected_sum(const Complex_1& C1,
                                  const Complex_2& C2,
                                  const Int f1, const Int f2,
                                  Array<std::string>& L1,
                                  const Array<std::string>& L2,
                                  hash_map<Int, Int>& P)
{
   std::list<Set<Int>> CS;
      
   // add facets of C1, omitting f1, and compute the vertex set of C1
   Set<Int> V1, V2, facet1, facet2;
   Int i = 0;
   for (auto c_it = entire(C1);  !c_it.at_end();  ++c_it, ++i) {
      if (i == f1)
         facet1 = *c_it;
      else
         CS.push_back(*c_it);
      V1 += *c_it;
   }

   if (facet1.empty())
      throw std::runtime_error("connected_sum - f1 is not a facet index");

   // find facet f2 and compute the vertex set of C2
   i = 0;
   for (auto c_it = entire(C2); !c_it.at_end(); ++c_it, ++i) {
      if (i==f2)
         facet2 = *c_it;
      V2 += *c_it;
   }

   if (facet2.empty())
      throw std::runtime_error("connected_sum - f2 is not a facet index");

   if (facet1.size() != facet2.size())
      throw std::runtime_error("connected_sum - facets dimension mismatch");

   // compute new vertex indices for V2, identifying facet1 and facet2
   Int index_diff = V1.back()-V2.front()+1;
   hash_map<Int, Int> vertex_map(V2.size());
   auto f1_it = facet1.begin();
   for (auto it = entire(V2); !it.at_end(); ++it) {
      const Int v = *it;
      if (facet2.contains(v)) {
         vertex_map[*it] = P.empty() ? *f1_it : P[*f1_it];
         ++f1_it;
         --index_diff;
      } else {
         vertex_map[*it] = *it+index_diff;
      }
   }

   // add facets of C2, omitting f2, and adjust the vertex indices
   i = 0;
   for (auto c_it = entire(C2);  !c_it.at_end();  ++c_it, ++i) {
      if (i != f2) {
         Set<Int> f;
         for (auto f_it = entire(*c_it); !f_it.at_end(); ++f_it)
            f += vertex_map[*f_it];
         CS.push_back(f);
      }
   }

   // adjust labels
   if (L1.size() != 0) {         // if L1.size()==0 => no L1 and L2 specified.
      Int count = L1.size();
      L1.resize(count + L2.size() - facet1.size());

      for (Int v = 0; v < count; ++v) {
         if (!facet1.contains(v))
            L1[v].append("_1");
      }

      for (Int v = 0; v < L2.size(); ++v) {
         if (!facet2.contains(v)) {
            L1[count] = L2[v] + "_2";
            ++count;
         }
      }
   }

   const Set<Int> V = accumulate(CS, operations::add());
   if (adj_numbering(CS, V) && !L1.empty()) {

      // adjust labels
      Array<std::string> L_tmp(V.size());
      auto l = L_tmp.begin();
      for (auto v = entire(V); !v.at_end(); ++v, ++l)
         *l = L1[*v];

      L1 = L_tmp;
   }
      
   return CS;
}

} }

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