From 2ad738340654969026352878bc37f0fdabf4a7e3 Mon Sep 17 00:00:00 2001 From: MitchellHansen Date: Wed, 22 Mar 2017 23:24:43 -0700 Subject: [PATCH] Added a struct to pass away from get_voxel --- include/Map.h | 26 ++++++++--------- include/util.hpp | 57 ------------------------------------ src/Map.cpp | 75 +++++++++++++++++++----------------------------- 3 files changed, 43 insertions(+), 115 deletions(-) diff --git a/include/Map.h b/include/Map.h index 5626be2..7366f54 100644 --- a/include/Map.h +++ b/include/Map.h @@ -29,6 +29,18 @@ struct XYZHasher { } }; +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: Octree(); @@ -36,6 +48,7 @@ public: uint64_t *blob = new uint64_t[100000]; + uint64_t root_index = 0; uint64_t stack_pos = 0x8000; uint64_t global_pos = 0; @@ -98,23 +111,10 @@ private: std::stringstream output_stream; // ========================= - uint64_t generate_children(sf::Vector3i pos, int dim); char* voxel_data = new char[OCT_DIM * OCT_DIM * OCT_DIM]; - double* height_map; - - // 2^k - int chunk_radius = 6; - - sf::Vector3i world_to_chunk(sf::Vector3i world_coords) { - return sf::Vector3i( - world_coords.x / CHUNK_DIM + 1, - world_coords.y / CHUNK_DIM + 1, - world_coords.z / CHUNK_DIM + 1 - ); - } }; diff --git a/include/util.hpp b/include/util.hpp index 946751c..dca47fb 100644 --- a/include/util.hpp +++ b/include/util.hpp @@ -125,7 +125,6 @@ inline sf::Vector3f FixOrigin(sf::Vector3f base, sf::Vector3f head) { return head - base; } - inline sf::Vector3f Normalize(sf::Vector3f in) { float multiplier = sqrt(in.x * in.x + in.y * in.y + in.z * in.z); @@ -138,7 +137,6 @@ inline sf::Vector3f Normalize(sf::Vector3f in) { } - inline float DotProduct(sf::Vector3f a, sf::Vector3f b){ return a.x * b.x + a.y * b.y + a.z * b.z; } @@ -208,61 +206,6 @@ inline void DumpLog(std::stringstream* ss, std::string file_name) { } -inline std::string sfml_get_input(sf::RenderWindow *window) { - - std::stringstream ss; - - sf::Event event; - while (window->pollEvent(event)) { - if (event.type == sf::Event::TextEntered) { - ss << event.text.unicode; - } - - else if (event.type == sf::Event::KeyPressed) { - if (event.key.code == sf::Keyboard::Return) { - return ss.str(); - } - } - } -} - -inline std::vector sfml_get_float_input(sf::RenderWindow *window) { - - std::stringstream ss; - - sf::Event event; - while (true) { - - if (window->pollEvent(event)) { - - if (event.type == sf::Event::TextEntered) { - if (event.text.unicode > 47 && event.text.unicode < 58 || event.text.unicode == 32) - ss << static_cast(event.text.unicode); - } - - else if (event.type == sf::Event::KeyPressed) { - - if (event.key.code == sf::Keyboard::Return) { - break; - } - } - } - } - - std::istream_iterator begin(ss); - std::istream_iterator end; - std::vector vstrings(begin, end); - - std::vector ret; - - for (auto i: vstrings) { - ret.push_back(std::stof(i)); - } - - return ret; - -} - #ifdef _MSC_VER # include # define __builtin_popcount _mm_popcnt_u32 diff --git a/src/Map.cpp b/src/Map.cpp index 28e9552..b111f2d 100644 --- a/src/Map.cpp +++ b/src/Map.cpp @@ -74,7 +74,7 @@ Map::Map(sf::Vector3i position) { if (rand() % 25 < 2) voxel_data[i] = 1; else - voxel_data[i] = 0; + voxel_data[i] = 1; } } @@ -172,14 +172,14 @@ void Map::generate_octree() { uint64_t tmp = 0; // ========= DEBUG ============== - // PrettyPrintUINT64(root_node, &output_stream); - // output_stream << " " << OCT_DIM << " " << counter++ << std::endl; + PrettyPrintUINT64(root_node, &output_stream); + output_stream << " " << OCT_DIM << " " << counter++ << std::endl; // ============================== - int position = a.copy_to_stack(std::vector{root_node}); + a.root_index = a.copy_to_stack(std::vector{root_node}); // Dump the debug log - // DumpLog(&output_stream, "raw_output.txt"); + DumpLog(&output_stream, "raw_output.txt"); } @@ -227,19 +227,19 @@ void Map::test_map() { sf::Clock timer; timer.restart(); - + for (int x = 0; x < OCT_DIM; x++) { for (int y = 0; y < OCT_DIM; y++) { for (int z = 0; z < OCT_DIM; z++) { sf::Vector3i pos(x, y, z); - bool arr2 = getVoxelFromOctree(pos); + bool arr1 = getVoxel(pos); } } } - std::cout << "Octree linear xyz access : "; + std::cout << "Array linear xyz access : "; std::cout << timer.restart().asMicroseconds() << " microseconds" << std::endl; for (int x = 0; x < OCT_DIM; x++) { @@ -248,14 +248,16 @@ void Map::test_map() { sf::Vector3i pos(x, y, z); - bool arr1 = getVoxel(pos); + bool arr2 = getVoxelFromOctree(pos); } } } - std::cout << "Array linear xyz access : "; + std::cout << "Octree linear xyz access : "; std::cout << timer.restart().asMicroseconds() << " microseconds" << std::endl; + + } Octree::Octree() { @@ -293,23 +295,12 @@ uint64_t Octree::copy_to_stack(std::vector children) { bool Octree::get_voxel(sf::Vector3i position) { - // Init the parent stack - int parent_stack_position = 0; - uint64_t parent_stack[32] = { 0 }; - - // and push the head node - uint64_t head = blob[stack_pos]; - parent_stack[parent_stack_position] = head; - - // Get the index of the first child of the head node - uint64_t index = head & child_pointer_mask; + // Struct that holds the state necessary to continue the traversal from the found voxel + oct_state state; - // Init the idx stack - uint8_t scale = 0; - uint8_t idx_stack[32] = { 0 }; - - // Init the idx stack (DEBUG) - //std::vector> scale_stack(static_cast(log2(OCT_DIM))); + // push the root node to the parent stack + uint64_t head = blob[root_index]; + state.parent_stack[state.parent_stack_position] = head; // Set our initial dimension and the position at the corner of the oct to keep track of our position int dimension = OCT_DIM; @@ -343,10 +334,7 @@ bool Octree::get_voxel(sf::Vector3i position) { mask_index += 1; // Set the idx to represent the move - idx_stack[scale] |= idx_set_x_mask; - - // Debug - //scale_stack.at(static_cast(log2(OCT_DIM) - log2(dimension))).set(0); + state.idx_stack[state.scale] |= idx_set_x_mask; } if (position.y >= (dimension / 2) + quad_position.y) { @@ -355,8 +343,8 @@ bool Octree::get_voxel(sf::Vector3i position) { mask_index += 2; - idx_stack[scale] ^= idx_set_y_mask; - //scale_stack.at(static_cast(log2(OCT_DIM) - log2(dimension))).set(1); + state.idx_stack[state.scale] ^= idx_set_y_mask; + } if (position.z >= (dimension / 2) + quad_position.z) { @@ -364,12 +352,9 @@ bool Octree::get_voxel(sf::Vector3i position) { mask_index += 4; - idx_stack[scale] |= idx_set_z_mask; - //scale_stack.at(static_cast(log2(OCT_DIM) - log2(dimension))).set(2); - } + state.idx_stack[state.scale] |= idx_set_z_mask; - uint64_t out1 = (head >> 16) & mask_8[mask_index]; - uint64_t out2 = (head >> 24) & mask_8[mask_index]; + } // Check to see if we are on a valid oct if ((head >> 16) & mask_8[mask_index]) { @@ -382,20 +367,20 @@ bool Octree::get_voxel(sf::Vector3i position) { } // If all went well and we found a valid non-leaf oct then we will traverse further down the hierarchy - scale++; + state.scale++; dimension /= 2; // Count the number of valid octs that come before and add it to the index to get the position - int count = count_bits((uint8_t)(head >> 16) & count_mask_8[mask_index]); + // Negate it by one as it counts itself + int count = count_bits((uint8_t)(head >> 16) & count_mask_8[mask_index]) - 1; - // Because we are getting the position at the first child we need to back up one - // Or maybe it's because my count bits function is wrong... - index = (head & child_pointer_mask) + count - 1; - head = blob[index]; + // access the element at which head points to and then add the specified number of indices + // to get to the correct child descriptor + head = blob[(head & child_pointer_mask) + count]; // Increment the parent stack position and put the new oct node as the parent - parent_stack_position++; - parent_stack[parent_stack_position] = head; + state.parent_stack_position++; + state.parent_stack[state.parent_stack_position] = head; } else {