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
|
/*
* tersest_triangulation.c
*
* This file provides the functions
*
* void terse_to_tersest( TerseTriangulation *terse,
* TersestTriangulation tersest);
*
* void tersest_to_terse( TersestTriangulation tersest,
* TerseTriangulation **terse);
*
* void tri_to_tersest( Triangulation *manifold,
* TersestTriangulation tersest);
*
* void tersest_to_tri( TersestTriangulation tersest,
* Triangulation **manifold);
*
* terse_to_tersest() and tersest_to_terse() convert back and forth
* between TerseTriangulations and TersestTriangulations.
* tersest_to_terse() allocates space for its result, but
* terse_to_tersest() does not.
*
* tri_to_tersest() is the composition of tri_to_terse() and terse_to_tersest().
* tersest_to_tri() is the composition of tersest_to_terse() and terse_to_tri().
*/
#include "kernel.h"
void terse_to_tersest(
TerseTriangulation *terse,
TersestTriangulation tersest)
{
int i,
j;
double cs,
integer_part;
/*
* The TersestTriangulation is suitable only for Triangulations
* with 7 or fewer Tetrahedra.
*/
if (terse->num_tetrahedra > 7)
uFatalError("terse_to_tersest", "tersest_triangulation");
/*
* Compress the glues_to_old_tet[] array.
*/
for (i = 0; i < 2; i++)
{
tersest[i] = 0;
for (j = 8; --j >= 0; )
{
tersest[i] <<= 1;
if (8*i + j < 2 * terse->num_tetrahedra)
tersest[i] |= terse->glues_to_old_tet[8*i + j];
}
}
/*
* Compress which_old_tet[] and which_gluing[]
*/
for (i = 0; i < terse->num_tetrahedra + 1; i++)
{
tersest[i+2] = terse->which_old_tet[i];
tersest[i+2] <<= 5;
tersest[i+2] |= index_by_permutation[terse->which_gluing[i]];
}
/*
* Set unused bytes to 0.
*/
for (i = terse->num_tetrahedra + 1; i < 8; i++)
tersest[i+2] = 0;
/*
* Compress the Chern-Simons invariant, if present.
*/
if (terse->CS_is_present)
{
/*
* Set the highest-order bit in byte 1 to TRUE.
*/
tersest[1] |= 0x80;
/*
* Copy the given value.
*/
cs = terse->CS_value;
/*
* Make sure it's in the range [-1/4, +1/4).
*/
while (cs < -0.25)
cs += 0.5;
while (cs >= 0.25)
cs -= 0.5;
/*
* cs = 2*cs + 1/2 places cs in the range [0, 1)
*/
cs = 2*cs + 0.5;
/*
* Peel off the significant binary digits 8 at a time
* and stash them in tersest[10] through tersest[17].
*/
for (i = 0; i < 8; i++)
{
/*
* Do a "floating point bitshift".
*/
cs *= (double) 0x100;
/*
* Extract the integer part, and replace cs with
* the fractional part.
*/
cs = modf(cs, &integer_part);
/*
* The integer part should be in the range [0, 256).
* Store it as a 1-byte unsigned integer in tersest[10 + i].
*/
tersest[10 + i] = (unsigned char) integer_part;
}
}
else
{
/*
* Set the highest-order bit in byte 1 to FALSE.
*/
tersest[1] &= 0x7F;
for (i = 0; i < 8; i++)
tersest[10 + i] = 0;
}
}
void tersest_to_terse(
TersestTriangulation tersest,
TerseTriangulation **terse)
{
int i,
j,
byte,
glue_data[16],
num_tetrahedra,
num_free_faces,
bits_read;
double cs;
/*
* Extract the bits for the glues_to_old_tet[] array. We don't yet
* know how many are meaningful, so get all 16 bits, and then use them
* to deduce the number of Tetrahedra. (Yes, I know the last two can't
* possibly be meaningful, but reading them keeps the code simple.)
*/
for (i = 0; i < 2; i++)
{
byte = tersest[i];
for (j = 0; j < 8; j++)
{
glue_data[8*i + j] = byte & 0x01;
byte >>= 1;
}
}
/*
* Deduce the number of Tetrahedra.
*/
num_tetrahedra = 1;
num_free_faces = 4;
bits_read = 0;
while (num_free_faces > 0)
if (glue_data[bits_read++] == TRUE)
{
num_free_faces -= 2;
}
else
{
num_tetrahedra++;
num_free_faces += 2;
}
if (bits_read != 2 * num_tetrahedra
|| num_tetrahedra > 7)
uFatalError("tersest_to_terse", "tersest_triangulation");
/*
* Allocate space for the TerseTriangulation.
*/
(*terse) = alloc_terse(num_tetrahedra);
/*
* Set num_tetrahedra.
*/
(*terse)->num_tetrahedra = num_tetrahedra;
/*
* Set glues_to_old_tet[].
*/
for (i = 0; i < 2 * num_tetrahedra; i++)
(*terse)->glues_to_old_tet[i] = glue_data[i];
/*
* Set which_old_tet[] and which_gluing[].
*/
for (i = 0; i < num_tetrahedra + 1; i++)
{
(*terse)->which_old_tet[i] = tersest[2 + i] >> 5;
(*terse)->which_gluing[i] = permutation_by_index[tersest[2 + i] & 0x1F];
}
/*
* Set the Chern-Simons invariant, if present.
*
* These steps reverse the corresponding steps documented
* in terse_to_tersest() above.
*/
if (tersest[1] & 0x80)
{
(*terse)->CS_is_present = TRUE;
cs = 0.0;
for (i = 8; --i >= 0; )
{
cs += (double) tersest[10 + i];
cs /= (double) 0x100;
}
cs = (cs - 0.5) / 2.0;
(*terse)->CS_value = cs;
}
else
{
(*terse)->CS_is_present = FALSE;
(*terse)->CS_value = 0.0;
}
}
void tri_to_tersest(
Triangulation *manifold,
TersestTriangulation tersest)
{
TerseTriangulation *terse;
terse = tri_to_terse(manifold);
terse_to_tersest(terse, tersest);
free_terse_triangulation(terse);
}
void tersest_to_tri(
TersestTriangulation tersest,
Triangulation **manifold)
{
TerseTriangulation *terse;
tersest_to_terse(tersest, &terse);
*manifold = terse_to_tri(terse);
free_terse_triangulation(terse);
}
|