File: dual_curves.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 (1133 lines) | stat: -rw-r--r-- 30,442 bytes parent folder | download | duplicates (8)
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
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
/*
 *	dual_curves.c
 *
 *	This file provides the functions
 *
 *		void dual_curves(	Triangulation			*manifold,
 *							int						max_size,
 *							int						*num_curves,
 *							DualOneSkeletonCurve	***the_curves);
 *
 *		void get_dual_curve_info(
 *							DualOneSkeletonCurve	*the_curve,
 *							Complex					*complete_length,
 *							Complex					*filled_length,
 *							MatrixParity			*parity)
 *
 *		void free_dual_curves(
 *							int						num_curves,
 *							DualOneSkeletonCurve	**the_curves);
 *
 *	dual_curves() computes a reasonable selection of simple closed curves
 *	in a manifold's dual 1-skeleton.  The meaning of "reasonable selection"
 *	will be clarified below in the description of the algorithm.
 *
 *	Input arguments:
 *
 *		manifold		is a pointer to the Triangulation of interest.
 *
 *		max_size		is the maximum number of segments in the curves
 *						to be considered.  Six is a reasonable value.
 *
 *	Output aguments:
 *
 *		*num_curves		will be set to the number of curves the function finds.
 *
 *		*the_curves		will be set to the address of an array containing
 *						pointers to the DualOneSkeletonCurves.  That is,
 *						(*the_curves)[i] will be a pointer to the i-th curve.
 *						If no nonparabolic curves are found (as happens
 *						with the Gieseking), *the_curves will be set to NULL.
 *
 *	get_dual_curve_info() reports the complex length of a curve
 *	in the dual 1-skeleton, relative to both the complete and filled
 *	hyperbolic structures, and also its parity (orientation_preserving
 *	or orientation_reversing).
 *
 *	free_dual_curves() releases the array of DualOneSkeletonCurves
 *	allocated by dual_curves().  (It releases both the
 *	DualOneSkeletonCurves and the array of pointers to them.)
 *
 *
 *	Terminology:  Throughout this file we will flip-flop freely between
 *	the description of a curve as vertices and edges in the dual 1-skeleton
 *	and its dual description as Tetrahedra and 2-cells in the original
 *	Triangulation.  Please don't let this confuse you.
 *
 *
 *	The algorithm.
 *
 *	The set of all simple closed curves in the dual 1-skeleton
 *	divides naturally into homotopy classes.  Ideally, we'd like to
 *	compute precisely one representative of each homotopy class,
 *	and we'd like that representative to be unknotted in the sense
 *	that it's isotopic to the unique geodesic in its homotopy class.
 *	We won't always achieve this goal, but we'll do the best we can.
 *
 *	The main obstacle to achieving the goal is the vast number of
 *	simple closed curves to be considered, and the large number of
 *	curves within each homotopy class.  Even for a given curve of
 *	size n, we could start traversing it at any of its n vertices,
 *	and in either of two directions.  To avoid this last problem, we
 *	number all the Tetrahedra in the Triangulation and make two conventions:
 *
 *	Convention #1:  Each curve will have a "base Tetrahedron" which
 *	is the lowest-numbered Tetrahedron on the curve.
 *
 *	Convention #2:  Each curve is traversed by starting at its
 *	base Tetrahedron and going in the direction which takes you
 *	through the face of lower index (of the two faces of the base
 *	Tetrahedron which intersect the curve).  For example, if a
 *	curve intersects faces 1 and 3 of its base Tetrahedron, then
 *	the canonical direction to traverse it is to start off through
 *	face 1, traverse the whole curve, and return through face 3.
 *
 *	It's easy enough to detect whether two different curves are
 *	homotopic in the universal cover (they'll have the same Moebius
 *	transformation) but it's not so easy to detect when one is homotopic
 *	to a translate of the other.  For this reason we keep only one curve
 *	for any given complex length.  An unfortunate side effect of this
 *	decision is that when a manifold contains two geodesics of the
 *	same length, we'll be able to drill out only one of them (in most
 *	cases geodesics of the same length will be equivalent under some
 *	symmetry of the manifold, but nevertheless it would have been nice
 *	not to have had to impose this restriction).  For each complex length,
 *	the curve we keep will have minimal combinatorial size, to minimize
 *	the chance of choosing a knotted representative of the homotopy class.
 *
 *	[Modified 93/9/14 by JRW to compare the complex lengths of curves
 *	in the filled structure as well as in the complete structure.
 *	A curve will be discarded only if it has the same complex length
 *	as some other curve, relative to both the complete and the filled
 *	hyperbolic structures.]
 *
 *	Within this file we are interested in the complex lengths of
 *	geodesics relative to the complete structure on the manifold,
 *	because curves which are parabolics relative to the complete
 *	structure will either be obviously parallel to the boundary
 *	(in which case drill_cusp() will fail), or not-so-obviously
 *	parallel to the boundary, in which case drill_cusp() will yield
 *	a nonhyperbolic manifold.  But we also compute the complex
 *	lengths of geodesics relative to the filled structure, for
 *	the convenience of the user (e.g. the user might want to drill
 *	out a geodesic of minimal length in a certain closed manifold,
 *	perhaps as part of an effort to prove that two closed manifolds
 *	are isometric [symmetry_group_closed.c now does this automatically]).
 */

/*
 *	95/10/1  JRW
 *	I was concerned about stack/heap collisions caused by recursive
 *	functions, and was going through the SnapPea kernel revising
 *	recursive algorithms to use heap space instead.  However, I decided
 *	not to revise consider_its_neighbor() for the following reasons.
 *
 *	(1)	It's fairly complicated.  Revising it would be time-consuming
 *		and error-prone.
 *
 *	(2)	The recursion is very "shallow".  It never goes more than
 *		max_size levels deep.  Typically max_size is about 6, or at
 *		most 8 or 10, because the number of curves grows exponentially
 *		with max_size.  In other words, this is a harmless recursion.
 */

#include "kernel.h"

/*
 *	BIG_MODULUS is used to recognize when a complex number
 *	represents the point at infinity on the Riemann sphere.
 *	I haven't given much thought to the best value for
 *	BIG_MODULUS, because in all common cases a number
 *	will either clearly be infinite or clearly not be
 *	infinite.
 *
 *	93/10/9.  An error message provided the occasion to think
 *	about the best value for BIG_MODULUS.  Amazingly enough, after
 *	doing several dozen Dehn fillings on each of thousands of cusped
 *	census manifolds, I got the
 *
 *		uFatalError("verify_mt_action", "dual_curves");
 *
 *	which indicated the previous value of BIG_MODULUS (namely 1e10)
 *	was not big enough.  The offending (revealing?) example
 *	is v2395(-1,1).  The value of fz and w are
 *
 *		fz = 1.2656500338200879e+8 + i 1.89154960743708773e+9
 *		w  = 1.2656500287902592e+8 + i 1.89154960735500588e+9
 *
 *	Later another example occurred with
 *
 *		fz = -6.19138762819279958e+7 + i -6.56272444717916559e+7
 *		w  = -6.19138762819635613e+7 + i -6.5627244471300831e+7
 *
 *	For most purposes I think I'll leave BIG_MODULUS at 1e10
 *	(this ensures accurate computations), but for the error check
 *	in verify_mt_action() I'll make a provision that if fz and w
 *	have moduli in the range 1e5 - 1e10 and differ by a small
 *	percentage, they should be considered equal.
 */

#define BIG_MODULUS		1e10
#define BIG_MODULUS1	 1e5
#define FRACTIONAL_DIFF	 1e4

/*
 *	MoebiusTransformations which translate a distance less
 *	than PARABOLIC_EPSILON are judged to be parabolics.  I haven't
 *	given a lot of thought to the best value for PARABOLIC_EPSILON,
 *	because in practice I expect that in all common examples
 *	the MoebiusTransformations will be clearly parabolic or
 *	clearly not parabolic.
 */

#define PARABOLIC_EPSILON	1e-2

/*
 *	Two complex lengths are considered equal iff their real
 *	and imaginary parts are within LENGTH_EPSILON of each other.
 */

#define	LENGTH_EPSILON		1e-5

/*
 *	If the MoebiusTransformation's action on the base
 *	Tetrahedron's fourth vertex isn't correct to within
 *	MINIMAL_ACCURACY, a fatal error is generated.
 */

#define MINIMAL_ACCURACY 1e-6


static void					initialize_flags(Triangulation *manifold);
static void					consider_its_neighbor(Tetrahedron *tet, FaceIndex face, int size, Complex corners[2][4], Orientation orientation, Tetrahedron *tet0, FaceIndex face0, int max_size, Triangulation *manifold, DualOneSkeletonCurve **curve_tree);
static void					compute_corners(Complex corners[4], Complex nbr_corners[4], FaceIndex face, FaceIndex entry_face, Permutation gluing, Orientation nbr_orientation, ComplexWithLog cwl[3]);
static void					compute_Moebius_transformation(Tetrahedron *tet, Orientation orientation, Complex corners[4], MoebiusTransformation *mt);
static void					verify_mt_action(MoebiusTransformation *mt, Complex z, Complex w);
static void					add_curve_to_tree(Triangulation *manifold, DualOneSkeletonCurve **curve_tree, MatrixParity parity, Complex cl[2], int size);
static DualOneSkeletonCurve	*package_up_the_curve(Triangulation *manifold, MatrixParity parity, Complex cl[2], int size);
static void					replace_contents_of_node(DualOneSkeletonCurve *node, Triangulation *manifold, MatrixParity parity, Complex cl[2], int size);
static void					convert_tree_to_pointer_array( DualOneSkeletonCurve *curve_tree, int *num_curves, DualOneSkeletonCurve ***the_curves);
static int					count_the_curves(DualOneSkeletonCurve *curve_tree);
static void					write_node_addresses(DualOneSkeletonCurve *curve_tree, DualOneSkeletonCurve **the_array, int *count);


void dual_curves(
	Triangulation			*manifold,
	int						max_size,
	int						*num_curves,
	DualOneSkeletonCurve	***the_curves)
{
	Tetrahedron				*tet0;
	FaceIndex				face0;
	Complex					corners0[2][4];
	DualOneSkeletonCurve	*curve_tree;
	int						i;

	/*
	 *	If the manifold does not have a hyperbolic
	 *	structure, return no curves.
	 */

	if
	(
		(
			manifold->solution_type[complete] != geometric_solution
		 && manifold->solution_type[complete] != nongeometric_solution
		)
	 ||
		(
			manifold->solution_type[filled]   != geometric_solution
		 && manifold->solution_type[filled]   != nongeometric_solution
		 && manifold->solution_type[filled]   != flat_solution
		)
	)
	{
		*num_curves	= 0;
		*the_curves	= NULL;
		return;
	}

	/*
	 *	Make sure the Tetrahedra are numbered.
	 */

	number_the_tetrahedra(manifold);

	/*
	 *	curve_tree is a pointer to the root of a binary
	 *	tree containing all the curves found so far.
	 *	Initialize it to NULL.
	 */

	curve_tree = NULL;

	/*
	 *	Set the tet_on_curve and face_on_curve[] flags
	 *	to FALSE to show that the curve is initially empty.
	 */

	initialize_flags(manifold);

	/*
	 *	Consider each possible base Tetrahedron.
	 */

	for (tet0 = manifold->tet_list_begin.next;
		 tet0 != &manifold->tet_list_end;
		 tet0 = tet0->next)
	{
		/*
		 *	Mark the base Tetrahedron.
		 */
		tet0->tet_on_curve = TRUE;

		/*
		 *	Put the corners of the base Tetrahedron
		 *	in the standard position.
		 */
		for (i = 0; i < 2; i++)		/* i = complete, filled */
		{
			corners0[i][0] = Infinity;
			corners0[i][1] = Zero;
			corners0[i][2] = One;
			corners0[i][3] = tet0->shape[i]->cwl[ultimate][0].rect;
		}

		/*
		 *	Consider each possible initial face for the curve.
		 *	By Convention #2 above, we may assume the initial
		 *	face is not face 3.
		 */
		for (face0 = 0; face0 < 3; face0++)
			consider_its_neighbor(	tet0, face0, 1,
									corners0, right_handed,
									tet0, face0, max_size,
									manifold, &curve_tree);

		/*
		 *	Unmark the base Tetrahedron.
		 */
		tet0->tet_on_curve = FALSE;
	}

	/*
	 *	curve_tree will now point to a binary tree containing
	 *	the DualOneSkeletonCurves.  Write the addresses of the
	 *	nodes into an array, as specified in the documentation
	 *	at the top of this file.
	 */

	convert_tree_to_pointer_array(curve_tree, num_curves, the_curves);
}


static void initialize_flags(
	Triangulation	*manifold)
{
	Tetrahedron	*tet;
	int			i;

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

		for (i = 0; i < 4; i++)
			tet->face_on_curve[i] = FALSE;
	}
}


static void consider_its_neighbor(
	Tetrahedron				*tet,
	FaceIndex				face,
	int						size,
	Complex					corners[2][4],
	Orientation				orientation,
	Tetrahedron				*tet0,
	FaceIndex				face0,
	int						max_size,
	Triangulation			*manifold,
	DualOneSkeletonCurve	**curve_tree)
{
	Tetrahedron				*nbr;
	Permutation				gluing;
	FaceIndex				nbr_face,
							entry_face;
	Orientation				nbr_orientation;
	Complex					nbr_corners[2][4];
	MoebiusTransformation	mt[2];
	Complex					cl[2];
	int						i;

	/*
	 *	We want to examine the Tetrahedron incident
	 *	to face "face" of Tetrahedron "tet".
	 */
	nbr				= tet->neighbor[face];
	gluing			= tet->gluing[face];
	entry_face		= EVALUATE(gluing, face);
	nbr_orientation	= (parity[gluing] == orientation_preserving) ?
						  orientation :
						! orientation;

	/*
	 *	Is nbr the base Tetrahedron?
	 *	If so, process the curve and return.
	 */
	if (nbr == tet0)
	{
		/*
		 *	We've found a curve adhering to Convention #2
		 *	iff we're reentering the base Tetrahedron at
		 *	a higher numbered face than the one we left at.
		 */
		if (entry_face > face0)
		{
			/*
			 *	Process this curve.
			 */

			/*
			 *	Compute the locations of the nbr_corners.
			 */
			for (i = 0; i < 2; i++)	/* i = complete, filled */
				compute_corners(corners[i], nbr_corners[i], face, entry_face,
								gluing, nbr_orientation,
								nbr->shape[i]->cwl[ultimate]);

			/*
			 *	For the complete structure and also for the filled structure,
			 *	compute the MoebiusTransformation which takes the original
			 *	base Tetrahedron to nbr.
			 */
			for (i = 0; i < 2; i++)	/* i = complete, filled */
				compute_Moebius_transformation(
							nbr, nbr_orientation, nbr_corners[i], &mt[i]);

			/*
			 *	The computation of the MoebiusTransformation used
			 *	only the location of corners 0, 1 and 2.  As a check
			 *	against errors, let's see whether the MoebiusTransformation
			 *	also takes corner 3 to the right place.
			 */
			for (i = 0; i < 2; i++)	/* i = complete, filled */
				verify_mt_action(
					&mt[i],
					tet0->shape[i]->cwl[ultimate][0].rect,
					nbr_corners[i][3]);

			/*
			 *	Compute the complex length of the geodesic corresponding
			 *	to the covering transformation represented by mt.
			 */
			for (i = 0; i < 2; i++)	/* i = complete, filled */
				cl[i] = complex_length_mt(&mt[i]);

			/*
			 *	Ignore parabolics (relative to the complete structure).
			 */
			if (fabs(cl[complete].real) < PARABOLIC_EPSILON)
				return;

			/*
			 *	Add the final segment to close the curve.
			 */
			tet->face_on_curve[face]		= TRUE;
			nbr->face_on_curve[entry_face]	= TRUE;

			/*
			 *	Add the curve to the list, unless a curve of
			 *	equal complex length and smaller or equal
			 *	combinatorial size is already there.
			 */
			add_curve_to_tree(manifold, curve_tree, mt[0].parity, cl, size);

			/*
			 *	Remove the final segment of the curve before
			 *	continuing on to look for other possibilities.
			 */
			tet->face_on_curve[face]		= FALSE;
			nbr->face_on_curve[entry_face]	= FALSE;
		}

		return;
	}

	/*
	 *	Is nbr a Tetrahedron which has already been visited
	 *	(other than the base Tetrahedron, which was handled above)?
	 *	If so, return.
	 */
	if (nbr->tet_on_curve == TRUE)
		return;

	/*
	 *	If nbr's index is less than the index of the base Tetrahedron,
	 *	then Convention #1 dictates that we return without doing anything.
	 */
	if (nbr->index < tet0->index)
		return;

	/*
	 *	If size == max_size, then we should stop the recursion.
	 */
	if (size == max_size)
		return;

	/*
	 *	nbr has passed all the above tests, so add it to the curve...
	 */
	nbr->tet_on_curve = TRUE;
	tet->face_on_curve[face]		= TRUE;
	nbr->face_on_curve[entry_face]	= TRUE;

	/*
	 *	...compute the positions of its corners...
	 */
	for (i = 0; i < 2; i++)	/* i = complete, filled */
		compute_corners(corners[i], nbr_corners[i], face, entry_face,
						gluing, nbr_orientation,
						nbr->shape[i]->cwl[ultimate]);

	/*
	 *	...recursively consider each of its neighbors...
	 */
	for (nbr_face = 0; nbr_face < 4; nbr_face++)
		if (nbr_face != entry_face)
			consider_its_neighbor(
				nbr,  nbr_face, size + 1,
				nbr_corners, nbr_orientation,
				tet0, face0,    max_size,
				manifold, curve_tree);

	/*
	 *	...and remove it from the curve.
	 */
	nbr->tet_on_curve = FALSE;
	tet->face_on_curve[face]		= FALSE;
	nbr->face_on_curve[entry_face]	= FALSE;
}


static void compute_corners(
	Complex			corners[4],
	Complex			nbr_corners[4],
	FaceIndex		face,
	FaceIndex		entry_face,
	Permutation		gluing,
	Orientation		nbr_orientation,
	ComplexWithLog	cwl[3])
{
	int	i;

	/*
	 *	Knock off the three easy ones.
	 */
	for (i = 0; i < 4; i++)
		if (i != face)
			nbr_corners[EVALUATE(gluing, i)] = corners[i];

	/*
	 *	Then call compute_fourth_corner() to find the
	 *	fourth corner in terms of the first three.
	 */
	compute_fourth_corner(	nbr_corners,
							entry_face,
							nbr_orientation,
							cwl);
}


static void compute_Moebius_transformation(
	Tetrahedron				*tet,
	Orientation				orientation,
	Complex					corners[4],
	MoebiusTransformation	*mt)
{
	/*
	 *	The base Tetrahedron originally had its corners
	 *	at (infinity, 0, 1, z).  We've now traced out a
	 *	curve which, when lifted to the universal cover,
	 *	leads to a translate of the base Tetrahedron with
	 *	corner coordinates given by the array corners[].
	 *	The associated covering transformation will be
	 *	the Moebius transformation which takes
	 *
	 *			infinity  ->  corners[0]
	 *			   0      ->  corners[1]
	 *			   1      ->  corners[2]
	 *
	 *	and has the specified Orientation.
	 *
	 *	Because {infinity, 0, 1} are invariant under complex
	 *	conjugation, we can compute the SL2CMatrix without
	 *	worrying about the Orientation.  (The Orientation
	 *	will, of course, determine the parity field of the
	 *	MoebiusTransformation.)
	 *
	 *	An SL2CMatrix taking
	 *
	 *			infinity  ->  c0
	 *			   0      ->  c1
	 *			   1      ->  c2
	 *
	 *	is given by
	 *
	 *			c0(c2 - c1) z  +  c1(c0 - c2)
	 *		w = -----------------------------
	 *			  (c2 - c1) z  +    (c0 - c2)
	 *
	 *	(This matrix must, of course, be normalized to have
	 *	determinant one.)
	 *
	 *	In the special case that c0 is infinite, the formula
	 *	reduces to
	 *
	 *		w = (c2 - c1) z  +  c1
	 *
	 *	In the special case that c1 is infinite, the formula
	 *	reduces to
	 *
	 *			c0 z  +  (c2 - c0)
	 *		w = ------------------
	 *			   z
	 *
	 *	In the special case that c2 is infinite, the formula
	 *	reduces to
	 *
	 *			c0 z  -  c1
	 *		w = -----------
	 *			   z  -  1
	 *
	 */

	/*
	 *	Evaluate the appropriate formula from above.
	 *	Don't worry yet about normalizing to have
	 *	determinant one.
	 */

	if (complex_modulus(corners[0]) > BIG_MODULUS)
	{
		/*
		 *	c0 is infinite.
		 *	Use the special formula
		 *
		 *		w = (c2 - c1) z  +  c1
		 */
		mt->matrix[0][0] = complex_minus(corners[2], corners[1]);
		mt->matrix[0][1] = corners[1];
		mt->matrix[1][0] = Zero;
		mt->matrix[1][1] = One;
	}
	else if (complex_modulus(corners[1]) > BIG_MODULUS)
	{
		/*
		 *	c1 is infinite.
		 *	Use the special formula
		 *
		 *			c0 z  +  (c2 - c0)
		 *		w = ------------------
		 *			   z
		 */
		mt->matrix[0][0] = corners[0];
		mt->matrix[0][1] = complex_minus(corners[2], corners[0]);
		mt->matrix[1][0] = One;
		mt->matrix[1][1] = Zero;
	}
	else if (complex_modulus(corners[2]) > BIG_MODULUS)
	{
		/*
		 *	c2 is infinite.
		 *	Use the special formula
		 *
		 *			c0 z  -  c1
		 *		w = -----------
		 *			   z  -  1
		 */
		mt->matrix[0][0] = corners[0];
		mt->matrix[0][1] = complex_negate(corners[1]);
		mt->matrix[1][0] = One;
		mt->matrix[1][1] = MinusOne;
	}
	else
	{
		/*
		 *	None of {c0, c1, c2} is infinite.
		 *	Use the general formula
		 *
		 *			c0(c2 - c1) z  +  c1(c0 - c2)
		 *		w = -----------------------------
		 *			  (c2 - c1) z  +    (c0 - c2)
		 *
		 *	Note that for computational efficiency we
		 *	evaluate the terms in the denominator first.
		 */
		mt->matrix[1][0] = complex_minus(corners[2], corners[1]);
		mt->matrix[1][1] = complex_minus(corners[0], corners[2]);
		mt->matrix[0][0] = complex_mult(corners[0], mt->matrix[1][0]);
		mt->matrix[0][1] = complex_mult(corners[1], mt->matrix[1][1]);
	}

	/*
	 *	Normalize matrix to have determinant one.
	 */
	sl2c_normalize(mt->matrix);

	/*
	 *	Set the MoebiusTransformation's parity.
	 *	The base Tetrahedron had the right_handed Orientation,
	 *	so the MoebiusTransformation will be orientation_preserving
	 *	iff the translate of the base Tetrahedron also has the
	 *	right_handed Orientation.
	 */
	mt->parity = (orientation == right_handed) ?
				 orientation_preserving :
				 orientation_reversing;
}


static void verify_mt_action(
	MoebiusTransformation	*mt,
	Complex					z,
	Complex					w)
{
	Complex	fz;

	/*
	 *	Does mt take z to w?
	 */

	/*
	 *	If the MoebiusTransformation is orientation_reversing,
	 *	we must first replace z by its complex conjugate.
	 */

	if (mt->parity == orientation_reversing)
		z = complex_conjugate(z);

	/*	Evaluate
	 *
	 *	f(z) = (az + b)/(cz + d)
	 *
	 *	and compare the result to w.
	 */

	fz = complex_div(
		complex_plus(
			complex_mult(mt->matrix[0][0], z),
			mt->matrix[0][1]
		),
		complex_plus(
			complex_mult(mt->matrix[1][0], z),
			mt->matrix[1][1]
		)
	);

	/*
	 *	fz and w should either be very close, or
	 *	both should be infinite.  Flag an error
	 *	if this is not the case.
	 */

	if
	(
		complex_modulus(complex_minus(fz, w)) > MINIMAL_ACCURACY
	 && 
		(
			complex_modulus(fz) < BIG_MODULUS
		 || complex_modulus(w)  < BIG_MODULUS
		)
	 &&
		(
			complex_modulus(fz) < BIG_MODULUS1
		 || complex_modulus(w)  < BIG_MODULUS1
		 || complex_modulus(complex_div(complex_minus(fz, w), fz)) > FRACTIONAL_DIFF
		)
	)
		uFatalError("verify_mt_action", "dual_curves");
}


static void add_curve_to_tree(
	Triangulation			*manifold,
	DualOneSkeletonCurve	**curve_tree,
	MatrixParity			parity,
	Complex					cl[2],	/*	complex length of geodesic	*/
	int						size)	/*	combinatorial size of curve	*/
{
	DualOneSkeletonCurve	*node;
	int						position;

	/*
	 *	First check for the special case that the
	 *	curve_tree might be empty.
	 */

	if (*curve_tree == NULL)
	{
		*curve_tree = package_up_the_curve(manifold, parity, cl, size);
		return;
	}

	/*
	 *	if (the tree does not yet contain a curve of
	 *		the given complex length)
	 *			add the current curve to the tree
	 *	else
	 *		if the current curve is combinatorially shorter
	 *		than the old curve of the same length,
	 *			replace it.
	 */

	node = *curve_tree;
	while (TRUE)
	{
		/*
		 *	Set the integer "position" to -1 if we belong
		 *	somewhere to the left of this node, to +1 if we
		 *	belong to the right, and to 0 if we belong here.
		 *
		 *	Modified 93/9/14 by JRW.  If two curves have the same complex
		 *	length in the complete structure but different complex lengths
		 *	in the filled structure, then we want to list them separately.
		 *
		 *	Modified 94/10/8 by JRW.  Sort first by filled length,
		 *	then by complete length.  The user is probably paying more
		 *	attention to filled lengths than complete ones.
		 */

		if      (cl[filled].real < node->length[filled].real - LENGTH_EPSILON)
			position = -1;
		else if (cl[filled].real > node->length[filled].real + LENGTH_EPSILON)
			position = +1;
		else if (cl[filled].imag < node->length[filled].imag - LENGTH_EPSILON)
			position = -1;
		else if (cl[filled].imag > node->length[filled].imag + LENGTH_EPSILON)
			position = +1;

		else if (cl[complete].real < node->length[complete].real - LENGTH_EPSILON)
			position = -1;
		else if (cl[complete].real > node->length[complete].real + LENGTH_EPSILON)
			position = +1;
		else if (cl[complete].imag < node->length[complete].imag - LENGTH_EPSILON)
			position = -1;
		else if (cl[complete].imag > node->length[complete].imag + LENGTH_EPSILON)
			position = +1;

		else
			position = 0;


		switch (position)
		{
			case -1:
				if (node->left_child != NULL)
					node = node->left_child;
				else
				{
					node->left_child = package_up_the_curve(manifold, parity, cl, size);
					return;
				}
				break;

			case +1:
				if (node->right_child != NULL)
					node = node->right_child;
				else
				{
					node->right_child = package_up_the_curve(manifold, parity, cl, size);
					return;
				}
				break;

			case 0:
				if (size < node->size)
					replace_contents_of_node(node, manifold, parity, cl, size);
				return;
		}
	}

	/*
	 *	The program never reaches this point.
	 */
}


static DualOneSkeletonCurve *package_up_the_curve(
	Triangulation	*manifold,
	MatrixParity	parity,
	Complex			cl[2],
	int				size)
{
	DualOneSkeletonCurve	*node;

	node = NEW_STRUCT(DualOneSkeletonCurve);
	node->tet_intersection = NEW_ARRAY(manifold->num_tetrahedra, DualOneSkeletonCurvePiece);

	replace_contents_of_node(node, manifold, parity, cl, size);

	node->left_child  = NULL;
	node->right_child = NULL;

	return node;
}


static void replace_contents_of_node(
	DualOneSkeletonCurve	*node,
	Triangulation			*manifold,
	MatrixParity			parity,
	Complex					cl[2],
	int						size)
{
	Tetrahedron	*tet;
	int			i;

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

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

			node->tet_intersection[tet->index][i] = tet->face_on_curve[i];

	node->parity			= parity;
	node->length[complete]	= cl[complete];
	node->length[filled]	= cl[filled];
	node->size				= size;
}


static void convert_tree_to_pointer_array(
	DualOneSkeletonCurve	*curve_tree,
	int						*num_curves,
	DualOneSkeletonCurve	***the_curves)
{
	int	count;

	/*
	 *	First handle the special case that no curves were found.
	 */
	if (curve_tree == NULL)
	{
		*num_curves = 0;
		*the_curves = NULL;
		return;
	}

	/*
	 *	Count the curves.
	 */
	*num_curves = count_the_curves(curve_tree);

	/*
	 *	Allocate the array for the pointers.
	 */
	*the_curves = NEW_ARRAY(*num_curves, DualOneSkeletonCurve *);

	/*
	 *	Write the addresses of the nodes into the array.
	 */
	count = 0;
	write_node_addresses(curve_tree, *the_curves, &count);

	/*
	 *	A quick error check.
	 */
	if (count != *num_curves)
		uFatalError("convert_tree_to_pointer_array", "dual_curves");
}


static int count_the_curves(
	DualOneSkeletonCurve	*curve_tree)
{
	DualOneSkeletonCurve	*subtree_stack,
							*subtree;
	int						num_curves;

	/*
	 *	Initialize the stack to contain the whole tree.
	 */
	subtree_stack = curve_tree;
	if (curve_tree != NULL)
		curve_tree->next_subtree = NULL;

	/*
	 *	Initialize the count to zero.
	 */
	num_curves = 0;

	/*
	 *	Process the subtrees on the stack one at a time.
	 */
	while (subtree_stack != NULL)
	{
		/*
		 *	Pull a subtree off the stack.
		 */
		subtree					= subtree_stack;
		subtree_stack			= subtree_stack->next_subtree;
		subtree->next_subtree	= NULL;

		/*
		 *	If the subtree's root has nonempty left and/or right subtrees,
		 *	add them to the stack.
		 */
		if (subtree->left_child != NULL)
		{
			subtree->left_child->next_subtree = subtree_stack;
			subtree_stack = subtree->left_child;
		}
		if (subtree->right_child != NULL)
		{
			subtree->right_child->next_subtree = subtree_stack;
			subtree_stack = subtree->right_child;
		}

		/*
		 *	Count the subtree's root node.
		 */
		num_curves++;
	}

	return num_curves;
}


static void write_node_addresses(
	DualOneSkeletonCurve	*curve_tree,
	DualOneSkeletonCurve	**the_array,
	int						*count)
{
	DualOneSkeletonCurve	*subtree_stack,
							*subtree;

	/*
	 *	Implement the recursive tree traversal using our own stack
	 *	rather than the system stack, to avoid the possibility of a
	 *	stack/heap collision.
	 */

	/*
	 *	Initialize the stack to contain the whole product_tree.
	 */
	subtree_stack = curve_tree;
	if (curve_tree != NULL)
		curve_tree->next_subtree = NULL;

	/*
	 *	Process the subtrees on the stack one at a time.
	 */
	while (subtree_stack != NULL)
	{
		/*
		 *	Pull a subtree off the stack.
		 */
		subtree					= subtree_stack;
		subtree_stack			= subtree_stack->next_subtree;
		subtree->next_subtree	= NULL;

		/*
		 *	If it has no further subtrees, append it to the array.
		 *
		 *	Otherwise break it into three chunks:
		 *
		 *		the left subtree
		 *		this node
		 *		the right subtree
		 *
		 *	and push them onto the stack in reverse order, so that they'll
		 *	come off in the correct order.  Set this node's left_child and
		 *	right_child fields to NULL, so the next time it comes off the
		 *	stack we'll know the subtrees have been accounted for.
		 */
		if (subtree->left_child == NULL  &&  subtree->right_child == NULL)
		{
			the_array[(*count)++] = subtree;
		}
		else
		{
			/*
			 *	Push the right subtree (if any) onto the stack.
			 */
			if (subtree->right_child != NULL)
			{
				subtree->right_child->next_subtree = subtree_stack;
				subtree_stack = subtree->right_child;
				subtree->right_child = NULL;
			}

			/*
			 *	Push this node onto the stack.
			 *	(Its left_child and right_child fields will soon be NULL.)
			 */
			subtree->next_subtree = subtree_stack;
			subtree_stack = subtree;

			/*
			 *	Push the left subtree (if any) onto the stack.
			 */
			if (subtree->left_child != NULL)
			{
				subtree->left_child->next_subtree = subtree_stack;
				subtree_stack = subtree->left_child;
				subtree->left_child = NULL;
			}
		}
	}
}


void get_dual_curve_info(
	DualOneSkeletonCurve	*the_curve,
	Complex					*complete_length,
	Complex					*filled_length,
	MatrixParity			*parity)
{
	if (complete_length != NULL)
		*complete_length = the_curve->length[complete];

	if (filled_length != NULL)
		*filled_length = the_curve->length[filled];

	if (parity != NULL)
		*parity = the_curve->parity;
}


void free_dual_curves(
	int						num_curves,
	DualOneSkeletonCurve	**the_curves)
{
	int	i;

	/*
	 *	If num_curves is zero, then no storage should
	 *	have been allocated in the first place.
	 */
	if (num_curves == 0)
	{
		if (the_curves == NULL)
			return;
		else
			uFatalError("free_dual_curves", "dual_curves");
	}

	/*
	 *	Free each DualOneSkeletonCurve.
	 */
	for (i = 0; i < num_curves; i++)
	{
		my_free(the_curves[i]->tet_intersection);
		my_free(the_curves[i]);
	}

	/*
	 *	Free the pointer array.
	 */
	my_free(the_curves);
}