From d1b9ecd3e5288b4a77b2aeb3c10aa3722ea47365 Mon Sep 17 00:00:00 2001 From: MitchellHansen Date: Wed, 22 Mar 2017 00:55:10 -0700 Subject: [PATCH] Octree traversal now works perfectly, edge cases worked out --- include/Map.h | 19 +++++++++++++++++-- src/Map.cpp | 27 +++++++++++++++++++-------- src/main.cpp | 4 ++-- 3 files changed, 38 insertions(+), 12 deletions(-) diff --git a/include/Map.h b/include/Map.h index 6f9ceae..445414e 100644 --- a/include/Map.h +++ b/include/Map.h @@ -95,6 +95,12 @@ public: 0x1F, 0x3F, 0x7F, 0xFF }; + //uint8_t count_mask_8[8]{ + // 0xFF, 0x7F, 0x3F, 0x1F, + // 0xF, 0x7, 0x3, 0x1 + //}; + + // With a position and the head of the stack. Traverse down the voxel hierarchy to find // the IDX and stack position of the highest resolution (maybe set resolution?) oct bool get_voxel(sf::Vector3i position) { @@ -195,7 +201,16 @@ public: // We also need to traverse to the correct child pointer // Count the number of non-leaf octs that come before and add it to the index to get the position - int count = count_bits((uint8_t)(head >> 24) ^ count_mask_8[mask_index]); + int i1 = count_bits((uint8_t)(head >> 16) & count_mask_8[0]); + int i2 = count_bits((uint8_t)(head >> 16) & count_mask_8[1]); + int i3 = count_bits((uint8_t)(head >> 16) & count_mask_8[2]); + int i4 = count_bits((uint8_t)(head >> 16) & count_mask_8[3]); + int i5 = count_bits((uint8_t)(head >> 16) & count_mask_8[4]); + int i6 = count_bits((uint8_t)(head >> 16) & count_mask_8[5]); + int i7 = count_bits((uint8_t)(head >> 16) & count_mask_8[6]); + int i8 = count_bits((uint8_t)(head >> 16) & count_mask_8[7]); + + int count = count_bits((uint8_t)(head >> 16) & count_mask_8[mask_index]); // 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... @@ -208,7 +223,7 @@ public: } else { // If the oct was not valid, then no CP's exists any further - + // This implicitly says that if it's non-valid then it must be a leaf!! // It appears that the traversal is now working but I need // to focus on how to now take care of the end condition. diff --git a/src/Map.cpp b/src/Map.cpp index 26e70b5..f4260f6 100644 --- a/src/Map.cpp +++ b/src/Map.cpp @@ -68,18 +68,27 @@ bool IsLeaf(const uint64_t descriptor) { Map::Map(sf::Vector3i position) { - srand(time(NULL)); + //srand(time(NULL)); - load_unload(position); + //load_unload(position); for (int i = 0; i < OCT_DIM * OCT_DIM * OCT_DIM; i++) { - if (rand() % 2 == 1) - voxel_data[i] = 0; + if (rand() % 25 > 1) + voxel_data[i] = 1; else voxel_data[i] = 1; } - voxel_data[0 + OCT_DIM * (0 + OCT_DIM * 0)] = 1; + + //voxel_data[1 + OCT_DIM * (0 + OCT_DIM * 0)] = 0; + //voxel_data[1 + OCT_DIM * (1 + OCT_DIM * 0)] = 0; + //voxel_data[1 + OCT_DIM * (0 + OCT_DIM * 1)] = 0; + //voxel_data[1 + OCT_DIM * (1 + OCT_DIM * 1)] = 0; + + //voxel_data[0 + OCT_DIM * (0 + OCT_DIM * 0)] = 0; + //voxel_data[0 + OCT_DIM * (1 + OCT_DIM * 0)] = 0; + //voxel_data[0 + OCT_DIM * (0 + OCT_DIM * 1)] = 0; + //voxel_data[0 + OCT_DIM * (1 + OCT_DIM * 1)] = 0; } @@ -136,10 +145,12 @@ uint64_t Map::generate_children(sf::Vector3i pos, int voxel_scale) { output_stream << " " << voxel_scale << " " << counter++ << std::endl; if (IsLeaf(child)) { - if (CheckLeafSign(child)) + if (CheckLeafSign(child)) { SetBit(i + 16, &tmp); - - SetBit(i + 16 + 8, &tmp); + children.push_back(child); + } else { + SetBit(i + 16 + 8, &tmp); + } } else { diff --git a/src/main.cpp b/src/main.cpp index fc451fb..58e77e7 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -95,8 +95,8 @@ int main() { // ============================= Map _map(sf::Vector3i(0, 0, 0)); _map.generate_octree(); - std::cout << _map.a.get_voxel(sf::Vector3i(5, 5, 0)); - std::cout << _map.getVoxel(sf::Vector3i(5, 5, 0)); + std::cout << _map.a.get_voxel(sf::Vector3i(1, 1, 0)); + std::cout << _map.getVoxel(sf::Vector3i(1, 1, 0)); _map.test_map(); std::cin.get(); return 0;