File: csvsurfacelist.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 (269 lines) | stat: -rw-r--r-- 8,914 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

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

#include "foreign/csvsurfacelist.h"
#include "surfaces/nnormalsurfacelist.h"

namespace regina {

namespace {
    /**
     * Write the given string to the given output stream, properly
     * quoted and escaped.  The entire string will be placed in double
     * quotes, and any double quotes that appear inside the string will
     * be replaced by a pair of double quotes.
     */
    void writeCSVQuotedString(std::ostream& out, const char* str) {
        out << '"';
        while (*str) {
            if (*str == '"')
                out << "\"\"";
            else
                out << *str;
            ++str;
        }
        out << '"';
    }

    /**
     * Writes a piece of the CSV header corresponding to the given set
     * of optional fields.
     */
    void writePropHeader(std::ostream& out, int fields) {
        if (fields & surfaceExportName)
            out << "name,";
        if (fields & surfaceExportEuler)
            out << "euler,";
        if (fields & surfaceExportOrient)
            out << "orientable,";
        if (fields & surfaceExportSides)
            out << "sides,";
        if (fields & surfaceExportBdry)
            out << "boundary,";
        if (fields & surfaceExportLink)
            out << "link,";
        if (fields & surfaceExportType)
            out << "type,";
    }

    /**
     * Writes a piece of the CSV data for the given normal surface
     * corresponding to the given set of optional fields.
     */
    void writePropData(std::ostream& out, const NNormalSurface* s, int fields) {
        if (fields & surfaceExportName) {
            if (s->getName().length() > 0)
                writeCSVQuotedString(out, s->getName().c_str());
            out << ',';
        }
        if (fields & surfaceExportEuler) {
            if (s->isCompact())
                out << s->getEulerCharacteristic();
            out << ',';
        }
        if (fields & surfaceExportOrient) {
            if (s->isCompact()) {
                NTriBool ans = s->isOrientable();
                if (ans.isTrue())
                    out << "TRUE";
                else if (ans.isFalse())
                    out << "FALSE";
            }
            out << ',';
        }
        if (fields & surfaceExportSides) {
            if (s->isCompact()) {
                NTriBool ans = s->isTwoSided();
                if (ans.isTrue())
                    out << '2';
                else if (ans.isFalse())
                    out << '1';
            }
            out << ',';
        }
        if (fields & surfaceExportBdry) {
            if (! s->isCompact())
                out << "infinite";
            else if (s->hasRealBoundary())
                out << "real bdry";
            else
                out << "closed";
            out << ',';
        }
        if (fields & surfaceExportLink) {
            // Mirror the information that gets shown in the Link column
            // in the GUI.
            NTriangulation* t = s->getTriangulation();
            const NVertex* v = s->isVertexLink();
            if (v)
                out << "\"Vertex " << t->vertexIndex(v) << "\"";
            else {
                std::pair<const regina::NEdge*, const regina::NEdge*> e =
                    s->isThinEdgeLink();
                if (e.second)
                    out << "\"Thin edges " << t->edgeIndex(e.first)
                        << ", " << t->edgeIndex(e.second) << "\"";
                else if (e.first)
                    out << "\"Thin edge " << t->edgeIndex(e.first) << "\"";
            }
            out << ',';
        }
        if (fields & surfaceExportType) {
            // Mirror the information that gets shown in the Type column
            // in the GUI.
            if (s->isSplitting())
                out << "\"Splitting\"";
            else {
                NLargeInteger tot = s->isCentral();
                if (tot != 0)
                    out << "\"Central (" << tot << ")\"";
            }
            out << ',';
        }
    }
}

bool writeCSVStandard(const char* filename, NNormalSurfaceList& surfaces,
        int additionalFields) {
    std::ofstream out(filename);
    if (! out)
        return false;

    unsigned long n = surfaces.getTriangulation()->getNumberOfTetrahedra();

    unsigned long i, j;

    // Write the CSV header.
    writePropHeader(out, additionalFields);
    for (i = 0; i < n; ++i) {
        out << 'T' << i << ":0,";
        out << 'T' << i << ":1,";
        out << 'T' << i << ":2,";
        out << 'T' << i << ":3,";
        out << 'Q' << i << ":01/23,";
        out << 'Q' << i << ":02/13,";
        out << 'Q' << i << ":03/12";

        if (! surfaces.allowsAlmostNormal()) {
            if (i < n - 1)
                out << ',';
            continue;
        }
        out << ',';

        out << 'K' << i << ":01/23,";
        out << 'K' << i << ":02/13,";
        out << 'K' << i << ":03/12";

        if (i < n - 1)
            out << ',';
    }
    out << std::endl;

    // Write the data for individual surfaces.
    unsigned long tot = surfaces.getNumberOfSurfaces();
    const NNormalSurface* s;
    for (i = 0; i < tot; ++i) {
        s = surfaces.getSurface(i);

        writePropData(out, s, additionalFields);

        for (j = 0; j < n; ++j) {
            out << s->getTriangleCoord(j, 0) << ',';
            out << s->getTriangleCoord(j, 1) << ',';
            out << s->getTriangleCoord(j, 2) << ',';
            out << s->getTriangleCoord(j, 3) << ',';
            out << s->getQuadCoord(j, 0) << ',';
            out << s->getQuadCoord(j, 1) << ',';
            out << s->getQuadCoord(j, 2);

            if (! surfaces.allowsAlmostNormal()) {
                if (j < n - 1)
                    out << ',';
                continue;
            }
            out << ',';

            out << s->getOctCoord(j, 0) << ',';
            out << s->getOctCoord(j, 1) << ',';
            out << s->getOctCoord(j, 2);

            if (j < n - 1)
                out << ',';
        }
        out << std::endl;
    }

    // All done!
    return true;
}

bool writeCSVEdgeWeight(const char* filename, NNormalSurfaceList& surfaces,
        int additionalFields) {
    std::ofstream out(filename);
    if (! out)
        return false;

    unsigned long n = surfaces.getTriangulation()->getNumberOfEdges();

    unsigned long i, j;

    // Write the CSV header.
    writePropHeader(out, additionalFields);
    for (i = 0; i < n; ++i) {
        out << 'E' << i;

        if (i < n - 1)
            out << ',';
    }
    out << std::endl;

    // Write the data for individual surfaces.
    unsigned long tot = surfaces.getNumberOfSurfaces();
    const NNormalSurface* s;
    for (i = 0; i < tot; ++i) {
        s = surfaces.getSurface(i);

        writePropData(out, s, additionalFields);

        for (j = 0; j < n; ++j) {
            out << s->getEdgeWeight(j);

            if (j < n - 1)
                out << ',';
        }
        out << std::endl;
    }

    // All done!
    return true;
}

} // namespace regina