From 71e6c6e443aea5d85ce825f0ad88e4d1ed3a4d8c Mon Sep 17 00:00:00 2001 From: samcake Date: Thu, 28 Jan 2016 17:57:21 -0800 Subject: [PATCH] Starting to add the Items on the Octree --- libraries/render/src/render/Item.h | 4 +- libraries/render/src/render/Octree.cpp | 25 +++++++++++ libraries/render/src/render/Octree.h | 61 +++++++++++++++++--------- 3 files changed, 67 insertions(+), 23 deletions(-) diff --git a/libraries/render/src/render/Item.h b/libraries/render/src/render/Item.h index 568b052623..46793d1ec3 100644 --- a/libraries/render/src/render/Item.h +++ b/libraries/render/src/render/Item.h @@ -190,11 +190,12 @@ inline QDebug operator<<(QDebug debug, const ItemFilter& me) { return debug; } +using ItemID = uint32_t; class Item { public: typedef std::vector 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; using ItemIDSet = std::set; diff --git a/libraries/render/src/render/Octree.cpp b/libraries/render/src/render/Octree.cpp index 9aa4541d65..a162ee3a3f 100644 --- a/libraries/render/src/render/Octree.cpp +++ b/libraries/render/src/render/Octree.cpp @@ -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); + }); } } } diff --git a/libraries/render/src/render/Octree.h b/libraries/render/src/render/Octree.h index 6362f66e2d..7ee4910358 100644 --- a/libraries/render/src/render/Octree.h +++ b/libraries/render/src/render/Octree.h @@ -16,12 +16,21 @@ #include #include #include +#include #include #include #include +// maybe we could avoid the Item inclusion here for the OCtree class? +#include "Item.h" + namespace render { + class Brick { + public: + std::vector 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 _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; + + 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() {}