FSubOctree.hpp 18 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#ifndef FSUBOCTREE_HPP
#define FSUBOCTREE_HPP
// /!\ Please, you must read the license at the bottom of this page

#include "../Utils/FGlobal.hpp"
#include "../Utils/F3DPosition.hpp"
#include "../Utils/FAssertable.hpp"
#include "../Utils/FMath.hpp"
#include "../Utils/FConvert.hpp"

#include "FTreeCoordinate.hpp"
#include "FList.hpp"


/**
 * @author Berenger Bramas (berenger.bramas@inria.fr)
 * @class FAbstractSubOctree
 * Please read the license
 *
 * This class is a sub-octree container.
 * This class is the main component of the octree.
 * 
 * This class does not have a main root. In fact, the root
 * is contained in the parent sub-octree.
 *
 * If the sub-octree is a middle suboctree, the leaf level is pointers
 * on other suboctrees.
 *
 * If the sub-octree is a bottom subtree then the leaf level is pointers on
 * lists<particle>
 *
 * This two situations are implemented in two different classes that inherite of FAbstractSubOctree.
 *
 * Please refere to testOctree.cpp to see an example
 * @warning Give the particleClass & cellClass
 */
template< class ParticleClass, class CellClass , template <class ParticleClass> class LeafClass>
class FAbstractSubOctree : protected FAssertable{
protected:

    CellClass*** cells;		            //< Potential cells, cells are allocated only if needed
    FAbstractSubOctree* const parent;       //< Parent suboctree (null for root)

    const long indexInParent;               //< This is the index of the current octree in the parent's array

    long leftLeafIndex;                     //< The leaf at the left position (this is the array index to start when iterate)
    long rightLeafIndex;                    //< The leaf at the right position (this is the last array index when iterate)

    const int subOctreeHeight;              //< Height of this suboctree
    const int subOctreePosition;	    //< Level of the current suboctree in the global tree (0 if node)


    /**
     * This function compute the morton index for the last level of this suboctree.
     * suppose we have an index like : 000.010.111.001.101.110
     * and now considere that suboctree's height is 2, the morton index have to be cut as :
     * [000.010].[111.001].[101.110] each part correspond to a morton index a leaf level for the
     * different suboctrees.
     * This is why depending on the level of the octree we need to remove extra part on the left and
     * on the right.
     */
    MortonIndex getLeafIndex(const MortonIndex index, const int inTreeHeight) const {
        // Remove right useless part - used by child
        const MortonIndex fullIndex = index >> (3 * (inTreeHeight - (this->subOctreeHeight + this->subOctreePosition) ) );
        // Remove left extra data part - used by parent
        const MortonIndex treeLeafMask = ~(~0x00LL << (3 *  this->subOctreeHeight ));
        return treeLeafMask & fullIndex;
    }

    /**
     * This function creats all the intermediates cells between
     * a leaf and the root.
     * It is used after inserting a new leaf to have cells from leaf to root
     * when computing
     * @param arrayIndex the index at the leaf index of the new element
     */
    void createPreviousCells(MortonIndex arrayIndex, MortonIndex inLeafCellIndex, const FReal* const inBoxWidthAtLevel){
        int indexLevel = this->subOctreeHeight - 1;
        while(indexLevel >= 0 && !this->cells[indexLevel][arrayIndex]){
            this->cells[indexLevel][arrayIndex] = new CellClass();
            this->cells[indexLevel][arrayIndex]->setMortonIndex(inLeafCellIndex);

            const int realLevel = indexLevel + this->getSubOctreePosition();
            this->cells[indexLevel][arrayIndex]->setPosition(FConvert::MortonToPosition(inLeafCellIndex,realLevel,inBoxWidthAtLevel[realLevel]));

            --indexLevel;

            arrayIndex >>= 3;
            inLeafCellIndex >>= 3;
        }
    }

    /**
      * This function is initializing variables when a new leaf is inserted in the tree
      * for example it updates the leaf array marges and calls createPreviousCells()
      * @param arrayIndex the position of the new leaf in the leafs array
      */
    void newLeafInserted(const long arrayIndex, const MortonIndex inLeafCellIndex, const FReal* const inBoxWidthAtLevel){
        createPreviousCells(arrayIndex,inLeafCellIndex, inBoxWidthAtLevel);
        // Update if this is the bottom left
        if(arrayIndex < this->leftLeafIndex) this->leftLeafIndex = arrayIndex;
        if(arrayIndex > this->rightLeafIndex) this->rightLeafIndex = arrayIndex;
    }

    /** Disable copy */
private:
    FAbstractSubOctree(const FAbstractSubOctree&){}
    FAbstractSubOctree& operator=(const FAbstractSubOctree&){return *this;}

public:
    /**
    * Constructor
    * Allocate the cells arrays to be able to create every potential cells
    * @param inParent the SubOctree parent (0 if node)
    * @param inSubOctreeHeight Height of this suboctree
    * @param inSubOctreePosition Level of the current suboctree in the global tree (1 if upper tree)
    */
    FAbstractSubOctree(FAbstractSubOctree* const inParent, const long inIndexInParent,
                       const int inSubOctreeHeight, const int inSubOctreePosition) :
                        cells(0), parent( inParent ), indexInParent(inIndexInParent), leftLeafIndex(1 << (3 * inSubOctreeHeight)), rightLeafIndex(-1),
                        subOctreeHeight( inSubOctreeHeight ), subOctreePosition( inSubOctreePosition ) {

        this->cells = new CellClass**[this->subOctreeHeight];
        assert(this->cells, "Allocation failled", __LINE__, __FILE__);

        // We start at a sub-level - 8^1
        long cellsAtlevel = 8;
        for( int indexLevel = 0 ; indexLevel < this->subOctreeHeight ; ++indexLevel ){
            this->cells[indexLevel] = new CellClass*[cellsAtlevel];
            assert(this->cells[indexLevel], "Allocation failled", __LINE__, __FILE__);

            for( int indexCells = 0 ; indexCells < cellsAtlevel ; ++indexCells ){
                this->cells[indexLevel][indexCells] = 0;
            }

            cellsAtlevel <<= 3; // => * 8 >> 8^indexLevel
        }
    }

    /**
    * Destructor
    * Delete cells arrays and allocated cells
    */
    virtual ~FAbstractSubOctree(){
        long cellsAtlevel = 8;
        for( int indexLevel = 0 ; indexLevel < this->subOctreeHeight ; ++indexLevel ){
            for( int indexCells = 0 ; indexCells < cellsAtlevel ; ++indexCells ){
                if(this->cells[indexLevel][indexCells]){
                    delete this->cells[indexLevel][indexCells];
                }
            }

            delete [] this->cells[indexLevel];
            cellsAtlevel <<= 3; // => * 8 >> 8^indexLevel
        }

        delete [] this->cells;
    }


    /**
    * Insert a particle on the subtree
    * @param index the morton index of the particle to insert
    * @param inParticle the particle to insert (must inherite from FAbstractParticle)
    * @param inParticle the inTreeHeight the height of the tree
    */
167
    virtual void insert(const MortonIndex index, const ParticleClass& inParticle, const int inTreeHeight, const FReal* const inBoxWidthAtLevel) = 0;
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299

    ///////////////////////////////////////
    // This is the FOctree::Iterator Part
    ///////////////////////////////////////

    /** Suboctree height accessor (leaf level + 1)
      * @return subOctreeHeight */
    int getSubOctreeHeight() const{
        return subOctreeHeight;
    }

    /** Suboctree position in the real tree
      * @return subOctreePosition */
    int getSubOctreePosition() const {
        return subOctreePosition;
    }

    /** Return the more left leaf index
      * the smallest index on the leafs array
      * @return leftLeafIndex */
    long getLeftLeafIndex() const {
        return leftLeafIndex;
    }

    /** Return the more right leaf index
      * the biggest index on the leafs array
      * @return rightLeafIndex */
    long getRightLeafIndex() const {
        return rightLeafIndex;
    }

    /** Return the array of cells at a specious index
      * @param level the level to access cells array (must be < subOctreeHeight)
      * @return cells[level] */
    CellClass** cellsAt(const int level) const{
        assert(level < subOctreeHeight, "Level out of memory", __LINE__, __FILE__);
        return cells[level];
    }

    /** To know if it is the root suboctree
      * @return true if has parent otherwise return false */
    bool hasParent() const {
        return parent;
    }

    /** To get access to the parent suboctree
      * @return parent */
    FAbstractSubOctree* getParent(){
        return parent;
    }

    /** To get access to the parent suboctree (const version)
      * @return parent */
    const FAbstractSubOctree* getParent() const{
        return parent;
    }

    /** To get the index of the current suboctree in the parent leafs array
      * This index can is part of the morton index of the cells contains
      * in this suboctree
      * @return indexInParent */
    long getIndexInParent() const{
        return indexInParent;
    }
};



/////////////////////////////////////////////////////////////////////////////////////
// Last level sub octree
/////////////////////////////////////////////////////////////////////////////////////

/**
 * @author Berenger Bramas (berenger.bramas@inria.fr)
 * @class FSubOctreeWithLeafs
 * Please read the license
 *
 * This class is an sub-octree container.
 * But this is the specialized bottom part, in fact the last level is composed by
 * a cells array (managing by abstract class) and lists of particles.
 *
 * Please refere to testOctree.cpp to see an example.
 * @warning Give the particleClass & cellClass
 */
template< class ParticleClass, class CellClass , template <class ParticleClass> class LeafClass>
class FSubOctreeWithLeafs : public FAbstractSubOctree<ParticleClass,CellClass,LeafClass> {
private:

    LeafClass<ParticleClass>** leafs;            //< Leafs array

    /** Disable copy */
    FSubOctreeWithLeafs(const FSubOctreeWithLeafs&){}
    FSubOctreeWithLeafs& operator=(const FSubOctreeWithLeafs&){return *this;}

public:     
    /**
    * Constructor
    * Allocate the leafs array
    * @param inParent the SubOctree parent (0 if node)
    * @param inSubOctreeHeight Height of this suboctree
    * @param inSubOctreePosition Level of the current suboctree in the global tree (1 if upper tree)
    */
    FSubOctreeWithLeafs(FAbstractSubOctree<ParticleClass,CellClass,LeafClass>* const inParent, const long inIndexInParent,
                        const int inSubOctreeHeight, const int inSubOctreePosition) :
                        FAbstractSubOctree<ParticleClass,CellClass,LeafClass>(inParent, inIndexInParent, inSubOctreeHeight, inSubOctreePosition) {

        const long cellsAtLeafLevel = 1 << (3 * inSubOctreeHeight);

        this->leafs = new LeafClass<ParticleClass>*[cellsAtLeafLevel];
        assert(this->leafs, "Allocation failled", __LINE__, __FILE__);

        for( long indexLeaf = 0 ; indexLeaf < cellsAtLeafLevel ; ++indexLeaf ){
            this->leafs[indexLeaf] = 0;
        }
    }

    /**
    * Destructor dealloc all leafs & the leaf array
    */
    virtual ~FSubOctreeWithLeafs(){
        const int cellsAtLeafLevel = 1 << (3 * this->subOctreeHeight );
        for( int indexLeaf = 0 ; indexLeaf < cellsAtLeafLevel ; ++indexLeaf ){
            if(this->leafs[indexLeaf]){
                delete this->leafs[indexLeaf];
            }
        }
        delete [] this->leafs;
    }

    /**
    * Refer to FAbstractSubOctree::insert
    */
300
    void insert(const MortonIndex index, const ParticleClass& inParticle, const int inTreeHeight, const FReal* const inBoxWidthAtLevel){
301
302
303
304
305
306
307
308
309
310
311
312
313
314
        // Get the morton index for the leaf level
        const MortonIndex arrayIndex = FAbstractSubOctree<ParticleClass,CellClass,LeafClass>::getLeafIndex(index,inTreeHeight);
        // is there already a leaf?
        if( !this->leafs[arrayIndex] ){
            this->leafs[arrayIndex] = new LeafClass<ParticleClass>();
            FAbstractSubOctree<ParticleClass,CellClass,LeafClass>::newLeafInserted( arrayIndex , index, inBoxWidthAtLevel);
        }
        // add particle to leaf list
        this->leafs[arrayIndex]->push(inParticle);
    }

    /** To get access to leafs elements
      * @param index the position of the leaf
      * @return the list of particles at this index */
315
    FList<ParticleClass>* getLeafSrc(const int index){
316
317
318
319
320
321
322
        LeafClass<ParticleClass>* const leaf = this->leafs[index];
        return (leaf ? leaf->getSrc(): 0);
    }

    /** To get access to leafs elements
      * @param index the position of the leaf
      * @return the list of particles at this index */
323
    FList<ParticleClass>* getLeafTargets(const int index){
324
325
326
327
328
329
330
        LeafClass<ParticleClass>* const leaf = this->leafs[index];
        return (leaf ? leaf->getTargets(): 0);
    }

    /** To get access to leafs elements
      * @param index the position of the leaf
      * @return the list of particles at this index */
331
    const FList<ParticleClass>* getLeafSrc(const int index) const {
332
333
334
335
336
337
338
        LeafClass<ParticleClass>* const leaf = this->leafs[index];
        return (leaf ? leaf->getSrc(): 0);
    }

    /** To get access to leafs elements
      * @param index the position of the leaf
      * @return the list of particles at this index */
339
    const FList<ParticleClass>* getLeafTargets(const int index) const {
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
        LeafClass<ParticleClass>* const leaf = this->leafs[index];
        return (leaf ? leaf->getTargets() : 0);
    }

};



/////////////////////////////////////////////////////////////////////////////////////
// Middle level sub octree
/////////////////////////////////////////////////////////////////////////////////////

/**
 * @author Berenger Bramas (berenger.bramas@inria.fr)
 * @class FSubOctree
 * Please read the license
 *
 * This class is an sub-octree container.
 * This is the middle level specialized suboctree, it means that it does not contain
 * leaf but pointers to other suboctree.
 * These suboctrees at the last level can be FSubOctree of FSubOctreeWithLeafs depending
 * if they are at the bottom of the tree or not
 *
 * @warning Give the particleClass & cellClass
 */
template< class ParticleClass, class CellClass , template <class ParticleClass> class LeafClass>
class FSubOctree : public FAbstractSubOctree<ParticleClass,CellClass,LeafClass> {
private:
    FAbstractSubOctree<ParticleClass,CellClass,LeafClass>** subleafs;    //< Last levels is composed of suboctree

    /** Disable copy */
    FSubOctree(const FSubOctree&){}
    FSubOctree& operator=(const FSubOctree&){return *this;}

public:	
    /**
    * Constructor
    * Allocate the subleafs array
    * @param inParent the SubOctree parent (0 if node)
    * @param inSubOctreeHeight Height of this suboctree
    * @param inSubOctreePosition Level of the current suboctree in the global tree (0 if upper tree)
    */
    FSubOctree(FAbstractSubOctree<ParticleClass,CellClass,LeafClass>* const inParent,  const long inIndexInParent,
               const int inSubOctreeHeight, const int inSubOctreePosition) :
            FAbstractSubOctree<ParticleClass,CellClass,LeafClass>(inParent, inIndexInParent, inSubOctreeHeight, inSubOctreePosition) {

        const long cellsAtLeafLevel = 1 << (3 * inSubOctreeHeight);
        this->subleafs = new FAbstractSubOctree<ParticleClass,CellClass,LeafClass>*[cellsAtLeafLevel];
        assert(this->subleafs, "Allocation failled", __LINE__, __FILE__);

        for( int indexLeaf = 0 ; indexLeaf < cellsAtLeafLevel ; ++indexLeaf ){
            this->subleafs[indexLeaf] = 0;
        }
    }

    /**
    * Destructor dealloc all suboctrees leafs & leafs array
    */
    virtual ~FSubOctree(){
        const long cellsAtLeafLevel = 1 << (3 * this->subOctreeHeight);
        for( int indexLeaf = 0 ; indexLeaf < cellsAtLeafLevel ; ++indexLeaf ){
            if(this->subleafs[indexLeaf]) delete this->subleafs[indexLeaf];
        }
        delete [] this->subleafs;
    }

    /**
    * Refer to FAbstractSubOctree::insert
    */
409
    void insert(const MortonIndex index, const ParticleClass& inParticle, const int inTreeHeight, const FReal* const inBoxWidthAtLevel){
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
        // We need the morton index at the bottom level of this sub octree
        // so we remove the right side
        const MortonIndex arrayIndex = FAbstractSubOctree<ParticleClass,CellClass,LeafClass>::getLeafIndex(index,inTreeHeight);
        // Is there already a leaf?
        if( !this->subleafs[arrayIndex] ){
            // We need to create leaf sub octree
            const int nextSubOctreePosition = this->subOctreePosition + this->subOctreeHeight;
            const int nextSubOctreeHeight = FMath::Min(inTreeHeight - nextSubOctreePosition, this->subOctreeHeight);

            // Next suboctree is a middle suboctree
            if(inTreeHeight > nextSubOctreeHeight + nextSubOctreePosition){
                this->subleafs[arrayIndex] = new FSubOctree(this,arrayIndex,nextSubOctreeHeight,nextSubOctreePosition);
            }
            // Or next suboctree contains the reail leaf!
            else{
                this->subleafs[arrayIndex] = new FSubOctreeWithLeafs<ParticleClass,CellClass,LeafClass>(this,arrayIndex,nextSubOctreeHeight,nextSubOctreePosition);
            }

            // We need to inform parent class
            FAbstractSubOctree<ParticleClass,CellClass,LeafClass>::newLeafInserted( arrayIndex, index >> (3 * (inTreeHeight-nextSubOctreePosition) ), inBoxWidthAtLevel);
        }
        // Ask next suboctree to insert the particle
        this->subleafs[arrayIndex]->insert( index, inParticle, inTreeHeight, inBoxWidthAtLevel);
    }

    /** To get access to leafs elements (child suboctree)
      * @param index the position of the leaf/child suboctree
      * @return child at this index */
    FAbstractSubOctree<ParticleClass,CellClass,LeafClass>* leafs(const int index) {
        return this->subleafs[index];
    }

    /** To get access to leafs elements (child suboctree)
      * @param index the position of the leaf/child suboctree
      * @return child at this index */
    const FAbstractSubOctree<ParticleClass,CellClass,LeafClass>* leafs(const int index) const {
        return this->subleafs[index];
    }
};


#endif //FSUBOCTREE_HPP
// [--LICENSE--]