diff --git a/include/map/Map.h b/include/map/Map.h index d7c364d..aafaee1 100644 --- a/include/map/Map.h +++ b/include/map/Map.h @@ -24,8 +24,6 @@ public: Map(uint32_t dimensions); - void dump_logs(); - void setVoxel(sf::Vector3i position, int val); bool getVoxelFromOctree(sf::Vector3i position); @@ -44,9 +42,6 @@ private: void generate_octree(unsigned int dimensions); - // Generate children is the main recursive function - uint64_t generate_children(sf::Vector3i pos, int dim); - char* voxel_data; }; diff --git a/include/map/Octree.h b/include/map/Octree.h index 240cbb1..afa6041 100644 --- a/include/map/Octree.h +++ b/include/map/Octree.h @@ -31,23 +31,23 @@ public: void Generate(char* data, sf::Vector3i dimensions); void Load(std::string octree_file_name); - uint64_t *trunk_buffer = new uint64_t[buffer_size]{0}; + uint64_t *trunk_buffer; uint64_t trunk_buffer_position = buffer_size; - uint64_t *descriptor_buffer = new uint64_t[buffer_size]{0}; + uint64_t *descriptor_buffer; uint64_t descriptor_buffer_position = buffer_size; - uint32_t *attachment_lookup = new uint32_t[buffer_size]{0}; + uint32_t *attachment_lookup; uint64_t attachment_lookup_position = buffer_size; - uint64_t *attachment_buffer = new uint64_t[buffer_size]{0}; + uint64_t *attachment_buffer; uint64_t attachment_buffer_position = buffer_size; unsigned int trunk_cutoff = 3; uint64_t root_index = 0; int page_header_counter = 0x8000; - uint64_t current_info_section_position = buffer_size - 50; + uint64_t current_info_section_position = ((uint64_t)0)-1; uint64_t stack_pos = 0x8000; uint64_t global_pos = buffer_size - 50; @@ -68,8 +68,8 @@ private: sf::Vector3i pos, // position of this generation node unsigned int voxel_scale // the voxel scale of this node ); - - static char get1DIndexedVoxel(char* data, sf::Vector3i dimensions, sf::Vector3i position); + + char get1DIndexedVoxel(char* data, sf::Vector3i dimensions, sf::Vector3i position); std::vector anchor_stack; unsigned int octree_voxel_dimension = 32; diff --git a/include/util.hpp b/include/util.hpp index e31063d..881a139 100644 --- a/include/util.hpp +++ b/include/util.hpp @@ -151,7 +151,7 @@ inline void PrettyPrintUINT64(uint64_t i, std::stringstream* ss) { *ss << "[" << std::bitset<1>(i >> 15) << "]"; *ss << "[" << std::bitset<8>(i >> 16) << "]"; *ss << "[" << std::bitset<8>(i >> 24) << "]"; - *ss << "[" << std::bitset<32>(i >> 32) << "]"; + *ss << "[" << std::bitset<32>(i >> 32) << "]\n"; } inline void PrettyPrintUINT64(uint64_t i) { diff --git a/src/main.cpp b/src/main.cpp index d3a8055..0db92c4 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -93,7 +93,6 @@ int main() { // ============================= Map _map(32); //_map.test(); - _map.dump_logs(); std::cin.get(); return 0; // ============================= diff --git a/src/map/Map.cpp b/src/map/Map.cpp index 5a0d91c..4d9de8c 100644 --- a/src/map/Map.cpp +++ b/src/map/Map.cpp @@ -17,100 +17,9 @@ Map::Map(uint32_t dimensions) { generate_octree(dimensions); } - -void Map::dump_logs() { - - octree.print_block(0); -} - -uint64_t Map::generate_children(sf::Vector3i pos, int voxel_scale) { - - - // The 8 subvoxel coords starting from the 1th direction, the direction of the origin of the 3d grid - // XY, Z++, XY - std::vector v = { - sf::Vector3i(pos.x , pos.y , pos.z), - sf::Vector3i(pos.x + voxel_scale, pos.y , pos.z), - sf::Vector3i(pos.x , pos.y + voxel_scale, pos.z), - sf::Vector3i(pos.x + voxel_scale, pos.y + voxel_scale, pos.z), - sf::Vector3i(pos.x , pos.y , pos.z + voxel_scale), - sf::Vector3i(pos.x + voxel_scale, pos.y , pos.z + voxel_scale), - sf::Vector3i(pos.x , pos.y + voxel_scale, pos.z + voxel_scale), - sf::Vector3i(pos.x + voxel_scale, pos.y + voxel_scale, pos.z + voxel_scale) - }; - - // If we hit the 1th voxel scale then we need to query the 3D grid - // and get the voxel at that position. I assume in the future when I - // want to do chunking / loading of raw data I can edit the voxel access - if (voxel_scale == 1) { - - uint64_t child_descriptor = 0; - - // Setting the individual valid mask bits - // These don't bound check, should they? - for (int i = 0; i < v.size(); i++) { - if (getVoxel(v.at(i))) - SetBit(i + 16, &child_descriptor); - } - - // We are querying leafs, so we need to fill the leaf mask - child_descriptor |= 0xFF000000; - - // The CP will be left blank, contour mask and ptr will need to - // be added here later - return child_descriptor; - - } - - // Init a blank child descriptor for this node - uint64_t child_descriptor = 0; - - std::vector descriptor_array; - std::vector index_array; - - // Generate down the recursion, returning the descriptor of the current node - for (int i = 0; i < v.size(); i++) { - - uint64_t child = 0; - - // Get the child descriptor from the i'th to 8th subvoxel - child = generate_children(v.at(i), voxel_scale / 2); - - // =========== Debug =========== - PrettyPrintUINT64(child, &output_stream); - output_stream << " " << voxel_scale << " " << counter++ << std::endl; - // ============================= - - // If the child is a leaf (contiguous) of non-valid values - if (IsLeaf(child) && !CheckLeafSign(child)) { - // Leave the valid mask 0, set leaf mask to 1 - SetBit(i + 16 + 8, &child_descriptor); - } - - // If the child is valid and not a leaf - else { - - // Set the valid mask, and add it to the descriptor array - SetBit(i + 16, &child_descriptor); - descriptor_array.push_back(child); - } - } - - // Any free space between the child descriptors must be added here in order to - // interlace them and allow the memory handler to work correctly. - - // Copy the children to the stack and set the child_descriptors pointer - // to the correct value - child_descriptor |= octree.copy_to_stack(descriptor_array, voxel_scale); - - // Free space may also be allocated here as well - - // Return the node up the stack - return child_descriptor; -} - void Map::generate_octree(unsigned int dimensions) { - + + octree.Generate(voxel_data, sf::Vector3i(dimensions, dimensions, dimensions)); } diff --git a/src/map/Octree.cpp b/src/map/Octree.cpp index 6db446a..d6dfee0 100644 --- a/src/map/Octree.cpp +++ b/src/map/Octree.cpp @@ -2,11 +2,12 @@ Octree::Octree() { - // initialize the first stack block + // initialize the the buffers to 0's + trunk_buffer = new uint64_t[buffer_size](); + descriptor_buffer = new uint64_t[buffer_size](); + attachment_lookup = new uint32_t[buffer_size](); + attachment_buffer = new uint64_t[buffer_size](); - for (int i = 0; i < 0x8000; i++) { - descriptor_buffer[i] = 0; - } } @@ -30,55 +31,21 @@ void Octree::Generate(char* data, sf::Vector3i dimensions) { stack_pos -= 1; } - memcpy(&descriptor_buffer[stack_pos + global_pos], &std::get<0>(root_node), 1 * sizeof(uint64_t)); + memcpy(&descriptor_buffer[descriptor_buffer_position], &std::get<0>(root_node), sizeof(uint64_t)); + descriptor_buffer_position--; + // ======================================== DumpLog(&output_stream, "raw_output.txt"); -} - -// Copy to stack enables the hybrid depth-breadth first tree by taking -// a list of valid non-leaf child descriptors contained under a common parent. - -// It takes the list of children, and the current level in the voxel hierarchy. -// It returns the index to the first element of the + output_stream.str(""); -// This is all fine and dandy, but we have the problem where we need to assign -// relative pointers to objects so we need to keep track of where their children are -// being assigned. - -uint64_t Octree::copy_to_stack(std::vector children, unsigned int voxel_scale) { - - // Check for the 15 bit boundry - if (stack_pos - children.size() > stack_pos) { - global_pos = stack_pos; - stack_pos = 0x8000; - } - else { - stack_pos -= children.size(); + for (int i = 0; i < buffer_size; i++) { + PrettyPrintUINT64(descriptor_buffer[i], &output_stream); } - // Copy to stack needs to keep track of an "anchor_stack" which will hopefully facilitate - // relative pointer generation for items being copied to the stack + DumpLog(&output_stream, "raw_data.txt"); - // We need to return the relative pointer to the child node list - // 16 bits, one far bit, one sign bit? 14 bits == +- 16384 - - // Worth halving the ptr reach to enable backwards ptrs? - // could increase packability allowing far ptrs and attachments to come before or after - - - - //stack_pos -= children.size(); - memcpy(&descriptor_buffer[stack_pos + global_pos], children.data(), children.size() * sizeof(uint64_t)); - - // Return the bitmask encoding the index of that value - // If we tripped the far bit, allocate a far index to the stack and place - // it at the bottom of the child_descriptor node level array - // And then shift the far bit to 1 - - // If not, shift the index to its correct place - return stack_pos; } bool Octree::get_voxel(sf::Vector3i position) { @@ -291,11 +258,12 @@ std::tuple Octree::GenerationRecursion(char* data, sf::Vecto uint64_t far_pointer_block_position = descriptor_buffer_position; // Count the far pointers we need to allocate - for (int i = descriptor_position_array.size() - 1; i >= 0; i--) { + for (int i = 0; i < descriptor_position_array.size(); i++) { // this is not the actual relative distance write, so we pessimistically guess that we will have // the worst relative distance via the insertion size - uint64_t relative_distance = std::get<1>(descriptor_position_array.at(i)) - (descriptor_buffer_position - worst_case_insertion_size); + + int relative_distance = std::get<1>(descriptor_position_array.at(i)) - (descriptor_buffer_position - worst_case_insertion_size); // check to see if we tripped the far pointer if (relative_distance > 0x8000) { @@ -303,16 +271,17 @@ std::tuple Octree::GenerationRecursion(char* data, sf::Vecto // This is writing the ABSOLUTE POSITION for far pointers, is this what I want? memcpy(&descriptor_buffer[descriptor_buffer_position], &std::get<1>(descriptor_position_array.at(i)), sizeof(uint64_t)); descriptor_buffer_position--; + page_header_counter--; far_pointer_count++; } } // We gotta go backwards as memcpy of a vector can be emulated by starting from the rear - for (int i = descriptor_position_array.size() - 1; i >= 0; i--) { + for (int i = 0; i < descriptor_position_array.size(); i++) { // just gonna redo the far pointer check loosing a couple of cycles but oh well - uint64_t relative_distance = std::get<1>(descriptor_position_array.at(i)) - descriptor_buffer_position; + int relative_distance = std::get<1>(descriptor_position_array.at(i)) - descriptor_buffer_position; uint64_t descriptor = std::get<0>(descriptor_position_array.at(i)); @@ -324,15 +293,16 @@ std::tuple Octree::GenerationRecursion(char* data, sf::Vecto far_pointer_block_position--; - } else { - - descriptor |= relative_distance; - + } else if (relative_distance > 0) { + + descriptor |= (uint64_t)relative_distance; + } // We have finished building the CD so we push it onto the buffer memcpy(&descriptor_buffer[descriptor_buffer_position], &descriptor, sizeof(uint64_t)); descriptor_buffer_position--; + page_header_counter--; }