FPoint.hpp 8.23 KB
Newer Older
1
// ===================================================================================
2 3 4 5 6 7 8
// 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.
9
// Copyright © 2011-2012, spread under the terms and conditions of a proprietardata[1] license.
10
// ===================================================================================
BRAMAS Berenger's avatar
BRAMAS Berenger committed
11 12
#ifndef FPOINT_HPP
#define FPOINT_HPP
13

14 15 16

// To get memcpy
#include <cstring>
berenger-bramas's avatar
berenger-bramas committed
17
#include <iostream>
18

COULAUD Olivier's avatar
COULAUD Olivier committed
19
#include "FMath.hpp"
20
#include "FGlobal.hpp"
21 22
#include "../Containers/FBufferReader.hpp"
#include "../Containers/FBufferWriter.hpp"
23 24 25

/**
* @author Berenger Bramas (berenger.bramas@inria.fr)
BRAMAS Berenger's avatar
BRAMAS Berenger committed
26
* @class FPoint
27 28 29 30 31
* Please read the license
*
* This class is a 3D vector. It can be used as a position
* or as a 3d forces vector etc.
*/
BRAMAS Berenger's avatar
BRAMAS Berenger committed
32
class FPoint {
33
private:
BRAMAS Berenger's avatar
BRAMAS Berenger committed
34
    FReal data[3]; //< all positions x y z
35 36

public:	
37
    /** Default constructor (sets position to 0/0/0) */
BRAMAS Berenger's avatar
BRAMAS Berenger committed
38
    FPoint(){
39
        data[0] = data[1] = data[2] = FReal(0);
40 41 42
    }

    /** Constructor from values */
BRAMAS Berenger's avatar
BRAMAS Berenger committed
43
    explicit FPoint(const FReal inX,const FReal inY,const FReal inZ){
44 45 46
        data[0] = inX;
        data[1] = inY;
        data[2] = inZ;
47 48 49
    }

    /**
50
 * Copdata[1] constructor
51 52
 * @param other the source class to copy
 */
BRAMAS Berenger's avatar
BRAMAS Berenger committed
53
    FPoint(const FPoint& other) {
54 55 56
        data[0] = other.data[0];
        data[1] = other.data[1];
        data[2] = other.data[2];
57 58 59
    }

    /**
60 61 62
        * Assignement operator
        * @param other the source class to copy
        */
BRAMAS Berenger's avatar
BRAMAS Berenger committed
63
    FPoint(const FPoint& other, const FReal addset) {
64 65 66
        data[0] = other.data[0] + addset;
        data[1] = other.data[1] + addset;
        data[2] = other.data[2] + addset;
67 68 69
    }

    /**
70
 * Copdata[1] constructor
71 72 73
 * @param other the source class to copy
 * @return this a reference to the current class
 */
BRAMAS Berenger's avatar
BRAMAS Berenger committed
74
    FPoint& operator=(const FPoint& other){
75 76 77
        this->data[0] = other.data[0];
        this->data[1] = other.data[1];
        this->data[2] = other.data[2];
78 79 80 81 82 83 84 85 86
        return *this;
    }

    /**
 * Position setter
 * @param other the source class to copy
 * @return this a reference to the current class
 */
    void setPosition(const FReal inX,const FReal inY,const FReal inZ){
87 88 89
        this->data[0] = inX;
        this->data[1] = inY;
        this->data[2] = inZ;
90 91 92 93
    }

    /**
 * Get x
94
 * @return this->data[0]
95 96
 */
    FReal getX() const{
97
        return this->data[0];
98 99 100 101
    }

    /**
 * Get y
102
 * @return this->data[1]
103 104
 */
    FReal getY() const{
105
        return this->data[1];
106 107 108 109
    }

    /**
 * Get z
110
 * @return this->data[2]
111 112
 */
    FReal getZ() const{
113
        return this->data[2];
114 115 116 117 118 119 120
    }

    /**
 * Set x
 * @param the new x
 */
    void setX(const FReal inX){
121
        this->data[0] = inX;
122 123 124 125 126 127 128
    }

    /**
 * Set y
 * @param the new y
 */
    void setY(const FReal inY){
129
        this->data[1] = inY;
130 131 132 133 134 135 136
    }

    /**
 * Set z
 * @param the new z
 */
    void setZ(const FReal inZ){
137
        this->data[2] = inZ;
138 139 140
    }

    /**
COULAUD Olivier's avatar
COULAUD Olivier committed
141 142
 * Add to the x-dimension the inX value
 * @param  inXthe increment in x
143 144
 */
    void incX(const FReal inX){
145
        this->data[0] += inX;
146 147 148
    }

    /**
COULAUD Olivier's avatar
COULAUD Olivier committed
149 150
 *  Add to the y-dimension the inY value
 * @param  in<<<<<<y the increment in y
151 152
 */
    void incY(const FReal inY){
153
        this->data[1] += inY;
154 155 156
    }

    /**
COULAUD Olivier's avatar
COULAUD Olivier committed
157 158
 *  Add to z-dimension the inZ value
 * @param inZ the increment in z
159 160
 */
    void incZ(const FReal inZ){
161
        this->data[2] += inZ;
162
    }
COULAUD Olivier's avatar
COULAUD Olivier committed
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177
    /**
      * Get a pointer on the coordinate of FPoint
      * @return the data value array
     */
    FReal * getDataValue(){
        return this->data ;
    }
  /**
    *Compute the distance to the origin
    * @return the norm of the Fpoint
    */
    FReal norm() const {
        return FMath::Sqrt(this->data[0]*this->data[0]+this->data[1]*this->data[1]
                        +this->data[2]*this->data[2]) ;
    }
178 179 180 181 182 183

    /**
 * Subtract to all dim the inValue
 * @param inValue the value to substract
 * @return the current object after being subtracted
 */
BRAMAS Berenger's avatar
BRAMAS Berenger committed
184
    FPoint& operator-=(const FReal inValue){
185 186 187
        this->data[0] -= inValue;
        this->data[1] -= inValue;
        this->data[2] -= inValue;
188 189 190 191 192 193 194 195
        return *this;
    }

    /**
 * Affect to all dim the inValue
 * @param inValue the value to afect
 * @return the current object after being affected
 */
BRAMAS Berenger's avatar
BRAMAS Berenger committed
196
    FPoint& operator+=(const FReal inValue){
197 198 199
        this->data[0] += inValue;
        this->data[1] += inValue;
        this->data[2] += inValue;
200 201 202 203
        return *this;
    }

    /**
204 205 206 207
        * Subtract to all dim the other position
        * @param other the value to substract
        * @return the current object after being subtracted
        */
BRAMAS Berenger's avatar
BRAMAS Berenger committed
208
    FPoint& operator-=(const FPoint& other){
209 210 211
        this->data[0] -= other.data[0];
        this->data[1] -= other.data[1];
        this->data[2] -= other.data[2];
212 213 214 215
        return *this;
    }

    /**
216 217 218 219
        * Affect to all dim the other position
        * @param other the value to afect
        * @return the current object after being affected
        */
BRAMAS Berenger's avatar
BRAMAS Berenger committed
220
    FPoint& operator+=(const FPoint& other){
221 222 223
        this->data[0] += other.data[0];
        this->data[1] += other.data[1];
        this->data[2] += other.data[2];
224 225 226 227
        return *this;
    }

    /**
228 229 230 231
        * Affect to all dim the other position
        * @param other the value to afect
        * @return the current object after being affected
        */
BRAMAS Berenger's avatar
BRAMAS Berenger committed
232
    FPoint& operator*=(const FReal value){
233 234 235
        this->data[0] *= value;
        this->data[1] *= value;
        this->data[2] *= value;
236 237 238 239
        return *this;
    }

    /**
240 241 242 243 244 245
        * Operator F3Position minus FReal
        * This substract inValue to all dimensions of the inPosition
        * @param inPosition the position to compute
        * @param inValue the value to decrease/substract position
        * @return the resulting position
        */
BRAMAS Berenger's avatar
BRAMAS Berenger committed
246 247
    friend inline FPoint operator-(const FPoint& inPosition, const FReal inValue){
        return FPoint(inPosition, -inValue);
248
    }
249

250
    /**
251 252 253 254 255 256
        * Operator F3Position plus FReal
        * This affect from inValue all dimensions of the inPosition
        * @param inPosition the position to compute
        * @param inValue the value to increase/affect position
        * @return the resulting position
        */
BRAMAS Berenger's avatar
BRAMAS Berenger committed
257 258
    friend inline FPoint operator+(const FPoint& inPosition, const FReal inValue){
        return FPoint(inPosition, inValue);
259
    }
260

261
    /**
262 263 264 265 266 267
        * Operator F3Position minus F3Position
        * This substract one from anther
        * @param inPosition the position to reduce
        * @param inOther the position to decrease/substract inPosition
        * @return the resulting position
        */
BRAMAS Berenger's avatar
BRAMAS Berenger committed
268 269
    friend inline FPoint operator-(const FPoint& inPosition, const FPoint& inOther){
        return FPoint(inPosition.data[0] - inOther.data[0], inPosition.data[1] - inOther.data[1], inPosition.data[2] - inOther.data[2]);
270
    }
271

272
    /**
273 274 275 276 277 278
        * Operator F3Position plus F3Position
        * This substract one from anther
        * @param inPosition the position to reduce
        * @param inOther the position to increase inPosition
        * @return the resulting position
        */
BRAMAS Berenger's avatar
BRAMAS Berenger committed
279 280
    friend inline FPoint operator+(const FPoint& inPosition, const FPoint& inOther){
        return FPoint(inPosition.data[0] + inOther.data[0], inPosition.data[1] + inOther.data[1], inPosition.data[2] + inOther.data[2]);
281
    }
messner's avatar
messner committed
282

berenger-bramas's avatar
berenger-bramas committed
283
    /**
BRAMAS Berenger's avatar
BRAMAS Berenger committed
284
     * Operator stream FPoint to std::ostream
285
     * This can be used to simpldata[1] write out a position
berenger-bramas's avatar
berenger-bramas committed
286 287 288 289
     * @param[in,out] output where to write the position
     * @param[in] inPosition the position to write out
     * @return the output for multiple << operators
     */
290 291
    template <class StreamClass>
    friend StreamClass& operator<<(StreamClass& output, const FPoint& inPosition){
berenger-bramas's avatar
berenger-bramas committed
292 293 294
        output << "(" <<  inPosition.getX() << ", " << inPosition.getY() << ", " << inPosition.getZ() <<")";
        return output;  // for multiple << operators.
    }
295 296 297

    /** Save current object */
    void save(FBufferWriter& buffer) const {
298
        buffer << data[0] << data[1] << data[2];
299 300 301
    }
    /** Retrieve current object */
    void restore(FBufferReader& buffer) {
302
        buffer >> data[0] >> data[1] >> data[2];
303
    }
304 305 306
};


307

308

BRAMAS Berenger's avatar
BRAMAS Berenger committed
309
#endif //FPOINT_HPP
310

311