File: triangles.cpp

package info (click to toggle)
regina-normal 7.4.1-1.1
  • links: PTS
  • area: main
  • in suites: forky, sid
  • size: 154,244 kB
  • sloc: cpp: 295,026; xml: 9,992; sh: 1,344; python: 1,225; perl: 616; ansic: 138; makefile: 26
file content (260 lines) | stat: -rw-r--r-- 11,469 bytes parent folder | download
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

/**************************************************************************
 *                                                                        *
 *  Regina - A Normal Surface Theory Calculator                           *
 *  Computational Engine                                                  *
 *                                                                        *
 *  Copyright (c) 1999-2025, Ben Burton                                   *
 *  For further details contact Ben Burton (bab@debian.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 of the    *
 *  License, or (at your option) any later version.                       *
 *                                                                        *
 *  As an exception, when this program is distributed through (i) the     *
 *  App Store by Apple Inc.; (ii) the Mac App Store by Apple Inc.; or     *
 *  (iii) Google Play by Google Inc., then that store may impose any      *
 *  digital rights management, device limits and/or redistribution        *
 *  restrictions that are required by its terms of service.               *
 *                                                                        *
 *  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.                              *
 *                                                                        *
 *  You should have received a copy of the GNU General Public License     *
 *  along with this program. If not, see <https://www.gnu.org/licenses/>. *
 *                                                                        *
 **************************************************************************/

#include "surface/normalsurface.h"
#include "maths/matrix.h"
#include "maths/rational.h"
#include "triangulation/dim3.h"

namespace regina {

namespace {
    /**
     * A structure representing one end of an edge.
     */
    struct EdgeEnd {
        Edge<3>* edge; /**< The edge under consideration. */
        int end; /**< Either 0 or 1, indicating which end of the edge. */
    };
}

NormalEncoding NormalSurface::reconstructTriangles(
        const Triangulation<3>& tri, Vector<LargeInteger>& vector,
        NormalEncoding enc) {
    // We are offering this function to the public, so do a sanity check on enc.
    // For our own code this is redundant but it's just one bit-flag test.
    if (enc.storesTriangles())
        return enc;

    const NormalEncoding newEnc = enc.withTriangles();
    const int newBlock = newEnc.block();
    const size_t newSize = newBlock * tri.size();
    Vector<LargeInteger> ans(newSize);

    // Set every triangular coordinate in the answer to infinity.
    // For coordinates about vertices not enjoying infinitely many discs,
    // infinity will mean "unknown".
    for (size_t row = 0; row < newSize; row += newBlock)
        for (int i = 0; i < 4; ++i)
            ans[row + i].makeInfinite();

    // Copy across the other (non-triangular) coordinates.
    for (size_t row = 0; newBlock * row < newSize; ++row)
        for (int i = 0; i < enc.block(); ++i)
            ans[newBlock * row + 4 + i] = vector[enc.block() * row + i];

    // Record which edge ends we have already examined.
    bool* used = new bool[2 * tri.countEdges()];
    std::fill(used, used + 2 * tri.countEdges(), false);

    // Prepare a stack of edge ends that we are ready to examine.
    auto* examine = new EdgeEnd[2 * tri.countEdges()];
    size_t nExamine = 0;

    // Run through the vertices and fix the triangular coordinates
    // about each vertex in turn.
    for (Vertex<3>* v : tri.vertices()) {
        nExamine = 0;
        bool broken = false; // Are the equations broken around this vertex?

        // Pick some triangular disc and set it to zero.
        const VertexEmbedding<3>& vemb = v->front();
        ans[newBlock * vemb.tetrahedron()->index() + vemb.vertex()] = 0;
        LargeInteger min; // The minimum triangle coordinate around this vertex.

        // Mark the three surrounding edge ends for examination.
        for (int i=0; i<4; i++) {
            if (i == vemb.vertex())
                continue;
            EdgeEnd e {
                vemb.tetrahedron()->edge(Edge<3>::edgeNumber[vemb.vertex()][i]),
                vemb.tetrahedron()->edgeMapping(
                    Edge<3>::edgeNumber[vemb.vertex()][i])[0] == i ? 1 : 0
            };
            size_t usedIndex = 2 * e.edge->index() + e.end;
            if (! used[usedIndex]) {
                used[usedIndex] = true;
                examine[nExamine++] = e;
            }
        }

        // Run a depth-first search through the edge ends that meet this
        // vertex link.
        while ((! broken) && nExamine) {
            EdgeEnd current = examine[--nExamine];

            // Run around this edge end.
            // We know there is a pre-chosen coordinate somewhere; run
            // forwards and find this.
            const auto beginit = current.edge->begin();
            const auto endit = current.edge->end();

            auto eembit = beginit;
            for ( ; eembit != endit; ++eembit)
                if (! ans[newBlock * eembit->tetrahedron()->index() +
                        eembit->vertices()[current.end]].isInfinite())
                    break;

            // We are now at the first pre-chosen coordinate about this
            // vertex.  Run backwards from here and fill in all the holes.
            Perm<4> adjPerm = eembit->vertices();
            size_t adjPos = newBlock * eembit->tetrahedron()->index();

            auto backupit = eembit;
            while (eembit != beginit) {
                --eembit;

                // Work out the coordinate for the disc type at eembit.
                Tetrahedron<3>* tet = eembit->tetrahedron();
                Perm<4> tetPerm = eembit->vertices();
                size_t tetPos = newBlock * tet->index();

                LargeInteger expect =
                    ans[adjPos + adjPerm[current.end]]
                    + ans[adjPos + 4 +
                        quadSeparating[adjPerm[3]][adjPerm[current.end]]]
                    - ans[tetPos + 4 +
                        quadSeparating[tetPerm[2]][tetPerm[current.end]]];
                if (enc.storesOctagons()) {
                    expect = expect
                        + ans[adjPos + 7 + quadMeeting
                            [adjPerm[3]][adjPerm[current.end]][0]]
                        + ans[adjPos + 7 + quadMeeting
                            [adjPerm[3]][adjPerm[current.end]][1]]
                        - ans[tetPos + 7 + quadMeeting
                            [tetPerm[2]][tetPerm[current.end]][0]]
                        - ans[tetPos + 7 + quadMeeting
                            [tetPerm[2]][tetPerm[current.end]][1]];
                }
                ans[tetPos + tetPerm[current.end]] = expect;
                if (expect < min)
                    min = expect;

                // Remember to examine the new edge end if appropriate.
                EdgeEnd e {
                    tet->edge(Edge<3>::edgeNumber[tetPerm[2]]
                        [tetPerm[current.end]]),
                    tet->edgeMapping(Edge<3>::edgeNumber[tetPerm[2]]
                        [tetPerm[current.end]])[0] == tetPerm[2] ? 1 : 0
                };
                size_t usedIndex = 2 * e.edge->index() + e.end;
                if (! used[usedIndex]) {
                    used[usedIndex] = true;
                    examine[nExamine++] = e;
                }

                adjPerm = tetPerm;
                adjPos = tetPos;
            }

            // Now move forwards from the original first pre-chosen
            // coordinate, again filling in the holes.
            eembit = backupit;
            adjPerm = eembit->vertices();
            adjPos = newBlock * eembit->tetrahedron()->index();

            for (++eembit; eembit != endit; ++eembit) {
                // Work out the coordinate for the disc type at eembit.
                Tetrahedron<3>* tet = eembit->tetrahedron();
                Perm<4> tetPerm = eembit->vertices();
                size_t tetPos = newBlock * tet->index();

                LargeInteger expect =
                    ans[adjPos + adjPerm[current.end]]
                    + ans[adjPos + 4 +
                        quadSeparating[adjPerm[2]][adjPerm[current.end]]]
                    - ans[tetPos + 4 +
                        quadSeparating[tetPerm[3]][tetPerm[current.end]]];
                if (enc.storesOctagons()) {
                    expect = expect
                        + ans[adjPos + 7 + quadMeeting
                            [adjPerm[2]][adjPerm[current.end]][0]]
                        + ans[adjPos + 7 + quadMeeting
                            [adjPerm[2]][adjPerm[current.end]][1]]
                        - ans[tetPos + 7 + quadMeeting
                            [tetPerm[3]][tetPerm[current.end]][0]]
                        - ans[tetPos + 7 + quadMeeting
                            [tetPerm[3]][tetPerm[current.end]][1]];
                }
                size_t row = tetPos + tetPerm[current.end];
                if (ans[row].isInfinite()) {
                    ans[row] = expect;
                    if (expect < min)
                        min = expect;

                    // Remember to examine the new edge end if appropriate.
                    EdgeEnd e {
                        tet->edge(Edge<3>::edgeNumber[tetPerm[3]]
                            [tetPerm[current.end]]),
                        tet->edgeMapping(Edge<3>::edgeNumber[tetPerm[3]]
                            [tetPerm[current.end]])[0] == tetPerm[3] ? 1 : 0
                    };
                    size_t usedIndex = 2 * e.edge->index() + e.end;
                    if (! used[usedIndex]) {
                        used[usedIndex] = true;
                        examine[nExamine++] = e;
                    }
                } else {
                    // This coordinate has already been set.
                    // Make sure it's the same value!
                    if (ans[row] != expect) {
                        broken = true;
                        break;
                    }
                }

                adjPerm = tetPerm;
                adjPos = tetPos;
            }
        }

        // If the matching equations were broken, set every coordinate
        // to infinity.  Otherwise subtract min from every coordinate to
        // make the values as small as possible.
        for (const auto& emb : *v) {
            size_t row = newBlock * emb.tetrahedron()->index() + emb.vertex();
            if (broken)
                ans[row].makeInfinite();
            else
                ans[row] -= min;
        }
    }

    delete[] used;

    // Note that there should be no need to remove common factors since
    // the quad coordinates have not changed and in theory they already
    // had gcd=1.
    vector = std::move(ans);
    return newEnc;
}

} // namespace regina