-
Notifications
You must be signed in to change notification settings - Fork 0
/
hypersphere.cpp
127 lines (105 loc) · 3.03 KB
/
hypersphere.cpp
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
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <float.h>
#include "util.h"
#include "hypersphere.h"
// cached tessellations of S^3
#define MAX_LEVELS 20
hypersphere_tessellation_t tessellations[MAX_LEVELS];
#define PROXIMITY_QUEUE_SIZE 100
#define PROXIMITY_QUEUE_MEMORY_LIMIT 1e6
/*
* Reproject mesh vertices onto the unit hypersphere.
*/
static void reproject_vertices(double **vertices, int nv, int dim)
{
int i;
for (i = 0; i < nv; i++) {
double d = norm(vertices[i], dim);
mult(vertices[i], vertices[i], 1/d, dim);
}
}
/*
* Create the initial (low-res) mesh of S3 (in R4).
*/
static octetramesh_t *init_mesh_S3_octetra()
{
octetramesh_t *mesh;
safe_calloc(mesh, 1, octetramesh_t);
octetramesh_new(mesh, 8, 16, 0, 4);
double vertices[8][4] = {{1,0,0,0}, {0,1,0,0}, {0,0,1,0}, {0,0,0,1},
{-1,0,0,0}, {0,-1,0,0}, {0,0,-1,0}, {0,0,0,-1}};
int tetrahedra[16][4] = {{0,1,2,3}, {0,1,2,7}, {0,1,6,3}, {0,5,2,3},
{0,1,6,7}, {0,5,2,7}, {0,5,6,3}, {0,5,6,7},
{4,1,2,3}, {4,1,2,7}, {4,1,6,3}, {4,5,2,3},
{4,1,6,7}, {4,5,2,7}, {4,5,6,3}, {4,5,6,7}};
memcpy(mesh->vertices[0], vertices, 8*4*sizeof(double));
memcpy(mesh->tetrahedra[0], tetrahedra, 16*4*sizeof(int));
return mesh;
}
/*
* Create a tesselation of the 3-sphere (in R4) at a given level (# of subdivisions)
*/
static octetramesh_t *build_octetra(int level)
{
int i;
octetramesh_t *mesh = init_mesh_S3_octetra();
octetramesh_t tmp;
for (i = 0; i < level; i++) {
octetramesh_subdivide(&tmp, mesh);
octetramesh_free(mesh);
free(mesh);
mesh = octetramesh_clone(&tmp);
octetramesh_free(&tmp);
}
reproject_vertices(mesh->vertices, mesh->nv, mesh->d);
return mesh;
}
/*
* Fill in the fields of a hypersphere_tessellation from an octetramesh.
*/
static void octetramesh_to_tessellation(hypersphere_tessellation_t *T, octetramesh_t *mesh)
{
// get tetramesh
T->tetramesh = octetramesh_to_tetramesh(mesh);
// dimensions
T->d = 4;
T->n = T->tetramesh->nt;
int n = T->n, d = T->d;
// compute cell centroids and volumes
T->centroids = new_matrix2(n, d);
safe_calloc(T->volumes, n, double);
tetramesh_centroids(T->centroids, T->volumes, T->tetramesh);
reproject_vertices(T->centroids, n, d);
}
/*
* Pre-cache some tessellations of hyperspheres.
*/
void hypersphere_init()
{
int i;
const int levels = 5; //7;
memset(tessellations, 0, MAX_LEVELS*sizeof(hypersphere_tessellation_t));
for (i = 0; i < levels; i++) {
octetramesh_t *mesh = build_octetra(i);
octetramesh_to_tessellation(&tessellations[i], mesh);
}
}
/*
* Returns a tesselation of the 3-sphere (in R4) with at least n cells.
*/
hypersphere_tessellation_t *tessellate_S3(int n)
{
int i;
for (i = 0; i < MAX_LEVELS; i++) {
if (tessellations[i].tetramesh == NULL) {
octetramesh_t *mesh = build_octetra(i);
octetramesh_to_tessellation(&tessellations[i], mesh);
}
if (tessellations[i].tetramesh->nt >= n)
return &tessellations[i];
}
return &tessellations[MAX_LEVELS-1];
}