testOctreePrintMorton.cpp 13.3 KB
Newer Older
1
// ===================================================================================
2
3
4
5
6
// Copyright ScalFmm 2011 INRIA, Olivier Coulaud, Bérenger Bramas, Matthias Messner
// olivier.coulaud@inria.fr, berenger.bramas@inria.fr
// This software is a computer program whose purpose is to compute the FMM.
//
// This software is governed by the CeCILL-C and LGPL licenses and
7
8
// abiding by the rules of distribution of free software.
//
9
10
11
12
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public and CeCILL-C Licenses for more details.
13
// "http://www.cecill.info".
14
// "http://www.gnu.org/licenses".
15
// ===================================================================================
berenger-bramas's avatar
berenger-bramas committed
16
17
18

#include <iostream>

19
20
#include <cstdio>
#include <cstdlib>
berenger-bramas's avatar
berenger-bramas committed
21
22
23
#include <time.h>
#include <string>

24
25
#include "../../Src/Utils/FGlobal.hpp"
#include "../../Src/Containers/FTreeCoordinate.hpp"
BRAMAS Berenger's avatar
BRAMAS Berenger committed
26
#include "../../Src/Utils/FPoint.hpp"
27
#include "../../Src/Utils/FMath.hpp"
28
#include "../../Src/Utils/FParameterNames.hpp"
berenger-bramas's avatar
berenger-bramas committed
29
30
31
32
33
34
35
36
37

/**
* 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 */
38
std::string MortonToBinary(MortonIndex index, int level){
berenger-bramas's avatar
berenger-bramas committed
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
    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;
}


56
57
58
59
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");
60
61
62
63
    ///////////////////////What we do/////////////////////////////
    std::cout << ">> This executable has to be used to test morton index.\n";
    //////////////////////////////////////////////////////////////

64
65
    typedef double FReal;

berenger-bramas's avatar
berenger-bramas committed
66
67
68
69
    bool stop = false;
    char buffer[256];

    int treeLevel = 10;
70
    FPoint<FReal> centerOfBox(0.5,0.5,0.5);
71
    FReal rootBoxWidth = 1;
berenger-bramas's avatar
berenger-bramas committed
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128

    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);
129
                const MortonIndex index = coord.getMortonIndex() ;
130
                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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
            }
            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);
                }

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

149
                float x,y,z;
berenger-bramas's avatar
berenger-bramas committed
150
151
152
153
154
155
156
                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
157
158
159
                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
160

161
                const MortonIndex index = host.getMortonIndex();
162
                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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
            }
            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);
196
                            const MortonIndex index = coord.getMortonIndex();
berenger-bramas's avatar
berenger-bramas committed
197
                            std::cout << "[x = " << x << " y = " << y << " z = " << z << "]\n";
198
                            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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
                        }
                    }
                }
            }
            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);
222
                            const MortonIndex index = coord.getMortonIndex();
berenger-bramas's avatar
berenger-bramas committed
223
                            std::cout << "[x = " << x << " y = " << y << " z = " << z << "]\n";
224
                            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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
                        }
                    }
                }
            }
            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);
                }

241
                float x,y,z;
berenger-bramas's avatar
berenger-bramas committed
242
243
244
245
                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);
246
                centerOfBox = FPoint<FReal>(FReal(x),FReal(y),FReal(z));
berenger-bramas's avatar
berenger-bramas committed
247
248
249
250

                std::cout << "    boxe width (default is = " << rootBoxWidth << ") : ";
                std::cin.getline( buffer , sizeof(buffer));
                if( buffer[0] != '\0' ){
251
252
253
                    float frootBoxWidth = 0;
                    sscanf(buffer,"%f",&frootBoxWidth);
                    rootBoxWidth = FReal(frootBoxWidth);
berenger-bramas's avatar
berenger-bramas committed
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
                }

                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;
277
                coord.setPositionFromMorton(index);
berenger-bramas's avatar
berenger-bramas committed
278
279
280

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

281
282
                FReal boxWidthAtThisLevel = rootBoxWidth;
                for(int idx = 0 ; idx < requiredlevel ; ++idx) boxWidthAtThisLevel /= FReal(2.0);
berenger-bramas's avatar
berenger-bramas committed
283
                std::cout << "    This center of this boxe is"
284
285
286
                        << " 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
287
288
289
290
291
292
293
294
295
            }
            break;
        default:
            ;
        }
    }

    return 0;
}