testOctreePrintMorton.cpp 12.5 KB
Newer Older
1
// See LICENCE file at project root
berenger-bramas's avatar
berenger-bramas committed
2 3 4

#include <iostream>

5 6
#include <cstdio>
#include <cstdlib>
berenger-bramas's avatar
berenger-bramas committed
7 8 9
#include <time.h>
#include <string>

10 11 12 13 14
#include "Utils/FGlobal.hpp"
#include "Containers/FTreeCoordinate.hpp"
#include "Utils/FPoint.hpp"
#include "Utils/FMath.hpp"
#include "Utils/FParameterNames.hpp"
berenger-bramas's avatar
berenger-bramas committed
15 16 17 18 19 20 21 22 23

/**
* In this file we show the morton indexed for each boxes
* in a tree (but we did not build the tree because if we build
* an entire tree it may be tooo big!)
*/


/** Basic function to convert a morton index in decimal string */
24
std::string MortonToBinary(MortonIndex index, int level){
berenger-bramas's avatar
berenger-bramas committed
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
    std::string str;
    int bits = 1 << ((level * 3) - 1);
    int dim = 0;
    while(bits){
        if(index & bits) str.append("1");
        else str.append("0");
        bits >>= 1;
        // we put a dot each 3 values
        if(++dim == 3){
            str.append(".");
            dim = 0;
        }
    }
    return str;
}


42 43 44 45
int main(int argc, char ** argv){
    FHelpDescribeAndExit(argc, argv,
                         "Print the morton and cell properties of an octree\n"
                         "it is also interesting to look at the code if one needs to work with morton indexes");
46 47 48 49
    ///////////////////////What we do/////////////////////////////
    std::cout << ">> This executable has to be used to test morton index.\n";
    //////////////////////////////////////////////////////////////

50 51
    typedef double FReal;

berenger-bramas's avatar
berenger-bramas committed
52 53 54 55
    bool stop = false;
    char buffer[256];

    int treeLevel = 10;
56
    FPoint<FReal> centerOfBox(0.5,0.5,0.5);
57
    FReal rootBoxWidth = 1;
berenger-bramas's avatar
berenger-bramas committed
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114

    std::cout << "Welcome in the morton index test." << std::endl;

    while(!stop){
        /////////////////////////////////////////////////////////////////////////
        // Menu
        /////////////////////////////////////////////////////////////////////////
        std::cout << "-- Current states :\n";
        std::cout << "     tree level = " << treeLevel << "\n";
        std::cout << "     box width = " << rootBoxWidth << "\n";
        std::cout << "     center of the box ; x = " << centerOfBox.getX() << " y = " << centerOfBox.getY() << " z = " << centerOfBox.getZ() << "\n";

        std::cout << "-- Menu :\n";
        std::cout << "   1 - Print a morton index from position in tree\n";
        std::cout << "   2 - Print a morton index from real position\n";
        std::cout << "   3 - Print an interval of morton index\n";
        std::cout << "   4 - Print all morton index for a level\n";
        std::cout << "   5 - Change tree properties\n";
        std::cout << "   6 - Find tree coordinate & position from morton index & level\n";
        std::cout << "   0 - Quit current app\n";

        int userChoice = 0;
        do{
            std::cout << "Select action : ";
            std::cin.getline( buffer , sizeof(buffer) , '\n');
            userChoice = -1;
            sscanf(buffer,"%d",&userChoice);
        } while( userChoice < 0 || userChoice > 7);

        /////////////////////////////////////////////////////////////////////////
        // Actions
        /////////////////////////////////////////////////////////////////////////
        switch(userChoice){
        case 0: stop = true;
            break;
        case 1: /////////////////////////////////////////////////////////////////
            {
                std::cout << "-- Morton index from position :\n";
                std::cout << "    You will now give the position in the tree and the level to compute morton index.\n";

                int requiredlevel = treeLevel - 1;
                std::cout << "    level (default is leaf level = " << (treeLevel-1) << ") : ";
                std::cin.getline( buffer , sizeof(buffer));
                if( buffer[0] != '\0' ){
                    sscanf(buffer,"%d",&requiredlevel);
                }

                const int maxBoxAtThisLevel = 1 << requiredlevel;
                std::cout << "    At level "<< requiredlevel << " there is a grid of [" << maxBoxAtThisLevel << " x " << maxBoxAtThisLevel << " x " << maxBoxAtThisLevel << "] boxes\n";

                int x,y,z;
                do{
                    std::cout << "    Tapes x y z = ";
                    std::cin.getline( buffer , sizeof(buffer));
                }while(sscanf(buffer,"%d %d %d",&x,&y,&z) != 3);

                FTreeCoordinate coord(x,y,z);
115
                const MortonIndex index = coord.getMortonIndex() ;
116
                std::cout << "    Morton Index is " << index << " \t " << std::hex << index << "H \t " << MortonToBinary(index,requiredlevel) << "D\n\n";
berenger-bramas's avatar
berenger-bramas committed
117 118 119 120 121 122 123 124 125 126 127 128 129 130
            }
            break;
        case 2: /////////////////////////////////////////////////////////////////
            {
                std::cout << "-- Morton index from real position :\n";
                std::cout << "    You will now give the position in the tree and the level to compute morton index.\n";

                int requiredlevel = treeLevel - 1;
                std::cout << "    level (default is leaf level = " << (treeLevel-1) << ") : ";
                std::cin.getline( buffer , sizeof(buffer));
                if( buffer[0] != '\0' ){
                    sscanf(buffer,"%d",&requiredlevel);
                }

131 132
                FReal boxWidthAtThisLevel = rootBoxWidth;
                for(int idx = 0 ; idx < requiredlevel ; ++idx) boxWidthAtThisLevel /= FReal(2.0);
berenger-bramas's avatar
berenger-bramas committed
133 134
                std::cout << "    At level "<< requiredlevel << " boxes width is " << boxWidthAtThisLevel << "\n";

135
                float x,y,z;
berenger-bramas's avatar
berenger-bramas committed
136 137 138 139 140 141 142
                do{
                    std::cout << "    Tapes x y z = ";
                    std::cin.getline( buffer , sizeof(buffer));
                }while(sscanf(buffer,"%f %f %f",&x,&y,&z) != 3);

                FTreeCoordinate host;
                // position has to be relative to corner not center
143 144 145
                host.setX( int(FMath::dfloor(( FReal(x) - centerOfBox.getX() - rootBoxWidth/2) / boxWidthAtThisLevel ) ));
                host.setY( int(FMath::dfloor(( FReal(y) - centerOfBox.getY() - rootBoxWidth/2) / boxWidthAtThisLevel ) ));
                host.setZ( int(FMath::dfloor(( FReal(z) - centerOfBox.getZ() - rootBoxWidth/2) / boxWidthAtThisLevel ) ));
berenger-bramas's avatar
berenger-bramas committed
146

147
                const MortonIndex index = host.getMortonIndex();
148
                std::cout << "    Morton Index is " << index << " \t " << std::hex << index << "h \t " << MortonToBinary(index,requiredlevel) << "d\n\n";
berenger-bramas's avatar
berenger-bramas committed
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181
            }
            break;
        case 3: /////////////////////////////////////////////////////////////////
            {
                std::cout << "-- Morton for an interval :\n";
                std::cout << "    You will now give 2 positions and the level.\n";

                int requiredlevel = treeLevel - 1;
                std::cout << "    level (default is leaf level = " << (treeLevel-1) << ") : ";
                std::cin.getline( buffer , sizeof(buffer));
                if( buffer[0] != '\0' ){
                    sscanf(buffer,"%d",&requiredlevel);
                }

                const int maxBoxAtThisLevel = 1 << requiredlevel;
                std::cout << "    At level "<< requiredlevel << " there is a grid of [" << maxBoxAtThisLevel << " x " << maxBoxAtThisLevel << " x " << maxBoxAtThisLevel << "] boxes\n";

                int sx,sy,sz;
                do{
                    std::cout << "    Tapes start index x y z = ";
                    std::cin.getline( buffer , sizeof(buffer));
                }while(sscanf(buffer,"%d %d %d",&sx,&sy,&sz) != 3);

                int ex,ey,ez;
                do{
                    std::cout << "    Tapes end index x y z = ";
                    std::cin.getline( buffer , sizeof(buffer));
                }while(sscanf(buffer,"%d %d %d",&ex,&ey,&ez) != 3);

                for(int z = sz ; z <= ez ; ++z){
                    for(int y = sy ; y <= ey ; ++y){
                        for(int x = sx ; x <= ex ; ++x){
                            FTreeCoordinate coord(x,y,z);
182
                            const MortonIndex index = coord.getMortonIndex();
berenger-bramas's avatar
berenger-bramas committed
183
                            std::cout << "[x = " << x << " y = " << y << " z = " << z << "]\n";
184
                            std::cout << "    Morton Index is " << index << " \t " << std::hex << index << "H \t " << MortonToBinary(index,requiredlevel) << "D\n\n";
berenger-bramas's avatar
berenger-bramas committed
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207
                        }
                    }
                }
            }
            break;
        case 4: /////////////////////////////////////////////////////////////////
            {
                std::cout << "-- Morton for a level :\n";
                std::cout << "    You will now give 2 positions and the level.\n";

                int requiredlevel = treeLevel - 1;
                std::cout << "    level (default is leaf level = " << (treeLevel-1) << ") : ";
                std::cin.getline( buffer , sizeof(buffer));
                if( buffer[0] != '\0' ){
                    sscanf(buffer,"%d",&requiredlevel);
                }

                const int maxBoxAtThisLevel = 1 << requiredlevel;

                for(int z = 0 ; z < maxBoxAtThisLevel ; ++z){
                    for(int y = 0 ; y < maxBoxAtThisLevel ; ++y){
                        for(int x = 0 ; x < maxBoxAtThisLevel ; ++x){
                            FTreeCoordinate coord(x,y,z);
208
                            const MortonIndex index = coord.getMortonIndex();
berenger-bramas's avatar
berenger-bramas committed
209
                            std::cout << "[x = " << x << " y = " << y << " z = " << z << "]\n";
210
                            std::cout << "    Morton Index is " << index << " \t " << std::hex << index << "H \t " << MortonToBinary(index,requiredlevel) << "D\n\n";
berenger-bramas's avatar
berenger-bramas committed
211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226
                        }
                    }
                }
            }
            break;
        case 5: /////////////////////////////////////////////////////////////////
            {
                std::cout << "-- Change properties :\n";
                std::cout << "    You will now give tree height, the center of the box & the box width.\n";

                std::cout << "    height (default is = " << treeLevel << ") : ";
                std::cin.getline( buffer , sizeof(buffer));
                if( buffer[0] != '\0' ){
                    sscanf(buffer,"%d",&treeLevel);
                }

227
                float x,y,z;
berenger-bramas's avatar
berenger-bramas committed
228 229 230 231
                do{
                    std::cout << "    Center of boxe Tapes x y z = ";
                    std::cin.getline( buffer , sizeof(buffer));
                }while(sscanf(buffer,"%f %f %f",&x,&y,&z) != 3);
232
                centerOfBox = FPoint<FReal>(FReal(x),FReal(y),FReal(z));
berenger-bramas's avatar
berenger-bramas committed
233 234 235 236

                std::cout << "    boxe width (default is = " << rootBoxWidth << ") : ";
                std::cin.getline( buffer , sizeof(buffer));
                if( buffer[0] != '\0' ){
237 238 239
                    float frootBoxWidth = 0;
                    sscanf(buffer,"%f",&frootBoxWidth);
                    rootBoxWidth = FReal(frootBoxWidth);
berenger-bramas's avatar
berenger-bramas committed
240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262
                }

                std::cout << "\n";
            }
            break;
        case 6: /////////////////////////////////////////////////////////////////
            {
                std::cout << "-- Morton index to position :\n";
                std::cout << "    You will now give the morton index and the level.\n";

                int requiredlevel;
                do{
                    std::cout << "    level = ";
                    std::cin.getline( buffer , sizeof(buffer));
                }while(sscanf(buffer,"%d",&requiredlevel) != 1);

                MortonIndex index;
                do{
                    std::cout << "    index = ";
                    std::cin.getline( buffer , sizeof(buffer));
                }while(sscanf(buffer,"%lld",&index) != 1);

                FTreeCoordinate coord;
263
                coord.setPositionFromMorton(index);
berenger-bramas's avatar
berenger-bramas committed
264 265 266

                std::cout << "    This position is in the boxe x = "<< coord.getX() << " y = " << coord.getY() << " z = " << coord.getZ() << "\n";

267 268
                FReal boxWidthAtThisLevel = rootBoxWidth;
                for(int idx = 0 ; idx < requiredlevel ; ++idx) boxWidthAtThisLevel /= FReal(2.0);
berenger-bramas's avatar
berenger-bramas committed
269
                std::cout << "    This center of this boxe is"
270 271 272
                        << " x = " << (FReal(coord.getX())*boxWidthAtThisLevel) + centerOfBox.getX() + boxWidthAtThisLevel/FReal(2.0) - rootBoxWidth/FReal(2.0)
                        << " y = " << (FReal(coord.getY())*boxWidthAtThisLevel) + centerOfBox.getX() + boxWidthAtThisLevel/FReal(2.0) - rootBoxWidth/FReal(2.0)
                        << " z = " << (FReal(coord.getZ())*boxWidthAtThisLevel) + centerOfBox.getX() + boxWidthAtThisLevel/FReal(2.0) - rootBoxWidth/FReal(2.0) << "\n\n";
berenger-bramas's avatar
berenger-bramas committed
273 274 275 276 277 278 279 280 281
            }
            break;
        default:
            ;
        }
    }

    return 0;
}