File: orient.c

package info (click to toggle)
snappea 3.0d3-20.1
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 5,896 kB
  • ctags: 3,582
  • sloc: ansic: 33,469; sh: 8,293; python: 7,623; makefile: 240
file content (860 lines) | stat: -rw-r--r-- 21,531 bytes parent folder | download | duplicates (11)
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
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
/*
 *	orient.c
 *
 *	This file provides the functions
 *
 *		void orient(Triangulation *manifold);
 *		void reorient(Triangulation *manifold);
 *		void fix_peripheral_orientations(Triangulation *manifold);
 *
 *	orient() attempts to consistently orient the Tetrahedra of the
 *	Triangulation *manifold.  It begins with manifold->tet_list_begin.next
 *	and recursively reorients its neighbors as necessary until either
 *	all the tetrahedra are consistently oriented, or it discovers that
 *	the manifold is nonorientable.  (In particular, the Orientation of
 *	an orientable Triangulation will match the initial Orientation of
 *	manifold->tet_list_begin.next, which is important in subdivide.c and
 *	terse_triangulation.c.)  orient() sets the manifold->orientability
 *	field to oriented_manifold or nonorientable_manifold, as appropriate.
 *	Its method for reorienting a tetrahedron is to swap the indices
 *	of vertices 2 and 3, and adjust the relevant fields accordingly
 *	(including the gluing fields of neighboring tetrahedra).
 *	It is aware of the fact that some fields may not yet be set,
 *	and doesn't try to modify data structures if they aren't present
 *	(all it requires are the neighbor and gluing fields).
 *
 *	If the manifold is orientable, then
 *
 *	(1)	All peripheral curves (i.e. the meridians and longitudes)
 *		are transferred to the right_handed sheets of the orientation
 *		double covers of the cusps.  This makes their holonomies complex
 *		analytic functions of the tetrahedron shapes, rather than
 *		complex conjugates of complex analytic functions.  (However,
 *		the {meridian, longitude} pair may no longer be right-handed.
 *		To fix that, call fix_peripheral_orientations().)
 *
 *	(2)	All edge_orientations are set to right_handed.
 *
 *
 *	orient() and orient_edge_classes() may be called in either order.
 *
 *	If orient() is called first, then
 *		If the manifold is orientable,
 *			orient() will set all edge_orientations to right_handed, and then
 *			orient_edge_classes() will (redundantly) do the same thing.
 *		If the manifold is nonorientable,
 *			orient() won't set the edge_orientations, but
 *			orient_edge_classes() will.
 *
 *	If orient_edge_classes() is called first, then
 *		If the manifold is orientable,
 *			orient_edge_classes() will assign an arbitrary Orientation
 *				to each EdgeClass, and then
 *			orient() will overwrite it with the right_handed Orientation.
 *		If the manifold is nonorientable,
 *			orient_edge_classes() will assign an arbitrary Orientation
 *				to each EdgeClass, and
 *			orient() will preserve it.
 *
 *
 *	orient() could be made available to the UI with no modifications
 *	beyond moving the prototype.
 *
 *
 *	reorient() reverses the orientation of all the Tetrahedra
 *	in the Triangulation, by swapping VertexIndices 2 and 3 as described
 *	above.  If the manifold is orientable, reorient() also
 *	transfers the peripheral curves to the right_handed sheets of the
 *	double covers and sets all edge_orientations to right_handed, as
 *	described above.  It reverses the directions of all meridians, so the
 *	peripheral curves will continue to adhere to the standard orientation
 *	convention.  reorient() is intended for oriented manifolds, but
 *	nothing terrible will happen if you pass it a nonorientable manifold.
 *
 *	fix_peripheral_orientations() makes sure each {meridian, longitude}
 *	pairs obeys the right-hand rule.  It should be called only for
 *	orientable manifolds, typically following a call to orient().
 *
 *	Note to myself:  Eventually I may want to make transfer_peripheral_curves()
 *	responsible for making the peripheral curves adhere to the standard
 *	orientation convention.  If this is done, reorient() won't have to
 *	explicitly change the directions of meridians.  (However, this
 *	change would require that orient() check whether Cusps are present.)
 */

#include "kernel.h"

static void				reverse_orientation(Tetrahedron *tet);
static void				renumber_neighbors_and_gluings(Tetrahedron *tet);
static void				renumber_cusps(Tetrahedron *tet);
static void				renumber_peripheral_curves(Tetrahedron *tet);
static void				renumber_edge_classes(Tetrahedron *tet);
static void				renumber_shapes(Tetrahedron *tet);
static void				renumber_shape_histories(Tetrahedron *tet);
static void				renumber_one_part(ComplexWithLog edge_parameters[3]);
static void				swap_rows(int m[4][4], int a, int b);
static void				swap_columns(int m[4][4], int a, int b);
static void				swap_sheets(int m[2][4][4]);
static void				transfer_peripheral_curves(Triangulation *manifold);
static void				make_all_edge_orientations_right_handed(Triangulation *manifold);
static void				reverse_all_meridians(Triangulation *manifold);


void orient(
	Triangulation	*manifold)
{
	/*
	 *	Pick an arbitrary initial Tetrahedron,
	 *	and try to extend its orientation to the whole manifold.
	 */
	extend_orientation(manifold, manifold->tet_list_begin.next);
}


void extend_orientation(
	Triangulation	*manifold,
	Tetrahedron		*initial_tet)
{
	Tetrahedron	**queue,
				*tet;
	int			queue_first,
				queue_last;
	FaceIndex	f;

	/*
	 *	Set all the tet->flag fields to FALSE,
	 *	to show that no Tetrahedra have been visited.
	 */

	for (tet = manifold->tet_list_begin.next;
		 tet != &manifold->tet_list_end;
		 tet = tet->next)

		tet->flag = FALSE;

	/*
	 *	Tentatively assume the manifold is orientable.
	 */
	manifold->orientability = oriented_manifold;

	/*
	 *	Allocate space for a queue of pointers to the Tetrahedra.
	 *	Each Tetrahedron will appear on the queue exactly once,
	 *	so an array of length manifold->num_tetrahedra will be just right.
	 */
	queue = NEW_ARRAY(manifold->num_tetrahedra, Tetrahedron *);

	/*
	 *	Put the initial Tetrahedron on the queue,
	 *	and mark it as visited.
	 */
	queue_first = 0;
	queue_last  = 0;
	queue[0] = initial_tet;
	queue[0]->flag = TRUE;

	/*
	 *	Start processing the queue.
	 */
	do
	{
		/*
		 *	Pull a Tetrahedron off the front of the queue.
		 */
		tet = queue[queue_first++];

		/*
		 *	Look at the four neighboring Tetrahedra.
		 */
		for (f = 0; f < 4; f++)
		{
			/*
			 *	If the neighbor hasn't been visited...
			 */
			if (tet->neighbor[f]->flag == FALSE)
			{
				/*
				 *	...reverse its orientation if necessary...
				 */
				if (parity[tet->gluing[f]] == orientation_reversing)
					reverse_orientation(tet->neighbor[f]);

				/*
				 *	...mark it as visited...
				 */
				tet->neighbor[f]->flag = TRUE;

				/*
				 *	...and put it on the back of the queue.
				 */
				queue[++queue_last] = tet->neighbor[f];
			}
			/*
			 *	If the neighbor has been visited . . .
			 */
			else
			{
				/*
				 *	...check whether its orientation is consistent.
				 */
				if (parity[tet->gluing[f]] == orientation_reversing)
					manifold->orientability = nonorientable_manifold;
			}
		}
	}
	/*
	 *	Keep going until either we discover the manifold is nonorientable,
	 *	or the queue is exhausted.
	 */
	while (manifold->orientability == oriented_manifold
		&& queue_first <= queue_last);

	/*
	 *	Free the memory used for the queue.
	 */
	my_free(queue);	

	/*
	 *	An "unnecessary" (but quick) error check.
	 */
	if (manifold->orientability == oriented_manifold
	 && (	queue_first != manifold->num_tetrahedra
		 || queue_last  != manifold->num_tetrahedra - 1))
		uFatalError("orient", "orient");

	/*
	 *	Another error check.
	 *	We should have oriented a manifold before attempting to
	 *	compute the Chern-Simons invariant.
	 */
	if (manifold->CS_value_is_known || manifold->CS_fudge_is_known)
		uFatalError("orient", "orient");

	/*
	 *	Respect the conventions for peripheral curves and
	 *	edge orientations in oriented manifolds.
	 */
	if (manifold->orientability == oriented_manifold)
	{
		transfer_peripheral_curves(manifold);
		make_all_edge_orientations_right_handed(manifold);
	}
}


/*
 *	reverse_orientation() reverses the orientation of a Tetrahedron
 *	by swapping the indices of vertices 2 and 3.  It adjusts all
 *	relevant fields, including the gluing fields of neighboring
 *	tetrahedra.
 */

static void reverse_orientation(
	Tetrahedron	*tet)
{
	renumber_neighbors_and_gluings(tet);
	renumber_cusps(tet);
	renumber_peripheral_curves(tet);
	renumber_edge_classes(tet);
	renumber_shapes(tet);
	renumber_shape_histories(tet);
}


static void renumber_neighbors_and_gluings(
	Tetrahedron	*tet)
{
	Tetrahedron	*temp_neighbor;
	Permutation	temp_gluing;
	int			i,
				j,
				d[4],
				temp_digit;
	Tetrahedron	*nbr_tet;


	/*
	 *	Renumbering the neighbors is easy:  we simply swap
	 *	neighbor[2] and neighbor[3].
	 */

	temp_neighbor		= tet->neighbor[2];
	tet->neighbor[2]	= tet->neighbor[3];
	tet->neighbor[3]	= temp_neighbor;

	/*
	 *	Renumbering the gluings is trickier, because three
	 *	changes are required:
	 *
	 *		Change A:	Swap gluing[2] and gluing[3].
	 *
	 *		Change B:	Within each gluing of tet, swap the image of
	 *					vertex 2 and the image of vertex 3, e.g. 0312 -> 3012.
	 *
	 *		Change C:	For each gluing of a face (typically of a Tetrahedron
	 *					other than tet) that glues to tet, interchange
	 *					2 and 3, e.g. 0312 -> 0213.
	 */

	/*
	 *	Change A:  Swap gluing[2] and gluing[3].
	 */

	temp_gluing		= tet->gluing[2];
	tet->gluing[2]	= tet->gluing[3];
	tet->gluing[3]	= temp_gluing;


	/*
	 *	Changes B and C are carried out for each of the four gluings of tet.
	 */

	for (i = 0; i < 4; i++)
	{
		/*
		 *	Change B:  Swap the image of vertex 2 and the image of vertex 3.
		 */

		/*
		 *	Unpack the digits of the gluing.
		 */
		for (j = 0; j < 4; j++)
		{
			d[j] = tet->gluing[i] & 0x3;
			tet->gluing[i] >>= 2;
		}

		/*
		 *	Swap the digits in positions 2 and 3.
		 */
		temp_digit	= d[3];
		d[3]		= d[2];
		d[2]		= temp_digit;

		/*
		 *	Repack the digits.
		 */
		for (j = 4; --j >= 0; )
		{
			tet->gluing[i] <<= 2;
			tet->gluing[i] += d[j];
		}

		/*
		 *	Change C:  Fix up the inverse of tet->gluing[i].
		 *
		 *	If tet->neighbor[i] != tet, we simply write the inverse of
		 *	tet->gluing[i] into the appropriate gluing field of the neighbor.
		 *
		 *	If tet->neighbor[i] == tet, the simple approach doesn't work
		 *	because of the messy interaction between Changes B and C.
		 *	Instead, we exploit the fact that tet->gluing[i] is the inverse
		 *	of some other gluing of tet, and apply Change C directly to
		 *	tet->gluing[i] rather than its inverse.
		 */

		nbr_tet = tet->neighbor[i];

		if (nbr_tet != tet)

			/*
			 *	Write the inverse directly.
			 */

			nbr_tet->gluing[EVALUATE(tet->gluing[i],i)] = inverse_permutation[tet->gluing[i]];

		else
		{
			/*
			 *	Perform Change C on tet->gluing[i].
			 */

			/*
			 *	Unpack the digits.
			 */
			for (j = 0; j < 4; j++)
			{
				d[j] = tet->gluing[i] & 0x3;
				tet->gluing[i] >>= 2;
			}

			/*
			 *	Swap 2 and 3 in the images.
			 */
			for (j = 0; j < 4; j++)
				switch (d[j])
				{
					case 0:
					case 1:
						/* leave d[j] alone */
						break;
					case 2:
						d[j] = 3;
						break;
					case 3:
						d[j] = 2;
						break;
				}

			/*
			 *	Repack the digits.
			 */
			for (j = 4; --j >= 0; )
			{
				tet->gluing[i] <<= 2;
				tet->gluing[i] += d[j];
			}

		}
	}
}


static void renumber_cusps(
	Tetrahedron	*tet)
{
	Cusp		*temp_cusp;

	temp_cusp		= tet->cusp[2];
	tet->cusp[2]	= tet->cusp[3];
	tet->cusp[3]	= temp_cusp;
}


static void renumber_peripheral_curves(
	Tetrahedron	*tet)
{
	int	i,
		j;

	for (i = 0; i < 2; i++)
	{
		for (j = 0; j < 2; j++)
		{
			swap_rows   (tet->curve[i][j], 2, 3);
			swap_columns(tet->curve[i][j], 2, 3);
		}

		swap_sheets(tet->curve[i]);
	}
}


static void renumber_edge_classes(
	Tetrahedron	*tet)
{
	EdgeClass	*temp_edge_class;
	Orientation	temp_edge_orientation;
	EdgeIndex	i;

	temp_edge_class		= tet->edge_class[1];
	tet->edge_class[1]	= tet->edge_class[2];
	tet->edge_class[2]	= temp_edge_class;

	temp_edge_class		= tet->edge_class[3];
	tet->edge_class[3]	= tet->edge_class[4];
	tet->edge_class[4]	= temp_edge_class;

	for (i = 1; i < 5; i++)
		if (tet->edge_class[i] != NULL)
		{
			tet->edge_class[i]->incident_tet = tet;
			tet->edge_class[i]->incident_edge_index = i;
		}

	temp_edge_orientation		= tet->edge_orientation[1];
	tet->edge_orientation[1]	= tet->edge_orientation[2];
	tet->edge_orientation[2]	= temp_edge_orientation;

	temp_edge_orientation		= tet->edge_orientation[3];
	tet->edge_orientation[3]	= tet->edge_orientation[4];
	tet->edge_orientation[4]	= temp_edge_orientation;

	for (i = 0; i < 6; i++)
		tet->edge_orientation[i] = ! tet->edge_orientation[i];
}


static void renumber_shapes(
	Tetrahedron	*tet)
{
	/*
	 *	Renumber the TetShapes iff they are actually present.
	 */

	int	i,
		j;

	if (tet->shape[complete] != NULL)

		for (i = 0; i < 2; i++)			/*	i = complete, filled		*/

			for (j = 0; j < 2; j++)		/*	j = ultimate, penultimate	*/

				renumber_one_part(tet->shape[i]->cwl[j]);
}


static void renumber_one_part(
	ComplexWithLog	edge_parameters[3])
{
	ComplexWithLog	temp;
	int				i;

	/*
	 *	Swap the indices on edges 1 and 2.
	 */

	temp				= edge_parameters[1];
	edge_parameters[1]	= edge_parameters[2];
	edge_parameters[2]	= temp;

	/*
	 *	Invert the modulus of each edge parameter, but leave
	 *	the angles the same.
	 */
 
	for (i = 0; i < 3; i++)
	{
		edge_parameters[i].log.real = - edge_parameters[i].log.real;
		edge_parameters[i].rect = complex_exp(edge_parameters[i].log);
	}
}


static void renumber_shape_histories(
	Tetrahedron	*tet)
{
	int				i;
	ShapeInversion	*shape_inversion;

	for (i = 0; i < 2; i++)

		for (	shape_inversion = tet->shape_history[i];
				shape_inversion != NULL;
				shape_inversion = shape_inversion->next)

			switch (shape_inversion->wide_angle)
			{
				case 0:
					shape_inversion->wide_angle = 0;
					break;

				case 1:
					shape_inversion->wide_angle = 2;
					break;

				case 2:
					shape_inversion->wide_angle = 1;
					break;
			}
}


static void swap_rows(
	int	m[4][4],
	int	a,
	int	b)
{
	int	j,
		temp;

	for (j = 0; j < 4; j++)
	{
		temp	= m[a][j];
		m[a][j]	= m[b][j];
		m[b][j]	= temp;
	}
}

static void swap_columns(
	int	m[4][4],
	int	a,
	int	b)
{
	int	i,
		temp;

	for (i = 0; i < 4; i++)
	{
		temp	= m[i][a];
		m[i][a]	= m[i][b];
		m[i][b]	= temp;
	}
}

static void swap_sheets(
	int	m[2][4][4])
{
	int	i,
		j,
		temp;

	for (i = 0; i < 4; i++)
		for (j = 0; j < 4; j++)
		{
			temp					= m[right_handed][i][j];
			m[right_handed][i][j]	= m[left_handed ][i][j];
			m[left_handed ][i][j]	= temp;
		}
}


/*
 *	We want the peripheral curves of an oriented manifold to lie
 *	on the right_handed sheets of the orientation double covers
 *	of the cusps.  transfer_peripheral_curves() adds the curves
 *	from the left_handed sheet to the right_handed sheet, and
 *	sets the contents of the left_handed sheet to zero.
 */

static void transfer_peripheral_curves(
	Triangulation	*manifold)
{
	Tetrahedron		*tet;
	PeripheralCurve	c;
	VertexIndex		v;
	FaceIndex		f;

	for (tet = manifold->tet_list_begin.next;
		 tet != &manifold->tet_list_end;
		 tet = tet->next)

		for (c = 0; c < 2; c++)

			for (v = 0; v < 4; v++)

				for (f = 0; f < 4; f++)
				{
					tet->curve[c][right_handed][v][f] += tet->curve[c][left_handed][v][f];
					tet->curve[c][left_handed] [v][f] = 0;

				}
}


static void make_all_edge_orientations_right_handed(
	Triangulation *manifold)
{
	Tetrahedron	*tet;
	EdgeIndex	e;

	for (tet = manifold->tet_list_begin.next;
		 tet != &manifold->tet_list_end;
		 tet = tet->next)

		for (e = 0; e < 6; e++)

			tet->edge_orientation[e] = right_handed;
}


void reorient(
	Triangulation	*manifold)
{
	Tetrahedron	*tet;

	for (tet = manifold->tet_list_begin.next;
		 tet != &manifold->tet_list_end;
		 tet = tet->next)

		reverse_orientation(tet);


	if (manifold->orientability == oriented_manifold)
	{
		/*
		 *	The peripheral curves haven't gone anywhere,
		 *	but the sheets they are on are now considered
		 *	the left_handed sheets rather than the right_handed
		 *	sheets.  So transfer them to what used to be the
		 *	left_handed sheets but are now the right_handed sheets.
		 */
		transfer_peripheral_curves(manifold);

		/*
		 *	To adhere to the orientation conventions for peripheral curves
		 *	(see the documentation at the top of peripheral_curves.c)
		 *	we must reverse the directions of all meridians.
		 *
		 *	Note that it was the act of transferring the peripheral curves
		 *	from the left_handed to right_handed sheets -- note the reversal
		 *	of the Tetrahedra -- that caused the violation of the orientaiton
		 *	convention.  In particular, curves in nonorientable manifold, even
		 *	on (double covers of) Klein bottle cusps, still respect the convention.
		 */
		reverse_all_meridians(manifold);

		/*
		 *	Adjust the edge orientations, too.
		 */
		make_all_edge_orientations_right_handed(manifold);
	}

	/*
	 *	The Chern-Simons invariant of the manifold will be negated,
	 *	and the fudge factor will be different.
	 */
	if (manifold->CS_value_is_known)
	{
		manifold->CS_value[ultimate]	= - manifold->CS_value[ultimate];
		manifold->CS_value[penultimate]	= - manifold->CS_value[penultimate];
	}
	compute_CS_fudge_from_value(manifold);
}


static void reverse_all_meridians(
	Triangulation	*manifold)
{
	Tetrahedron	*tet;
	Cusp		*cusp;
	int			i,
				j,
				k;

	/*
	 *	Change the directions of all meridians.
	 */

	for (tet = manifold->tet_list_begin.next;
		 tet != &manifold->tet_list_end;
		 tet = tet->next)

		for (i = 0; i < 2; i++)

			for (j = 0; j < 4; j++)

				for (k = 0; k < 4; k++)

					tet->curve[M][i][j][k] = - tet->curve[M][i][j][k];

	/*
	 *	Negating the m coefficient of all Dehn fillings compensates for
	 *	the fact that we reversed the meridian, and gives us the same
	 *	(oriented) Dehn filling curve as before.  However, this curve
	 *	will now wind clockwise around the core geodesics, relative to
	 *	the new orientation on the manifold.  This causes a whole new
	 *	solution to be found to the gluing equations.  To avoid this,
	 *	we reverse the direction of the Dehn filling curve (i.e. we
	 *	negate both the m and l coefficients).  The net effect is that
	 *	we negate the l coefficient.
	 *
	 *	This reversal of the Dehn filling curve is not really
	 *	necessary, and could be eliminated if it's ever causes problems.
	 */

	for (cusp = manifold->cusp_list_begin.next;
		 cusp != &manifold->cusp_list_end;
		 cusp = cusp->next)

		cusp->l = - cusp->l;

	/*
	 *	Adjust all cusp_shapes.
	 *	(The current cusp_shape of a filled Cusp will be Zero, but that's OK.)
	 */

	for (cusp = manifold->cusp_list_begin.next;
		 cusp != &manifold->cusp_list_end;
		 cusp = cusp->next)

		for (i = 0; i < 2; i++)		/* i = initial, current */

			cusp->cusp_shape[i].real = - cusp->cusp_shape[i].real;

	/*
	 *	Adjust the holonomies.
	 *
	 *	Changing the orientation of the manifold negates the imaginary
	 *	parts of log(H(m)) and log(H(l)).
	 *
	 *	But we also reversed the direction of the meridian, which
	 *	negates both the real and imaginary parts of log(H(m)), so
	 *	the net effect on log(H(m)) is that its real part is negated.
	 */

	for (cusp = manifold->cusp_list_begin.next;
		 cusp != &manifold->cusp_list_end;
		 cusp = cusp->next)

		for (i = 0; i < 2; i++)		/* i = ultimate, penultimate */
		{
			cusp->holonomy[i][M].real = - cusp->holonomy[i][M].real;
			cusp->holonomy[i][L].imag = - cusp->holonomy[i][L].imag;
		}
}


void fix_peripheral_orientations(
	Triangulation	*manifold)
{
	Tetrahedron	*tet;
	VertexIndex	v;
	FaceIndex	f;
	Cusp		*cusp;

	/*
	 *	This function should get called only for orientable manifolds.
	 */
	if (manifold->orientability != oriented_manifold)
		uFatalError("fix_peripheral_orientations", "orient");

	/*
	 *	Compute the intersection number of the meridian and longitude.
	 */
	copy_curves_to_scratch(manifold, 0, FALSE);
	copy_curves_to_scratch(manifold, 1, FALSE);
	compute_intersection_numbers(manifold);

	/*
	 *	Reverse the meridian on cusps with intersection_number[L][M] == -1.
	 */

	/* which Tetrahedron */
	for (tet = manifold->tet_list_begin.next;
		 tet != &manifold->tet_list_end;
		 tet = tet->next)

		/* which ideal vertex */
		for (v = 0; v < 4; v++)

			if (tet->cusp[v]->intersection_number[L][M] == -1)

				/* which side of the vertex */
				for (f = 0; f < 4; f++)

					if (v != f)
					{
						tet->curve[M][right_handed][v][f] = - tet->curve[M][right_handed][v][f];

						if (tet->curve[M][left_handed][v][f] != 0.0
						 || tet->curve[L][left_handed][v][f] != 0.0)
							uFatalError("fix_peripheral_orientations", "orient");
					}

	/*
	 *	When we reverse the meridian we must also negate the meridional
	 *	Dehn filling coefficient in order to maintain the same (oriented)
	 *	Dehn filling curve as before.  However, this Dehn filling curve
	 *	will wind clockwise around the core geodesics, relative to
	 *	the global orientation on the manifold (because the global
	 *	orientation disagrees with the local orientation we had been using
	 *	on the nonorientable manifold's torus cusp).  This forces a whole
	 *	new solution to be found to the gluing equations.  To avoid this,
	 *	we reverse the direction of the Dehn filling curve (i.e. we
	 *	negate both the m and l coefficients).  The net effect is that
	 *	we negate the l coefficient.
	 *
	 *	This reversal of the Dehn filling curve is not really
	 *	necessary, and could be eliminated if it's ever causes problems.
	 */

	for (cusp = manifold->cusp_list_begin.next;
		 cusp != &manifold->cusp_list_end;
		 cusp = cusp->next)

		if (cusp->intersection_number[L][M] == -1)

			cusp->l = - cusp->l;
}