File: nsquad.cpp

package info (click to toggle)
regina-normal 4.5-1
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 16,824 kB
  • ctags: 7,862
  • sloc: cpp: 63,296; ansic: 12,913; sh: 10,556; perl: 3,294; makefile: 947; python: 188
file content (315 lines) | stat: -rw-r--r-- 13,165 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
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

/**************************************************************************
 *                                                                        *
 *  Regina - A Normal Surface Theory Calculator                           *
 *  Computational Engine                                                  *
 *                                                                        *
 *  Copyright (c) 1999-2008, 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.                       *
 *                                                                        *
 *  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, write to the Free            *
 *  Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,       *
 *  MA 02110-1301, USA.                                                   *
 *                                                                        *
 **************************************************************************/

/* end stub */

#include <deque>
#include "enumerate/ncompconstraint.h"
#include "surfaces/nsquad.h"
#include "surfaces/nsstandard.h"
#include "utilities/nrational.h"
#include "maths/nmatrixint.h"
#include "maths/nmatrixfield.h"
#include "maths/nvectorunit.h"
#include "triangulation/ntriangulation.h"

namespace regina {

NMatrixInt* NNormalSurfaceVectorQuad::makeMatchingEquations(
        NTriangulation* triangulation) {
    unsigned long nCoords = 3 * triangulation->getNumberOfTetrahedra();
    // One equation per non-boundary edge.
    long nEquations = long(triangulation->getNumberOfEdges());
    for (NTriangulation::BoundaryComponentIterator bit = triangulation->
            getBoundaryComponents().begin();
            bit != triangulation->getBoundaryComponents().end(); bit++)
        nEquations -= (*bit)->getNumberOfEdges();

    NMatrixInt* ans = new NMatrixInt(nEquations, nCoords);
    unsigned long row = 0;

    // Run through each internal edge and add the corresponding
    // equation.
    std::deque<NEdgeEmbedding>::const_iterator embit;
    NPerm perm;
    unsigned long tetIndex;
    for (NTriangulation::EdgeIterator eit = triangulation->getEdges().begin();
            eit != triangulation->getEdges().end(); eit++) {
        if (! (*eit)->isBoundary()) {
            for (embit = (*eit)->getEmbeddings().begin();
                    embit != (*eit)->getEmbeddings().end(); embit++) {
                tetIndex = triangulation->tetrahedronIndex(
                    (*embit).getTetrahedron());
                perm = (*embit).getVertices();
                ans->entry(row, 3 * tetIndex + vertexSplit[perm[0]][perm[2]])
                    += 1;
                ans->entry(row, 3 * tetIndex + vertexSplit[perm[0]][perm[3]])
                    -= 1;
            }
            row++;
        }
    }
    return ans;
}

NCompConstraintSet* NNormalSurfaceVectorQuad::makeEmbeddedConstraints(
        NTriangulation* triangulation) {
    NCompConstraintSet* ans = new NCompConstraintSet();
    NCompConstraint* constraint;

    unsigned i;
    unsigned long base = 0;
    for (unsigned long tet = 0; tet < triangulation->getNumberOfTetrahedra();
            tet++) {
        constraint = new NCompConstraint(1);
        for (i = 0; i < 3; i++)
            constraint->getCoordinates().insert(
                constraint->getCoordinates().end(), base + i);
        base += 3;

        ans->push_back(constraint);
    }

    return ans;
}

namespace {
    /**
     * A structure representing a particular end of an edge.
     */
    struct EdgeEnd {
        NEdge* edge;
            /**< The edge under consideration. */
        int end;
            /**< The end of the edge under consideration; this is 0 or 1. */

        EdgeEnd() {}
        EdgeEnd(NEdge* newEdge, int newEnd) : edge(newEdge), end(newEnd) {}
        EdgeEnd(const EdgeEnd& cloneMe) : edge(cloneMe.edge),
                end(cloneMe.end) {}
        void operator = (const EdgeEnd& cloneMe) {
            edge = cloneMe.edge; end = cloneMe.end;
        }
    };
}

NNormalSurfaceVector* NNormalSurfaceVectorQuad::makeMirror(
        NTriangulation* triang) const {
    // We're going to do this by wrapping around each edge and seeing
    // what comes.
    unsigned long nRows = 7 * triang->getNumberOfTetrahedra();
    NNormalSurfaceVectorStandard* ans =
        new NNormalSurfaceVectorStandard(nRows);

    // Set every triangular coordinate in the answer to infinity.
    // For coordinates about vertices not enjoying infinitely many discs,
    // infinity will mean "unknown".
    unsigned long row;
    int i;
    for (row = 0; row < nRows; row+=7)
        for (i = 0; i < 4; i++)
            ans->setElement(row + i, NLargeInteger::infinity);
    for (row = 0; 7 * row < nRows; row++)
        for (i = 0; i < 3; i++)
            ans->setElement(7 * row + 4 + i, (*this)[3 * row + i]);

    // Run through the vertices and work out the triangular coordinates
    // about each vertex in turn.
    stdhash::hash_set<NEdge*, HashPointer> usedEdges[2];
        // usedEdges[i] contains the edges for which we have already
        // examined end i.
    NLargeInteger min;
        // The minimum coordinate that has been assigned about this
        // vertex.
    std::deque<EdgeEnd> examine;
    bool broken;
        // Are the matching equations broken about this edge end?
    int end;
    NEdge* edge;
    EdgeEnd current;
    std::vector<NVertexEmbedding>::const_iterator vembit;
    std::deque<NEdgeEmbedding>::const_iterator eembit, backupit,
        endit, beginit;
    NTetrahedron* tet;
    NTetrahedron* adj;
    NPerm tetPerm, adjPerm;
    unsigned long tetIndex, adjIndex;
    NLargeInteger expect;
    for (NTriangulation::VertexIterator vit = triang->getVertices().begin();
            vit != triang->getVertices().end(); vit++) {
        usedEdges[0].clear(); usedEdges[1].clear();
        examine.clear();
        broken = false;

        // Pick some triangular disc and set it to zero.
        const NVertexEmbedding& vemb = (*vit)->getEmbedding(0);
        row = 7 * triang->tetrahedronIndex(vemb.getTetrahedron())
            + vemb.getVertex();
        ans->setElement(row, NLargeInteger::zero);

        min = NLargeInteger::zero;

        // Mark the three surrounding edge ends for examination.
        for (i=0; i<4; i++) {
            if (i == vemb.getVertex())
                continue;
            edge = vemb.getTetrahedron()->getEdge(
                edgeNumber[vemb.getVertex()][i]);
            end = vemb.getTetrahedron()->getEdgeMapping(
                edgeNumber[vemb.getVertex()][i])[0] == i ? 1 : 0;
            if (usedEdges[end].insert(edge).second)
                examine.push_back(EdgeEnd(edge, end));
        }

        // Cycle through edge ends until we are finished or until the
        // matching equations are broken.  Each time we pick a value for
        // a coordinate, add the corresponding nearby edge ends to the
        // list of edge ends to examine.
        while ((! broken) && (! examine.empty())) {
            current = examine.front();
            examine.pop_front();

            // Run around this edge end.
            // We know there is a pre-chosen coordinate somewhere; run
            // forwards and find this.
            beginit = current.edge->getEmbeddings().begin();
            endit = current.edge->getEmbeddings().end();
            for (eembit = beginit; eembit != endit; eembit++)
                if (! (*ans)[7 * triang->tetrahedronIndex(
                        (*eembit).getTetrahedron()) +
                        (*eembit).getVertices()[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.
            backupit = eembit;
            adj = (*eembit).getTetrahedron();
            adjPerm = (*eembit).getVertices();
            adjIndex = triang->tetrahedronIndex(adj);
            while (eembit != beginit) {
                eembit--;

                // Work out the coordinate for the disc type at eembit.
                tet = (*eembit).getTetrahedron();
                tetPerm = (*eembit).getVertices();
                tetIndex = triang->tetrahedronIndex(tet);

                expect =
                    (*ans)[7 * adjIndex + adjPerm[current.end]] +
                    (*ans)[7 * adjIndex + 4 +
                        vertexSplit[adjPerm[3]][adjPerm[current.end]]] -
                    (*ans)[7 * tetIndex + 4 +
                        vertexSplit[tetPerm[2]][tetPerm[current.end]]];
                ans->setElement(7 * tetIndex + tetPerm[current.end], expect);
                if (expect < min)
                    min = expect;

                // Remember to examine the new edge end if appropriate.
                edge = tet->getEdge(
                    edgeNumber[tetPerm[2]][tetPerm[current.end]]);
                end = tet->getEdgeMapping(
                    edgeNumber[tetPerm[2]][tetPerm[current.end]])[0]
                    == tetPerm[2] ? 1 : 0;
                if (usedEdges[end].insert(edge).second)
                    examine.push_back(EdgeEnd(edge, end));

                adj = tet;
                adjPerm = tetPerm;
                adjIndex = tetIndex;
            }

            // Now move forwards from the original first pre-chosen
            // coordinate and fill in the holes from here onwards,
            // always checking to ensure the
            // matching equations have not been broken.
            eembit = backupit;
            adj = (*eembit).getTetrahedron();
            adjPerm = (*eembit).getVertices();
            adjIndex = triang->tetrahedronIndex(adj);
            for (eembit++; eembit != endit; eembit++) {
                // Work out the coordinate for the disc type at eembit.
                tet = (*eembit).getTetrahedron();
                tetPerm = (*eembit).getVertices();
                tetIndex = triang->tetrahedronIndex(tet);

                expect =
                    (*ans)[7 * adjIndex + adjPerm[current.end]] +
                    (*ans)[7 * adjIndex + 4 +
                        vertexSplit[adjPerm[2]][adjPerm[current.end]]] -
                    (*ans)[7 * tetIndex + 4 +
                        vertexSplit[tetPerm[3]][tetPerm[current.end]]];
                row = 7 * tetIndex + tetPerm[current.end];
                if ((*ans)[row].isInfinite()) {
                    ans->setElement(row, expect);
                    if (expect < min)
                        min = expect;

                    // Remember to examine the new edge end if appropriate.
                    edge = tet->getEdge(
                        edgeNumber[tetPerm[3]][tetPerm[current.end]]);
                    end = tet->getEdgeMapping(
                        edgeNumber[tetPerm[3]][tetPerm[current.end]])[0]
                        == tetPerm[3] ? 1 : 0;
                    if (usedEdges[end].insert(edge).second)
                        examine.push_back(EdgeEnd(edge, end));
                } else {
                    // This coordinate has already been set.
                    // Make sure it's the same value!
                    if ((*ans)[row] != expect) {
                        broken = true;
                        break;
                    }
                }

                adj = tet;
                adjPerm = tetPerm;
                adjIndex = tetIndex;
            }
        }

        // 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 (vembit = (*vit)->getEmbeddings().begin();
                vembit != (*vit)->getEmbeddings().end(); vembit++) {
            row = 7 * triang->tetrahedronIndex((*vembit).getTetrahedron())
                + (*vembit).getVertex();
            if (broken)
                ans->setElement(row, NLargeInteger::infinity);
            else
                ans->setElement(row, (*ans)[row] - min);
        }
    }

    // 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.
    return ans;
}

} // namespace regina