Starting to add the Items on the Octree

This commit is contained in:
samcake 2016-01-28 17:57:21 -08:00
parent 9c5670fc0f
commit 71e6c6e443
3 changed files with 67 additions and 23 deletions

View file

@ -190,11 +190,12 @@ inline QDebug operator<<(QDebug debug, const ItemFilter& me) {
return debug;
}
using ItemID = uint32_t;
class Item {
public:
typedef std::vector<Item> Vector;
typedef unsigned int ID;
typedef ItemID ID;
static const ID INVALID_ITEM_ID = 0;
@ -420,7 +421,6 @@ typedef Item::PayloadPointer PayloadPointer;
typedef std::vector< PayloadPointer > Payloads;
// A few typedefs for standard containers of ItemIDs
using ItemID = Item::ID;
using ItemIDs = std::vector<ItemID>;
using ItemIDSet = std::set<ItemID>;

View file

@ -125,11 +125,36 @@ Octree::Indices Octree::indexCellPath(const Locations& path) {
}
Octree::Index Octree::allocateBrick() {
Index brickIdx = _bricks.size();
_bricks.push_back(Brick());
return brickIdx;
}
Octree::Index Octree::accessCellBrick(const Location& loc, const CellBrickAccessor& accessor) {
auto cellId = indexCell(loc);
auto cell = editCell(cellId);
if (!cell.asBrick()) {
cell.setBrick(allocateBrick());
}
// access the brick
auto& brick = _bricks[cell.brick()];
// execute the accessor
accessor(brick, cell.brick());
return cell.brick();
}
void ItemSpatialTree::insert(const ItemBounds& items) {
for (auto& item : items) {
if (!item.bound.isNull()) {
auto cellIdx = indexCell(evalLocation(item.bound));
accessCellBrick(evalLocation(item.bound), [&] (Brick& brick, Octree::Index cellID) {
brick.items.push_back(item.id);
});
}
}
}

View file

@ -16,12 +16,21 @@
#include <memory>
#include <cassert>
#include <array>
#include <functional>
#include <glm/glm.hpp>
#include <glm/gtx/bit.hpp>
#include <AABox.h>
// maybe we could avoid the Item inclusion here for the OCtree class?
#include "Item.h"
namespace render {
class Brick {
public:
std::vector<ItemID> items;
};
class Octree {
public:
@ -40,8 +49,10 @@ namespace render {
NUM_OCTANTS,
Parent = NUM_OCTANTS,
BrickLink = Parent + 1,
NUM_LINKS = BrickLink + 1,
NUM_LINKS = NUM_OCTANTS + 1,
XAxis = 0x01,
YAxis = 0x02,
@ -150,27 +161,25 @@ namespace render {
bool asChild(Link octant) const { return child(octant) != INVALID; }
void setChild(Link octant, Index child) { _links[octant] = child; }
Index brick() const { return _links[BrickLink]; }
bool asBrick() const { return _links[BrickLink] != INVALID; }
void setBrick(Index brick) { _links[BrickLink] = brick; }
Cell() :
_links({ { INVALID, INVALID, INVALID, INVALID, INVALID, INVALID, INVALID, INVALID, INVALID } })
_links({ { INVALID, INVALID, INVALID, INVALID, INVALID, INVALID, INVALID, INVALID, INVALID, INVALID } })
{}
Cell(Index parent, Location loc) :
_location(loc),
_links({ { INVALID, INVALID, INVALID, INVALID, INVALID, INVALID, INVALID, INVALID, parent } })
_links({ { INVALID, INVALID, INVALID, INVALID, INVALID, INVALID, INVALID, INVALID, parent, INVALID } })
{}
Index brick{ INVALID };
private:
Location _location;
std::array<Index, NUM_LINKS> _links;
Location _location;
};
using Cells = std::vector< Cell >;
class Brick {
public:
};
using Bricks = std::vector< Brick >;
@ -194,15 +203,32 @@ namespace render {
assert(index < _cells.size());
return _cells[index];
}
// Let s talk about the Cell Bricks now
using CellBrickAccessor = std::function<void(Brick& brick, Index brickIdx)>;
Index accessCellBrick(const Location& loc, const CellBrickAccessor& accessor);
const Brick& getBrick(Index index) const {
assert(index < _bricks.size());
return _bricks[index];
}
protected:
Index allocateCell(Index parent, const Location& location);
Index allocateBrick();
Cell& editCell(Index index) {
assert(index < _cells.size());
return _cells[index];
}
};
}
// CLose the namespace here before including the Item in the picture, maybe Octre could stay pure of it
#include "Item.h"
// CLose the namespace here before including the Item in the picture, maybe Octre could stay pure of it
namespace render {
// An octree of Items organizing them efficiently for culling
@ -237,15 +263,8 @@ namespace render {
return AABox(evalPos(loc.pos, cellWidth), cellWidth);
}
Location evalLocation(const AABox& bound) const {
auto minVec = bound.getMinimumPoint();
auto maxVec = bound.getMaximumPoint();
auto minPos = evalCoord(minVec);
auto maxPos = evalCoord(maxVec);
return Location::evalFromRange(minPos, maxPos);
return Location::evalFromRange(evalCoord(bound.getMinimumPoint()), evalCoord(bound.getMaximumPoint()));
}
ItemSpatialTree() {}