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 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560
|
/**************************************************************************
* *
* 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 <queue>
#include "triangulation/ntriangulation.h"
#include "utilities/nrational.h"
namespace regina {
void NTriangulation::calculateSkeleton() const {
ideal = false;
valid = true;
orientable = true;
standard = true;
calculateComponents();
// Sets components, orientable, NComponent.orientable,
// NTetrahedron.component
calculateFaces();
// Sets faces, NFace.component
calculateVertices();
// Sets vertices, NVertex.component, NVertex.linkOrientable.
calculateEdges();
// Sets edges, NEdge.component, valid, NEdge.valid
calculateBoundary();
// Sets boundaryComponents, NFace.boundaryComponent,
// NEdge.boundaryComponent, NVertex.boundaryComponent,
// NComponent.boundaryComponents
calculateVertexLinks();
// Sets valid, ideal, NVertex.link,
// NVertex.linkEulerCharacteristic, NComponent.ideal,
// boundaryComponents, NVertex.boundaryComponent
calculatedSkeleton = true;
}
void NTriangulation::calculateComponents() const {
TetrahedronIterator it;
NComponent* label;
NTetrahedron* tet;
for (it = tetrahedra.begin(); it != tetrahedra.end(); it++)
(*it)->component = 0;
for (it = tetrahedra.begin(); it != tetrahedra.end(); it++) {
tet = *it;
if (tet->component == 0) {
label = new NComponent();
labelComponent(tet, label, 1);
components.push_back(label);
}
}
}
void NTriangulation::labelComponent(NTetrahedron* firstTet,
NComponent* component, int firstOrientation) const {
// Now non-recursive; uses a queue instead.
// The queue contains tetrahedra from which we need to propagate
// component labelling.
// Use plain C arrays for the queue. Since each tetrahedron is pushed
// on at most once, the array size does not need to be very large.
// Note that we have >= 1 tetrahedron, since firstTet != 0.
NTetrahedron** queue = new NTetrahedron*[tetrahedra.size()];
firstTet->component = component;
component->tetrahedra.push_back(firstTet);
firstTet->tetOrientation = firstOrientation;
unsigned queueStart = 0, queueEnd = 1;
queue[0] = firstTet;
NTetrahedron* tet;
NTetrahedron* adjTet;
int face;
int yourOrientation;
while (queueStart < queueEnd) {
tet = queue[queueStart++];
for (face=0; face<4; face++) {
adjTet = tet->getAdjacentTetrahedron(face);
if (adjTet) {
yourOrientation = (tet->getAdjacentTetrahedronGluing(face).
sign() == 1 ? -tet->tetOrientation : tet->tetOrientation);
if (adjTet->component) {
if (yourOrientation != adjTet->tetOrientation) {
orientable = false;
component->orientable = false;
}
} else {
adjTet->component = component;
component->tetrahedra.push_back(adjTet);
adjTet->tetOrientation = yourOrientation;
queue[queueEnd++] = adjTet;
}
}
}
}
delete[] queue;
}
void NTriangulation::calculateVertices() const {
TetrahedronIterator it;
int vertex;
NTetrahedron* tet;
NVertex* label;
for (it = tetrahedra.begin(); it != tetrahedra.end(); it++) {
tet = *it;
for (vertex=0; vertex<4; vertex++)
tet->vertices[vertex] = 0;
}
for (it = tetrahedra.begin(); it != tetrahedra.end(); it++) {
tet = *it;
for (vertex=0; vertex<4; vertex++)
if (! tet->getVertex(vertex)) {
label = new NVertex(tet->component);
tet->component->vertices.push_back(label);
labelVertex(tet, vertex, label, 1);
vertices.push_back(label);
}
}
}
void NTriangulation::labelVertex(NTetrahedron* firstTet, int firstVertex,
NVertex* label, int firstOrientation) const {
// Create a queue using simple arrays.
// Since each tetrahedron vertex is pushed on at most once, the
// array size does not need to be very large.
// Note that we have >= 1 tetrahedron, since firstTet != 0.
NTetrahedron** queueTet = new NTetrahedron*[tetrahedra.size() * 4];
int* queueVtx = new int[tetrahedra.size() * 4];
firstTet->vertices[firstVertex] = label;
firstTet->tmpOrientation[firstVertex] = firstOrientation;
label->embeddings.push_back(NVertexEmbedding(firstTet, firstVertex));
unsigned queueStart = 0, queueEnd = 1;
queueTet[0] = firstTet;
queueVtx[0] = firstVertex;
NTetrahedron* tet;
NTetrahedron* altTet;
int vertex;
int yourVertex;
int yourOrientation;
int yourFace;
int face;
while (queueStart < queueEnd) {
tet = queueTet[queueStart];
vertex = queueVtx[queueStart];
queueStart++;
for (face=0; face<4; face++) {
if (face == vertex) continue;
altTet = tet->getAdjacentTetrahedron(face);
if (altTet) {
yourVertex = tet->getAdjacentTetrahedronGluing(face)[vertex];
yourFace = tet->getAdjacentFace(face);
// We should actually be inverting faceOrdering(yourVertex).
// However, all we care about is the sign of the permutation,
// so let's save ourselves those extra few CPU cycles.
if ((faceOrdering(yourVertex) *
tet->getAdjacentTetrahedronGluing(face) *
faceOrdering(vertex)).sign() > 0)
yourOrientation = -(tet->tmpOrientation[vertex]);
else
yourOrientation = tet->tmpOrientation[vertex];
if (altTet->getVertex(yourVertex)) {
if (altTet->tmpOrientation[yourVertex] !=
yourOrientation)
label->linkOrientable = false;
} else {
altTet->vertices[yourVertex] = label;
altTet->tmpOrientation[yourVertex] = yourOrientation;
label->embeddings.push_back(NVertexEmbedding(altTet,
yourVertex));
queueTet[queueEnd] = altTet;
queueVtx[queueEnd] = yourVertex;
queueEnd++;
}
}
}
}
delete[] queueTet;
delete[] queueVtx;
}
void NTriangulation::calculateEdges() const {
TetrahedronIterator it;
int edge;
NTetrahedron* tet;
NEdge* label;
for (it = tetrahedra.begin(); it != tetrahedra.end(); it++) {
tet = *it;
for (edge=0; edge<6; edge++)
tet->edges[edge] = 0;
}
for (it = tetrahedra.begin(); it != tetrahedra.end(); it++) {
tet = *it;
for (edge=0; edge<6; edge++)
if (! tet->getEdge(edge)) {
label = new NEdge(tet->component);
tet->component->edges.push_back(label);
labelEdge(tet, edge, label, edgeOrdering(edge));
edges.push_back(label);
}
}
}
void NTriangulation::labelEdge(NTetrahedron* firstTet, int firstEdge,
NEdge* label, const NPerm& firstTetVertices) const {
// Since tetrahedron edges are joined together in a loop, the depth-first
// search is really just a straight line in either direction.
// We therefore do away with the usual stack/queue and just keep track
// of the next edge to process in the current direction.
firstTet->edges[firstEdge] = label;
firstTet->edgeMapping[firstEdge] = firstTetVertices;
label->embeddings.push_back(NEdgeEmbedding(firstTet, firstEdge));
// The last tetrahedron edge that was successfully processed.
NTetrahedron* tet;
int edge;
NPerm tetVertices;
int exitFace;
NPerm exitPerm;
// The next tetrahedron edge around from this.
NTetrahedron* nextTet;
int nextEdge;
NPerm nextVertices;
for (int dir = 0; dir < 2; dir++) {
// Start at the start and walk in one particular direction.
tet = firstTet;
edge = firstEdge;
tetVertices = tet->edgeMapping[edge];
while (true) {
// Move through to the next tetrahedron.
exitFace = tetVertices[dir == 0 ? 2 : 3];
nextTet = tet->getAdjacentTetrahedron(exitFace);
if (! nextTet)
break;
exitPerm = tet->getAdjacentTetrahedronGluing(exitFace);
nextVertices = exitPerm * tetVertices * NPerm(2, 3);
nextEdge = edgeNumber[nextVertices[0]][nextVertices[1]];
if (nextTet->edges[nextEdge]) {
// We looped right around.
// Check that we're not labelling the edge in reverse.
if (nextTet->edgeMapping[nextEdge][0] != nextVertices[0]) {
// The edge is being labelled in reverse!
label->valid = false;
valid = false;
}
break;
}
// We have a new tetrahedron edge; this needs to be labelled.
nextTet->edges[nextEdge] = label;
nextTet->edgeMapping[nextEdge] = nextVertices;
if (dir == 0)
label->embeddings.push_back(NEdgeEmbedding(nextTet, nextEdge));
else
label->embeddings.push_front(NEdgeEmbedding(nextTet, nextEdge));
tet = nextTet;
edge = nextEdge;
tetVertices = nextVertices;
}
}
}
void NTriangulation::calculateFaces() const {
TetrahedronIterator it;
int face;
NTetrahedron* tet;
NTetrahedron* adjTet;
NFace* label;
NPerm adjVertices;
int adjFace;
for (it = tetrahedra.begin(); it != tetrahedra.end(); it++) {
tet = *it;
for (face=0; face<4; face++)
tet->faces[face] = 0;
}
for (it = tetrahedra.begin(); it != tetrahedra.end(); it++) {
tet = *it;
for (face=3; face>=0; face--)
if (! tet->getFace(face)) {
label = new NFace(tet->component);
tet->component->faces.push_back(label);
tet->faces[face] = label;
tet->faceMapping[face] = faceOrdering(face);
label->embeddings[0] = new NFaceEmbedding(tet, face);
label->nEmbeddings = 1;
adjTet = tet->getAdjacentTetrahedron(face);
if (adjTet) {
// Face is not on the boundary.
adjFace = tet->getAdjacentFace(face);
adjVertices = (tet->getAdjacentTetrahedronGluing(face))*
(label->embeddings[0]->getVertices());
adjTet->faces[adjFace] = label;
adjTet->faceMapping[adjFace] = adjVertices;
label->embeddings[1] = new NFaceEmbedding(adjTet, adjFace);
label->nEmbeddings = 2;
}
faces.push_back(label);
}
}
}
void NTriangulation::calculateBoundary() const {
// Sets boundaryComponents, NFace.boundaryComponent,
// NEdge.boundaryComponent, NVertex.boundaryComponent,
// NComponent.boundaryComponents
FaceIterator it;
NFace* face;
NBoundaryComponent* label;
for (it = faces.begin(); it != faces.end(); it++) {
face = *it;
if (face->nEmbeddings < 2)
if (face->boundaryComponent == 0) {
label = new NBoundaryComponent();
label->orientable = true;
labelBoundaryFace(face, label, 1);
boundaryComponents.push_back(label);
face->component->boundaryComponents.push_back(label);
}
}
}
void NTriangulation::labelBoundaryFace(NFace* firstFace,
NBoundaryComponent* label, int firstOrientation) const {
std::queue<NFace*> faceQueue;
NFaceEmbedding* emb;
emb = firstFace->embeddings[0];
firstFace->boundaryComponent = label;
label->faces.push_back(firstFace);
emb->getTetrahedron()->tmpOrientation[emb->getFace()] = firstOrientation;
faceQueue.push(firstFace);
NTetrahedron* tet;
NPerm tetVertices;
int tetFace;
int i,j;
NVertex* vertex;
NEdge* edge;
NFace* face;
NFace* nextFace;
int nextFaceNumber;
NPerm nextFacePerm;
NTetrahedron* nextTet;
int followFromFace;
NPerm switchPerm;
int yourOrientation;
while (! faceQueue.empty()) {
face = faceQueue.front();
faceQueue.pop();
// Run through the edges and vertices on this face.
emb = face->embeddings[0];
tet = emb->getTetrahedron();
tetVertices = emb->getVertices();
tetFace = emb->getFace();
// Run through the vertices.
for (i=0; i<3; i++) {
vertex = tet->getVertex(tetVertices[i]);
if (vertex->boundaryComponent != label) {
vertex->boundaryComponent = label;
label->vertices.push_back(vertex);
}
}
// Run through the edges.
for (i=0; i<3; i++)
for (j=i+1; j<3; j++) {
edge = tet->getEdge(edgeNumber[tetVertices[i]][tetVertices[j]]);
if (! (edge->boundaryComponent)) {
edge->boundaryComponent = label;
label->edges.push_back(edge);
}
// Label the adjacent boundary face with the same label.
followFromFace = 6 - tetVertices[i] - tetVertices[j] - tetFace;
switchPerm = NPerm(followFromFace, tetFace);
nextFaceNumber = followFromFace;
nextFacePerm = NPerm();
nextTet = tet;
while (nextTet->getAdjacentTetrahedron(nextFaceNumber)) {
nextFacePerm = nextTet->getAdjacentTetrahedronGluing(
nextFaceNumber) * nextFacePerm * switchPerm;
nextTet = nextTet->getAdjacentTetrahedron(nextFaceNumber);
nextFaceNumber = nextFacePerm[followFromFace];
}
nextFace = nextTet->getFace(nextFaceNumber);
// Find the expected orientation of the next face.
yourOrientation =
(nextTet->getFaceMapping(nextFaceNumber).inverse() *
nextFacePerm * switchPerm * tet->getFaceMapping(tetFace))
.sign() == 1 ? -tet->tmpOrientation[tetFace] :
tet->tmpOrientation[tetFace];
if (nextFace->boundaryComponent) {
// Check the orientation.
if (yourOrientation !=
nextTet->tmpOrientation[nextFaceNumber])
label->orientable = false;
} else {
// Add this adjacent face to the queue.
nextFace->boundaryComponent = label;
label->faces.push_back(nextFace);
nextTet->tmpOrientation[nextFaceNumber] = yourOrientation;
faceQueue.push(nextFace);
}
}
}
}
void NTriangulation::calculateVertexLinks() const {
// Begin by calculating Euler characteristics.
// Here we use the formula: chi = (2 v_int + v_bdry - f) / 2, which
// is easily proven with a little arithmetic.
// Note that NVertex::linkEulerCharacteristic is initialised to 0 in
// the NVertex constructor.
// Begin by calculating (2 v_int + v_bdry) for each vertex link.
NEdge* e;
for (EdgeIterator eit = edges.begin(); eit != edges.end(); eit++) {
e = *eit;
if (e->isBoundary()) {
// Contribute to v_bdry.
e->getVertex(0)->linkEulerCharacteristic++;
if (e->valid)
e->getVertex(1)->linkEulerCharacteristic++;
} else {
// Contribute to 2 v_int.
e->getVertex(0)->linkEulerCharacteristic += 2;
if (e->valid)
e->getVertex(1)->linkEulerCharacteristic += 2;
}
}
// Run through each vertex and finalise Euler characteristic, link
// and more.
NVertex* vertex;
for (VertexIterator it = vertices.begin(); it != vertices.end(); it++) {
vertex = *it;
// Fix the Euler characteristic (subtract f, divide by two).
vertex->linkEulerCharacteristic = (vertex->linkEulerCharacteristic
- static_cast<long>(vertex->getEmbeddings().size())) / 2;
if (vertex->isBoundary()) {
// We haven't added ideal vertices to the boundary list yet,
// so this must be real boundary.
if (vertex->linkEulerCharacteristic == 1)
vertex->link = NVertex::DISC;
else {
vertex->link = NVertex::NON_STANDARD_BDRY;
valid = false;
standard = false;
}
} else {
if (vertex->linkEulerCharacteristic == 2)
vertex->link = NVertex::SPHERE;
else {
if (vertex->linkEulerCharacteristic == 0)
vertex->link = (vertex->isLinkOrientable() ?
NVertex::TORUS : NVertex::KLEIN_BOTTLE);
else {
vertex->link = NVertex::NON_STANDARD_CUSP;
standard = false;
}
ideal = true;
vertex->component->ideal = true;
NBoundaryComponent* bc = new NBoundaryComponent(vertex);
bc->orientable = vertex->isLinkOrientable();
vertex->boundaryComponent = bc;
boundaryComponents.push_back(bc);
vertex->component->boundaryComponents.push_back(bc);
}
}
}
}
void NTriangulation::calculateBoundaryProperties() const {
// Make sure the skeleton has been calculated!
if (! calculatedSkeleton)
calculateSkeleton();
bool localTwoSphereBoundaryComponents = false;
bool localNegativeIdealBoundaryComponents = false;
for (BoundaryComponentIterator it = boundaryComponents.begin();
it != boundaryComponents.end(); it++) {
if ((*it)->getEulerCharacteristic() == 2)
localTwoSphereBoundaryComponents = true;
else if ((*it)->isIdeal() && (*it)->getEulerCharacteristic() < 0)
localNegativeIdealBoundaryComponents = true;
// Stop the search if we've found everything we're looking for.
if (localTwoSphereBoundaryComponents &&
localNegativeIdealBoundaryComponents)
break;
}
twoSphereBoundaryComponents = localTwoSphereBoundaryComponents;
negativeIdealBoundaryComponents = localNegativeIdealBoundaryComponents;
}
} // namespace regina
|