File: cusps.c

package info (click to toggle)
regina-normal 4.93-1
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 28,576 kB
  • sloc: cpp: 86,815; ansic: 13,030; xml: 9,089; perl: 951; sh: 380; python: 273; makefile: 103
file content (423 lines) | stat: -rw-r--r-- 9,660 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
422
423
/*
 *	cusps.c
 *
 *	This file contains the functions
 *
 *		void	create_cusps(Triangulation *manifold);
 *		void	create_fake_cusps(Triangulation *manifold);
 *		void	count_cusps(Triangulation *manifold);
 *		Boolean	mark_fake_cusps(Triangulation *manifold);
 *
 *	create_cusps() is used within the kernel to assign Cusp data
 *	structures to Triangulations.  It assumes the neighbor and gluing
 *	fields have been set (all other fields are optional).  It assigns
 *	cusp indices, but does not install peripheral curves, determine
 *	the CuspTopologies, or count the cusps.  You should call
 *	peripheral_curves() to install the peripheral curves and determine
 *	the CuspTopologies, then call count_cusps() to set num_cusps,
 *	num_or_cusps and num_nonor_cusps.
 *
 *	create_fake_cusps() is used within the kernel to assign Cusp data
 *	structures to the "fake cusps" corresponding to finite vertices.
 *	It assumes fake cusps are indicated by tet->cusp[v] fields of NULL.
 *	The fake cusps are numbered -1, -2, etc.  As explained in the
 *	documentation at the top of finite_vertices.c, finite vertices use
 *	only the is_finite, index, prev and next fields of the Cusp data
 *	structure.  create_fake_cusps() does not disturb the real cusps or
 *	the non-NULL tet->cusp[v] fields.
 *
 *	count_cusps() counts the Cusps of each CuspTopology, and sets
 *	manifold->num_cusps, manifold->num_or_cusps and manifold->num_nonor_cusps.
 *
 *	mark_fake_cusps() distinguishes real cusps from fake cusps
 *	( = finite vertices) by computing the Euler characteristic.
 *	Sets is_finite to TRUE for fake cusps, and renumbers all cusps so that
 *	real cusps have consecutive nonnegative indices beginning at 0 and
 *	fake cusps have consecutive negative indices beginning at -1.
 *	Returns TRUE if fake cusps are present, FALSE otherwise.
 */

#include "kernel.h"


typedef struct
{
	Tetrahedron	*tet;
	VertexIndex	v;
} IdealVertex;


static void	compute_cusp_Euler_characteristics(Triangulation *manifold);


void create_cusps(
	Triangulation	*manifold)
{
	int			count;
	Tetrahedron	*tet;
	VertexIndex	v;

	/*
	 *	Make sure no Cusps are present, and everything is neat and tidy.
	 */

	error_check_for_create_cusps(manifold);

	/*
	 *	The variable "count" will keep track of the next index
	 *	to be assigned.  The first Cusp we create will have
	 *	index 0, the next will have 1, and so on.
	 */

	count = 0;

	/*
	 *	We look at each vertex of each Tetrahedron, and whenever we
	 *	encounter a vertex with no assigned Cusp, we create a Cusp
	 *	for it and recursively assign it to neighboring ideal vertices.
	 */

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

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

			if (tet->cusp[v] == NULL)

				create_one_cusp(manifold, tet, FALSE, v, count++);
}


void error_check_for_create_cusps(
	Triangulation	*manifold)
{
	Tetrahedron	*tet;
	VertexIndex	v;

	if (manifold->num_cusps			!= 0
	 || manifold->num_or_cusps		!= 0
	 || manifold->num_nonor_cusps	!= 0
	 || manifold->cusp_list_begin.next != &manifold->cusp_list_end)

		uFatalError("error_check_for_create_cusps", "cusps.c");


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

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

			if (tet->cusp[v] != NULL)

				uFatalError("error_check_for_create_cusps", "cusps.c");
}


void create_fake_cusps(
	Triangulation	*manifold)
{
	int			count;
	Tetrahedron	*tet;
	VertexIndex	v;

	/*
	 *	The variable "count" will keep track of the (negative) index
	 *	most recently assigned.  The first finite vertex we create
	 *	will have index -1, the next will have -2, and so on.
	 */

	count = 0;

	/*
	 *	We look at each vertex of each Tetrahedron, and whenever we
	 *	encounter an ideal vertex with no assigned Cusp, we create a Cusp
	 *	for it and assign it recursively to neighboring ideal vertices.
	 */

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

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

			if (tet->cusp[v] == NULL)

				create_one_cusp(manifold, tet, TRUE, v, --count);
}


void create_one_cusp(
	Triangulation	*manifold,
	Tetrahedron		*tet,
	Boolean			is_finite,
	VertexIndex		v,
	int				cusp_index)
{
	Cusp		*cusp;
	IdealVertex	*queue;
	int			queue_first,
				queue_last;
	Tetrahedron	*tet1,
				*nbr;
	VertexIndex	v1,
				nbr_v;
	FaceIndex	f;

	/*
	 *	Create the cusp, add it to the list, and set
	 *	the is_finite and index fields.
	 */

	cusp = NEW_STRUCT(Cusp);
	initialize_cusp(cusp);
	INSERT_BEFORE(cusp, &manifold->cusp_list_end);
	cusp->is_finite	= is_finite;
	cusp->index		= cusp_index;

	/*
	 *	We don't set the topology, is_complete, m, l, holonomy,
	 *	cusp_shape or shape_precision fields.
	 *
	 *	For "real" cusps the calling routine may
	 *
	 *		(1)	call peripheral_curves() to set the cusp->topology,
	 *
	 *		(2)	keep the default values of cusp->is_complete,
	 *			cusp->m and cusp->l as set by initialize_cusp(), and
	 *
	 *		(3)	let the holonomy and cusp_shape be computed automatically
	 *			when hyperbolic structure is computed.
	 *
	 *	Alternatively, the calling routine may set these fields in other
	 *	ways, as it sees fit.
	 *
	 *	If we were called by create_fake_cusps(), then the above fields
	 *	are all irrelevant and ignored.
	 */

	/*
	 *	Set the tet->cusp field at all vertices incident to the new cusp.
	 */

	/*
	 *	Allocate space for a queue of pointers to the IdealVertices.
	 *	Each IdealVertex will appear on the queue at most once, so an
	 *	array of length 4 * manifold->num_tetrahedra will suffice.
	 */
	queue = NEW_ARRAY(4 * manifold->num_tetrahedra, IdealVertex);

	/*
	 *	Set the cusp of the given IdealVertex...
	 */
	tet->cusp[v] = cusp;

	/*
	 *	...and put it on the queue.
	 */
	queue_first = 0;
	queue_last  = 0;
	queue[0].tet = tet;
	queue[0].v   = v;

	/*
	 *	Start processing the queue.
	 */
	do
	{
		/*
		 *	Pull an IdealVertex off the front of the queue.
		 */
		tet1 = queue[queue_first].tet;
		v1   = queue[queue_first].v;
		queue_first++;

		/*
		 *	Look at the three neighboring IdealVertices.
		 */
		for (f = 0; f < 4; f++)
		{
			if (f == v1)
				continue;

			nbr   = tet1->neighbor[f];
			nbr_v = EVALUATE(tet1->gluing[f], v1);

			/*
			 *	If the neighbor's cusp hasn't been set...
			 */
			if (nbr->cusp[nbr_v] == NULL)
			{
				/*
				 *	...set it...
				 */
				nbr->cusp[nbr_v] = cusp;

				/*
				 *	...and add it to the end of the queue.
				 */
				++queue_last;
				queue[queue_last].tet	= nbr;
				queue[queue_last].v		= nbr_v;
			}
		}
	}
	while (queue_first <= queue_last);

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


void count_cusps(
	Triangulation	*manifold)
{
	Cusp	*cusp;

	manifold->num_cusps			= 0;
	manifold->num_or_cusps		= 0;
	manifold->num_nonor_cusps	= 0;

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

		switch (cusp->topology)
		{
			case torus_cusp:
				manifold->num_or_cusps++;
				break;

			case Klein_cusp:
				manifold->num_nonor_cusps++;
				break;

			default:
				uFatalError("count_cusps", "cusps.c");
		}
	}
}


Boolean mark_fake_cusps(
	Triangulation	*manifold)
{
	int		real_cusp_count,
			fake_cusp_count;
	Cusp	*cusp;
	
	compute_cusp_Euler_characteristics(manifold);
	
	real_cusp_count = 0;
	fake_cusp_count = 0;

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

		switch (cusp->euler_characteristic)
		{
			case 0:
				cusp->is_finite = FALSE;
				cusp->index = real_cusp_count++;
				break;
			
			case 2:
				cusp->is_finite = TRUE;
				cusp->index = --fake_cusp_count;
				break;
			
			default:
				uFatalError("mark_fake_cusps", "cusps.c");
		}
	
	return (fake_cusp_count < 0);
}


static void compute_cusp_Euler_characteristics(
	Triangulation	*manifold)
{
	Cusp		*cusp;
	EdgeClass	*edge;
	Tetrahedron	*tet;
	VertexIndex	v,
				v0,
				v1;

	/*
	 *	Initialize all Euler characteristics to zero.
	 */

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

		cusp->euler_characteristic = 0;
	
	/*
	 *	It'll be easier to count the edges twice (once from each side)
	 *	so compute twice the Euler characteristic and divide by two
	 *	at the end. 
	 */
	 
	/*
	 *	Count the vertices in the triangulation of the boundary,
	 *	which come from edges in the manifold itself.
	 */

	for (edge = manifold->edge_list_begin.next;
		 edge != &manifold->edge_list_end;
		 edge = edge->next)
	{
		tet	= edge->incident_tet;
		v0	=   one_vertex_at_edge[edge->incident_edge_index];
		v1	= other_vertex_at_edge[edge->incident_edge_index];
		tet->cusp[v0]->euler_characteristic += 2;
		tet->cusp[v1]->euler_characteristic += 2;
	}
	
	/*
	 *	Count the edges in the triangulation of the boundary,
	 *	which come from faces in the manifold itself.
	 */

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

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

			tet->cusp[v]->euler_characteristic -= 3;
	
	/*
	 *	Count the faces in the triangulation of the boundary,
	 *	which come from tetrahedra in the manifold itself.
	 */

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

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

			tet->cusp[v]->euler_characteristic += 2;

	/*
	 *	Divide by two (cf. above).
	 */

	for (cusp = manifold->cusp_list_begin.next;
		 cusp != &manifold->cusp_list_end;
		 cusp = cusp->next)
	{
		if (cusp->euler_characteristic % 2 != 0)
			uFatalError("compute_cusp_Euler_characteristics", "cusps.c");
		cusp->euler_characteristic /= 2;
	}
}