diff --git a/include/map/Octree.h b/include/map/Octree.h index c2b01b2..daf7f40 100644 --- a/include/map/Octree.h +++ b/include/map/Octree.h @@ -2,22 +2,42 @@ #include #include #include "util.hpp" +#include #define OCT_DIM 32 +struct oct_state { + + int parent_stack_position = 0; + uint64_t parent_stack[32] = { 0 }; + + uint8_t scale = 0; + uint8_t idx_stack[32] = { 0 }; + + uint64_t current_descriptor; + +}; + class Octree { public: + + static const int buffer_size = 100000; + Octree(); ~Octree() {}; - uint64_t *trunk_buffer = new uint64_t[10000]; - uint64_t *descriptor_buffer = new uint64_t[100000]; - uint32_t *attachment_lookup = new uint32_t[100000]; - uint64_t *attachment_buffer = new uint64_t[100000]; + 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 *descriptor_buffer = new uint64_t[buffer_size]{0}; + uint32_t *attachment_lookup = new uint32_t[buffer_size]{0}; + uint64_t *attachment_buffer = new uint64_t[buffer_size]{0}; + unsigned int trunk_cutoff = 3; uint64_t root_index = 0; uint64_t stack_pos = 0x8000; - uint64_t global_pos = 0; + uint64_t global_pos = buffer_size - 50; uint64_t copy_to_stack(std::vector children, unsigned int voxel_scale); @@ -29,6 +49,10 @@ public: private: + std::tuple GenerationRecursion(char* data, sf::Vector3i dimensions, sf::Vector3i pos, unsigned int voxel_scale); + + static char get1DIndexedVoxel(char* data, sf::Vector3i dimensions, sf::Vector3i position); + std::vector anchor_stack; unsigned int octree_voxel_dimension = 32; @@ -58,4 +82,9 @@ private: const uint64_t contour_pointer_mask = 0xFFFFFF00000000; const uint64_t contour_mask = 0xFF00000000000000; + + // ======= DEBUG =========== + int counter = 0; + std::stringstream output_stream; + // ========================= }; diff --git a/include/util.hpp b/include/util.hpp index a9bfa4b..e31063d 100644 --- a/include/util.hpp +++ b/include/util.hpp @@ -50,18 +50,6 @@ private: }; -struct oct_state { - - int parent_stack_position = 0; - uint64_t parent_stack[32] = { 0 }; - - uint8_t scale = 0; - uint8_t idx_stack[32] = { 0 }; - - uint64_t current_descriptor; - -}; - inline sf::Vector3f SphereToCart(sf::Vector2f i) { auto r = sf::Vector3f( diff --git a/src/map/Map.cpp b/src/map/Map.cpp index 3129d5e..5a0d91c 100644 --- a/src/map/Map.cpp +++ b/src/map/Map.cpp @@ -99,7 +99,8 @@ uint64_t Map::generate_children(sf::Vector3i pos, int voxel_scale) { // 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 + // 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 @@ -110,19 +111,6 @@ uint64_t Map::generate_children(sf::Vector3i pos, int voxel_scale) { void Map::generate_octree(unsigned int dimensions) { - // Launch the recursive generator at (0,0,0) as the first point - // and the octree dimension as the initial block size - uint64_t root_node = generate_children(sf::Vector3i(0, 0, 0), OCT_DIM/2); - - // ========= DEBUG ============== - PrettyPrintUINT64(root_node, &output_stream); - output_stream << " " << OCT_DIM << " " << counter++ << std::endl; - // ============================== - - octree.root_index = octree.copy_to_stack(std::vector{root_node}, OCT_DIM); - - // Dump the debug log - DumpLog(&output_stream, "raw_output.txt"); } diff --git a/src/map/Octree.cpp b/src/map/Octree.cpp index 9728677..4c05ce7 100644 --- a/src/map/Octree.cpp +++ b/src/map/Octree.cpp @@ -10,12 +10,43 @@ Octree::Octree() { } +void Octree::Generate(char* data, sf::Vector3i dimensions) { + + // Launch the recursive generator at (0,0,0) as the first point + // and the octree dimension as the initial block size + std::tuple root_node = GenerationRecursion(data, dimensions, sf::Vector3i(0, 0, 0), OCT_DIM/2); + + // ========= DEBUG ============== + PrettyPrintUINT64(std::get<0>(root_node), &output_stream); + output_stream << " " << OCT_DIM << " " << counter++ << std::endl; + // ============================== + + // ============= TEMP!!! =================== + if (stack_pos - 1 > stack_pos) { + global_pos -= stack_pos; + stack_pos = 0x8000; + } + else { + stack_pos -= 1; + } + + memcpy(&descriptor_buffer[stack_pos + global_pos], &std::get<0>(root_node), 1 * sizeof(uint64_t)); + // ======================================== + + 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 +// 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 @@ -166,3 +197,106 @@ void Octree::print_block(int block_pos) { } +std::tuple Octree::GenerationRecursion(char* data, sf::Vector3i dimensions, sf::Vector3i pos, unsigned 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) + }; + + // A tuple holding the child descriptor that we're going to fill out and the + // absolute position of it within the descriptor buffer + std::tuple descriptor_and_position(0, 0); + + // 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) { + + // Setting the individual valid mask bits + // These don't bound check, should they? + for (int i = 0; i < v.size(); i++) { + if (get1DIndexedVoxel(data, dimensions, v.at(i))) + SetBit(i + 16, &std::get<0>(descriptor_and_position)); + } + + // We are querying leafs, so we need to fill the leaf mask + std::get<0>(descriptor_and_position) |= 0xFF000000; + + // The CP will be left blank, contour mask and ptr will need to + // be added here later + return descriptor_and_position; + + } + + std::vector> descriptor_position_array; + + // Generate down the recursion, returning the descriptor of the current node + for (int i = 0; i < v.size(); i++) { + + std::tuple child(0, 0); + + // Get the child descriptor from the i'th to 8th subvoxel + child = GenerationRecursion(data, dimensions, v.at(i), voxel_scale / 2); + + // =========== Debug =========== + PrettyPrintUINT64(std::get<0>(child), &output_stream); + output_stream << " " << voxel_scale << " " << counter++ << std::endl; + // ============================= + + // If the child is a leaf (contiguous) of non-valid values + if (IsLeaf(std::get<0>(child)) && !CheckLeafSign(std::get<0>(child))) { + // Leave the valid mask 0, set leaf mask to 1 + SetBit(i + 16 + 8, &std::get<0>(descriptor_and_position)); + } + + // If the child is valid and not a leaf + else { + + // Set the valid mask, and add it to the descriptor array + SetBit(i + 16, &std::get<0>(descriptor_and_position)); + descriptor_position_array.push_back(child); + } + } + + // We are working bottom up so we need to subtract from the stack position + // the amount of elements we want to use + + for (auto desc_pos: descriptor_position_array) { + + } + + if (stack_pos - descriptor_array.size() > stack_pos) { + global_pos = stack_pos; + stack_pos = 0x8000; + } + else { + stack_pos -= descriptor_array.size(); + } + + + + + memcpy(&descriptor_buffer[stack_pos + global_pos], descriptor_array.data(), descriptor_array.size() * sizeof(uint64_t)); + + + + + // Return the node up the stack + return descriptor_and_position; +} + +char Octree::get1DIndexedVoxel(char* data, sf::Vector3i dimensions, sf::Vector3i position) { + return data[position.x + OCT_DIM * (position.y + OCT_DIM * position.z)]; +} + +