File: reorder.cpp

package info (click to toggle)
regina-normal 4.93-1
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 28,576 kB
  • sloc: cpp: 86,815; ansic: 13,030; xml: 9,089; perl: 951; sh: 380; python: 273; makefile: 103
file content (358 lines) | stat: -rw-r--r-- 12,573 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
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

/**************************************************************************
 *                                                                        *
 *  Regina - A Normal Surface Theory Calculator                           *
 *  Computational Engine                                                  *
 *                                                                        *
 *  Copyright (c) 1999-2011, 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 "maths/nperm4.h"
#include "triangulation/ntriangulation.h"
#include "triangulation/nedge.h"
#include "triangulation/nisomorphism.h"

#include <iostream>
#include <cstdlib>

namespace regina {

namespace { // Begin anonymous namespace

void reorder_fatal_error(const char* msg) {
    std::cerr << "ERROR: " << msg << std::endl;
    std::exit(1);
}

// Given is a tetrahedron with an ordering inducing edge orientations.
// edge_orientations_on_tet[i] == -1 means that we intend to flip the
// edge orientation of the i-th edge of the tetrahedron.
// perm_from_edges returns the permutation that needs to be applied to the
// tetrahedron to achieve this.

NPerm4 perm_from_edges(const int edge_orientations_on_tet[6]) {
    // p[i] = number of edge_orientations pointing to vertex i
    int p[4] = {0, 0, 0, 0};
    for(int i = 0; i < 6; i++)
        if(edge_orientations_on_tet[i] == +1)
            p[NEdge::edgeVertex[i][1]]++;
        else
            p[NEdge::edgeVertex[i][0]]++;

    // Consistency check
    for(int i = 0; i < 4; i++) {
        if(p[i] > 3 || p[i] < 0)
            reorder_fatal_error("bad permutation in reorder.cpp");
        for(int j = i+1; j < 4; j++)
            if(p[i] == p[j])
                reorder_fatal_error("bad permutation in reorder.cpp");
    }

    return NPerm4(p[0],p[1],p[2],p[3]);
}

// edge_orientations[i] is the edge orientation of the i-th edge in
//                                     the triangulation
// edge_orientations_tet[i] is the edge orientation of the i-th edge of
//                                     the tetrahedron
// edge_orientations_tet[i] == +1 means that the edge is oriented pointing
//                                     from the lower to the higher vertex

// writes the result into edge_orientations_tet

void edge_orientations_on_tet(const NTriangulation &trig,
                              const std::vector<int> &edge_orientations,
                              const NTetrahedron *tet,
                              int edge_orientations_tet[6]) {
    for(int i = 0; i < 6; i++)
    {
        // to get the edge orientation on a tetrahedron's edge
        // look it up in edge_orientations

        int orientation = edge_orientations[trig.edgeIndex(tet->getEdge(i))];

        // a tetrahedron's edge might be identified with the edge in the
        // triangulation in a way that the edge orientation is not consistent

        NPerm4 perm = tet->getEdgeMapping(i);
        if(perm[0] > perm[1])
            orientation = - orientation;
        edge_orientations_tet[i] = orientation;
    }
}

// edge_orientations_tet are as above
// v0, v1, v2 are the vertices of the face
// It is assumed that v0 < v1 < v2

inline bool check_consistency_on_face(const int edge_orientations_tet[6],
                                      int v0, int v1, int v2) {

    // There are only two ways to get a cyclic orientation of edges on a face

    if(   (edge_orientations_tet[NEdge::edgeNumber[v0][v1]] == +1)
       && (edge_orientations_tet[NEdge::edgeNumber[v1][v2]] == +1)
       && (edge_orientations_tet[NEdge::edgeNumber[v0][v2]] == -1))
        return false;

    if(   (edge_orientations_tet[NEdge::edgeNumber[v0][v1]] == -1)
       && (edge_orientations_tet[NEdge::edgeNumber[v1][v2]] == -1)
       && (edge_orientations_tet[NEdge::edgeNumber[v0][v2]] == +1))
        return false;

    return true;
}

// edge_orientations[i] == 0 means that the edge_orientation has not been
// assigned yet, so ignore it for testing

// checks that the edge_orientations give an ordering of the triangulation

bool check_consistency_on_tet(const NTriangulation &trig,
                              const std::vector<int> &edge_orientations,
                              const NTetrahedron *tet,
                              bool force_oriented)
{
    int edge_orientations_tet[6];

    // compute how the assignment of orientations to edges of the triangulation
    // look on the tetrahedron
    edge_orientations_on_tet(trig,edge_orientations,tet,edge_orientations_tet);

    // check that edge orientations are acyclic on each face of tet

    if(!check_consistency_on_face(edge_orientations_tet,1,2,3))
        return false;
    if(!check_consistency_on_face(edge_orientations_tet,0,2,3))
        return false;
    if(!check_consistency_on_face(edge_orientations_tet,0,1,3))
        return false;
    if(!check_consistency_on_face(edge_orientations_tet,0,1,2))
        return false;

    // if we do not need to check for consistent orientation, we are done:

    if(!force_oriented)
        return true;

    // orientation can't be determined until all edge_orientations are assigned

    for(int i = 0; i < 6; i++)
        if(edge_orientations_tet[i] == 0)
            return true;

    // check for valid orientation

    NPerm4 p = perm_from_edges(edge_orientations_tet);
    if(p.sign() * tet->orientation() == -1)
        return false;

    return true;
}

// checks that the edge orientations give a valid ordering of the triangulation

bool check_consistency_around_edge(const NTriangulation &trig,
                                   const std::vector<int> &edge_orientations,
                                   int edge_index,
                                   bool force_oriented)
{
    typedef std::deque<NEdgeEmbedding> NEmbed;
    typedef NEmbed::const_iterator NEmbedIterator;

    NEdge*           edge            = trig.getEdge(edge_index);
    const NEmbed &   edge_embeddings = edge -> getEmbeddings();
    NEmbedIterator   it;

    // iterate through all tetrahedra around an edge

    for(it = edge_embeddings.begin(); it != edge_embeddings.end(); ++it)
        if(!check_consistency_on_tet(trig, edge_orientations,
                                     it->getTetrahedron(), force_oriented))
            return false;

    return true;
}

// construct isomorphism from edge orientations

NIsomorphism* iso_from_edges(const NTriangulation &trig,
                             const std::vector<int> & edge_orientations,
                             bool force_oriented) {

    NIsomorphism* iso = new NIsomorphism(trig.getNumberOfTetrahedra());

    // iterate through all tetrahedra

    for(unsigned i = 0; i < trig.getNumberOfTetrahedra(); i++) {

        // consistency check

        if(!check_consistency_on_tet(trig,
                                     edge_orientations,
                                     trig.getTetrahedron(i),
                                     force_oriented))
            reorder_fatal_error("Inconsistent edge orientations in reorder.cpp");

        int edge_orientations_tet[6];

        // compute how the edge orientations look on the tetrahedron

        edge_orientations_on_tet(trig,
                                 edge_orientations,
                                 trig.getTetrahedron(i),
                                 edge_orientations_tet);

        // derive the permutation
        iso->facePerm(i) = perm_from_edges(edge_orientations_tet);
        iso->tetImage(i) = i;
    }
    return iso;
}

// Find edge orientations (through back tracking) such that they induce a valid
// ordering on each tetrahedron (and if force_oriented also a consistent
// orientation on each tetrahedron).
// If the function succeeds the isomorphism turning the triangulation into an
// ordered triangulation is returned.

NIsomorphism* ordering_iso(const NTriangulation &trig, bool force_oriented)
{
    std::vector<int> edge_orientations(trig.getNumberOfEdges(),0);

    int i = 0;

    while(true) {
        if(i < 0)
            return NULL;

        if(i >= static_cast<int>(trig.getNumberOfEdges()))
            return iso_from_edges(trig, edge_orientations, force_oriented);

        if(edge_orientations[i] == 0) {
            edge_orientations[i] = +1;
            if(check_consistency_around_edge(trig,edge_orientations,
                                             i,force_oriented))
                ++i;
        }
        else
            if(edge_orientations[i] == +1) {
                edge_orientations[i] = -1;
                if(check_consistency_around_edge(trig,edge_orientations,
                                                 i,force_oriented))
                    ++i;
            }
            else {
                edge_orientations[i] = 0;
                --i;
            }
    }
    return NULL;
}

} // End anonymous namespace

bool NTriangulation::isOriented() const {
    TetrahedronIterator it;

    // Calling isOrientable() will force a skeletal calculation if this
    // has not been done already.
    if(!isOrientable())
        return false;

    for(it = tetrahedra.begin(); it != tetrahedra.end(); ++it)
        if( (*it) -> tetOrientation != 1)
            return false;

    return true;
}

void NTriangulation::orient() {
    if (! calculatedSkeleton)
        calculateSkeleton();

    NIsomorphism flip_tets_iso(getNumberOfTetrahedra());

    TetrahedronIterator it;
    int t;
    for (t = 0, it = tetrahedra.begin(); it != tetrahedra.end(); ++it, ++t) {
        flip_tets_iso.tetImage(t) = t;
        if ((*it)->tetOrientation == 1 ||
                ! (*it)->getComponent()->isOrientable())
            flip_tets_iso.facePerm(t) = NPerm4(); // Identity
        else
            flip_tets_iso.facePerm(t) = NPerm4(2,3);
    }

    flip_tets_iso.applyInPlace(this);
}

bool NTriangulation::isOrdered() const {
    TetrahedronIterator it;

    for(it = tetrahedra.begin(); it != tetrahedra.end(); ++it)
        for(int face = 0; face < 4; face++)

            if((*it)->tetrahedra[face]) {
                NPerm4 perm = (*it) -> tetrahedronPerm[face];

                // check that the permuation is order preserving on the face
                int last = -1;
                for(int k = 0; k < 4; ++k)
                    if( k != face ) {
                        if(perm[k] < last)
                            return false;
                        last = perm[k];
                    }
            }
    return true;
}

bool NTriangulation::order(bool force_oriented)
{
    if(!calculatedSkeleton)
        calculateSkeleton();

    if(force_oriented && !isOrientable())
        return false;

    // find the isomorphism to order (and orient) the triangulation

    NIsomorphism* iso = ordering_iso(*this, force_oriented);
    if(!iso) return false;

    // apply the isomorphism

    iso -> applyInPlace(this);
    delete iso;

    // consistency check

    if(! isOrdered())
        reorder_fatal_error("NTriangulation::order returned unordered triangulation in reorder.cpp");
    if(force_oriented && ! isOriented())
        reorder_fatal_error("NTriangulation::order returned unoriented triangulation in reorder.cpp");

    return true;
}

}