File: normal_surface_recognition.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 (516 lines) | stat: -rw-r--r-- 14,096 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
/*
 *	normal_surface_recognition.c
 *
 *	The function
 *
 *		void recognize_embedded_surface(
 * 								Triangulation	*manifold,
 *								Boolean			*connected,
 *								Boolean			*orientable,
 *								Boolean			*two_sided,
 *								int				*Euler_characteristic);
 *
 *	reports the connectedness, orientability, two-sidedness and Euler
 *	characteristic of the normal surface described in the parallel_edge,
 *	num_squares and num_triangles fields of the manifold's Tetrahedra.
 *	The present implementation assumes the manifold has no filled cusps.
 */

#include "kernel.h"
#include "normal_surfaces.h"

typedef struct
{
	/*
	 *	The "positive" normal vector to a square points in the direction of
	 *		tet->parallel_edge (which has EdgeIndex 0, 1 or 2) and away from
	 *		the opposite edge (which has EdgeIndex 5, 4 or 3, respectively).
	 *	The "positive" normal vector to a triangle points in the direction
	 *		of the associated ideal vertex.
	 *
	 *	The algorithm for testing two-sidedness attempts to make a globally
	 *	consistent choice of normal vectors across the whole surface.
	 */
	Boolean	positive_normal;
	
	/*
	 *	A Tetrahedron's right_handed Orientation lets us extend the above
	 *	definition of a positive normal vector to a definition of a positive
	 *	orientation on each square and triangle.  It doesn't matter whether
	 *	you imagine using a right-hand rule or a left-hand rule, just so
	 *	you're consistent.
	 *
	 *	The algorithm for testing orientability attempts to make a globally
	 *	consistent choice of orientation across the whole surface.
	 */
	Boolean	positive_orientation;
	
	/*
	 *	Has the recursive algorithm visited this EmbeddedPolygon?
	 */
	Boolean	visited;
	
} EmbeddedPolygon;

/*
 *	Each Tetrahedron will need one array of squares, and four arrays
 *	of triangles, one for each cusp.
 */
typedef struct
{
	EmbeddedPolygon	*squares,
					*triangles[4];
} PolygonsInTetrahedron;

/*
 *	The algorithm for determining connectedness, orientability and
 *	two-sidedness keeps references to squares and triangles on a
 *	NULL-terminated, singly linked list.
 */

typedef int EmbeddedPolygonType;
enum
{
	embedded_square,
	embedded_triangle
};

typedef struct ListNode
{
	/*
	 *	Which Tetrahedron is the polygon in?
	 */
	Tetrahedron			*tet;
	
	/*
	 *	Is the polygon an embedded_square or an embedded_triangle?
	 */
	EmbeddedPolygonType	type;
	
	/*
	 *	If the polygon is an embedded_triangle, which ideal vertex is it at?
	 */
	VertexIndex			v;
	
	/*
	 *	Parallel copies of a square or triangle are indexed in the
	 *	direction opposite the normal vector defined above.  For example,
	 *	the triangle closest to the ideal vertex has index 0, the next
	 *	closest one has index 1, etc.  Similarly, the square closest
	 *	tet->parallel_edge has index 0, the next closest one has index 1, etc.
	 */
	int					index;
	
	/*
	 *	The next ListNode on the NULL-terminated, singly linked list.
	 */
	struct ListNode		*next;
	
} ListNode;


static void	connected_orientable_twosided(Triangulation *manifold, Boolean *connected, Boolean *orientable, Boolean *two_sided);
static int	Euler_characteristic_of_embedded_surface(Triangulation *manifold);


void recognize_embedded_surface(
	Triangulation	*manifold,
	Boolean			*connected,
	Boolean			*orientable,
	Boolean			*two_sided,
	int				*Euler_characteristic)
{
	/*
	 *	The present version of the software assumes all cusps are complete.
	 */
	if (all_cusps_are_complete(manifold) == FALSE)
		uFatalError("recognize_embedded_surface", "normal_surface_recognition");
	
	/*
	 *	Compute the connectedness, orientability, two-sidedness and
	 *	Euler characteristic.
	 */
	connected_orientable_twosided(manifold, connected, orientable, two_sided);
	*Euler_characteristic = Euler_characteristic_of_embedded_surface(manifold);

	/*
	 *	In an orientable 3-manifold, a surface is orientable iff it's 2-sided.
	 */
	if (manifold->orientability == oriented_manifold
	 && *orientable != *two_sided)
		uFatalError("recognize_embedded_surface", "normal_surface_recognition");

	/*
	 *	An embedded sphere must be 2-sided.
	 */
	if (*connected == TRUE
	 && *Euler_characteristic == 2
	 && *two_sided == FALSE)
		uFatalError("recognize_embedded_surface", "normal_surface_recognition");

	/*
	 *	Orientable surfaces have even Euler characteristic.
	 */
	if (*orientable == TRUE
	 && (*Euler_characteristic)%2 != 0)
		uFatalError("recognize_embedded_surface", "normal_surface_recognition");
}


static void connected_orientable_twosided(
	Triangulation	*manifold,
	Boolean			*connected,
	Boolean			*orientable,
	Boolean			*two_sided)
{
	PolygonsInTetrahedron	*model;
	Tetrahedron				*tet,
							*nbr;
	Permutation				gluing;
	VertexIndex				v,
							nbr_v;
	FaceIndex				f,
							nbr_f;
	int						i,
							index,
							nbr_index;
	ListNode				*list,
							*node,
							*new_node;
	Boolean					positive_normal,
							positive_orientation;
	EmbeddedPolygonType		nbr_type;
	EmbeddedPolygon			*data,
							*nbr_data;
	
	/*
	 *	Make an explicit model of the surface using EmbeddedPolygon structures.
	 */

	model = NEW_ARRAY(manifold->num_tetrahedra, PolygonsInTetrahedron);

	for (tet = manifold->tet_list_begin.next;
		 tet != &manifold->tet_list_end;
		 tet = tet->next)
	{
		/*
		 *	NEW_ARRAY uses my_malloc(), which gracefully handles requests
		 *	for zero bytes when num_squares or num_triangles is zero.
		 */

		model[tet->index].squares = NEW_ARRAY(tet->num_squares, EmbeddedPolygon);
		for (i = 0; i < tet->num_squares; i++)
			model[tet->index].squares[i].visited = FALSE;

		for (v = 0; v < 4; v++)
		{
			model[tet->index].triangles[v] = NEW_ARRAY(tet->num_triangles[v], EmbeddedPolygon);
			for (i = 0; i < tet->num_triangles[v]; i++)
				model[tet->index].triangles[v][i].visited = FALSE;
		}
	}

	/*
	 *	Initialize the linked list to be empty.
	 */
	list = NULL;
	
	/*
	 *	Find an arbitrary embedded square.  The calling routine won't
	 *	create empty or boundary-parallel surfaces, so a square must exist.
	 *	"Visit" the square and set its normal vector and orientation
	 *	to be positive.  Put a reference to the square onto the linked list.
	 *	The linked list will hold squares and triangles which have been
	 *	visited, but whose neighbors have not yet been visited.
	 */
	for (tet = manifold->tet_list_begin.next;
		 tet != &manifold->tet_list_end;
		 tet = tet->next)
		if (tet->num_squares != 0)
		{
			model[tet->index].squares[0].positive_normal		= TRUE;
			model[tet->index].squares[0].positive_orientation	= TRUE;
			model[tet->index].squares[0].visited				= TRUE;
			
			node = NEW_STRUCT(ListNode);
			node->tet	= tet;
			node->type	= embedded_square;
			node->v		= -1; /* unused for a square */
			node->index	= 0;
			node->next	= list;
			list		= node;
			break;
		}
	if (list == NULL)
		uFatalError("connected_orientable_twosided", "normal_surface_recognition");

	/*
	 *	Tentatively assume the surface is orientable and two-sided.
	 *	If the recursion below discovers that it cannot consistently
	 *	assign an orientation or normal vector, it will set the
	 *	corresponding variable to FALSE.
	 */
	*orientable	= TRUE;
	*two_sided	= TRUE;

	/*
	 *	As stated above, the linked list holds squares and triangles which
	 *	have been visited, but whose neighbors have not yet been visited.
	 */
	while (list != NULL)
	{
		/*
		 *	Pull the first node off the list.
		 */
		node = list;
		list = list->next;
		
		/*
		 *	The node defines a square or triangle in the embedded surface.
		 *	Look at each of its neighbors.
		 */
		tet = node->tet;
		for (f = 0; f < 4; f++)
		{
			/*
			 *	A square connects to all four of the tetrahedron's neighbors.
			 *	A triangle connects to only three of them.
			 */
			if (node->type == embedded_triangle  &&  node->v == f)
				continue;
			
			/*
			 *	What vertex (of the tetrahedron) are we at,
			 *	and what's the index of the sheet we're on?
			 */
			switch (node->type)
			{
				case embedded_square:
					if (f == one_vertex_at_edge[tet->parallel_edge])
					{
						v		= other_vertex_at_edge[tet->parallel_edge];
						index	= node->index + tet->num_triangles[v];
					}
					if (f == other_vertex_at_edge[tet->parallel_edge])
					{
						v		= one_vertex_at_edge[tet->parallel_edge];
						index	= node->index + tet->num_triangles[v];
					}
					if (f == one_vertex_at_edge[5 - tet->parallel_edge])
					{
						v		= other_vertex_at_edge[5 - tet->parallel_edge];
						index	= ((tet->num_squares - 1) - node->index) + tet->num_triangles[v];
					}
					if (f == other_vertex_at_edge[5 - tet->parallel_edge])
					{
						v		= one_vertex_at_edge[5 - tet->parallel_edge];
						index	= ((tet->num_squares - 1) - node->index) + tet->num_triangles[v];
					}
					break;
				
				case embedded_triangle:
					v		= node->v;
					index	= node->index;
					break;

				default:  uFatalError("connected_orientable_twosided", "normal_surface_recognition");
			}
			
			/*
			 *	What normal vector and orientation are we passing
			 *	to the neighbor?  Usually it will just be our own
			 *	normal vector and orientation, but in the case of
			 *	an "upside down" square we have to reverse them.
			 */

			if (node->type == embedded_square)
				data = &model[node->tet->index].squares[node->index];
			else
				data = &model[node->tet->index].triangles[node->v][node->index];

			positive_normal			= data->positive_normal;
			positive_orientation	= data->positive_orientation;
			if (data->visited != TRUE)
				uFatalError("connected_orientable_twosided", "normal_surface_recognition");

			if (node->type == embedded_square
			 && edge_between_vertices[v][f] != tet->parallel_edge)
			{
				positive_normal			= ! positive_normal;
				positive_orientation	= ! positive_orientation;
			}
			
			/*
			 *	Find our neighbor.
			 */
			nbr		= tet->neighbor[f];
			gluing	= tet->gluing[f];
			nbr_f	= EVALUATE(gluing, f);
			nbr_v	= EVALUATE(gluing, v);
			
			/*
			 *	If the gluing is orientation_reversing, then
			 *	what the old tetrahedron saw as right-handed,
			 *	the neighbor will see as left-handed, and vice-versa.
			 *	They'll agree on normal vectors, though.
			 */
			if (parity[gluing] == orientation_reversing)
				positive_orientation = ! positive_orientation;
			
			/*
			 *	Find the square or triangle we're connecting to.
			 */
			if (index < nbr->num_triangles[nbr_v])
			{
				nbr_type	= embedded_triangle;
				nbr_index	= index;
				nbr_data	= &model[nbr->index].triangles[nbr_v][nbr_index];
			}
			else
			{
				if (edge3_between_vertices[nbr_f][nbr_v] != nbr->parallel_edge
				 || index >= nbr->num_triangles[nbr_v] + nbr->num_squares)
					uFatalError("connected_orientable_twosided", "normal_surface_recognition");

				nbr_type	= embedded_square;
				nbr_index	= index - nbr->num_triangles[nbr_v];

				/*
				 *	If the square is "upside down", adjust the index,
				 *	orientation and normal vector.
				 */
				if (edge_between_vertices[nbr_f][nbr_v] != nbr->parallel_edge)
				{
					nbr_index = (nbr->num_squares - 1) - nbr_index;
					positive_normal			= ! positive_normal;
					positive_orientation	= ! positive_orientation;
				}
				
				nbr_data = &model[nbr->index].squares[nbr_index];
			}
			
			/*
			 *	Has the newly found square or triangle already been visited?
			 *	If it has, check whether its orientation and normal vector
			 *	agree with the ones we're passing.  If not, assign the
			 *	orientation and normal vectors, and add it to the linked list.
			 */
			if (nbr_data->visited == TRUE)
			{
				if (nbr_data->positive_normal != positive_normal)
					*two_sided = FALSE;
				if (nbr_data->positive_orientation != positive_orientation)
					*orientable = FALSE;
			}
			else
			{
				nbr_data->positive_normal		= positive_normal;
				nbr_data->positive_orientation	= positive_orientation;
				nbr_data->visited				= TRUE;

				new_node = NEW_STRUCT(ListNode);
				new_node->tet	= nbr;
				new_node->type	= nbr_type;
				new_node->v		= (nbr_type == embedded_triangle) ? nbr_v : -1;
				new_node->index	= nbr_index;
				new_node->next	= list;
				list			= new_node;
			}
		}
		
		/*
		 *	Free the node, and continue with the loop.
		 */
		my_free(node);
	}
	
	/*
	 *	The embedded surface is connected iff we visited all its polygons.
	 */
	*connected = TRUE;
	for (tet = manifold->tet_list_begin.next;
		 tet != &manifold->tet_list_end;
		 tet = tet->next)
	{
		for (i = 0; i < tet->num_squares; i++)
			if (model[tet->index].squares[i].visited == FALSE)
				*connected = FALSE;
		for (v = 0; v < 4; v++)
			for (i = 0; i < tet->num_triangles[v]; i++)
				if (model[tet->index].triangles[v][i].visited == FALSE)
					*connected = FALSE;
	}
	
	/*
	 *	Free the memory used to hold the EmbeddedPolygons.
	 */
	for (tet = manifold->tet_list_begin.next;
		 tet != &manifold->tet_list_end;
		 tet = tet->next)
	{
		my_free(model[tet->index].squares);
		for (v = 0; v < 4; v++)
			my_free(model[tet->index].triangles[v]);
	}
	my_free(model);
}


static int Euler_characteristic_of_embedded_surface(
	Triangulation	*manifold)
{
	int			num_vertices,
				num_edges,
				num_faces;
	EdgeClass	*edge;
	Tetrahedron	*tet;
	EdgeIndex	e;
	VertexIndex	v;
	int			total_squares,
				total_triangles;

	/*
	 *	Count the vertices.
	 */
	
	num_vertices = 0;
	
	for (edge = manifold->edge_list_begin.next;
		 edge != &manifold->edge_list_end;
		 edge = edge->next)
	{
		tet	= edge->incident_tet;
		e	= edge->incident_edge_index;

		if (edge3[e] != tet->parallel_edge)
			num_vertices += tet->num_squares;

		num_vertices += tet->num_triangles[one_vertex_at_edge[e]];
		num_vertices += tet->num_triangles[other_vertex_at_edge[e]];
	}
	
	/*
	 *	Count the edges and faces.
	 */
	
	total_squares	= 0;
	total_triangles	= 0;

	for (tet = manifold->tet_list_begin.next;
		 tet != &manifold->tet_list_end;
		 tet = tet->next)
	{
		total_squares += tet->num_squares;

		for (v = 0; v < 4; v++)
			total_triangles += tet->num_triangles[v];
	}
	
	num_edges = (4*total_squares + 3*total_triangles) / 2;
	num_faces = total_squares + total_triangles;

	/*
	 *	Return the Euler characteristic.
	 */
	return num_vertices - num_edges + num_faces;
}