35 : _volumeDimensions(
Vector3ui(0u, 0u, 0u))
38 CORE_INFO(
"Nb of vectors : " << vectors.size());
42 Vector3ui octreeSize(_pow2roundup(std::ceil((maxAABB.x - minAABB.x) / voxelSize)),
43 _pow2roundup(std::ceil((maxAABB.y - minAABB.y) / voxelSize)),
44 _pow2roundup(std::ceil((maxAABB.z - minAABB.z) / voxelSize)));
47 _octreeSize = std::max(std::max(octreeSize.x, octreeSize.y), octreeSize.z);
49 CORE_INFO(
"Vector Octree size : " << _octreeSize);
51 _depth = std::log2(_octreeSize) + 1u;
52 std::vector<VectorOctreeLevelMap> octree(_depth);
54 CORE_INFO(
"Vector Octree depth : " << _depth <<
" " << octree.size());
59 for (uint32_t i = 0; i < vectors.size(); ++i)
61 CORE_PROGRESS(
"Bulding Vector Octree from vectors", i, vectors.size());
62 const uint32_t xpos = std::floor((vectors[i].position.x - minAABB.x) / voxelSize);
63 const uint32_t ypos = std::floor((vectors[i].position.y - minAABB.y) / voxelSize);
64 const uint32_t zpos = std::floor((vectors[i].position.z - minAABB.z) / voxelSize);
65 const Vector3d &value = vectors[i].direction;
67 const uint32_t indexX = xpos;
68 const uint32_t indexY = ypos * (uint32_t)_octreeSize;
69 const uint32_t indexZ = zpos * (uint32_t)_octreeSize * (uint32_t)_octreeSize;
71 auto it = octree[0].find(indexX + indexY + indexZ);
72 if (it == octree[0].end())
75 for (uint32_t level = 0; level < _depth; ++level)
78 const uint32_t divisor = std::pow(2, level);
79 const Vector3f center(xpos, ypos, zpos);
81 const uint32_t nBlock = _octreeSize / divisor;
82 const uint32_t index = std::floor(xpos / divisor) + nBlock * std::floor(ypos / divisor) +
83 nBlock * nBlock * std::floor(zpos / divisor);
85 const double size = voxelSize * (level + 1u);
87 if (octree[level].find(index) == octree[level].end())
89 octree[level].insert(VectorOctreeLevelMap::value_type(index,
VectorOctreeNode(center, size)));
93 octree[level].at(index).addValue(value);
95 if ((level > 0) && (child !=
nullptr))
96 octree[level].at(index).setChild(child);
99 child = &(octree[level].at(index));
106 for (uint32_t level = 0; level < _depth; ++level)
108 const uint32_t divisor = std::pow(2, level);
109 const uint32_t nBlock = _octreeSize / divisor;
110 const uint32_t index = std::floor(xpos / divisor) + nBlock * std::floor(ypos / divisor) +
111 nBlock * nBlock * std::floor(zpos / divisor);
112 octree[level].at(index).addValue(value);
116 for (uint32_t i = 0; i < octree.size(); ++i)
117 CORE_DEBUG(
"Number of leaves [" << i <<
"]: " << octree[i].size());
120 _offsetPerLevel.resize(_depth);
121 _offsetPerLevel[_depth - 1u] = 0;
122 uint32_t previousOffset = 0u;
123 for (uint32_t i = _depth - 1u; i > 0u; --i)
125 _offsetPerLevel[i - 1u] = previousOffset + octree[i].size();
126 previousOffset = _offsetPerLevel[i - 1u];
129 uint32_t totalNodeNumber = 0;
131 for (uint32_t i = 0; i < octree.size(); ++i)
132 totalNodeNumber += octree[i].size();
135 _flatIndices.resize(totalNodeNumber * 2u, 0);
139 _flattenChildren(&(octree[_depth - 1u].at(0)), _depth - 1u);
142 Vector3ui(std::ceil((maxAABB.x - minAABB.x) / voxelSize), std::ceil((maxAABB.y - minAABB.y) / voxelSize),
143 std::ceil((maxAABB.z - minAABB.z) / voxelSize));
144 _volumeSize = (uint32_t)_volumeDimensions.x * (uint32_t)_volumeDimensions.y * (uint32_t)_volumeDimensions.z;
151 const std::vector<VectorOctreeNode *> children = node->getChildren();
152 const auto &position = node->getCenter();
153 const auto &direction = node->getValue();
154 if ((children.empty()) || (level == 0))
163 _offsetPerLevel[level] += 1u;
173 _flatIndices[_offsetPerLevel[level] * 2u] = _offsetPerLevel[level - 1];
174 _flatIndices[_offsetPerLevel[level] * 2u + 1] = _offsetPerLevel[level - 1] + children.size() - 1u;
175 _offsetPerLevel[level] += 1u;
177 for (VectorOctreeNode *child : children)
178 _flattenChildren(child, level - 1u);
The VectorOctreeNode class implement a spherical node of the Octree acceleration structure used by th...
VectorOctree(const OctreeVectors &vectors, double voxelSize, const Vector3d &minAABB, const Vector3d &maxAABB)
Construct a new VectorOctree object.
~VectorOctree()
Destroy the VectorOctree object.
std::map< uint32_t, VectorOctreeNode > VectorOctreeLevelMap
glm::vec< 3, uint32_t > Vector3ui
glm::vec< 3, double > Vector3d
std::vector< OctreeVector > OctreeVectors