Fixed sample and added visit.

This commit is contained in:
Robin Engman 2013-11-28 14:18:44 +01:00
parent 6d12045df9
commit 632d412f9c
3 changed files with 60 additions and 9 deletions

Binary file not shown.

View File

@ -16,6 +16,16 @@ Octree::~Octree()
} }
Octree& Octree::operator=(const Octree& orig)
{
this->leafData = orig.leafData;
this->updateQueue = orig.updateQueue;
this->worldNode = orig.worldNode;
this->mapReferences = orig.mapReferences;
return *this;
}
void Octree::AddObject(Utility::DynamicMemory::UniquePointer< ICustomBody > customBodyRef) void Octree::AddObject(Utility::DynamicMemory::UniquePointer< ICustomBody > customBodyRef)
{ {
Data data; Data data;
@ -50,7 +60,6 @@ void Octree::MoveToUpdateQueue(Utility::DynamicMemory::UniquePointer< ICustomBod
this->updateQueue.push_back(&this->leafData[this->mapReferences[customBodyRef]]);*/ this->updateQueue.push_back(&this->leafData[this->mapReferences[customBodyRef]]);*/
} }
void Octree::DestroyObject(Utility::DynamicMemory::UniquePointer< ICustomBody > customBodyRef) void Octree::DestroyObject(Utility::DynamicMemory::UniquePointer< ICustomBody > customBodyRef)
{ {
std::map<ICustomBody*, unsigned int>::iterator it = this->mapReferences.find(customBodyRef); std::map<ICustomBody*, unsigned int>::iterator it = this->mapReferences.find(customBodyRef);
@ -60,13 +69,22 @@ void Octree::DestroyObject(Utility::DynamicMemory::UniquePointer< ICustomBody >
this->leafData.erase(this->leafData.begin() + this->leafData[this->mapReferences[customBodyRef]].queueRef); this->leafData.erase(this->leafData.begin() + this->leafData[this->mapReferences[customBodyRef]].queueRef);
} }
std::vector<ICustomBody*> Octree::Sample(Utility::DynamicMemory::UniquePointer< ICustomBody > customBodyRef) std::vector<ICustomBody*> Octree::Sample(ICustomBody* customBodyRef)
{ {
std::vector<ICustomBody*> list; std::vector<ICustomBody*> list;
auto object = this->mapReferences.find(customBodyRef);
if(object == this->mapReferences.end())
{
return list;
}
unsigned int tempRef = object->second;
for(unsigned int i = 0; i<this->leafData.size(); i++) for(unsigned int i = 0; i<this->leafData.size(); i++)
{ {
if(this->leafData[i].customBodyRef != customBodyRef) if(this->leafData[i].container.Intersects(customBodyRef->GetBoundingSphere())) if(tempRef != i) if(this->leafData[tempRef].container.Intersects(this->leafData[i].container))
{ {
list.push_back(this->leafData[i].customBodyRef); list.push_back(this->leafData[i].customBodyRef);
} }
@ -74,3 +92,28 @@ std::vector<ICustomBody*> Octree::Sample(Utility::DynamicMemory::UniquePointer<
return list; return list;
} }
void Octree::Visit(ICustomBody* customBodyRef, VistorAction hitAction )
{
auto object = this->mapReferences.find(customBodyRef);
if(object == this->mapReferences.end())
{
return;
}
unsigned int tempRef = object->second;
for(unsigned int i = 0; i<this->leafData.size(); i++)
{
if(tempRef != i) if(this->leafData[tempRef].container.Intersects(this->leafData[i].container))
{
hitAction(*this, tempRef, i);
}
}
}
ICustomBody* Octree::GetCustomBody(const unsigned int tempRef)
{
return this->leafData[tempRef].customBodyRef;
}

View File

@ -15,6 +15,8 @@ namespace Oyster
class Octree class Octree
{ {
public: public:
typedef void(*VistorAction)(Octree&, unsigned int, unsigned int);
struct Data struct Data
{ {
Data* prev; Data* prev;
@ -22,7 +24,7 @@ namespace Oyster
Collision3D::Sphere container; Collision3D::Sphere container;
Utility::DynamicMemory::UniquePointer< ICustomBody > customBodyRef; ::Utility::DynamicMemory::UniquePointer< ICustomBody > customBodyRef;
unsigned int queueRef; unsigned int queueRef;
}; };
@ -34,16 +36,22 @@ namespace Oyster
Collision3D::BoxAxisAligned container; Collision3D::BoxAxisAligned container;
}; };
Octree(unsigned int bufferSize, unsigned char numLayers, Math::Float3 worldSize); Octree(unsigned int bufferSize = 0, unsigned char numLayers = 0, Math::Float3 worldSize = Math::Float3::null);
virtual ~Octree(); virtual ~Octree();
void AddObject(Utility::DynamicMemory::UniquePointer< ICustomBody > customBodyRef); Octree& operator=(const Octree& orig);
void MoveToUpdateQueue(Utility::DynamicMemory::UniquePointer< ICustomBody > customBodyRef); void AddObject(::Utility::DynamicMemory::UniquePointer< ICustomBody > customBodyRef);
void DestroyObject(Utility::DynamicMemory::UniquePointer< ICustomBody > customBodyRef); void MoveToUpdateQueue(::Utility::DynamicMemory::UniquePointer< ICustomBody > customBodyRef);
void DestroyObject(::Utility::DynamicMemory::UniquePointer< ICustomBody > customBodyRef);
std::vector<ICustomBody*> Sample(ICustomBody* customBodyRef);
void Visit(ICustomBody* customBodyRef, VistorAction hitAction );
ICustomBody* GetCustomBody(const unsigned int tempRef);
std::vector<ICustomBody*> Sample(Utility::DynamicMemory::UniquePointer< ICustomBody > customBodyRef);
private: private:
std::vector < Data > leafData; std::vector < Data > leafData;
std::vector < Data* > updateQueue; std::vector < Data* > updateQueue;