utestOctree.cpp 3.47 KB
Newer Older
1
// See LICENCE file at project root
2 3
#include "FUTester.hpp"

BRAMAS Berenger's avatar
BRAMAS Berenger committed
4 5 6 7
#include "Containers/FOctree.hpp"
#include "Containers/FBlockAllocator.hpp"
#include "Containers/FVector.hpp"
#include "Components/FSimpleLeaf.hpp"
8

BRAMAS Berenger's avatar
BRAMAS Berenger committed
9 10
#include "Utils/FAssert.hpp"
#include "Utils/FPoint.hpp"
11

BRAMAS Berenger's avatar
BRAMAS Berenger committed
12 13
#include "Components/FBasicParticleContainer.hpp"
#include "Components/FBasicCell.hpp"
14

BRAMAS Berenger's avatar
BRAMAS Berenger committed
15
#include "Utils/FTic.hpp"
16 17 18 19 20 21 22 23 24 25

/**
  In this test we create a lot of different octree by using various height and subheigt
  then we insert particle in all leaf and we test that all leaves
  and all cells has been created.
  */


/** this class test the octree container */
class TestOctree : public FUTester<TestOctree> {
26
    typedef double FReal;
27
    typedef FBasicCell                   CellClass;
28
    typedef FBasicParticleContainer<FReal,0,FReal>      ContainerClass;
29

30 31
    typedef FSimpleLeaf<FReal, ContainerClass >                     LeafClass;
    typedef FOctree<FReal, CellClass, ContainerClass , LeafClass , FBasicBlockAllocator<CellClass> >  OctreeClass;
32 33 34 35 36 37 38 39 40 41

    // test size
    void TestAll(){
        const FReal BoxWidth = 1.0;
        const FReal BoxCenter = 0.5;

        // We try for many levels
        for(int idxHeight = 2 ; idxHeight < 8 ; ++idxHeight){

            // Compute the number of leaves for a tree of this height
berenger-bramas's avatar
berenger-bramas committed
42
            const int NbSmallBoxesPerSide = (1 << (idxHeight-1));
43 44 45 46 47 48 49 50
            const FReal SmallBoxWidth = BoxWidth / FReal(NbSmallBoxesPerSide);
            const FReal SmallBoxWidthDiv2 = SmallBoxWidth / 2;

            const int NbPart = NbSmallBoxesPerSide * NbSmallBoxesPerSide * NbSmallBoxesPerSide;

            // For each level we try many sub-levels
            for(int idxSub = 1 ; idxSub < idxHeight ; ++idxSub){

51
                OctreeClass tree(idxHeight, idxSub, BoxWidth, FPoint<FReal>(BoxCenter,BoxCenter,BoxCenter));
52 53 54 55 56

                // fill the tree
                for(int idxX = 0 ; idxX < NbSmallBoxesPerSide ; ++idxX){
                    for(int idxY = 0 ; idxY < NbSmallBoxesPerSide ; ++idxY){
                        for(int idxZ = 0 ; idxZ < NbSmallBoxesPerSide ; ++idxZ){
57
                            const FPoint<FReal> pos(FReal(idxX)*SmallBoxWidth + SmallBoxWidthDiv2,
58 59
                                                       FReal(idxY)*SmallBoxWidth + SmallBoxWidthDiv2,
                                                       FReal(idxZ)*SmallBoxWidth + SmallBoxWidthDiv2);
60
                            tree.insert(pos);
61 62 63 64 65 66 67 68 69 70 71 72
                        }
                    }
                }

                // test all cells
                OctreeClass::Iterator octreeIterator(&tree);
                octreeIterator.gotoBottomLeft();
                int nbCell = NbPart;
                for(int idxLevel = idxHeight - 1 ; idxLevel >= 1 ; --idxLevel ){
                    MortonIndex currentIndex = 0;
                    do{
                        // Morton index must increase one by one
73
                        uassert( currentIndex == octreeIterator.getCurrentGlobalIndex());
74 75 76
                        ++currentIndex;
                    } while(octreeIterator.moveRight());
                    // Then number of cells must be divided by 8 at each level
77
                    uassert(currentIndex == nbCell);
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
                    nbCell /= 8;

                    octreeIterator.moveUp();
                    octreeIterator.gotoLeft();
                }
            }
        }
    }

    // set test
    void SetTests(){
        AddTest(&TestOctree::TestAll,"Test Octree");
    }
};

// You must do this
TestClass(TestOctree)