Last active
May 24, 2016 16:45
-
-
Save muhummadPatel/22b4106b329dd0224ecc5bb5da15b1c2 to your computer and use it in GitHub Desktop.
Quick test to figure out how to bitpack a 3d array of voxels into a flat array of ints.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* | |
| * Quick test to figure out how to bitpack a 3d array of voxels into a flat | |
| * array of ints. Need to understand this concept for cgp2 assignment. | |
| * | |
| * Relevant sources: | |
| * http://stackoverflow.com/questions/6916974/change-a-bit-of-an-integer | |
| * http://stackoverflow.com/questions/47981/how-do-you-set-clear-and-toggle-a-single-bit-in-c-c | |
| * http://www.learncpp.com/cpp-tutorial/38-bitwise-operators/ | |
| * | |
| * Muhummad Patel 1-March-2016 | |
| */ | |
| #include <iostream> | |
| #include <stdint.h> | |
| using namespace std; | |
| int* voxels; | |
| int x_size = 2, y_size = 4, z_size = 3; | |
| void setVoxel(int x, int y, int z){ | |
| int bit_to_set = (x_size * y_size * z) + (y_size * y) + x; | |
| int voxel_index = bit_to_set / (sizeof(int) * 8.0); | |
| int offset = bit_to_set % (sizeof(int) * 8); | |
| int* v = &voxels[voxel_index]; | |
| *v |= (1u << offset); | |
| } | |
| void unsetVoxel(int x, int y, int z){ | |
| int bit_to_set = (x_size * y_size * z) + (y_size * y) + x; | |
| int voxel_index = bit_to_set / (sizeof(int) * 8.0); | |
| int offset = bit_to_set % (sizeof(int) * 8); | |
| int* v = &voxels[voxel_index]; | |
| *v &= ~(1u << offset); | |
| } | |
| int getVoxel(int x, int y, int z){ | |
| int bit_to_set = (x_size * y_size * z) + (y_size * y) + x; | |
| int voxel_index = bit_to_set / (sizeof(int) * 8.0); | |
| int offset = bit_to_set % (sizeof(int) * 8); | |
| int v = voxels[voxel_index]; | |
| bool bit = (v >> offset) & 1u; | |
| return bit? 1: 0; | |
| } | |
| void fill(bool val){ | |
| int total_bits = x_size * y_size * z_size; | |
| int bits_per_int = sizeof(int) * 8; | |
| int ints_required = (int)(((1.0 * total_bits) / bits_per_int) + 0.5); | |
| for(int i = 0; i < sizeof(voxels)/sizeof(int); i++){ | |
| if(val){ | |
| voxels[i] = ~0u; | |
| }else{ | |
| voxels[i] = 0u; | |
| } | |
| } | |
| } | |
| int main(){ | |
| cout << "Hello world!" << endl; | |
| int total_bits = x_size * y_size * z_size; | |
| int bits_per_int = sizeof(int) * 8; | |
| int ints_required = (int)(((1.0 * total_bits) / bits_per_int) + 0.5); | |
| voxels = new int[ints_required]; | |
| // for(int x = 0; x < x_size; x++){ | |
| // for(int y = 0; y < y_size; y++){ | |
| // for(int z = 0; z < z_size; z++){ | |
| // unsetVoxel(x, y, z); | |
| // } | |
| // } | |
| // } | |
| fill(true); | |
| setVoxel(1, 2, 2); | |
| cout << "Voxels:" << endl; | |
| for(int z = 0; z < z_size; z++){ | |
| for(int y = 0; y < y_size; y++){ | |
| for(int x = 0; x < x_size; x++){ | |
| cout << getVoxel(x, y, z) << " "; | |
| } | |
| cout << endl; | |
| } | |
| cout << endl; | |
| } | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment