File: knotplot.cpp

package info (click to toggle)
regina-normal 7.4.1-1.1
  • links: PTS
  • area: main
  • in suites: forky, sid
  • size: 154,244 kB
  • sloc: cpp: 295,026; xml: 9,992; sh: 1,344; python: 1,225; perl: 616; ansic: 138; makefile: 26
file content (421 lines) | stat: -rw-r--r-- 19,447 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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421

/**************************************************************************
 *                                                                        *
 *  Regina - A Normal Surface Theory Calculator                           *
 *  Computational Engine                                                  *
 *                                                                        *
 *  Copyright (c) 1999-2025, 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.                       *
 *                                                                        *
 *  As an exception, when this program is distributed through (i) the     *
 *  App Store by Apple Inc.; (ii) the Mac App Store by Apple Inc.; or     *
 *  (iii) Google Play by Google Inc., then that store may impose any      *
 *  digital rights management, device limits and/or redistribution        *
 *  restrictions that are required by its terms of service.               *
 *                                                                        *
 *  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, see <https://www.gnu.org/licenses/>. *
 *                                                                        *
 **************************************************************************/

#include <algorithm>
#include <bit> // for std::endian
#include <cctype>
#include <cstring>
#include <fstream>
#include <iostream>
#include "link/spatiallink.h"
#include "utilities/exception.h"

// Converts two successive bytes into a single unsigned 16-bit integer, where
// the input is treated as big-endian (regardless of the endianness of the
// current platform).
//
// PRE: the array c has length at least 2.
static constexpr uint16_t KPInt16(const char c[]) {
    // The double-casts are to ensure that, if char is signed, then
    // -1 casts up to 255 (and not 65535).
    return (uint16_t(uint8_t(c[0])) << 8) |
        (uint16_t(uint8_t(c[1])));
}

// Converts four successive bytes into a single unsigned 32-bit integer, where
// the input is treated as big-endian (regardless of the endianness of the
// current platform).
//
// PRE: the array c has length at least 4.
static constexpr uint32_t KPInt32(const char c[]) {
    // The double-casts are to ensure that, if char is signed, then
    // -1 casts up to 255 (and not 65535).
    return (uint32_t(uint8_t(c[0])) << 24) |
        (uint32_t(uint8_t(c[1])) << 16) |
        (uint32_t(uint8_t(c[2])) << 8) |
        (uint32_t(uint8_t(c[3])));
}

// Converts four successive bytes into a single 32-bit float, where the input
// is treated as big-endian (regardless of the endianness of the current
// platform).
//
// THIS NEEDS TESTING ON DIFFERENT PLATFORMS.  I have seen a promise (on
// cppreference.com) that float and double should use IEEE-754 binary32 and
// IEEE-754 binary64 respectively.
//
// However, I believe endianness still matters.  My own main platforms are
// all little-endian.
//
// PRE: the array c has length at least 4.
float KPFloat32(const char c[]) {
    static_assert(std::endian::native == std::endian::big ||
        std::endian::native == std::endian::little,
        "Only big-endian and little-endian platforms are supported.");

    if constexpr (std::endian::native == std::endian::big) {
        return *(reinterpret_cast<const float*>(c));
    } else {
        const char x[4] = { c[3], c[2], c[1], c[0] };
        return *(reinterpret_cast<const float*>(x));
    }
}

// Converts eight successive bytes into a single 64-bit double, where the input
// is treated as big-endian (regardless of the endianness of the current
// platform).
//
// THIS NEEDS TESTING ON DIFFERENT PLATFORMS.  See the comments above for
// KPFloat32().
//
// PRE: the array c has length at least 8.
double KPFloat64(const char c[]) {
    static_assert(std::endian::native == std::endian::big ||
        std::endian::native == std::endian::little,
        "Only big-endian and little-endian platforms are supported.");

    if constexpr (std::endian::native == std::endian::big) {
        return *(reinterpret_cast<const double*>(c));
    } else {
        const char x[8] = { c[7], c[6], c[5], c[4], c[3], c[2], c[1], c[0] };
        return *(reinterpret_cast<const double*>(x));
    }
}

namespace regina {

SpatialLink SpatialLink::fromKnotPlot(const char* filename) {
    // When we move to C++23, I think we get access to fixed-size floating-point
    // types.  I should check this.
    if (sizeof(float) != 4 || sizeof(double) != 8)
        throw NotImplemented("fromKnotPlot(): binary file format requires "
            "a platform with 32-bit floats and 64-bit doubles");

    std::ifstream in(filename, std::ios::binary);
    if (! in)
        throw FileError("fromKnotPlot(): could not open the given file");

    // The file _must_ begin with "KnotPlot 1.0".
    char banner[12];
    in.read(banner, 12);
    if (! in)
        throw InvalidInput("fromKnotPlot(): unexpected end of file");
    if (std::memcmp(banner, "KnotPlot 1.0", 12) != 0)
        throw InvalidInput("fromKnotPlot(): file has no KnotPlot header");

    // The header ends with '\f' followed by another arbitrary character.
    in.ignore(std::numeric_limits<std::streamsize>::max(), '\f');
    if (! in)
        throw InvalidInput("fromKnotPlot(): unexpected end of file");
    in.get();
    if (! in)
        throw InvalidInput("fromKnotPlot(): unexpected end of file");

    SpatialLink ans;

    bool finished = false;
    while (! finished) {
        // Extract the next field.
        char field[4];
        in.read(field, 4);
        if (! in)
            throw InvalidInput("fromKnotPlot(): unexpected end of file");

        if (std::islower(field[0])) {
            if (std::islower(field[1])) {
                // This field contains no data at all.
                switch (KPInt32(field)) {
                    case KPInt32("endf"):
                        // End of data file.
                        finished = true;
                        break;
                    case KPInt32("comp"):
                        ans.components_.emplace_back();
                        break;
                }
            } else
                throw InvalidInput("fromKnotPlot(): invalid field name");
        } else if (std::isupper(field[0])) {
            if (std::islower(field[1])) {
                // This field contains exactly 4 bytes of data.
                char data[4];
                in.read(data, 4);
                if (! in)
                    throw InvalidInput(
                        "fromKnotPlot(): unexpected end of file");

                switch (KPInt32(field)) {
                    case KPInt32("Attr"):
                        // Attributes are stored as a 4-byte integer:
                        // the lowest order bit is 1 for closed, or 0 for open.
                        // If the attributes are missing entirely then the
                        // component is assumed to be closed.
                        uint32_t attr = KPInt32(data);
                        if (! (attr & 1))
                            throw InvalidInput("fromKnotPlot(): file contains "
                                "an open link component, with free ends");
                        break;
                }
            } else if (std::isupper(field[1])) {
                // This field contains a 4-byte integer indicating how many
                // _subsequent_ bytes the field contains.
                char lenData[4];
                in.read(lenData, 4);
                if (! in)
                    throw InvalidInput("fromKnotPlot(): "
                        "unexpected end of file");

                uint32_t len = KPInt32(lenData);

                switch (KPInt32(field)) {
#if 0
                    case KPInt32("NAME"):
                        // Name of the knot/link.
                        // For now we do not actually use this.
                        {
                            char* data = new char[len];
                            in.read(data, len);
                            if (! in) {
                                delete [] data;
                                throw InvalidInput("fromKnotPlot(): "
                                    "unexpected end of file");
                            }
                            std::string name(data, len);
                            delete[] data;
                            // ... send name to wherever it needs to go.
                        }
                        break;
#endif
                    case KPInt32("LOCS"):
                        // 2-byte unsigned integers for coordinates.
                        // Preceeded by scale and offset data; I am assuming
                        // based on inspecting some KnotPlot sample files that
                        // these are stored as four 4-byte floats (scale,
                        // offset_x, offset_y, offset_z).
                        if (ans.components_.empty())
                            throw InvalidInput("fromKnotPlot(): found "
                                "coordinates before the first component");
                        if (len % 6 != 4)
                            throw InvalidInput("fromKnotPlot(): invalid "
                                "LOCS field length");

                        {
                            char data[16];

                            in.read(data, 16);
                            if (! in)
                                throw InvalidInput(
                                    "fromKnotPlot(): unexpected end of file");

                            float scale = KPFloat32(data);
                            float offset[3] = {
                                KPFloat32(data + 4),
                                KPFloat32(data + 8),
                                KPFloat32(data + 12) };

                            for (size_t i = 16; i < len; i += 6) {
                                in.read(data, 6);
                                if (! in)
                                    throw InvalidInput("fromKnotPlot(): "
                                        "unexpected end of file");

                                // Note: we already tested above that
                                // components_ is non-empty.
                                ans.components_.back().emplace_back(
                                    float(KPInt16(data)) * scale + offset[0],
                                    float(KPInt16(data + 2)) * scale +
                                        offset[1],
                                    float(KPInt16(data + 4)) * scale +
                                        offset[2]);
                            }
                        }
                        break;
                    case KPInt32("LOCF"):
                        // 4-byte floats for coordinates.
                        if (ans.components_.empty())
                            throw InvalidInput("fromKnotPlot(): found "
                                "coordinates before the first component");
                        if (len % 12 != 0)
                            throw InvalidInput("fromKnotPlot(): invalid "
                                "LOCF field length");

                        {
                            char data[12];
                            for (size_t i = 0; i < len; i += 12) {
                                in.read(data, 12);
                                if (! in)
                                    throw InvalidInput("fromKnotPlot(): "
                                        "unexpected end of file");

                                // Note: we already tested above that
                                // components_ is non-empty.
                                ans.components_.back().emplace_back(
                                    KPFloat32(data),
                                    KPFloat32(data + 4),
                                    KPFloat32(data + 8));
                            }
                        }
                        break;
                    case KPInt32("LOCD"):
                        // 8-byte doubles for coordinates.
                        if (ans.components_.empty())
                            throw InvalidInput("fromKnotPlot(): found "
                                "coordinates before the first component");
                        if (len % 24 != 0)
                            throw InvalidInput("fromKnotPlot(): invalid "
                                "LOCD field length");

                        {
                            char data[24];
                            for (size_t i = 0; i < len; i += 24) {
                                in.read(data, 24);
                                if (! in)
                                    throw InvalidInput("fromKnotPlot(): "
                                        "unexpected end of file");

                                // Note: we already tested above that
                                // components_ is non-empty.
                                ans.components_.back().emplace_back(
                                    KPFloat64(data),
                                    KPFloat64(data + 8),
                                    KPFloat64(data + 16));
                            }
                        }
                        break;
                    case KPInt32("LOCC"):
                        // This format is not documented alongside the others,
                        // and I'm not convinced I'm interpreting it correctly.
                        // For now we explicitly disable it until such a time
                        // as we can find out exactly what it is meant to store.
                        //
                        // Below is the code for what I _thought_ it was meant
                        // to hold, but some tests on KnotPlot files from the
                        // wild suggest this is not actually what's going on.
                        //
                        // The good news: AFAICT this is used more often with
                        // open paths (not closed loops), which we do not
                        // support anyway.
                        throw InvalidInput("fromKnotPlot(): found a block "
                            "of translations (not coordinates), which "
                            "are not currently supported");
#if 0
                        // This appears to hold a series of translations,
                        // not absolute coordinates.
                        // The translations use signed 1-byte integers for
                        // coordinates.
                        // Before the translations we have two triples of
                        // 4-byte floats:
                        // - scaling factors for x, y, z translations;
                        // - the starting coordinates.
                        if (ans.components_.empty())
                            throw InvalidInput("fromKnotPlot(): found "
                                "coordinates before the first component");
                        if (len % 3 != 0)
                            throw InvalidInput("fromKnotPlot(): invalid "
                                "LOCC field length");

                        {
                            char data[12];

                            in.read(data, 12);
                            if (! in)
                                throw InvalidInput(
                                    "fromKnotPlot(): unexpected end of file");
                            float scale[3] = {
                                KPFloat32(data + 4),
                                KPFloat32(data + 8),
                                KPFloat32(data + 12) };

                            in.read(data, 12);
                            if (! in)
                                throw InvalidInput(
                                    "fromKnotPlot(): unexpected end of file");
                            Node pos(
                                KPFloat32(data + 4),
                                KPFloat32(data + 8),
                                KPFloat32(data + 12));
                            // Note: we already tested above that
                            // components_ is non-empty.
                            ans.components_.back().push_back(pos);

                            for (size_t i = 24; i < len; i += 3) {
                                in.read(data, 3);
                                if (! in)
                                    throw InvalidInput("fromKnotPlot(): "
                                        "unexpected end of file");

                                // Cast from char to int8_t, because we have
                                // no guarantee that char is signed.
                                pos.x += (float(int8_t(data[0])) * scale[0]);
                                pos.y += (float(int8_t(data[1])) * scale[1]);
                                pos.z += (float(int8_t(data[2])) * scale[2]);
                                ans.components_.back().push_back(pos);
                            }
                        }
                        break;
#endif
                    /*
                    case KPInt32("COLR"):
                        // This holds an RGB triple specifying the colour of
                        // the current component.  We might wish to support
                        // this at some later date.
                    */
                    default:
                        // Skip over the remainder of this field.
                        in.ignore(len);
                        if (! in)
                            throw InvalidInput(
                                "fromKnotPlot(): unexpected end of file");
                        break;
                }
            } else
                throw InvalidInput("fromKnotPlot(): invalid field name");
        } else
            throw InvalidInput("fromKnotPlot(): invalid field name");
    }

    if (ans.components_.empty()) {
        // Assume the file used some other method of storing coordinates, and
        // that we were not able to read it.
        throw InvalidInput("fromKnotPlot(): no coordinates could be read");
    }

    // A final basic sanity check: to be embedded, each component must have at
    // least three nodes.
    for (const auto& c : ans.components_)
        if (c.size() < 3)
            throw InvalidInput("fromKnotPlot(): read a component with "
                "< 3 nodes");

    return ans;
}

} // namespace regina