1#ifndef AABBTree_def_hpp
2#define AABBTree_def_hpp
15 template <
class SC,
class LO,
class GO,
class NO>
16 AABBTree<SC, LO, GO, NO>::AABBTree():
33 template <
class SC,
class LO,
class GO,
class NO> \
34 void AABBTree<SC, LO, GO, NO>::createTreeFromElements(
35 ElementsPtr_Type elements,
36 vec2D_dbl_ptr_Type nodes,
44 std::cout <<
" Starting AABBTree::'createTreeFromElements'" <<
'\n';
47 std::cout <<
" Starting to create an AABB-Tree" <<
'\n';
48 int numElems = elements->numberElements();
51 vec2D_dbl_ptr_Type triangleMinMax(
58 vec_int_Type localNodes;
60 double currentNodeX, currentNodeY;
62 for (
int elem=0; elem<numElems; elem++){
63 localNodes = elements->getElement(elem).getVectorNodeList();
65 currentNode = localNodes[0];
66 currentNodeX = nodes->at(currentNode).at(0);
67 currentNodeY = nodes->at(currentNode).at(1);
69 triangleMinMax->at(elem).at(0) = currentNodeX;
70 triangleMinMax->at(elem).at(1) = currentNodeY;
72 triangleMinMax->at(elem).at(2) = currentNodeX;
73 triangleMinMax->at(elem).at(3) = currentNodeY;
74 for (
int node=1; node<3; node++ ){
75 currentNode = localNodes[node];
76 currentNodeX = nodes->at(currentNode).at(0);
77 currentNodeY = nodes->at(currentNode).at(1);
79 if ( currentNodeX < triangleMinMax->at(elem).at(0) )
80 triangleMinMax->at(elem).at(0) = currentNodeX;
82 if ( currentNodeY < triangleMinMax->at(elem).at(1) )
83 triangleMinMax->at(elem).at(1) = currentNodeY;
85 if ( currentNodeX > triangleMinMax->at(elem).at(2) )
86 triangleMinMax->at(elem).at(2) = currentNodeX;
88 if ( currentNodeY > triangleMinMax->at(elem).at(3) )
89 triangleMinMax->at(elem).at(3) = currentNodeY;
93 createTreeFromRectangles(
102 std::cout <<
" Ending AABBTree::'createTreeFromElements'" <<
'\n';
122 template <
class SC,
class LO,
class GO,
class NO> \
123 void AABBTree<SC, LO, GO, NO>::createTreeFromRectangles(
124 vec2D_dbl_ptr_Type initMinMaxXY,
142 std::cout <<
" Starting AABBTree::'createTreeFromRectangles'" <<
'\n';
147 int numberElements = initMinMaxXY->size();
148 std::cout <<
" Number of Elements = " << numberElements <<
'\n';
158 vec2D_dbl_ptr_Type recMin(
164 vec2D_dbl_ptr_Type recMax(
171 vec2D_int_ptr_Type parentChild
178 std::vector<std::list<int> > containedElements(
183 vec_int_Type stack(numberElements, 0);
186 vec_dbl_Type bigMinMax = initMinMaxXY->at(0);
187 for(
int elem=1; elem<numberElements; elem++){
188 if (initMinMaxXY->at(elem).at(0) < bigMinMax[0])
189 bigMinMax[0] = initMinMaxXY->at(elem).at(0);
190 if (initMinMaxXY->at(elem).at(1) < bigMinMax[1])
191 bigMinMax[1] = initMinMaxXY->at(elem).at(1);
192 if (initMinMaxXY->at(elem).at(2) > bigMinMax[2])
193 bigMinMax[2] = initMinMaxXY->at(elem).at(2);
194 if (initMinMaxXY->at(elem).at(3) > bigMinMax[3])
195 bigMinMax[3] = initMinMaxXY->at(elem).at(3);
199 vec_dbl_Type inflateBy(2, 0.0);
200 inflateBy[0] = bigMinMax[2] - bigMinMax[0];
201 inflateBy[1] = bigMinMax[3] - bigMinMax[1];
202 for (
int elem=0; elem<numberElements; elem++){
203 initMinMaxXY->at(elem).at(0) -= 1e-13 * inflateBy[0];
204 initMinMaxXY->at(elem).at(1) -= 1e-13 * inflateBy[1];
205 initMinMaxXY->at(elem).at(2) += 1e-13 * inflateBy[0];
206 initMinMaxXY->at(elem).at(3) += 1e-13 * inflateBy[1];
209 vec2D_dbl_ptr_Type recCenter(
216 vec2D_dbl_ptr_Type recLength(
224 for (
int elem = 0; elem<numberElements; elem++){
225 recCenter->at(elem).at(0) = initMinMaxXY->at(elem).at(0) + initMinMaxXY->at(elem).at(2);
226 recCenter->at(elem).at(0) *= 0.5;
227 recCenter->at(elem).at(1) = initMinMaxXY->at(elem).at(1) + initMinMaxXY->at(elem).at(3);
228 recCenter->at(elem).at(1) *= 0.5;
230 recLength->at(elem).at(0) = initMinMaxXY->at(elem).at(2) - initMinMaxXY->at(elem).at(0);
231 recLength->at(elem).at(0) = initMinMaxXY->at(elem).at(3) - initMinMaxXY->at(elem).at(1);
236 recMin->at(0).at(0) = bigMinMax[0] - 1e-13 * inflateBy[0];
237 recMin->at(0).at(1) = bigMinMax[1] - 1e-13 * inflateBy[1];
238 recMax->at(0).at(0) = bigMinMax[2] + 1e-13 * inflateBy[0];
239 recMax->at(0).at(1) = bigMinMax[3] + 1e-13 * inflateBy[1];
241 for (
int elem = 0; elem<numberElements; elem++){
242 containedElements[0].push_back(elem);
246 std::cout <<
" First rectangle = " << recMin->at(0).at(0) <<
", " << recMin->at(0).at(1)<<
", " << recMax->at(0).at(0) <<
", " << recMax->at(0).at(1) <<
'\n';
256 int nodesOnStack = 0;
258 int sonLeft, sonRight, parentNode, axis;
259 int countCurrentElements, countShortRectangles, countLongRectangles;
260 int countLeft, countRight;
261 double lengthAxis, splitPosition, volumeParent, volumeLeft, volumeRight;
262 std::list<int> currentElements;
263 vec_dbl_Type dd(2, 0.0);
264 vec_int_Type ia(2, 0);
265 std::vector<bool> isLong(numberElements,
false);
266 std::vector<bool> isLeft(numberElements,
false);
268 std::cout <<
"##### Beginning Main Loop #####" <<
'\n';
270 while (nodesOnStack != -1){
272 std::cout <<
" nodesOnStack = " << nodesOnStack <<
'\n';
275 parentNode = stack[nodesOnStack];
276 nodesOnStack = nodesOnStack - 1;
279 std::cout <<
" main loop for node = " << parentNode <<
'\n';
283 sonLeft = numberNodes;
284 sonRight = numberNodes + 1;
287 std::cout <<
" sonLeft = " << sonLeft <<
'\n';
288 std::cout <<
" sonRight = " << sonRight <<
'\n';
292 currentElements = containedElements[parentNode];
293 countCurrentElements = currentElements.size();
295 std::cout <<
" count currentElements = " << countCurrentElements <<
'\n';
298 dd[0] = recMax->at(parentNode).at(0) - recMin->at(parentNode).at(0);
299 dd[1] = recMax->at(parentNode).at(1) - recMin->at(parentNode).at(1);
312 std::cout <<
" lengthAxis[0] = " << dd[0] <<
", lengthAxis[1] = "<< dd[1] <<
'\n';
315 for (
int id=1;
id>-1;
id--){
320 countShortRectangles = 0;
321 countLongRectangles = 0;
322 for (
auto const& elem : currentElements) {
323 if(recLength->at(elem).at(axis) > longTol * lengthAxis){
326 countLongRectangles += 1;
330 isLong[elem] =
false;
331 countShortRectangles += 1;
334 if (countLongRectangles < 0.5*countShortRectangles
335 && countLongRectangles < 0.5 * numberObjects){
340 std::cout <<
" splitting on axis = " << axis <<
", with length = " << lengthAxis <<
'\n';
341 std::cout <<
" countShortRectangles = " << countShortRectangles <<
'\n';
342 std::cout <<
" countLongRectangles = " << countLongRectangles <<
'\n';
345 if(countShortRectangles == 0){
353 for (
auto const& elem : currentElements){
355 splitPosition = splitPosition + recCenter->at(elem).at(axis);
358 splitPosition = splitPosition / countShortRectangles;
361 std::cout <<
" split position = " << splitPosition <<
'\n';
369 for (
auto const& elem : currentElements){
371 if (recCenter->at(elem).at(axis) < splitPosition){
376 isLeft[elem] =
false;
383 std::cout <<
" countLeft = " << countLeft <<
'\n';
384 std::cout <<
" countRight = " << countRight <<
'\n';
386 if (countLeft == 0 || countRight == 0) {
393 recMin->at(sonLeft).at(0) = recMax->at(parentNode).at(0);
394 recMin->at(sonLeft).at(1) = recMax->at(parentNode).at(1);
395 recMax->at(sonLeft).at(0) = recMin->at(parentNode).at(0);
396 recMax->at(sonLeft).at(1) = recMin->at(parentNode).at(1);
398 recMin->at(sonRight).at(0) = recMax->at(parentNode).at(0);
399 recMin->at(sonRight).at(1) = recMax->at(parentNode).at(0);
400 recMax->at(sonRight).at(0) = recMin->at(parentNode).at(0);
401 recMax->at(sonRight).at(1) = recMin->at(parentNode).at(0);
402 containedElements[parentNode].clear();
403 containedElements[sonLeft].clear();
404 containedElements[sonRight].clear();
405 for(
auto elem : currentElements){
408 containedElements[parentNode].push_back(elem);
414 containedElements[sonLeft].push_back(elem);
415 if (initMinMaxXY->at(elem).at(0) < recMin->at(sonLeft).at(0)){
416 recMin->at(sonLeft).at(0) = initMinMaxXY->at(elem).at(0);
418 if (initMinMaxXY->at(elem).at(1) < recMin->at(sonLeft).at(1)){
419 recMin->at(sonLeft).at(1) = initMinMaxXY->at(elem).at(1);
421 if (initMinMaxXY->at(elem).at(2) > recMax->at(sonLeft).at(0)){
422 recMax->at(sonLeft).at(0) = initMinMaxXY->at(elem).at(2);
424 if (initMinMaxXY->at(elem).at(3) > recMax->at(sonLeft).at(1)){
425 recMax->at(sonLeft).at(1) = initMinMaxXY->at(elem).at(3);
430 containedElements[sonRight].push_back(elem);
431 if (initMinMaxXY->at(elem).at(0) < recMin->at(sonRight).at(0)){
432 recMin->at(sonRight).at(0) = initMinMaxXY->at(elem).at(0);
434 if (initMinMaxXY->at(elem).at(1) < recMin->at(sonRight).at(1)){
435 recMin->at(sonRight).at(1) = initMinMaxXY->at(elem).at(1);
437 if (initMinMaxXY->at(elem).at(2) > recMax->at(sonRight).at(0)){
438 recMax->at(sonRight).at(0) = initMinMaxXY->at(elem).at(2);
440 if (initMinMaxXY->at(elem).at(3) > recMax->at(sonRight).at(1)){
441 recMax->at(sonRight).at(1) = initMinMaxXY->at(elem).at(3);
448 std::cout <<
" left rectangle (" << recMin->at(sonLeft).at(0) <<
", " << recMin->at(sonLeft).at(1)<<
"), (" << recMax->at(sonLeft).at(0) <<
", " << recMax->at(sonLeft).at(1) <<
")"<<
'\n';
449 std::cout <<
" right rectangle (" << recMin->at(sonRight).at(0) <<
", " << recMin->at(sonRight).at(1)<<
"), (" << recMax->at(sonRight).at(0) <<
", " << recMax->at(sonRight).at(1) <<
")"<<
'\n';
452 if (countCurrentElements <= numberObjects){
456 std::cout <<
" Checking for Volume Constraint (ie objects constraint satisfied)" <<
'\n';
459 (recMax->at(parentNode).at(0) - recMin->at(parentNode).at(0))\
460 * (recMax->at(parentNode).at(1) - recMin->at(parentNode).at(1));
462 (recMax->at(sonLeft).at(0) - recMin->at(sonLeft).at(0))\
463 * (recMax->at(sonLeft).at(1) - recMin->at(sonLeft).at(1));
465 (recMax->at(sonRight).at(0) - recMin->at(sonRight).at(0))\
466 * (recMax->at(sonRight).at(1) - recMin->at(sonRight).at(1));
467 if (volumeLeft + volumeRight < volumeTol * volumeParent){
469 parentChild->at(sonLeft).at(0) = parentNode;
470 parentChild->at(sonRight).at(0) = parentNode;
471 parentChild->at(parentNode).at(1) = sonLeft;
473 stack[nodesOnStack + 1] = sonLeft;
474 stack[nodesOnStack + 2] = sonRight;
475 nodesOnStack = nodesOnStack + 2;
476 numberNodes = numberNodes + 2;
481 containedElements[parentNode].splice(
482 containedElements[parentNode].begin(),
483 containedElements[sonLeft]
485 containedElements[parentNode].splice(
486 containedElements[parentNode].begin(),
487 containedElements[sonRight]
494 std::cout <<
" Objects constraint not satisfied, Setting indices" <<
'\n';
496 parentChild->at(sonLeft).at(0) = parentNode;
497 parentChild->at(sonRight).at(0) = parentNode;
498 parentChild->at(parentNode).at(1) = sonLeft;
499 stack[nodesOnStack + 1] = sonLeft;
500 stack[nodesOnStack + 2] = sonRight;
501 nodesOnStack = nodesOnStack + 2;
502 numberNodes = numberNodes + 2;
506 std::cout <<
" nodesOnStack = " << nodesOnStack <<
'\n';
510 std::cout <<
" " <<
'\n';
515 std::cout <<
"##### End Main Loop #####" <<
'\n';
518 std::cout <<
" Number of nodes in Tree = " << numberNodes <<
'\n';
521 recMin->resize(numberNodes);
522 recMax->resize(numberNodes);
523 parentChild->resize(numberNodes);
524 containedElements.resize(numberNodes);
528 this->numNodes_ = numberNodes;
529 this->minXY_ = recMin;
530 this->maxXY_ = recMax;
531 this->parentChild_ = parentChild;
532 this->containedElements_ = containedElements;
533 this->empty_ =
false;
536 std::cout <<
"##### End AABBTree::'createTreeFromRectangles' #####" <<
'\n';
554 template <
class SC,
class LO,
class GO,
class NO> \
555 std::tuple<std::map<int, std::list<int>>, std::map<int, std::list<int> > > \
556 AABBTree<SC, LO, GO, NO>::scanTree(
557 vec2D_dbl_ptr_Type queryPoints,
562 std::cout <<
" Starting AABBTree::'scanTree'" <<
'\n';
565 if (queryPoints->at(0).size() != this->dim_){
566 std::cerr <<
"Query point dimension =" << queryPoints->at(0).size() <<
", but expected dim = " << this->dim_ <<
'\n';
568 int numberQueryPoints = queryPoints->size();
571 std::map<int, std::list<int>> treeToItem;
572 std::map<int, std::list<int>> itemToTree;
585 std::vector<std::list<int> > pointsInNode(
590 int leftSon, rightSon;
591 int nodesOnStack = 0;
592 int parentNode, no=0;
594 for (
int point = 0; point<queryPoints->size(); point++){
595 pointsInNode[0].push_back(point);
597 while (nodesOnStack != -1){
599 parentNode = stack[nodesOnStack];
600 nodesOnStack = nodesOnStack - 1;
603 std::cout <<
" main loop for node = " << parentNode <<
'\n';
606 if (!this->containedElements_[parentNode].empty()){
608 treeToItem[parentNode] = pointsInNode[parentNode];
611 std::cout <<
" Node " << parentNode <<
" is not empty, pushing onto tree-item mapping" <<
'\n';
615 if(this->parentChild_->at(parentNode).at(1) != 0){
617 leftSon = parentChild_->at(parentNode).at(1);
618 rightSon = parentChild_->at(parentNode).at(1) + 1;
620 std::cout <<
"leftSon = "<< leftSon <<
", rightSon = " << rightSon <<
'\n';
623 for (
auto point : pointsInNode[parentNode]){
624 if(isInRectangle(leftSon, queryPoints->at(point),
false)){
626 pointsInNode[leftSon].push_back(point);
628 if(isInRectangle(rightSon, queryPoints->at(point),
false)){
630 pointsInNode[rightSon].push_back(point);
633 if (!pointsInNode[leftSon].empty()){
635 nodesOnStack = nodesOnStack + 1;
636 stack[nodesOnStack] = leftSon;
640 if (!pointsInNode[leftSon].empty()){
642 nodesOnStack = nodesOnStack + 1;
643 stack[nodesOnStack] = rightSon;
654 for (
auto keyValue: treeToItem){
656 for (
auto value: keyValue.second){
658 itemToTree[value].push_back(keyValue.first);
663 std::cout <<
" End AABBTree::'scanTree'" <<
'\n';
666 return std::make_tuple(treeToItem, itemToTree);
672 template <
class SC,
class LO,
class GO,
class NO> \
674 AABBTree<SC, LO, GO, NO>::isInRectangle(
676 vec_dbl_Type queryPoint,
682 std::cout <<
" Start AABBTree::'isInRectangle'" <<
'\n';
685 vec_dbl_Type rectangleMin, rectangleMax;
686 rectangleMin = this->minXY_->at(numberRectangle);
687 rectangleMax = this->maxXY_->at(numberRectangle);
689 for (
int dim = 0; dim < this->dim_-1; dim ++){
691 && queryPoint[dim] > rectangleMin[dim] \
692 && queryPoint[dim] < rectangleMax[dim]){
700 std::cout <<
" End AABBTree::'isInRectangle'" <<
'\n';
706 template <
class SC,
class LO,
class GO,
class NO> \
707 bool AABBTree<SC, LO, GO, NO>::isEmpty(){
711 template <
class SC,
class LO,
class GO,
class NO> \
712 int AABBTree<SC, LO, GO, NO>::getDim(){
716 template <
class SC,
class LO,
class GO,
class NO> \
717 int AABBTree<SC, LO, GO, NO>::getNumNodes(){
721 template <
class SC,
class LO,
class GO,
class NO> \
722 std::list<int> AABBTree<SC, LO, GO, NO>::getElements(
int node){
723 return containedElements_[node];
Adaptive Mesh Refinement.
Definition AdaptiveMeshRefinement_decl.hpp:36