File: links.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 (312 lines) | stat: -rw-r--r-- 12,123 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

/**************************************************************************
 *                                                                        *
 *  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 "surfaces/nnormalsurface.h"
#include "triangulation/ntriangulation.h"

#define NO_EDGES std::make_pair(static_cast<NEdge*>(0), static_cast<NEdge*>(0))

namespace regina {

bool NNormalSurfaceVector::isVertexLinking(NTriangulation* triang) const {
    unsigned long nTets = triang->getNumberOfTetrahedra();
    unsigned long tet;
    int type;
    for (tet = 0; tet < nTets; tet++) {
        for (type = 0; type < 3; type++)
            if (getQuadCoord(tet, type, triang) != 0)
                return false;
    }
    if (allowsAlmostNormal())
        for (tet = 0; tet < nTets; tet++)
            for (type = 0; type < 3; type++)
                if (getOctCoord(tet, type, triang) != 0)
                    return false;
    return true;
}

const NVertex* NNormalSurfaceVector::isVertexLink(NTriangulation* triang)
        const {
    unsigned long nTets = triang->getNumberOfTetrahedra();
    unsigned long tet;
    int type;

    // Check that there are no quad/oct discs.
    for (tet = 0; tet < nTets; tet++) {
        for (type = 0; type < 3; type++)
            if (getQuadCoord(tet, type, triang) != 0)
                return 0;
    }
    if (allowsAlmostNormal())
        for (tet = 0; tet < nTets; tet++)
            for (type = 0; type < 3; type++)
                if (getOctCoord(tet, type, triang) != 0)
                    return 0;

    // Now examine the triangle to see if we link only a single vertex.
    stdhash::hash_set<NVertex*, HashPointer> notAns;
        /**< We will ignore notAns once ans != 0. */
    NVertex* ans = 0;
    NLargeInteger ansMult;

    NTetrahedron* t;
    NVertex* v;
    NLargeInteger coord;

    for (tet = 0; tet < nTets; tet++) {
        t = triang->getTetrahedron(tet);
        for (type = 0; type < 4; type++) {
            v = t->getVertex(type);
            coord = getTriangleCoord(tet, type, triang);

            if (coord == 0) {
                // No discs in this coordinate.
                // Do we have a candidate vertex yet?
                if (! ans) {
                    // Still haven't found a candidate.
                    notAns.insert(v);
                } else if (ans == v) {
                    // This is our only candidate vertex.
                    return 0;
                }
            }

            if (coord != 0) {
                // Some discs in this coordinate.
                // Do we have a candidate vertex?
                if (! ans) {
                    // We've found our first and only possible candidate.
                    if (notAns.count(v))
                        return 0;
                    ans = v;
                    ansMult = coord;
                } else if (ans == v) {
                    // This vertex matches our current candidate.
                    if (ansMult != coord)
                        return 0;
                } else {
                    // This vertex does not match our current candidate.
                    return 0;
                }
            } else {
                // No discs in this coordinate.
                if (ans == v)
                    return 0;
            }
        }
    }

    return ans;
}

std::pair<const NEdge*, const NEdge*> NNormalSurfaceVector::isThinEdgeLink(
        NTriangulation* triang) const {
    unsigned long nTets = triang->getNumberOfTetrahedra();
    unsigned long tet;
    int type;

    // Check that there are no octagonal discs.
    if (allowsAlmostNormal())
        for (tet = 0; tet < nTets; tet++)
            for (type = 0; type < 3; type++)
                if (getOctCoord(tet, type, triang) != 0)
                    return NO_EDGES;

    // Run through the quadrilateral discs and work out if there are any
    // valid candidates.
    stdhash::hash_set<NEdge*, HashPointer> notAns;
        /**< We will ignore notAns once ans != 0. */
    bool foundQuads = false;
    const NEdge* ans[2];
    NLargeInteger ansMultDouble;

    NTetrahedron* t;
    NEdge* e[6]; // { 2*link, 4*intersect }
    NLargeInteger coord;
    int i;

    for (tet = 0; tet < nTets; tet++) {
        t = triang->getTetrahedron(tet);
        for (type = 0; type < 3; type++) {
            coord = getQuadCoord(tet, type, triang);
            e[0] = t->getEdge(edgeNumber[vertexSplitDefn[type][0]]
                [vertexSplitDefn[type][1]]);
            e[1] = t->getEdge(edgeNumber[vertexSplitDefn[type][2]]
                [vertexSplitDefn[type][3]]);
            e[2] = t->getEdge(edgeNumber[vertexSplitDefn[type][0]]
                [vertexSplitDefn[type][2]]);
            e[3] = t->getEdge(edgeNumber[vertexSplitDefn[type][0]]
                [vertexSplitDefn[type][3]]);
            e[4] = t->getEdge(edgeNumber[vertexSplitDefn[type][1]]
                [vertexSplitDefn[type][2]]);
            e[5] = t->getEdge(edgeNumber[vertexSplitDefn[type][1]]
                [vertexSplitDefn[type][3]]);

            if (coord == 0) {
                // No discs in this coordinate.
                // Do we have any candidate edges yet?
                if (foundQuads) {
                    // Rule out any candidates that should have discs here.
                    for (i = 0; i < 2; i++)
                        if (ans[i] == e[0] || ans[i] == e[1])
                            ans[i] = 0;
                } else {
                    // Still haven't found any candidates.
                    notAns.insert(e[0]);
                    notAns.insert(e[1]);
                }
            } else {
                // Some discs in this coordinate.
                // Do we have any candidate edges yet?
                if (foundQuads) {
                    // Check consistency with our candidates.
                    if (e[0] == e[1]) {
                        // Same edge on both sides of the quad.
                        // Note that there can only be one candidate now.
                        if (e[0] == ans[0])
                            ans[1] = 0;
                        else if (e[0] == ans[1]) {
                            ans[0] = ans[1];
                            ans[1] = 0;
                        } else
                            return NO_EDGES;

                        // The only possible candidate is ans[0].
                        if (ans[0] == 0 || ansMultDouble != coord)
                            return NO_EDGES;
                    } else {
                        // Different edges on both sides of the quad.
                        // Check each candidate in turn.
                        for (i = 0; i < 2; i++)
                            if (ans[i] != e[0] && ans[i] != e[1])
                                ans[i] = 0;
                        if (ansMultDouble != coord * 2)
                            return NO_EDGES;
                    }
                } else {
                    // We've found our first and only possible candidates.
                    if (e[0] == e[1]) {
                        // Same edge on both sides of the quad.
                        if (notAns.count(e[0]))
                            return NO_EDGES;
                        ans[0] = e[0];
                        ans[1] = 0;
                        ansMultDouble = coord;
                    } else {
                        // Different edges on both sides of the quad.
                        for (i = 0; i < 2; i++) {
                            if (notAns.count(e[i]))
                                ans[i] = 0;
                            else {
                                ans[i] = e[i];
                                ansMultDouble = coord;
                                ansMultDouble *= 2;
                            }
                        }
                    }
                    foundQuads = true;
                }

                // We now absolutely have candidates (or have exhausted
                // them all).  Check that these candidates don't
                // intersect the new quads.
                for (i = 2; i < 6; i++) {
                    if (ans[0] == e[i])
                        ans[0] = 0;
                    if (ans[1] == e[i])
                        ans[1] = 0;
                }
            }

            // Have we ruled out all the candidates we ever had?
            if (foundQuads && (! ans[0]) && (! ans[1]))
                return NO_EDGES;
        }
    }

    // So did we actually find anything?
    if (! foundQuads)
        return NO_EDGES;
    if ((! ans[0]) && (! ans[1]))
        return NO_EDGES;

    // Finally check the triangular discs.
    NVertex* v;
    bool expectZero[2];
    int j;
    for (tet = 0; tet < nTets; tet++) {
        t = triang->getTetrahedron(tet);
        for (type = 0; type < 4; type++) {
            v = t->getVertex(type);
            coord = getTriangleCoord(tet, type, triang);

            // Should we actually see any discs?
            for (i = 0; i < 2; i++) {
                if (! ans[i])
                    continue;

                // If the candidate edge does not touch this vertex, the
                // triangular coordinate should be 0.
                expectZero[i] = (v != ans[i]->getVertex(0) &&
                    v != ans[i]->getVertex(1));

                // If this triangular disc type intersects the candidate
                // edge, the coordinate should also be 0.
                if (! expectZero[i])
                    for (j = 0; j < 3; j++)
                        if (t->getEdge(edgeNumber[type][(type + j + 1) % 4])
                                == ans[i]) {
                            expectZero[i] = true;
                            break;
                        }

                // So did we get the right triangular coordinate?
                if (expectZero[i]) {
                    if (coord != 0)
                        ans[i] = 0;
                } else {
                    if (ansMultDouble != coord * 2)
                        ans[i] = 0;
                }
            }

            // Have we ruled out all possibilities?
            if ((! ans[0]) && (! ans[1]))
                return NO_EDGES;
        }
    }

    // Return whatever candidates have survived.
    if (ans[0])
        return std::make_pair(ans[0], ans[1]);
    else
        return std::make_pair(ans[1], ans[0]);
}

} // namespace regina