iMSTK
Interactive Medical Simulation Toolkit
OctreeDebugModel.cpp
1 /*
2 ** This file is part of the Interactive Medical Simulation Toolkit (iMSTK)
3 ** iMSTK is distributed under the Apache License, Version 2.0.
4 ** See accompanying NOTICE for details.
5 */
6 
7 #include "OctreeDebugModel.h"
8 #include "imstkLooseOctree.h"
9 
10 namespace imstk
11 {
12 bool
14 {
15  if (node->m_Depth > static_cast<uint32_t>(m_maxDisplayDepth))
16  {
17  return false;
18  }
19 
20  // Compute the current nodes vertices
21  // Which nodes are rendered
22  // The amount of child nodes rendered
23  int renderCount = 0;
24  Vec3d vertices[8];
25  bool rendered[8]{ false, false, false, false,
26  false, false, false, false };
27 
28  // Recurse down the tree so long as the current node isn't a leaf
29  for (int i = 0; i < 8; i++)
30  {
31  vertices[i] = node->m_Center;
32  vertices[i][0] += (i & 1) ? node->m_HalfWidth : -node->m_HalfWidth;
33  vertices[i][1] += (i & 2) ? node->m_HalfWidth : -node->m_HalfWidth;
34  vertices[i][2] += (i & 4) ? node->m_HalfWidth : -node->m_HalfWidth;
35 
36  if (!node->isLeaf())
37  {
38  rendered[i] = updateGeom(&node->m_pChildren->m_Nodes[i]);
39  renderCount += static_cast<int>(rendered[i]);
40  }
41  }
42 
43  //--------------------------------------------------------
44  //
45  // 6-------7
46  // /| /|
47  // 2-+-----3 |
48  // | | | | y
49  // | 4-----+-5 | z
50  // |/ |/ |/
51  // 0-------1 +--x
52  //
53  // 0 => 0, 0, 0
54  // 1 => 0, 0, 1
55  // 2 => 0, 1, 0
56  // 3 => 0, 1, 1
57  // 4 => 1, 0, 0
58  // 5 => 1, 0, 1
59  // 6 => 1, 1, 0
60  // 7 => 1, 1, 1
61  //
62  //--------------------------------------------------------
63 
64  // If the current node is empty
65  if (node->m_PrimitiveCounts[OctreePrimitiveType::Point] == 0
66  && node->m_PrimitiveCounts[OctreePrimitiveType::Triangle] == 0
67  && node->m_PrimitiveCounts[OctreePrimitiveType::Analytical] == 0)
68  {
69  // If we shouldn't draw empty parents
70  if (!m_drawNonEmptyParents)
71  {
72  // So long as no child is rendered
73  return renderCount > 0;
74  }
75 
76  if (renderCount == 0 // Children did not render
77  && node->m_pTree->getRootNode() != node) // Not root node, and no data in this node)
78  {
79  return false;
80  }
81  }
82 
83  if (renderCount < 8) // If renderCount == 8 then no need to render this node
84  {
85  //const auto& debugLines = node->m_pTree->m_DebugGeometry;
86  for (int i = 0; i < 8; i++)
87  {
88  if ((i & 1) && (!rendered[i] || !rendered[i - 1]))
89  {
90  addLine(vertices[i], vertices[i - 1]);
91  }
92  if ((i & 2) && (!rendered[i] || !rendered[i - 2]))
93  {
94  addLine(vertices[i], vertices[i - 2]);
95  }
96  if ((i & 4) && (!rendered[i] || !rendered[i - 4]))
97  {
98  addLine(vertices[i], vertices[i - 4]);
99  }
100  }
101  }
102  return true;
103 }
104 
105 void
106 OctreeDebugModel::debugUpdate(int maxDisplayDepth, bool drawNonEmptyParents)
107 {
108  m_maxDisplayDepth = maxDisplayDepth;
109  m_drawNonEmptyParents = drawNonEmptyParents;
110 
111  // Clear the geometry
112  clear();
113 
114  if (m_looseOctree != nullptr)
115  {
116  // Buffer the vertices and indices for lines to avoid reallocations
117 
118  // Update the geometry, recursively appending for each node
119  updateGeom(m_looseOctree->getRootNode());
120  }
121 }
122 } // namespace imstk
LooseOctree * m_pTree
Pointer to the octree, used to request children from memory pool during splitting node...
int m_maxDisplayDepth
Maximum level of nodes that will be rendered during debug rendering.
const uint32_t m_Depth
Depth of this node (depth > 0, depth = 1 starting at the root node)
Compound Geometry.
OctreeNodeBlock * m_pChildren
Pointer to a memory block containing 8 children nodes.
void debugUpdate(int maxDisplayDepth, bool drawNonEmptyParents=true)
Update the geometry.
The OctreeNode class.
const Vec3d m_Center
Center of this node.
void addLine(const Vec3d &a, const Vec3d &b)
Adds a line to the debug lines with default color.
bool updateGeom(OctreeNode *node)
Recursively update debug geometry by adding lines drawing bounding boxes of the active nodes...
const double m_HalfWidth
Half width of the node AABB.
OctreeNode * getRootNode() const
Get the root node.
bool isLeaf() const
Check if this node is a leaf node.
uint32_t m_PrimitiveCounts[OctreePrimitiveType::NumPrimitiveTypes]
Count the number of (classified) primitives stored in this node.
void clear()
Clears all primitives.