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
|
/**************************************************************************
* *
* 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/>. *
* *
**************************************************************************/
/**
* 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/orb.h"
#include "triangulation/dim3.h"
#include "utilities/stringutils.h"
// This macro was originally provided in casson.h:
#define LN(ch) ((ch)=='u') ? 0 : (((ch)=='v') ? 1 : (((ch)=='w') ? 2 : 3))
namespace regina {
// Anonymous namespace for the private routines used only by this file.
namespace {
// Structures originally provided in casson.h:
struct TetEdgeInfo {
int tet_index,f1,f2;
TetEdgeInfo *prev,
*next;
};
struct EdgeInfo {
int index,
singular_index;
double singular_order;
TetEdgeInfo *head;
EdgeInfo *prev,
*next;
};
struct CassonFormat {
int num_tet;
EdgeInfo *head;
};
// Constants originally provided in casson.h:
const int vertex_at_faces[4][4] =
{{9,2,3,1},
{3,9,0,2},
{1,3,9,0},
{2,0,1,9}};
/**
* Modified from Orb's cassonToTriangulation() routine.
*
* The routine was changed to be compatible with Regina's Triangulation<3>
* data structure.
*
* If the conversion cannot be performed (e.g., the Casson format has
* inconsistent tetrahedron gluings), then this routine returns \c null.
*/
std::shared_ptr<PacketOf<Triangulation<3>>> cassonToTriangulation(
CassonFormat *cf ) {
int i;
auto triang = make_packet<Triangulation<3>>();
// since CassonFormat does not allow naming of triangulations,
// triang is given a name in the readOrb() function.
// I try to mimic Triangulation<3>::fromSnapPea 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;
auto* tet = new Tetrahedron<3>*[cf->num_tet]; // tet corresponds to tet_array in Orb
for (i=0; i<cf->num_tet; i++)
tet[i]=triang->newTetrahedron();
// now tet is a pointer to an array of Tetrahedron<3>s,
// so for each tet[i] we need to run
// for (j=0; j<4; j++)
// tet[i]->join(j,tet[g[j]],Perm<4>(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;
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) // if we have a non-trivial edge, proceed
{
tei1 = ei->head;
while (tei1) // now we spin about the tetrahedra adj to ei.
{
if (! tei1->next)
tei2 = ei->head;
else tei2 = tei1->next;
Tetrahedron<3>* t1 = tet[tei1->tet_index];
Tetrahedron<3>* t2 = tet[tei2->tet_index];
int a1 = tei1->f1;
int a2 = tei1->f2;
int b1 = tei2->f1;
int b2 = tei2->f2;
Perm<4> gluing(a1, b2, a2, b1,
vertex_at_faces[a1][a2], vertex_at_faces[b1][b2],
vertex_at_faces[a2][a1], vertex_at_faces[b2][b1]);
if (auto adj = t1->adjacentTetrahedron(tei1->f1)) {
if (adj != t2 || t1->adjacentGluing(tei1->f1) != gluing) {
// The tetrahedron gluings are inconsistent.
// The API promises to return null instead of
// throwing an exception, so we do this.
delete[] tet;
return {};
}
} else
t1->join(tei1->f1, t2, gluing);
tei1 = tei1->next;
}
ei = ei->next;
}
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;
bool vertices_known = false;
cf = new CassonFormat;
cf->head = nullptr;
cf->num_tet = 0;
// Skip any initial non-empty lines (looking whether there is
// "vertices_known") and then some empty lines.
// After that there should be the real information.
// The code from Orb used QString::skipWhiteSpace(); we do it
// manually.
do {
getline(ts, line);
stripWhitespace(line);
if (line == "vertices_known") vertices_known=true;
} while ((!ts.eof()) && (!line.empty()));
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)
cf->head = nei;
else ei->next = nei;
nei->next = nullptr;
nei->head = nullptr;
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;
// if vertices are listed, discard
if (vertices_known)
{ tokens >> section; tokens>>section; }
tokens >> section;
while (!section.empty())
{
ntei = new TetEdgeInfo;
if (! ei->head)
ei->head = ntei;
else
tei->next = ntei;
ntei->next = nullptr;
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)
return false;
while(ei)
{
tei = ei->head;
if (! tei)
return false;
while(tei)
{
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)
{
e2 = e1->next;
t1 = e1->head;
while (t1)
{
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 Triangulation<3>
* data structure, and to use standard C++ string and I/O streams
* instead of Qt strings and I/O streams.
*
* This routine returns \c null in case of error.
*/
std::shared_ptr<PacketOf<Triangulation<3>>> 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 {};
}
getline(ts, file_id);
CassonFormat* cf = readCassonFormat( ts );
if (! verifyCassonFormat( cf )) {
std::cerr << "Error verifying Orb / Casson file." << std::endl;
freeCassonFormat( cf );
return {};
}
auto manifold = cassonToTriangulation( cf );
freeCassonFormat( cf );
if (! manifold) {
std::cerr << "Orb / Casson file contains invalid or inconsistent data."
<< std::endl;
return {};
}
manifold->setLabel(file_id);
return manifold;
}
} // End anonymous namespace
std::shared_ptr<PacketOf<Triangulation<3>>> readOrb(const char *filename) {
std::ifstream file(filename);
if (! file) {
std::cerr << "Error opening Orb / Casson file." << std::endl;
return nullptr;
}
return readTriangulation(file);
}
} // namespace regina
|