Skip to content

Instantly share code, notes, and snippets.

@nemerle
Last active April 10, 2018 15:57
Show Gist options
  • Save nemerle/7502cb98880262f81114bb5a8bbdf677 to your computer and use it in GitHub Desktop.
Save nemerle/7502cb98880262f81114bb5a8bbdf677 to your computer and use it in GitHub Desktop.
bintree packing code
static int storeBinTreesResult(BitStream &bs,const std::array<bintree_in,7> &bintree)
{
// this least means that midpoint ( idx 0 ) depends on midpoint, but that's check with idx==0
// then checking S - * - M - - - E (idx 1) depends on actual midpoint, and we check it
// then checking S - - - M - * - E (idx 2) same as above
// then checking S * - - M - - - E (idx 3) checking that we had SM/2 midpoint
// then checking S - - * M - - - E (idx 4) checking that we had SM/2 midpoint
// ...
static const uint8_t dependency_indices[] = {0,0,0,1,1,2,2};
static const uint8_t tree_depth_bits_mod[8] = { 1,2,2,3,3,3,3 };
char num_bits;
int val_sign;
char base_bitcount;
int res=0;
std::array<bintree_in,7> tree_copy = bintree;
base_bitcount = g_interpDataPrecision + 5;
if(tree_copy[0].has_height && tree_copy[0].has_other)
{
// we have both values
bs.StoreBits(1,1);
}
else
{
bs.StoreBits(1,tree_copy[0].has_height); // otherwise it is assumed that has_other is true
}
for (int idx = 0; idx < 7; ++idx )
{
if ( (idx && !tree_copy[dependency_indices[idx]].has_height) || tree_depth_bits_mod[idx] > g_interpolation_level )
tree_copy[idx].has_height = 0; // mark dependency as missing
else
{
if ( idx )
bs.StoreBits(1, tree_copy[idx].has_height);
if ( tree_copy[idx].has_height )
{
num_bits = base_bitcount - tree_depth_bits_mod[idx];
res = 1;
bs.StoreBits(1,tree_copy[idx].y<0);
bs.StoreBits(num_bits,std::abs(tree_copy[idx].y));
}
}
}
for (int idx = 0; idx < 7; ++idx )
{
// check if the 'source' value for the one located at idx has xz values,
if ( (idx && !tree_copy[dependency_indices[idx]].has_other) || tree_depth_bits_mod[idx] > g_interpolation_level )
tree_copy[idx].has_other = false;
else
{
if ( idx )
bs.StoreBits(1, tree_copy[idx].has_other);
if ( tree_copy[idx].has_other )
{
num_bits = base_bitcount - tree_depth_bits_mod[idx];
res = 1;
bs.StoreBits(1,tree_copy[idx].x<0);
bs.StoreBits(num_bits,std::abs(tree_copy[idx].x));
bs.StoreBits(1,tree_copy[idx].z<0);
bs.StoreBits(num_bits,std::abs(tree_copy[idx].z));
}
}
}
return res;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment