69 enum { dimworld = GeometricEntitySet::dimensionworld };
70 using ctype =
typename GeometricEntitySet::ctype;
77 struct BoundingBoxNode
95 void build(std::shared_ptr<const GeometricEntitySet> set)
101 boundingBoxNodes_.clear();
102 boundingBoxCoordinates_.clear();
108 const auto numLeaves = set->size();
111 const auto numNodes = 2*numLeaves - 1;
112 boundingBoxNodes_.reserve(numNodes);
113 boundingBoxCoordinates_.reserve(numNodes*2*dimworld);
116 std::vector<ctype> leafBoxes(2*dimworld*numLeaves);
118 for (
const auto& geometricEntity : *set)
119 computeEntityBoundingBox_(leafBoxes.data() + 2*dimworld*set->index(geometricEntity), geometricEntity);
122 std::vector<std::size_t> leafPartition(numLeaves);
123 std::iota(leafPartition.begin(), leafPartition.end(), 0);
126 build_(leafBoxes, leafPartition.begin(), leafPartition.end());
130 <<
" nodes for " << numLeaves <<
" grid entities in "
131 << timer.stop() <<
" seconds." << std::endl;
136 {
return *entitySet_; }
144 {
return boundingBoxNodes_[nodeIdx]; }
148 {
return boundingBoxCoordinates_.data() + 2*dimworld*nodeIdx; }
152 {
return boundingBoxNodes_.size(); }
156 bool isLeaf(
const BoundingBoxNode& node, std::size_t nodeIdx)
const
157 {
return node.child0 == nodeIdx; }
162 std::vector<BoundingBoxNode> boundingBoxNodes_;
165 std::vector<ctype> boundingBoxCoordinates_;
168 std::shared_ptr<const EntitySet> entitySet_;
171 template <
class Entity>
172 void computeEntityBoundingBox_(ctype* b,
const Entity& entity)
const
176 ctype* xMax = b + dimworld;
179 auto geometry = entity.geometry();
182 auto corner = geometry.corner(0);
183 for (std::size_t dimIdx = 0; dimIdx < dimworld; ++dimIdx)
184 xMin[dimIdx] = xMax[dimIdx] = corner[dimIdx];
187 for (std::size_t cornerIdx = 1; cornerIdx < geometry.corners(); ++cornerIdx)
189 corner = geometry.corner(cornerIdx);
190 for (std::size_t dimIdx = 0; dimIdx < dimworld; ++dimIdx)
194 xMin[dimIdx] = min(xMin[dimIdx], corner[dimIdx]);
195 xMax[dimIdx] = max(xMax[dimIdx], corner[dimIdx]);
201 std::size_t build_(
const std::vector<ctype>& leafBoxes,
202 const std::vector<std::size_t>::iterator& begin,
203 const std::vector<std::size_t>::iterator& end)
208 if (end - begin == 1)
211 const std::size_t leafNodeIdx = *begin;
212 const auto beginCoords = leafBoxes.begin() + 2*dimworld*leafNodeIdx;
213 const auto endCoords = beginCoords + 2*dimworld;
218 return addBoundingBox_(BoundingBoxNode{
numBoundingBoxes(), leafNodeIdx}, beginCoords, endCoords);
222 const auto bCoords = computeBBoxOfBBoxes_(leafBoxes, begin, end);
225 const auto axis = computeLongestAxis_(bCoords);
229 auto middle = begin + (end - begin)/2;
230 std::nth_element(begin, middle, end, [&leafBoxes, axis](std::size_t i, std::size_t j)
232 const ctype* bi = leafBoxes.data() + 2*dimworld*i;
233 const ctype* bj = leafBoxes.data() + 2*dimworld*j;
234 return bi[axis] + bi[axis + dimworld] < bj[axis] + bj[axis + dimworld];
239 return addBoundingBox_(BoundingBoxNode{build_(leafBoxes, begin, middle), build_(leafBoxes, middle, end)},
240 bCoords.begin(), bCoords.end());
244 template <
class Iterator>
245 std::size_t addBoundingBox_(BoundingBoxNode&& node,
246 const Iterator& coordBegin,
247 const Iterator& coordEnd)
250 boundingBoxNodes_.emplace_back(node);
253 boundingBoxCoordinates_.insert(boundingBoxCoordinates_.end(), coordBegin, coordEnd);
256 return boundingBoxNodes_.size() - 1;
260 std::array<ctype, 2*dimworld>
261 computeBBoxOfBBoxes_(
const std::vector<ctype>& leafBoxes,
262 const std::vector<std::size_t>::iterator& begin,
263 const std::vector<std::size_t>::iterator& end)
265 std::array<ctype, 2*dimworld> bBoxCoords;
269 const auto* bFirst = leafBoxes.data() + 2*dimworld*(*it);
271 for (
int coordIdx = 0; coordIdx < 2*dimworld; ++coordIdx)
272 bBoxCoords[coordIdx] = bFirst[coordIdx];
275 for (; it != end; ++it)
277 const auto* b = leafBoxes.data() + 2*dimworld*(*it);
278 for (
int coordIdx = 0; coordIdx < dimworld; ++coordIdx)
279 if (b[coordIdx] < bBoxCoords[coordIdx])
280 bBoxCoords[coordIdx] = b[coordIdx];
281 for (
int coordIdx = dimworld; coordIdx < 2*dimworld; ++coordIdx)
282 if (b[coordIdx] > bBoxCoords[coordIdx])
283 bBoxCoords[coordIdx] = b[coordIdx];
290 std::size_t computeLongestAxis_(
const std::array<ctype, 2*dimworld>& bCoords)
292 std::array<ctype, dimworld> axisLength;
293 for (
int coordIdx = 0; coordIdx < dimworld; ++coordIdx)
294 axisLength[coordIdx] = bCoords[dimworld + coordIdx] - bCoords[coordIdx];
296 return std::distance(axisLength.begin(), std::max_element(axisLength.begin(), axisLength.end()));