10 #include "imstkParallelUtils.h" 13 #include <unordered_set> 16 #pragma warning(disable : 4201) 86 friend class OctreeBasedCD;
87 friend class LooseOctreeTest;
98 m_Center(Vec3d::Zero()),
108 const double halfWidth,
const uint32_t depth);
113 bool isLeaf()
const {
return m_bIsLeaf; }
119 OctreeNode* getChildNode(
const uint32_t childIdx)
const;
137 bounds << m_LowerBound[0], m_UpperBound[0], m_LowerBound[1], m_UpperBound[1], m_LowerBound[2], m_UpperBound[2];
147 bounds << m_LowerExtendedBound[0], m_UpperExtendedBound[0], m_LowerExtendedBound[1], m_UpperExtendedBound[1], m_LowerExtendedBound[2], m_UpperExtendedBound[2];
167 void removeAllDescendants();
172 void removeEmptyDescendants();
193 bool contains(
const std::array<double, 3>& point) {
return contains(point[0], point[1], point[2]); }
194 bool contains(
const double x,
const double y,
const double z)
196 return x >= m_LowerBound[0]
197 && y >= m_LowerBound[1]
198 && z >= m_LowerBound[2]
199 && x <= m_UpperBound[0]
200 && y <= m_UpperBound[1]
201 && z <= m_UpperBound[2];
209 bool contains(
const std::array<double, 3>& lowerCorner,
const std::array<double, 3>& upperCorner)
211 return lowerCorner[0] >= m_LowerBound[0]
212 && lowerCorner[1] >= m_LowerBound[1]
213 && lowerCorner[2] >= m_LowerBound[2]
214 && upperCorner[0] <= m_UpperBound[0]
215 && upperCorner[1] <= m_UpperBound[1]
216 && upperCorner[2] <= m_UpperBound[2];
223 bool looselyContains(
const std::array<double, 3>& point) {
return looselyContains(point[0], point[1], point[2]); }
224 bool looselyContains(
const double x,
const double y,
const double z)
226 return x >= m_LowerExtendedBound[0]
227 && y >= m_LowerExtendedBound[1]
228 && z >= m_LowerExtendedBound[2]
229 && x <= m_UpperExtendedBound[0]
230 && y <= m_UpperExtendedBound[1]
231 && z <= m_UpperExtendedBound[2];
240 bool looselyContains(
const std::array<double, 3>& lowerCorner,
const std::array<double, 3>& upperCorner)
242 return lowerCorner[0] >= m_LowerExtendedBound[0]
243 && lowerCorner[1] >= m_LowerExtendedBound[1]
244 && lowerCorner[2] >= m_LowerExtendedBound[2]
245 && upperCorner[0] <= m_UpperExtendedBound[0]
246 && upperCorner[1] <= m_UpperExtendedBound[1]
247 && upperCorner[2] <= m_UpperExtendedBound[2];
256 bool looselyOverlaps(
const std::array<double, 3>& lowerCorner,
const std::array<double, 3>& upperCorner)
258 return upperCorner[0] >= m_LowerExtendedBound[0]
259 && upperCorner[1] >= m_LowerExtendedBound[1]
260 && upperCorner[2] >= m_LowerExtendedBound[2]
261 && lowerCorner[0] <= m_UpperExtendedBound[0]
262 && lowerCorner[1] <= m_UpperExtendedBound[1]
263 && lowerCorner[2] <= m_UpperExtendedBound[2];
280 bool m_bIsLeaf =
true;
286 uint32_t m_PrimitiveCounts[OctreePrimitiveType::NumPrimitiveTypes];
316 friend class LooseOctreeTest;
326 explicit LooseOctree(
const Vec3d& center,
const double width,
const double minWidth,
327 const double minWidthRatio = 1.0,
const std::string name =
"LooseOctree");
337 virtual void clear();
372 uint32_t
getNumActiveNodes()
const {
return m_NumAllocatedNodes - m_NumAvaiableBlocksInPool * 8u; }
387 uint32_t getMaxNumPrimitivesInNodes()
const;
397 bool hasGeometry(uint32_t geomIdx)
const {
return m_sGeometryIndices.find(geomIdx) != m_sGeometryIndices.end(); }
403 uint32_t addPointSet(
const std::shared_ptr<PointSet>& pointset);
409 uint32_t addTriangleMesh(
const std::shared_ptr<SurfaceMesh>& surfMesh);
415 uint32_t addAnalyticalGeometry(
const std::shared_ptr<Geometry>& geometry);
436 void addGeometry(
const uint32_t geomIdx);
441 void removeGeometry(
const uint32_t geomIdx);
451 void populatePointPrimitives();
462 void incrementalUpdate();
468 void updatePositionAndCheckValidity();
479 void removeInvalidPrimitivesFromNodes();
506 void allocateMoreNodeBlock(
const uint32_t numBlocks);
511 void deallocateMemoryPool();
526 uint32_t m_NumAvaiableBlocksInPool = 0;
538 std::vector<OctreePrimitive*> m_vPrimitivePtrs[OctreePrimitiveType::NumPrimitiveTypes];
542 std::vector<OctreePrimitive*> m_pPrimitiveBlocks[OctreePrimitiveType::NumPrimitiveTypes];
547 bool m_bAlwaysRebuild =
false;
548 bool m_bCompleteBuild =
false;
552 bool m_bDrawNonEmptyParent =
true;
556 #pragma warning(default : 4201)
std::array< double, 3 > m_Position
For a point primitive, store its position.
bool m_useMaxDepth
If on max depth specified by user will be used, otherwise maxdepth is based of minwidth.
size_t getPrimitiveCount(const OctreePrimitiveType type) const
Get the number of primitives of the given type.
const uint32_t m_Idx
Index of the primitive in the parent geometry (such as index of the triangle in a mesh) ...
OctreeNode *const m_pRootNode
Root node, should not be reassigned throughout the existence of the tree.
uint32_t getMaxDepth() const
Get the maximum depth in this tree, which is computed based on the minWidth value.
LooseOctree * m_pTree
Pointer to the octree, used to request children from memory pool during splitting node...
tbb::concurrent_unordered_set< OctreeNodeBlock * > m_sActiveTreeNodeBlocks
Set of node blocks that are in use (node blocks that have been taken from memory pool) ...
const double m_Width
Width of the tree bounding box.
bool contains(const std::array< double, 3 > &lowerCorner, const std::array< double, 3 > &upperCorner)
Check if the given non-point primitive (triangle/analytical geometry) is exactly contained in the nod...
OctreePrimitiveType
The OctreePrimitiveType enum Type of primitive stored in the octree.
uint32_t getNumActiveNodes() const
Get the number of active nodes in the tree (the non-leaf nodes or leaf nodes that contain primitives)...
double getWidth() const
Return width of the tree.
bool contains(const Vec3d &point)
Check if the given point is contained exactly in the node boundary (bounding box) ...
const uint32_t m_Depth
Depth of this node (depth > 0, depth = 1 starting at the root node)
uint32_t getNumAllocatedNodes() const
Get the total number of tree nodes that have been allocated in memory.
bool looselyContains(const std::array< double, 3 > &lowerCorner, const std::array< double, 3 > &upperCorner)
Check if the given non-point primitive (triangle/analytical geometry) is contained in the node loose ...
std::array< double, 3 > m_LowerCorner
For a non-point primitive, store its AABB's lower corner.
std::array< double, 3 > m_UpperCorner
For a non-point primitive, store its AABB's upper corner.
const Vec3d m_LowerBound
The AABB's lower corner of the node.
ParallelUtils::SpinLock m_PoolLock
Atomic lock for multi-threading modification of the memory pool.
double getMinWidth() const
Return width of the lowest level tree nodes.
Geometry *const m_pGeometry
Pointer to the parent geometry that the primitive belong to.
const uint32_t m_GeomIdx
Global index of the parent geometry.
ParallelUtils::SpinLock m_NodeSplitingLock
Mutex lock for thread-safe splitting node.
OctreeNode()
Dummy constructor, called only during memory allocation in memory pool.
uint32_t m_MaxDepth
Cache the max depth of the tree (maximum depth level possible)
size_t getNumGeometries() const
Get number of geometries that have been added to the octree.
const Vec3d m_UpperExtendedBound
The extended AABB's upper corner of the node, which is 2X bigger than the exact AABB.
void setAlwaysRebuild(const bool bAlwaysRebuild)
Set the alwaysRebuild flag (true: rebuilt the tree from scratch in every update, false: incrementally...
const Vec3d m_UpperBound
The AABB's upper corner of the node.
const Vec3d m_Center
Center of the tree.
const Vec3d m_Center
Center of this node.
bool looselyOverlaps(const std::array< double, 3 > &lowerCorner, const std::array< double, 3 > &upperCorner)
Check if the bounding box of the given primitive (triangle/analytical geometry) overlaps with the loo...
uint32_t getPrimitiveCount(const OctreePrimitiveType type) const
Get the number of primitives of the given type in this node.
Base class for any geometrical representation.
const Vec3d m_LowerExtendedBound
The extended AABB's lower corner of the node, which is 2X bigger than the exact AABB.
double m_MinWidth
Minimum width allowed for the tree nodes.
uint32_t m_MaxDepth
Max depth of the tree, which is computed based on m_MinWidth.
const double m_HalfWidth
Half width of the node AABB.
const Vec3d getCenter() const
Return center of the tree.
The OctreePrimitive struct For each octree primitive (point/triangle/analytical geometry), store its relevant data.
OctreeNode * getRootNode() const
Get the root node.
const double m_MinWidthRatio
If there is no point primitive, minWidth will be recomputed as minWidth = min(width of all non-point ...
uint32_t m_NumAllocatedNodes
Count the total number of allocated nodes so far.
const std::string m_Name
Name of the tree.
std::vector< OctreeNodeBlock * > m_pNodeBigBlocks
Class LooseOctree, where each tree node has a loose boundary which is exactly twice big as its exact...
bool looselyContains(const Vec3d &point)
Check if the given point is contained in the node loose boundary (which is 2X bigger than the boundin...
OctreeNode * m_pParent
Pointer to the parent node.
bool isLeaf() const
Check if this node is a leaf node.
OctreePrimitive * getPrimitiveList(const OctreePrimitiveType type) const
For the given primitive type, return the head node of the primitive list of that type.
The OctreeNodeBlock struct This is a data structure to store a memory block of 8 tree node at a time ...
const Vec6d getBounds()
Get the bounds of the node.
OctreeNode * m_pNode
Pointer to the octree node containing the primitive.
bool hasGeometry(uint32_t geomIdx) const
Check if a geometry with the given geometry index has been added to the octree before.
std::unordered_set< uint32_t > m_sGeometryIndices
List of all indices of the added geometries, to check for duplication such that one geometry cannot b...
const Vec6d getLooseBounds()
Get the loose bounds of the node.
OctreePrimitive * m_pNext
Pointer to the next node in the primitive list of the octree node.