From 6b16da11c377951434d7014f9802e38d6999396d Mon Sep 17 00:00:00 2001 From: Soren Date: Sat, 25 Apr 2015 23:19:28 -0600 Subject: [PATCH 1/5] Initial Version --- common/util/Compass.cpp | 119 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 119 insertions(+) create mode 100644 common/util/Compass.cpp diff --git a/common/util/Compass.cpp b/common/util/Compass.cpp new file mode 100644 index 0000000..3a4701a --- /dev/null +++ b/common/util/Compass.cpp @@ -0,0 +1,119 @@ +#include "Compass.h" +#include "tbb/tbb.h" +#include "tbb/parallel_for.h" +#include "tbb/blocked_range.h" +#include "tbb/concurrent_queue.h" +#include "../world.h" + +#include +#include +#include + +Compass::Compass(World input, glm::vec3 pos) +{ + world = input; + currentPos = pos; +}; + +struct CheckType +{ + //const Block* input; + std::vector > blocks; + glm::vec3 position; + BlockType type; + bool* output; + void operator()( const tbb::blocked_range& range ) const + { + for (int i=range.begin(); i!=range.end(); ++i) + { + if (blocks[i]->position == position) + { + if (blocks[i]->type == type) + output[i] = true; + else + output[i] = false; + } + } + } +}; + + +int Compass::find(BlockType type) +{ + auto blocks = world.blocks; + auto worldSize = blocks.size(); + auto found = false; + auto distance = 1; + bool boolArray[worldSize]; + + while (!found) + { + auto q = getQueue(currentPos, distance); + auto qSize = q.unsafe_size(); + glm::vec3 e; + + while (q.try_pop(e)) + { + CheckType check; + check.blocks = blocks; + check.position = e; + check.type = type; + check.output = boolArray; + + tbb::parallel_for( tbb::blocked_range( 1, worldSize), check); + + for (unsigned int i=0; i< worldSize; ++i) + if (boolArray[i] == true) + return distance; + } + + if (worldSize == qSize) + return -1; + distance++; + } + + return -1; +}; + +struct populatej { + tbb::concurrent_queue* queue; + glm::vec3* start; + int distance; + int i; + void operator()( tbb::blocked_range& range ) const { + for( int j=range.begin(); j!=range.end(); ++j ){ + int k=i>0?distance-j:distance+j; + queue->push(glm::vec3(start->x+i,start->y+j,start->z+k)); + if(k!=0)queue->push(glm::vec3(start->x+i,start->y+j,start->z-k)); + } + } +}; + +struct populateQueue { + tbb::concurrent_queue* queue; + glm::vec3* start; + int distance; + void operator()( tbb::blocked_range& range ) const { + for( int i=range.begin(); i!=range.end(); ++i ){ + populatej jloop; + int jdistance=i>0?distance-i:distance+i; + jloop.distance=jdistance; + jloop.queue=queue; + jloop.start=start; + jloop.i=i; + tbb::parallel_for( tbb::blocked_range(-jdistance,jdistance),jloop); + } + } +}; + + +tbb::concurrent_queue Compass::getQueue( glm::vec3& start,int dis ) { + tbb::concurrent_queue queue; + populateQueue pq; + pq.queue=&queue; + pq.distance=dis; + pq.start=&start; + tbb::parallel_for( tbb::blocked_range( -dis, +dis ), pq ); + return queue; +} + From fa1620f604651386714636ab0101f26c9cf7daa2 Mon Sep 17 00:00:00 2001 From: Soren Date: Sat, 25 Apr 2015 23:19:38 -0600 Subject: [PATCH 2/5] Initial Version --- common/util/Compass.h | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 common/util/Compass.h diff --git a/common/util/Compass.h b/common/util/Compass.h new file mode 100644 index 0000000..e13d8c0 --- /dev/null +++ b/common/util/Compass.h @@ -0,0 +1,24 @@ +#ifndef COMPASS_H +#define COMPASS_H +#include "tbb/parallel_for.h" +#include "tbb/blocked_range.h" +#include "tbb/concurrent_queue.h" +#include "../world.h" +#include "vector3.cpp" + + +class Compass +{ + public: + glm::vec3 currentPos; + int n; + World world; + + Compass(World, glm::vec3); + tbb::concurrent_queue getQueue( glm::vec3&,int); + int find(BlockType); + + + +}; +#endif From e54e8537ff411177255a67d0946346f6c03ff68f Mon Sep 17 00:00:00 2001 From: Soren Date: Sat, 25 Apr 2015 23:20:40 -0600 Subject: [PATCH 3/5] Added Compass.cpp to common_srcs --- CMakeLists.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/CMakeLists.txt b/CMakeLists.txt index 987abb0..60c27f4 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -53,6 +53,7 @@ set(common_srcs common/interfaces/IVertexContainer.cpp common/util/vector3.cpp + common/util/Compass.cpp common/voxel/block.cpp common/voxel/chunk.cpp From 704011216c3a728cbdd4984158affd3995d7151a Mon Sep 17 00:00:00 2001 From: Soren Date: Thu, 30 Apr 2015 10:04:30 -0600 Subject: [PATCH 4/5] Changed to more efficient parallel algorithm --- common/util/Compass.cpp | 90 ++++++++++++++++++++++------------------- 1 file changed, 48 insertions(+), 42 deletions(-) diff --git a/common/util/Compass.cpp b/common/util/Compass.cpp index 3a4701a..ca5fd5d 100644 --- a/common/util/Compass.cpp +++ b/common/util/Compass.cpp @@ -4,75 +4,78 @@ #include "tbb/blocked_range.h" #include "tbb/concurrent_queue.h" #include "../world.h" - -#include +#include "chunkmanager.h" #include -#include -Compass::Compass(World input, glm::vec3 pos) +int modx(int s) +{ + return (s>0?s%ChunkManager::BOUNDX :(s+ChunkManager::BOUNDX)%ChunkManager::BOUNDX); +}; + +int mody(int s) +{ + return (s>0?s%ChunkManager::BOUNDY :(s+ChunkManager::BOUNDY)%ChunkManager::BOUNDY); +}; + +int modz(int s) { - world = input; + return s; +}; + +Compass::Compass(World inWorld, ChunkManager inChunk, glm::vec3 pos) +{ + world = inWorld; + chunk = inChunk; currentPos = pos; }; struct CheckType { - //const Block* input; std::vector > blocks; - glm::vec3 position; BlockType type; - bool* output; + bool* found; + ChunkManager* chunk; + glm::vec3* distance; void operator()( const tbb::blocked_range& range ) const { - for (int i=range.begin(); i!=range.end(); ++i) + for (auto i=range.begin(); i!=range.end(); ++i) { - if (blocks[i]->position == position) - { - if (blocks[i]->type == type) - output[i] = true; - else - output[i] = false; + if (type == chunk->get(blocks[i]->position.x, blocks[i]->position.y, blocks[i]->position.z)) + { + (*found) = true; + (*distance) = blocks[i]->position; } } } }; -int Compass::find(BlockType type) +glm::vec3 Compass::find(BlockType type) { auto blocks = world.blocks; auto worldSize = blocks.size(); auto found = false; auto distance = 1; - bool boolArray[worldSize]; + glm::vec3 typeDistance; - while (!found) + while (distance <= static_cast(worldSize)/2) { - auto q = getQueue(currentPos, distance); - auto qSize = q.unsafe_size(); - glm::vec3 e; - - while (q.try_pop(e)) - { - CheckType check; - check.blocks = blocks; - check.position = e; - check.type = type; - check.output = boolArray; + auto q = getQueue(currentPos, distance, worldSize); + CheckType check; + check.chunk =& chunk; + check.blocks = blocks; + check.type = type; + check.found = &found; + check.distance = &typeDistance; - tbb::parallel_for( tbb::blocked_range( 1, worldSize), check); + tbb::parallel_for( tbb::blocked_range(0, q.unsafe_size()), check); + if (found) + return typeDistance; - for (unsigned int i=0; i< worldSize; ++i) - if (boolArray[i] == true) - return distance; - } - - if (worldSize == qSize) - return -1; - distance++; + ++distance; } - - return -1; + + return glm::vec3(0,0,0); }; struct populatej { @@ -80,6 +83,7 @@ struct populatej { glm::vec3* start; int distance; int i; + int worldSize; void operator()( tbb::blocked_range& range ) const { for( int j=range.begin(); j!=range.end(); ++j ){ int k=i>0?distance-j:distance+j; @@ -93,6 +97,7 @@ struct populateQueue { tbb::concurrent_queue* queue; glm::vec3* start; int distance; + int worldSize; void operator()( tbb::blocked_range& range ) const { for( int i=range.begin(); i!=range.end(); ++i ){ populatej jloop; @@ -101,17 +106,18 @@ struct populateQueue { jloop.queue=queue; jloop.start=start; jloop.i=i; + jloop.worldSize=worldSize; tbb::parallel_for( tbb::blocked_range(-jdistance,jdistance),jloop); } } }; - -tbb::concurrent_queue Compass::getQueue( glm::vec3& start,int dis ) { +tbb::concurrent_queue Compass::getQueue( glm::vec3& start,int dis , int worldSize) { tbb::concurrent_queue queue; populateQueue pq; pq.queue=&queue; pq.distance=dis; + pq.worldSize=worldSize; pq.start=&start; tbb::parallel_for( tbb::blocked_range( -dis, +dis ), pq ); return queue; From 931ad5002f55dd2a8660a2dad021e21a5f404e5e Mon Sep 17 00:00:00 2001 From: Soren Date: Thu, 30 Apr 2015 10:04:35 -0600 Subject: [PATCH 5/5] Changed to more efficient parallel algorithm --- common/util/Compass.h | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/common/util/Compass.h b/common/util/Compass.h index e13d8c0..cbe963a 100644 --- a/common/util/Compass.h +++ b/common/util/Compass.h @@ -4,8 +4,7 @@ #include "tbb/blocked_range.h" #include "tbb/concurrent_queue.h" #include "../world.h" -#include "vector3.cpp" - +#include "../voxel/chunkmanager.cpp" class Compass { @@ -13,12 +12,11 @@ class Compass glm::vec3 currentPos; int n; World world; + ChunkManager chunk; - Compass(World, glm::vec3); - tbb::concurrent_queue getQueue( glm::vec3&,int); - int find(BlockType); - - + Compass(World, ChunkManager, glm::vec3); + tbb::concurrent_queue getQueue( glm::vec3&,int, int); + glm::vec3 find(BlockType); }; #endif