Cut down a few of the compiler warnings, refactored the octree into its own file. Refactored all map items into their own subfolder

This commit is contained in:
MitchellHansen
2017-03-22 23:51:46 -07:00
parent 2ad7383406
commit 7c534500f6
15 changed files with 537 additions and 542 deletions

197
src/map/Map.cpp Normal file
View File

@@ -0,0 +1,197 @@
#include "map/Map.h"
Map::Map(uint32_t dimensions) {
srand(time(nullptr));
voxel_data = new char[dimensions * dimensions * dimensions];
for (uint64_t i = 0; i < dimensions * dimensions * dimensions; i++) {
if (rand() % 25 < 2)
voxel_data[i] = 1;
else
voxel_data[i] = 1;
}
}
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<sf::Vector3i> 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;
// This is where contours
// The CP will be left blank, contours will be added maybe
return child_descriptor;
}
// Init a blank child descriptor for this node
uint64_t child_descriptor = 0;
std::vector<uint64_t> descriptor_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 |= a.copy_to_stack(descriptor_array);
// Free space may also be allocated here as well
// Return the node up the stack
return child_descriptor;
}
void Map::generate_octree() {
// 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);
uint64_t tmp = 0;
// ========= DEBUG ==============
PrettyPrintUINT64(root_node, &output_stream);
output_stream << " " << OCT_DIM << " " << counter++ << std::endl;
// ==============================
a.root_index = a.copy_to_stack(std::vector<uint64_t>{root_node});
// Dump the debug log
DumpLog(&output_stream, "raw_output.txt");
}
void Map::setVoxel(sf::Vector3i world_position, int val) {
}
bool Map::getVoxelFromOctree(sf::Vector3i position)
{
return a.get_voxel(position);
}
bool Map::getVoxel(sf::Vector3i pos){
if (voxel_data[pos.x + OCT_DIM * (pos.y + OCT_DIM * pos.z)]) {
return true;
} else {
return false;
}
}
void Map::test_map() {
std::cout << "Validating map..." << std::endl;
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 arr1 = getVoxel(pos);
bool arr2 = getVoxelFromOctree(pos);
if (arr1 != arr2) {
std::cout << "X: " << pos.x << "Y: " << pos.y << "Z: " << pos.z << std::endl;
}
}
}
}
std::cout << "Done" << std::endl;
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 arr1 = getVoxel(pos);
}
}
}
std::cout << "Array linear xyz access : ";
std::cout << timer.restart().asMicroseconds() << " microseconds" << std::endl;
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);
}
}
}
std::cout << "Octree linear xyz access : ";
std::cout << timer.restart().asMicroseconds() << " microseconds" << std::endl;
}

151
src/map/Octree.cpp Normal file
View File

@@ -0,0 +1,151 @@
#include "map/Octree.h"
Octree::Octree() {
// initialize the first stack block
for (int i = 0; i < 0x8000; i++) {
blob[i] = 0;
}
}
uint64_t Octree::copy_to_stack(std::vector<uint64_t> children) {
// 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();
}
// Check for the far bit
memcpy(&blob[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) {
// Struct that holds the state necessary to continue the traversal from the found voxel
oct_state state;
// 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;
sf::Vector3i quad_position(0, 0, 0);
// While we are not at the required resolution
// Traverse down by setting the valid/leaf mask to the subvoxel
// Check to see if it is valid
// Yes?
// Check to see if it is a leaf
// No? Break
// Yes? Scale down to the next hierarchy, push the parent to the stack
//
// No?
// Break
while (dimension > 1) {
// So we can be a little bit tricky here and increment our
// array index that holds our masks as we build the idx.
// Adding 1 for X, 2 for Y, and 4 for Z
int mask_index = 0;
// Do the logic steps to find which sub oct we step down into
if (position.x >= (dimension / 2) + quad_position.x) {
// Set our voxel position to the (0,0) of the correct oct
quad_position.x += (dimension / 2);
// increment the mask index and mentioned above
mask_index += 1;
// Set the idx to represent the move
state.idx_stack[state.scale] |= idx_set_x_mask;
}
if (position.y >= (dimension / 2) + quad_position.y) {
quad_position.y |= (dimension / 2);
mask_index += 2;
state.idx_stack[state.scale] ^= idx_set_y_mask;
}
if (position.z >= (dimension / 2) + quad_position.z) {
quad_position.z += (dimension / 2);
mask_index += 4;
state.idx_stack[state.scale] |= idx_set_z_mask;
}
// Check to see if we are on a valid oct
if ((head >> 16) & mask_8[mask_index]) {
// Check to see if it is a leaf
if ((head >> 24) & mask_8[mask_index]) {
// If it is, then we cannot traverse further as CP's won't have been generated
return true;
}
// If all went well and we found a valid non-leaf oct then we will traverse further down the hierarchy
state.scale++;
dimension /= 2;
// Count the number of valid octs that come before and add it to the index to get the position
// Negate it by one as it counts itself
int count = count_bits((uint8_t)(head >> 16) & count_mask_8[mask_index]) - 1;
// 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
state.parent_stack_position++;
state.parent_stack[state.parent_stack_position] = head;
}
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.
// Currently it adds the last parent on the second to lowest
// oct CP. Not sure if thats correct
return false;
}
}
return true;
}
void Octree::print_block(int block_pos) {
std::stringstream sss;
for (int i = block_pos; i < (int)pow(2, 15); i++) {
PrettyPrintUINT64(blob[i], &sss);
sss << "\n";
}
DumpLog(&sss, "raw_data.txt");
}

382
src/map/Old_Map.cpp Normal file
View File

@@ -0,0 +1,382 @@
#include <iostream>
#include <SFML/System/Vector3.hpp>
#include <SFML/System/Vector2.hpp>
#include "util.hpp"
#include <map/Old_Map.h>
#include <algorithm>
Old_Map::Old_Map(sf::Vector3i dim) {
dimensions = dim;
}
Old_Map::~Old_Map() {
}
void generate_at(int x, int y, std::vector<std::vector<int>> *grid) {
size_t x_bound = grid->size();
size_t y_bound = grid->at(0).size();
// N S E W
std::vector<int> t = { 1, 2, 3, 4 };
std::random_shuffle(t.begin(), t.end());
while (t.size() > 0) {
switch (t.back()) {
// 20 lines to hard code, a headache to do it cleverly
case 1: {
if (y + 1 < y_bound && grid->at(x).at(y + 1) == 0) {
grid->at(x).at(y) = 1;
grid->at(x).at(y + 1) = 2;
generate_at(x, y + 1, grid);
}
break;
}
case 2: {
if (y - 1 >= 0 && grid->at(x).at(y - 1) == 0) {
grid->at(x).at(y) = 2;
grid->at(x).at(y - 1) = 1;
generate_at(x, y - 1, grid);
}
break;
}
case 3: {
if (x + 1 < x_bound && grid->at(x+1).at(y) == 0) {
grid->at(x).at(y) = 3;
grid->at(x + 1).at(y) = 4;
generate_at(x + 1, y, grid);
}
break;
}
case 4: {
if (x - 1 >= 0 && grid->at(x-1).at(y) == 0) {
grid->at(x).at(y) = 4;
grid->at(x - 1).at(y) = 3;
generate_at(x - 1, y, grid);
}
break;
}
}
t.pop_back();
}
}
std::vector<std::vector<int>> generate_maze(sf::Vector2i dimensions, sf::Vector2i start_point) {
std::vector<std::vector<int>> grid(dimensions.x, std::vector<int>(dimensions.y, 0));
generate_at(start_point.x, start_point.y, &grid);
return grid;
}
void Old_Map::generate_terrain() {
std::mt19937 gen;
std::uniform_real_distribution<double> dis(-1.0, 1.0);
auto f_rand = std::bind(dis, std::ref(gen));
voxel_data = new char[dimensions.x * dimensions.y * dimensions.z];
height_map = new double[dimensions.x * dimensions.y];
for (int i = 0; i < dimensions.x * dimensions.y * dimensions.z; i++) {
voxel_data[i] = 0;
}
//set_voxel(sf::Vector3i(63, 63, 63), 1);
for (int i = 0; i < dimensions.x * dimensions.y; i++) {
height_map[i] = 0;
}
//size of grid to generate, note this must be a
//value 2^n+1
int DATA_SIZE = dimensions.x + 1;
//an initial seed value for the corners of the data
//srand(f_rand());
double SEED = rand() % 40 + 40;
//seed the data
set_sample(0, 0, SEED);
set_sample(0, dimensions.y, SEED);
set_sample(dimensions.x, 0, SEED);
set_sample(dimensions.x, dimensions.y, SEED);
double h = 40.0;//the range (-h -> +h) for the average offset
//for the new value in range of h
//side length is distance of a single square side
//or distance of diagonal in diamond
for (int sideLength = DATA_SIZE - 1;
//side length must be >= 2 so we always have
//a new value (if its 1 we overwrite existing values
//on the last iteration)
sideLength >= 2;
//each iteration we are looking at smaller squares
//diamonds, and we decrease the variation of the offset
sideLength /= 2, h /= 2.0) {
//half the length of the side of a square
//or distance from diamond center to one corner
//(just to make calcs below a little clearer)
int halfSide = sideLength / 2;
//generate the new square values
for (int x = 0; x < DATA_SIZE - 1; x += sideLength) {
for (int y = 0; y < DATA_SIZE - 1; y += sideLength) {
//x, y is upper left corner of square
//calculate average of existing corners
double avg = sample(x, y) + //top left
sample(x + sideLength, y) +//top right
sample(x, y + sideLength) + //lower left
sample(x + sideLength, y + sideLength);//lower right
avg /= 4.0;
//center is average plus random offset
set_sample(x + halfSide, y + halfSide,
//We calculate random value in range of 2h
//and then subtract h so the end value is
//in the range (-h, +h)
avg + (f_rand() * 2 * h) - h);
}
}
//generate the diamond values
//since the diamonds are staggered we only move x
//by half side
//NOTE: if the data shouldn't wrap then x < DATA_SIZE
//to generate the far edge values
for (int x = 0; x < DATA_SIZE - 1; x += halfSide) {
//and y is x offset by half a side, but moved by
//the full side length
//NOTE: if the data shouldn't wrap then y < DATA_SIZE
//to generate the far edge values
for (int y = (x + halfSide) % sideLength; y < DATA_SIZE - 1; y += sideLength) {
//x, y is center of diamond
//note we must use mod and add DATA_SIZE for subtraction
//so that we can wrap around the array to find the corners
double avg =
sample((x - halfSide + DATA_SIZE) % DATA_SIZE, y) + //left of center
sample((x + halfSide) % DATA_SIZE, y) + //right of center
sample(x, (y + halfSide) % DATA_SIZE) + //below center
sample(x, (y - halfSide + DATA_SIZE) % DATA_SIZE); //above center
avg /= 4.0;
//new value = average plus random offset
//We calculate random value in range of 2h
//and then subtract h so the end value is
//in the range (-h, +h)
avg = avg + (f_rand() * 2 * h) - h;
//update value for center of diamond
set_sample(x, y, avg);
//wrap values on the edges, remove
//this and adjust loop condition above
//for non-wrapping values.
if (x == 0) set_sample(DATA_SIZE - 1, y, avg);
if (y == 0) set_sample(x, DATA_SIZE - 1, avg);
}
}
}
for (int x = 100; x < 150; x += 10) {
for (int y = 100; y < 150; y += 10) {
for (int z = 0; z < 10; z += 1) {
voxel_data[x + dimensions.x * (y + dimensions.z * z)] = 5;
}
}
}
for (int x = 0; x < dimensions.x; x++) {
for (int y = 0; y < dimensions.y; y++) {
if (height_map[x + y * dimensions.x] > 0) {
int z = static_cast<int>(height_map[x + y * dimensions.x]);
while (z > 0 && z < dimensions.z) {
voxel_data[x + dimensions.x * (y + dimensions.z * z)] = 5;
z--;
}
}
}
}
for (int x = dimensions.x / 2; x < dimensions.x / 2 + dimensions.x / 64; x++) {
for (int y = dimensions.x / 2; y < dimensions.y / 2 + dimensions.x / 64; y++) {
for (int z = 0; z < 5; z++) {
voxel_data[x + dimensions.x * (y + dimensions.z * z)] = 6;
}
}
}
for (int x = 0; x < dimensions.x; x++) {
for (int y = 0; y < dimensions.y; y++) {
// for (int z = 0; z < dimensions.z; z++) {
//if (rand() % 1000 < 1)
voxel_data[x + dimensions.x * (y + dimensions.z * 1)] = 6;
// }
}
}
for (int x = 30; x < 60; x++) {
//for (int y = 0; y < dimensions.y; y++) {
for (int z = 0; z < 25; z++) {
voxel_data[x + dimensions.x * (50 + dimensions.z * z)] = 6;
}
//}
}
// Hand code in some constructions
std::vector<std::vector<int>> maze =
generate_maze(sf::Vector2i(8, 8), sf::Vector2i(0, 0));
for (int x = 0; x < maze.size(); x++) {
for (int y = 0; y < maze.at(0).size(); y++) {
switch(maze.at(x).at(y)) {
case 1: { // North
voxel_data[x * 3 + 1 + dimensions.x * (y * 3 + dimensions.z * 1)] = 6;
voxel_data[x * 3 + 1 + dimensions.x * (y * 3 + 1 + dimensions.z * 1)] = 6;
voxel_data[x * 3 + 1 + dimensions.x * (y * 3 + 2 + dimensions.z * 1)] = 5;
//voxel_data[x * 3 + dimensions.x * (y * 3 + 2 + dimensions.z * 1)] = 6;
//voxel_data[x * 3 + 2 + dimensions.x * (y * 3 + 2 + dimensions.z * 1)] = 6;
break;
}
case 2: { // South
voxel_data[x * 3 + 1 + dimensions.x * (y * 3 + dimensions.z * 1)] = 5;
voxel_data[x * 3 + 1 + dimensions.x * (y * 3 + 1 + dimensions.z * 1)] = 6;
voxel_data[x * 3 + 1 + dimensions.x * (y * 3 + 2 + dimensions.z * 1)] = 6;
//voxel_data[x * 3 + dimensions.x * (y * 3 + dimensions.z * 1)] = 6;
//voxel_data[x * 3 + 2 + dimensions.x * (y * 3 + dimensions.z * 1)] = 6;
break;
}
case 3: { // East
voxel_data[x * 3 + dimensions.x * (y * 3 + 1 + dimensions.z * 1)] = 6;
voxel_data[x * 3 + 1 + dimensions.x * (y * 3 + 1 + dimensions.z * 1)] = 6;
voxel_data[x * 3 + 2 + dimensions.x * (y * 3 + 1 + dimensions.z * 1)] = 5;
//voxel_data[x * 3 + 2 + dimensions.x * (y * 3 + dimensions.z * 1)] = 6;
//voxel_data[x * 3 + 2 + dimensions.x * (y * 3 + 2 + dimensions.z * 1)] = 6;
break;
}
case 4: { // West
voxel_data[x * 3 + dimensions.x * (y * 3 + 1 + dimensions.z * 1)] = 5;
voxel_data[x * 3 + 1 + dimensions.x * (y * 3 + 1 + dimensions.z * 1)] = 6;
voxel_data[x * 3 + 2 + dimensions.x * (y * 3 + 1 + dimensions.z * 1)] = 6;
//voxel_data[x * 3 + dimensions.x * (y * 3 + dimensions.z * 1)] = 6;
//voxel_data[x * 3 + dimensions.x * (y * 3 + 2 + dimensions.z * 1)] = 6;
break;
}
}
}
}
//for (int x = 0; x < dimensions.x; x++) {
// for (int y = 0; y < dimensions.y; y++) {
// voxel_data[x + dimensions.x * (y + dimensions.z * 1)] = 6;
// }
//}
set_voxel(sf::Vector3i(45, 70, 5), 1);
set_voxel(sf::Vector3i(47, 70, 5), 1);
set_voxel(sf::Vector3i(100, 100, 50), 1);
}
void Old_Map::set_voxel(sf::Vector3i position, int val) {
voxel_data[position.x + dimensions.x * (position.y + dimensions.z * position.z)] = val;
}
sf::Vector3i Old_Map::getDimensions() {
return dimensions;
}
char* Old_Map::get_voxel_data() {
return voxel_data;
}
double Old_Map::sample(int x, int y) {
return height_map[(x & (dimensions.x - 1)) + (y & (dimensions.y - 1)) * dimensions.x];
}
void Old_Map::set_sample(int x, int y, double value) {
height_map[(x & (dimensions.x - 1)) + (y & (dimensions.y - 1)) * dimensions.x] = value;
}
void Old_Map::sample_square(int x, int y, int size, double value) {
int hs = size / 2;
// a b
//
// x
//
// c d
double a = sample(x - hs, y - hs);
double b = sample(x + hs, y - hs);
double c = sample(x - hs, y + hs);
double d = sample(x + hs, y + hs);
set_sample(x, y, ((a + b + c + d) / 4.0) + value);
}
void Old_Map::sample_diamond(int x, int y, int size, double value) {
int hs = size / 2;
// c
//
//a x b
//
// d
double a = sample(x - hs, y);
double b = sample(x + hs, y);
double c = sample(x, y - hs);
double d = sample(x, y + hs);
set_sample(x, y, ((a + b + c + d) / 4.0) + value);
}
void Old_Map::diamond_square(int stepsize, double scale) {
std::mt19937 generator;
std::uniform_real_distribution<double> uniform_distribution(-1.0, 1.0);
auto f_rand = std::bind(uniform_distribution, std::ref(generator));
int halfstep = stepsize / 2;
for (int y = halfstep; y < dimensions.y + halfstep; y += stepsize) {
for (int x = halfstep; x < dimensions.x + halfstep; x += stepsize) {
sample_square(x, y, stepsize, f_rand() * scale);
}
}
for (int y = 0; y < dimensions.y; y += stepsize) {
for (int x = 0; x < dimensions.x; x += stepsize) {
sample_diamond(x + halfstep, y, stepsize, f_rand() * scale);
sample_diamond(x, y + halfstep, stepsize, f_rand() * scale);
}
}
}