FTreeCoordinate.hpp 4.9 KB
Newer Older
1
// ===================================================================================
2 3 4 5 6 7 8 9
// Logiciel initial: ScalFmm Version 0.5
// Co-auteurs : Olivier Coulaud, Bérenger Bramas.
// Propriétaires : INRIA.
// Copyright © 2011-2012, diffusé sous les termes et conditions d’une licence propriétaire.
// Initial software: ScalFmm Version 0.5
// Co-authors: Olivier Coulaud, Bérenger Bramas.
// Owners: INRIA.
// Copyright © 2011-2012, spread under the terms and conditions of a proprietary license.
10
// ===================================================================================
11 12
#ifndef FTREECOORDINATE_HPP
#define FTREECOORDINATE_HPP
13

14

15
#include "../Utils/FGlobal.hpp"
16 17 18 19 20 21 22 23 24 25 26 27

/**
* @author Berenger Bramas (berenger.bramas@inria.fr)
* @class FTreeCoordinate
* Please read the license
*
* This class represents tree coordinate. It is used to save
* the position in "box unit" (not system/space unit!).
* It is directly related to morton index
*/
class FTreeCoordinate{
private:
28 29 30
    int x;	//< x box-th position
    int y;	//< y box-th position
    int z;	//< z box-th position
31 32 33 34 35 36 37 38 39 40 41 42

public:	
    /** Default constructor (position = {0,0,0})*/
    FTreeCoordinate() : x(0), y(0), z(0){
    }

    /**
        * Default constructor
        * @param inX the x
        * @param inY the y
        * @param inZ the z
        */
43
    explicit FTreeCoordinate(const int inX,const int inY,const int inZ)
44 45 46 47 48 49 50 51 52 53
        : x(inX), y(inY), z(inZ){
    }

    /**
	* Copy constructor
	* @param other the source class to copy
	*/
    FTreeCoordinate(const FTreeCoordinate& other):x(other.x), y(other.y), z(other.z){
    }

berenger-bramas's avatar
berenger-bramas committed
54 55 56 57 58 59 60 61
    /**
        * Copy constructor
        * @param other the source class to copy
        */
    FTreeCoordinate(const FTreeCoordinate& other, const int inOffset)
        : x(other.x + inOffset), y(other.y + inOffset), z(other.z + inOffset){
    }

62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
    /**
	* Copy constructor
	* @param other the source class to copy
	* @return this a reference to the current object
	*/
    FTreeCoordinate& operator=(const FTreeCoordinate& other){
        this->x = other.x;
        this->y = other.y;
        this->z = other.z;
        return *this;
    }

    /**
	* Position setter
        * @param inX the new x
        * @param inY the new y
        * @param inZ the new z
	*/
80
    void setPosition(const int inX,const int inY,const int inZ){
81 82 83 84 85 86 87 88 89
        this->x = inX;
        this->y = inY;
        this->z = inZ;
    }

    /**
	* X Getter
	* @return this->x
	*/
90
    int getX() const{
91 92 93 94 95 96 97
        return this->x;
    }

    /**
	* Y Getter
	* @return this->y
	*/
98
    int getY() const{
99 100 101 102 103 104 105
        return this->y;
    }

    /**
	* Z Getter
	* @return this->z
	*/
106
    int getZ() const{
107 108 109 110 111 112 113
        return this->z;
    }

    /**
	* X Setter, simply change x position
	* @param the new x
	*/
114
    void setX(const int inX){
115 116 117 118 119 120 121
        this->x = inX;
    }

    /**
	* Y Setter, simply change y position
	* @param the new y
	*/
122
    void setY(const int inY){
123 124 125 126 127 128 129
        this->y = inY;
    }

    /**
	* Z Setter, simply change z position
	* @param the new z
	*/
130
    void setZ(const int inZ){
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 167 168 169 170 171 172 173 174 175
        this->z = inZ;
    }

    /**
	* To get the morton index of the current position
	* @complexity inLevel
	* @param inLevel the level of the component
	* @return morton index
	*/
    MortonIndex getMortonIndex(const int inLevel) const{
        MortonIndex index = 0x0LL;
        MortonIndex mask = 0x1LL;
        // the ordre is xyz.xyz...
        MortonIndex mx = this->x << 2;
        MortonIndex my = this->y << 1;
        MortonIndex mz = this->z;

        for(int indexLevel = 0; indexLevel < inLevel ; ++indexLevel){
            index |= (mz & mask);
            mask <<= 1;
            index |= (my & mask);
            mask <<= 1;
            index |= (mx & mask);
            mask <<= 1;

            mz <<= 2;
            my <<= 2;
            mx <<= 2;
        }

        return index;
    }

    /** This function set the position of the current object using a morton index
          * @param inIndex the morton index to compute position
          * @param the level of the morton index
          */
    void setPositionFromMorton(MortonIndex inIndex, const int inLevel){
        MortonIndex mask = 0x1LL;

        this->x = 0;
        this->y = 0;
        this->z = 0;

        for(int indexLevel = 0; indexLevel < inLevel ; ++indexLevel){
176
            z |= int(inIndex & mask);
177
            inIndex >>= 1;
178
            y |= int(inIndex & mask);
179
            inIndex >>= 1;
180
            x |= int(inIndex & mask);
181 182 183 184 185 186 187 188 189 190

            mask <<= 1;
        }

    }

    /** Test equal operator
          * @param other the coordinate to compare
          * @return true if other & current object have same position
          */
191
    bool operator==(const FTreeCoordinate& other) const {
192 193
        return x == other.x && y == other.y && z == other.z;
    }
194 195 196 197 198 199 200

    /** To test difference
      *
      */
    bool operator!=(const FTreeCoordinate& other) const{
        return x != other.x || y != other.y || z != other.z;
    }
201 202 203 204 205 206
};



#endif //FTREECOORDINATE_HPP

207