67 enum { dimworld = GeometricEntitySet::dimensionworld };
68 using ctype =
typename GeometricEntitySet::ctype;
75 struct BoundingBoxNode
93 void build(std::shared_ptr<const GeometricEntitySet> set)
99 boundingBoxNodes_.clear();
100 boundingBoxCoordinates_.clear();
106 const auto numLeaves = set->size();
109 const auto numNodes = 2*numLeaves - 1;
110 boundingBoxNodes_.reserve(numNodes);
111 boundingBoxCoordinates_.reserve(numNodes*2*dimworld);
114 std::vector<ctype> leafBoxes(2*dimworld*numLeaves);
116 for (
const auto& geometricEntity : *set)
117 computeEntityBoundingBox_(leafBoxes.data() + 2*dimworld*set->index(geometricEntity), geometricEntity);
120 std::vector<std::size_t> leafPartition(numLeaves);
121 std::iota(leafPartition.begin(), leafPartition.end(), 0);
124 build_(leafBoxes, leafPartition.begin(), leafPartition.end());
128 <<
" nodes for " << numLeaves <<
" grid entites in "
129 << timer.stop() <<
" seconds." << std::endl;
134 {
return *entitySet_; }
142 {
return boundingBoxNodes_[nodeIdx]; }
146 {
return boundingBoxCoordinates_.data() + 2*dimworld*nodeIdx; }
150 {
return boundingBoxNodes_.size(); }
154 bool isLeaf(
const BoundingBoxNode& node, std::size_t nodeIdx)
const
155 {
return node.child0 == nodeIdx; }
160 std::vector<BoundingBoxNode> boundingBoxNodes_;
163 std::vector<ctype> boundingBoxCoordinates_;
166 std::shared_ptr<const EntitySet> entitySet_;
169 template <
class Entity>
170 void computeEntityBoundingBox_(ctype* b,
const Entity& entity)
const
174 ctype* xMax = b + dimworld;
177 auto geometry = entity.geometry();
180 auto corner = geometry.corner(0);
181 for (std::size_t dimIdx = 0; dimIdx < dimworld; ++dimIdx)
182 xMin[dimIdx] = xMax[dimIdx] = corner[dimIdx];
185 for (std::size_t cornerIdx = 1; cornerIdx < geometry.corners(); ++cornerIdx)
187 corner = geometry.corner(cornerIdx);
188 for (std::size_t dimIdx = 0; dimIdx < dimworld; ++dimIdx)
192 xMin[dimIdx] = min(xMin[dimIdx], corner[dimIdx]);
193 xMax[dimIdx] = max(xMax[dimIdx], corner[dimIdx]);
199 std::size_t build_(
const std::vector<ctype>& leafBoxes,
200 const std::vector<std::size_t>::iterator& begin,
201 const std::vector<std::size_t>::iterator& end)
206 if (end - begin == 1)
209 const std::size_t leafNodeIdx = *begin;
210 const auto beginCoords = leafBoxes.begin() + 2*dimworld*leafNodeIdx;
211 const auto endCoords = beginCoords + 2*dimworld;
216 return addBoundingBox_(BoundingBoxNode{
numBoundingBoxes(), leafNodeIdx}, beginCoords, endCoords);
220 const auto bCoords = computeBBoxOfBBoxes_(leafBoxes, begin, end);
223 const auto axis = computeLongestAxis_(bCoords);
227 auto middle = begin + (end - begin)/2;
228 std::nth_element(begin, middle, end, [&leafBoxes, axis](std::size_t i, std::size_t j)
230 const ctype* bi = leafBoxes.data() + 2*dimworld*i;
231 const ctype* bj = leafBoxes.data() + 2*dimworld*j;
232 return bi[axis] + bi[axis + dimworld] < bj[axis] + bj[axis + dimworld];
237 return addBoundingBox_(BoundingBoxNode{build_(leafBoxes, begin, middle), build_(leafBoxes, middle, end)},
238 bCoords.begin(), bCoords.end());
242 template <
class Iterator>
243 std::size_t addBoundingBox_(BoundingBoxNode&& node,
244 const Iterator& coordBegin,
245 const Iterator& coordEnd)
248 boundingBoxNodes_.emplace_back(node);
251 boundingBoxCoordinates_.insert(boundingBoxCoordinates_.end(), coordBegin, coordEnd);
254 return boundingBoxNodes_.size() - 1;
258 std::array<ctype, 2*dimworld>
259 computeBBoxOfBBoxes_(
const std::vector<ctype>& leafBoxes,
260 const std::vector<std::size_t>::iterator& begin,
261 const std::vector<std::size_t>::iterator& end)
263 std::array<ctype, 2*dimworld> bBoxCoords;
267 const auto* bFirst = leafBoxes.data() + 2*dimworld*(*it);
269 for (
int coordIdx = 0; coordIdx < 2*dimworld; ++coordIdx)
270 bBoxCoords[coordIdx] = bFirst[coordIdx];
273 for (; it != end; ++it)
275 const auto* b = leafBoxes.data() + 2*dimworld*(*it);
276 for (
int coordIdx = 0; coordIdx < dimworld; ++coordIdx)
277 if (b[coordIdx] < bBoxCoords[coordIdx])
278 bBoxCoords[coordIdx] = b[coordIdx];
279 for (
int coordIdx = dimworld; coordIdx < 2*dimworld; ++coordIdx)
280 if (b[coordIdx] > bBoxCoords[coordIdx])
281 bBoxCoords[coordIdx] = b[coordIdx];
288 std::size_t computeLongestAxis_(
const std::array<ctype, 2*dimworld>& bCoords)
290 std::array<ctype, dimworld> axisLength;
291 for (
int coordIdx = 0; coordIdx < dimworld; ++coordIdx)
292 axisLength[coordIdx] = bCoords[dimworld + coordIdx] - bCoords[coordIdx];
294 return std::distance(axisLength.begin(), std::max_element(axisLength.begin(), axisLength.end()));