Added a struct to pass away from get_voxel

master
MitchellHansen 8 years ago
parent e45df185f7
commit 2ad7383406

@ -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
);
}
};

@ -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<float> 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<char>(event.text.unicode);
}
else if (event.type == sf::Event::KeyPressed) {
if (event.key.code == sf::Keyboard::Return) {
break;
}
}
}
}
std::istream_iterator<std::string> begin(ss);
std::istream_iterator<std::string> end;
std::vector<std::string> vstrings(begin, end);
std::vector<float> ret;
for (auto i: vstrings) {
ret.push_back(std::stof(i));
}
return ret;
}
#ifdef _MSC_VER
# include <intrin.h>
# define __builtin_popcount _mm_popcnt_u32

@ -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<uint64_t>{root_node});
a.root_index = a.copy_to_stack(std::vector<uint64_t>{root_node});
// Dump the debug log
// DumpLog(&output_stream, "raw_output.txt");
DumpLog(&output_stream, "raw_output.txt");
}
@ -234,12 +234,12 @@ void Map::test_map() {
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<uint64_t> 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;
// Init the idx stack
uint8_t scale = 0;
uint8_t idx_stack[32] = { 0 };
// Struct that holds the state necessary to continue the traversal from the found voxel
oct_state state;
// Init the idx stack (DEBUG)
//std::vector<std::bitset<3>> scale_stack(static_cast<uint64_t>(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<uint64_t>(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<uint64_t>(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<uint64_t>(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 {

Loading…
Cancel
Save