File: orb.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 (351 lines) | stat: -rw-r--r-- 11,674 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

/**************************************************************************
 *                                                                        *
 *  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 */

/**
 * Thanks to Ryan Budney and Damien Heard for providing the bulk of the
 * code in this file.
 *
 * Ryan Budney contributed the Orb / Casson import filters for Regina.
 * From his initial comments:
 *
 *   This file contains the engine routines to read Casson/Orb format
 *   triangulations and import them into Regina.  The main routines are
 *   adapted from Damian Heard's Orb.
 *                                      -Ryan Budney (April 3rd, 2006)
 *
 * For information on which routines are adapted from Orb and how they
 * have been modified, see the comments above each routine.  For information
 * on Orb itself, see http://www.ms.unimelb.edu.au/~snap/orb.html .
 */

#include <cctype>
#include <cstdio>
#include <cstring>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <sstream>

#include "foreign/casson.h"
#include "foreign/orb.h"
#include "triangulation/ntriangulation.h"
#include "utilities/stringutils.h"

namespace regina {

// Anonymous namespace for the private routines used only by this file.
namespace {

/**
 * Modified from Orb's cassonToTriangulation() routine.
 *
 * The routine was changed to be compatible with Regina's NTriangulation
 * data structure.
 */
NTriangulation *cassonToNTriangulation( CassonFormat *cf )
 {
 int i;
 NTriangulation *triang = new NTriangulation();
 // since CassonFormat does not allow naming of triangulations,
 //  triang is given a name in the readOrb() function.
 //  I try to mimic NTriangulation::readSnapPea and
 //  Orb::cassonToTriangulation as much as possible.

 // If the triangulation is empty, leave now before we start allocating
 // empty arrays.
 if (cf->num_tet == 0)
     return triang;

 NTetrahedron **tet = new NTetrahedron*[cf->num_tet]; // tet corresponds to tet_array in Orb
 for (i=0; i<cf->num_tet; i++)
        tet[i]=new NTetrahedron();
 // now tet is a pointer to an array of NTetrahedrons,
 //  so for each tet[i] we need to run
 //   for (j=0; j<4; j++)
 //     tet[i]->joinTo(j,tet[g[j]],NPerm(p[j][0],p[j][1],p[j][2],p[j][3],p[j][4]))
 //     where g[j] is the tetrahedron adjacent to face j of tet[i]
 //           p[j][k] is the permutation specifying how the faces are glued together.
 EdgeInfo *ei;
 TetEdgeInfo *tei1, *tei2;
 int t1, t2, a1, a2, a3, a4, b1, b2, b3, b4;
 ei = cf->head;

 // this routine goes through the edges of cf, picking off the adjacent
 // tetrahedra and assembled the information into tet. this code is
 // adapted from Orb::cassonToTriangulation in Orb's organizer.cpp
 while (ei!=NULL) // if we have a non-trivial edge, proceed
        {
        tei1 = ei->head;
        while (tei1!=NULL) // now we spin about the tetrahedra adj to ei.
                {
                if (tei1->next==NULL)
                        tei2 = ei->head;
                else    tei2 = tei1->next;

                t1 = tei1->tet_index;
                a1 = tei1->f1;
                a2 = tei1->f2;
                a3 = vertex_at_faces[a1][a2];
                a4 = vertex_at_faces[a2][a1];

                t2 = tei2->tet_index;
                b1 = tei2->f1;
                b2 = tei2->f2;
                b3 = vertex_at_faces[b1][b2];
                b4 = vertex_at_faces[b2][b1];

                tet[t1]->joinTo( tei1->f1 , tet[t2], // 1st entry is the face of tet[t1]
                                NPerm(a1,b2,a2,b1,a3,b3,a4,b4) ); // being attached to tet[t2]

                tet[t2]->joinTo( tei2->f2 , tet[t1],
                                NPerm(b1,a2,b2,a1,b3,a3,b4,a4) );

                tei1 = tei1->next;
                }
        ei = ei->next;
        }

 for (i=0; i<cf->num_tet; i++)
        triang->addTetrahedron(tet[i]);
 delete[] tet;

 return triang;
}


/**
 * Modified from Orb's readCassonFormat() routine.
 *
 * This routine was modified to remove the dependence on Qt strings and
 * I/O streams, and work only with standard C++ strings and I/O streams
 * instead.
 *
 * On entering this routine we assume the lines containing "% orb" and
 * the manifold name have already been read.
 */
CassonFormat *readCassonFormat( std::istream &ts )
{
        CassonFormat    *cf;
        std::string     line,
                        section;
        EdgeInfo        *nei,
                        *ei;
        TetEdgeInfo     *ntei,
                        *tei;

        cf = new CassonFormat;
        cf->head = NULL;
        cf->num_tet = 0;

        // Find a non-empty line.
        // The code from Orb used QString::skipWhiteSpace(); we do it
        // manually.
        do {
                getline(ts, line);
                stripWhitespace(line);
        } while ((! ts.eof()) && line.empty());

        // Process lines one at a time until we hit an empty line or EOF.
        while ((! ts.eof()) && (! line.empty()) && (line != "% diagram"))
        {
                // The code from Orb used QString's record separation
                // routines.  We don't have that in std::string, so
                // we'll do it all with istringstreams instead.
                std::istringstream tokens(line);

                nei = new EdgeInfo;

                if (cf->head==NULL)
                        cf->head = nei;
                else    ei->next = nei;

                nei->next = NULL;
                nei->head = NULL;

                ei = nei;

                tokens >> ei->index;
                ei->index--;

                // We never use these two values; just suck them in and
                // forget them.
                tokens >> ei->singular_index >> ei->singular_order;

                tokens >> section;
                while (!section.empty())
                {
                        ntei = new TetEdgeInfo;

                        if (ei->head==NULL)
                                ei->head        = ntei;
                        else
                                tei->next       = ntei;

                        ntei->next = NULL;
                        tei = ntei;

                        tei->f1 = LN(section[section.length()-2]);
                        tei->f2 = LN(section[section.length()-1]);
                        section.resize(section.length()-2);
                        tei->tet_index = atoi(section.c_str()) - 1;

                        if (tei->tet_index + 1 > cf->num_tet)
                                cf->num_tet = tei->tet_index + 1;

                        section.clear();
                        tokens >> section;
                }

                getline(ts, line);
        }

        return cf;
}

/**
 * A direct copy of Orb's verifyCassonFormat() routine.
 */
bool verifyCassonFormat( CassonFormat *cf )
{
        int             i,j,k;
        bool            check[4][4];
        EdgeInfo        *ei;
        TetEdgeInfo     *tei;

        for(i=0;i<cf->num_tet;i++)
        {
                for(j=0;j<4;j++)
                        for(k=0;k<4;k++)
                        if (j==k)
                                check[j][k] = true;
                        else    check[j][k] = false;

                ei = cf->head;

                if (ei == NULL)
                        return false;

                while(ei!=NULL)
                {
                        tei = ei->head;
                        if (tei == NULL)
                                return false;
                        while(tei!=NULL)
                        {
                                if (tei->tet_index == i )
                                {
                                        if (check[tei->f1][tei->f2])
                                                return true;
                                        check[tei->f1][tei->f2] = true;
                                        check[tei->f2][tei->f1] = true;
                                }
                                tei = tei->next;
                        }
                        ei = ei->next;
                }

                for(j=0;j<4;j++)
                        for(k=0;k<4;k++)
                        if (check[j][k]==false)
                                return false;
        }
        return true;
}

/**
 * A direct copy of Orb's freeCassonFormat() routine.
 */
void freeCassonFormat( CassonFormat *cf )
{
        EdgeInfo *e1, *e2;
        TetEdgeInfo *t1, *t2;
        e1 = cf->head;

        while (e1!=NULL)
        {
                e2 = e1->next;
                t1 = e1->head;
                while (t1!=NULL)
                {
                        t2 = t1->next;
                        delete t1;
                        t1 = t2;
                }
                delete e1;
                e1 = e2;
        }
        delete cf;
}

/**
 * Modified from Orb's readTriangulation() routine.
 *
 * The routine was changed to be compatible with Regina's NTriangulation
 * data structure, and to use standard C++ string and I/O streams
 * instead of Qt strings and I/O streams.
 */
NTriangulation *readTriangulation( std::istream &ts) {
    std::string line, file_id;

    getline(ts, line);
    if (line != "% orb") {
        std::cerr << "Orb / Casson file is not in the correct format."
            << std::endl;
        return 0;
    }

    getline(ts, file_id);

    CassonFormat* cf = readCassonFormat( ts );
    if (! verifyCassonFormat( cf )) {
        std::cerr << "Error verifying Orb / Casson file." << std::endl;
        freeCassonFormat( cf );
        return 0;
    }

    NTriangulation* manifold = cassonToNTriangulation( cf );
    freeCassonFormat( cf );

    manifold->setPacketLabel(file_id);
    return manifold;
}

} // End anonymous namespace

NTriangulation *readOrb(const char *filename) {
    std::ifstream file(filename);

    if (! file) {
        std::cerr << "Error opening Orb / Casson file." << std::endl;
        return 0;
    }

    return readTriangulation(file);
}

} // namespace regina